Skip to content

Commit

Permalink
change StableStateBloc to SafePointBlock and enhanced 'pruneancient' …
Browse files Browse the repository at this point in the history
…flag hints
  • Loading branch information
actioncli committed Jun 20, 2022
1 parent 2e6bd89 commit 1be0f49
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 16 deletions.
2 changes: 1 addition & 1 deletion cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ var (
}
PruneAncientDataFlag = cli.BoolFlag{
Name: "pruneancient",
Usage: "Keep a fixed number of blocks(9w) in kvstore under full syncmode, enable release more os resources",
Usage: "Prune ancient data, recommends to the user who don't care about the ancient data. Note that once be turned on, the ancient data will not be recovered",
}
// Miner settings
MiningEnabledFlag = cli.BoolFlag{
Expand Down
12 changes: 8 additions & 4 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,8 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
}
bc.snaps, _ = snapshot.New(bc.db, bc.stateCache.TrieDB(), bc.cacheConfig.SnapshotLimit, int(bc.cacheConfig.TriesInMemory), head.Root(), !bc.cacheConfig.SnapshotWait, true, recover)
}
// write stable state block number
rawdb.WriteStableStateBlockNumber(bc.db, bc.CurrentBlock().NumberU64())
// write safe point block number
rawdb.WriteSafePointBlockNumber(bc.db, bc.CurrentBlock().NumberU64())
// do options before start any routine
for _, option := range options {
bc = option(bc)
Expand Down Expand Up @@ -1262,13 +1262,17 @@ func (bc *BlockChain) Stop() {
log.Info("Writing cached state to disk", "block", recent.Number(), "hash", recent.Hash(), "root", recent.Root())
if err := triedb.Commit(recent.Root(), true, nil); err != nil {
log.Error("Failed to commit recent state trie", "err", err)
} else {
rawdb.WriteSafePointBlockNumber(bc.db, recent.NumberU64())
}
}
}
if snapBase != (common.Hash{}) {
log.Info("Writing snapshot state to disk", "root", snapBase)
if err := triedb.Commit(snapBase, true, nil); err != nil {
log.Error("Failed to commit recent state trie", "err", err)
} else {
rawdb.WriteSafePointBlockNumber(bc.db, bc.CurrentBlock().NumberU64())
}
}
for !bc.triegc.Empty() {
Expand Down Expand Up @@ -1768,7 +1772,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
}
// Flush an entire trie and restart the counters
triedb.Commit(header.Root, true, nil)
rawdb.WriteStableStateBlockNumber(bc.db, current)
rawdb.WriteSafePointBlockNumber(bc.db, current)
lastWrite = chosen
bc.gcproc = 0
}
Expand Down Expand Up @@ -2361,7 +2365,7 @@ func (bc *BlockChain) insertSideChain(block *types.Block, it *insertIterator) (i
// Append the next block to our batch
block := bc.GetBlock(hashes[i], numbers[i])
if block == nil {
continue
log.Crit("Importing heavy sidechain block is nil", "hash", hashes[i], "number", numbers[i])
}

blocks = append(blocks, block)
Expand Down
12 changes: 6 additions & 6 deletions core/rawdb/accessors_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,18 +194,18 @@ func WriteFrozenOfAncientFreezer(db ethdb.KeyValueWriter, frozen uint64) {
}
}

// ReadStableStateBlockNumber return the number of block that roothash save to disk
func ReadStableStateBlockNumber(db ethdb.KeyValueReader) uint64 {
num, _ := db.Get(stableStateBlockNumberKey)
// ReadSafePointBlockNumber return the number of block that roothash save to disk
func ReadSafePointBlockNumber(db ethdb.KeyValueReader) uint64 {
num, _ := db.Get(LastSafePointBlockKey)
if num == nil {
return 0
}
return new(big.Int).SetBytes(num).Uint64()
}

// WriteStableStateBlockNumber write the number of block that roothash save to disk
func WriteStableStateBlockNumber(db ethdb.KeyValueWriter, number uint64) {
if err := db.Put(stableStateBlockNumberKey, new(big.Int).SetUint64(number).Bytes()); err != nil {
// WriteSafePointBlockNumber write the number of block that roothash save to disk
func WriteSafePointBlockNumber(db ethdb.KeyValueWriter, number uint64) {
if err := db.Put(LastSafePointBlockKey, new(big.Int).SetUint64(number).Bytes()); err != nil {
log.Crit("Failed to store offSetOfAncientFreezer", "err", err)
}
}
Expand Down
5 changes: 2 additions & 3 deletions core/rawdb/prunedfreezer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/prometheus/tsdb/fileutil"
)

// nodatafreezer is an empty freezer, only record 'frozen' , the next recycle block number form kvstore.
// prunedfreezer not contain ancient data, only record 'frozen' , the next recycle block number form kvstore.
type prunedfreezer struct {
db ethdb.KeyValueStore // Meta database
// WARNING: The `frozen` field is accessed atomically. On 32 bit platforms, only
Expand Down Expand Up @@ -225,7 +225,7 @@ func (f *prunedfreezer) freeze() {
continue
}

stableStabeNumber := ReadStableStateBlockNumber(nfdb)
stableStabeNumber := ReadSafePointBlockNumber(nfdb)
switch {
case stableStabeNumber < params.StableStateThreshold:
log.Debug("Stable state block not old enough", "number", stableStabeNumber)
Expand Down Expand Up @@ -279,7 +279,6 @@ func (f *prunedfreezer) freeze() {
if err := f.Sync(); err != nil {
log.Crit("Failed to flush frozen tables", "err", err)
}
// Batch of blocks have been frozen, flush them before wiping from leveldb
backoff = f.frozen-first >= freezerBatchLimit
gcKvStore(f.db, ancients, first, f.frozen, start)
}
Expand Down
4 changes: 2 additions & 2 deletions core/rawdb/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ var (
//frozenOfAncientDBKey tracks the block number for ancientDB to save.
frozenOfAncientDBKey = []byte("FrozenOfAncientDB")

//stableStateBlockNumberKey tracks the block number for block state that write disk
stableStateBlockNumberKey = []byte("StableStateBlockNumber")
//LastSafePointBlockKey tracks the block number for block state that write disk
LastSafePointBlockKey = []byte("LastSafePointBlockNumber")

//PruneAncientFlag flag whether prune ancient
pruneAncientKey = []byte("PruneAncientFlag")
Expand Down

0 comments on commit 1be0f49

Please sign in to comment.