Skip to content

Commit a6dc0af

Browse files
authored
Refresh if the difference between blocks in the chain and ClientState exceeds this value (#47)
* refresh if block difference exceeds threold Signed-off-by: Naohiro Yoshida <naohiro.yoshida@datachain.jp>
1 parent 3fffbac commit a6dc0af

File tree

5 files changed

+118
-33
lines changed

5 files changed

+118
-33
lines changed

e2e/config/demo/ibc-0.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"refreshThresholdRate": {
3131
"numerator": 1,
3232
"denominator": 2
33-
}
33+
},
34+
"refreshBlockDifferenceThreshold": 1000
3435
}
3536
}

module/config.pb.go

Lines changed: 66 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

module/prover.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,26 @@ func (pr *Prover) CheckRefreshRequired(counterparty core.ChainInfoICS02Querier)
191191
return time.Duration(nsec) * time.Nanosecond
192192
}
193193
threshold := durationMulByFraction(pr.config.GetTrustingPeriod(), pr.config.GetRefreshThresholdRate())
194-
needsRefresh := elapsedTime > threshold
195-
if needsRefresh {
194+
if elapsedTime > threshold {
196195
log.GetLogger().Debug("needs refresh", "elapsedTime", elapsedTime, "threshold", threshold)
196+
return true, nil
197197
}
198198

199-
return needsRefresh, nil
199+
// Check if the block difference exceeds the threshold
200+
blockDiffThreshold := pr.config.RefreshBlockDifferenceThreshold
201+
if blockDiffThreshold == 0 || selfQueryHeight.GetRevisionHeight() < cs.GetLatestHeight().GetRevisionHeight() {
202+
return false, nil
203+
}
204+
blockDiff := selfQueryHeight.GetRevisionHeight() - cs.GetLatestHeight().GetRevisionHeight()
205+
if blockDiff > blockDiffThreshold {
206+
log.GetLogger().Debug("needs refresh due to block diff",
207+
"chain", cpQueryHeight.GetRevisionHeight(),
208+
"cs", cs.GetLatestHeight().GetRevisionHeight(),
209+
"threshold", blockDiffThreshold)
210+
return true, nil
211+
}
212+
return false, nil
213+
200214
}
201215

202216
func (pr *Prover) withProofAndValidators(height uint64, ethHeaders []*ETHHeader) (core.Header, error) {

module/prover_test.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,22 +237,48 @@ func (ts *ProverTestSuite) TestCheckRefreshRequired() {
237237
Chain: ts.prover.chain,
238238
Prover: ts.prover,
239239
}
240+
defer func() {
241+
ts.chain.latestHeight = 0
242+
ts.chain.trustedHeight = 0
243+
}()
244+
240245
now := time.Now()
241246
chainHeight := clienttypes.NewHeight(0, 0)
242247
csHeight := clienttypes.NewHeight(0, 0)
243248
ts.chain.chainTimestamp[chainHeight] = uint64(now.Unix())
244249

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

251-
// needless
256+
// needless by trusting_period
252257
ts.chain.consensusStateTimestamp[csHeight] = uint64(now.Add(-50 * time.Second).UnixNano())
253258
required, err = ts.prover.CheckRefreshRequired(dst)
254259
ts.Require().NoError(err)
255260
ts.Require().False(required)
261+
262+
// should refresh by block difference
263+
ts.chain.latestHeight = 2
264+
ts.prover.config.RefreshBlockDifferenceThreshold = 1
265+
required, err = ts.prover.CheckRefreshRequired(dst)
266+
ts.Require().NoError(err)
267+
ts.Require().True(required)
268+
269+
// needless by block difference
270+
ts.prover.config.RefreshBlockDifferenceThreshold = 2
271+
required, err = ts.prover.CheckRefreshRequired(dst)
272+
ts.Require().NoError(err)
273+
ts.Require().False(required)
274+
275+
// needless by invalid block difference
276+
ts.chain.latestHeight = 1
277+
ts.chain.trustedHeight = 3
278+
ts.prover.config.RefreshBlockDifferenceThreshold = 1
279+
required, err = ts.prover.CheckRefreshRequired(dst)
280+
ts.Require().NoError(err)
281+
ts.Require().False(required)
256282
}
257283

258284
func (ts *ProverTestSuite) TestProveHostConsensusState() {

proto/relayer/provers/parlia/config/config.proto

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ import "gogoproto/gogo.proto";
88
message ProverConfig {
99
google.protobuf.Duration trusting_period = 1 [(gogoproto.nullable) = false, (gogoproto.stdduration) = true];
1010
google.protobuf.Duration max_clock_drift = 2 [(gogoproto.nullable) = false, (gogoproto.stdduration) = true];
11+
// Fraction of the trusting period that is allowed to pass before the client is considered expired
1112
Fraction refresh_threshold_rate = 3;
13+
// Difference in blocks to refresh.
14+
// Refresh if the difference between blocks in the chain and ClientState exceeds this value.
15+
// If the value is 0, no refresh decision is made.
16+
uint64 refresh_block_difference_threshold = 4;
1217
}
1318

1419
message Fraction {

0 commit comments

Comments
 (0)