Skip to content

Commit cda2d67

Browse files
committed
feat: add AsyncPruner
1 parent 99dd5fe commit cda2d67

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

block/async_store.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package block
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
"github.com/rollkit/rollkit/pkg/store"
8+
)
9+
10+
const DefaultFlushInterval = 1 * time.Second
11+
12+
// AsyncPruner is a service that periodically prunes block data in the background.
13+
type AsyncPruner struct {
14+
ps store.PruningStore
15+
16+
flushInterval time.Duration
17+
}
18+
19+
func NewAsyncPruner(pruningStore store.PruningStore, flushInterval time.Duration) *AsyncPruner {
20+
if flushInterval <= 0 {
21+
flushInterval = DefaultFlushInterval
22+
}
23+
24+
return &AsyncPruner{
25+
ps: pruningStore,
26+
27+
flushInterval: flushInterval,
28+
}
29+
}
30+
31+
// Start starts the async pruner that periodically prunes block data.
32+
func (s *AsyncPruner) Start(ctx context.Context) {
33+
ticker := time.NewTicker(s.flushInterval)
34+
defer ticker.Stop()
35+
36+
for {
37+
select {
38+
case <-ctx.Done():
39+
return
40+
case <-ticker.C:
41+
// Currently PruneBlockData only returns nil.
42+
_ = s.ps.PruneBlockData(ctx)
43+
}
44+
}
45+
}

node/full.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ type FullNode struct {
6161
Store store.Store
6262
blockManager *block.Manager
6363
reaper *block.Reaper
64+
asyncPruner *block.AsyncPruner
6465

6566
prometheusSrv *http.Server
6667
pprofSrv *http.Server
@@ -129,12 +130,17 @@ func newFullNode(
129130
// Connect the reaper to the manager for transaction notifications
130131
reaper.SetManager(blockManager)
131132

133+
asyncPruner := block.NewAsyncPruner(
134+
rktStore, block.DefaultFlushInterval,
135+
)
136+
132137
node := &FullNode{
133138
genesis: genesis,
134139
nodeConfig: nodeConfig,
135140
p2pClient: p2pClient,
136141
blockManager: blockManager,
137142
reaper: reaper,
143+
asyncPruner: asyncPruner,
138144
da: da,
139145
Store: rktStore,
140146
hSyncService: headerSyncService,
@@ -390,6 +396,7 @@ func (n *FullNode) Run(parentCtx context.Context) error {
390396
spawnWorker(func() { n.blockManager.SyncLoop(ctx, errCh) })
391397
spawnWorker(func() { n.blockManager.DAIncluderLoop(ctx, errCh) })
392398
}
399+
spawnWorker(func() { n.asyncPruner.Start(ctx) })
393400

394401
select {
395402
case err := <-errCh:

0 commit comments

Comments
 (0)