diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index f8f736ae6b..48e8a418a7 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -1076,7 +1076,7 @@ var ( SnapshotKeepAfterPruningFlag = &cli.IntFlag{ Name: "snapshot.keep-after-pruning", - Usage: "The number of lastest snapshots to keep after pruning (default 50)", + Usage: "The number of lastest snapshots to keep after pruning (default 200 * 144 = 28800 ~ 1 day)", Value: 200 * 144, } ) diff --git a/consensus/consortium/v2/snapshot.go b/consensus/consortium/v2/snapshot.go index c903379dc1..dda5b30b26 100644 --- a/consensus/consortium/v2/snapshot.go +++ b/consensus/consortium/v2/snapshot.go @@ -21,6 +21,17 @@ import ( lru "github.com/hashicorp/golang-lru" ) +const ( + blocksPerEpoch = 200 + epochsPerPeriod = 144 +) + +var ( + latestSnapshotsKeep = blocksPerEpoch * epochsPerPeriod * 5 // 5 days + snapshotsToBePruned = epochsPerPeriod * 2 // 2 days + pruningPeriod = blocksPerEpoch * epochsPerPeriod * 1 // every 1 day +) + // Snapshot is the state of the authorization validators at a given point in time. type Snapshot struct { // private fields are not json.Marshalled @@ -119,8 +130,8 @@ func (s *Snapshot) pruneSnapshot(db ethdb.Database, nSnapshotPrune int, chain co log.Info("Pruning snapshots at block", "block", s.Number, "nSnapshotPrune", nSnapshotPrune) // Get block number to start pruning curBlockNumber := s.Number - curBlockNumber -= curBlockNumber % 200 // make sure the block number is multiple of 200 - curBlockNumber -= 200 * 144 * 5 // keep the last 5 days of snapshots + curBlockNumber -= curBlockNumber % uint64(blocksPerEpoch) // start of the current epoch + curBlockNumber -= uint64(latestSnapshotsKeep) // start of the oldest epoch to keep // delete nSnapshotPrune snapshots starting from curBlockNumber to the older ones batch := db.NewBatch() @@ -135,25 +146,28 @@ func (s *Snapshot) pruneSnapshot(db ethdb.Database, nSnapshotPrune int, chain co if err := batch.Delete(append(rawdb.ConsortiumSnapshotPrefix, curHash[:]...)); err != nil { return err } - curBlockNumber -= 200 + curBlockNumber -= uint64(blocksPerEpoch) } log.Info("Pruned snapshots done") return batch.Write() } +func (s *Snapshot) pruneSnapshotPeriodically(db ethdb.Database, chain consensus.ChainHeaderReader) error { + if s.Number%uint64(pruningPeriod) == 0 { + return s.pruneSnapshot(db, snapshotsToBePruned, chain) + } + return nil +} + // store inserts the snapshot into the database. func (s *Snapshot) store(db ethdb.Database, chain consensus.ChainHeaderReader) error { blob, err := json.Marshal(s) if err != nil { return err } - // prune nSnapshotPrune snapshots every prunePeriod blocks - nSnapshotPrune := 144 * 2 // 2 days - prunePeriod := 200 * 144 // 1 day - if s.Number%uint64(prunePeriod) == 0 { - if err := s.pruneSnapshot(db, nSnapshotPrune, chain); err != nil { - log.Error("Failed to prune snapshots", "err", err) - } + err = s.pruneSnapshotPeriodically(db, chain) + if err != nil { + log.Error("Failed to prune snapshots", "err", err) } return db.Put(append(rawdb.ConsortiumSnapshotPrefix, s.Hash[:]...), blob) }