periodic truncate triedb head

Signed-off-by: jsvisa <delweng@gmail.com>
This commit is contained in:
jsvisa 2025-06-20 19:51:48 +08:00
parent eaf712c80b
commit 5c2bf5b33a
2 changed files with 18 additions and 1 deletions

View file

@ -994,6 +994,15 @@ func (bc *BlockChain) setHeadBeyondRoot(head uint64, time uint64, root common.Ha
// and the current freezer limit to start nuking it's underflown. // and the current freezer limit to start nuking it's underflown.
pivot = rawdb.ReadLastPivotNumber(bc.db) pivot = rawdb.ReadLastPivotNumber(bc.db)
) )
const truncateInterval = 100
truncateFn := func() {
// Truncate triedb to align the state histories with the current state.
if bc.triedb.Scheme() == rawdb.PathScheme {
if err := bc.triedb.TruncateHead(); err != nil {
log.Crit("Failed to truncate trie database", "err", err)
}
}
}
updateFn := func(db ethdb.KeyValueWriter, header *types.Header) (*types.Header, bool) { updateFn := func(db ethdb.KeyValueWriter, header *types.Header) (*types.Header, bool) {
// Rewind the blockchain, ensuring we don't end up with a stateless head // Rewind the blockchain, ensuring we don't end up with a stateless head
// block. Note, depth equality is permitted to allow using SetHead as a // block. Note, depth equality is permitted to allow using SetHead as a
@ -1003,6 +1012,11 @@ func (bc *BlockChain) setHeadBeyondRoot(head uint64, time uint64, root common.Ha
newHeadBlock, rootNumber = bc.rewindHead(header, root) newHeadBlock, rootNumber = bc.rewindHead(header, root)
rawdb.WriteHeadBlockHash(db, newHeadBlock.Hash()) rawdb.WriteHeadBlockHash(db, newHeadBlock.Hash())
// The truncate costs a lot of time, no need to run for each block
if header.Number.Uint64()%truncateInterval == 0 {
truncateFn()
}
// Degrade the chain markers if they are explicitly reverted. // Degrade the chain markers if they are explicitly reverted.
// In theory we should update all in-memory markers in the // In theory we should update all in-memory markers in the
// last step, however the direction of SetHead is from high // last step, however the direction of SetHead is from high
@ -1096,6 +1110,9 @@ func (bc *BlockChain) setHeadBeyondRoot(head uint64, time uint64, root common.Ha
bc.blockCache.Purge() bc.blockCache.Purge()
bc.txLookupCache.Purge() bc.txLookupCache.Purge()
// Truncate to align the state histories with the current state.
truncateFn()
// Clear safe block, finalized block if needed // Clear safe block, finalized block if needed
if safe := bc.CurrentSafeBlock(); safe != nil && head < safe.Number.Uint64() { if safe := bc.CurrentSafeBlock(); safe != nil && head < safe.Number.Uint64() {
log.Warn("SetHead invalidated safe block") log.Warn("SetHead invalidated safe block")

View file

@ -281,7 +281,7 @@ func (db *Database) Recover(target common.Hash) error {
} }
// TruncateHead truncates all state histories in the freezer above the bottom state layer. // TruncateHead truncates all state histories in the freezer above the bottom state layer.
// This operation is only supported by path-based database It's typically used during // This operation is only supported by path-based database. It's typically used during
// state recovery to align the state histories with the current state. // state recovery to align the state histories with the current state.
func (db *Database) TruncateHead() error { func (db *Database) TruncateHead() error {
pdb, ok := db.backend.(*pathdb.Database) pdb, ok := db.backend.(*pathdb.Database)