From c710b24558dda68cefdad6d582232fa301c8b36d Mon Sep 17 00:00:00 2001 From: Masanori Yoshida Date: Fri, 20 Oct 2023 12:47:38 +0900 Subject: [PATCH] tendermint: fix Prover::CheckRefreshRequired to be indentent of concrete types of ClientState/ConsensusState Signed-off-by: Masanori Yoshida --- chains/tendermint/config.go | 16 +++++++++++----- chains/tendermint/prover.go | 15 ++++----------- 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/chains/tendermint/config.go b/chains/tendermint/config.go index 10756abb..13e547b8 100644 --- a/chains/tendermint/config.go +++ b/chains/tendermint/config.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "strings" + "time" "github.com/hyperledger-labs/yui-relayer/core" ) @@ -62,14 +63,19 @@ func (c ProverConfig) Build(chain core.Chain) (core.Prover, error) { } 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") + if _, err := time.ParseDuration(c.TrustingPeriod); err != nil { + return fmt.Errorf("config attribute \"trusting_period\" is invalid: %v", err) } if c.RefreshThresholdRate <= 0 { return fmt.Errorf("config attribute \"refresh_threshold_rate\" is too small: %v", c.RefreshThresholdRate) } return nil } + +func (c ProverConfig) GetTrustingPeriod() time.Duration { + if d, err := time.ParseDuration(c.TrustingPeriod); err != nil { + panic(err) + } else { + return d + } +} diff --git a/chains/tendermint/prover.go b/chains/tendermint/prover.go index 9dbce63c..dbb1c9a3 100644 --- a/chains/tendermint/prover.go +++ b/chains/tendermint/prover.go @@ -132,12 +132,8 @@ func (pr *Prover) CheckRefreshRequired(counterparty core.ChainInfoICS02Querier) if err := pr.chain.codec.UnpackAny(resCs.ClientState, &cs); err != nil { return false, fmt.Errorf("failed to unpack Any into tendermint client state: %v", err) } - tmCs, ok := cs.(*tmclient.ClientState) - if !ok { - return false, fmt.Errorf("unexpected instance type of exported.ClientState: %T", cs) - } - resCons, err := counterparty.QueryClientConsensusState(cpQueryCtx, tmCs.LatestHeight) + resCons, err := counterparty.QueryClientConsensusState(cpQueryCtx, cs.GetLatestHeight()) if err != nil { return false, fmt.Errorf("failed to query the consensus state on the counterparty chain: %v", err) } @@ -146,10 +142,7 @@ func (pr *Prover) CheckRefreshRequired(counterparty core.ChainInfoICS02Querier) if err := pr.chain.codec.UnpackAny(resCons.ConsensusState, &cons); err != nil { return false, fmt.Errorf("failed to unpack Any into tendermint consensus state: %v", err) } - tmCons, ok := cons.(*tmclient.ConsensusState) - if !ok { - return false, fmt.Errorf("unexpected instance type of exported.ConsensusState: %T", cons) - } + lcLastTimestamp := time.Unix(0, int64(cons.GetTimestamp())) selfQueryHeight, err := pr.chain.LatestHeight() if err != nil { @@ -161,14 +154,14 @@ func (pr *Prover) CheckRefreshRequired(counterparty core.ChainInfoICS02Querier) return false, fmt.Errorf("failed to get timestamp of the self chain: %v", err) } - elapsedTime := selfTimestamp.Sub(tmCons.Timestamp) + elapsedTime := selfTimestamp.Sub(lcLastTimestamp) durationMulByFloat := func(d time.Duration, f float64) time.Duration { nsec := float64(d.Nanoseconds()) nsec *= f return time.Duration(nsec) * time.Nanosecond } - needsRefresh := elapsedTime > durationMulByFloat(tmCs.TrustingPeriod, pr.config.RefreshThresholdRate) + needsRefresh := elapsedTime > durationMulByFloat(pr.config.GetTrustingPeriod(), pr.config.RefreshThresholdRate) return needsRefresh, nil }