Skip to content
26 changes: 13 additions & 13 deletions headertest/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Store[H header.Header[H]] struct {
TailHeight uint64

onDeleteMu sync.Mutex
onDelete []func(context.Context, []H) error
onDelete []func(context.Context, uint64) error
}

// NewDummyStore creates a store for DummyHeader.
Expand Down Expand Up @@ -80,26 +80,26 @@ func (m *Store[H]) GetByHeight(_ context.Context, height uint64) (H, error) {
}

func (m *Store[H]) DeleteTo(ctx context.Context, to uint64) error {
var deleted []H
for h := m.TailHeight; h < to; h++ {
hdr, ok := m.Headers[h]
if ok {
delete(m.Headers, h)
deleted = append(deleted, hdr)
_, ok := m.Headers[h]
if !ok {
continue
}
}

m.TailHeight = to
for _, deleteFn := range m.onDelete {
err := deleteFn(ctx, deleted)
if err != nil {
return err
for _, deleteFn := range m.onDelete {
err := deleteFn(ctx, h)
if err != nil {
return err
}
}
delete(m.Headers, h) // must be after deleteFn
}

m.TailHeight = to
return nil
}

func (m *Store[H]) OnDelete(fn func(context.Context, []H) error) {
func (m *Store[H]) OnDelete(fn func(context.Context, uint64) error) {
m.onDeleteMu.Lock()
defer m.onDeleteMu.Unlock()

Expand Down
6 changes: 4 additions & 2 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,10 @@ type Store[H Header[H]] interface {
// DeleteTo deletes the range [Tail():to).
DeleteTo(ctx context.Context, to uint64) error

// OnDelete registers given handler to be called whenever headers are removed from the Store.
OnDelete(func(context.Context, []H) error)
// OnDelete registers given handler to be called whenever a header with the height is being removed.
// OnDelete guarantees that the header is accessible for the handler with GetByHeight and is removed
// only after the handler terminates with nil error.
OnDelete(handler func(ctx context.Context, height uint64) error)
}

// Getter contains the behavior necessary for a component to retrieve
Expand Down
2 changes: 1 addition & 1 deletion p2p/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,4 @@ func (timeoutStore[H]) DeleteTo(ctx context.Context, _ uint64) error {
return ctx.Err()
}

func (timeoutStore[H]) OnDelete(fn func(context.Context, []H) error) {}
func (timeoutStore[H]) OnDelete(fn func(context.Context, uint64) error) {}
3 changes: 2 additions & 1 deletion p2p/subscriber.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"errors"
"fmt"
"sync"
"time"

pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/libp2p/go-libp2p/core/peer"
Expand Down Expand Up @@ -85,7 +86,7 @@
// be called separately to ensure a validator is mounted on the topic.
func (s *Subscriber[H]) Start(context.Context) (err error) {
log.Debugw("joining topic", "topic ID", s.pubsubTopicID)
err = s.pubsub.RegisterTopicValidator(s.pubsubTopicID, s.verifyMessage)
err = s.pubsub.RegisterTopicValidator(s.pubsubTopicID, s.verifyMessage, pubsub.WithValidatorTimeout(time.Minute))

Check failure on line 89 in p2p/subscriber.go

View workflow job for this annotation

GitHub Actions / build

File is not properly formatted (golines)
if err != nil {
return err
}
Expand Down
10 changes: 8 additions & 2 deletions store/height_indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ func newHeightIndexer[H header.Header[H]](
}

// HashByHeight loads a header hash corresponding to the given height.
func (hi *heightIndexer[H]) HashByHeight(ctx context.Context, h uint64) (header.Hash, error) {
func (hi *heightIndexer[H]) HashByHeight(
ctx context.Context,
h uint64,
cache bool,
) (header.Hash, error) {
if v, ok := hi.cache.Get(h); ok {
return v, nil
}
Expand All @@ -44,6 +48,8 @@ func (hi *heightIndexer[H]) HashByHeight(ctx context.Context, h uint64) (header.
return nil, err
}

hi.cache.Add(h, header.Hash(val))
if cache {
hi.cache.Add(h, header.Hash(val))
}
return val, nil
}
Loading
Loading