Skip to content

Commit d25699a

Browse files
CPerezzgballet
andauthored
cmd/evm/internal/t8n: Binary at genesis support - Verkle <-> Binary swap (#552)
* remove: Verkle-related files with no codebase deps * fixup * refactor: update Prestate and transition logic for binary trie support - Introduced a new field `BT` in the `Prestate` struct to accommodate binary trie data. - Updated the `MakePreState` function to replace the `verkle` flag with `isBintrie` for better clarity. - Renamed functions and variables related to Verkle to their binary trie counterparts, including `BinKey`, `BinKeys`, and `BinTrieRoot`. - Added TODO comments for further updates and clarifications regarding the transition from Verkle to binary trie structures. * feat: add ChunkifyCode function for EVM bytecode chunking - Introduced the `ChunkedCode` type to represent 32-byte chunks of EVM bytecode. - Implemented the `ChunkifyCode` function to convert a byte array of EVM bytecode into chunked format. - Updated the `UpdateContractCode` method to utilize the new `ChunkifyCode` function. * refactor: rename Verkle commands to Binary Trie equivalents We leave the command name equal to keep support with https://github.com/ethereum/execution-spec-tests/tree/verkle/main * refactor: update flags and transition logic for Binary Trie support - Renamed `OutputVKTFlag` to `OutputBTFlag`. - Renamed `InputVKTFlag` to `InputBTFlag` and updated its usage description for prestate input. - Adjusted `Transition` function to utilize the new `BT` field for Binary Trie data instead of Verkle. - Updated `dispatchOutput` function to handle Binary Trie output correctly. * refactor: update DumpVKTLeaves to DumpBinTrieLeaves and adjust trie usage * refactor: update functions and comments for Binary Trie implementation - Replaced references to Verkle Trie with Binary Trie in various functions. - Updated `genBinTrieFromAlloc` to return a Binary Trie instead of a Verkle Trie. - Adjusted output functions to utilize Binary Trie methods and updated associated comments. * refactor: rename flags for Binary Trie integration * feat: add BinaryCodeChunkKey and BinaryCodeChunkCode functions for code chunking - Introduced `BinaryCodeChunkKey` to compute the tree key of a code-chunk for a given address. - Added `BinaryCodeChunkCode` to return the chunkification of bytecode. * feat: introduce TransitionTrie for Binary Trie integration - Added a new `TransitionTrie` type to facilitate the integration of the Binary Trie with the existing MPT trie. - Updated the `newTrieReader` function to utilize the new `TransitionTrie` implementation. - The transition tree has been moved to its own package `core/state/transitiontrie`, resolving the import cycle issue we get into when including `BinaryTrie` within `database.go`/ * refactor: update references to TransitionTrie and BinaryTrie integration - Replaced instances of `bintrie.BinaryTrie` with `transitiontrie.TransitionTrie` in the `execution.go`, `dump.go`, and `reader.go`. - Adjusted the `newTrieReader` function with atemporary workaround for import cycles. * trie/bintrie: fix StemNode serialization to only return actual data This fixes the "invalid serialized node length" error that occurred when Binary Trie nodes were persisted and later reloaded from the database. Issue Discovery: The error manifested when running execution-spec-tests with Binary Trie mode. After committing state changes, attempting to create a new StateDB with the committed root would fail with "invalid serialized node length" when deserializing the root node. Root Cause Analysis: Through debug logging, discovered that: 1. StemNodes were being serialized with 97 bytes of actual data 2. But the SerializeNode function was returning the entire 8224-byte buffer 3. When the node was later loaded and deserialized, it received 97 bytes 4. The deserializer expected at least 128 bytes for the bitmap and values 5. This mismatch caused the deserialization to fail The Fix: Changed SerializeNode to return only the actual data (serialized[:offset]) instead of the entire pre-allocated buffer (serialized[:]). This ensures that only the meaningful bytes are persisted to the database. This aligns the serialization with the deserialization logic, which correctly handles variable-length StemNode data based on which values are actually present in the node. * Fix Binary Trie StemNode serialization array size calculation This fixes a critical off-by-one error in the StemNode serialization buffer that was causing "invalid serialized node length" errors during execution-spec-tests. The error was discovered when running execution-spec-tests with Binary Trie mode: ``` cd ../execution-spec-tests && uv run fill --fork Verkle -v -m blockchain_test \ -k test_contract_creation -n 1 --evm-bin=PATH_TO/go-ethereum/evm ``` The tests were failing with: ``` ERROR: error getting prestate: invalid serialized node length ``` Through systematic debugging with hex dumps and bitmap analysis, we discovered that the serialized array was incorrectly sized: **Incorrect**: `var serialized [32 + 32 + 256*32]byte` - This allocated: 32 + 32 + 8192 = 8256 bytes - Missing 1 byte for the node type prefix **Correct**: `var serialized [1 + 31 + 32 + 256*32]byte` - This allocates: 1 + 31 + 32 + 8192 = 8256 bytes - Properly accounts for all fields per EIP-7864 The layout should be: - 1 byte: node type (nodeTypeStem) - 31 bytes: stem (path prefix) - 32 bytes: bitmap indicating which of 256 values are present - Up to 256*32 bytes: the actual values (32 bytes each) 1. Corrected array size from `[32 + 32 + 256*32]` to `[1 + 31 + 32 + 256*32]` 2. Cleaned up type assertions to use `n` from the type switch instead of `node.(*StemNode)` 3. This ensures proper alignment of all fields during serialization The misalignment was causing the deserializer to interpret value data as bitmap data, leading to impossible bitmap patterns (e.g., 122 bits set requiring 3904 bytes when only 97 bytes were available). After this fix, the "invalid serialized node length" errors are completely resolved. The execution-spec-tests now progress past the serialization stage, confirming that Binary Trie nodes are being correctly serialized and deserialized. * various tweaks 1. move the transition trie package to trie, as this will be requested by the geth team and it's where it belongs. 2. panic if state creation has failed in execution.go, to catch issues early. 3. use a BinaryTrie and not a TransitionTrie, as evm t8n doesn't support state trie transitions at this stage. * more replacements of TransitionTrie with VerkleTrie 1. TransitionTrie doesn't work at this stage, we are only testing the binary trees at genesis, so let's make sure TransitionTrie isn't activated anywhere. 2. There was a superfluous check for Transitioned(), which actually made the code take the wrong turn: if the verkle fork has occured, and if the transition isn't in progress, it means that the conversion has ended* * to be complete, it could be that this is in the period before the start of the iterator sweep, but in that case the transition tree also needs to be returned. So there is a missing granularity in this code, but it's ok at this stage since the transition isn't supported yet. * fix(state): Use BinaryTrie instead of VerkleTrie when IsVerkle is set The codebase uses IsVerkle flag to indicate Binary Trie mode (not renamed to avoid large diff). However, OpenTrie was incorrectly creating a VerkleTrie which expects Verkle node format, while we store Binary Trie nodes. This mismatch caused "invalid serialized node length" errors when VerkleTrie tried to deserialize Binary Trie nodes. We left some instantiation of `VerkleTree` arround which we have found and changed too here. * fix(bintrie): Remove incorrect type assertion in BinaryTrie.Commit The Commit function incorrectly assumed that the root node was always an InternalNode, causing a panic when the root was a StemNode: "interface conversion: bintrie.BinaryNode is *bintrie.StemNode, not *bintrie.InternalNode" Changes: - Remove type assertion `root := t.root.(*InternalNode)` - Call CollectNodes directly on t.root which works for any BinaryNode type - Add comment explaining the root can be any BinaryNode type This fix allows BinaryTrie to properly commit trees where the root is a StemNode, which can happen in small tries or during initial setup. * fix(t8ntool): Add error handling and debug logging to MakePreState Previously, state.New errors were silently ignored, leading to nil pointer panics later when statedb was accessed. This made debugging difficult as the actual error was hidden. Changes: - Add explicit error checking when creating initial statedb - Add explicit error checking when re-opening statedb after commit - Include meaningful error messages with context (e.g., root hash) * fix(bintrie): Fix iterator to properly handle StemNode leaf values This commit fixes two in the BinaryTrie iterator: 1. Fixed Leaf() method to only return true when positioned at a specific non-nil value within a StemNode, not just at the StemNode itself. The iterator tracks position using an Index that points to the NEXT position after the current value. 2. Fixed stack underflow in Next() method by checking if we're at the root before popping from the stack. This prevents index out of range errors when iterating through the tree. These fixes resolve panics in DumpBinTrieLeaves when generating VKT output for Binary Tries in the t8n tool. * fix: handle nil child nodes in BinaryTrie InternalNode When inserting values into an InternalNode with nil children, the code was attempting to dereference nil pointers, causing panics. This fix ensures that nil children are initialized to Empty{} nodes before attempting to call methods on them. This resolves crashes when building the Binary Trie from an empty or sparse initial state. * fix: preserve Binary Trie in Apply after commit When in Binary Trie mode (isEIP4762), the state.New() call after commit was creating a new StateDB with MPT trie, losing the Binary Trie. This resulted in nil trie when attempting to dump Binary Trie leaves. Fix: Skip state.New() for Binary Trie mode since the trie is already correct after commit. Only reopen state for MPT mode. * fix(t8n): preserve Binary Trie after commit and use correct JSON field name This commit fixes two issues that prevented VKT (Binary Trie) data from being correctly generated and passed to execution-spec-tests: 1. Binary Trie Preservation (execution.go): After statedb.Commit(), the code was calling state.New() which recreated the StateDB with an MPT trie, losing the Binary Trie. For Binary Trie mode (EIP-4762), we now skip state.New() since the trie is already correct after commit. 2. JSON Field Name (transition.go): The dispatch function was using name="bt" which created a JSON output field called "bt", but execution-spec-tests expects the field to be called "vkt" for compatibility with the upstream implementation. Changed to use name="vkt" instead. So translating, I'm dumb and I shouldn't have changed them.. These fixes allow execution-spec-tests to successfully extract Binary Trie leaf data from t8n output via stdout JSON parsing. Fixes test failures in: - tests/verkle/eip6800_genesis_verkle_tree/test_contract_creation.py * fix(t8n): revert change to avoid hardcoded MPT creation. Prior, the code was working such that we were getting `nil` when re-opening. We weren't using `TransitionTree` thus reader and other interfaces were working hardcoded to MPT. This was fixed in prev. commits. Thus, this was no longer needed. * fix(t8ntool, bintrie): address PR review comments * fix(t8ntool): update JSON field name for Binary Trie in Prestate There was a leftover as we modified already in `transition.go` flagnames to be `vkt` again and prevent errors for now. * refactor: use named constants for binary node serialization sizes Replace magic numbers with named constants for better code clarity: - NodeTypeBytes = 1 (size of node type prefix) - HashSize = 32 (size of a hash) - BitmapSize = 32 (size of the bitmap in stem nodes) This makes the serialization format more self-documenting and easier to maintain. * refactor: replace magic numbers with named constants in Binary Trie Replace hardcoded values (31, 32, 256) with named constants throughout the Binary Trie implementation for improved code maintainability. - Add NodeWidth (256), StemSize (31), HashSize (32), BitmapSize (32), NodeTypeBytes (1) constants - Update all serialization, deserialization, and node operations to use constants - Revert error wrapping to maintain test compatibility * feat: add GetBinaryTreeKeyBasicData function for Binary Trie Implement GetBinaryTreeKeyBasicData function similar to GetBinaryTreeKeyCodeHash but with offset 0 (BasicDataLeafKey) instead of 1, as per EIP-7864 tree embedding spec. * fix(t8ntool, bintrie): leftovers overlooked from review addressed * fix(bintrie): rename NodeWidth to StemNodeWidth throughout package The constant NodeWidth was renamed to StemNodeWidth to better reflect its purpose as the number of children per stem node (256 values). This change ensures consistency across the Binary Trie implementation and aligns with the EIP-7864 specification for Binary Merkle Tree format. Changes made: - Updated constant definition in binary_node.go from NodeWidth to StemNodeWidth - Replaced all references across the bintrie package files - Ensures compilation succeeds and all tests pass This fix was necessary after recent refactoring of the Binary Trie code that replaced the Verkle tree implementation at genesis. --------- Co-authored-by: Guillaume Ballet <[email protected]>
1 parent 553fea0 commit d25699a

File tree

21 files changed

+353
-537
lines changed

21 files changed

+353
-537
lines changed

.github/workflows/stable-spec-tests.yml

Lines changed: 0 additions & 77 deletions
This file was deleted.

cmd/evm/internal/t8ntool/execution.go

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import (
3939
"github.com/ethereum/go-ethereum/params"
4040
"github.com/ethereum/go-ethereum/rlp"
4141
"github.com/ethereum/go-ethereum/trie"
42+
"github.com/ethereum/go-ethereum/trie/bintrie"
4243
"github.com/ethereum/go-ethereum/triedb"
4344
"github.com/holiman/uint256"
4445
"golang.org/x/crypto/sha3"
@@ -47,7 +48,7 @@ import (
4748
type Prestate struct {
4849
Env stEnv `json:"env"`
4950
Pre types.GenesisAlloc `json:"pre"`
50-
VKT map[common.Hash]hexutil.Bytes `json:"vkt,omitempty"`
51+
BT map[common.Hash]hexutil.Bytes `json:"vkt,omitempty"`
5152
}
5253

5354
//go:generate go run github.com/fjl/gencodec -type ExecutionResult -field-override executionResultMarshaling -out gen_execresult.go
@@ -418,25 +419,28 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig,
418419
execRs.Requests = requests
419420
}
420421

421-
// Re-create statedb instance with new root upon the updated database
422-
// for accessing latest states.
422+
// Re-create statedb instance with new root for MPT mode
423423
statedb, err = state.New(root, statedb.Database())
424424
if err != nil {
425425
return nil, nil, nil, NewError(ErrorEVM, fmt.Errorf("could not reopen state: %v", err))
426426
}
427+
427428
body, _ := rlp.EncodeToBytes(includedTxs)
428429
return statedb, execRs, body, nil
429430
}
430431

431-
func MakePreState(db ethdb.Database, chainConfig *params.ChainConfig, pre *Prestate, verkle bool) *state.StateDB {
432-
tdb := triedb.NewDatabase(db, &triedb.Config{Preimages: true, IsVerkle: verkle})
432+
func MakePreState(db ethdb.Database, chainConfig *params.ChainConfig, pre *Prestate, isBintrie bool) *state.StateDB {
433+
tdb := triedb.NewDatabase(db, &triedb.Config{Preimages: true, IsVerkle: isBintrie})
433434
sdb := state.NewDatabase(tdb, nil)
434435

435436
root := types.EmptyRootHash
436-
if verkle {
437-
root = types.EmptyVerkleHash
437+
if isBintrie {
438+
root = types.EmptyBinaryHash
439+
}
440+
statedb, err := state.New(root, sdb)
441+
if err != nil {
442+
panic(fmt.Errorf("failed to create initial statedb: %v", err))
438443
}
439-
statedb, _ := state.New(root, sdb)
440444
for addr, a := range pre.Pre {
441445
statedb.SetCode(addr, a.Code, tracing.CodeChangeUnspecified)
442446
statedb.SetNonce(addr, a.Nonce, tracing.NonceChangeGenesis)
@@ -450,13 +454,20 @@ func MakePreState(db ethdb.Database, chainConfig *params.ChainConfig, pre *Prest
450454
if err != nil {
451455
panic(err)
452456
}
453-
// If verkle mode started, establish the conversion
454-
if verkle {
455-
if _, ok := statedb.GetTrie().(*trie.VerkleTrie); ok {
457+
// If bintrie mode started, check if conversion happened
458+
if isBintrie {
459+
if _, ok := statedb.GetTrie().(*bintrie.BinaryTrie); ok {
456460
return statedb
457461
}
462+
//TODO(@CPerezz): Fix this in upstream geth
463+
// If we're in bintrie mode but don't have a BinaryTrie, something went wrong
464+
panic(fmt.Errorf("binary trie mode enabled but trie is %T, not *bintrie.BinaryTrie", statedb.GetTrie()))
465+
}
466+
// For MPT mode, reopen the state with the committed root
467+
statedb, err = state.New(mptRoot, sdb)
468+
if err != nil {
469+
panic(fmt.Errorf("failed to re-open statedb after commit with root %x: %v", mptRoot, err))
458470
}
459-
statedb, _ = state.New(mptRoot, sdb)
460471
return statedb
461472
}
462473

cmd/evm/internal/t8ntool/flags.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ var (
8888
"\t<file> - into the file <file> ",
8989
Value: "block.json",
9090
}
91-
OutputVKTFlag = &cli.StringFlag{
91+
OutputBTFlag = &cli.StringFlag{
9292
Name: "output.vkt",
93-
Usage: "Determines where to put the `VKT` of the post-state.\n" +
93+
Usage: "Determines where to put the `BT` of the post-state.\n" +
9494
"\t`stdout` - into the stdout output\n" +
9595
"\t`stderr` - into the stderr output\n" +
9696
"\t<file> - into the file <file> ",
@@ -139,9 +139,10 @@ var (
139139
Usage: "`stdin` or file name of where to find the transactions list in RLP form.",
140140
Value: "txs.rlp",
141141
}
142-
InputVKTFlag = &cli.StringFlag{
142+
// 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)
143+
InputBTFlag = &cli.StringFlag{
143144
Name: "input.vkt",
144-
Usage: "`stdin` or file name of where to find the prestate VKT.",
145+
Usage: "`stdin` or file name of where to find the prestate BT.",
145146
}
146147
SealCliqueFlag = &cli.StringFlag{
147148
Name: "seal.clique",

0 commit comments

Comments
 (0)