From df53ea4c4c7d746cd1b7b13948622e0fe0553edf Mon Sep 17 00:00:00 2001 From: Masanori Yoshida Date: Thu, 12 Oct 2023 10:18:37 +0900 Subject: [PATCH 1/3] use unsigned integer for finality_delay instead of signed integer Signed-off-by: Masanori Yoshida --- proto/relayer/provers/mock/config/config.proto | 2 +- provers/mock/config.pb.go | 10 +++++----- provers/mock/prover.go | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/proto/relayer/provers/mock/config/config.proto b/proto/relayer/provers/mock/config/config.proto index 4ef5eb1b..4cade670 100644 --- a/proto/relayer/provers/mock/config/config.proto +++ b/proto/relayer/provers/mock/config/config.proto @@ -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; } diff --git a/provers/mock/config.pb.go b/provers/mock/config.pb.go index 930bc355..8244eef3 100644 --- a/provers/mock/config.pb.go +++ b/provers/mock/config.pb.go @@ -24,7 +24,7 @@ var _ = math.Inf const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package type ProverConfig struct { - FinalityDelay int64 `protobuf:"varint,1,opt,name=finality_delay,json=finalityDelay,proto3" json:"finality_delay,omitempty"` + FinalityDelay uint64 `protobuf:"varint,1,opt,name=finality_delay,json=finalityDelay,proto3" json:"finality_delay,omitempty"` } func (m *ProverConfig) Reset() { *m = ProverConfig{} } @@ -76,13 +76,13 @@ var fileDescriptor_a75943d2e3c08b58 = []byte{ 0xd2, 0x50, 0x95, 0x7a, 0x50, 0x95, 0x7a, 0x20, 0x95, 0x7a, 0x10, 0x25, 0x52, 0x22, 0xe9, 0xf9, 0xe9, 0xf9, 0x60, 0x75, 0xfa, 0x20, 0x16, 0x44, 0x8b, 0x92, 0x29, 0x17, 0x4f, 0x00, 0x58, 0xb1, 0x33, 0x58, 0x95, 0x90, 0x2a, 0x17, 0x5f, 0x5a, 0x66, 0x5e, 0x62, 0x4e, 0x66, 0x49, 0x65, 0x7c, - 0x0a, 0xc8, 0x34, 0x09, 0x46, 0x05, 0x46, 0x0d, 0xe6, 0x20, 0x5e, 0x98, 0xa8, 0x0b, 0x48, 0xd0, + 0x0a, 0xc8, 0x34, 0x09, 0x46, 0x05, 0x46, 0x0d, 0x96, 0x20, 0x5e, 0x98, 0xa8, 0x0b, 0x48, 0xd0, 0x29, 0xe8, 0xc4, 0x43, 0x39, 0x86, 0x13, 0x8f, 0xe4, 0x18, 0x2f, 0x3c, 0x92, 0x63, 0x7c, 0xf0, 0x48, 0x8e, 0x71, 0xc2, 0x63, 0x39, 0x86, 0x0b, 0x8f, 0xe5, 0x18, 0x6e, 0x3c, 0x96, 0x63, 0x88, 0x32, 0x49, 0xcf, 0x2c, 0xc9, 0x28, 0x4d, 0xd2, 0x4b, 0xce, 0xcf, 0xd5, 0xcf, 0xa8, 0x2c, 0x48, 0x2d, 0xca, 0x49, 0x4d, 0x49, 0x4f, 0x2d, 0xd2, 0xcd, 0x49, 0x4c, 0x2a, 0xd6, 0xaf, 0x2c, 0xcd, - 0xd4, 0xc5, 0xe6, 0xa3, 0x24, 0x36, 0xb0, 0x8b, 0x8c, 0x01, 0x01, 0x00, 0x00, 0xff, 0xff, 0xd0, - 0x69, 0x8e, 0x82, 0xf0, 0x00, 0x00, 0x00, + 0xd4, 0xc5, 0xe6, 0xa3, 0x24, 0x36, 0xb0, 0x8b, 0x8c, 0x01, 0x01, 0x00, 0x00, 0xff, 0xff, 0x3b, + 0x20, 0xe9, 0xbc, 0xf0, 0x00, 0x00, 0x00, } func (m *ProverConfig) Marshal() (dAtA []byte, err error) { @@ -185,7 +185,7 @@ func (m *ProverConfig) Unmarshal(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - m.FinalityDelay |= int64(b&0x7F) << shift + m.FinalityDelay |= uint64(b&0x7F) << shift if b < 0x80 { break } diff --git a/provers/mock/prover.go b/provers/mock/prover.go index d0f0850b..4f126316 100644 --- a/provers/mock/prover.go +++ b/provers/mock/prover.go @@ -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 { From be65dabaa6d661b3c1a30ad789e0e0cb0325e07c Mon Sep 17 00:00:00 2001 From: Masanori Yoshida Date: Thu, 12 Oct 2023 12:24:19 +0900 Subject: [PATCH 2/3] add Validate() to ChainConfig/ProverConfig interfaces and implement them for tm/mock modules Signed-off-by: Masanori Yoshida --- chains/tendermint/config.go | 47 +++++++++++++++++++++++++++++++++++++ core/config.go | 2 ++ provers/mock/config.go | 4 ++++ 3 files changed, 53 insertions(+) diff --git a/chains/tendermint/config.go b/chains/tendermint/config.go index c605c211..26d1004e 100644 --- a/chains/tendermint/config.go +++ b/chains/tendermint/config.go @@ -1,7 +1,9 @@ package tendermint import ( + "errors" "fmt" + "strings" "github.com/hyperledger-labs/yui-relayer/core" ) @@ -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) { @@ -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 +} diff --git a/core/config.go b/core/config.go index 4cd666f8..79fc0f61 100644 --- a/core/config.go +++ b/core/config.go @@ -24,12 +24,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 diff --git a/provers/mock/config.go b/provers/mock/config.go index 466a5e02..a72ac683 100644 --- a/provers/mock/config.go +++ b/provers/mock/config.go @@ -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 +} From 44846c53dbefb054e52869ed9b04c99591b6a895 Mon Sep 17 00:00:00 2001 From: Masanori Yoshida Date: Thu, 12 Oct 2023 12:32:17 +0900 Subject: [PATCH 3/3] call ChainConfig::Validate and ProverConfig::Validate in ChainProverConfig::Init Signed-off-by: Masanori Yoshida --- core/config.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/core/config.go b/core/config.go index 79fc0f61..0e2825c8 100644 --- a/core/config.go +++ b/core/config.go @@ -3,6 +3,7 @@ package core import ( "encoding/json" "errors" + "fmt" "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/gogoproto/proto" @@ -60,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