Skip to content
This repository was archived by the owner on Jun 6, 2025. It is now read-only.

Commit d1b48b4

Browse files
committed
consortium-v2/snapshot: add pruneSnapshotPeriodically
pruneSnapshot: delete the nSnapshotsPrune oldest snapshots, keep the latestSnapshotsKeep snapshots pruneSnapshotPeriodically: prune the snapshots at the start of each pruningPeriod
1 parent 4bfb2d6 commit d1b48b4

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

consensus/consortium/v2/consortium.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,7 @@ func (c *Consortium) verifyCascadingFields(chain consensus.ChainHeaderReader, he
465465
err = c.verifyValidatorFieldsInExtraData(chain, extraData, header)
466466
if err != nil {
467467
return err
468+
468469
}
469470

470471
if isShillin && extraData.HasFinalityVote == 1 {
@@ -629,6 +630,9 @@ func (c *Consortium) snapshot(chain consensus.ChainHeaderReader, number uint64,
629630
if err := snap.store(c.db); err != nil {
630631
return nil, err
631632
}
633+
if err := snap.pruneSnapshotPeriodically(c.db, chain); err != nil {
634+
return nil, err
635+
}
632636
log.Info("Stored checkpoint snapshot to disk", "number", number, "hash", hash)
633637
figure.NewColorFigure("Welcome to DPOS", "", "green", true).Print()
634638
break
@@ -688,6 +692,10 @@ func (c *Consortium) snapshot(chain consensus.ChainHeaderReader, number uint64,
688692
if err = snap.store(c.db); err != nil {
689693
return nil, err
690694
}
695+
// Prune the snapshot periodically
696+
if err := snap.pruneSnapshotPeriodically(c.db, chain); err != nil {
697+
return nil, err
698+
}
691699
log.Trace("Stored snapshot to disk", "number", snap.Number, "hash", snap.Hash)
692700
}
693701
log.Trace("Checking snapshot data", "number", snap.Number, "validators", snap.validators())
@@ -1832,6 +1840,7 @@ func (c *Consortium) IsPeriodBlock(chain consensus.ChainHeaderReader, header *ty
18321840
if c.isTest {
18331841
return c.testTrippPeriod
18341842
}
1843+
18351844
number := header.Number.Uint64()
18361845
if number%c.config.EpochV2 != 0 || !chain.Config().IsTripp(header.Number) {
18371846
return false

consensus/consortium/v2/snapshot.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,22 @@ import (
1616
blsCommon "github.com/ethereum/go-ethereum/crypto/bls/common"
1717
"github.com/ethereum/go-ethereum/ethdb"
1818
"github.com/ethereum/go-ethereum/internal/ethapi"
19+
"github.com/ethereum/go-ethereum/log"
1920
"github.com/ethereum/go-ethereum/params"
2021
lru "github.com/hashicorp/golang-lru"
2122
)
2223

24+
const (
25+
blocksPerEpoch = 200
26+
epochsPerPeriod = 144
27+
)
28+
29+
var (
30+
latestSnapshotsKeep = blocksPerEpoch * epochsPerPeriod * 5 // 5 days
31+
snapshotsToBePruned = epochsPerPeriod * 2 // 2 days
32+
pruningPeriod = blocksPerEpoch * epochsPerPeriod * 1 // every 1 day
33+
)
34+
2335
// Snapshot is the state of the authorization validators at a given point in time.
2436
type Snapshot struct {
2537
// private fields are not json.Marshalled
@@ -112,6 +124,42 @@ func loadSnapshot(
112124
return snap, nil
113125
}
114126

127+
// snapshot pruning
128+
// delete the nSnapshotsPrune oldest snapshots, keep the latestSnapshotsKeep snapshots
129+
func (s *Snapshot) pruneSnapshot(db ethdb.Database, nSnapshotPrune int, chain consensus.ChainHeaderReader) error {
130+
log.Info("Pruning snapshots at block", "block", s.Number, "nSnapshotPrune", nSnapshotPrune)
131+
// Get block number to start pruning
132+
curBlockNumber := s.Number
133+
curBlockNumber -= curBlockNumber % uint64(blocksPerEpoch) // start of the current epoch
134+
curBlockNumber -= uint64(latestSnapshotsKeep) // start of the oldest epoch to keep
135+
136+
// delete nSnapshotPrune snapshots starting from curBlockNumber to the older ones
137+
batch := db.NewBatch()
138+
for nSnapshotPrune > 0 {
139+
nSnapshotPrune--
140+
header := chain.GetHeaderByNumber(curBlockNumber)
141+
if header == nil {
142+
// no more snapshots to prune
143+
break
144+
}
145+
curHash := header.Hash()
146+
if err := batch.Delete(append(rawdb.ConsortiumSnapshotPrefix, curHash[:]...)); err != nil {
147+
return err
148+
}
149+
curBlockNumber -= uint64(blocksPerEpoch)
150+
}
151+
log.Info("Pruned snapshots done")
152+
return batch.Write()
153+
}
154+
155+
// periodically prune the snapshots at the start of each pruningPeriod
156+
func (s *Snapshot) pruneSnapshotPeriodically(db ethdb.Database, chain consensus.ChainHeaderReader) error {
157+
if s.Number%uint64(pruningPeriod) == 0 {
158+
return s.pruneSnapshot(db, snapshotsToBePruned, chain)
159+
}
160+
return nil
161+
}
162+
115163
// store inserts the snapshot into the database.
116164
func (s *Snapshot) store(db ethdb.Database) error {
117165
blob, err := json.Marshal(s)

0 commit comments

Comments
 (0)