Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 2 additions & 3 deletions .github/workflows/go-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ on:
- main
pull_request:
jobs:

build:
runs-on: ubuntu-latest
steps:
Expand All @@ -28,8 +27,8 @@ jobs:
file: ./coverage.o

- name: Lint
uses: golangci/golangci-lint-action@v7
uses: golangci/golangci-lint-action@v9
with:
version: latest
- name: Go Mod Tidy
run: go mod tidy && git diff --exit-code
run: go mod tidy && git diff --exit-code
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++ {
_, 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
}

// 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).
// 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
6 changes: 5 additions & 1 deletion p2p/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ func (serv *ExchangeServer[H]) requestHandler(stream network.Stream) {
case *p2p_pb.HeaderRequest_Hash:
headers, err = serv.handleRequestByHash(ctx, pbreq.GetHash())
case *p2p_pb.HeaderRequest_Origin:
headers, err = serv.handleRangeRequest(ctx, pbreq.GetOrigin(), pbreq.GetOrigin()+pbreq.Amount)
headers, err = serv.handleRangeRequest(
ctx,
pbreq.GetOrigin(),
pbreq.GetOrigin()+pbreq.Amount,
)
default:
log.Warn("server: invalid data type received")
stream.Reset() //nolint:errcheck
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