Skip to content

Commit 4f3022b

Browse files
committed
Refactor transition post genesis (#311)
* rewrite per-block conversion pointer management * remove unused method * fix: a branch that can verge at genesis or post genesis (#314) * fix: import cycle in conversion refactor (#315) * fix shadowfork panic in OpenStorageTrie
1 parent 1f42024 commit 4f3022b

File tree

9 files changed

+389
-315
lines changed

9 files changed

+389
-315
lines changed

consensus/beacon/consensus.go

+7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/ethereum/go-ethereum/consensus"
2626
"github.com/ethereum/go-ethereum/consensus/misc/eip1559"
2727
"github.com/ethereum/go-ethereum/consensus/misc/eip4844"
28+
"github.com/ethereum/go-ethereum/core/overlay"
2829
"github.com/ethereum/go-ethereum/core/state"
2930
"github.com/ethereum/go-ethereum/core/types"
3031
"github.com/ethereum/go-ethereum/params"
@@ -363,6 +364,12 @@ func (beacon *Beacon) Finalize(chain consensus.ChainHeaderReader, header *types.
363364
state.Witness().TouchAddressOnWriteAndComputeGas(w.Address[:], uint256.Int{}, utils.CodeKeccakLeafKey)
364365
state.Witness().TouchAddressOnWriteAndComputeGas(w.Address[:], uint256.Int{}, utils.CodeSizeLeafKey)
365366
}
367+
368+
if chain.Config().IsPrague(header.Number, header.Time) {
369+
fmt.Println("at block", header.Number, "performing transition?", state.Database().InTransition())
370+
parent := chain.GetHeaderByHash(header.ParentHash)
371+
overlay.OverlayVerkleTransition(state, parent.Root)
372+
}
366373
}
367374

368375
// FinalizeAndAssemble implements consensus.Engine, setting the final state and

core/blockchain.go

+6
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,12 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
313313

314314
// Declare the end of the verkle transition if need be
315315
if bc.chainConfig.Rules(head.Number, false /* XXX */, head.Time).IsPrague {
316+
// TODO this only works when resuming a chain that has already gone
317+
// through the conversion. All pointers should be saved to the DB
318+
// for it to be able to recover if interrupted during the transition
319+
// but that's left out to a later PR since there's not really a need
320+
// right now.
321+
bc.stateCache.InitTransitionStatus(true, true)
316322
bc.stateCache.EndVerkleTransition()
317323
}
318324

core/chain_makers.go

+7
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,15 @@ func GenerateVerkleChain(config *params.ChainConfig, parent *types.Block, engine
435435
}
436436
var snaps *snapshot.Tree
437437
triedb := state.NewDatabaseWithConfig(db, nil)
438+
triedb.StartVerkleTransition(common.Hash{}, common.Hash{}, config, config.PragueTime, common.Hash{})
438439
triedb.EndVerkleTransition()
440+
//statedb, err := state.New(parent.Root(), triedb, snaps)
441+
//if err != nil {
442+
// panic(fmt.Sprintf("could not find state for block %d: err=%v, parent root=%x", parent.NumberU64(), err, parent.Root()))
443+
//}
444+
statedb.Database().SaveTransitionState(parent.Root())
439445
for i := 0; i < n; i++ {
446+
// XXX merge uncommment
440447
statedb, err := state.New(parent.Root(), triedb, snaps)
441448
if err != nil {
442449
panic(fmt.Sprintf("could not find state for block %d: err=%v, parent root=%x", i, err, parent.Root()))

core/genesis.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ func (ga *GenesisAlloc) deriveHash(cfg *params.ChainConfig, timestamp uint64) (c
126126
// all the derived states will be discarded to not pollute disk.
127127
db := state.NewDatabase(rawdb.NewMemoryDatabase())
128128
if cfg.IsPrague(big.NewInt(int64(0)), timestamp) {
129+
db.StartVerkleTransition(common.Hash{}, common.Hash{}, cfg, &timestamp, common.Hash{})
129130
db.EndVerkleTransition()
130131
}
131132
statedb, err := state.New(types.EmptyRootHash, db, nil)
@@ -146,15 +147,17 @@ func (ga *GenesisAlloc) deriveHash(cfg *params.ChainConfig, timestamp uint64) (c
146147
// flush is very similar with deriveHash, but the main difference is
147148
// all the generated states will be persisted into the given database.
148149
// Also, the genesis state specification will be flushed as well.
149-
func (ga *GenesisAlloc) flush(db ethdb.Database, triedb *trie.Database, blockhash common.Hash, cfg *params.ChainConfig) error {
150-
statedb, err := state.New(types.EmptyRootHash, state.NewDatabaseWithNodeDB(db, triedb), nil)
151-
if err != nil {
152-
return err
150+
func (ga *GenesisAlloc) flush(db ethdb.Database, triedb *trie.Database, blockhash common.Hash, cfg *params.ChainConfig, timestamp *uint64) error {
151+
database := state.NewDatabaseWithNodeDB(db, triedb)
152+
// End the verkle conversion at genesis if the fork block is 0
153+
if timestamp != nil && cfg.IsPrague(big.NewInt(int64(0)), *timestamp) {
154+
database.StartVerkleTransition(common.Hash{}, common.Hash{}, cfg, timestamp, common.Hash{})
155+
database.EndVerkleTransition()
153156
}
154157

155-
// End the verkle conversion at genesis if the fork block is 0
156-
if triedb.IsVerkle() {
157-
statedb.Database().EndVerkleTransition()
158+
statedb, err := state.New(types.EmptyRootHash, database, nil)
159+
if err != nil {
160+
return err
158161
}
159162

160163
for addr, account := range *ga {
@@ -221,7 +224,7 @@ func CommitGenesisState(db ethdb.Database, triedb *trie.Database, blockhash comm
221224
return errors.New("not found")
222225
}
223226
}
224-
return alloc.flush(db, triedb, blockhash, config)
227+
return alloc.flush(db, triedb, blockhash, config, nil)
225228
}
226229

227230
// GenesisAccount is an account in the state of the genesis block.
@@ -536,7 +539,7 @@ func (g *Genesis) Commit(db ethdb.Database, triedb *trie.Database) (*types.Block
536539
// All the checks has passed, flush the states derived from the genesis
537540
// specification as well as the specification itself into the provided
538541
// database.
539-
if err := g.Alloc.flush(db, triedb, block.Hash(), g.Config); err != nil {
542+
if err := g.Alloc.flush(db, triedb, block.Hash(), g.Config, &g.Timestamp); err != nil {
540543
return nil, err
541544
}
542545
rawdb.WriteTd(db, block.Hash(), block.NumberU64(), block.Difficulty())

0 commit comments

Comments
 (0)