Skip to content

Commit

Permalink
feat(verify): remove height check in Verify (#216)
Browse files Browse the repository at this point in the history
Remove `heightThreshold` check in `Verify` func.
  • Loading branch information
cristaloleg authored and renaynay committed Oct 22, 2024
1 parent c3a0082 commit 6f12bfa
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 33 deletions.
15 changes: 10 additions & 5 deletions headertest/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,22 @@ func TestVerify(t *testing.T) {
},
{
prepare: func() *DummyHeader {
untrusted := next()
untrusted.HeightI += 100000
return untrusted
return zero

Check failure on line 90 in headertest/verify_test.go

View workflow job for this annotation

GitHub Actions / build

undefined: zero
},
err: header.ErrHeightFromFuture,
err: header.ErrZeroHeader,
},
{
trusted: zero,

Check failure on line 95 in headertest/verify_test.go

View workflow job for this annotation

GitHub Actions / build

unknown field trusted in struct literal of type struct{prepare func() *DummyHeader; err error; soft bool}

Check failure on line 95 in headertest/verify_test.go

View workflow job for this annotation

GitHub Actions / build

undefined: 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())

Check failure on line 105 in headertest/verify_test.go

View workflow job for this annotation

GitHub Actions / build

test.trusted undefined (type struct{prepare func() *DummyHeader; err error; soft bool} has no field or method trusted)
if test.err != nil {
var verErr *header.VerifyError
assert.ErrorAs(t, err, &verErr)
Expand Down
2 changes: 1 addition & 1 deletion p2p/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 1 addition & 7 deletions sync/sync_head.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
31 changes: 11 additions & 20 deletions verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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.
Expand Down

0 comments on commit 6f12bfa

Please sign in to comment.