Skip to content

Commit

Permalink
Merge branch 'main' into store/fix-store-head
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg authored Oct 17, 2024
2 parents 4572354 + 4b626c3 commit 1cdbdb8
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 39 deletions.
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,
},
{
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
29 changes: 8 additions & 21 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,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 1cdbdb8

Please sign in to comment.