Skip to content

Commit

Permalink
Refresh if the difference between blocks in the chain and ClientState…
Browse files Browse the repository at this point in the history
… exceeds this value (#47)

* refresh if block difference exceeds threold

Signed-off-by: Naohiro Yoshida <[email protected]>
  • Loading branch information
yoshidan authored Oct 16, 2024
1 parent 3fffbac commit a6dc0af
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 33 deletions.
3 changes: 2 additions & 1 deletion e2e/config/demo/ibc-0.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"refreshThresholdRate": {
"numerator": 1,
"denominator": 2
}
},
"refreshBlockDifferenceThreshold": 1000
}
}
93 changes: 66 additions & 27 deletions module/config.pb.go

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

20 changes: 17 additions & 3 deletions module/prover.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,12 +191,26 @@ func (pr *Prover) CheckRefreshRequired(counterparty core.ChainInfoICS02Querier)
return time.Duration(nsec) * time.Nanosecond
}
threshold := durationMulByFraction(pr.config.GetTrustingPeriod(), pr.config.GetRefreshThresholdRate())
needsRefresh := elapsedTime > threshold
if needsRefresh {
if elapsedTime > threshold {
log.GetLogger().Debug("needs refresh", "elapsedTime", elapsedTime, "threshold", threshold)
return true, nil
}

return needsRefresh, nil
// Check if the block difference exceeds the threshold
blockDiffThreshold := pr.config.RefreshBlockDifferenceThreshold
if blockDiffThreshold == 0 || selfQueryHeight.GetRevisionHeight() < cs.GetLatestHeight().GetRevisionHeight() {
return false, nil
}
blockDiff := selfQueryHeight.GetRevisionHeight() - cs.GetLatestHeight().GetRevisionHeight()
if blockDiff > blockDiffThreshold {
log.GetLogger().Debug("needs refresh due to block diff",
"chain", cpQueryHeight.GetRevisionHeight(),
"cs", cs.GetLatestHeight().GetRevisionHeight(),
"threshold", blockDiffThreshold)
return true, nil
}
return false, nil

}

func (pr *Prover) withProofAndValidators(height uint64, ethHeaders []*ETHHeader) (core.Header, error) {
Expand Down
30 changes: 28 additions & 2 deletions module/prover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,22 +237,48 @@ func (ts *ProverTestSuite) TestCheckRefreshRequired() {
Chain: ts.prover.chain,
Prover: ts.prover,
}
defer func() {
ts.chain.latestHeight = 0
ts.chain.trustedHeight = 0
}()

now := time.Now()
chainHeight := clienttypes.NewHeight(0, 0)
csHeight := clienttypes.NewHeight(0, 0)
ts.chain.chainTimestamp[chainHeight] = uint64(now.Unix())

// should refresh
// should refresh by trusting_period
ts.chain.consensusStateTimestamp[csHeight] = uint64(now.Add(-51 * time.Second).UnixNano())
required, err := ts.prover.CheckRefreshRequired(dst)
ts.Require().NoError(err)
ts.Require().True(required)

// needless
// needless by trusting_period
ts.chain.consensusStateTimestamp[csHeight] = uint64(now.Add(-50 * time.Second).UnixNano())
required, err = ts.prover.CheckRefreshRequired(dst)
ts.Require().NoError(err)
ts.Require().False(required)

// should refresh by block difference
ts.chain.latestHeight = 2
ts.prover.config.RefreshBlockDifferenceThreshold = 1
required, err = ts.prover.CheckRefreshRequired(dst)
ts.Require().NoError(err)
ts.Require().True(required)

// needless by block difference
ts.prover.config.RefreshBlockDifferenceThreshold = 2
required, err = ts.prover.CheckRefreshRequired(dst)
ts.Require().NoError(err)
ts.Require().False(required)

// needless by invalid block difference
ts.chain.latestHeight = 1
ts.chain.trustedHeight = 3
ts.prover.config.RefreshBlockDifferenceThreshold = 1
required, err = ts.prover.CheckRefreshRequired(dst)
ts.Require().NoError(err)
ts.Require().False(required)
}

func (ts *ProverTestSuite) TestProveHostConsensusState() {
Expand Down
5 changes: 5 additions & 0 deletions proto/relayer/provers/parlia/config/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import "gogoproto/gogo.proto";
message ProverConfig {
google.protobuf.Duration trusting_period = 1 [(gogoproto.nullable) = false, (gogoproto.stdduration) = true];
google.protobuf.Duration max_clock_drift = 2 [(gogoproto.nullable) = false, (gogoproto.stdduration) = true];
// Fraction of the trusting period that is allowed to pass before the client is considered expired
Fraction refresh_threshold_rate = 3;
// Difference in blocks to refresh.
// Refresh if the difference between blocks in the chain and ClientState exceeds this value.
// If the value is 0, no refresh decision is made.
uint64 refresh_block_difference_threshold = 4;
}

message Fraction {
Expand Down

0 comments on commit a6dc0af

Please sign in to comment.