-
Notifications
You must be signed in to change notification settings - Fork 20.8k
core, eth, les, tests, trie: abstract node scheme #25532
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -169,10 +169,12 @@ type BlockChain struct { | |
chainConfig *params.ChainConfig // Chain & network configuration | ||
cacheConfig *CacheConfig // Cache configuration for pruning | ||
|
||
db ethdb.Database // Low level persistent database to store final content in | ||
snaps *snapshot.Tree // Snapshot tree for fast trie leaf access | ||
triegc *prque.Prque // Priority queue mapping block numbers to tries to gc | ||
gcproc time.Duration // Accumulates canonical block processing for trie dumping | ||
db ethdb.Database // Low level persistent database to store final content in | ||
snaps *snapshot.Tree // Snapshot tree for fast trie leaf access | ||
triegc *prque.Prque // Priority queue mapping block numbers to tries to gc | ||
gcproc time.Duration // Accumulates canonical block processing for trie dumping | ||
triedb *trie.Database // The database handler for maintaining trie nodes. | ||
stateCache state.Database // State database to reuse between imports (contains state cache) | ||
|
||
// txLookupLimit is the maximum number of blocks from head whose tx indices | ||
// are reserved: | ||
|
@@ -200,7 +202,6 @@ type BlockChain struct { | |
currentFinalizedBlock atomic.Value // Current finalized head | ||
currentSafeBlock atomic.Value // Current safe head | ||
|
||
stateCache state.Database // State database to reuse between imports (contains state cache) | ||
bodyCache *lru.Cache[common.Hash, *types.Body] | ||
bodyRLPCache *lru.Cache[common.Hash, rlp.RawValue] | ||
receiptsCache *lru.Cache[common.Hash, []*types.Receipt] | ||
|
@@ -231,10 +232,16 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis | |
cacheConfig = defaultCacheConfig | ||
} | ||
|
||
// Open trie database with provided config | ||
triedb := trie.NewDatabaseWithConfig(db, &trie.Config{ | ||
Cache: cacheConfig.TrieCleanLimit, | ||
Journal: cacheConfig.TrieCleanJournal, | ||
Preimages: cacheConfig.Preimages, | ||
}) | ||
// Setup the genesis block, commit the provided genesis specification | ||
// to database if the genesis block is not present yet, or load the | ||
// stored one from database. | ||
chainConfig, genesisHash, genesisErr := SetupGenesisBlockWithOverride(db, genesis, overrides) | ||
chainConfig, genesisHash, genesisErr := SetupGenesisBlockWithOverride(db, triedb, genesis, overrides) | ||
if _, ok := genesisErr.(*params.ConfigCompatError); genesisErr != nil && !ok { | ||
return nil, genesisErr | ||
} | ||
|
@@ -247,15 +254,11 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis | |
log.Info("") | ||
|
||
bc := &BlockChain{ | ||
chainConfig: chainConfig, | ||
cacheConfig: cacheConfig, | ||
db: db, | ||
triegc: prque.New(nil), | ||
stateCache: state.NewDatabaseWithConfig(db, &trie.Config{ | ||
Cache: cacheConfig.TrieCleanLimit, | ||
Journal: cacheConfig.TrieCleanJournal, | ||
Preimages: cacheConfig.Preimages, | ||
}), | ||
chainConfig: chainConfig, | ||
cacheConfig: cacheConfig, | ||
db: db, | ||
triedb: triedb, | ||
triegc: prque.New(nil), | ||
quit: make(chan struct{}), | ||
chainmu: syncx.NewClosableMutex(), | ||
bodyCache: lru.NewCache[common.Hash, *types.Body](bodyCacheLimit), | ||
|
@@ -268,6 +271,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis | |
vmConfig: vmConfig, | ||
} | ||
bc.forker = NewForkChoice(bc, shouldPreserve) | ||
bc.stateCache = state.NewDatabaseWithNodeDB(bc.db, bc.triedb) | ||
bc.validator = NewBlockValidator(chainConfig, bc, engine) | ||
bc.prefetcher = newStatePrefetcher(chainConfig, bc, engine) | ||
bc.processor = NewStateProcessor(chainConfig, bc, engine) | ||
|
@@ -300,7 +304,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis | |
} | ||
// Make sure the state associated with the block is available | ||
head := bc.CurrentBlock() | ||
if _, err := state.New(head.Root(), bc.stateCache, bc.snaps); err != nil { | ||
if !bc.HasState(head.Root()) { | ||
// Head state is missing, before the state recovery, find out the | ||
// disk layer point of snapshot(if it's enabled). Make sure the | ||
// rewound point is lower than disk layer. | ||
|
@@ -388,7 +392,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis | |
var recover bool | ||
|
||
head := bc.CurrentBlock() | ||
if layer := rawdb.ReadSnapshotRecoveryNumber(bc.db); layer != nil && *layer > head.NumberU64() { | ||
if layer := rawdb.ReadSnapshotRecoveryNumber(bc.db); layer != nil && *layer >= head.NumberU64() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was a mistake before. If we rewind the chain state to disk layer, then in this case recovery mode should be enabled. |
||
log.Warn("Enabling snapshot recovery", "chainhead", head.NumberU64(), "diskbase", *layer) | ||
recover = true | ||
} | ||
|
@@ -398,7 +402,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis | |
NoBuild: bc.cacheConfig.SnapshotNoBuild, | ||
AsyncBuild: !bc.cacheConfig.SnapshotWait, | ||
} | ||
bc.snaps, _ = snapshot.New(snapconfig, bc.db, bc.stateCache.TrieDB(), head.Root()) | ||
bc.snaps, _ = snapshot.New(snapconfig, bc.db, bc.triedb, head.Root()) | ||
} | ||
|
||
// Start future block processor. | ||
|
@@ -411,11 +415,10 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis | |
log.Warn("Sanitizing invalid trie cache journal time", "provided", bc.cacheConfig.TrieCleanRejournal, "updated", time.Minute) | ||
bc.cacheConfig.TrieCleanRejournal = time.Minute | ||
} | ||
triedb := bc.stateCache.TrieDB() | ||
bc.wg.Add(1) | ||
go func() { | ||
defer bc.wg.Done() | ||
triedb.SaveCachePeriodically(bc.cacheConfig.TrieCleanJournal, bc.cacheConfig.TrieCleanRejournal, bc.quit) | ||
bc.triedb.SaveCachePeriodically(bc.cacheConfig.TrieCleanJournal, bc.cacheConfig.TrieCleanRejournal, bc.quit) | ||
}() | ||
} | ||
// Rewind the chain in case of an incompatible config upgrade. | ||
|
@@ -594,7 +597,7 @@ func (bc *BlockChain) setHeadBeyondRoot(head uint64, root common.Hash, repair bo | |
if root != (common.Hash{}) && !beyondRoot && newHeadBlock.Root() == root { | ||
beyondRoot, rootNumber = true, newHeadBlock.NumberU64() | ||
} | ||
if _, err := state.New(newHeadBlock.Root(), bc.stateCache, bc.snaps); err != nil { | ||
if !bc.HasState(newHeadBlock.Root()) { | ||
log.Trace("Block state missing, rewinding further", "number", newHeadBlock.NumberU64(), "hash", newHeadBlock.Hash()) | ||
if pivot == nil || newHeadBlock.NumberU64() > *pivot { | ||
parent := bc.GetBlock(newHeadBlock.ParentHash(), newHeadBlock.NumberU64()-1) | ||
|
@@ -617,7 +620,7 @@ func (bc *BlockChain) setHeadBeyondRoot(head uint64, root common.Hash, repair bo | |
// if the historical chain pruning is enabled. In that case the logic | ||
// needs to be improved here. | ||
if !bc.HasState(bc.genesisBlock.Root()) { | ||
if err := CommitGenesisState(bc.db, bc.genesisBlock.Hash()); err != nil { | ||
if err := CommitGenesisState(bc.db, bc.triedb, bc.genesisBlock.Hash()); err != nil { | ||
log.Crit("Failed to commit genesis state", "err", err) | ||
} | ||
log.Debug("Recommitted genesis state to disk") | ||
|
@@ -900,7 +903,7 @@ func (bc *BlockChain) Stop() { | |
// - HEAD-1: So we don't do large reorgs if our HEAD becomes an uncle | ||
// - HEAD-127: So we have a hard limit on the number of blocks reexecuted | ||
if !bc.cacheConfig.TrieDirtyDisabled { | ||
triedb := bc.stateCache.TrieDB() | ||
triedb := bc.triedb | ||
|
||
for _, offset := range []uint64{0, 1, TriesInMemory - 1} { | ||
if number := bc.CurrentBlock().NumberU64(); number > offset { | ||
|
@@ -932,8 +935,7 @@ func (bc *BlockChain) Stop() { | |
// Ensure all live cached entries be saved into disk, so that we can skip | ||
// cache warmup when node restarts. | ||
if bc.cacheConfig.TrieCleanJournal != "" { | ||
triedb := bc.stateCache.TrieDB() | ||
triedb.SaveCache(bc.cacheConfig.TrieCleanJournal) | ||
bc.triedb.SaveCache(bc.cacheConfig.TrieCleanJournal) | ||
} | ||
log.Info("Blockchain stopped") | ||
} | ||
|
@@ -1306,24 +1308,22 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types. | |
if err != nil { | ||
return err | ||
} | ||
triedb := bc.stateCache.TrieDB() | ||
|
||
// If we're running an archive node, always flush | ||
if bc.cacheConfig.TrieDirtyDisabled { | ||
return triedb.Commit(root, false, nil) | ||
return bc.triedb.Commit(root, false, nil) | ||
} else { | ||
// Full but not archive node, do proper garbage collection | ||
triedb.Reference(root, common.Hash{}) // metadata reference to keep trie alive | ||
bc.triedb.Reference(root, common.Hash{}) // metadata reference to keep trie alive | ||
bc.triegc.Push(root, -int64(block.NumberU64())) | ||
|
||
if current := block.NumberU64(); current > TriesInMemory { | ||
// If we exceeded our memory allowance, flush matured singleton nodes to disk | ||
var ( | ||
nodes, imgs = triedb.Size() | ||
nodes, imgs = bc.triedb.Size() | ||
limit = common.StorageSize(bc.cacheConfig.TrieDirtyLimit) * 1024 * 1024 | ||
) | ||
if nodes > limit || imgs > 4*1024*1024 { | ||
triedb.Cap(limit - ethdb.IdealBatchSize) | ||
bc.triedb.Cap(limit - ethdb.IdealBatchSize) | ||
} | ||
// Find the next state trie we need to commit | ||
chosen := current - TriesInMemory | ||
|
@@ -1342,7 +1342,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types. | |
log.Info("State in memory for too long, committing", "time", bc.gcproc, "allowance", bc.cacheConfig.TrieTimeLimit, "optimum", float64(chosen-lastWrite)/TriesInMemory) | ||
} | ||
// Flush an entire trie and restart the counters | ||
triedb.Commit(header.Root, true, nil) | ||
bc.triedb.Commit(header.Root, true, nil) | ||
lastWrite = chosen | ||
bc.gcproc = 0 | ||
} | ||
|
@@ -1354,7 +1354,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types. | |
bc.triegc.Push(root, number) | ||
break | ||
} | ||
triedb.Dereference(root.(common.Hash)) | ||
bc.triedb.Dereference(root.(common.Hash)) | ||
} | ||
} | ||
} | ||
|
@@ -1760,7 +1760,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals, setHead bool) | |
stats.processed++ | ||
stats.usedGas += usedGas | ||
|
||
dirty, _ := bc.stateCache.TrieDB().Size() | ||
dirty, _ := bc.triedb.Size() | ||
stats.report(chain, it.index, dirty, setHead) | ||
|
||
if !setHead { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we enable preimage-recording by default? Or just based on the flags passed by users.