-
Notifications
You must be signed in to change notification settings - Fork 972
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nodebuilder/prune): Enable sampling window for light nodes
prog prog prog prog prog prog feat: initial untested findPruneableHeaders impl change provides in nodebuilder provide properly to fx
- Loading branch information
Showing
12 changed files
with
362 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package prune | ||
|
||
import ( | ||
"github.com/ipfs/go-datastore" | ||
|
||
hdr "github.com/celestiaorg/go-header" | ||
|
||
"github.com/celestiaorg/celestia-node/header" | ||
"github.com/celestiaorg/celestia-node/nodebuilder/p2p" | ||
"github.com/celestiaorg/celestia-node/pruner" | ||
) | ||
|
||
func newPrunerService( | ||
p pruner.Pruner, | ||
window pruner.AvailabilityWindow, | ||
getter hdr.Store[*header.ExtendedHeader], | ||
ds datastore.Batching, | ||
opts ...pruner.Option, | ||
) *pruner.Service { | ||
// TODO @renaynay: remove this once pruning implementation | ||
opts = append(opts, pruner.WithDisabledGC()) | ||
return pruner.NewService(p, window, getter, ds, p2p.BlockTime, opts...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package pruner | ||
|
||
import ( | ||
"context" | ||
"sync/atomic" | ||
"time" | ||
|
||
"github.com/ipfs/go-datastore" | ||
|
||
"github.com/celestiaorg/celestia-node/header" | ||
) | ||
|
||
var ( | ||
lastPrunedHeaderKey = datastore.NewKey("last_pruned_header") | ||
) | ||
|
||
type checkpoint struct { | ||
ds datastore.Datastore | ||
|
||
lastPrunedHeader atomic.Pointer[header.ExtendedHeader] | ||
|
||
// TODO @renaynay: keep track of failed roots to retry in separate job | ||
} | ||
|
||
func newCheckpoint(ds datastore.Datastore) *checkpoint { | ||
return &checkpoint{ds: ds} | ||
} | ||
|
||
// findPruneableHeaders returns all headers that are eligible for pruning | ||
// (outside the sampling window). | ||
func (s *Service) findPruneableHeaders(ctx context.Context) ([]*header.ExtendedHeader, error) { | ||
lastPruned := s.lastPruned() | ||
pruneCutoff := time.Now().Add(time.Duration(-s.window)) | ||
estimatedCutoffHeight := lastPruned.Height() + s.numBlocksInWindow | ||
|
||
headers, err := s.getter.GetRangeByHeight(ctx, lastPruned, estimatedCutoffHeight) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// if our estimated range didn't cover enough headers, we need to fetch more | ||
for { | ||
lastHeader := headers[len(headers)-1] | ||
if lastHeader.Time().After(pruneCutoff) { | ||
break | ||
} | ||
|
||
nextHeader, err := s.getter.GetByHeight(ctx, lastHeader.Height()+1) | ||
if err != nil { | ||
return nil, err | ||
} | ||
headers = append(headers, nextHeader) | ||
} | ||
|
||
for i, h := range headers { | ||
if h.Time().After(pruneCutoff) { | ||
if i == 0 { | ||
// we can't prune anything | ||
return nil, nil | ||
} | ||
|
||
// we can ignore the rest of the headers since they are all newer than the cutoff | ||
return headers[:i-1], nil | ||
} | ||
} | ||
return headers, nil | ||
} | ||
|
||
// updateCheckpoint updates the checkpoint with the last pruned header height | ||
// and persists it to disk. | ||
func (s *Service) updateCheckpoint(ctx context.Context, lastPruned *header.ExtendedHeader) error { | ||
s.checkpoint.lastPrunedHeader.Store(lastPruned) | ||
|
||
bin, err := lastPruned.MarshalJSON() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return s.checkpoint.ds.Put(ctx, lastPrunedHeaderKey, bin) | ||
} | ||
|
||
func (s *Service) lastPruned() *header.ExtendedHeader { | ||
return s.checkpoint.lastPrunedHeader.Load() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package pruner | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type Option func(*Params) | ||
|
||
type Params struct { | ||
// gcCycle is the frequency at which the pruning Service | ||
// runs the ticker. If set to 0, the Service will not run. | ||
gcCycle time.Duration | ||
} | ||
|
||
func DefaultParams() Params { | ||
return Params{ | ||
gcCycle: time.Hour, | ||
} | ||
} | ||
|
||
// WithGCCycle configures how often the pruning Service | ||
// triggers a pruning cycle. | ||
func WithGCCycle(cycle time.Duration) Option { | ||
return func(p *Params) { | ||
p.gcCycle = cycle | ||
} | ||
} | ||
|
||
// WithDisabledGC disables the pruning Service's pruning | ||
// routine. | ||
func WithDisabledGC() Option { | ||
return func(p *Params) { | ||
p.gcCycle = time.Duration(0) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.