move transition state to its own file

Signed-off-by: Guillaume Ballet <3272758+gballet@users.noreply.github.com>
Co-authored-by: Gary Rong <garyrong0905@gmail.com>
This commit is contained in:
Guillaume Ballet 2025-08-04 15:01:47 +02:00
parent a483135b2b
commit f0f51d1ef4
4 changed files with 13 additions and 53 deletions

View file

@ -315,6 +315,7 @@ func TestVerkleGenesisCommit(t *testing.T) {
} }
db := rawdb.NewMemoryDatabase() db := rawdb.NewMemoryDatabase()
config := *pathdb.Defaults config := *pathdb.Defaults
config.NoAsyncFlush = true config.NoAsyncFlush = true

View file

@ -24,6 +24,7 @@ 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/state/snapshot" "github.com/ethereum/go-ethereum/core/state/snapshot"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
@ -145,50 +146,6 @@ type Trie interface {
IsVerkle() bool 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 // 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 // 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. // long-live object and has a few caches inside for sharing between blocks.
@ -201,7 +158,7 @@ type CachingDB struct {
pointCache *utils.PointCache pointCache *utils.PointCache
// Transition-specific fields // 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. // 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), codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize),
codeSizeCache: lru.NewCache[common.Hash, int](codeSizeCacheSize), codeSizeCache: lru.NewCache[common.Hash, int](codeSizeCacheSize),
pointCache: utils.NewPointCache(pointCacheSize), 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 // SaveTransitionState saves the transition state to the cache and commits
// it to the database if it's not already in the cache. // 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 { if ts == nil {
panic("nil transition state") 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) 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 // Try to get the transition state from the cache and
// the DB if it's not there. // the DB if it's not there.
ts, ok := db.TransitionStatePerRoot.Get(root) 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 a state could be read from the db, attempt to decode it
if len(data) > 0 { if len(data) > 0 {
var ( var (
newts TransitionState newts overlay.TransitionState
buf = bytes.NewBuffer(data[:]) buf = bytes.NewBuffer(data[:])
dec = gob.NewDecoder(buf) dec = gob.NewDecoder(buf)
) )
@ -416,7 +373,7 @@ func (db *CachingDB) LoadTransitionState(root common.Hash) *TransitionState {
// as a verkle database. // as a verkle database.
log.Debug("no transition state found, starting fresh", "is verkle", db.triedb.IsVerkle()) log.Debug("no transition state found, starting fresh", "is verkle", db.triedb.IsVerkle())
// Start with a fresh state // Start with a fresh state
ts = &TransitionState{Ended: db.triedb.IsVerkle()} ts = &overlay.TransitionState{Ended: db.triedb.IsVerkle()}
} }
db.TransitionStatePerRoot.Add(root, ts) db.TransitionStatePerRoot.Add(root, ts)

View file

@ -21,6 +21,7 @@ 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/state/snapshot" "github.com/ethereum/go-ethereum/core/state/snapshot"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
@ -154,10 +155,10 @@ func (db *HistoricDB) Snapshot() *snapshot.Tree {
return nil return nil
} }
func (db *HistoricDB) LoadTransitionState(common.Hash) *TransitionState { func (db *HistoricDB) LoadTransitionState(common.Hash) *overlay.TransitionState {
panic("should not be called") 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") panic("should not be called")
} }

View file

@ -23,6 +23,7 @@ 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"
@ -233,7 +234,7 @@ 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 *TransitionState) (*trieReader, error) { func newTrieReader(root common.Hash, db *triedb.Database, cache *utils.PointCache, ts *overlay.TransitionState) (*trieReader, error) {
var ( var (
tr Trie tr Trie
err error err error