Skip to content

Commit

Permalink
v2: set constants and varialbes
Browse files Browse the repository at this point in the history
  • Loading branch information
Francesco4203 committed Jul 8, 2024
1 parent 90bd034 commit a95579f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
)
Expand Down
34 changes: 24 additions & 10 deletions consensus/consortium/v2/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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)
}
Expand Down

0 comments on commit a95579f

Please sign in to comment.