@@ -18,6 +18,7 @@ package core
18
18
19
19
import (
20
20
"bytes"
21
+ "encoding/gob"
21
22
"encoding/json"
22
23
"errors"
23
24
"fmt"
@@ -145,6 +146,9 @@ func hashAlloc(ga *types.GenesisAlloc, isVerkle bool) (common.Hash, error) {
145
146
emptyRoot = types .EmptyVerkleHash
146
147
}
147
148
db := rawdb .NewMemoryDatabase ()
149
+ if isVerkle {
150
+ saveVerkleTransitionStatusAtVerlkeGenesis (db )
151
+ }
148
152
statedb , err := state .New (emptyRoot , state .NewDatabase (triedb .NewDatabase (db , config ), nil ))
149
153
if err != nil {
150
154
return common.Hash {}, err
@@ -276,6 +280,24 @@ func (o *ChainOverrides) apply(cfg *params.ChainConfig) error {
276
280
return cfg .CheckConfigForkOrder ()
277
281
}
278
282
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
+
279
301
// SetupGenesisBlock writes or updates the genesis block in db.
280
302
// The block that will be used is:
281
303
//
@@ -299,6 +321,11 @@ func SetupGenesisBlockWithOverride(db ethdb.Database, triedb *triedb.Database, g
299
321
if genesis != nil && genesis .Config == nil {
300
322
return nil , common.Hash {}, nil , errGenesisNoConfig
301
323
}
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
+ }
302
329
// Commit the genesis if the database is empty
303
330
ghash := rawdb .ReadCanonicalHash (db , 0 )
304
331
if (ghash == common.Hash {}) {
@@ -443,7 +470,7 @@ func (g *Genesis) chainConfigOrDefault(ghash common.Hash, stored *params.ChainCo
443
470
// IsVerkle indicates whether the state is already stored in a verkle
444
471
// tree at genesis time.
445
472
func (g * Genesis ) IsVerkle () bool {
446
- return g .Config .IsVerkleGenesis ()
473
+ return g .Config .VerkleTime != nil && * g . Config . VerkleTime == g . Timestamp
447
474
}
448
475
449
476
// 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
547
574
if err != nil {
548
575
return nil , err
549
576
}
577
+ if g .IsVerkle () {
578
+ saveVerkleTransitionStatus (db , block .Root (), & state.TransitionState {Ended : true })
579
+ }
550
580
batch := db .NewBatch ()
551
581
rawdb .WriteGenesisStateSpec (batch , block .Hash (), blob )
552
582
rawdb .WriteBlock (batch , block )
@@ -569,29 +599,6 @@ func (g *Genesis) MustCommit(db ethdb.Database, triedb *triedb.Database) *types.
569
599
return block
570
600
}
571
601
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
-
595
602
// DefaultGenesisBlock returns the Ethereum main net genesis block.
596
603
func DefaultGenesisBlock () * Genesis {
597
604
return & Genesis {
0 commit comments