Skip to content

Commit

Permalink
Merge pull request #111 from siburu/config-validation
Browse files Browse the repository at this point in the history
Config validation
  • Loading branch information
siburu authored Oct 12, 2023
2 parents de9d8f9 + 44846c5 commit 0a2b77f
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 7 deletions.
47 changes: 47 additions & 0 deletions chains/tendermint/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package tendermint

import (
"errors"
"fmt"
"strings"

"github.com/hyperledger-labs/yui-relayer/core"
)
Expand All @@ -14,6 +16,41 @@ func (c ChainConfig) Build() (core.Chain, error) {
}, nil
}

func (c ChainConfig) Validate() error {
isEmpty := func(s string) bool {
return strings.TrimSpace(s) == ""
}

var errs []error
if isEmpty(c.Key) {
errs = append(errs, fmt.Errorf("config attribute \"key\" is empty"))
}
if isEmpty(c.ChainId) {
errs = append(errs, fmt.Errorf("config attribute \"chain_id\" is empty"))
}
if isEmpty(c.RpcAddr) {
errs = append(errs, fmt.Errorf("config attribute \"rpc_addr\" is empty"))
}
if isEmpty(c.AccountPrefix) {
errs = append(errs, fmt.Errorf("config attribute \"account_prefix\" is empty"))
}
if c.GasAdjustment <= 0 {
errs = append(errs, fmt.Errorf("config attribute \"gas_adjustment\" is too small: %v", c.GasAdjustment))
}
if isEmpty(c.GasPrices) {
errs = append(errs, fmt.Errorf("config attribute \"gas_prices\" is empty"))
}
if c.AverageBlockTimeMsec == 0 {
errs = append(errs, fmt.Errorf("config attribute \"average_block_time_msec\" is zero"))
}
if c.MaxRetryForCommit == 0 {
errs = append(errs, fmt.Errorf("config attribute \"max_retry_for_commit\" is zero"))
}

// errors.Join returns nil if len(errs) == 0
return errors.Join(errs...)
}

var _ core.ProverConfig = (*ProverConfig)(nil)

func (c ProverConfig) Build(chain core.Chain) (core.Prover, error) {
Expand All @@ -23,3 +60,13 @@ func (c ProverConfig) Build(chain core.Chain) (core.Prover, error) {
}
return NewProver(chain_, c), nil
}

func (c ProverConfig) Validate() error {
isEmpty := func(s string) bool {
return strings.TrimSpace(s) == ""
}
if isEmpty(c.TrustingPeriod) {
return fmt.Errorf("config attribute \"trusting_period\" is empty")
}
return nil
}
7 changes: 7 additions & 0 deletions core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package core
import (
"encoding/json"
"errors"
"fmt"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/gogoproto/proto"
Expand All @@ -24,12 +25,14 @@ type ChainProverConfig struct {
type ChainConfig interface {
proto.Message
Build() (Chain, error)
Validate() error
}

// ProverConfig defines a prover configuration and its builder
type ProverConfig interface {
proto.Message
Build(Chain) (Prover, error)
Validate() error
}

// NewChainProverConfig returns a new config instance
Expand Down Expand Up @@ -58,10 +61,14 @@ func (cc *ChainProverConfig) Init(m codec.Codec) error {
var chain ChainConfig
if err := utils.UnmarshalJSONAny(m, &chain, cc.Chain); err != nil {
return err
} else if err := chain.Validate(); err != nil {
return fmt.Errorf("invalid chain config: %v", err)
}
var prover ProverConfig
if err := utils.UnmarshalJSONAny(m, &prover, cc.Prover); err != nil {
return err
} else if err := prover.Validate(); err != nil {
return fmt.Errorf("invalid prover config: %v", err)
}
cc.chain = chain
cc.prover = prover
Expand Down
2 changes: 1 addition & 1 deletion proto/relayer/provers/mock/config/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ option go_package = "github.com/hyperledger-labs/yui-relayer/provers/mock";
option (gogoproto.goproto_getters_all) = false;

message ProverConfig {
int64 finality_delay = 1;
uint64 finality_delay = 1;
}
4 changes: 4 additions & 0 deletions provers/mock/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ var _ core.ProverConfig = (*ProverConfig)(nil)
func (c ProverConfig) Build(chain core.Chain) (core.Prover, error) {
return NewProver(chain, c), nil
}

func (c ProverConfig) Validate() error {
return nil
}
10 changes: 5 additions & 5 deletions provers/mock/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion provers/mock/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (pr *Prover) GetLatestFinalizedHeader() (latestFinalizedHeader core.Header,
if err != nil {
return nil, err
}
for i := int64(0); i < pr.config.FinalityDelay; i++ {
for i := uint64(0); i < pr.config.FinalityDelay; i++ {
if prevHeight, success := chainLatestHeight.Decrement(); success {
chainLatestHeight = prevHeight
} else {
Expand Down

0 comments on commit 0a2b77f

Please sign in to comment.