core, consensus: avoid transition state read in reader construction

This commit is contained in:
Gary Rong 2025-08-05 09:28:48 +08:00
parent 24f848dac8
commit 320ea13898
5 changed files with 19 additions and 19 deletions

View file

@ -353,9 +353,9 @@ func (beacon *Beacon) Finalize(chain consensus.ChainHeaderReader, header *types.
// FinalizeAndAssemble implements consensus.Engine, setting the final state and // FinalizeAndAssemble implements consensus.Engine, setting the final state and
// assembling the block. // assembling the block.
func (beacon *Beacon) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, statedb *state.StateDB, body *types.Body, receipts []*types.Receipt) (*types.Block, error) { func (beacon *Beacon) FinalizeAndAssemble(chain consensus.ChainHeaderReader, header *types.Header, state *state.StateDB, body *types.Body, receipts []*types.Receipt) (*types.Block, error) {
if !beacon.IsPoSHeader(header) { if !beacon.IsPoSHeader(header) {
return beacon.ethone.FinalizeAndAssemble(chain, header, statedb, body, receipts) return beacon.ethone.FinalizeAndAssemble(chain, header, state, body, receipts)
} }
shanghai := chain.Config().IsShanghai(header.Number, header.Time) shanghai := chain.Config().IsShanghai(header.Number, header.Time)
if shanghai { if shanghai {
@ -369,10 +369,10 @@ func (beacon *Beacon) FinalizeAndAssemble(chain consensus.ChainHeaderReader, hea
} }
} }
// Finalize and assemble the block. // Finalize and assemble the block.
beacon.Finalize(chain, header, statedb, body) beacon.Finalize(chain, header, state, body)
// Assign the final state root to header. // Assign the final state root to header.
header.Root = statedb.IntermediateRoot(true) header.Root = state.IntermediateRoot(true)
// Assemble the final block. // Assemble the final block.
block := types.NewBlock(header, body, receipts, trie.NewStackTrie(nil)) block := types.NewBlock(header, body, receipts, trie.NewStackTrie(nil))
@ -380,20 +380,23 @@ func (beacon *Beacon) FinalizeAndAssemble(chain consensus.ChainHeaderReader, hea
// Create the block witness and attach to block. // Create the block witness and attach to block.
// This step needs to happen as late as possible to catch all access events. // This step needs to happen as late as possible to catch all access events.
if chain.Config().IsVerkle(header.Number, header.Time) { if chain.Config().IsVerkle(header.Number, header.Time) {
//statedb.Database().SaveTransitionState(header.Root, &state.TransitionState{Ended: true}) keys := state.AccessEvents().Keys()
keys := statedb.AccessEvents().Keys()
// Open the pre-tree to prove the pre-state against // Open the pre-tree to prove the pre-state against
parent := chain.GetHeaderByNumber(header.Number.Uint64() - 1) parent := chain.GetHeaderByNumber(header.Number.Uint64() - 1)
if parent == nil { if parent == nil {
return nil, fmt.Errorf("nil parent header for block %d", header.Number) return nil, fmt.Errorf("nil parent header for block %d", header.Number)
} }
preTrie, err := statedb.Database().OpenTrie(parent.Root) preTrie, err := state.Database().OpenTrie(parent.Root)
if err != nil { if err != nil {
return nil, fmt.Errorf("error opening pre-state tree root: %w", err) return nil, fmt.Errorf("error opening pre-state tree root: %w", err)
} }
postTrie := state.GetTrie()
if postTrie == nil {
return nil, errors.New("post-state tree is not available")
}
vktPreTrie, okpre := preTrie.(*trie.VerkleTrie) vktPreTrie, okpre := preTrie.(*trie.VerkleTrie)
vktPostTrie, okpost := statedb.GetTrie().(*trie.VerkleTrie) vktPostTrie, okpost := postTrie.(*trie.VerkleTrie)
// The witness is only attached iff both parent and current block are // The witness is only attached iff both parent and current block are
// using verkle tree. // using verkle tree.

View file

@ -61,12 +61,10 @@ func (ts *TransitionState) Copy() *TransitionState {
CurrentPreimageOffset: ts.CurrentPreimageOffset, CurrentPreimageOffset: ts.CurrentPreimageOffset,
StorageProcessed: ts.StorageProcessed, StorageProcessed: ts.StorageProcessed,
} }
if ts.CurrentAccountAddress != nil { if ts.CurrentAccountAddress != nil {
ret.CurrentAccountAddress = &common.Address{} addr := *ts.CurrentAccountAddress
copy(ret.CurrentAccountAddress[:], ts.CurrentAccountAddress[:]) ret.CurrentAccountAddress = &addr
} }
return ret return ret
} }
@ -99,9 +97,9 @@ func LoadTransitionState(db ethdb.KeyValueReader, root common.Hash, isVerkle boo
// field set to true if the database was created // field set to true if the database was created
// as a verkle database. // as a verkle database.
log.Debug("no transition state found, starting fresh", "is verkle", db) log.Debug("no transition state found, starting fresh", "is verkle", db)
// Start with a fresh state // Start with a fresh state
ts = &TransitionState{Ended: isVerkle} ts = &TransitionState{Ended: isVerkle}
} }
return ts return ts
} }

View file

@ -599,7 +599,7 @@ var knownMetadataKeys = [][]byte{
snapshotGeneratorKey, snapshotRecoveryKey, txIndexTailKey, fastTxLookupLimitKey, snapshotGeneratorKey, snapshotRecoveryKey, txIndexTailKey, fastTxLookupLimitKey,
uncleanShutdownKey, badBlockKey, transitionStatusKey, skeletonSyncStatusKey, uncleanShutdownKey, badBlockKey, transitionStatusKey, skeletonSyncStatusKey,
persistentStateIDKey, trieJournalKey, snapshotSyncStatusKey, snapSyncStatusFlagKey, persistentStateIDKey, trieJournalKey, snapshotSyncStatusKey, snapSyncStatusFlagKey,
filterMapsRangeKey, headStateHistoryIndexKey, filterMapsRangeKey, headStateHistoryIndexKey, VerkleTransitionStatePrefix,
} }
// printChainMetadata prints out chain metadata to stderr. // printChainMetadata prints out chain metadata to stderr.

View file

@ -189,7 +189,6 @@ func (db *CachingDB) Reader(stateRoot common.Hash) (Reader, error) {
readers = append(readers, newFlatReader(snap)) readers = append(readers, newFlatReader(snap))
} }
} }
ts := overlay.LoadTransitionState(db.TrieDB().Disk(), stateRoot, db.triedb.IsVerkle())
// Configure the state reader using the path database in path mode. // Configure the state reader using the path database in path mode.
// This reader offers improved performance but is optional and only // This reader offers improved performance but is optional and only
// partially useful if the snapshot data in path database is not // partially useful if the snapshot data in path database is not
@ -202,7 +201,7 @@ func (db *CachingDB) Reader(stateRoot common.Hash) (Reader, error) {
} }
// Configure the trie reader, which is expected to be available as the // Configure the trie reader, which is expected to be available as the
// gatekeeper unless the state is corrupted. // gatekeeper unless the state is corrupted.
tr, err := newTrieReader(stateRoot, db.triedb, db.pointCache, ts) tr, err := newTrieReader(stateRoot, db.triedb, db.pointCache)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View file

@ -23,7 +23,6 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/lru" "github.com/ethereum/go-ethereum/common/lru"
"github.com/ethereum/go-ethereum/core/overlay"
"github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
@ -234,14 +233,15 @@ type trieReader struct {
// trieReader constructs a trie reader of the specific state. An error will be // trieReader constructs a trie reader of the specific state. An error will be
// returned if the associated trie specified by root is not existent. // returned if the associated trie specified by root is not existent.
func newTrieReader(root common.Hash, db *triedb.Database, cache *utils.PointCache, ts *overlay.TransitionState) (*trieReader, error) { func newTrieReader(root common.Hash, db *triedb.Database, cache *utils.PointCache) (*trieReader, error) {
var ( var (
tr Trie tr Trie
err error err error
) )
if !ts.Transitioned() && !ts.InTransition() { if !db.IsVerkle() {
tr, err = trie.NewStateTrie(trie.StateTrieID(root), db) tr, err = trie.NewStateTrie(trie.StateTrieID(root), db)
} else { } else {
// TODO @gballet determine the trie type (verkle or overlay) by transition state
tr, err = trie.NewVerkleTrie(root, db, cache) tr, err = trie.NewVerkleTrie(root, db, cache)
} }
if err != nil { if err != nil {