diff --git a/core/genesis.go b/core/genesis.go index 2306598db5..4dbc3ad963 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -121,13 +121,13 @@ func (ga *GenesisAlloc) UnmarshalJSON(data []byte) error { } // deriveHash computes the state root according to the genesis specification. -func (ga *GenesisAlloc) deriveHash(cfg *params.ChainConfig) (common.Hash, error) { +func (ga *GenesisAlloc) deriveHash(cfg *params.ChainConfig, timestamp uint64) (common.Hash, error) { // Create an ephemeral in-memory database for computing hash, // all the derived states will be discarded to not pollute disk. db := state.NewDatabase(rawdb.NewMemoryDatabase()) // XXX check this is the case // TODO remove the nil config check once we have rebased, it should never be nil - if cfg != nil && cfg.IsCancun(big.NewInt(int64(0)), 0 /* XXX */) { + if cfg != nil && cfg.IsCancun(big.NewInt(int64(0)), timestamp) { db.EndVerkleTransition() } statedb, err := state.New(types.EmptyRootHash, db, nil) @@ -155,7 +155,7 @@ func (ga *GenesisAlloc) flush(db ethdb.Database, triedb *trie.Database, blockhas } // End the verkle conversion at genesis if the fork block is 0 - if cfg != nil && cfg.IsCancun(big.NewInt(int64(0)), 0 /* XXX */) { + if triedb.IsVerkle() { statedb.Database().EndVerkleTransition() } @@ -456,7 +456,7 @@ func (g *Genesis) configOrDefault(ghash common.Hash) *params.ChainConfig { // ToBlock returns the genesis block according to genesis specification. func (g *Genesis) ToBlock() *types.Block { - root, err := g.Alloc.deriveHash(g.Config) + root, err := g.Alloc.deriveHash(g.Config, g.Timestamp) if err != nil { panic(err) } @@ -547,7 +547,8 @@ func (g *Genesis) Commit(db ethdb.Database, triedb *trie.Database) (*types.Block // Note the state changes will be committed in hash-based scheme, use Commit // if path-scheme is preferred. func (g *Genesis) MustCommit(db ethdb.Database) *types.Block { - block, err := g.Commit(db, trie.NewDatabase(db)) + triedb := trie.NewDatabaseWithConfig(db, &trie.Config{Verkle: g.Config != nil && g.Config.IsCancun(big.NewInt(int64(g.Number)), g.Timestamp)}) + block, err := g.Commit(db, triedb) if err != nil { panic(err) } diff --git a/core/genesis_test.go b/core/genesis_test.go index c6df6f59a3..77a214ebe7 100644 --- a/core/genesis_test.go +++ b/core/genesis_test.go @@ -219,7 +219,7 @@ func TestReadWriteGenesisAlloc(t *testing.T) { {1}: {Balance: big.NewInt(1), Storage: map[common.Hash]common.Hash{{1}: {1}}}, {2}: {Balance: big.NewInt(2), Storage: map[common.Hash]common.Hash{{2}: {2}}}, } - hash, _ = alloc.deriveHash(¶ms.ChainConfig{}) + hash, _ = alloc.deriveHash(¶ms.ChainConfig{}, 0) ) blob, _ := json.Marshal(alloc) rawdb.WriteGenesisStateSpec(db, hash, blob) diff --git a/core/state/database.go b/core/state/database.go index 7d7b3b1457..df3365dc25 100644 --- a/core/state/database.go +++ b/core/state/database.go @@ -195,6 +195,7 @@ func NewDatabaseWithNodeDB(db ethdb.Database, triedb *trie.Database) Database { codeCache: lru.NewSizeConstrainedCache[common.Hash, []byte](codeCacheSize), triedb: triedb, addrToPoint: utils.NewPointCache(), + ended: triedb.IsVerkle(), } } @@ -313,8 +314,16 @@ func (db *cachingDB) OpenTrie(root common.Hash) (Trie, error) { err error ) - if db.started { - vkt, err := db.openVKTrie(db.getTranslation(root)) + // TODO separate both cases when I can be certain that it won't + // find a Verkle trie where is expects a Transitoion trie. + if db.started || db.ended { + var r common.Hash + if db.ended { + r = root + } else { + r = db.getTranslation(root) + } + vkt, err := db.openVKTrie(r) if err != nil { return nil, err } diff --git a/core/state/statedb.go b/core/state/statedb.go index f65dbb21c6..a3d320e6f8 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -175,19 +175,19 @@ func New(root common.Hash, db Database, snaps *snapshot.Tree) (*StateDB, error) } if tr.IsVerkle() { sdb.witness = NewAccessWitness(sdb) - if sdb.snaps == nil { - snapconfig := snapshot.Config{ - CacheSize: 256, - Recovery: false, - NoBuild: false, - AsyncBuild: false, - Verkle: true, - } - sdb.snaps, err = snapshot.New(snapconfig, db.DiskDB(), db.TrieDB(), root) - if err != nil { - return nil, err - } - } + // if sdb.snaps == nil { + // snapconfig := snapshot.Config{ + // CacheSize: 256, + // Recovery: false, + // NoBuild: false, + // AsyncBuild: false, + // Verkle: true, + // } + // sdb.snaps, err = snapshot.New(snapconfig, db.DiskDB(), db.TrieDB(), root) + // if err != nil { + // return nil, err + // } + // } } if sdb.snaps != nil { if sdb.snap = sdb.snaps.Snapshot(root); sdb.snap == nil { diff --git a/trie/database.go b/trie/database.go index 5989e11481..a6f7d98913 100644 --- a/trie/database.go +++ b/trie/database.go @@ -33,6 +33,7 @@ type Config struct { Cache int // Memory allowance (MB) to use for caching trie nodes in memory Preimages bool // Flag whether the preimage of trie key is recorded PathDB *pathdb.Config // Configs for experimental path-based scheme, not used yet. + Verkle bool // Testing hooks OnCommit func(states *triestate.Set) // Hook invoked when commit is performed @@ -279,3 +280,7 @@ func (db *Database) ClearStorageRootConversion(addr common.Address) { defer db.addrToRootLock.Unlock() delete(db.addrToRoot, addr) } + +func (db *Database) IsVerkle() bool { + return db.config != nil && db.config.Verkle +}