From f0f51d1ef458c1593e908741180615ad83fd07eb Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Mon, 4 Aug 2025 15:01:47 +0200 Subject: [PATCH] move transition state to its own file Signed-off-by: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Co-authored-by: Gary Rong --- core/genesis_test.go | 1 + core/state/database.go | 57 +++++----------------------------- core/state/database_history.go | 5 +-- core/state/reader.go | 3 +- 4 files changed, 13 insertions(+), 53 deletions(-) diff --git a/core/genesis_test.go b/core/genesis_test.go index 702cd3445f..a41dfce578 100644 --- a/core/genesis_test.go +++ b/core/genesis_test.go @@ -315,6 +315,7 @@ func TestVerkleGenesisCommit(t *testing.T) { } db := rawdb.NewMemoryDatabase() + config := *pathdb.Defaults config.NoAsyncFlush = true diff --git a/core/state/database.go b/core/state/database.go index ea24b4f5cc..187dc79bce 100644 --- a/core/state/database.go +++ b/core/state/database.go @@ -24,6 +24,7 @@ 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/state/snapshot" "github.com/ethereum/go-ethereum/core/types" @@ -145,50 +146,6 @@ type Trie interface { IsVerkle() bool } -// TransitionState is a structure that holds the progress markers of the -// translation process. -type TransitionState struct { - CurrentAccountAddress *common.Address // addresss of the last translated account - CurrentSlotHash common.Hash // hash of the last translated storage slot - CurrentPreimageOffset int64 // next byte to read from the preimage file - Started, Ended bool - - // Mark whether the storage for an account has been processed. This is useful if the - // maximum number of leaves of the conversion is reached before the whole storage is - // processed. - StorageProcessed bool - - BaseRoot common.Hash // hash of the last read-only MPT base tree -} - -// InTransition returns true if the translation process is in progress. -func (ts *TransitionState) InTransition() bool { - return ts != nil && ts.Started && !ts.Ended -} - -// Transitioned returns true if the translation process has been completed. -func (ts *TransitionState) Transitioned() bool { - return ts != nil && ts.Ended -} - -// Copy returns a deep copy of the TransitionState object. -func (ts *TransitionState) Copy() *TransitionState { - ret := &TransitionState{ - Started: ts.Started, - Ended: ts.Ended, - CurrentSlotHash: ts.CurrentSlotHash, - CurrentPreimageOffset: ts.CurrentPreimageOffset, - StorageProcessed: ts.StorageProcessed, - } - - if ts.CurrentAccountAddress != nil { - ret.CurrentAccountAddress = &common.Address{} - copy(ret.CurrentAccountAddress[:], ts.CurrentAccountAddress[:]) - } - - return ret -} - // CachingDB is an implementation of Database interface. It leverages both trie and // state snapshot to provide functionalities for state access. It's meant to be a // long-live object and has a few caches inside for sharing between blocks. @@ -201,7 +158,7 @@ type CachingDB struct { pointCache *utils.PointCache // Transition-specific fields - TransitionStatePerRoot *lru.Cache[common.Hash, *TransitionState] + TransitionStatePerRoot *lru.Cache[common.Hash, *overlay.TransitionState] } // NewDatabase creates a state database with the provided data sources. @@ -213,7 +170,7 @@ func NewDatabase(triedb *triedb.Database, snap *snapshot.Tree) *CachingDB { codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize), codeSizeCache: lru.NewCache[common.Hash, int](codeSizeCacheSize), pointCache: utils.NewPointCache(pointCacheSize), - TransitionStatePerRoot: lru.NewCache[common.Hash, *TransitionState](1000), + TransitionStatePerRoot: lru.NewCache[common.Hash, *overlay.TransitionState](1000), } } @@ -349,7 +306,7 @@ func mustCopyTrie(t Trie) Trie { // SaveTransitionState saves the transition state to the cache and commits // it to the database if it's not already in the cache. -func (db *CachingDB) SaveTransitionState(root common.Hash, ts *TransitionState) { +func (db *CachingDB) SaveTransitionState(root common.Hash, ts *overlay.TransitionState) { if ts == nil { panic("nil transition state") } @@ -381,7 +338,7 @@ func (db *CachingDB) SaveTransitionState(root common.Hash, ts *TransitionState) log.Debug("saving transition state", "storage processed", ts.StorageProcessed, "addr", ts.CurrentAccountAddress, "slot hash", ts.CurrentSlotHash, "root", root, "ended", ts.Ended, "started", ts.Started) } -func (db *CachingDB) LoadTransitionState(root common.Hash) *TransitionState { +func (db *CachingDB) LoadTransitionState(root common.Hash) *overlay.TransitionState { // Try to get the transition state from the cache and // the DB if it's not there. ts, ok := db.TransitionStatePerRoot.Get(root) @@ -396,7 +353,7 @@ func (db *CachingDB) LoadTransitionState(root common.Hash) *TransitionState { // if a state could be read from the db, attempt to decode it if len(data) > 0 { var ( - newts TransitionState + newts overlay.TransitionState buf = bytes.NewBuffer(data[:]) dec = gob.NewDecoder(buf) ) @@ -416,7 +373,7 @@ func (db *CachingDB) LoadTransitionState(root common.Hash) *TransitionState { // as a verkle database. log.Debug("no transition state found, starting fresh", "is verkle", db.triedb.IsVerkle()) // Start with a fresh state - ts = &TransitionState{Ended: db.triedb.IsVerkle()} + ts = &overlay.TransitionState{Ended: db.triedb.IsVerkle()} } db.TransitionStatePerRoot.Add(root, ts) diff --git a/core/state/database_history.go b/core/state/database_history.go index 705c1dd0ac..88b893695e 100644 --- a/core/state/database_history.go +++ b/core/state/database_history.go @@ -21,6 +21,7 @@ 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/state/snapshot" "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethdb" @@ -154,10 +155,10 @@ func (db *HistoricDB) Snapshot() *snapshot.Tree { return nil } -func (db *HistoricDB) LoadTransitionState(common.Hash) *TransitionState { +func (db *HistoricDB) LoadTransitionState(common.Hash) *overlay.TransitionState { panic("should not be called") } -func (db *HistoricDB) SaveTransitionState(common.Hash, *TransitionState) { +func (db *HistoricDB) SaveTransitionState(common.Hash, *overlay.TransitionState) { panic("should not be called") } diff --git a/core/state/reader.go b/core/state/reader.go index 7f08ef1a56..d9c846cdc0 100644 --- a/core/state/reader.go +++ b/core/state/reader.go @@ -23,6 +23,7 @@ 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" @@ -233,7 +234,7 @@ 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 *TransitionState) (*trieReader, error) { +func newTrieReader(root common.Hash, db *triedb.Database, cache *utils.PointCache, ts *overlay.TransitionState) (*trieReader, error) { var ( tr Trie err error