Skip to content

Commit

Permalink
tendermint: fix Prover::CheckRefreshRequired to be indentent of concr…
Browse files Browse the repository at this point in the history
…ete types of ClientState/ConsensusState

Signed-off-by: Masanori Yoshida <[email protected]>
  • Loading branch information
siburu committed Oct 20, 2023
1 parent f4dba2d commit c710b24
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
16 changes: 11 additions & 5 deletions chains/tendermint/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"strings"
"time"

"github.com/hyperledger-labs/yui-relayer/core"
)
Expand Down Expand Up @@ -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
}
}
15 changes: 4 additions & 11 deletions chains/tendermint/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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 {
Expand All @@ -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
}
Expand Down

0 comments on commit c710b24

Please sign in to comment.