From 6f12bfaf30ae730744fee5ed8b719274b04f632f 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 | 15 ++++++++++----- p2p/exchange.go | 2 +- sync/sync_head.go | 8 +------- verify.go | 31 +++++++++++-------------------- 4 files changed, 23 insertions(+), 33 deletions(-) diff --git a/headertest/verify_test.go b/headertest/verify_test.go index 2774c056..07d233e5 100644 --- a/headertest/verify_test.go +++ b/headertest/verify_test.go @@ -87,17 +87,22 @@ func TestVerify(t *testing.T) { }, { prepare: func() *DummyHeader { - untrusted := next() - untrusted.HeightI += 100000 - return untrusted + return zero }, - err: header.ErrHeightFromFuture, + err: header.ErrZeroHeader, + }, + { + trusted: zero, + prepare: func() *DummyHeader { + return next() + }, + err: header.ErrZeroHeader, }, } for i, test := range tests { t.Run(strconv.Itoa(i), func(t *testing.T) { - err := header.Verify(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 3f6a6572..900b06a8 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 a5a97a3a..93642cab 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 0c1f3e9e..c9840014 100644 --- a/verify.go +++ b/verify.go @@ -13,11 +13,11 @@ const DefaultHeightThreshold uint64 = 80000 // ~ 14 days of 15 second headers // Verify verifies untrusted Header against trusted following general Header checks and // custom user-specific checks defined in Header.Verify // -// If heightThreshold is zero, uses DefaultHeightThreshold. +// Given headers must be non-zero // 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} } @@ -45,11 +45,10 @@ 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 } - if untrstd.IsZero() { return ErrZeroHeader } @@ -71,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.