Skip to content

Commit

Permalink
refactor: Change from RequestOption to HeadOption
Browse files Browse the repository at this point in the history
  • Loading branch information
renaynay committed Jul 4, 2023
1 parent 6640fe8 commit 3301aca
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 96 deletions.
2 changes: 1 addition & 1 deletion headertest/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (m *Store[H]) Height() uint64 {
return uint64(m.HeadHeight)
}

func (m *Store[H]) Head(context.Context, ...header.RequestOption) (H, error) {
func (m *Store[H]) Head(context.Context, ...header.HeadOption) (H, error) {
return m.Headers[m.HeadHeight], nil
}

Expand Down
2 changes: 1 addition & 1 deletion interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,5 @@ type Getter[H Header] interface {
// reporting it.
type Head[H Header] interface {
// Head returns the latest known header.
Head(context.Context, ...RequestOption) (H, error)
Head(context.Context, ...HeadOption) (H, error)
}
2 changes: 1 addition & 1 deletion local/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (l *Exchange[H]) Stop(context.Context) error {
return nil
}

func (l *Exchange[H]) Head(ctx context.Context, _ ...header.RequestOption) (H, error) {
func (l *Exchange[H]) Head(ctx context.Context, _ ...header.HeadOption) (H, error) {
return l.store.Head(ctx)
}

Expand Down
21 changes: 11 additions & 10 deletions opts.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
package header

type RequestOption func(opts *RequestParams)
type HeadOption func(opts *HeadRequestParams)

// RequestParams contains options to be used for header Exchange
// HeadRequestParams contains options to be used for header Exchange
// requests.
type RequestParams struct {
type HeadRequestParams struct {
// SubjectiveInit determines whether the Exchange should use
// trusted peers for its Head request (true = yes).
SubjectiveInit bool
}

func DefaultRequestParams() RequestParams {
return RequestParams{
SubjectiveInit: false,
func DefaultHeadRequestParams() HeadRequestParams {
return HeadRequestParams{
SubjectiveInit: true,
}
}

// WithSubjectiveInit sets the SubjectiveInit parameter to true,
// indicating to the Head method to use the trusted peer set.
func WithSubjectiveInit(opts *RequestParams) {
opts.SubjectiveInit = true
// WithDisabledSubjectiveInit sets the SubjectiveInit parameter to false,
// indicating to the Head method to use the tracked peerset rather than
// the trusted peerset (if enough tracked peers are available)
func WithDisabledSubjectiveInit(opts *HeadRequestParams) {
opts.SubjectiveInit = false
}
4 changes: 2 additions & 2 deletions p2p/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (ex *Exchange[H]) Stop(ctx context.Context) error {
// The Head must be verified thereafter where possible.
// We request in parallel all the trusted peers, compare their response
// and return the highest one.
func (ex *Exchange[H]) Head(ctx context.Context, opts ...header.RequestOption) (H, error) {
func (ex *Exchange[H]) Head(ctx context.Context, opts ...header.HeadOption) (H, error) {
log.Debug("requesting head")

reqCtx := ctx
Expand All @@ -126,7 +126,7 @@ func (ex *Exchange[H]) Head(ctx context.Context, opts ...header.RequestOption) (
defer cancel()
}

reqParams := header.DefaultRequestParams()
reqParams := header.DefaultHeadRequestParams()
for _, opt := range opts {
opt(&reqParams)
}
Expand Down
10 changes: 5 additions & 5 deletions p2p/exchange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestExchange_RequestHead(t *testing.T) {
// create new server-side exchange that will act as the tracked peer
// it will have a higher chain head than the trusted peer so that the
// test can determine which peer was asked
trackedStore := headertest.NewStore[*headertest.DummyHeader](t, headertest.NewTestSuite(t), 10)
trackedStore := headertest.NewStore[*headertest.DummyHeader](t, headertest.NewTestSuite(t), 50)
serverSideEx, err := NewExchangeServer[*headertest.DummyHeader](hosts[2], trackedStore,
WithNetworkID[ServerParameters](networkID),
)
Expand Down Expand Up @@ -75,9 +75,9 @@ func TestExchange_RequestHead(t *testing.T) {

for i, tt := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
var opts []header.RequestOption
if tt.withSubjInit {
opts = append(opts, header.WithSubjectiveInit)
var opts []header.HeadOption
if !tt.withSubjInit {
opts = append(opts, header.WithDisabledSubjectiveInit)
}

header, err := exchg.Head(ctx, opts...)
Expand Down Expand Up @@ -584,7 +584,7 @@ func (t *timedOutStore) HasAt(_ context.Context, _ uint64) bool {
return true
}

func (t *timedOutStore) Head(context.Context, ...header.RequestOption) (*headertest.DummyHeader, error) {
func (t *timedOutStore) Head(context.Context, ...header.HeadOption) (*headertest.DummyHeader, error) {
time.Sleep(t.timeout)
return nil, header.ErrNoHead
}
2 changes: 1 addition & 1 deletion store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (s *Store[H]) Height() uint64 {
return s.heightSub.Height()
}

func (s *Store[H]) Head(ctx context.Context, _ ...header.RequestOption) (H, error) {
func (s *Store[H]) Head(ctx context.Context, _ ...header.HeadOption) (H, error) {
head, err := s.GetByHeight(ctx, s.heightSub.Height())
if err == nil {
return head, nil
Expand Down
2 changes: 1 addition & 1 deletion sync/sync_getter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type syncGetter[H header.Header] struct {
head H
}

func (se *syncGetter[H]) Head(ctx context.Context, _ ...header.RequestOption) (H, error) {
func (se *syncGetter[H]) Head(ctx context.Context, _ ...header.HeadOption) (H, error) {
// the lock construction here ensures only one routine calling Head at a time
// while others wait via Rlock
if !se.headLk.TryLock() {
Expand Down
2 changes: 1 addition & 1 deletion sync/sync_getter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type fakeGetter[H header.Header] struct {
hits atomic.Uint32
}

func (f *fakeGetter[H]) Head(ctx context.Context, _ ...header.RequestOption) (h H, err error) {
func (f *fakeGetter[H]) Head(ctx context.Context, _ ...header.HeadOption) (h H, err error) {
f.hits.Add(1)
select {
case <-time.After(time.Millisecond * 100):
Expand Down
6 changes: 3 additions & 3 deletions sync/sync_head.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
// Known subjective head is considered network head if it is recent enough(now-timestamp<=blocktime)
// Otherwise, head is requested from a trusted peer and
// set as the new subjective head, assuming that trusted peer is always fully synced.
func (s *Syncer[H]) Head(ctx context.Context, _ ...header.RequestOption) (H, error) {
func (s *Syncer[H]) Head(ctx context.Context, _ ...header.HeadOption) (H, error) {
sbjHead, err := s.subjectiveHead(ctx)
if err != nil {
return sbjHead, err
Expand All @@ -31,7 +31,7 @@ func (s *Syncer[H]) Head(ctx context.Context, _ ...header.RequestOption) (H, err
// * If now >= TNH && now <= TNH + (THP) header propagation time
// * Wait for header to arrive instead of requesting it
// * This way we don't request as we know the new network header arrives exactly
netHead, err := s.getter.Head(ctx)
netHead, err := s.getter.Head(ctx, header.WithDisabledSubjectiveInit)
if err != nil {
return netHead, err
}
Expand Down Expand Up @@ -68,7 +68,7 @@ func (s *Syncer[H]) subjectiveHead(ctx context.Context) (H, error) {
}
// otherwise, request head from a trusted peer via subjective initialization
log.Infow("stored head header expired", "height", storeHead.Height())
trustHead, err := s.getter.Head(ctx, header.WithSubjectiveInit)
trustHead, err := s.getter.Head(ctx)
if err != nil {
return trustHead, err
}
Expand Down
70 changes: 0 additions & 70 deletions sync/sync_head_test.go

This file was deleted.

0 comments on commit 3301aca

Please sign in to comment.