Skip to content

Commit

Permalink
fix(p2p): use header.Verify instead of trustedHeader.Verify(h)
Browse files Browse the repository at this point in the history
  • Loading branch information
renaynay committed Sep 22, 2023
1 parent 9b2f384 commit 19ecd5b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
4 changes: 2 additions & 2 deletions opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ type HeadOption[H Header[H]] func(opts *HeadParams[H])
type HeadParams[H Header[H]] struct {
// TrustedHead allows the caller of Head to specify a trusted header
// against which the underlying implementation of Head can verify against.
TrustedHead Header[H]
TrustedHead H
}

// WithTrustedHead sets the TrustedHead parameter to the given header.
func WithTrustedHead[H Header[H]](verified Header[H]) func(opts *HeadParams[H]) {
func WithTrustedHead[H Header[H]](verified H) func(opts *HeadParams[H]) {
return func(opts *HeadParams[H]) {
opts.TrustedHead = verified
}
Expand Down
4 changes: 2 additions & 2 deletions p2p/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (ex *Exchange[H]) Head(ctx context.Context, opts ...header.HeadOption[H]) (
// trusted peers for its Head request. If nil, trusted peers will
// be used. If non-nil, Exchange will ask several peers from its network for
// their Head and verify against the given trusted header.
useTrackedPeers := reqParams.TrustedHead != nil
useTrackedPeers := !reqParams.TrustedHead.IsZero()
if useTrackedPeers {
trackedPeers := ex.peerTracker.getPeers(maxUntrustedHeadRequests)
if len(trackedPeers) > 0 {
Expand All @@ -154,7 +154,7 @@ func (ex *Exchange[H]) Head(ctx context.Context, opts ...header.HeadOption[H]) (
}
// if tracked (untrusted) peers were requested, verify head
if useTrackedPeers {
err = reqParams.TrustedHead.Verify(headers[0])
err = header.Verify[H](reqParams.TrustedHead, headers[0], header.DefaultHeightThreshold)
if err != nil {
var verErr *header.VerifyError
if errors.As(err, &verErr) && verErr.SoftFailure {
Expand Down
9 changes: 4 additions & 5 deletions p2p/exchange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestExchange_RequestHead(t *testing.T) {

tests := []struct {
requestFromTrusted bool
lastHeader header.Header[*headertest.DummyHeader]
lastHeader *headertest.DummyHeader
expectedHeight uint64
expectedHash header.Hash
}{
Expand All @@ -76,7 +76,7 @@ func TestExchange_RequestHead(t *testing.T) {
t.Run(strconv.Itoa(i), func(t *testing.T) {
var opts []header.HeadOption[*headertest.DummyHeader]
if !tt.requestFromTrusted {
opts = append(opts, header.WithTrustedHead(tt.lastHeader))
opts = append(opts, header.WithTrustedHead[*headertest.DummyHeader](tt.lastHeader))
}

header, err := exchg.Head(ctx, opts...)
Expand Down Expand Up @@ -121,13 +121,12 @@ func TestExchange_RequestHead_SoftFailure(t *testing.T) {

// get first subjective head from trusted peer to initialize the
// exchange's store
var head header.Header[*headertest.DummyHeader]
head, err = exchg.Head(ctx)
head, err := exchg.Head(ctx)
require.NoError(t, err)

// now use that trusted head to request a new head from the exchange
// from the tracked peer
softFailHead, err := exchg.Head(ctx, header.WithTrustedHead(head))
softFailHead, err := exchg.Head(ctx, header.WithTrustedHead[*headertest.DummyHeader](head))
require.NoError(t, err)
assert.Equal(t, trackedStore.HeadHeight, softFailHead.Height())
}
Expand Down

0 comments on commit 19ecd5b

Please sign in to comment.