@@ -18,6 +18,7 @@ package t8ntool
1818
1919import (
2020 "fmt"
21+ stdmath "math"
2122 "math/big"
2223
2324 "github.com/ethereum/go-ethereum/common"
@@ -43,8 +44,9 @@ import (
4344)
4445
4546type Prestate struct {
46- Env stEnv `json:"env"`
47- Pre types.GenesisAlloc `json:"pre"`
47+ Env stEnv `json:"env"`
48+ Pre types.GenesisAlloc `json:"pre"`
49+ TreeLeaves map [common.Hash ]hexutil.Bytes `json:"vkt,omitempty"`
4850}
4951
5052//go:generate go run github.com/fjl/gencodec -type ExecutionResult -field-override executionResultMarshaling -out gen_execresult.go
@@ -142,7 +144,8 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
142144 return h
143145 }
144146 var (
145- statedb = MakePreState (rawdb .NewMemoryDatabase (), pre .Pre )
147+ isEIP4762 = chainConfig .IsVerkle (big .NewInt (int64 (pre .Env .Number )), pre .Env .Timestamp )
148+ statedb = MakePreState (rawdb .NewMemoryDatabase (), pre .Pre , isEIP4762 )
146149 signer = types .MakeSigner (chainConfig , new (big.Int ).SetUint64 (pre .Env .Number ), pre .Env .Timestamp )
147150 gaspool = new (core.GasPool )
148151 blockHash = common.Hash {0x13 , 0x37 }
@@ -301,6 +304,10 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
301304 // Amount is in gwei, turn into wei
302305 amount := new (big.Int ).Mul (new (big.Int ).SetUint64 (w .Amount ), big .NewInt (params .GWei ))
303306 statedb .AddBalance (w .Address , uint256 .MustFromBig (amount ), tracing .BalanceIncreaseWithdrawal )
307+
308+ if isEIP4762 {
309+ statedb .AccessEvents ().AddAccount (w .Address , true , stdmath .MaxUint64 )
310+ }
304311 }
305312
306313 // Gather the execution-layer triggered requests.
@@ -361,8 +368,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
361368 execRs .Requests = requests
362369 }
363370
364- // Re-create statedb instance with new root upon the updated database
365- // for accessing latest states.
371+ // Re-create statedb instance with new root for MPT mode
366372 statedb , err = state .New (root , statedb .Database ())
367373 if err != nil {
368374 return nil , nil , nil , NewError (ErrorEVM , fmt .Errorf ("could not reopen state: %v" , err ))
@@ -371,12 +377,17 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
371377 return statedb , execRs , body , nil
372378}
373379
374- func MakePreState (db ethdb.Database , accounts types.GenesisAlloc ) * state.StateDB {
375- tdb := triedb .NewDatabase (db , & triedb.Config {Preimages : true })
380+ func MakePreState (db ethdb.Database , accounts types.GenesisAlloc , isBintrie bool ) * state.StateDB {
381+ tdb := triedb .NewDatabase (db , & triedb.Config {Preimages : true , IsVerkle : isBintrie })
376382 sdb := state .NewDatabase (tdb , nil )
377- statedb , err := state .New (types .EmptyRootHash , sdb )
383+
384+ root := types .EmptyRootHash
385+ if isBintrie {
386+ root = types .EmptyBinaryHash
387+ }
388+ statedb , err := state .New (root , sdb )
378389 if err != nil {
379- panic (fmt .Errorf ("failed to create initial state : %v" , err ))
390+ panic (fmt .Errorf ("failed to create initial statedb : %v" , err ))
380391 }
381392 for addr , a := range accounts {
382393 statedb .SetCode (addr , a .Code , tracing .CodeChangeUnspecified )
@@ -387,18 +398,23 @@ func MakePreState(db ethdb.Database, accounts types.GenesisAlloc) *state.StateDB
387398 }
388399 }
389400 // Commit and re-open to start with a clean state.
390- root , err : = statedb .Commit (0 , false , false )
401+ root , err = statedb .Commit (0 , false , false )
391402 if err != nil {
392403 panic (fmt .Errorf ("failed to commit initial state: %v" , err ))
393404 }
405+ // If bintrie mode started, check if conversion happened
406+ if isBintrie {
407+ return statedb
408+ }
409+ // For MPT mode, reopen the state with the committed root
394410 statedb , err = state .New (root , sdb )
395411 if err != nil {
396412 panic (fmt .Errorf ("failed to reopen state after commit: %v" , err ))
397413 }
398414 return statedb
399415}
400416
401- func rlpHash (x interface {} ) (h common.Hash ) {
417+ func rlpHash (x any ) (h common.Hash ) {
402418 hw := sha3 .NewLegacyKeccak256 ()
403419 rlp .Encode (hw , x )
404420 hw .Sum (h [:0 ])
0 commit comments