Skip to content

Commit 2a2f106

Browse files
cmd/evm/internal/t8ntool, trie: support for verkle-at-genesis, use UBT, and move the transition tree to its own package (#32445)
This is broken off of #31730 to only focus on testing networks that start with verkle at genesis. The PR has seen a lot of work since its creation, and it now targets creating and re-executing tests for a binary tree testnet without the transition (so it starts at genesis). The transition tree has been moved to its own package. It also replaces verkle with the binary tree for this specific application. --------- Co-authored-by: Gary Rong <[email protected]>
1 parent 81c5b43 commit 2a2f106

30 files changed

+1142
-1468
lines changed

cmd/evm/internal/t8ntool/execution.go

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package t8ntool
1818

1919
import (
2020
"fmt"
21+
stdmath "math"
2122
"math/big"
2223

2324
"github.com/ethereum/go-ethereum/common"
@@ -43,8 +44,9 @@ import (
4344
)
4445

4546
type 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])

cmd/evm/internal/t8ntool/flags.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ var (
8888
"\t<file> - into the file <file> ",
8989
Value: "block.json",
9090
}
91+
OutputBTFlag = &cli.StringFlag{
92+
Name: "output.vkt",
93+
Usage: "Determines where to put the `BT` of the post-state.\n" +
94+
"\t`stdout` - into the stdout output\n" +
95+
"\t`stderr` - into the stderr output\n" +
96+
"\t<file> - into the file <file> ",
97+
Value: "vkt.json",
98+
}
9199
InputAllocFlag = &cli.StringFlag{
92100
Name: "input.alloc",
93101
Usage: "`stdin` or file name of where to find the prestate alloc to use.",
@@ -123,6 +131,11 @@ var (
123131
Usage: "`stdin` or file name of where to find the transactions list in RLP form.",
124132
Value: "txs.rlp",
125133
}
134+
// TODO(@CPerezz): rename `Name` of the file in a follow-up PR (relays on EEST -> https://github.com/ethereum/execution-spec-tests/tree/verkle/main)
135+
InputBTFlag = &cli.StringFlag{
136+
Name: "input.vkt",
137+
Usage: "`stdin` or file name of where to find the prestate BT.",
138+
}
126139
SealCliqueFlag = &cli.StringFlag{
127140
Name: "seal.clique",
128141
Usage: "Seal block with Clique. `stdin` or file name of where to find the Clique sealing data.",

0 commit comments

Comments
 (0)