From 4b626c39975d60134b690e74f902c2849fda05a7 Mon Sep 17 00:00:00 2001 From: Oleg Kovalov Date: Wed, 18 Sep 2024 16:46:51 +0200 Subject: [PATCH] feat(verify): remove height check in Verify (#216) Remove `heightThreshold` check in `Verify` func. --- headertest/verify_test.go | 11 +---------- p2p/exchange.go | 2 +- sync/sync_head.go | 8 +------- verify.go | 29 ++++++++--------------------- 4 files changed, 11 insertions(+), 39 deletions(-) diff --git a/headertest/verify_test.go b/headertest/verify_test.go index 35456991..e4d13026 100644 --- a/headertest/verify_test.go +++ b/headertest/verify_test.go @@ -95,15 +95,6 @@ func TestVerify(t *testing.T) { }, err: header.ErrKnownHeader, }, - { - trusted: trusted, - prepare: func() *DummyHeader { - untrusted := next() - untrusted.HeightI += 100000 - return untrusted - }, - err: header.ErrHeightFromFuture, - }, { trusted: trusted, prepare: func() *DummyHeader { @@ -122,7 +113,7 @@ func TestVerify(t *testing.T) { for i, test := range tests { t.Run(strconv.Itoa(i), func(t *testing.T) { - err := header.Verify(test.trusted, test.prepare(), 0) + err := header.Verify(test.trusted, test.prepare()) if test.err != nil { var verErr *header.VerifyError assert.ErrorAs(t, err, &verErr) diff --git a/p2p/exchange.go b/p2p/exchange.go index fbf1f23a..c498425e 100644 --- a/p2p/exchange.go +++ b/p2p/exchange.go @@ -182,7 +182,7 @@ func (ex *Exchange[H]) Head(ctx context.Context, opts ...header.HeadOption[H]) ( } // if tracked (untrusted) peers were requested, verify head if useTrackedPeers { - err = header.Verify[H](reqParams.TrustedHead, headers[0], header.DefaultHeightThreshold) + err = header.Verify[H](reqParams.TrustedHead, headers[0]) if err != nil { var verErr *header.VerifyError if errors.As(err, &verErr) && verErr.SoftFailure { diff --git a/sync/sync_head.go b/sync/sync_head.go index 19a2b272..c74347b7 100644 --- a/sync/sync_head.go +++ b/sync/sync_head.go @@ -167,13 +167,7 @@ func (s *Syncer[H]) verify(ctx context.Context, newHead H) (bool, error) { return true, &header.VerifyError{Reason: err, SoftFailure: true} } - var heightThreshold uint64 - if s.Params.TrustingPeriod != 0 && s.Params.blockTime != 0 { - buffer := time.Hour * 48 / s.Params.blockTime // generous buffer to account for variable block time - heightThreshold = uint64(s.Params.TrustingPeriod/s.Params.blockTime + buffer) - } - - err = header.Verify(sbjHead, newHead, heightThreshold) + err = header.Verify(sbjHead, newHead) if err == nil { return false, nil } diff --git a/verify.go b/verify.go index 72888ef8..5ec19538 100644 --- a/verify.go +++ b/verify.go @@ -14,11 +14,10 @@ const DefaultHeightThreshold uint64 = 80000 // ~ 14 days of 15 second headers // custom user-specific checks defined in Header.Verify. // // Given headers must be non-zero -// If heightThreshold is zero, uses DefaultHeightThreshold. // Always returns VerifyError. -func Verify[H Header[H]](trstd, untrstd H, heightThreshold uint64) error { +func Verify[H Header[H]](trstd, untrstd H) error { // general mandatory verification - err := verify[H](trstd, untrstd, heightThreshold) + err := verify[H](trstd, untrstd) if err != nil { return &VerifyError{Reason: err} } @@ -46,11 +45,7 @@ func Verify[H Header[H]](trstd, untrstd H, heightThreshold uint64) error { // verify is a little bro of Verify yet performs mandatory Header checks // for any Header implementation. -func verify[H Header[H]](trstd, untrstd H, heightThreshold uint64) error { - if heightThreshold == 0 { - heightThreshold = DefaultHeightThreshold - } - +func verify[H Header[H]](trstd, untrstd H) error { if trstd.IsZero() { return ErrZeroHeader } @@ -75,24 +70,16 @@ func verify[H Header[H]](trstd, untrstd H, heightThreshold uint64) error { if known { return fmt.Errorf("%w: '%d' <= current '%d'", ErrKnownHeader, untrstd.Height(), trstd.Height()) } - // reject headers with height too far from the future - // this is essential for headers failed non-adjacent verification - // yet taken as sync target - adequateHeight := untrstd.Height()-trstd.Height() < heightThreshold - if !adequateHeight { - return fmt.Errorf("%w: '%d' - current '%d' >= threshold '%d'", ErrHeightFromFuture, untrstd.Height(), trstd.Height(), heightThreshold) - } return nil } var ( - ErrZeroHeader = errors.New("zero header") - ErrWrongChainID = errors.New("wrong chain id") - ErrUnorderedTime = errors.New("unordered headers") - ErrFromFuture = errors.New("header is from the future") - ErrKnownHeader = errors.New("known header") - ErrHeightFromFuture = errors.New("header height is far from future") + ErrZeroHeader = errors.New("zero header") + ErrWrongChainID = errors.New("wrong chain id") + ErrUnorderedTime = errors.New("unordered headers") + ErrFromFuture = errors.New("header is from the future") + ErrKnownHeader = errors.New("known header") ) // VerifyError is thrown if a Header failed verification.