@@ -18,6 +18,7 @@ package core
1818
1919import (
2020 "bytes"
21+ "encoding/gob"
2122 "encoding/json"
2223 "errors"
2324 "fmt"
@@ -145,6 +146,9 @@ func hashAlloc(ga *types.GenesisAlloc, isVerkle bool) (common.Hash, error) {
145146 emptyRoot = types .EmptyVerkleHash
146147 }
147148 db := rawdb .NewMemoryDatabase ()
149+ if isVerkle {
150+ saveVerkleTransitionStatusAtVerlkeGenesis (db )
151+ }
148152 statedb , err := state .New (emptyRoot , state .NewDatabase (triedb .NewDatabase (db , config ), nil ))
149153 if err != nil {
150154 return common.Hash {}, err
@@ -276,6 +280,24 @@ func (o *ChainOverrides) apply(cfg *params.ChainConfig) error {
276280 return cfg .CheckConfigForkOrder ()
277281}
278282
283+ // saveVerkleTransitionStatusAtVerlkeGenesis saves a conversion marker
284+ // representing a converted state, which is used in devnets that activate
285+ // verkle at genesis.
286+ func saveVerkleTransitionStatusAtVerlkeGenesis (db ethdb.Database ) {
287+ saveVerkleTransitionStatus (db , common.Hash {}, & state.TransitionState {Ended : true })
288+ }
289+
290+ func saveVerkleTransitionStatus (db ethdb.Database , root common.Hash , ts * state.TransitionState ) {
291+ var buf bytes.Buffer
292+ enc := gob .NewEncoder (& buf )
293+ err := enc .Encode (ts )
294+ if err != nil {
295+ log .Error ("failed to encode transition state" , "err" , err )
296+ return
297+ }
298+ rawdb .WriteVerkleTransitionState (db , root , buf .Bytes ())
299+ }
300+
279301// SetupGenesisBlock writes or updates the genesis block in db.
280302// The block that will be used is:
281303//
@@ -299,6 +321,11 @@ func SetupGenesisBlockWithOverride(db ethdb.Database, triedb *triedb.Database, g
299321 if genesis != nil && genesis .Config == nil {
300322 return nil , common.Hash {}, nil , errGenesisNoConfig
301323 }
324+ // In case of verkle-at-genesis, we need to ensure that the conversion
325+ // markers are indicating that the conversion has completed.
326+ if genesis != nil && genesis .Config .VerkleTime != nil && * genesis .Config .VerkleTime == genesis .Timestamp {
327+ saveVerkleTransitionStatusAtVerlkeGenesis (db )
328+ }
302329 // Commit the genesis if the database is empty
303330 ghash := rawdb .ReadCanonicalHash (db , 0 )
304331 if (ghash == common.Hash {}) {
@@ -443,7 +470,7 @@ func (g *Genesis) chainConfigOrDefault(ghash common.Hash, stored *params.ChainCo
443470// IsVerkle indicates whether the state is already stored in a verkle
444471// tree at genesis time.
445472func (g * Genesis ) IsVerkle () bool {
446- return g .Config .IsVerkleGenesis ()
473+ return g .Config .VerkleTime != nil && * g . Config . VerkleTime == g . Timestamp
447474}
448475
449476// ToBlock returns the genesis block according to genesis specification.
@@ -547,6 +574,9 @@ func (g *Genesis) Commit(db ethdb.Database, triedb *triedb.Database) (*types.Blo
547574 if err != nil {
548575 return nil , err
549576 }
577+ if g .IsVerkle () {
578+ saveVerkleTransitionStatus (db , block .Root (), & state.TransitionState {Ended : true })
579+ }
550580 batch := db .NewBatch ()
551581 rawdb .WriteGenesisStateSpec (batch , block .Hash (), blob )
552582 rawdb .WriteBlock (batch , block )
@@ -569,29 +599,6 @@ func (g *Genesis) MustCommit(db ethdb.Database, triedb *triedb.Database) *types.
569599 return block
570600}
571601
572- // EnableVerkleAtGenesis indicates whether the verkle fork should be activated
573- // at genesis. This is a temporary solution only for verkle devnet testing, where
574- // verkle fork is activated at genesis, and the configured activation date has
575- // already passed.
576- //
577- // In production networks (mainnet and public testnets), verkle activation always
578- // occurs after the genesis block, making this function irrelevant in those cases.
579- func EnableVerkleAtGenesis (db ethdb.Database , genesis * Genesis ) (bool , error ) {
580- if genesis != nil {
581- if genesis .Config == nil {
582- return false , errGenesisNoConfig
583- }
584- return genesis .Config .EnableVerkleAtGenesis , nil
585- }
586- if ghash := rawdb .ReadCanonicalHash (db , 0 ); ghash != (common.Hash {}) {
587- chainCfg := rawdb .ReadChainConfig (db , ghash )
588- if chainCfg != nil {
589- return chainCfg .EnableVerkleAtGenesis , nil
590- }
591- }
592- return false , nil
593- }
594-
595602// DefaultGenesisBlock returns the Ethereum main net genesis block.
596603func DefaultGenesisBlock () * Genesis {
597604 return & Genesis {
0 commit comments