Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(verify): remove height check in Verify #216

Merged
merged 3 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions headertest/verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
cristaloleg marked this conversation as resolved.
Show resolved Hide resolved
},
{
trusted: trusted,
prepare: func() *DummyHeader {
Expand All @@ -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)
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
18 changes: 3 additions & 15 deletions verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
}
Expand Down Expand Up @@ -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
}
Expand All @@ -75,13 +70,6 @@ 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
}
Expand Down
Loading