Skip to content
Merged
Show file tree
Hide file tree
Changes from 18 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
26 changes: 23 additions & 3 deletions headertest/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,20 @@ func (m *Store[H]) GetByHeight(_ context.Context, height uint64) (H, error) {
return zero, header.ErrNotFound
}

func (m *Store[H]) DeleteTo(ctx context.Context, to uint64) error {
func (m *Store[H]) DeleteRange(ctx context.Context, from, to uint64) error {
m.HeaderMu.Lock()
defer m.HeaderMu.Unlock()
for h := m.TailHeight; h < to; h++ {

if from >= to {
return fmt.Errorf("malformed range, from: %d, to: %d", from, to)
}

if to > m.HeadHeight+1 {
return fmt.Errorf("delete range to %d beyond current head+1(%d)", to, m.HeadHeight+1)
}

// Delete headers in the range [from:to)
for h := from; h < to; h++ {
Comment thread
julienrbrt marked this conversation as resolved.
_, ok := m.Headers[h]
if !ok {
continue
Expand All @@ -100,7 +110,17 @@ func (m *Store[H]) DeleteTo(ctx context.Context, to uint64) error {
delete(m.Headers, h) // must be after deleteFn
}

m.TailHeight = to
// Update TailHeight if we deleted from the beginning
if from <= m.TailHeight {
m.TailHeight = to
}
Comment thread
julienrbrt marked this conversation as resolved.

// Update HeadHeight if we deleted from the end
// Range is [from:to), so head is only affected if to > HeadHeight
if to > m.HeadHeight {
m.HeadHeight = from - 1
}

return nil
}

Expand Down
5 changes: 3 additions & 2 deletions interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ type Store[H Header[H]] interface {
// GetRange returns the range [from:to).
GetRange(context.Context, uint64, uint64) ([]H, error)

// DeleteTo deletes the range [Tail():to).
DeleteTo(ctx context.Context, to uint64) error
// DeleteRange deletes the range [from:to).
Comment thread
julienrbrt marked this conversation as resolved.
// It disallows the creation of gaps in the implementation's chain, ensuring contiguity between Tail --> Head.
DeleteRange(ctx context.Context, from, to uint64) 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
Expand Down
2 changes: 1 addition & 1 deletion p2p/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func (timeoutStore[H]) GetRange(ctx context.Context, _, _ uint64) ([]H, error) {
return nil, ctx.Err()
}

func (timeoutStore[H]) DeleteTo(ctx context.Context, _ uint64) error {
func (timeoutStore[H]) DeleteRange(ctx context.Context, _, _ uint64) error {
<-ctx.Done()
return ctx.Err()
}
Expand Down
Loading