Skip to content

Commit

Permalink
refactor(pruner): reset maxHeadersPerLoop properly (#3552)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg authored Jul 8, 2024
1 parent 1828f78 commit a81a43e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
8 changes: 4 additions & 4 deletions pruner/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// maxHeadersPerLoop is the maximum number of headers to fetch
// for a prune loop (prevents fetching too many headers at a
// time for nodes that have a large number of pruneable headers).
var maxHeadersPerLoop = uint64(512)
var maxHeadersPerLoop = 512

// findPruneableHeaders returns all headers that are eligible for pruning
// (outside the sampling window).
Expand Down Expand Up @@ -56,7 +56,7 @@ func (s *Service) findPruneableHeaders(
// loop we could increase by a range every iteration
headerCount := len(headers)
for {
if headerCount > int(maxHeadersPerLoop) {
if headerCount > maxHeadersPerLoop {
headers = headers[:maxHeadersPerLoop]
break
}
Expand Down Expand Up @@ -106,8 +106,8 @@ func (s *Service) calculateEstimatedCutoff(
estimatedCutoffHeight = head.Height()
}

if estimatedCutoffHeight-lastPruned.Height() > maxHeadersPerLoop {
estimatedCutoffHeight = lastPruned.Height() + maxHeadersPerLoop
if estimatedCutoffHeight-lastPruned.Height() > uint64(maxHeadersPerLoop) {
estimatedCutoffHeight = lastPruned.Height() + uint64(maxHeadersPerLoop)
}

return estimatedCutoffHeight, nil
Expand Down
2 changes: 1 addition & 1 deletion pruner/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (s *Service) prune(
return lastPrunedHeader
}

if uint64(len(headers)) < maxHeadersPerLoop {
if len(headers) < maxHeadersPerLoop {
// we've pruned all the blocks we can
return lastPrunedHeader
}
Expand Down
11 changes: 5 additions & 6 deletions pruner/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,17 @@ func TestPrune_LargeNumberOfBlocks(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

maxHeadersPerLoop = 10
t.Cleanup(func() {
maxHeadersPerLoop = 1024
})
var maxHeadersPerLoopOld int
maxHeadersPerLoopOld, maxHeadersPerLoop = maxHeadersPerLoop, 10
t.Cleanup(func() { maxHeadersPerLoop = maxHeadersPerLoopOld })

blockTime := time.Nanosecond
availabilityWindow := AvailabilityWindow(blockTime * 10)

// all headers generated in suite are timestamped to time.Now(), so
// they will all be considered "pruneable" within the availability window
suite := headertest.NewTestSuite(t, 1, blockTime)
store := headertest.NewCustomStore(t, suite, int(maxHeadersPerLoop*6)) // add small buffer
store := headertest.NewCustomStore(t, suite, maxHeadersPerLoop*6) // add small buffer

mp := &mockPruner{failHeight: make(map[uint64]int, 0)}

Expand All @@ -196,7 +195,7 @@ func TestPrune_LargeNumberOfBlocks(t *testing.T) {
_ = serv.prune(ctx, lastPruned)

// ensure all headers have been pruned
assert.Equal(t, maxHeadersPerLoop*5, serv.checkpoint.LastPrunedHeight)
assert.Equal(t, uint64(maxHeadersPerLoop*5), serv.checkpoint.LastPrunedHeight)
assert.Len(t, serv.checkpoint.FailedHeaders, 0)
}

Expand Down

0 comments on commit a81a43e

Please sign in to comment.