From a6dc0af1d7e359773630e80c0fbe293635c26b66 Mon Sep 17 00:00:00 2001 From: Naohiro Yoshida Date: Wed, 16 Oct 2024 11:52:25 +0900 Subject: [PATCH] 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 --- e2e/config/demo/ibc-0.json | 3 +- module/config.pb.go | 93 +++++++++++++------ module/prover.go | 20 +++- module/prover_test.go | 30 +++++- .../provers/parlia/config/config.proto | 5 + 5 files changed, 118 insertions(+), 33 deletions(-) diff --git a/e2e/config/demo/ibc-0.json b/e2e/config/demo/ibc-0.json index f9985b5..45fcee7 100644 --- a/e2e/config/demo/ibc-0.json +++ b/e2e/config/demo/ibc-0.json @@ -30,6 +30,7 @@ "refreshThresholdRate": { "numerator": 1, "denominator": 2 - } + }, + "refreshBlockDifferenceThreshold": 1000 } } diff --git a/module/config.pb.go b/module/config.pb.go index 88327b1..68e02bb 100644 --- a/module/config.pb.go +++ b/module/config.pb.go @@ -28,9 +28,11 @@ var _ = time.Kitchen const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package type ProverConfig struct { - TrustingPeriod time.Duration `protobuf:"bytes,1,opt,name=trusting_period,json=trustingPeriod,proto3,stdduration" json:"trusting_period"` - MaxClockDrift time.Duration `protobuf:"bytes,2,opt,name=max_clock_drift,json=maxClockDrift,proto3,stdduration" json:"max_clock_drift"` - RefreshThresholdRate *Fraction `protobuf:"bytes,3,opt,name=refresh_threshold_rate,json=refreshThresholdRate,proto3" json:"refresh_threshold_rate,omitempty"` + TrustingPeriod time.Duration `protobuf:"bytes,1,opt,name=trusting_period,json=trustingPeriod,proto3,stdduration" json:"trusting_period"` + MaxClockDrift time.Duration `protobuf:"bytes,2,opt,name=max_clock_drift,json=maxClockDrift,proto3,stdduration" json:"max_clock_drift"` + // Fraction of the trusting period that is allowed to pass before the client is considered expired + RefreshThresholdRate *Fraction `protobuf:"bytes,3,opt,name=refresh_threshold_rate,json=refreshThresholdRate,proto3" json:"refresh_threshold_rate,omitempty"` + RefreshBlockDifferenceThreshold uint64 `protobuf:"varint,4,opt,name=refresh_block_difference_threshold,json=refreshBlockDifferenceThreshold,proto3" json:"refresh_block_difference_threshold,omitempty"` } func (m *ProverConfig) Reset() { *m = ProverConfig{} } @@ -87,6 +89,13 @@ func (m *ProverConfig) GetRefreshThresholdRate() *Fraction { return nil } +func (m *ProverConfig) GetRefreshBlockDifferenceThreshold() uint64 { + if m != nil { + return m.RefreshBlockDifferenceThreshold + } + return 0 +} + type Fraction struct { Numerator uint64 `protobuf:"varint,1,opt,name=numerator,proto3" json:"numerator,omitempty"` Denominator uint64 `protobuf:"varint,2,opt,name=denominator,proto3" json:"denominator,omitempty"` @@ -149,30 +158,33 @@ func init() { } var fileDescriptor_4d00ceb9ab8b08a6 = []byte{ - // 367 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x90, 0xbf, 0x6a, 0xe3, 0x40, - 0x10, 0xc6, 0x25, 0xdf, 0x71, 0xf8, 0xd6, 0x77, 0x67, 0x10, 0xe6, 0xf0, 0x99, 0x8b, 0x6c, 0xdc, - 0x24, 0x04, 0xbc, 0x0b, 0xc9, 0x1b, 0xd8, 0x26, 0x90, 0x3f, 0x85, 0x11, 0xa9, 0x02, 0x41, 0xac, - 0xa4, 0x95, 0xb4, 0x44, 0xd2, 0x88, 0xf1, 0x2a, 0x38, 0x6f, 0x90, 0x32, 0x65, 0x1e, 0xc9, 0xa5, - 0xcb, 0x54, 0x49, 0xb0, 0x5f, 0x24, 0x68, 0x25, 0x91, 0x54, 0x21, 0xd5, 0x0e, 0x33, 0xdf, 0xf7, - 0xfb, 0x76, 0x86, 0x1c, 0xa2, 0x48, 0xf8, 0x9d, 0x40, 0x96, 0x23, 0xdc, 0x0a, 0x5c, 0xb2, 0x9c, - 0x63, 0x22, 0x39, 0xf3, 0x21, 0x0b, 0x65, 0x54, 0x3f, 0x34, 0x47, 0x50, 0x60, 0xed, 0xd5, 0x5a, - 0x5a, 0x6b, 0x69, 0xa5, 0xa5, 0x95, 0x68, 0x60, 0x47, 0x00, 0x51, 0x22, 0x98, 0x16, 0x7b, 0x45, - 0xc8, 0x82, 0x02, 0xb9, 0x92, 0x90, 0x55, 0xf6, 0x41, 0x2f, 0x82, 0x08, 0x74, 0xc9, 0xca, 0xaa, - 0xea, 0x8e, 0xef, 0x5b, 0xe4, 0xd7, 0x42, 0xf3, 0x66, 0x1a, 0x63, 0x5d, 0x90, 0xae, 0xc2, 0x62, - 0xa9, 0x64, 0x16, 0xb9, 0xb9, 0x40, 0x09, 0x41, 0xdf, 0x1c, 0x99, 0x07, 0x9d, 0xa3, 0x7f, 0xb4, - 0x0a, 0xa0, 0x4d, 0x00, 0x9d, 0xd7, 0x01, 0xd3, 0xf6, 0xfa, 0x79, 0x68, 0x3c, 0xbe, 0x0c, 0x4d, - 0xe7, 0x4f, 0xe3, 0x5d, 0x68, 0xab, 0x75, 0x4e, 0xba, 0x29, 0x5f, 0xb9, 0x7e, 0x02, 0xfe, 0x8d, - 0x1b, 0xa0, 0x0c, 0x55, 0xbf, 0xf5, 0x75, 0xda, 0xef, 0x94, 0xaf, 0x66, 0xa5, 0x75, 0x5e, 0x3a, - 0xad, 0x6b, 0xf2, 0x17, 0x45, 0x88, 0x62, 0x19, 0xbb, 0x2a, 0x2e, 0x1f, 0x48, 0x02, 0x17, 0xb9, - 0x12, 0xfd, 0x6f, 0x9a, 0xb9, 0x4f, 0x3f, 0xbd, 0x10, 0x3d, 0x41, 0xee, 0x97, 0x09, 0x4e, 0xaf, - 0xc6, 0x5c, 0x36, 0x14, 0x87, 0x2b, 0x31, 0x3e, 0x23, 0xed, 0x46, 0x61, 0xfd, 0x27, 0x3f, 0xb3, - 0x22, 0x15, 0xc8, 0x15, 0xa0, 0xde, 0xff, 0xbb, 0xf3, 0xde, 0xb0, 0x46, 0xa4, 0x13, 0x88, 0x0c, - 0x52, 0x99, 0xe9, 0x79, 0x4b, 0xcf, 0x3f, 0xb6, 0xa6, 0xa7, 0xeb, 0xad, 0x6d, 0x6e, 0xb6, 0xb6, - 0xf9, 0xba, 0xb5, 0xcd, 0x87, 0x9d, 0x6d, 0x6c, 0x76, 0xb6, 0xf1, 0xb4, 0xb3, 0x8d, 0x2b, 0x16, - 0x49, 0x15, 0x17, 0x1e, 0xf5, 0x21, 0x65, 0x01, 0x57, 0xdc, 0x8f, 0xb9, 0xcc, 0x12, 0xee, 0x31, - 0xe9, 0xf9, 0x93, 0xea, 0xbf, 0x13, 0xbd, 0x06, 0x4b, 0x21, 0x28, 0x12, 0xe1, 0xfd, 0xd0, 0x17, - 0x3a, 0x7e, 0x0b, 0x00, 0x00, 0xff, 0xff, 0x18, 0xe4, 0x64, 0x12, 0x2b, 0x02, 0x00, 0x00, + // 402 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x90, 0xcf, 0x6a, 0xd5, 0x40, + 0x14, 0xc6, 0x33, 0xd7, 0x20, 0x75, 0xaa, 0x16, 0x42, 0x91, 0x58, 0x34, 0xb9, 0xdc, 0x8d, 0x45, + 0xe8, 0x0c, 0xe8, 0x1b, 0xa4, 0x45, 0xf0, 0xcf, 0xa2, 0x06, 0x57, 0x82, 0x84, 0xc9, 0x64, 0x92, + 0x0c, 0x26, 0x73, 0xc2, 0xb9, 0x13, 0xa9, 0x6f, 0xe1, 0xd2, 0x47, 0xba, 0xcb, 0x2e, 0x05, 0xa1, + 0xca, 0xbd, 0x2f, 0x22, 0x99, 0x24, 0xbd, 0xae, 0xc4, 0xd5, 0x0c, 0xe7, 0x7c, 0xdf, 0xef, 0x3b, + 0x7c, 0xf4, 0x39, 0xaa, 0x46, 0x7c, 0x55, 0xc8, 0x3b, 0x84, 0x2f, 0x0a, 0xd7, 0xbc, 0x13, 0xd8, + 0x68, 0xc1, 0x25, 0x98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 0x27, 0xfe, 0xe6, 0x26, 0x26, + 0x69, 0x3c, 0xe9, 0x13, 0x77, 0xe3, 0xad, 0xfa, 0x16, 0xbb, 0x7a, 0x43, 0x0f, 0xe6, 0xd0, 0xe0, + 0x09, 0xbd, 0x67, 0xfa, 0x56, 0xa1, 0xb0, 0x80, 0xae, 0x52, 0x3f, 0xdd, 0x0f, 0x82, 0x25, 0x3d, + 0x2c, 0x94, 0x81, 0x56, 0x1b, 0xb7, 0x5f, 0xb8, 0xfd, 0xdf, 0xa3, 0xe4, 0xf5, 0x66, 0x1b, 0x91, + 0xeb, 0x6d, 0x44, 0x7e, 0x6f, 0x23, 0xf2, 0x6d, 0x17, 0x79, 0xd7, 0xbb, 0xc8, 0xfb, 0xb1, 0x8b, + 0xbc, 0x8f, 0xbc, 0xd2, 0xb6, 0xee, 0x73, 0x26, 0xa1, 0xe5, 0x85, 0xb0, 0x42, 0xd6, 0x42, 0x9b, + 0x46, 0xe4, 0x5c, 0xe7, 0xf2, 0x6c, 0xac, 0xe0, 0xcc, 0x35, 0xc3, 0x5b, 0x28, 0xfa, 0x46, 0xe5, + 0x77, 0x5d, 0xe9, 0x2f, 0xff, 0x04, 0x00, 0x00, 0xff, 0xff, 0xdb, 0xef, 0x3f, 0x4b, 0x7e, 0x02, + 0x00, 0x00, } func (m *ProverConfig) Marshal() (dAtA []byte, err error) { @@ -195,6 +207,11 @@ func (m *ProverConfig) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if m.RefreshBlockDifferenceThreshold != 0 { + i = encodeVarintConfig(dAtA, i, uint64(m.RefreshBlockDifferenceThreshold)) + i-- + dAtA[i] = 0x20 + } if m.RefreshThresholdRate != nil { { size, err := m.RefreshThresholdRate.MarshalToSizedBuffer(dAtA[:i]) @@ -284,6 +301,9 @@ func (m *ProverConfig) Size() (n int) { l = m.RefreshThresholdRate.Size() n += 1 + l + sovConfig(uint64(l)) } + if m.RefreshBlockDifferenceThreshold != 0 { + n += 1 + sovConfig(uint64(m.RefreshBlockDifferenceThreshold)) + } return n } @@ -439,6 +459,25 @@ func (m *ProverConfig) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex + case 4: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field RefreshBlockDifferenceThreshold", wireType) + } + m.RefreshBlockDifferenceThreshold = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowConfig + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.RefreshBlockDifferenceThreshold |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } default: iNdEx = preIndex skippy, err := skipConfig(dAtA[iNdEx:]) diff --git a/module/prover.go b/module/prover.go index f04e49b..b986217 100644 --- a/module/prover.go +++ b/module/prover.go @@ -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) { diff --git a/module/prover_test.go b/module/prover_test.go index 6d7c553..ce6462c 100644 --- a/module/prover_test.go +++ b/module/prover_test.go @@ -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() { diff --git a/proto/relayer/provers/parlia/config/config.proto b/proto/relayer/provers/parlia/config/config.proto index 260a8ca..844faee 100644 --- a/proto/relayer/provers/parlia/config/config.proto +++ b/proto/relayer/provers/parlia/config/config.proto @@ -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 {