@@ -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 {}) {
@@ -446,7 +473,7 @@ func (g *Genesis) chainConfigOrDefault(ghash common.Hash, stored *params.ChainCo
446473// IsVerkle indicates whether the state is already stored in a verkle
447474// tree at genesis time.
448475func (g * Genesis ) IsVerkle () bool {
449- return g .Config .IsVerkleGenesis ()
476+ return g .Config .VerkleTime != nil && * g . Config . VerkleTime == g . Timestamp
450477}
451478
452479// ToBlock returns the genesis block according to genesis specification.
@@ -550,6 +577,9 @@ func (g *Genesis) Commit(db ethdb.Database, triedb *triedb.Database) (*types.Blo
550577 if err != nil {
551578 return nil , err
552579 }
580+ if g .IsVerkle () {
581+ saveVerkleTransitionStatus (db , block .Root (), & state.TransitionState {Ended : true })
582+ }
553583 batch := db .NewBatch ()
554584 rawdb .WriteGenesisStateSpec (batch , block .Hash (), blob )
555585 rawdb .WriteBlock (batch , block )
@@ -572,29 +602,6 @@ func (g *Genesis) MustCommit(db ethdb.Database, triedb *triedb.Database) *types.
572602 return block
573603}
574604
575- // EnableVerkleAtGenesis indicates whether the verkle fork should be activated
576- // at genesis. This is a temporary solution only for verkle devnet testing, where
577- // verkle fork is activated at genesis, and the configured activation date has
578- // already passed.
579- //
580- // In production networks (mainnet and public testnets), verkle activation always
581- // occurs after the genesis block, making this function irrelevant in those cases.
582- func EnableVerkleAtGenesis (db ethdb.Database , genesis * Genesis ) (bool , error ) {
583- if genesis != nil {
584- if genesis .Config == nil {
585- return false , errGenesisNoConfig
586- }
587- return genesis .Config .EnableVerkleAtGenesis , nil
588- }
589- if ghash := rawdb .ReadCanonicalHash (db , 0 ); ghash != (common.Hash {}) {
590- chainCfg := rawdb .ReadChainConfig (db , ghash )
591- if chainCfg != nil {
592- return chainCfg .EnableVerkleAtGenesis , nil
593- }
594- }
595- return false , nil
596- }
597-
598605// DefaultGenesisBlock returns the Ethereum main net genesis block.
599606func DefaultGenesisBlock () * Genesis {
600607 return & Genesis {
0 commit comments