Skip to content

Commit

Permalink
Merge pull request #28 from datachainlab/improve-config-validation
Browse files Browse the repository at this point in the history
Improve prover config validation

Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele authored Jun 21, 2024
2 parents 11101a9 + f1f19bc commit e9082fe
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 16 deletions.
30 changes: 26 additions & 4 deletions relay/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,39 @@ func (pc ProverConfig) Validate() error {
}
if l := len(pc.Operators); l > 1 {
return fmt.Errorf("Operators: currently only one or zero(=permissionless) operator is supported, but got %v", l)
} else if l == 0 {
return nil
}
if pc.OperatorsEip712Params != nil {
if pc.OperatorSigner == nil {
return fmt.Errorf("OperatorSigner must be set if OperatorsEip712Params is set")
}

// ----- operators config validation -----

if pc.OperatorSigner == nil {
return fmt.Errorf("OperatorSigner must be set if Operators or OperatorsEip712Params is set")
}
{
signerConfig, ok := pc.OperatorSigner.GetCachedValue().(signer.SignerConfig)
if !ok {
return fmt.Errorf("failed to cast OperatorSigner's config: %T", pc.OperatorSigner.GetCachedValue())
} else if err := signerConfig.Validate(); err != nil {
return fmt.Errorf("failed to validate the OperatorSigner's config: %v", err)
}
signer, err := signerConfig.Build()
if err != nil {
return fmt.Errorf("failed to build the OperatorSigner: %v", err)
}
addr, err := NewEIP712Signer(signer).GetSignerAddress()
if err != nil {
return fmt.Errorf("failed to get the OperatorSigner's address: %v", err)
}
op, err := decodeOperatorAddress(pc.Operators[0])
if err != nil {
return fmt.Errorf("failed to decode operator address: %v", err)
}
if addr != op {
return fmt.Errorf("OperatorSigner's address must be equal to the first operator's address: %v != %v", addr, op)
}
}
if pc.OperatorsEip712Params != nil {
switch params := pc.OperatorsEip712Params.(type) {
case *ProverConfig_OperatorsEip712EvmChainParams:
if params.OperatorsEip712EvmChainParams.ChainId == 0 {
Expand Down
24 changes: 18 additions & 6 deletions relay/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@ import (
)

func (pr *Prover) IsOperatorEnabled() bool {
return pr.eip712Signer != nil && pr.config.OperatorsEip712Params != nil
return len(pr.config.Operators) > 0
}

func (pr *Prover) GetOperators() ([]common.Address, error) {
var operators []common.Address
for _, operator := range pr.config.Operators {
addrStr := strings.TrimPrefix(operator, "0x")
if len(addrStr) != 40 {
return nil, fmt.Errorf("invalid operator address length %v", len(addrStr))
for i, operator := range pr.config.Operators {
addr, err := decodeOperatorAddress(operator)
if err != nil {
return nil, fmt.Errorf("failed to decode operator address: index=%v, operator=%v %w", i, operator, err)
}
addr := common.HexToAddress(operator)
operators = append(operators, addr)
}
return operators, nil
Expand All @@ -41,6 +40,11 @@ func (pr *Prover) GetOperatorsThreshold() Fraction {
}

func (pr *Prover) updateOperators(counterparty core.Chain, nonce uint64, newOperators []common.Address, threshold Fraction) error {
if !pr.IsOperatorEnabled() {
return fmt.Errorf("operator is not enabled")
} else if pr.config.OperatorsEip712Params == nil {
return fmt.Errorf("operator EIP712 parameters are not set")
}
if nonce == 0 {
return fmt.Errorf("invalid nonce: %v", nonce)
}
Expand Down Expand Up @@ -139,3 +143,11 @@ func (s EIP712Signer) GetSignerAddress() (common.Address, error) {
}
return crypto.PubkeyToAddress(*pubKey), nil
}

func decodeOperatorAddress(s string) (common.Address, error) {
addrStr := strings.TrimPrefix(s, "0x")
if len(addrStr) != 40 {
return common.Address{}, fmt.Errorf("invalid operator address length %v", len(addrStr))
}
return common.HexToAddress(s), nil
}
14 changes: 8 additions & 6 deletions relay/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,6 @@ func (pr *Prover) GetChainID() string {
// These states will be submitted to the counterparty chain as MsgCreateClient.
// If `height` is nil, the latest finalized height is selected automatically.
func (pr *Prover) CreateInitialLightClientState(height exported.Height) (exported.ClientState, exported.ConsensusState, error) {
if res, err := pr.createELC(pr.config.ElcClientId, height); err != nil {
return nil, nil, fmt.Errorf("failed to create ELC: %w", err)
} else if res == nil {
pr.getLogger().Info("no need to create ELC", "elc_client_id", pr.config.ElcClientId)
}

ops, err := pr.GetOperators()
if err != nil {
return nil, nil, err
Expand All @@ -123,6 +117,7 @@ func (pr *Prover) CreateInitialLightClientState(height exported.Height) (exporte
for _, op := range ops {
operators = append(operators, op.Bytes())
}

clientState := &lcptypes.ClientState{
LatestHeight: clienttypes.Height{},
Mrenclave: pr.config.GetMrenclave(),
Expand All @@ -135,6 +130,13 @@ func (pr *Prover) CreateInitialLightClientState(height exported.Height) (exporte
OperatorsThresholdDenominator: pr.GetOperatorsThreshold().Denominator,
}
consensusState := &lcptypes.ConsensusState{}

if res, err := pr.createELC(pr.config.ElcClientId, height); err != nil {
return nil, nil, fmt.Errorf("failed to create ELC: %w", err)
} else if res == nil {
pr.getLogger().Info("no need to create ELC", "elc_client_id", pr.config.ElcClientId)
}

// NOTE after creates client, register an enclave key into the client state
return clientState, consensusState, nil
}
Expand Down

0 comments on commit e9082fe

Please sign in to comment.