Skip to content

Commit 3f61d14

Browse files
julienrbrtrenaynayWondertan
authored
feat: Add DeleteRange method to Store (#347)
Implement `DeleteRange` on store. This methods truncates the head to the given height. (different from DeleteTo that truncates the tail to the given height) --------- Co-authored-by: rene <41963722+renaynay@users.noreply.github.com> Co-authored-by: Hlib Kanunnikov <hlibwondertan@gmail.com>
1 parent 3ca208c commit 3f61d14

File tree

9 files changed

+931
-184
lines changed

9 files changed

+931
-184
lines changed

.github/workflows/go-ci.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ on:
77
- main
88
pull_request:
99
jobs:
10-
1110
build:
1211
runs-on: ubuntu-latest
1312
steps:
@@ -28,8 +27,8 @@ jobs:
2827
file: ./coverage.o
2928

3029
- name: Lint
31-
uses: golangci/golangci-lint-action@v7
30+
uses: golangci/golangci-lint-action@v9
3231
with:
3332
version: latest
3433
- name: Go Mod Tidy
35-
run: go mod tidy && git diff --exit-code
34+
run: go mod tidy && git diff --exit-code

headertest/store.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,20 @@ func (m *Store[H]) GetByHeight(_ context.Context, height uint64) (H, error) {
8282
return zero, header.ErrNotFound
8383
}
8484

85-
func (m *Store[H]) DeleteTo(ctx context.Context, to uint64) error {
85+
func (m *Store[H]) DeleteRange(ctx context.Context, from, to uint64) error {
8686
m.HeaderMu.Lock()
8787
defer m.HeaderMu.Unlock()
88-
for h := m.TailHeight; h < to; h++ {
88+
89+
if from >= to {
90+
return fmt.Errorf("malformed range, from: %d, to: %d", from, to)
91+
}
92+
93+
if to > m.HeadHeight+1 {
94+
return fmt.Errorf("delete range to %d beyond current head+1(%d)", to, m.HeadHeight+1)
95+
}
96+
97+
// Delete headers in the range [from:to)
98+
for h := from; h < to; h++ {
8999
_, ok := m.Headers[h]
90100
if !ok {
91101
continue
@@ -100,7 +110,17 @@ func (m *Store[H]) DeleteTo(ctx context.Context, to uint64) error {
100110
delete(m.Headers, h) // must be after deleteFn
101111
}
102112

103-
m.TailHeight = to
113+
// Update TailHeight if we deleted from the beginning
114+
if from <= m.TailHeight {
115+
m.TailHeight = to
116+
}
117+
118+
// Update HeadHeight if we deleted from the end
119+
// Range is [from:to), so head is only affected if to > HeadHeight
120+
if to > m.HeadHeight {
121+
m.HeadHeight = from - 1
122+
}
123+
104124
return nil
105125
}
106126

interface.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,9 @@ type Store[H Header[H]] interface {
8585
// GetRange returns the range [from:to).
8686
GetRange(context.Context, uint64, uint64) ([]H, error)
8787

88-
// DeleteTo deletes the range [Tail():to).
89-
DeleteTo(ctx context.Context, to uint64) error
88+
// DeleteRange deletes the range [from:to).
89+
// It disallows the creation of gaps in the implementation's chain, ensuring contiguity between Tail --> Head.
90+
DeleteRange(ctx context.Context, from, to uint64) error
9091

9192
// OnDelete registers given handler to be called whenever a header with the height is being removed.
9293
// OnDelete guarantees that the header is accessible for the handler with GetByHeight and is removed

p2p/server.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,11 @@ func (serv *ExchangeServer[H]) requestHandler(stream network.Stream) {
115115
case *p2p_pb.HeaderRequest_Hash:
116116
headers, err = serv.handleRequestByHash(ctx, pbreq.GetHash())
117117
case *p2p_pb.HeaderRequest_Origin:
118-
headers, err = serv.handleRangeRequest(ctx, pbreq.GetOrigin(), pbreq.GetOrigin()+pbreq.Amount)
118+
headers, err = serv.handleRangeRequest(
119+
ctx,
120+
pbreq.GetOrigin(),
121+
pbreq.GetOrigin()+pbreq.Amount,
122+
)
119123
default:
120124
log.Warn("server: invalid data type received")
121125
stream.Reset() //nolint:errcheck

p2p/server_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ func (timeoutStore[H]) GetRange(ctx context.Context, _, _ uint64) ([]H, error) {
192192
return nil, ctx.Err()
193193
}
194194

195-
func (timeoutStore[H]) DeleteTo(ctx context.Context, _ uint64) error {
195+
func (timeoutStore[H]) DeleteRange(ctx context.Context, _, _ uint64) error {
196196
<-ctx.Done()
197197
return ctx.Err()
198198
}

0 commit comments

Comments
 (0)