diff --git a/consensus/beacon/consensus.go b/consensus/beacon/consensus.go index b09a5cb4c8..196cbc857c 100644 --- a/consensus/beacon/consensus.go +++ b/consensus/beacon/consensus.go @@ -353,9 +353,9 @@ func (beacon *Beacon) Finalize(chain consensus.ChainHeaderReader, header *types. // FinalizeAndAssemble implements consensus.Engine, setting the final state and // 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) { - 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) if shanghai { @@ -369,10 +369,10 @@ func (beacon *Beacon) FinalizeAndAssemble(chain consensus.ChainHeaderReader, hea } } // Finalize and assemble the block. - beacon.Finalize(chain, header, statedb, body) + beacon.Finalize(chain, header, state, body) // Assign the final state root to header. - header.Root = statedb.IntermediateRoot(true) + header.Root = state.IntermediateRoot(true) // Assemble the final block. 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. // This step needs to happen as late as possible to catch all access events. if chain.Config().IsVerkle(header.Number, header.Time) { - //statedb.Database().SaveTransitionState(header.Root, &state.TransitionState{Ended: true}) - keys := statedb.AccessEvents().Keys() + keys := state.AccessEvents().Keys() // Open the pre-tree to prove the pre-state against parent := chain.GetHeaderByNumber(header.Number.Uint64() - 1) if parent == nil { 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 { 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) - vktPostTrie, okpost := statedb.GetTrie().(*trie.VerkleTrie) + vktPostTrie, okpost := postTrie.(*trie.VerkleTrie) // The witness is only attached iff both parent and current block are // using verkle tree. diff --git a/core/overlay/state_transition.go b/core/overlay/state_transition.go index 8cc1b4baaf..90b5c9431a 100644 --- a/core/overlay/state_transition.go +++ b/core/overlay/state_transition.go @@ -61,12 +61,10 @@ func (ts *TransitionState) Copy() *TransitionState { CurrentPreimageOffset: ts.CurrentPreimageOffset, StorageProcessed: ts.StorageProcessed, } - if ts.CurrentAccountAddress != nil { - ret.CurrentAccountAddress = &common.Address{} - copy(ret.CurrentAccountAddress[:], ts.CurrentAccountAddress[:]) + addr := *ts.CurrentAccountAddress + ret.CurrentAccountAddress = &addr } - 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 // as a verkle database. log.Debug("no transition state found, starting fresh", "is verkle", db) + // Start with a fresh state ts = &TransitionState{Ended: isVerkle} } - return ts } diff --git a/core/rawdb/database.go b/core/rawdb/database.go index 9681c39c58..be0e8973cf 100644 --- a/core/rawdb/database.go +++ b/core/rawdb/database.go @@ -599,7 +599,7 @@ var knownMetadataKeys = [][]byte{ snapshotGeneratorKey, snapshotRecoveryKey, txIndexTailKey, fastTxLookupLimitKey, uncleanShutdownKey, badBlockKey, transitionStatusKey, skeletonSyncStatusKey, persistentStateIDKey, trieJournalKey, snapshotSyncStatusKey, snapSyncStatusFlagKey, - filterMapsRangeKey, headStateHistoryIndexKey, + filterMapsRangeKey, headStateHistoryIndexKey, VerkleTransitionStatePrefix, } // printChainMetadata prints out chain metadata to stderr. diff --git a/core/state/database.go b/core/state/database.go index a12209bea3..b46e5d500d 100644 --- a/core/state/database.go +++ b/core/state/database.go @@ -189,7 +189,6 @@ func (db *CachingDB) Reader(stateRoot common.Hash) (Reader, error) { 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. // This reader offers improved performance but is optional and only // 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 // 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 { return nil, err } diff --git a/core/state/reader.go b/core/state/reader.go index d9c846cdc0..f56a1bfae1 100644 --- a/core/state/reader.go +++ b/core/state/reader.go @@ -23,7 +23,6 @@ import ( "github.com/ethereum/go-ethereum/common" "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/types" "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 // 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 ( tr Trie err error ) - if !ts.Transitioned() && !ts.InTransition() { + if !db.IsVerkle() { tr, err = trie.NewStateTrie(trie.StateTrieID(root), db) } else { + // TODO @gballet determine the trie type (verkle or overlay) by transition state tr, err = trie.NewVerkleTrie(root, db, cache) } if err != nil {