From a8ea6319f1551056205c6c436b9b0e0a1fbbef5e Mon Sep 17 00:00:00 2001 From: Mael Regnery Date: Wed, 8 Apr 2026 12:57:29 +0200 Subject: [PATCH 01/40] eth/filters: return -32602 when exceeding the block range limit (#34647) Co-authored-by: Felix Lange --- eth/filters/filter.go | 3 +-- eth/filters/filter_test.go | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/eth/filters/filter.go b/eth/filters/filter.go index 04e11f0475..f31b9568cd 100644 --- a/eth/filters/filter.go +++ b/eth/filters/filter.go @@ -19,7 +19,6 @@ package filters import ( "context" "errors" - "fmt" "math" "math/big" "slices" @@ -147,7 +146,7 @@ func (f *Filter) Logs(ctx context.Context) ([]*types.Log, error) { return nil, err } if f.rangeLimit != 0 && (end-begin) > f.rangeLimit { - return nil, fmt.Errorf("exceed maximum block range: %d", f.rangeLimit) + return nil, invalidParamsErr("exceed maximum block range %d", f.rangeLimit) } return f.rangeLogs(ctx, begin, end) } diff --git a/eth/filters/filter_test.go b/eth/filters/filter_test.go index e7b1b08046..c133438c64 100644 --- a/eth/filters/filter_test.go +++ b/eth/filters/filter_test.go @@ -19,6 +19,7 @@ package filters import ( "context" "encoding/json" + "errors" "math/big" "strings" "testing" @@ -634,7 +635,19 @@ func TestRangeLimit(t *testing.T) { // Set rangeLimit to 5, but request a range of 9 (end - begin = 9, from 0 to 9) filter := sys.NewRangeFilter(0, 9, nil, nil, 5) _, err = filter.Logs(context.Background()) - if err == nil || !strings.Contains(err.Error(), "exceed maximum block range") { - t.Fatalf("expected range limit error, got %v", err) + if err == nil { + t.Fatal("expected range limit error, got nil") + } + + var re rpc.Error + if errors.As(err, &re) { + if re.ErrorCode() != -32602 { + t.Fatalf("expected error code -32602, got %d", re.ErrorCode()) + } + if re.Error() != "exceed maximum block range 5" { + t.Fatalf("expected error message 'exceed maximum block range 5', got %q", re.Error()) + } + } else { + t.Fatalf("expected rpc error, got %v", err) } } From 21b19362c27762e2a1922e6a0f619c299b62d1b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felf=C3=B6ldi=20Zsolt?= Date: Thu, 9 Apr 2026 03:12:35 +0200 Subject: [PATCH 02/40] core/state: fix tracer hook for EIP-7708 burn logs (#34688) This PR fixes https://github.com/ethereum/go-ethereum/issues/34623 by changing the `vm.StateDB` interface: Instead of `EmitLogsForBurnAccounts()` emitting burn logs, `LogsForBurnAccounts() []*types.Log` just returns these logs which are then emitted by the caller. This way when tracing is used, `hookedStateDB.AddLog` will be used automatically and there is no need to duplicate either the burn log logic or the `OnLog` tracing hook. --- core/state/statedb.go | 19 +++++++++++-------- core/state/statedb_hooked.go | 4 ++-- core/state_transition.go | 4 +++- core/vm/interface.go | 2 +- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/core/state/statedb.go b/core/state/statedb.go index 854aaf6109..8b09ea89f6 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -745,7 +745,7 @@ type removedAccountWithBalance struct { balance *uint256.Int } -// EmitLogsForBurnAccounts emits the eth burn logs for accounts scheduled for +// LogsForBurnAccounts returns the eth burn logs for accounts scheduled for // removal which still have positive balance. The purpose of this function is // to handle a corner case of EIP-7708 where a self-destructed account might // still receive funds between sending/burning its previous balance and actual @@ -755,7 +755,7 @@ type removedAccountWithBalance struct { // // This function should only be invoked at the transaction boundary, specifically // before the Finalise. -func (s *StateDB) EmitLogsForBurnAccounts() { +func (s *StateDB) LogsForBurnAccounts() []*types.Log { var list []removedAccountWithBalance for addr := range s.journal.dirties { if obj, exist := s.stateObjects[addr]; exist && obj.selfDestructed && !obj.Balance().IsZero() { @@ -765,14 +765,17 @@ func (s *StateDB) EmitLogsForBurnAccounts() { }) } } - if list != nil { - sort.Slice(list, func(i, j int) bool { - return list[i].address.Cmp(list[j].address) < 0 - }) + if list == nil { + return nil } - for _, acct := range list { - s.AddLog(types.EthBurnLog(acct.address, acct.balance)) + sort.Slice(list, func(i, j int) bool { + return list[i].address.Cmp(list[j].address) < 0 + }) + logs := make([]*types.Log, len(list)) + for i, acct := range list { + logs[i] = types.EthBurnLog(acct.address, acct.balance) } + return logs } // Finalise finalises the state by removing the destructed objects and clears diff --git a/core/state/statedb_hooked.go b/core/state/statedb_hooked.go index 8c217fba48..52cf98d19b 100644 --- a/core/state/statedb_hooked.go +++ b/core/state/statedb_hooked.go @@ -229,8 +229,8 @@ func (s *hookedStateDB) AddLog(log *types.Log) { } } -func (s *hookedStateDB) EmitLogsForBurnAccounts() { - s.inner.EmitLogsForBurnAccounts() +func (s *hookedStateDB) LogsForBurnAccounts() []*types.Log { + return s.inner.LogsForBurnAccounts() } func (s *hookedStateDB) Finalise(deleteEmptyObjects bool) { diff --git a/core/state_transition.go b/core/state_transition.go index 52375bedaa..bd7e5daeff 100644 --- a/core/state_transition.go +++ b/core/state_transition.go @@ -584,7 +584,9 @@ func (st *stateTransition) execute() (*ExecutionResult, error) { } } if rules.IsAmsterdam { - st.evm.StateDB.EmitLogsForBurnAccounts() + for _, log := range st.evm.StateDB.LogsForBurnAccounts() { + st.evm.StateDB.AddLog(log) + } } return &ExecutionResult{ UsedGas: st.gasUsed(), diff --git a/core/vm/interface.go b/core/vm/interface.go index 6a93846ac5..d7c4340e06 100644 --- a/core/vm/interface.go +++ b/core/vm/interface.go @@ -87,7 +87,7 @@ type StateDB interface { Snapshot() int AddLog(*types.Log) - EmitLogsForBurnAccounts() + LogsForBurnAccounts() []*types.Log AddPreimage(common.Hash, []byte) Witness() *stateless.Witness From 68c7058a80859dc5de75d3e34708cd1ea2f094e5 Mon Sep 17 00:00:00 2001 From: Sina M <1591639+s1na@users.noreply.github.com> Date: Thu, 9 Apr 2026 03:19:54 +0200 Subject: [PATCH 03/40] core/stateless: fix parsing an empty witness (#34683) This is to fix a crasher in keeper. --- core/stateless/encoding.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core/stateless/encoding.go b/core/stateless/encoding.go index d559178892..1b20c4cb2a 100644 --- a/core/stateless/encoding.go +++ b/core/stateless/encoding.go @@ -17,6 +17,7 @@ package stateless import ( + "errors" "io" "github.com/ethereum/go-ethereum/common/hexutil" @@ -42,6 +43,9 @@ func (w *Witness) ToExtWitness() *ExtWitness { // FromExtWitness converts the consensus witness format into our internal one. func (w *Witness) FromExtWitness(ext *ExtWitness) error { + if len(ext.Headers) == 0 { + return errors.New("witness must contain at least one header") + } w.Headers = ext.Headers w.Codes = make(map[string]struct{}, len(ext.Codes)) From 3772bb536a67a2f17ed98244775eb16698f79140 Mon Sep 17 00:00:00 2001 From: CPerezz <37264926+CPerezz@users.noreply.github.com> Date: Thu, 9 Apr 2026 07:39:38 +0200 Subject: [PATCH 04/40] triedb/pathdb: fix lookup sentinel collision with zero disk layer root (#34680) --- triedb/pathdb/layertree.go | 8 +-- triedb/pathdb/layertree_test.go | 115 ++++++++++++++++++++++++++++++++ triedb/pathdb/lookup.go | 37 +++++----- 3 files changed, 137 insertions(+), 23 deletions(-) diff --git a/triedb/pathdb/layertree.go b/triedb/pathdb/layertree.go index 0d7ca5a5b4..b20e40bd05 100644 --- a/triedb/pathdb/layertree.go +++ b/triedb/pathdb/layertree.go @@ -319,8 +319,8 @@ func (tree *layerTree) lookupAccount(accountHash common.Hash, state common.Hash) tree.lock.RLock() defer tree.lock.RUnlock() - tip := tree.lookup.accountTip(accountHash, state, tree.base.root) - if tip == (common.Hash{}) { + tip, ok := tree.lookup.accountTip(accountHash, state, tree.base.root) + if !ok { return nil, fmt.Errorf("[%#x] %w", state, errSnapshotStale) } l := tree.layers[tip] @@ -337,8 +337,8 @@ func (tree *layerTree) lookupStorage(accountHash common.Hash, slotHash common.Ha tree.lock.RLock() defer tree.lock.RUnlock() - tip := tree.lookup.storageTip(accountHash, slotHash, state, tree.base.root) - if tip == (common.Hash{}) { + tip, ok := tree.lookup.storageTip(accountHash, slotHash, state, tree.base.root) + if !ok { return nil, fmt.Errorf("[%#x] %w", state, errSnapshotStale) } l := tree.layers[tip] diff --git a/triedb/pathdb/layertree_test.go b/triedb/pathdb/layertree_test.go index 285ca67b6c..82eb182990 100644 --- a/triedb/pathdb/layertree_test.go +++ b/triedb/pathdb/layertree_test.go @@ -916,3 +916,118 @@ func TestStorageLookup(t *testing.T) { } } } + +// TestLookupZeroBaseRootFallback is a regression test for a sentinel +// collision in accountTip/storageTip: before the fix they returned +// common.Hash{} as both the "stale" marker and the disk-layer fallback +// when the disk root itself happened to be zero. lookupAccount/Storage +// then misreported a legitimate fallback as errSnapshotStale. +// +// On the merkle path the collision was invisible because the empty +// merkle trie hashes to types.EmptyRootHash (a concrete non-zero +// keccak), so the disk layer's root was never the zero hash in +// practice. The bug only surfaces once the disk layer root can +// legitimately be zero (for example a fresh verkle/bintrie database +// where the empty binary trie hashes to EmptyVerkleHash == +// common.Hash{}). +// +// The test constructs a layer tree whose base layer's root IS the zero +// hash, stacks diff layers on top, and exercises four cases: +// +// 1. Look up an account NEVER written → should fall through to the +// disk layer and return (diskLayer, nil). Before the fix this +// returned errSnapshotStale because the fallback hash collided +// with the sentinel. +// 2. Symmetric case for lookupStorage. +// 3. Look up an account written in a diff layer → should return that +// diff layer (the normal happy path is unaffected by the fix). +// 4. Look up any key at a state root that isn't part of the tree +// (neither the disk root nor a descendant of it) → MUST still +// return errSnapshotStale. This pins the "other half" of the +// contract so a future refactor that always returns ok=true would +// fail here. +func TestLookupZeroBaseRootFallback(t *testing.T) { + // Build a layer tree whose disk-layer root is common.Hash{} — + // mirrors the bintrie/verkle configuration where the empty trie + // hashes to EmptyVerkleHash. newTestLayerTree can't be reused + // because it hard-codes common.Hash{0x1}. + db := New(rawdb.NewMemoryDatabase(), nil, false) + base := newDiskLayer(common.Hash{}, 0, db, nil, nil, newBuffer(0, nil, nil, 0), nil) + tr := newLayerTree(base) + + // Stack two diff layers on the zero-rooted disk layer, each + // touching a known account and slot so we have something for the + // happy-path lookups to find later. + if err := tr.add( + common.Hash{0x2}, common.Hash{}, + 1, + NewNodeSetWithOrigin(nil, nil), + NewStateSetWithOrigin( + randomAccountSet("0xa"), + randomStorageSet([]string{"0xa"}, [][]string{{"0x1"}}, nil), + nil, nil, false), + ); err != nil { + t.Fatalf("add first diff layer: %v", err) + } + if err := tr.add( + common.Hash{0x3}, common.Hash{0x2}, + 2, + NewNodeSetWithOrigin(nil, nil), + NewStateSetWithOrigin( + randomAccountSet("0xb"), + nil, nil, nil, false), + ); err != nil { + t.Fatalf("add second diff layer: %v", err) + } + + // Case 1: unknown account queried at the head. The lookup must + // fall through the diff layers, hit the disk-layer fallback at + // base=common.Hash{}, and return the disk layer with no error — + // NOT errSnapshotStale. + l, err := tr.lookupAccount(common.HexToHash("0xdead"), common.Hash{0x3}) + if err != nil { + t.Fatalf("lookupAccount on zero-base disk layer: unexpected error %v", err) + } + if l.rootHash() != (common.Hash{}) { + t.Errorf("expected fall-through to disk layer (root=0), got %x", l.rootHash()) + } + + // Case 2: symmetric check for storage. Slot 0x99 was never written, + // so the lookup must fall through to the disk layer just like + // Case 1. + l, err = tr.lookupStorage( + common.HexToHash("0xdead"), common.HexToHash("0x99"), common.Hash{0x3}) + if err != nil { + t.Fatalf("lookupStorage on zero-base disk layer: unexpected error %v", err) + } + if l.rootHash() != (common.Hash{}) { + t.Errorf("expected fall-through to disk layer (root=0), got %x", l.rootHash()) + } + + // Case 3: happy path. Account 0xa was written at diff layer 0x2. + // The lookup must return that layer, proving the fix didn't break + // the normal resolution path. + l, err = tr.lookupAccount(common.HexToHash("0xa"), common.Hash{0x3}) + if err != nil { + t.Fatalf("lookupAccount(known): %v", err) + } + if l.rootHash() != (common.Hash{0x2}) { + t.Errorf("known account tip: want %x, got %x", + common.Hash{0x2}, l.rootHash()) + } + + // Case 4: truly stale state root. This pins the other half of the + // contract — the boolean must actually signal not-found for an + // unknown state, otherwise a refactor that always returned + // ok=true would still pass cases 1–3. + _, err = tr.lookupAccount(common.HexToHash("0xa"), common.HexToHash("0xdeadbeef")) + if !errors.Is(err, errSnapshotStale) { + t.Errorf("lookupAccount(stale state): want errSnapshotStale, got %v", err) + } + _, err = tr.lookupStorage( + common.HexToHash("0xa"), common.HexToHash("0x1"), + common.HexToHash("0xdeadbeef")) + if !errors.Is(err, errSnapshotStale) { + t.Errorf("lookupStorage(stale state): want errSnapshotStale, got %v", err) + } +} diff --git a/triedb/pathdb/lookup.go b/triedb/pathdb/lookup.go index 719546f410..9b300ec871 100644 --- a/triedb/pathdb/lookup.go +++ b/triedb/pathdb/lookup.go @@ -92,12 +92,16 @@ func newLookup(head layer, descendant func(state common.Hash, ancestor common.Ha // stateID or is a descendant of it. // // If found, the account data corresponding to the supplied stateID resides -// in that layer. Otherwise, two scenarios are possible: +// in the layer identified by the returned hash (ok=true). Otherwise, +// (common.Hash{}, false) is returned to signal that the supplied stateID is +// stale. // -// (a) the account remains unmodified from the current disk layer up to the state -// layer specified by the stateID: fallback to the disk layer for data retrieval, -// (b) or the layer specified by the stateID is stale: reject the data retrieval. -func (l *lookup) accountTip(accountHash common.Hash, stateID common.Hash, base common.Hash) common.Hash { +// Note the returned hash may itself be common.Hash{} when the disk layer's +// root is zero — as is the case for a fresh verkle/bintrie database whose +// empty trie hashes to EmptyVerkleHash. Callers must therefore consult the +// boolean rather than comparing the returned hash against common.Hash{} +// directly. +func (l *lookup) accountTip(accountHash common.Hash, stateID common.Hash, base common.Hash) (common.Hash, bool) { // Traverse the mutation history from latest to oldest one. Several // scenarios are possible: // @@ -123,31 +127,26 @@ func (l *lookup) accountTip(accountHash common.Hash, stateID common.Hash, base c // containing the modified data. Otherwise, the current state may be ahead // of the requested one or belong to a different branch. if list[i] == stateID || l.descendant(stateID, list[i]) { - return list[i] + return list[i], true } } // No layer matching the stateID or its descendants was found. Use the // current disk layer as a fallback. if base == stateID || l.descendant(stateID, base) { - return base + return base, true } // The layer associated with 'stateID' is not the descendant of the current // disk layer, it's already stale, return nothing. - return common.Hash{} + return common.Hash{}, false } // storageTip traverses the layer list associated with the given account and // slot hash in reverse order to locate the first entry that either matches // the specified stateID or is a descendant of it. // -// If found, the storage data corresponding to the supplied stateID resides -// in that layer. Otherwise, two scenarios are possible: -// -// (a) the storage slot remains unmodified from the current disk layer up to -// the state layer specified by the stateID: fallback to the disk layer for -// data retrieval, (b) or the layer specified by the stateID is stale: reject -// the data retrieval. -func (l *lookup) storageTip(accountHash common.Hash, slotHash common.Hash, stateID common.Hash, base common.Hash) common.Hash { +// See accountTip for the returned-hash / ok convention — the same +// bintrie-zero-root caveat applies here. +func (l *lookup) storageTip(accountHash common.Hash, slotHash common.Hash, stateID common.Hash, base common.Hash) (common.Hash, bool) { list := l.storages[storageKey(accountHash, slotHash)] for i := len(list) - 1; i >= 0; i-- { // If the current state matches the stateID, or the requested state is a @@ -155,17 +154,17 @@ func (l *lookup) storageTip(accountHash common.Hash, slotHash common.Hash, state // containing the modified data. Otherwise, the current state may be ahead // of the requested one or belong to a different branch. if list[i] == stateID || l.descendant(stateID, list[i]) { - return list[i] + return list[i], true } } // No layer matching the stateID or its descendants was found. Use the // current disk layer as a fallback. if base == stateID || l.descendant(stateID, base) { - return base + return base, true } // The layer associated with 'stateID' is not the descendant of the current // disk layer, it's already stale, return nothing. - return common.Hash{} + return common.Hash{}, false } // addLayer traverses the state data retained in the specified diff layer and From 58557cb4635d4e6f3e49fcdc82a6469554e929a6 Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Thu, 9 Apr 2026 10:27:19 +0200 Subject: [PATCH 05/40] cmd/geth: add subcommand for offline binary tree conversion (#33740) This tool is designed for the offline translation of an MPT database to a binary trie. This is to be used for users who e.g. want to prove equivalence of a binary tree chain shadowing the MPT chain. It adds a `bintrie` command, cleanly separating the concerns. --- cmd/geth/bintrie_convert.go | 408 +++++++++++++++++++++++++++++++ cmd/geth/bintrie_convert_test.go | 229 +++++++++++++++++ cmd/geth/main.go | 2 + 3 files changed, 639 insertions(+) create mode 100644 cmd/geth/bintrie_convert.go create mode 100644 cmd/geth/bintrie_convert_test.go diff --git a/cmd/geth/bintrie_convert.go b/cmd/geth/bintrie_convert.go new file mode 100644 index 0000000000..3730768697 --- /dev/null +++ b/cmd/geth/bintrie_convert.go @@ -0,0 +1,408 @@ +// Copyright 2026 The go-ethereum Authors +// This file is part of go-ethereum. +// +// go-ethereum is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// go-ethereum is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with go-ethereum. If not, see . + +package main + +import ( + "errors" + "fmt" + "runtime" + "runtime/debug" + "slices" + "time" + + "github.com/ethereum/go-ethereum/cmd/utils" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/rawdb" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/rlp" + "github.com/ethereum/go-ethereum/trie" + "github.com/ethereum/go-ethereum/trie/bintrie" + "github.com/ethereum/go-ethereum/trie/trienode" + "github.com/ethereum/go-ethereum/triedb" + "github.com/ethereum/go-ethereum/triedb/pathdb" + "github.com/urfave/cli/v2" +) + +var ( + deleteSourceFlag = &cli.BoolFlag{ + Name: "delete-source", + Usage: "Delete MPT trie nodes after conversion", + } + memoryLimitFlag = &cli.Uint64Flag{ + Name: "memory-limit", + Usage: "Max heap allocation in MB before forcing a commit cycle", + Value: 16384, + } + + bintrieCommand = &cli.Command{ + Name: "bintrie", + Usage: "A set of commands for binary trie operations", + Description: "", + Subcommands: []*cli.Command{ + { + Name: "convert", + Usage: "Convert MPT state to binary trie", + ArgsUsage: "[state-root]", + Action: convertToBinaryTrie, + Flags: slices.Concat([]cli.Flag{ + deleteSourceFlag, + memoryLimitFlag, + }, utils.NetworkFlags, utils.DatabaseFlags), + Description: ` +geth bintrie convert [--delete-source] [--memory-limit MB] [state-root] + +Reads all state from the Merkle Patricia Trie and writes it into a Binary Trie, +operating offline. Memory-safe via periodic commit-and-reload cycles. + +The optional state-root argument specifies which state root to convert. +If omitted, the head block's state root is used. + +Flags: + --delete-source Delete MPT trie nodes after successful conversion + --memory-limit Max heap allocation in MB before forcing a commit (default: 16384) +`, + }, + }, + } +) + +type conversionStats struct { + accounts uint64 + slots uint64 + codes uint64 + commits uint64 + start time.Time + lastReport time.Time + lastMemChk time.Time +} + +func (s *conversionStats) report(force bool) { + if !force && time.Since(s.lastReport) < 8*time.Second { + return + } + elapsed := time.Since(s.start).Seconds() + acctRate := float64(0) + if elapsed > 0 { + acctRate = float64(s.accounts) / elapsed + } + log.Info("Conversion progress", + "accounts", s.accounts, + "slots", s.slots, + "codes", s.codes, + "commits", s.commits, + "accounts/sec", fmt.Sprintf("%.0f", acctRate), + "elapsed", common.PrettyDuration(time.Since(s.start)), + ) + s.lastReport = time.Now() +} + +func convertToBinaryTrie(ctx *cli.Context) error { + if ctx.NArg() > 1 { + return errors.New("too many arguments") + } + stack, _ := makeConfigNode(ctx) + defer stack.Close() + + chaindb := utils.MakeChainDatabase(ctx, stack, false) + defer chaindb.Close() + + headBlock := rawdb.ReadHeadBlock(chaindb) + if headBlock == nil { + return errors.New("no head block found") + } + var ( + root common.Hash + err error + ) + if ctx.NArg() == 1 { + root, err = parseRoot(ctx.Args().First()) + if err != nil { + return fmt.Errorf("invalid state root: %w", err) + } + } else { + root = headBlock.Root() + } + log.Info("Starting MPT to binary trie conversion", "root", root, "block", headBlock.NumberU64()) + + srcTriedb := utils.MakeTrieDatabase(ctx, stack, chaindb, true, true, false) + defer srcTriedb.Close() + + destTriedb := triedb.NewDatabase(chaindb, &triedb.Config{ + IsVerkle: true, + PathDB: &pathdb.Config{ + JournalDirectory: stack.ResolvePath("triedb-bintrie"), + }, + }) + defer destTriedb.Close() + + binTrie, err := bintrie.NewBinaryTrie(types.EmptyBinaryHash, destTriedb) + if err != nil { + return fmt.Errorf("failed to create binary trie: %w", err) + } + memLimit := ctx.Uint64(memoryLimitFlag.Name) * 1024 * 1024 + + currentRoot, err := runConversionLoop(chaindb, srcTriedb, destTriedb, binTrie, root, memLimit) + if err != nil { + return err + } + log.Info("Conversion complete", "binaryRoot", currentRoot) + + if ctx.Bool(deleteSourceFlag.Name) { + log.Info("Deleting source MPT data") + if err := deleteMPTData(chaindb, srcTriedb, root); err != nil { + return fmt.Errorf("MPT deletion failed: %w", err) + } + log.Info("Source MPT data deleted") + } + return nil +} + +func runConversionLoop(chaindb ethdb.Database, srcTriedb *triedb.Database, destTriedb *triedb.Database, binTrie *bintrie.BinaryTrie, root common.Hash, memLimit uint64) (common.Hash, error) { + currentRoot := types.EmptyBinaryHash + stats := &conversionStats{ + start: time.Now(), + lastReport: time.Now(), + lastMemChk: time.Now(), + } + + srcTrie, err := trie.NewStateTrie(trie.StateTrieID(root), srcTriedb) + if err != nil { + return common.Hash{}, fmt.Errorf("failed to open source trie: %w", err) + } + acctIt, err := srcTrie.NodeIterator(nil) + if err != nil { + return common.Hash{}, fmt.Errorf("failed to create account iterator: %w", err) + } + accIter := trie.NewIterator(acctIt) + + for accIter.Next() { + var acc types.StateAccount + if err := rlp.DecodeBytes(accIter.Value, &acc); err != nil { + return common.Hash{}, fmt.Errorf("invalid account RLP: %w", err) + } + addrBytes := srcTrie.GetKey(accIter.Key) + if addrBytes == nil { + return common.Hash{}, fmt.Errorf("missing preimage for account hash %x (run with --cache.preimages)", accIter.Key) + } + addr := common.BytesToAddress(addrBytes) + + var code []byte + codeHash := common.BytesToHash(acc.CodeHash) + if codeHash != types.EmptyCodeHash { + code = rawdb.ReadCode(chaindb, codeHash) + if code == nil { + return common.Hash{}, fmt.Errorf("missing code for hash %x (account %x)", codeHash, addr) + } + stats.codes++ + } + + if err := binTrie.UpdateAccount(addr, &acc, len(code)); err != nil { + return common.Hash{}, fmt.Errorf("failed to update account %x: %w", addr, err) + } + if len(code) > 0 { + if err := binTrie.UpdateContractCode(addr, codeHash, code); err != nil { + return common.Hash{}, fmt.Errorf("failed to update code for %x: %w", addr, err) + } + } + + if acc.Root != types.EmptyRootHash { + addrHash := common.BytesToHash(accIter.Key) + storageTrie, err := trie.NewStateTrie(trie.StorageTrieID(root, addrHash, acc.Root), srcTriedb) + if err != nil { + return common.Hash{}, fmt.Errorf("failed to open storage trie for %x: %w", addr, err) + } + storageNodeIt, err := storageTrie.NodeIterator(nil) + if err != nil { + return common.Hash{}, fmt.Errorf("failed to create storage iterator for %x: %w", addr, err) + } + storageIter := trie.NewIterator(storageNodeIt) + + slotCount := uint64(0) + for storageIter.Next() { + slotKey := storageTrie.GetKey(storageIter.Key) + if slotKey == nil { + return common.Hash{}, fmt.Errorf("missing preimage for storage key %x (account %x)", storageIter.Key, addr) + } + _, content, _, err := rlp.Split(storageIter.Value) + if err != nil { + return common.Hash{}, fmt.Errorf("invalid storage RLP for key %x (account %x): %w", slotKey, addr, err) + } + if err := binTrie.UpdateStorage(addr, slotKey, content); err != nil { + return common.Hash{}, fmt.Errorf("failed to update storage %x/%x: %w", addr, slotKey, err) + } + stats.slots++ + slotCount++ + + if slotCount%10000 == 0 { + binTrie, currentRoot, err = maybeCommit(binTrie, currentRoot, destTriedb, memLimit, stats) + if err != nil { + return common.Hash{}, err + } + } + } + if storageIter.Err != nil { + return common.Hash{}, fmt.Errorf("storage iteration error for %x: %w", addr, storageIter.Err) + } + } + stats.accounts++ + stats.report(false) + + if stats.accounts%1000 == 0 { + binTrie, currentRoot, err = maybeCommit(binTrie, currentRoot, destTriedb, memLimit, stats) + if err != nil { + return common.Hash{}, err + } + } + } + if accIter.Err != nil { + return common.Hash{}, fmt.Errorf("account iteration error: %w", accIter.Err) + } + + _, currentRoot, err = commitBinaryTrie(binTrie, currentRoot, destTriedb) + if err != nil { + return common.Hash{}, fmt.Errorf("final commit failed: %w", err) + } + stats.commits++ + stats.report(true) + return currentRoot, nil +} + +func maybeCommit(bt *bintrie.BinaryTrie, currentRoot common.Hash, destDB *triedb.Database, memLimit uint64, stats *conversionStats) (*bintrie.BinaryTrie, common.Hash, error) { + if time.Since(stats.lastMemChk) < 5*time.Second { + return bt, currentRoot, nil + } + stats.lastMemChk = time.Now() + + var m runtime.MemStats + runtime.ReadMemStats(&m) + if m.Alloc < memLimit { + return bt, currentRoot, nil + } + log.Info("Memory limit reached, committing", "alloc", common.StorageSize(m.Alloc), "limit", common.StorageSize(memLimit)) + + bt, currentRoot, err := commitBinaryTrie(bt, currentRoot, destDB) + if err != nil { + return nil, common.Hash{}, err + } + stats.commits++ + stats.report(true) + return bt, currentRoot, nil +} + +func commitBinaryTrie(bt *bintrie.BinaryTrie, currentRoot common.Hash, destDB *triedb.Database) (*bintrie.BinaryTrie, common.Hash, error) { + newRoot, nodeSet := bt.Commit(false) + if nodeSet != nil { + merged := trienode.NewWithNodeSet(nodeSet) + if err := destDB.Update(newRoot, currentRoot, 0, merged, triedb.NewStateSet()); err != nil { + return nil, common.Hash{}, fmt.Errorf("triedb update failed: %w", err) + } + if err := destDB.Commit(newRoot, false); err != nil { + return nil, common.Hash{}, fmt.Errorf("triedb commit failed: %w", err) + } + } + runtime.GC() + debug.FreeOSMemory() + + bt, err := bintrie.NewBinaryTrie(newRoot, destDB) + if err != nil { + return nil, common.Hash{}, fmt.Errorf("failed to reload binary trie: %w", err) + } + return bt, newRoot, nil +} + +func deleteMPTData(chaindb ethdb.Database, srcTriedb *triedb.Database, root common.Hash) error { + isPathDB := srcTriedb.Scheme() == rawdb.PathScheme + + srcTrie, err := trie.NewStateTrie(trie.StateTrieID(root), srcTriedb) + if err != nil { + return fmt.Errorf("failed to open source trie for deletion: %w", err) + } + acctIt, err := srcTrie.NodeIterator(nil) + if err != nil { + return fmt.Errorf("failed to create account iterator for deletion: %w", err) + } + batch := chaindb.NewBatch() + deleted := 0 + + for acctIt.Next(true) { + if isPathDB { + rawdb.DeleteAccountTrieNode(batch, acctIt.Path()) + } else { + node := acctIt.Hash() + if node != (common.Hash{}) { + rawdb.DeleteLegacyTrieNode(batch, node) + } + } + deleted++ + + if acctIt.Leaf() { + var acc types.StateAccount + if err := rlp.DecodeBytes(acctIt.LeafBlob(), &acc); err != nil { + return fmt.Errorf("invalid account during deletion: %w", err) + } + if acc.Root != types.EmptyRootHash { + addrHash := common.BytesToHash(acctIt.LeafKey()) + storageTrie, err := trie.NewStateTrie(trie.StorageTrieID(root, addrHash, acc.Root), srcTriedb) + if err != nil { + return fmt.Errorf("failed to open storage trie for deletion: %w", err) + } + storageIt, err := storageTrie.NodeIterator(nil) + if err != nil { + return fmt.Errorf("failed to create storage iterator for deletion: %w", err) + } + for storageIt.Next(true) { + if isPathDB { + rawdb.DeleteStorageTrieNode(batch, addrHash, storageIt.Path()) + } else { + node := storageIt.Hash() + if node != (common.Hash{}) { + rawdb.DeleteLegacyTrieNode(batch, node) + } + } + deleted++ + if batch.ValueSize() >= ethdb.IdealBatchSize { + if err := batch.Write(); err != nil { + return fmt.Errorf("batch write failed: %w", err) + } + batch.Reset() + } + } + if storageIt.Error() != nil { + return fmt.Errorf("storage deletion iterator error: %w", storageIt.Error()) + } + } + } + if batch.ValueSize() >= ethdb.IdealBatchSize { + if err := batch.Write(); err != nil { + return fmt.Errorf("batch write failed: %w", err) + } + batch.Reset() + } + } + if acctIt.Error() != nil { + return fmt.Errorf("account deletion iterator error: %w", acctIt.Error()) + } + if batch.ValueSize() > 0 { + if err := batch.Write(); err != nil { + return fmt.Errorf("final batch write failed: %w", err) + } + } + log.Info("MPT deletion complete", "nodesDeleted", deleted) + return nil +} diff --git a/cmd/geth/bintrie_convert_test.go b/cmd/geth/bintrie_convert_test.go new file mode 100644 index 0000000000..9b95f6a70f --- /dev/null +++ b/cmd/geth/bintrie_convert_test.go @@ -0,0 +1,229 @@ +// Copyright 2026 The go-ethereum Authors +// This file is part of go-ethereum. +// +// go-ethereum is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// go-ethereum is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with go-ethereum. If not, see . + +package main + +import ( + "math" + "math/big" + "testing" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/rawdb" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/params" + "github.com/ethereum/go-ethereum/trie/bintrie" + "github.com/ethereum/go-ethereum/triedb" + "github.com/ethereum/go-ethereum/triedb/pathdb" + "github.com/holiman/uint256" +) + +func TestBintrieConvert(t *testing.T) { + var ( + addr1 = common.HexToAddress("0x1111111111111111111111111111111111111111") + addr2 = common.HexToAddress("0x2222222222222222222222222222222222222222") + slotKey1 = common.HexToHash("0x01") + slotKey2 = common.HexToHash("0x02") + slotVal1 = common.HexToHash("0xdeadbeef") + slotVal2 = common.HexToHash("0xcafebabe") + code = []byte{0x60, 0x42, 0x60, 0x00, 0x52, 0x60, 0x20, 0x60, 0x00, 0xf3} + ) + + chaindb := rawdb.NewMemoryDatabase() + + srcTriedb := triedb.NewDatabase(chaindb, &triedb.Config{ + Preimages: true, + PathDB: pathdb.Defaults, + }) + + gspec := &core.Genesis{ + Config: params.TestChainConfig, + BaseFee: big.NewInt(params.InitialBaseFee), + Alloc: types.GenesisAlloc{ + addr1: { + Balance: big.NewInt(1000000), + Nonce: 5, + }, + addr2: { + Balance: big.NewInt(2000000), + Nonce: 10, + Code: code, + Storage: map[common.Hash]common.Hash{ + slotKey1: slotVal1, + slotKey2: slotVal2, + }, + }, + }, + } + + genesisBlock := gspec.MustCommit(chaindb, srcTriedb) + root := genesisBlock.Root() + t.Logf("Genesis root: %x", root) + srcTriedb.Close() + + srcTriedb2 := triedb.NewDatabase(chaindb, &triedb.Config{ + Preimages: true, + PathDB: &pathdb.Config{ReadOnly: true}, + }) + defer srcTriedb2.Close() + + destTriedb := triedb.NewDatabase(chaindb, &triedb.Config{ + IsVerkle: true, + PathDB: pathdb.Defaults, + }) + defer destTriedb.Close() + + bt, err := bintrie.NewBinaryTrie(types.EmptyBinaryHash, destTriedb) + if err != nil { + t.Fatalf("failed to create binary trie: %v", err) + } + + currentRoot, err := runConversionLoop(chaindb, srcTriedb2, destTriedb, bt, root, math.MaxUint64) + if err != nil { + t.Fatalf("conversion failed: %v", err) + } + t.Logf("Binary trie root: %x", currentRoot) + + bt2, err := bintrie.NewBinaryTrie(currentRoot, destTriedb) + if err != nil { + t.Fatalf("failed to reload binary trie: %v", err) + } + + acc1, err := bt2.GetAccount(addr1) + if err != nil { + t.Fatalf("failed to get account1: %v", err) + } + if acc1 == nil { + t.Fatal("account1 not found in binary trie") + } + if acc1.Nonce != 5 { + t.Errorf("account1 nonce: got %d, want 5", acc1.Nonce) + } + wantBal1 := uint256.NewInt(1000000) + if acc1.Balance.Cmp(wantBal1) != 0 { + t.Errorf("account1 balance: got %s, want %s", acc1.Balance, wantBal1) + } + + acc2, err := bt2.GetAccount(addr2) + if err != nil { + t.Fatalf("failed to get account2: %v", err) + } + if acc2 == nil { + t.Fatal("account2 not found in binary trie") + } + if acc2.Nonce != 10 { + t.Errorf("account2 nonce: got %d, want 10", acc2.Nonce) + } + wantBal2 := uint256.NewInt(2000000) + if acc2.Balance.Cmp(wantBal2) != 0 { + t.Errorf("account2 balance: got %s, want %s", acc2.Balance, wantBal2) + } + + treeKey1 := bintrie.GetBinaryTreeKeyStorageSlot(addr2, slotKey1[:]) + val1, err := bt2.GetWithHashedKey(treeKey1) + if err != nil { + t.Fatalf("failed to get storage slot1: %v", err) + } + if len(val1) == 0 { + t.Fatal("storage slot1 not found") + } + got1 := common.BytesToHash(val1) + if got1 != slotVal1 { + t.Errorf("storage slot1: got %x, want %x", got1, slotVal1) + } + + treeKey2 := bintrie.GetBinaryTreeKeyStorageSlot(addr2, slotKey2[:]) + val2, err := bt2.GetWithHashedKey(treeKey2) + if err != nil { + t.Fatalf("failed to get storage slot2: %v", err) + } + if len(val2) == 0 { + t.Fatal("storage slot2 not found") + } + got2 := common.BytesToHash(val2) + if got2 != slotVal2 { + t.Errorf("storage slot2: got %x, want %x", got2, slotVal2) + } +} + +func TestBintrieConvertDeleteSource(t *testing.T) { + addr1 := common.HexToAddress("0x3333333333333333333333333333333333333333") + + chaindb := rawdb.NewMemoryDatabase() + + srcTriedb := triedb.NewDatabase(chaindb, &triedb.Config{ + Preimages: true, + PathDB: pathdb.Defaults, + }) + + gspec := &core.Genesis{ + Config: params.TestChainConfig, + BaseFee: big.NewInt(params.InitialBaseFee), + Alloc: types.GenesisAlloc{ + addr1: { + Balance: big.NewInt(1000000), + }, + }, + } + + genesisBlock := gspec.MustCommit(chaindb, srcTriedb) + root := genesisBlock.Root() + srcTriedb.Close() + + srcTriedb2 := triedb.NewDatabase(chaindb, &triedb.Config{ + Preimages: true, + PathDB: &pathdb.Config{ReadOnly: true}, + }) + + destTriedb := triedb.NewDatabase(chaindb, &triedb.Config{ + IsVerkle: true, + PathDB: pathdb.Defaults, + }) + + bt, err := bintrie.NewBinaryTrie(types.EmptyBinaryHash, destTriedb) + if err != nil { + t.Fatalf("failed to create binary trie: %v", err) + } + + newRoot, err := runConversionLoop(chaindb, srcTriedb2, destTriedb, bt, root, math.MaxUint64) + if err != nil { + t.Fatalf("conversion failed: %v", err) + } + + if err := deleteMPTData(chaindb, srcTriedb2, root); err != nil { + t.Fatalf("deletion failed: %v", err) + } + srcTriedb2.Close() + + bt2, err := bintrie.NewBinaryTrie(newRoot, destTriedb) + if err != nil { + t.Fatalf("failed to reload binary trie after deletion: %v", err) + } + + acc, err := bt2.GetAccount(addr1) + if err != nil { + t.Fatalf("failed to get account after deletion: %v", err) + } + if acc == nil { + t.Fatal("account not found after MPT deletion") + } + wantBal := uint256.NewInt(1000000) + if acc.Balance.Cmp(wantBal) != 0 { + t.Errorf("balance after deletion: got %s, want %s", acc.Balance, wantBal) + } + destTriedb.Close() +} diff --git a/cmd/geth/main.go b/cmd/geth/main.go index b72cbb9885..e196ac8688 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -260,6 +260,8 @@ func init() { utils.ShowDeprecated, // See snapshot.go snapshotCommand, + // See bintrie_convert.go + bintrieCommand, } if logTestCommand != nil { app.Commands = append(app.Commands, logTestCommand) From ea5448814f14a5e946522891e4d14399c1c888d3 Mon Sep 17 00:00:00 2001 From: cui Date: Fri, 10 Apr 2026 23:41:59 +0800 Subject: [PATCH 06/40] core/filtermaps: remove dead condition check (#34695) already check on line 40 before. --- core/filtermaps/math_test.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/core/filtermaps/math_test.go b/core/filtermaps/math_test.go index a4c1609059..0cd0046a7d 100644 --- a/core/filtermaps/math_test.go +++ b/core/filtermaps/math_test.go @@ -41,9 +41,7 @@ func TestSingleMatch(t *testing.T) { t.Fatalf("Invalid length of matches (got %d, expected 1)", len(matches)) } if matches[0] != lvIndex { - if len(matches) != 1 { - t.Fatalf("Incorrect match returned (got %d, expected %d)", matches[0], lvIndex) - } + t.Fatalf("Incorrect match returned (got %d, expected %d)", matches[0], lvIndex) } } } From f71a884e37857e6cbb093e2fc3e03b33bda85d6f Mon Sep 17 00:00:00 2001 From: CPerezz <37264926+CPerezz@users.noreply.github.com> Date: Fri, 10 Apr 2026 19:23:44 +0200 Subject: [PATCH 07/40] trie/bintrie: fix DeleteAccount no-op (#34676) `BinaryTrie.DeleteAccount` was a no-op, silently ignoring the caller's deletion request and leaving the old `BasicData` and `CodeHash` in the trie. Co-authored-by: Guillaume Ballet <3272758+gballet@users.noreply.github.com> --- trie/bintrie/trie.go | 26 ++- trie/bintrie/trie_test.go | 328 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 349 insertions(+), 5 deletions(-) diff --git a/trie/bintrie/trie.go b/trie/bintrie/trie.go index 6c29239a87..45f9edd46c 100644 --- a/trie/bintrie/trie.go +++ b/trie/bintrie/trie.go @@ -216,10 +216,12 @@ func (t *BinaryTrie) GetAccount(addr common.Address) (*types.StateAccount, error return nil, nil } - // If the account has been deleted, then values[10] will be 0 and not nil. If it has - // been recreated after that, then its code keccak will NOT be 0. So return `nil` if - // the nonce, and values[10], and code keccak is 0. - if bytes.Equal(values[BasicDataLeafKey], zero[:]) && len(values) > 10 && len(values[10]) > 0 && bytes.Equal(values[CodeHashLeafKey], zero[:]) { + // If the account has been deleted, BasicData and CodeHash will both be + // 32-byte zero blobs (not nil). If the account is recreated afterwards, + // UpdateAccount overwrites BasicData and CodeHash with non-zero values, + // so this branch won't activate.. + if bytes.Equal(values[BasicDataLeafKey], zero[:]) && + bytes.Equal(values[CodeHashLeafKey], zero[:]) { return nil, nil } @@ -294,8 +296,22 @@ func (t *BinaryTrie) UpdateStorage(address common.Address, key, value []byte) er return nil } -// DeleteAccount is a no-op as it is disabled in stateless. +// DeleteAccount erases an account by overwriting the account +// descriptors with 0s. func (t *BinaryTrie) DeleteAccount(addr common.Address) error { + var ( + values = make([][]byte, StemNodeWidth) + stem = GetBinaryTreeKey(addr, zero[:]) + ) + // Clear BasicData (nonce, balance, code size) and CodeHash. + values[BasicDataLeafKey] = zero[:] + values[CodeHashLeafKey] = zero[:] + + root, err := t.root.InsertValuesAtStem(stem, values, t.nodeResolver, 0) + if err != nil { + return fmt.Errorf("DeleteAccount (%x) error: %v", addr, err) + } + t.root = root return nil } diff --git a/trie/bintrie/trie_test.go b/trie/bintrie/trie_test.go index 256fd218e2..f420f53ef8 100644 --- a/trie/bintrie/trie_test.go +++ b/trie/bintrie/trie_test.go @@ -267,6 +267,334 @@ func TestStorageRoundTrip(t *testing.T) { } } +// newEmptyTestTrie creates a fresh BinaryTrie with an empty root and a +// default prevalue tracer. Use this for tests that populate the trie +// incrementally via Update*; for tests that want a pre-populated trie with +// a fixed entry set, use makeTrie (in iterator_test.go) instead. +func newEmptyTestTrie(t *testing.T) *BinaryTrie { + t.Helper() + return &BinaryTrie{ + root: NewBinaryNode(), + tracer: trie.NewPrevalueTracer(), + } +} + +// makeAccount constructs a StateAccount with the given fields. The Root is +// zeroed out because the bintrie has no per-account storage root. +func makeAccount(nonce uint64, balance uint64, codeHash common.Hash) *types.StateAccount { + return &types.StateAccount{ + Nonce: nonce, + Balance: uint256.NewInt(balance), + CodeHash: codeHash.Bytes(), + } +} + +// TestDeleteAccountRoundTrip verifies the basic delete path: create an +// account, read it back, delete it, confirm subsequent reads return nil. +// Regression test for the no-op DeleteAccount bug where the deletion was +// silently ignored and the old values remained in the trie. +func TestDeleteAccountRoundTrip(t *testing.T) { + tr := newEmptyTestTrie(t) + addr := common.HexToAddress("0x1234567890abcdef1234567890abcdef12345678") + codeHash := common.HexToHash("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470") + + // Create: write account, verify round-trip. + acc := makeAccount(42, 1000, codeHash) + if err := tr.UpdateAccount(addr, acc, 0); err != nil { + t.Fatalf("UpdateAccount: %v", err) + } + got, err := tr.GetAccount(addr) + if err != nil { + t.Fatalf("GetAccount: %v", err) + } + if got == nil { + t.Fatal("GetAccount returned nil after UpdateAccount") + } + if got.Nonce != 42 { + t.Fatalf("Nonce: got %d, want 42", got.Nonce) + } + if got.Balance.Uint64() != 1000 { + t.Fatalf("Balance: got %s, want 1000", got.Balance) + } + if !bytes.Equal(got.CodeHash, codeHash[:]) { + t.Fatalf("CodeHash: got %x, want %x", got.CodeHash, codeHash) + } + + // Delete: verify GetAccount returns nil afterwards. + if err := tr.DeleteAccount(addr); err != nil { + t.Fatalf("DeleteAccount: %v", err) + } + got, err = tr.GetAccount(addr) + if err != nil { + t.Fatalf("GetAccount after delete: %v", err) + } + if got != nil { + t.Fatalf("GetAccount after delete: got %+v, want nil", got) + } +} + +// TestDeleteAccountOnMissingAccount verifies that deleting an account that +// was never created does not error and subsequent reads still return nil. +func TestDeleteAccountOnMissingAccount(t *testing.T) { + tr := newEmptyTestTrie(t) + addr := common.HexToAddress("0x1234567890abcdef1234567890abcdef12345678") + + // Delete without any prior create. Should not panic or error on an + // empty root, and GetAccount should still return nil. + if err := tr.DeleteAccount(addr); err != nil { + t.Fatalf("DeleteAccount on empty trie: %v", err) + } + got, err := tr.GetAccount(addr) + if err != nil { + t.Fatalf("GetAccount after delete on empty trie: %v", err) + } + if got != nil { + t.Fatalf("GetAccount on deleted missing account: got %+v, want nil", got) + } +} + +// TestDeleteAccountPreservesOtherAccounts verifies that deleting one account +// does not affect accounts at different stems. +func TestDeleteAccountPreservesOtherAccounts(t *testing.T) { + tr := newEmptyTestTrie(t) + addrA := common.HexToAddress("0x1234567890abcdef1234567890abcdef12345678") + addrB := common.HexToAddress("0xabcdef1234567890abcdef1234567890abcdef12") + codeHashA := common.HexToHash("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470") + codeHashB := common.HexToHash("f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff0102030405060708090a0b0c0d0e0f10") + + // Create two distinct accounts. + if err := tr.UpdateAccount(addrA, makeAccount(1, 100, codeHashA), 0); err != nil { + t.Fatalf("UpdateAccount(A): %v", err) + } + if err := tr.UpdateAccount(addrB, makeAccount(2, 200, codeHashB), 0); err != nil { + t.Fatalf("UpdateAccount(B): %v", err) + } + + // Delete A. + if err := tr.DeleteAccount(addrA); err != nil { + t.Fatalf("DeleteAccount(A): %v", err) + } + + // A should be gone. + if got, err := tr.GetAccount(addrA); err != nil { + t.Fatalf("GetAccount(A): %v", err) + } else if got != nil { + t.Fatalf("GetAccount(A) after delete: got %+v, want nil", got) + } + + // B should still be readable with its original values. + got, err := tr.GetAccount(addrB) + if err != nil { + t.Fatalf("GetAccount(B): %v", err) + } + if got == nil { + t.Fatal("GetAccount(B) returned nil after unrelated delete") + } + if got.Nonce != 2 { + t.Fatalf("Account B Nonce: got %d, want 2", got.Nonce) + } + if got.Balance.Uint64() != 200 { + t.Fatalf("Account B Balance: got %s, want 200", got.Balance) + } + if !bytes.Equal(got.CodeHash, codeHashB[:]) { + t.Fatalf("Account B CodeHash: got %x, want %x", got.CodeHash, codeHashB) + } +} + +// TestDeleteAccountThenRecreate verifies that an account can be deleted and +// then recreated with different values; the second read must return the new +// values, not the stale ones from before deletion. +func TestDeleteAccountThenRecreate(t *testing.T) { + tr := newEmptyTestTrie(t) + addr := common.HexToAddress("0x1234567890abcdef1234567890abcdef12345678") + codeHash1 := common.HexToHash("1111111111111111111111111111111111111111111111111111111111111111") + codeHash2 := common.HexToHash("2222222222222222222222222222222222222222222222222222222222222222") + + // Create. + if err := tr.UpdateAccount(addr, makeAccount(1, 100, codeHash1), 0); err != nil { + t.Fatalf("UpdateAccount #1: %v", err) + } + // Delete. + if err := tr.DeleteAccount(addr); err != nil { + t.Fatalf("DeleteAccount: %v", err) + } + // Recreate with new values. + if err := tr.UpdateAccount(addr, makeAccount(7, 9999, codeHash2), 0); err != nil { + t.Fatalf("UpdateAccount #2: %v", err) + } + // Read: must observe the new values, not the originals. + got, err := tr.GetAccount(addr) + if err != nil { + t.Fatalf("GetAccount: %v", err) + } + if got == nil { + t.Fatal("GetAccount returned nil after recreate") + } + if got.Nonce != 7 { + t.Fatalf("Nonce: got %d, want 7", got.Nonce) + } + if got.Balance.Uint64() != 9999 { + t.Fatalf("Balance: got %s, want 9999", got.Balance) + } + if !bytes.Equal(got.CodeHash, codeHash2[:]) { + t.Fatalf("CodeHash: got %x, want %x", got.CodeHash, codeHash2) + } +} + +// TestDeleteAccountDoesNotAffectMainStorage verifies that DeleteAccount only +// clears the account's BasicData and CodeHash, leaving main storage slots +// untouched. Main storage slots live at different stems entirely (their +// keys route through the non-header branch in GetBinaryTreeKeyStorageSlot), +// so this test exercises the inter-stem isolation. Header-range storage +// slots share the same stem and are covered separately by +// TestDeleteAccountPreservesHeaderStorage. +// +// Wiping storage on self-destruct is a separate concern handled at the +// StateDB level. +func TestDeleteAccountDoesNotAffectMainStorage(t *testing.T) { + tr := newEmptyTestTrie(t) + addr := common.HexToAddress("0x1234567890abcdef1234567890abcdef12345678") + codeHash := common.HexToHash("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470") + + // Create account. + if err := tr.UpdateAccount(addr, makeAccount(1, 100, codeHash), 0); err != nil { + t.Fatalf("UpdateAccount: %v", err) + } + // Write a main storage slot — i.e. key[31] >= 64 or key[:31] != 0 — so + // it lives at a different stem from the account header. + slot := common.HexToHash("0000000000000000000000000000000000000000000000000000000000000080") + value := common.TrimLeftZeroes(common.HexToHash("00000000000000000000000000000000000000000000000000000000deadbeef").Bytes()) + if err := tr.UpdateStorage(addr, slot[:], value); err != nil { + t.Fatalf("UpdateStorage: %v", err) + } + + // Delete the account. + if err := tr.DeleteAccount(addr); err != nil { + t.Fatalf("DeleteAccount: %v", err) + } + + // Account should be absent. + got, err := tr.GetAccount(addr) + if err != nil { + t.Fatalf("GetAccount after delete: %v", err) + } + if got != nil { + t.Fatalf("GetAccount after delete: got %+v, want nil", got) + } + + // Main storage slot should still be readable — DeleteAccount must not + // have touched it. + stored, err := tr.GetStorage(addr, slot[:]) + if err != nil { + t.Fatalf("GetStorage after DeleteAccount: %v", err) + } + if len(stored) == 0 { + t.Fatal("main storage slot was wiped by DeleteAccount, expected it to survive") + } + var expected [HashSize]byte + copy(expected[HashSize-len(value):], value) + if !bytes.Equal(stored, expected[:]) { + t.Fatalf("main storage slot: got %x, want %x", stored, expected) + } +} + +// TestDeleteAccountPreservesHeaderStorage verifies that DeleteAccount does +// not clobber header-range storage slots (key[31] < 64), which live at the +// SAME stem as BasicData/CodeHash but at offsets 64-127. The safety here +// relies on StemNode.InsertValuesAtStem treating nil entries in the values +// slice as "do not overwrite"; this test pins that invariant so a future +// change cannot silently corrupt slots 0-63 of any contract. +func TestDeleteAccountPreservesHeaderStorage(t *testing.T) { + tr := newEmptyTestTrie(t) + addr := common.HexToAddress("0x1234567890abcdef1234567890abcdef12345678") + codeHash := common.HexToHash("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470") + + // Create account. + if err := tr.UpdateAccount(addr, makeAccount(1, 100, codeHash), 0); err != nil { + t.Fatalf("UpdateAccount: %v", err) + } + + // Create a second, unrelated account so the root promotes from StemNode + // to InternalNode. BinaryTrie.GetStorage walks via root.Get, which is + // only implemented on InternalNode/Empty — calling it with a StemNode + // root panics. The existing main-storage test gets away with this because + // the main-storage slot lands on a separate stem and forces the same + // promotion implicitly; here we want a same-stem header slot, so the + // promotion has to come from a second account. + other := common.HexToAddress("0xabcdef1234567890abcdef1234567890abcdef12") + if err := tr.UpdateAccount(other, makeAccount(0, 0, common.Hash{}), 0); err != nil { + t.Fatalf("UpdateAccount(other): %v", err) + } + + // Write a header-range storage slot — key[:31] == 0 and key[31] < 64 + // — which routes through the header branch in GetBinaryTreeKeyStorageSlot + // and lands on the same stem as BasicData/CodeHash. + var slot [HashSize]byte + slot[31] = 5 + value := []byte{0xde, 0xad, 0xbe, 0xef} + if err := tr.UpdateStorage(addr, slot[:], value); err != nil { + t.Fatalf("UpdateStorage: %v", err) + } + + // Delete the account. + if err := tr.DeleteAccount(addr); err != nil { + t.Fatalf("DeleteAccount: %v", err) + } + + // Account metadata should be gone. + got, err := tr.GetAccount(addr) + if err != nil { + t.Fatalf("GetAccount after delete: %v", err) + } + if got != nil { + t.Fatalf("GetAccount after delete: got %+v, want nil", got) + } + + // Header storage slot must survive — DeleteAccount only writes offsets + // BasicDataLeafKey, CodeHashLeafKey, and accountDeletedMarkerKey, leaving + // the header-storage offsets (64-127) untouched. + stored, err := tr.GetStorage(addr, slot[:]) + if err != nil { + t.Fatalf("GetStorage after DeleteAccount: %v", err) + } + if len(stored) == 0 { + t.Fatal("header storage slot was wiped by DeleteAccount, expected it to survive") + } + var expected [HashSize]byte + copy(expected[HashSize-len(value):], value) + if !bytes.Equal(stored, expected[:]) { + t.Fatalf("header storage slot: got %x, want %x", stored, expected) + } +} + +func TestDeleteAccountHashIsDeterministic(t *testing.T) { + addr := common.HexToAddress("0x1234567890abcdef1234567890abcdef12345678") + codeHash := common.HexToHash("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470") + acc := makeAccount(42, 1000, codeHash) + + run := func() common.Hash { + tr := newEmptyTestTrie(t) + if err := tr.UpdateAccount(addr, acc, 0); err != nil { + t.Fatalf("UpdateAccount: %v", err) + } + if err := tr.DeleteAccount(addr); err != nil { + t.Fatalf("DeleteAccount: %v", err) + } + return tr.Hash() + } + + first := run() + second := run() + if first != second { + t.Fatalf("non-deterministic root after Update+Delete: first=%x second=%x", first, second) + } + + empty := newEmptyTestTrie(t).Hash() + if first == empty { + t.Fatalf("post-delete root unexpectedly equals empty-trie root %x", empty) + } +} + func TestBinaryTrieWitness(t *testing.T) { tracer := trie.NewPrevalueTracer() From deda47f6a1c0588b9e2600ca97552994c2889d37 Mon Sep 17 00:00:00 2001 From: CPerezz <37264926+CPerezz@users.noreply.github.com> Date: Fri, 10 Apr 2026 19:43:48 +0200 Subject: [PATCH 08/40] =?UTF-8?q?trie/bintrie:=20fix=20GetAccount/GetStora?= =?UTF-8?q?ge=20non-membership=20=E2=80=94=20verify=20stem=20before=20retu?= =?UTF-8?q?rning=20values=20(#34690)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix `GetAccount` returning **wrong account data** for non-existent addresses when the trie root is a `StemNode` (single-account trie) — the `StemNode` branch returned `r.Values` without verifying the queried address's stem matches. Co-authored-by: Guillaume Ballet <3272758+gballet@users.noreply.github.com> --- trie/bintrie/stem_node.go | 5 +- trie/bintrie/stem_node_test.go | 44 +++++++++ trie/bintrie/trie.go | 2 +- trie/bintrie/trie_test.go | 159 +++++++++++++++++++++++++++++++++ 4 files changed, 208 insertions(+), 2 deletions(-) diff --git a/trie/bintrie/stem_node.go b/trie/bintrie/stem_node.go index 3f69261d62..e5729e6182 100644 --- a/trie/bintrie/stem_node.go +++ b/trie/bintrie/stem_node.go @@ -37,7 +37,10 @@ type StemNode struct { // Get retrieves the value for the given key. func (bt *StemNode) Get(key []byte, _ NodeResolverFn) ([]byte, error) { - panic("this should not be called directly") + if !bytes.Equal(bt.Stem, key[:StemSize]) { + return nil, nil + } + return bt.Values[key[StemSize]], nil } // Insert inserts a new key-value pair into the node. diff --git a/trie/bintrie/stem_node_test.go b/trie/bintrie/stem_node_test.go index 92c1b49e02..310c553d39 100644 --- a/trie/bintrie/stem_node_test.go +++ b/trie/bintrie/stem_node_test.go @@ -23,6 +23,50 @@ import ( "github.com/ethereum/go-ethereum/common" ) +// TestStemNodeGet tests the Get method for matching stem, non-matching stem, +// and nil-value suffix scenarios. +func TestStemNodeGet(t *testing.T) { + stem := make([]byte, StemSize) + stem[0] = 0xAB + var values [StemNodeWidth][]byte + values[5] = common.HexToHash("0xdeadbeef").Bytes() + + node := &StemNode{Stem: stem, Values: values[:], depth: 0} + + // Matching stem, populated suffix → returns value. + key := make([]byte, HashSize) + copy(key[:StemSize], stem) + key[StemSize] = 5 + got, err := node.Get(key, nil) + if err != nil { + t.Fatalf("Get error: %v", err) + } + if !bytes.Equal(got, values[5]) { + t.Fatalf("Get = %x, want %x", got, values[5]) + } + + // Matching stem, empty suffix → returns nil (slot not set). + key[StemSize] = 99 + got, err = node.Get(key, nil) + if err != nil { + t.Fatalf("Get error: %v", err) + } + if got != nil { + t.Fatalf("Get(empty suffix) = %x, want nil", got) + } + + // Non-matching stem → returns nil, nil. + otherKey := make([]byte, HashSize) + otherKey[0] = 0xFF + got, err = node.Get(otherKey, nil) + if err != nil { + t.Fatalf("Get error: %v", err) + } + if got != nil { + t.Fatalf("Get(wrong stem) = %x, want nil", got) + } +} + // TestStemNodeInsertSameStem tests inserting values with the same stem func TestStemNodeInsertSameStem(t *testing.T) { stem := make([]byte, 31) diff --git a/trie/bintrie/trie.go b/trie/bintrie/trie.go index 45f9edd46c..b1e3c991c0 100644 --- a/trie/bintrie/trie.go +++ b/trie/bintrie/trie.go @@ -191,7 +191,7 @@ func (t *BinaryTrie) GetAccount(addr common.Address) (*types.StateAccount, error case *InternalNode: values, err = r.GetValuesAtStem(key[:StemSize], t.nodeResolver) case *StemNode: - values = r.Values + values, err = r.GetValuesAtStem(key[:StemSize], t.nodeResolver) case Empty: return nil, nil default: diff --git a/trie/bintrie/trie_test.go b/trie/bintrie/trie_test.go index f420f53ef8..5b104ddde4 100644 --- a/trie/bintrie/trie_test.go +++ b/trie/bintrie/trie_test.go @@ -620,3 +620,162 @@ func TestBinaryTrieWitness(t *testing.T) { t.Fatal("unexpected witness value for path2") } } + +// testAccount is a helper that creates a BinaryTrie with a tracer and +// inserts a single account, returning the trie. +func testAccount(t *testing.T, addr common.Address, nonce uint64, balance uint64) *BinaryTrie { + t.Helper() + tr := &BinaryTrie{ + root: NewBinaryNode(), + tracer: trie.NewPrevalueTracer(), + } + acc := &types.StateAccount{ + Nonce: nonce, + Balance: uint256.NewInt(balance), + CodeHash: types.EmptyCodeHash[:], + } + if err := tr.UpdateAccount(addr, acc, 0); err != nil { + t.Fatalf("UpdateAccount error: %v", err) + } + return tr +} + +// TestGetAccountNonMembershipStemRoot verifies that querying a non-existent +// address returns nil when the trie root is a StemNode (single-account trie). +// This is a regression test: previously the StemNode branch in GetAccount +// returned the root's values without verifying the stem. +func TestGetAccountNonMembershipStemRoot(t *testing.T) { + addr := common.HexToAddress("0x1111111111111111111111111111111111111111") + tr := testAccount(t, addr, 42, 100) + + // Verify root is a StemNode (single stem inserted). + if _, ok := tr.root.(*StemNode); !ok { + t.Fatalf("expected StemNode root, got %T", tr.root) + } + + // Query a completely different address — must return nil. + other := common.HexToAddress("0x2222222222222222222222222222222222222222") + got, err := tr.GetAccount(other) + if err != nil { + t.Fatalf("GetAccount error: %v", err) + } + if got != nil { + t.Fatalf("expected nil for non-existent account, got nonce=%d balance=%s", got.Nonce, got.Balance) + } + + // Original account must still be retrievable. + got, err = tr.GetAccount(addr) + if err != nil { + t.Fatalf("GetAccount(original) error: %v", err) + } + if got == nil { + t.Fatal("expected original account, got nil") + } + if got.Nonce != 42 { + t.Fatalf("expected nonce=42, got %d", got.Nonce) + } +} + +// TestGetAccountNonMembershipInternalRoot verifies that querying a non-existent +// address returns nil when the trie root is an InternalNode (multi-account trie). +func TestGetAccountNonMembershipInternalRoot(t *testing.T) { + tr := &BinaryTrie{ + root: NewBinaryNode(), + tracer: trie.NewPrevalueTracer(), + } + + // Insert two accounts whose binary tree keys have different first bits + // so the root splits into an InternalNode. + addr1 := common.HexToAddress("0x1111111111111111111111111111111111111111") + addr2 := common.HexToAddress("0x9999999999999999999999999999999999999999") + for _, addr := range []common.Address{addr1, addr2} { + acc := &types.StateAccount{ + Nonce: 1, + Balance: uint256.NewInt(1), + CodeHash: types.EmptyCodeHash[:], + } + if err := tr.UpdateAccount(addr, acc, 0); err != nil { + t.Fatalf("UpdateAccount error: %v", err) + } + } + + // Verify root is an InternalNode. + if _, ok := tr.root.(*InternalNode); !ok { + t.Fatalf("expected InternalNode root, got %T", tr.root) + } + + // Query a non-existent address — must return nil. + other := common.HexToAddress("0x5555555555555555555555555555555555555555") + got, err := tr.GetAccount(other) + if err != nil { + t.Fatalf("GetAccount error: %v", err) + } + if got != nil { + t.Fatalf("expected nil for non-existent account, got nonce=%d", got.Nonce) + } +} + +// TestGetStorageNonMembershipStemRoot verifies that querying storage for a +// non-existent address returns nil when the root is a StemNode. This is a +// regression test: previously StemNode.Get panicked unconditionally. +func TestGetStorageNonMembershipStemRoot(t *testing.T) { + addr := common.HexToAddress("0x1111111111111111111111111111111111111111") + tr := testAccount(t, addr, 1, 100) + + // Verify root is a StemNode. + if _, ok := tr.root.(*StemNode); !ok { + t.Fatalf("expected StemNode root, got %T", tr.root) + } + + // Query storage for a different address — must return nil, not panic. + other := common.HexToAddress("0x2222222222222222222222222222222222222222") + slot := common.HexToHash("0x01") + got, err := tr.GetStorage(other, slot[:]) + if err != nil { + t.Fatalf("GetStorage error: %v", err) + } + if len(got) > 0 && !bytes.Equal(got, zero[:]) { + t.Fatalf("expected nil/zero for non-existent storage, got %x", got) + } +} + +// TestGetStorageNonMembershipInternalRoot verifies that querying storage for a +// non-existent address returns nil when the root is an InternalNode. +func TestGetStorageNonMembershipInternalRoot(t *testing.T) { + tr := &BinaryTrie{ + root: NewBinaryNode(), + tracer: trie.NewPrevalueTracer(), + } + + addr := common.HexToAddress("0x1234567890abcdef1234567890abcdef12345678") + acc := &types.StateAccount{ + Nonce: 1, + Balance: uint256.NewInt(1000), + CodeHash: types.EmptyCodeHash[:], + } + if err := tr.UpdateAccount(addr, acc, 0); err != nil { + t.Fatalf("UpdateAccount error: %v", err) + } + + // Add a storage slot so the root becomes an InternalNode (storage + // slots use a different stem than account data). + slot := common.HexToHash("0xFF") + val := common.TrimLeftZeroes(common.HexToHash("0xdeadbeef").Bytes()) + if err := tr.UpdateStorage(addr, slot[:], val); err != nil { + t.Fatalf("UpdateStorage error: %v", err) + } + + if _, ok := tr.root.(*InternalNode); !ok { + t.Fatalf("expected InternalNode root, got %T", tr.root) + } + + // Query storage for a non-existent address — must return nil. + other := common.HexToAddress("0x9999999999999999999999999999999999999999") + got, err := tr.GetStorage(other, slot[:]) + if err != nil { + t.Fatalf("GetStorage error: %v", err) + } + if len(got) > 0 && !bytes.Equal(got, zero[:]) { + t.Fatalf("expected nil/zero for non-existent storage, got %x", got) + } +} From 6333855163d0ac99240f966df875be64c0885983 Mon Sep 17 00:00:00 2001 From: Marius van der Wijden Date: Mon, 13 Apr 2026 08:09:42 +0200 Subject: [PATCH 09/40] core: turn gas into a vector (#34691) Pre-refactor PR to get 8037 upstreamed in chunks --------- Co-authored-by: Gary Rong --- core/vm/contract.go | 14 +-- core/vm/eips.go | 6 +- core/vm/evm.go | 14 +-- core/vm/gas_table.go | 190 ++++++++++++++++++---------------- core/vm/gascosts.go | 36 +++++++ core/vm/instructions.go | 6 +- core/vm/instructions_test.go | 2 +- core/vm/interpreter.go | 16 +-- core/vm/jump_table.go | 2 +- core/vm/operations_acl.go | 98 +++++++++--------- core/vm/operations_verkle.go | 115 ++++++++++---------- eth/tracers/js/tracer_test.go | 4 +- 12 files changed, 278 insertions(+), 225 deletions(-) create mode 100644 core/vm/gascosts.go diff --git a/core/vm/contract.go b/core/vm/contract.go index 165ca833f8..3b5695e21a 100644 --- a/core/vm/contract.go +++ b/core/vm/contract.go @@ -42,7 +42,7 @@ type Contract struct { IsDeployment bool IsSystemCall bool - Gas uint64 + Gas GasCosts value *uint256.Int } @@ -56,7 +56,7 @@ func NewContract(caller common.Address, address common.Address, value *uint256.I caller: caller, address: address, jumpDests: jumpDests, - Gas: gas, + Gas: GasCosts{RegularGas: gas}, value: value, } } @@ -127,13 +127,13 @@ func (c *Contract) Caller() common.Address { // UseGas attempts the use gas and subtracts it and returns true on success func (c *Contract) UseGas(gas uint64, logger *tracing.Hooks, reason tracing.GasChangeReason) (ok bool) { - if c.Gas < gas { + if c.Gas.RegularGas < gas { return false } if logger != nil && logger.OnGasChange != nil && reason != tracing.GasChangeIgnored { - logger.OnGasChange(c.Gas, c.Gas-gas, reason) + logger.OnGasChange(c.Gas.RegularGas, c.Gas.RegularGas-gas, reason) } - c.Gas -= gas + c.Gas.RegularGas -= gas return true } @@ -143,9 +143,9 @@ func (c *Contract) RefundGas(gas uint64, logger *tracing.Hooks, reason tracing.G return } if logger != nil && logger.OnGasChange != nil && reason != tracing.GasChangeIgnored { - logger.OnGasChange(c.Gas, c.Gas+gas, reason) + logger.OnGasChange(c.Gas.RegularGas, c.Gas.RegularGas+gas, reason) } - c.Gas += gas + c.Gas.RegularGas += gas } // Address returns the contracts address diff --git a/core/vm/eips.go b/core/vm/eips.go index 3ccd9aaaf0..8f4ca3ae41 100644 --- a/core/vm/eips.go +++ b/core/vm/eips.go @@ -381,7 +381,7 @@ func opExtCodeCopyEIP4762(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, er addr := common.Address(a.Bytes20()) code := evm.StateDB.GetCode(addr) paddedCodeCopy, copyOffset, nonPaddedCopyLength := getDataAndAdjustedBounds(code, uint64CodeOffset, length.Uint64()) - consumed, wanted := evm.AccessEvents.CodeChunksRangeGas(addr, copyOffset, nonPaddedCopyLength, uint64(len(code)), false, scope.Contract.Gas) + consumed, wanted := evm.AccessEvents.CodeChunksRangeGas(addr, copyOffset, nonPaddedCopyLength, uint64(len(code)), false, scope.Contract.Gas.RegularGas) scope.Contract.UseGas(consumed, evm.Config.Tracer, tracing.GasChangeUnspecified) if consumed < wanted { return nil, ErrOutOfGas @@ -407,7 +407,7 @@ func opPush1EIP4762(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { // touch next chunk if PUSH1 is at the boundary. if so, *pc has // advanced past this boundary. contractAddr := scope.Contract.Address() - consumed, wanted := evm.AccessEvents.CodeChunksRangeGas(contractAddr, *pc+1, uint64(1), uint64(len(scope.Contract.Code)), false, scope.Contract.Gas) + consumed, wanted := evm.AccessEvents.CodeChunksRangeGas(contractAddr, *pc+1, uint64(1), uint64(len(scope.Contract.Code)), false, scope.Contract.Gas.RegularGas) scope.Contract.UseGas(wanted, evm.Config.Tracer, tracing.GasChangeUnspecified) if consumed < wanted { return nil, ErrOutOfGas @@ -435,7 +435,7 @@ func makePushEIP4762(size uint64, pushByteSize int) executionFunc { if !scope.Contract.IsDeployment && !scope.Contract.IsSystemCall { contractAddr := scope.Contract.Address() - consumed, wanted := evm.AccessEvents.CodeChunksRangeGas(contractAddr, uint64(start), uint64(pushByteSize), uint64(len(scope.Contract.Code)), false, scope.Contract.Gas) + consumed, wanted := evm.AccessEvents.CodeChunksRangeGas(contractAddr, uint64(start), uint64(pushByteSize), uint64(len(scope.Contract.Code)), false, scope.Contract.Gas.RegularGas) scope.Contract.UseGas(consumed, evm.Config.Tracer, tracing.GasChangeUnspecified) if consumed < wanted { return nil, ErrOutOfGas diff --git a/core/vm/evm.go b/core/vm/evm.go index 36494de2a8..4df2627486 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -303,7 +303,7 @@ func (evm *EVM) Call(caller common.Address, addr common.Address, input []byte, g contract.IsSystemCall = isSystemCall(caller) contract.SetCallCode(evm.resolveCodeHash(addr), code) ret, err = evm.Run(contract, input, false) - gas = contract.Gas + gas = contract.Gas.RegularGas } } // When an error was returned by the EVM or when setting the creation code @@ -365,7 +365,7 @@ func (evm *EVM) CallCode(caller common.Address, addr common.Address, input []byt contract := NewContract(caller, caller, value, gas, evm.jumpDests) contract.SetCallCode(evm.resolveCodeHash(addr), evm.resolveCode(addr)) ret, err = evm.Run(contract, input, false) - gas = contract.Gas + gas = contract.Gas.RegularGas } if err != nil { evm.StateDB.RevertToSnapshot(snapshot) @@ -413,7 +413,7 @@ func (evm *EVM) DelegateCall(originCaller common.Address, caller common.Address, contract := NewContract(originCaller, caller, value, gas, evm.jumpDests) contract.SetCallCode(evm.resolveCodeHash(addr), evm.resolveCode(addr)) ret, err = evm.Run(contract, input, false) - gas = contract.Gas + gas = contract.Gas.RegularGas } if err != nil { evm.StateDB.RevertToSnapshot(snapshot) @@ -472,7 +472,7 @@ func (evm *EVM) StaticCall(caller common.Address, addr common.Address, input []b // above we revert to the snapshot and consume any gas remaining. Additionally // when we're in Homestead this also counts for code storage gas errors. ret, err = evm.Run(contract, input, true) - gas = contract.Gas + gas = contract.Gas.RegularGas } if err != nil { evm.StateDB.RevertToSnapshot(snapshot) @@ -583,10 +583,10 @@ func (evm *EVM) create(caller common.Address, code []byte, gas uint64, value *ui if err != nil && (evm.chainRules.IsHomestead || err != ErrCodeStoreOutOfGas) { evm.StateDB.RevertToSnapshot(snapshot) if err != ErrExecutionReverted { - contract.UseGas(contract.Gas, evm.Config.Tracer, tracing.GasChangeCallFailedExecution) + contract.UseGas(contract.Gas.RegularGas, evm.Config.Tracer, tracing.GasChangeCallFailedExecution) } } - return ret, address, contract.Gas, err + return ret, address, contract.Gas.RegularGas, err } // initNewContract runs a new contract's creation code, performs checks on the @@ -613,7 +613,7 @@ func (evm *EVM) initNewContract(contract *Contract, address common.Address) ([]b return ret, ErrCodeStoreOutOfGas } } else { - consumed, wanted := evm.AccessEvents.CodeChunksRangeGas(address, 0, uint64(len(ret)), uint64(len(ret)), true, contract.Gas) + consumed, wanted := evm.AccessEvents.CodeChunksRangeGas(address, 0, uint64(len(ret)), uint64(len(ret)), true, contract.Gas.RegularGas) contract.UseGas(consumed, evm.Config.Tracer, tracing.GasChangeWitnessCodeChunk) if len(ret) > 0 && (consumed < wanted) { return ret, ErrCodeStoreOutOfGas diff --git a/core/vm/gas_table.go b/core/vm/gas_table.go index f075a99468..b3259b2ec7 100644 --- a/core/vm/gas_table.go +++ b/core/vm/gas_table.go @@ -64,26 +64,26 @@ func memoryGasCost(mem *Memory, newMemSize uint64) (uint64, error) { // EXTCODECOPY (stack position 3) // RETURNDATACOPY (stack position 2) func memoryCopierGas(stackpos int) gasFunc { - return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { + return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { // Gas for expanding the memory gas, err := memoryGasCost(mem, memorySize) if err != nil { - return 0, err + return GasCosts{}, err } // And gas for copying data, charged per word at param.CopyGas words, overflow := stack.Back(stackpos).Uint64WithOverflow() if overflow { - return 0, ErrGasUintOverflow + return GasCosts{}, ErrGasUintOverflow } if words, overflow = math.SafeMul(toWordSize(words), params.CopyGas); overflow { - return 0, ErrGasUintOverflow + return GasCosts{}, ErrGasUintOverflow } if gas, overflow = math.SafeAdd(gas, words); overflow { - return 0, ErrGasUintOverflow + return GasCosts{}, ErrGasUintOverflow } - return gas, nil + return GasCosts{RegularGas: gas}, nil } } @@ -95,9 +95,9 @@ var ( gasReturnDataCopy = memoryCopierGas(2) ) -func gasSStore(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { +func gasSStore(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { if evm.readOnly { - return 0, ErrWriteProtection + return GasCosts{}, ErrWriteProtection } var ( y, x = stack.Back(1), stack.Back(0) @@ -114,12 +114,12 @@ func gasSStore(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySi // 3. From a non-zero to a non-zero (CHANGE) switch { case current == (common.Hash{}) && y.Sign() != 0: // 0 => non 0 - return params.SstoreSetGas, nil + return GasCosts{RegularGas: params.SstoreSetGas}, nil case current != (common.Hash{}) && y.Sign() == 0: // non 0 => 0 evm.StateDB.AddRefund(params.SstoreRefundGas) - return params.SstoreClearGas, nil + return GasCosts{RegularGas: params.SstoreClearGas}, nil default: // non 0 => non 0 (or 0 => 0) - return params.SstoreResetGas, nil + return GasCosts{RegularGas: params.SstoreResetGas}, nil } } @@ -139,16 +139,16 @@ func gasSStore(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySi // (2.2.2.2.) Otherwise, add 4800 gas to refund counter. value := common.Hash(y.Bytes32()) if current == value { // noop (1) - return params.NetSstoreNoopGas, nil + return GasCosts{RegularGas: params.NetSstoreNoopGas}, nil } if original == current { if original == (common.Hash{}) { // create slot (2.1.1) - return params.NetSstoreInitGas, nil + return GasCosts{RegularGas: params.NetSstoreInitGas}, nil } if value == (common.Hash{}) { // delete slot (2.1.2b) evm.StateDB.AddRefund(params.NetSstoreClearRefund) } - return params.NetSstoreCleanGas, nil // write existing slot (2.1.2) + return GasCosts{RegularGas: params.NetSstoreCleanGas}, nil // write existing slot (2.1.2) } if original != (common.Hash{}) { if current == (common.Hash{}) { // recreate slot (2.2.1.1) @@ -164,7 +164,7 @@ func gasSStore(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySi evm.StateDB.AddRefund(params.NetSstoreResetRefund) } } - return params.NetSstoreDirtyGas, nil + return GasCosts{RegularGas: params.NetSstoreDirtyGas}, nil } // Here come the EIP2200 rules: @@ -182,13 +182,13 @@ func gasSStore(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySi // (2.2.2.) If original value equals new value (this storage slot is reset): // (2.2.2.1.) If original value is 0, add SSTORE_SET_GAS - SLOAD_GAS to refund counter. // (2.2.2.2.) Otherwise, add SSTORE_RESET_GAS - SLOAD_GAS gas to refund counter. -func gasSStoreEIP2200(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { +func gasSStoreEIP2200(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { if evm.readOnly { - return 0, ErrWriteProtection + return GasCosts{}, ErrWriteProtection } // If we fail the minimum gas availability invariant, fail (0) - if contract.Gas <= params.SstoreSentryGasEIP2200 { - return 0, errors.New("not enough gas for reentrancy sentry") + if contract.Gas.RegularGas <= params.SstoreSentryGasEIP2200 { + return GasCosts{}, errors.New("not enough gas for reentrancy sentry") } // Gas sentry honoured, do the actual gas calculation based on the stored value var ( @@ -198,16 +198,16 @@ func gasSStoreEIP2200(evm *EVM, contract *Contract, stack *Stack, mem *Memory, m value := common.Hash(y.Bytes32()) if current == value { // noop (1) - return params.SloadGasEIP2200, nil + return GasCosts{RegularGas: params.SloadGasEIP2200}, nil } if original == current { if original == (common.Hash{}) { // create slot (2.1.1) - return params.SstoreSetGasEIP2200, nil + return GasCosts{RegularGas: params.SstoreSetGasEIP2200}, nil } if value == (common.Hash{}) { // delete slot (2.1.2b) evm.StateDB.AddRefund(params.SstoreClearsScheduleRefundEIP2200) } - return params.SstoreResetGasEIP2200, nil // write existing slot (2.1.2) + return GasCosts{RegularGas: params.SstoreResetGasEIP2200}, nil // write existing slot (2.1.2) } if original != (common.Hash{}) { if current == (common.Hash{}) { // recreate slot (2.2.1.1) @@ -223,62 +223,66 @@ func gasSStoreEIP2200(evm *EVM, contract *Contract, stack *Stack, mem *Memory, m evm.StateDB.AddRefund(params.SstoreResetGasEIP2200 - params.SloadGasEIP2200) } } - return params.SloadGasEIP2200, nil // dirty update (2.2) + return GasCosts{RegularGas: params.SloadGasEIP2200}, nil // dirty update (2.2) } func makeGasLog(n uint64) gasFunc { - return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { + return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { requestedSize, overflow := stack.Back(1).Uint64WithOverflow() if overflow { - return 0, ErrGasUintOverflow + return GasCosts{}, ErrGasUintOverflow } gas, err := memoryGasCost(mem, memorySize) if err != nil { - return 0, err + return GasCosts{}, err } if gas, overflow = math.SafeAdd(gas, params.LogGas); overflow { - return 0, ErrGasUintOverflow + return GasCosts{}, ErrGasUintOverflow } if gas, overflow = math.SafeAdd(gas, n*params.LogTopicGas); overflow { - return 0, ErrGasUintOverflow + return GasCosts{}, ErrGasUintOverflow } var memorySizeGas uint64 if memorySizeGas, overflow = math.SafeMul(requestedSize, params.LogDataGas); overflow { - return 0, ErrGasUintOverflow + return GasCosts{}, ErrGasUintOverflow } if gas, overflow = math.SafeAdd(gas, memorySizeGas); overflow { - return 0, ErrGasUintOverflow + return GasCosts{}, ErrGasUintOverflow } - return gas, nil + return GasCosts{RegularGas: gas}, nil } } -func gasKeccak256(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { +func gasKeccak256(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { gas, err := memoryGasCost(mem, memorySize) if err != nil { - return 0, err + return GasCosts{}, err } wordGas, overflow := stack.Back(1).Uint64WithOverflow() if overflow { - return 0, ErrGasUintOverflow + return GasCosts{}, ErrGasUintOverflow } if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.Keccak256WordGas); overflow { - return 0, ErrGasUintOverflow + return GasCosts{}, ErrGasUintOverflow } if gas, overflow = math.SafeAdd(gas, wordGas); overflow { - return 0, ErrGasUintOverflow + return GasCosts{}, ErrGasUintOverflow } - return gas, nil + return GasCosts{RegularGas: gas}, nil } // pureMemoryGascost is used by several operations, which aside from their // static cost have a dynamic cost which is solely based on the memory // expansion -func pureMemoryGascost(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { - return memoryGasCost(mem, memorySize) +func pureMemoryGascost(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { + gas, err := memoryGasCost(mem, memorySize) + if err != nil { + return GasCosts{}, err + } + return GasCosts{RegularGas: gas}, nil } var ( @@ -290,64 +294,64 @@ var ( gasCreate = pureMemoryGascost ) -func gasCreate2(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { +func gasCreate2(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { gas, err := memoryGasCost(mem, memorySize) if err != nil { - return 0, err + return GasCosts{}, err } wordGas, overflow := stack.Back(2).Uint64WithOverflow() if overflow { - return 0, ErrGasUintOverflow + return GasCosts{}, ErrGasUintOverflow } if wordGas, overflow = math.SafeMul(toWordSize(wordGas), params.Keccak256WordGas); overflow { - return 0, ErrGasUintOverflow + return GasCosts{}, ErrGasUintOverflow } if gas, overflow = math.SafeAdd(gas, wordGas); overflow { - return 0, ErrGasUintOverflow + return GasCosts{}, ErrGasUintOverflow } - return gas, nil + return GasCosts{RegularGas: gas}, nil } -func gasCreateEip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { +func gasCreateEip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { gas, err := memoryGasCost(mem, memorySize) if err != nil { - return 0, err + return GasCosts{}, err } size, overflow := stack.Back(2).Uint64WithOverflow() if overflow { - return 0, ErrGasUintOverflow + return GasCosts{}, ErrGasUintOverflow } if err := CheckMaxInitCodeSize(&evm.chainRules, size); err != nil { - return 0, err + return GasCosts{}, err } // Since size <= the protocol-defined maximum initcode size limit, these multiplication cannot overflow moreGas := params.InitCodeWordGas * ((size + 31) / 32) if gas, overflow = math.SafeAdd(gas, moreGas); overflow { - return 0, ErrGasUintOverflow + return GasCosts{}, ErrGasUintOverflow } - return gas, nil + return GasCosts{RegularGas: gas}, nil } -func gasCreate2Eip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { +func gasCreate2Eip3860(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { gas, err := memoryGasCost(mem, memorySize) if err != nil { - return 0, err + return GasCosts{}, err } size, overflow := stack.Back(2).Uint64WithOverflow() if overflow { - return 0, ErrGasUintOverflow + return GasCosts{}, ErrGasUintOverflow } if err := CheckMaxInitCodeSize(&evm.chainRules, size); err != nil { - return 0, err + return GasCosts{}, err } // Since size <= the protocol-defined maximum initcode size limit, these multiplication cannot overflow moreGas := (params.InitCodeWordGas + params.Keccak256WordGas) * ((size + 31) / 32) if gas, overflow = math.SafeAdd(gas, moreGas); overflow { - return 0, ErrGasUintOverflow + return GasCosts{}, ErrGasUintOverflow } - return gas, nil + return GasCosts{RegularGas: gas}, nil } -func gasExpFrontier(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { +func gasExpFrontier(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { expByteLen := uint64((stack.data[stack.len()-2].BitLen() + 7) / 8) var ( @@ -355,12 +359,12 @@ func gasExpFrontier(evm *EVM, contract *Contract, stack *Stack, mem *Memory, mem overflow bool ) if gas, overflow = math.SafeAdd(gas, params.ExpGas); overflow { - return 0, ErrGasUintOverflow + return GasCosts{}, ErrGasUintOverflow } - return gas, nil + return GasCosts{RegularGas: gas}, nil } -func gasExpEIP158(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { +func gasExpEIP158(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { expByteLen := uint64((stack.data[stack.len()-2].BitLen() + 7) / 8) var ( @@ -368,9 +372,9 @@ func gasExpEIP158(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memor overflow bool ) if gas, overflow = math.SafeAdd(gas, params.ExpGas); overflow { - return 0, ErrGasUintOverflow + return GasCosts{}, ErrGasUintOverflow } - return gas, nil + return GasCosts{RegularGas: gas}, nil } var ( @@ -381,36 +385,36 @@ var ( ) func makeCallVariantGasCost(intrinsicFunc gasFunc) gasFunc { - return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { + return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { intrinsic, err := intrinsicFunc(evm, contract, stack, mem, memorySize) if err != nil { - return 0, err + return GasCosts{}, err } - evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas, intrinsic, stack.Back(0)) + evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas.RegularGas, intrinsic.RegularGas, stack.Back(0)) if err != nil { - return 0, err + return GasCosts{}, err } - gas, overflow := math.SafeAdd(intrinsic, evm.callGasTemp) + gas, overflow := math.SafeAdd(intrinsic.RegularGas, evm.callGasTemp) if overflow { - return 0, ErrGasUintOverflow + return GasCosts{}, ErrGasUintOverflow } - return gas, nil + return GasCosts{RegularGas: gas}, nil } } -func gasCallIntrinsic(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { +func gasCallIntrinsic(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { var ( gas uint64 transfersValue = !stack.Back(2).IsZero() address = common.Address(stack.Back(1).Bytes20()) ) if evm.readOnly && transfersValue { - return 0, ErrWriteProtection + return GasCosts{}, ErrWriteProtection } // Stateless check memoryGas, err := memoryGasCost(mem, memorySize) if err != nil { - return 0, err + return GasCosts{}, err } var transferGas uint64 if transfersValue && !evm.chainRules.IsEIP4762 { @@ -418,12 +422,12 @@ func gasCallIntrinsic(evm *EVM, contract *Contract, stack *Stack, mem *Memory, m } var overflow bool if gas, overflow = math.SafeAdd(memoryGas, transferGas); overflow { - return 0, ErrGasUintOverflow + return GasCosts{}, ErrGasUintOverflow } // Terminate the gas measurement if the leftover gas is not sufficient, // it can effectively prevent accessing the states in the following steps. - if contract.Gas < gas { - return 0, ErrOutOfGas + if contract.Gas.RegularGas < gas { + return GasCosts{}, ErrOutOfGas } // Stateful check var stateGas uint64 @@ -435,15 +439,15 @@ func gasCallIntrinsic(evm *EVM, contract *Contract, stack *Stack, mem *Memory, m stateGas += params.CallNewAccountGas } if gas, overflow = math.SafeAdd(gas, stateGas); overflow { - return 0, ErrGasUintOverflow + return GasCosts{}, ErrGasUintOverflow } - return gas, nil + return GasCosts{RegularGas: gas}, nil } -func gasCallCodeIntrinsic(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { +func gasCallCodeIntrinsic(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { memoryGas, err := memoryGasCost(mem, memorySize) if err != nil { - return 0, err + return GasCosts{}, err } var ( gas uint64 @@ -453,22 +457,30 @@ func gasCallCodeIntrinsic(evm *EVM, contract *Contract, stack *Stack, mem *Memor gas += params.CallValueTransferGas } if gas, overflow = math.SafeAdd(gas, memoryGas); overflow { - return 0, ErrGasUintOverflow + return GasCosts{}, ErrGasUintOverflow } - return gas, nil + return GasCosts{RegularGas: gas}, nil } -func gasDelegateCallIntrinsic(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { - return memoryGasCost(mem, memorySize) +func gasDelegateCallIntrinsic(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { + gas, err := memoryGasCost(mem, memorySize) + if err != nil { + return GasCosts{}, err + } + return GasCosts{RegularGas: gas}, nil } -func gasStaticCallIntrinsic(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { - return memoryGasCost(mem, memorySize) +func gasStaticCallIntrinsic(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { + gas, err := memoryGasCost(mem, memorySize) + if err != nil { + return GasCosts{}, err + } + return GasCosts{RegularGas: gas}, nil } -func gasSelfdestruct(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { +func gasSelfdestruct(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { if evm.readOnly { - return 0, ErrWriteProtection + return GasCosts{}, ErrWriteProtection } var gas uint64 @@ -490,5 +502,5 @@ func gasSelfdestruct(evm *EVM, contract *Contract, stack *Stack, mem *Memory, me if !evm.StateDB.HasSelfDestructed(contract.Address()) { evm.StateDB.AddRefund(params.SelfdestructRefundGas) } - return gas, nil + return GasCosts{RegularGas: gas}, nil } diff --git a/core/vm/gascosts.go b/core/vm/gascosts.go new file mode 100644 index 0000000000..ba6746758b --- /dev/null +++ b/core/vm/gascosts.go @@ -0,0 +1,36 @@ +// Copyright 2026 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package vm + +import "fmt" + +// GasCosts denotes a vector of gas costs in the +// multidimensional metering paradigm. +type GasCosts struct { + RegularGas uint64 + StateGas uint64 +} + +// Sum returns the total gas (regular + state). +func (g GasCosts) Sum() uint64 { + return g.RegularGas + g.StateGas +} + +// String returns a visual representation of the gas vector. +func (g GasCosts) String() string { + return fmt.Sprintf("<%v,%v>", g.RegularGas, g.StateGas) +} diff --git a/core/vm/instructions.go b/core/vm/instructions.go index a5fa11e307..74400732ac 100644 --- a/core/vm/instructions.go +++ b/core/vm/instructions.go @@ -566,7 +566,7 @@ func opMsize(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { } func opGas(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { - scope.Stack.push(new(uint256.Int).SetUint64(scope.Contract.Gas)) + scope.Stack.push(new(uint256.Int).SetUint64(scope.Contract.Gas.RegularGas)) return nil, nil } @@ -658,7 +658,7 @@ func opCreate(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { value = scope.Stack.pop() offset, size = scope.Stack.pop(), scope.Stack.pop() input = scope.Memory.GetCopy(offset.Uint64(), size.Uint64()) - gas = scope.Contract.Gas + gas = scope.Contract.Gas.RegularGas ) if evm.chainRules.IsEIP150 { gas -= gas / 64 @@ -702,7 +702,7 @@ func opCreate2(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { offset, size = scope.Stack.pop(), scope.Stack.pop() salt = scope.Stack.pop() input = scope.Memory.GetCopy(offset.Uint64(), size.Uint64()) - gas = scope.Contract.Gas + gas = scope.Contract.Gas.RegularGas ) // Apply EIP150 diff --git a/core/vm/instructions_test.go b/core/vm/instructions_test.go index 4c6d093d2e..1f69eea3da 100644 --- a/core/vm/instructions_test.go +++ b/core/vm/instructions_test.go @@ -879,7 +879,7 @@ func TestOpMCopy(t *testing.T) { if dynamicCost, err := gasMcopy(evm, nil, stack, mem, memorySize); err != nil { t.Error(err) } else { - haveGas = GasFastestStep + dynamicCost + haveGas = GasFastestStep + dynamicCost.RegularGas } // Expand mem if memorySize > 0 { diff --git a/core/vm/interpreter.go b/core/vm/interpreter.go index 620c069fc8..b507595fab 100644 --- a/core/vm/interpreter.go +++ b/core/vm/interpreter.go @@ -166,14 +166,14 @@ func (evm *EVM) Run(contract *Contract, input []byte, readOnly bool) (ret []byte for { if debug { // Capture pre-execution values for tracing. - logged, pcCopy, gasCopy = false, pc, contract.Gas + logged, pcCopy, gasCopy = false, pc, contract.Gas.RegularGas } if isEIP4762 && !contract.IsDeployment && !contract.IsSystemCall { // if the PC ends up in a new "chunk" of verkleized code, charge the // associated costs. contractAddr := contract.Address() - consumed, wanted := evm.TxContext.AccessEvents.CodeChunksRangeGas(contractAddr, pc, 1, uint64(len(contract.Code)), false, contract.Gas) + consumed, wanted := evm.TxContext.AccessEvents.CodeChunksRangeGas(contractAddr, pc, 1, uint64(len(contract.Code)), false, contract.Gas.RegularGas) contract.UseGas(consumed, evm.Config.Tracer, tracing.GasChangeWitnessCodeChunk) if consumed < wanted { return nil, ErrOutOfGas @@ -192,10 +192,10 @@ func (evm *EVM) Run(contract *Contract, input []byte, readOnly bool) (ret []byte return nil, &ErrStackOverflow{stackLen: sLen, limit: operation.maxStack} } // for tracing: this gas consumption event is emitted below in the debug section. - if contract.Gas < cost { + if contract.Gas.RegularGas < cost { return nil, ErrOutOfGas } else { - contract.Gas -= cost + contract.Gas.RegularGas -= cost } // All ops with a dynamic memory usage also has a dynamic gas cost. @@ -218,17 +218,17 @@ func (evm *EVM) Run(contract *Contract, input []byte, readOnly bool) (ret []byte } // Consume the gas and return an error if not enough gas is available. // cost is explicitly set so that the capture state defer method can get the proper cost - var dynamicCost uint64 + var dynamicCost GasCosts dynamicCost, err = operation.dynamicGas(evm, contract, stack, mem, memorySize) - cost += dynamicCost // for tracing + cost += dynamicCost.RegularGas // for tracing if err != nil { return nil, fmt.Errorf("%w: %v", ErrOutOfGas, err) } // for tracing: this gas consumption event is emitted below in the debug section. - if contract.Gas < dynamicCost { + if contract.Gas.RegularGas < dynamicCost.RegularGas { return nil, ErrOutOfGas } else { - contract.Gas -= dynamicCost + contract.Gas.RegularGas -= dynamicCost.RegularGas } } diff --git a/core/vm/jump_table.go b/core/vm/jump_table.go index a2e2c91194..82fc43ec13 100644 --- a/core/vm/jump_table.go +++ b/core/vm/jump_table.go @@ -24,7 +24,7 @@ import ( type ( executionFunc func(pc *uint64, evm *EVM, callContext *ScopeContext) ([]byte, error) - gasFunc func(*EVM, *Contract, *Stack, *Memory, uint64) (uint64, error) // last parameter is the requested memory size as a uint64 + gasFunc func(*EVM, *Contract, *Stack, *Memory, uint64) (GasCosts, error) // last parameter is the requested memory size as a uint64 // memorySizeFunc returns the required size, and whether the operation overflowed a uint64 memorySizeFunc func(*Stack) (size uint64, overflow bool) ) diff --git a/core/vm/operations_acl.go b/core/vm/operations_acl.go index addd2b162f..154c261cae 100644 --- a/core/vm/operations_acl.go +++ b/core/vm/operations_acl.go @@ -27,13 +27,13 @@ import ( ) func makeGasSStoreFunc(clearingRefund uint64) gasFunc { - return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { + return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { if evm.readOnly { - return 0, ErrWriteProtection + return GasCosts{}, ErrWriteProtection } // If we fail the minimum gas availability invariant, fail (0) - if contract.Gas <= params.SstoreSentryGasEIP2200 { - return 0, errors.New("not enough gas for reentrancy sentry") + if contract.Gas.RegularGas <= params.SstoreSentryGasEIP2200 { + return GasCosts{}, errors.New("not enough gas for reentrancy sentry") } // Gas sentry honoured, do the actual gas calculation based on the stored value var ( @@ -53,18 +53,18 @@ func makeGasSStoreFunc(clearingRefund uint64) gasFunc { if current == value { // noop (1) // EIP 2200 original clause: // return params.SloadGasEIP2200, nil - return cost + params.WarmStorageReadCostEIP2929, nil // SLOAD_GAS + return GasCosts{RegularGas: cost + params.WarmStorageReadCostEIP2929}, nil // SLOAD_GAS } if original == current { if original == (common.Hash{}) { // create slot (2.1.1) - return cost + params.SstoreSetGasEIP2200, nil + return GasCosts{RegularGas: cost + params.SstoreSetGasEIP2200}, nil } if value == (common.Hash{}) { // delete slot (2.1.2b) evm.StateDB.AddRefund(clearingRefund) } // EIP-2200 original clause: // return params.SstoreResetGasEIP2200, nil // write existing slot (2.1.2) - return cost + (params.SstoreResetGasEIP2200 - params.ColdSloadCostEIP2929), nil // write existing slot (2.1.2) + return GasCosts{RegularGas: cost + (params.SstoreResetGasEIP2200 - params.ColdSloadCostEIP2929)}, nil // write existing slot (2.1.2) } if original != (common.Hash{}) { if current == (common.Hash{}) { // recreate slot (2.2.1.1) @@ -89,7 +89,7 @@ func makeGasSStoreFunc(clearingRefund uint64) gasFunc { } // EIP-2200 original clause: //return params.SloadGasEIP2200, nil // dirty update (2.2) - return cost + params.WarmStorageReadCostEIP2929, nil // dirty update (2.2) + return GasCosts{RegularGas: cost + params.WarmStorageReadCostEIP2929}, nil // dirty update (2.2) } } @@ -98,7 +98,7 @@ func makeGasSStoreFunc(clearingRefund uint64) gasFunc { // whose storage is being read) is not yet in accessed_storage_keys, // charge 2100 gas and add the pair to accessed_storage_keys. // If the pair is already in accessed_storage_keys, charge 100 gas. -func gasSLoadEIP2929(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { +func gasSLoadEIP2929(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { loc := stack.peek() slot := common.Hash(loc.Bytes32()) // Check slot presence in the access list @@ -106,9 +106,9 @@ func gasSLoadEIP2929(evm *EVM, contract *Contract, stack *Stack, mem *Memory, me // If the caller cannot afford the cost, this change will be rolled back // If he does afford it, we can skip checking the same thing later on, during execution evm.StateDB.AddSlotToAccessList(contract.Address(), slot) - return params.ColdSloadCostEIP2929, nil + return GasCosts{RegularGas: params.ColdSloadCostEIP2929}, nil } - return params.WarmStorageReadCostEIP2929, nil + return GasCosts{RegularGas: params.WarmStorageReadCostEIP2929}, nil } // gasExtCodeCopyEIP2929 implements extcodecopy according to EIP-2929 @@ -116,12 +116,13 @@ func gasSLoadEIP2929(evm *EVM, contract *Contract, stack *Stack, mem *Memory, me // > If the target is not in accessed_addresses, // > charge COLD_ACCOUNT_ACCESS_COST gas, and add the address to accessed_addresses. // > Otherwise, charge WARM_STORAGE_READ_COST gas. -func gasExtCodeCopyEIP2929(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { +func gasExtCodeCopyEIP2929(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { // memory expansion first (dynamic part of pre-2929 implementation) - gas, err := gasExtCodeCopy(evm, contract, stack, mem, memorySize) + gasCost, err := gasExtCodeCopy(evm, contract, stack, mem, memorySize) if err != nil { - return 0, err + return GasCosts{}, err } + gas := gasCost.RegularGas addr := common.Address(stack.peek().Bytes20()) // Check slot presence in the access list if !evm.StateDB.AddressInAccessList(addr) { @@ -129,11 +130,11 @@ func gasExtCodeCopyEIP2929(evm *EVM, contract *Contract, stack *Stack, mem *Memo var overflow bool // We charge (cold-warm), since 'warm' is already charged as constantGas if gas, overflow = math.SafeAdd(gas, params.ColdAccountAccessCostEIP2929-params.WarmStorageReadCostEIP2929); overflow { - return 0, ErrGasUintOverflow + return GasCosts{}, ErrGasUintOverflow } - return gas, nil + return GasCosts{RegularGas: gas}, nil } - return gas, nil + return GasCosts{RegularGas: gas}, nil } // gasEip2929AccountCheck checks whether the first stack item (as address) is present in the access list. @@ -143,20 +144,20 @@ func gasExtCodeCopyEIP2929(evm *EVM, contract *Contract, stack *Stack, mem *Memo // - extcodehash, // - extcodesize, // - (ext) balance -func gasEip2929AccountCheck(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { +func gasEip2929AccountCheck(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { addr := common.Address(stack.peek().Bytes20()) // Check slot presence in the access list if !evm.StateDB.AddressInAccessList(addr) { // If the caller cannot afford the cost, this change will be rolled back evm.StateDB.AddAddressToAccessList(addr) // The warm storage read cost is already charged as constantGas - return params.ColdAccountAccessCostEIP2929 - params.WarmStorageReadCostEIP2929, nil + return GasCosts{RegularGas: params.ColdAccountAccessCostEIP2929 - params.WarmStorageReadCostEIP2929}, nil } - return 0, nil + return GasCosts{}, nil } func makeCallVariantGasCallEIP2929(oldCalculator gasFunc, addressPosition int) gasFunc { - return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { + return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { addr := common.Address(stack.Back(addressPosition).Bytes20()) // Check slot presence in the access list warmAccess := evm.StateDB.AddressInAccessList(addr) @@ -168,7 +169,7 @@ func makeCallVariantGasCallEIP2929(oldCalculator gasFunc, addressPosition int) g // Charge the remaining difference here already, to correctly calculate available // gas for call if !contract.UseGas(coldCost, evm.Config.Tracer, tracing.GasChangeCallStorageColdAccess) { - return 0, ErrOutOfGas + return GasCosts{}, ErrOutOfGas } } // Now call the old calculator, which takes into account @@ -176,21 +177,22 @@ func makeCallVariantGasCallEIP2929(oldCalculator gasFunc, addressPosition int) g // - transfer value // - memory expansion // - 63/64ths rule - gas, err := oldCalculator(evm, contract, stack, mem, memorySize) + gasCost, err := oldCalculator(evm, contract, stack, mem, memorySize) if warmAccess || err != nil { - return gas, err + return gasCost, err } // In case of a cold access, we temporarily add the cold charge back, and also // add it to the returned gas. By adding it to the return, it will be charged // outside of this function, as part of the dynamic gas, and that will make it // also become correctly reported to tracers. - contract.Gas += coldCost + contract.Gas.RegularGas += coldCost + gas := gasCost.RegularGas var overflow bool if gas, overflow = math.SafeAdd(gas, coldCost); overflow { - return 0, ErrGasUintOverflow + return GasCosts{}, ErrGasUintOverflow } - return gas, nil + return GasCosts{RegularGas: gas}, nil } } @@ -224,13 +226,13 @@ var ( // makeSelfdestructGasFn can create the selfdestruct dynamic gas function for EIP-2929 and EIP-3529 func makeSelfdestructGasFn(refundsEnabled bool) gasFunc { - gasFunc := func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { + gasFunc := func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { var ( gas uint64 address = common.Address(stack.peek().Bytes20()) ) if evm.readOnly { - return 0, ErrWriteProtection + return GasCosts{}, ErrWriteProtection } if !evm.StateDB.AddressInAccessList(address) { // If the caller cannot afford the cost, this change will be rolled back @@ -239,8 +241,8 @@ func makeSelfdestructGasFn(refundsEnabled bool) gasFunc { // Terminate the gas measurement if the leftover gas is not sufficient, // it can effectively prevent accessing the states in the following steps - if contract.Gas < gas { - return 0, ErrOutOfGas + if contract.Gas.RegularGas < gas { + return GasCosts{}, ErrOutOfGas } } // if empty and transfers value @@ -250,7 +252,7 @@ func makeSelfdestructGasFn(refundsEnabled bool) gasFunc { if refundsEnabled && !evm.StateDB.HasSelfDestructed(contract.Address()) { evm.StateDB.AddRefund(params.SelfdestructRefundGas) } - return gas, nil + return GasCosts{RegularGas: gas}, nil } return gasFunc } @@ -262,20 +264,20 @@ var ( gasCallCodeEIP7702 = makeCallVariantGasCallEIP7702(gasCallCodeIntrinsic) ) -func gasCallEIP7702(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { +func gasCallEIP7702(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { // Return early if this call attempts to transfer value in a static context. // Although it's checked in `gasCall`, EIP-7702 loads the target's code before // to determine if it is resolving a delegation. This could incorrectly record // the target in the block access list (BAL) if the call later fails. transfersValue := !stack.Back(2).IsZero() if evm.readOnly && transfersValue { - return 0, ErrWriteProtection + return GasCosts{}, ErrWriteProtection } return innerGasCallEIP7702(evm, contract, stack, mem, memorySize) } func makeCallVariantGasCallEIP7702(intrinsicFunc gasFunc) gasFunc { - return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { + return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { var ( eip2929Cost uint64 eip7702Cost uint64 @@ -294,7 +296,7 @@ func makeCallVariantGasCallEIP7702(intrinsicFunc gasFunc) gasFunc { // Charge the remaining difference here already, to correctly calculate // available gas for call if !contract.UseGas(eip2929Cost, evm.Config.Tracer, tracing.GasChangeCallStorageColdAccess) { - return 0, ErrOutOfGas + return GasCosts{}, ErrOutOfGas } } @@ -305,13 +307,13 @@ func makeCallVariantGasCallEIP7702(intrinsicFunc gasFunc) gasFunc { // - create new account intrinsicCost, err := intrinsicFunc(evm, contract, stack, mem, memorySize) if err != nil { - return 0, err + return GasCosts{}, err } // Terminate the gas measurement if the leftover gas is not sufficient, // it can effectively prevent accessing the states in the following steps. // It's an essential safeguard before any stateful check. - if contract.Gas < intrinsicCost { - return 0, ErrOutOfGas + if contract.Gas.RegularGas < intrinsicCost.RegularGas { + return GasCosts{}, ErrOutOfGas } // Check if code is a delegation and if so, charge for resolution. @@ -323,20 +325,20 @@ func makeCallVariantGasCallEIP7702(intrinsicFunc gasFunc) gasFunc { eip7702Cost = params.ColdAccountAccessCostEIP2929 } if !contract.UseGas(eip7702Cost, evm.Config.Tracer, tracing.GasChangeCallStorageColdAccess) { - return 0, ErrOutOfGas + return GasCosts{}, ErrOutOfGas } } // Calculate the gas budget for the nested call. The costs defined by // EIP-2929 and EIP-7702 have already been applied. - evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas, intrinsicCost, stack.Back(0)) + evm.callGasTemp, err = callGas(evm.chainRules.IsEIP150, contract.Gas.RegularGas, intrinsicCost.RegularGas, stack.Back(0)) if err != nil { - return 0, err + return GasCosts{}, err } // Temporarily add the gas charge back to the contract and return value. By // adding it to the return, it will be charged outside of this function, as // part of the dynamic gas. This will ensure it is correctly reported to // tracers. - contract.Gas += eip2929Cost + eip7702Cost + contract.Gas.RegularGas += eip2929Cost + eip7702Cost // Aggregate the gas costs from all components, including EIP-2929, EIP-7702, // the CALL opcode itself, and the cost incurred by nested calls. @@ -345,14 +347,14 @@ func makeCallVariantGasCallEIP7702(intrinsicFunc gasFunc) gasFunc { totalCost uint64 ) if totalCost, overflow = math.SafeAdd(eip2929Cost, eip7702Cost); overflow { - return 0, ErrGasUintOverflow + return GasCosts{}, ErrGasUintOverflow } - if totalCost, overflow = math.SafeAdd(totalCost, intrinsicCost); overflow { - return 0, ErrGasUintOverflow + if totalCost, overflow = math.SafeAdd(totalCost, intrinsicCost.RegularGas); overflow { + return GasCosts{}, ErrGasUintOverflow } if totalCost, overflow = math.SafeAdd(totalCost, evm.callGasTemp); overflow { - return 0, ErrGasUintOverflow + return GasCosts{}, ErrGasUintOverflow } - return totalCost, nil + return GasCosts{RegularGas: totalCost}, nil } } diff --git a/core/vm/operations_verkle.go b/core/vm/operations_verkle.go index 30f9957775..d57f2c4dcf 100644 --- a/core/vm/operations_verkle.go +++ b/core/vm/operations_verkle.go @@ -24,37 +24,37 @@ import ( "github.com/ethereum/go-ethereum/params" ) -func gasSStore4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { - return evm.AccessEvents.SlotGas(contract.Address(), stack.peek().Bytes32(), true, contract.Gas, true), nil +func gasSStore4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { + return GasCosts{RegularGas: evm.AccessEvents.SlotGas(contract.Address(), stack.peek().Bytes32(), true, contract.Gas.RegularGas, true)}, nil } -func gasSLoad4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { - return evm.AccessEvents.SlotGas(contract.Address(), stack.peek().Bytes32(), false, contract.Gas, true), nil +func gasSLoad4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { + return GasCosts{RegularGas: evm.AccessEvents.SlotGas(contract.Address(), stack.peek().Bytes32(), false, contract.Gas.RegularGas, true)}, nil } -func gasBalance4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { +func gasBalance4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { address := stack.peek().Bytes20() - return evm.AccessEvents.BasicDataGas(address, false, contract.Gas, true), nil + return GasCosts{RegularGas: evm.AccessEvents.BasicDataGas(address, false, contract.Gas.RegularGas, true)}, nil } -func gasExtCodeSize4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { +func gasExtCodeSize4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { address := stack.peek().Bytes20() if _, isPrecompile := evm.precompile(address); isPrecompile { - return 0, nil + return GasCosts{}, nil } - return evm.AccessEvents.BasicDataGas(address, false, contract.Gas, true), nil + return GasCosts{RegularGas: evm.AccessEvents.BasicDataGas(address, false, contract.Gas.RegularGas, true)}, nil } -func gasExtCodeHash4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { +func gasExtCodeHash4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { address := stack.peek().Bytes20() if _, isPrecompile := evm.precompile(address); isPrecompile { - return 0, nil + return GasCosts{}, nil } - return evm.AccessEvents.CodeHashGas(address, false, contract.Gas, true), nil + return GasCosts{RegularGas: evm.AccessEvents.CodeHashGas(address, false, contract.Gas.RegularGas, true)}, nil } func makeCallVariantGasEIP4762(oldCalculator gasFunc, withTransferCosts bool) gasFunc { - return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { + return func(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { var ( target = common.Address(stack.Back(1).Bytes20()) witnessGas uint64 @@ -65,9 +65,9 @@ func makeCallVariantGasEIP4762(oldCalculator gasFunc, withTransferCosts bool) ga // If value is transferred, it is charged before 1/64th // is subtracted from the available gas pool. if withTransferCosts && !stack.Back(2).IsZero() { - wantedValueTransferWitnessGas := evm.AccessEvents.ValueTransferGas(contract.Address(), target, contract.Gas) - if wantedValueTransferWitnessGas > contract.Gas { - return wantedValueTransferWitnessGas, nil + wantedValueTransferWitnessGas := evm.AccessEvents.ValueTransferGas(contract.Address(), target, contract.Gas.RegularGas) + if wantedValueTransferWitnessGas > contract.Gas.RegularGas { + return GasCosts{RegularGas: wantedValueTransferWitnessGas}, nil } witnessGas = wantedValueTransferWitnessGas } else if isPrecompile || isSystemContract { @@ -78,25 +78,26 @@ func makeCallVariantGasEIP4762(oldCalculator gasFunc, withTransferCosts bool) ga // (so before we get to this point) // But the message call is part of the subcall, for which only 63/64th // of the gas should be available. - wantedMessageCallWitnessGas := evm.AccessEvents.MessageCallGas(target, contract.Gas-witnessGas) + wantedMessageCallWitnessGas := evm.AccessEvents.MessageCallGas(target, contract.Gas.RegularGas-witnessGas) var overflow bool if witnessGas, overflow = math.SafeAdd(witnessGas, wantedMessageCallWitnessGas); overflow { - return 0, ErrGasUintOverflow + return GasCosts{}, ErrGasUintOverflow } - if witnessGas > contract.Gas { - return witnessGas, nil + if witnessGas > contract.Gas.RegularGas { + return GasCosts{RegularGas: witnessGas}, nil } } - contract.Gas -= witnessGas + contract.Gas.RegularGas -= witnessGas // if the operation fails, adds witness gas to the gas before returning the error - gas, err := oldCalculator(evm, contract, stack, mem, memorySize) - contract.Gas += witnessGas // restore witness gas so that it can be charged at the callsite + gasCost, err := oldCalculator(evm, contract, stack, mem, memorySize) + contract.Gas.RegularGas += witnessGas // restore witness gas so that it can be charged at the callsite + gas := gasCost.RegularGas var overflow bool if gas, overflow = math.SafeAdd(gas, witnessGas); overflow { - return 0, ErrGasUintOverflow + return GasCosts{}, ErrGasUintOverflow } - return gas, err + return GasCosts{RegularGas: gas}, err } } @@ -107,18 +108,18 @@ var ( gasDelegateCallEIP4762 = makeCallVariantGasEIP4762(gasDelegateCall, false) ) -func gasSelfdestructEIP4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { +func gasSelfdestructEIP4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { beneficiaryAddr := common.Address(stack.peek().Bytes20()) if _, isPrecompile := evm.precompile(beneficiaryAddr); isPrecompile { - return 0, nil + return GasCosts{}, nil } if contract.IsSystemCall { - return 0, nil + return GasCosts{}, nil } contractAddr := contract.Address() - wanted := evm.AccessEvents.BasicDataGas(contractAddr, false, contract.Gas, false) - if wanted > contract.Gas { - return wanted, nil + wanted := evm.AccessEvents.BasicDataGas(contractAddr, false, contract.Gas.RegularGas, false) + if wanted > contract.Gas.RegularGas { + return GasCosts{RegularGas: wanted}, nil } statelessGas := wanted balanceIsZero := evm.StateDB.GetBalance(contractAddr).Sign() == 0 @@ -126,44 +127,45 @@ func gasSelfdestructEIP4762(evm *EVM, contract *Contract, stack *Stack, mem *Mem isSystemContract := beneficiaryAddr == params.HistoryStorageAddress if (isPrecompile || isSystemContract) && balanceIsZero { - return statelessGas, nil + return GasCosts{RegularGas: statelessGas}, nil } if contractAddr != beneficiaryAddr { - wanted := evm.AccessEvents.BasicDataGas(beneficiaryAddr, false, contract.Gas-statelessGas, false) - if wanted > contract.Gas-statelessGas { - return statelessGas + wanted, nil + wanted := evm.AccessEvents.BasicDataGas(beneficiaryAddr, false, contract.Gas.RegularGas-statelessGas, false) + if wanted > contract.Gas.RegularGas-statelessGas { + return GasCosts{RegularGas: statelessGas + wanted}, nil } statelessGas += wanted } // Charge write costs if it transfers value if !balanceIsZero { - wanted := evm.AccessEvents.BasicDataGas(contractAddr, true, contract.Gas-statelessGas, false) - if wanted > contract.Gas-statelessGas { - return statelessGas + wanted, nil + wanted := evm.AccessEvents.BasicDataGas(contractAddr, true, contract.Gas.RegularGas-statelessGas, false) + if wanted > contract.Gas.RegularGas-statelessGas { + return GasCosts{RegularGas: statelessGas + wanted}, nil } statelessGas += wanted if contractAddr != beneficiaryAddr { if evm.StateDB.Exist(beneficiaryAddr) { - wanted = evm.AccessEvents.BasicDataGas(beneficiaryAddr, true, contract.Gas-statelessGas, false) + wanted = evm.AccessEvents.BasicDataGas(beneficiaryAddr, true, contract.Gas.RegularGas-statelessGas, false) } else { - wanted = evm.AccessEvents.AddAccount(beneficiaryAddr, true, contract.Gas-statelessGas) + wanted = evm.AccessEvents.AddAccount(beneficiaryAddr, true, contract.Gas.RegularGas-statelessGas) } - if wanted > contract.Gas-statelessGas { - return statelessGas + wanted, nil + if wanted > contract.Gas.RegularGas-statelessGas { + return GasCosts{RegularGas: statelessGas + wanted}, nil } statelessGas += wanted } } - return statelessGas, nil + return GasCosts{RegularGas: statelessGas}, nil } -func gasCodeCopyEip4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { - gas, err := gasCodeCopy(evm, contract, stack, mem, memorySize) +func gasCodeCopyEip4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { + gasCost, err := gasCodeCopy(evm, contract, stack, mem, memorySize) if err != nil { - return 0, err + return GasCosts{}, err } + gas := gasCost.RegularGas if !contract.IsDeployment && !contract.IsSystemCall { var ( codeOffset = stack.Back(1) @@ -175,31 +177,32 @@ func gasCodeCopyEip4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory, } _, copyOffset, nonPaddedCopyLength := getDataAndAdjustedBounds(contract.Code, uint64CodeOffset, length.Uint64()) - _, wanted := evm.AccessEvents.CodeChunksRangeGas(contract.Address(), copyOffset, nonPaddedCopyLength, uint64(len(contract.Code)), false, contract.Gas-gas) + _, wanted := evm.AccessEvents.CodeChunksRangeGas(contract.Address(), copyOffset, nonPaddedCopyLength, uint64(len(contract.Code)), false, contract.Gas.RegularGas-gas) gas += wanted } - return gas, nil + return GasCosts{RegularGas: gas}, nil } -func gasExtCodeCopyEIP4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (uint64, error) { +func gasExtCodeCopyEIP4762(evm *EVM, contract *Contract, stack *Stack, mem *Memory, memorySize uint64) (GasCosts, error) { // memory expansion first (dynamic part of pre-2929 implementation) - gas, err := gasExtCodeCopy(evm, contract, stack, mem, memorySize) + gasCost, err := gasExtCodeCopy(evm, contract, stack, mem, memorySize) if err != nil { - return 0, err + return GasCosts{}, err } + gas := gasCost.RegularGas addr := common.Address(stack.peek().Bytes20()) _, isPrecompile := evm.precompile(addr) if isPrecompile || addr == params.HistoryStorageAddress { var overflow bool if gas, overflow = math.SafeAdd(gas, params.WarmStorageReadCostEIP2929); overflow { - return 0, ErrGasUintOverflow + return GasCosts{}, ErrGasUintOverflow } - return gas, nil + return GasCosts{RegularGas: gas}, nil } - wgas := evm.AccessEvents.BasicDataGas(addr, false, contract.Gas-gas, true) + wgas := evm.AccessEvents.BasicDataGas(addr, false, contract.Gas.RegularGas-gas, true) var overflow bool if gas, overflow = math.SafeAdd(gas, wgas); overflow { - return 0, ErrGasUintOverflow + return GasCosts{}, ErrGasUintOverflow } - return gas, nil + return GasCosts{RegularGas: gas}, nil } diff --git a/eth/tracers/js/tracer_test.go b/eth/tracers/js/tracer_test.go index b21e104abc..694debcf98 100644 --- a/eth/tracers/js/tracer_test.go +++ b/eth/tracers/js/tracer_test.go @@ -66,9 +66,9 @@ func runTrace(tracer *tracers.Tracer, vmctx *vmContext, chaincfg *params.ChainCo tracer.OnTxStart(evm.GetVMContext(), types.NewTx(&types.LegacyTx{Gas: gasLimit, GasPrice: vmctx.txCtx.GasPrice.ToBig()}), contract.Caller()) tracer.OnEnter(0, byte(vm.CALL), contract.Caller(), contract.Address(), []byte{}, startGas, value.ToBig()) ret, err := evm.Run(contract, []byte{}, false) - tracer.OnExit(0, ret, startGas-contract.Gas, err, true) + tracer.OnExit(0, ret, startGas-contract.Gas.RegularGas, err, true) // Rest gas assumes no refund - tracer.OnTxEnd(&types.Receipt{GasUsed: gasLimit - contract.Gas}, nil) + tracer.OnTxEnd(&types.Receipt{GasUsed: gasLimit - contract.Gas.RegularGas}, nil) if err != nil { return nil, err } From 735bfd121a738eb35efb9b88e9fe25618b807fa4 Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Mon, 13 Apr 2026 09:42:37 +0200 Subject: [PATCH 10/40] trie/bintrie: spec change, big endian hashing of slot key (#34670) The spec has been changed during SIC #49, the offset is encoded as a big-endian number. --- core/genesis_test.go | 2 +- trie/bintrie/key_encoding.go | 9 +++------ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/core/genesis_test.go b/core/genesis_test.go index 2b08b36690..2ff64e8d21 100644 --- a/core/genesis_test.go +++ b/core/genesis_test.go @@ -308,7 +308,7 @@ func TestVerkleGenesisCommit(t *testing.T) { }, } - expected := common.FromHex("1fd154971d9a386c4ec75fe7138c17efb569bfc2962e46e94a376ba997e3fadc") + expected := common.FromHex("0870fd587c41dc778019de8c5cb3193fe4ef1f417976461952d3712ba39163f5") got := genesis.ToBlock().Root().Bytes() if !bytes.Equal(got, expected) { t.Fatalf("invalid genesis state root, expected %x, got %x", expected, got) diff --git a/trie/bintrie/key_encoding.go b/trie/bintrie/key_encoding.go index c009f1529f..265935293b 100644 --- a/trie/bintrie/key_encoding.go +++ b/trie/bintrie/key_encoding.go @@ -54,15 +54,12 @@ func getBinaryTreeKey(addr common.Address, offset []byte, overflow bool) []byte defer returnSha256(hasher) hasher.Write(zeroHash[:12]) hasher.Write(addr[:]) - var buf [32]byte - // key is big endian, hashed value is little endian - for i := range offset[:31] { - buf[i] = offset[30-i] - } + var buf [32]byte // TODO: make offset a 33-byte value to avoid an extra stack alloc + copy(buf[1:32], offset[:31]) if overflow { // Overflow detected when adding MAIN_STORAGE_OFFSET, // reporting it in the shifter 32 byte value. - buf[31] = 1 + buf[0] = 1 } hasher.Write(buf[:]) k := hasher.Sum(nil) From ecae519972c0c8b4b702fae651e1825be64275e0 Mon Sep 17 00:00:00 2001 From: Gaurav Dhiman Date: Mon, 13 Apr 2026 17:15:35 +0530 Subject: [PATCH 11/40] beacon/engine, miner: fix testing_buildBlockV1 response (#34704) Two fixes for `testing_buildBlockV1`: 1. Add `omitempty` to `SlotNumber` in `ExecutableData` so it is omitted for pre-Amsterdam payloads. The spec defines the response as `ExecutionPayloadV3` which does not include `slotNumber`. 2. Pass `res.fees` instead of `new(big.Int)` in `BuildTestingPayload` so `blockValue` reflects actual priority fees instead of always being zero. Corresponding fixture update: ethereum/execution-apis#783 --- beacon/engine/gen_ed.go | 4 ++-- beacon/engine/types.go | 2 +- miner/payload_building.go | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/beacon/engine/gen_ed.go b/beacon/engine/gen_ed.go index b460368b84..c733b3f350 100644 --- a/beacon/engine/gen_ed.go +++ b/beacon/engine/gen_ed.go @@ -34,7 +34,7 @@ func (e ExecutableData) MarshalJSON() ([]byte, error) { Withdrawals []*types.Withdrawal `json:"withdrawals"` BlobGasUsed *hexutil.Uint64 `json:"blobGasUsed"` ExcessBlobGas *hexutil.Uint64 `json:"excessBlobGas"` - SlotNumber *hexutil.Uint64 `json:"slotNumber"` + SlotNumber *hexutil.Uint64 `json:"slotNumber,omitempty"` } var enc ExecutableData enc.ParentHash = e.ParentHash @@ -83,7 +83,7 @@ func (e *ExecutableData) UnmarshalJSON(input []byte) error { Withdrawals []*types.Withdrawal `json:"withdrawals"` BlobGasUsed *hexutil.Uint64 `json:"blobGasUsed"` ExcessBlobGas *hexutil.Uint64 `json:"excessBlobGas"` - SlotNumber *hexutil.Uint64 `json:"slotNumber"` + SlotNumber *hexutil.Uint64 `json:"slotNumber,omitempty"` } var dec ExecutableData if err := json.Unmarshal(input, &dec); err != nil { diff --git a/beacon/engine/types.go b/beacon/engine/types.go index 5c94e67de1..a312fee88a 100644 --- a/beacon/engine/types.go +++ b/beacon/engine/types.go @@ -99,7 +99,7 @@ type ExecutableData struct { Withdrawals []*types.Withdrawal `json:"withdrawals"` BlobGasUsed *uint64 `json:"blobGasUsed"` ExcessBlobGas *uint64 `json:"excessBlobGas"` - SlotNumber *uint64 `json:"slotNumber"` + SlotNumber *uint64 `json:"slotNumber,omitempty"` } // JSON type overrides for executableData. diff --git a/miner/payload_building.go b/miner/payload_building.go index ccaabec373..db8126828a 100644 --- a/miner/payload_building.go +++ b/miner/payload_building.go @@ -360,5 +360,5 @@ func (miner *Miner) BuildTestingPayload(args *BuildPayloadArgs, transactions []* if res.err != nil { return nil, res.err } - return engine.BlockToExecutableData(res.block, new(big.Int), res.sidecars, res.requests), nil + return engine.BlockToExecutableData(res.block, res.fees, res.sidecars, res.requests), nil } From f7f57d29d43704cb49a3154da044f31970875f04 Mon Sep 17 00:00:00 2001 From: bigbear <155267841+aso20455@users.noreply.github.com> Date: Mon, 13 Apr 2026 13:57:11 +0200 Subject: [PATCH 12/40] crypto/bn256: fix comment in MulXi (#34659) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The comment formula showed (i+3) but the code multiplies by 9 (Lsh 3 + add = 8+1). This was a error when porting from upstream golang.org/x/crypto/bn256 where ξ=i+3. Go-ethereum changed the constant to ξ=i+9 but forgot to update the inner formula. --- crypto/bn256/google/gfp2.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crypto/bn256/google/gfp2.go b/crypto/bn256/google/gfp2.go index 3981f6cb4f..394ef83548 100644 --- a/crypto/bn256/google/gfp2.go +++ b/crypto/bn256/google/gfp2.go @@ -153,7 +153,7 @@ func (e *gfP2) MulScalar(a *gfP2, b *big.Int) *gfP2 { // MulXi sets e=ξa where ξ=i+9 and then returns e. func (e *gfP2) MulXi(a *gfP2, pool *bnPool) *gfP2 { - // (xi+y)(i+3) = (9x+y)i+(9y-x) + // (xi+y)(i+9) = (9x+y)i+(9y-x) tx := pool.Get().Lsh(a.x, 3) tx.Add(tx, a.x) tx.Add(tx, a.y) From 7d463aedd3ad1a88964a4286f443c712164543ec Mon Sep 17 00:00:00 2001 From: Daniel Liu <139250065@qq.com> Date: Mon, 13 Apr 2026 20:10:56 +0800 Subject: [PATCH 13/40] accounts/keystore: fix flaky TestUpdatedKeyfileContents (#34084) TestUpdatedKeyfileContents was intermittently failing with: - Emptying account file failed - wasn't notified of new accounts Root cause: waitForAccounts required the account list match and an immediately readable ks.changes notification in the same instant, creating a timing race between cache update visibility and channel delivery. This change keeps the same timeout window but waits until both conditions are observed, which preserves test intent while removing the flaky timing dependency. Validation: - go test ./accounts/keystore -run '^TestUpdatedKeyfileContents$' -count=100 --- accounts/keystore/account_cache_test.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/accounts/keystore/account_cache_test.go b/accounts/keystore/account_cache_test.go index c9a8cdfcef..e49b110e7e 100644 --- a/accounts/keystore/account_cache_test.go +++ b/accounts/keystore/account_cache_test.go @@ -68,18 +68,27 @@ func waitWatcherStart(ks *KeyStore) bool { func waitForAccounts(wantAccounts []accounts.Account, ks *KeyStore) error { var list []accounts.Account + haveAccounts := false + haveChange := false for t0 := time.Now(); time.Since(t0) < 5*time.Second; time.Sleep(100 * time.Millisecond) { - list = ks.Accounts() - if reflect.DeepEqual(list, wantAccounts) { - // ks should have also received change notifications + if !haveAccounts { + list = ks.Accounts() + haveAccounts = reflect.DeepEqual(list, wantAccounts) + } + if !haveChange { select { case <-ks.changes: + haveChange = true default: - return errors.New("wasn't notified of new accounts") } + } + if haveAccounts && haveChange { return nil } } + if haveAccounts { + return errors.New("wasn't notified of new accounts") + } return fmt.Errorf("\ngot %v\nwant %v", list, wantAccounts) } From 5b7511eeeda9a750214ea16b63d980dda6e8cd5e Mon Sep 17 00:00:00 2001 From: Daniel Liu <139250065@qq.com> Date: Mon, 13 Apr 2026 20:13:33 +0800 Subject: [PATCH 14/40] core/vm: include operand in error message (#34635) Return ErrInvalidOpCode with the executing opcode and offending immediate for forbidden DUPN, SWAPN, and EXCHANGE operands. Extend TestEIP8024_Execution to assert both opcode and operand for all invalid-immediate paths. --- core/vm/errors.go | 10 +++++-- core/vm/instructions.go | 9 ++++-- core/vm/instructions_test.go | 56 ++++++++++++++++++++++++++---------- 3 files changed, 55 insertions(+), 20 deletions(-) diff --git a/core/vm/errors.go b/core/vm/errors.go index e33c9fcb85..b6235d44a6 100644 --- a/core/vm/errors.go +++ b/core/vm/errors.go @@ -76,10 +76,16 @@ func (e ErrStackOverflow) Unwrap() error { // ErrInvalidOpCode wraps an evm error when an invalid opcode is encountered. type ErrInvalidOpCode struct { - opcode OpCode + opcode OpCode + operand *byte } -func (e *ErrInvalidOpCode) Error() string { return fmt.Sprintf("invalid opcode: %s", e.opcode) } +func (e *ErrInvalidOpCode) Error() string { + if e.operand != nil { + return fmt.Sprintf("invalid opcode: %s (operand: 0x%02x)", e.opcode, *e.operand) + } + return fmt.Sprintf("invalid opcode: %s", e.opcode) +} // rpcError is the same interface as the one defined in rpc/errors.go // but we do not want to depend on rpc package here so we redefine it. diff --git a/core/vm/instructions.go b/core/vm/instructions.go index 74400732ac..c3d1633c78 100644 --- a/core/vm/instructions.go +++ b/core/vm/instructions.go @@ -996,7 +996,8 @@ func opDupN(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { // This range is excluded to preserve compatibility with existing opcodes. if x > 90 && x < 128 { - return nil, &ErrInvalidOpCode{opcode: OpCode(x)} + operand := x + return nil, &ErrInvalidOpCode{opcode: DUPN, operand: &operand} } n := decodeSingle(x) @@ -1023,7 +1024,8 @@ func opSwapN(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { // This range is excluded to preserve compatibility with existing opcodes. if x > 90 && x < 128 { - return nil, &ErrInvalidOpCode{opcode: OpCode(x)} + operand := x + return nil, &ErrInvalidOpCode{opcode: SWAPN, operand: &operand} } n := decodeSingle(x) @@ -1053,7 +1055,8 @@ func opExchange(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) { // This range is excluded both to preserve compatibility with existing opcodes // and to keep decode_pair’s 16-aligned arithmetic mapping valid (0–81, 128–255). if x > 81 && x < 128 { - return nil, &ErrInvalidOpCode{opcode: OpCode(x)} + operand := x + return nil, &ErrInvalidOpCode{opcode: EXCHANGE, operand: &operand} } n, m := decodePair(x) need := max(n, m) + 1 diff --git a/core/vm/instructions_test.go b/core/vm/instructions_test.go index 1f69eea3da..5055ccb16d 100644 --- a/core/vm/instructions_test.go +++ b/core/vm/instructions_test.go @@ -1014,11 +1014,12 @@ func TestEIP8024_Execution(t *testing.T) { evm := NewEVM(BlockContext{}, nil, params.TestChainConfig, Config{}) tests := []struct { - name string - codeHex string - wantErr error - wantOpcode OpCode - wantVals []uint64 + name string + codeHex string + wantErr error + wantOpcode OpCode + wantOperand *byte + wantVals []uint64 }{ { name: "DUPN", @@ -1063,10 +1064,18 @@ func TestEIP8024_Execution(t *testing.T) { }, }, { - name: "INVALID_SWAPN_LOW", - codeHex: "e75b", - wantErr: &ErrInvalidOpCode{}, - wantOpcode: SWAPN, + name: "INVALID_DUPN_LOW", + codeHex: "e65b", + wantErr: &ErrInvalidOpCode{}, + wantOpcode: DUPN, + wantOperand: ptrToByte(0x5b), + }, + { + name: "INVALID_SWAPN_LOW", + codeHex: "e75b", + wantErr: &ErrInvalidOpCode{}, + wantOpcode: SWAPN, + wantOperand: ptrToByte(0x5b), }, { name: "JUMP_OVER_INVALID_DUPN", @@ -1079,10 +1088,11 @@ func TestEIP8024_Execution(t *testing.T) { wantVals: []uint64{1, 0, 0}, }, { - name: "INVALID_EXCHANGE", - codeHex: "e852", - wantErr: &ErrInvalidOpCode{}, - wantOpcode: EXCHANGE, + name: "INVALID_EXCHANGE", + codeHex: "e852", + wantErr: &ErrInvalidOpCode{}, + wantOpcode: EXCHANGE, + wantOperand: ptrToByte(0x52), }, { name: "UNDERFLOW_DUPN", @@ -1150,10 +1160,21 @@ func TestEIP8024_Execution(t *testing.T) { // Fail if we don't get the error we expect. switch tc.wantErr.(type) { case *ErrInvalidOpCode: - var want *ErrInvalidOpCode - if !errors.As(err, &want) { + var got *ErrInvalidOpCode + if !errors.As(err, &got) { t.Fatalf("expected ErrInvalidOpCode, got %v", err) } + if got.opcode != tc.wantOpcode { + t.Fatalf("ErrInvalidOpCode.opcode=%s; want %s", got.opcode, tc.wantOpcode) + } + if tc.wantOperand != nil { + if got.operand == nil { + t.Fatalf("ErrInvalidOpCode.operand=nil; want 0x%02x", *tc.wantOperand) + } + if *got.operand != *tc.wantOperand { + t.Fatalf("ErrInvalidOpCode.operand=0x%02x; want 0x%02x", *got.operand, *tc.wantOperand) + } + } case *ErrStackUnderflow: var want *ErrStackUnderflow if !errors.As(err, &want) { @@ -1183,3 +1204,8 @@ func TestEIP8024_Execution(t *testing.T) { }) } } + +func ptrToByte(v byte) *byte { + b := v + return &b +} From 289826fefbc9e36968807c37f9d5cd32a276fb12 Mon Sep 17 00:00:00 2001 From: Vadim Tertilov <97807449+rglKali@users.noreply.github.com> Date: Mon, 13 Apr 2026 14:42:34 +0200 Subject: [PATCH 15/40] cmd/abigen/v2: add package-level errors (#34076) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Summary Replaces the inline `errors.New("event signature mismatch")` in generated `UnpackXxxEvent` methods with per-event package-level sentinel errors (e.g. `ErrTransferSignatureMismatch`, `ErrApprovalSignatureMismatch`), allowing callers to reliably distinguish a topic mismatch from a genuine decoding failure via `errors.Is`. Each event gets its own sentinel, generated via the abigen template: ```go var ErrTransferSignatureMismatch = errors.New("event signature mismatch") ``` This scoping is intentional — it allows callers to be precise about *which* event was mismatched, which is useful when routing logs across multiple unpackers. # Motivation Previously, all errors returned from `UnpackXxxEvent` were indistinguishable without string matching. This is especially problematic when processing logs sourced from `eth_getBlockReceipts`, where a caller receives the full set of logs for a block across all contracts and event types. In that context, a signature mismatch is expected and should be skipped, while any other error (malformed data, topic parsing failure) indicates something is genuinely wrong and should halt execution: ```go for _, log := range blockLogs { event, err := contract.UnpackTransferEvent(log) if errors.Is(err, gen.ErrTransferSignatureMismatch) { continue // not our event, expected } if err != nil { return fmt.Errorf("unexpected decode failure: %w", err) // alert } // process event } ``` **Changes:** - `abigen` template: generates a `ErrXxxSignatureMismatch` sentinel per event and returns it on topic mismatch instead of an inline error - Existing generated bindings & testdata: regenerated to reflect the update Implements #34075 --- accounts/abi/abigen/source2.go.tpl | 7 ++-- .../abi/abigen/testdata/v2/crowdsale.go.txt | 7 ++-- accounts/abi/abigen/testdata/v2/dao.go.txt | 35 +++++++++++++------ .../abigen/testdata/v2/eventchecker.go.txt | 35 +++++++++++++------ .../abigen/testdata/v2/nameconflict.go.txt | 7 ++-- .../testdata/v2/numericmethodname.go.txt | 7 ++-- .../abi/abigen/testdata/v2/overload.go.txt | 14 +++++--- accounts/abi/abigen/testdata/v2/token.go.txt | 7 ++-- accounts/abi/abigen/testdata/v2/tuple.go.txt | 14 +++++--- accounts/abi/bind/v2/base.go | 12 +++---- .../bind/v2/internal/contracts/db/bindings.go | 14 +++++--- .../v2/internal/contracts/events/bindings.go | 14 +++++--- accounts/abi/bind/v2/lib_test.go | 8 ++--- 13 files changed, 125 insertions(+), 56 deletions(-) diff --git a/accounts/abi/abigen/source2.go.tpl b/accounts/abi/abigen/source2.go.tpl index 3d98cbb700..c517caf6f4 100644 --- a/accounts/abi/abigen/source2.go.tpl +++ b/accounts/abi/abigen/source2.go.tpl @@ -183,8 +183,11 @@ var ( // Solidity: {{.Original.String}} func ({{ decapitalise $contract.Type}} *{{$contract.Type}}) Unpack{{.Normalized.Name}}Event(log *types.Log) (*{{$contract.Type}}{{.Normalized.Name}}, error) { event := "{{.Original.Name}}" - if len(log.Topics) == 0 || log.Topics[0] != {{ decapitalise $contract.Type}}.abi.Events[event].ID { - return nil, errors.New("event signature mismatch") + if len(log.Topics) == 0 { + return nil, bind.ErrNoEventSignature + } + if log.Topics[0] != {{ decapitalise $contract.Type}}.abi.Events[event].ID { + return nil, bind.ErrEventSignatureMismatch } out := new({{$contract.Type}}{{.Normalized.Name}}) if len(log.Data) > 0 { diff --git a/accounts/abi/abigen/testdata/v2/crowdsale.go.txt b/accounts/abi/abigen/testdata/v2/crowdsale.go.txt index b548b6cdae..f0bba246ab 100644 --- a/accounts/abi/abigen/testdata/v2/crowdsale.go.txt +++ b/accounts/abi/abigen/testdata/v2/crowdsale.go.txt @@ -360,8 +360,11 @@ func (CrowdsaleFundTransfer) ContractEventName() string { // Solidity: event FundTransfer(address backer, uint256 amount, bool isContribution) func (crowdsale *Crowdsale) UnpackFundTransferEvent(log *types.Log) (*CrowdsaleFundTransfer, error) { event := "FundTransfer" - if len(log.Topics) == 0 || log.Topics[0] != crowdsale.abi.Events[event].ID { - return nil, errors.New("event signature mismatch") + if len(log.Topics) == 0 { + return nil, bind.ErrNoEventSignature + } + if log.Topics[0] != crowdsale.abi.Events[event].ID { + return nil, bind.ErrEventSignatureMismatch } out := new(CrowdsaleFundTransfer) if len(log.Data) > 0 { diff --git a/accounts/abi/abigen/testdata/v2/dao.go.txt b/accounts/abi/abigen/testdata/v2/dao.go.txt index c246771d6d..0e9adba31e 100644 --- a/accounts/abi/abigen/testdata/v2/dao.go.txt +++ b/accounts/abi/abigen/testdata/v2/dao.go.txt @@ -606,8 +606,11 @@ func (DAOChangeOfRules) ContractEventName() string { // Solidity: event ChangeOfRules(uint256 minimumQuorum, uint256 debatingPeriodInMinutes, int256 majorityMargin) func (dAO *DAO) UnpackChangeOfRulesEvent(log *types.Log) (*DAOChangeOfRules, error) { event := "ChangeOfRules" - if len(log.Topics) == 0 || log.Topics[0] != dAO.abi.Events[event].ID { - return nil, errors.New("event signature mismatch") + if len(log.Topics) == 0 { + return nil, bind.ErrNoEventSignature + } + if log.Topics[0] != dAO.abi.Events[event].ID { + return nil, bind.ErrEventSignatureMismatch } out := new(DAOChangeOfRules) if len(log.Data) > 0 { @@ -648,8 +651,11 @@ func (DAOMembershipChanged) ContractEventName() string { // Solidity: event MembershipChanged(address member, bool isMember) func (dAO *DAO) UnpackMembershipChangedEvent(log *types.Log) (*DAOMembershipChanged, error) { event := "MembershipChanged" - if len(log.Topics) == 0 || log.Topics[0] != dAO.abi.Events[event].ID { - return nil, errors.New("event signature mismatch") + if len(log.Topics) == 0 { + return nil, bind.ErrNoEventSignature + } + if log.Topics[0] != dAO.abi.Events[event].ID { + return nil, bind.ErrEventSignatureMismatch } out := new(DAOMembershipChanged) if len(log.Data) > 0 { @@ -692,8 +698,11 @@ func (DAOProposalAdded) ContractEventName() string { // Solidity: event ProposalAdded(uint256 proposalID, address recipient, uint256 amount, string description) func (dAO *DAO) UnpackProposalAddedEvent(log *types.Log) (*DAOProposalAdded, error) { event := "ProposalAdded" - if len(log.Topics) == 0 || log.Topics[0] != dAO.abi.Events[event].ID { - return nil, errors.New("event signature mismatch") + if len(log.Topics) == 0 { + return nil, bind.ErrNoEventSignature + } + if log.Topics[0] != dAO.abi.Events[event].ID { + return nil, bind.ErrEventSignatureMismatch } out := new(DAOProposalAdded) if len(log.Data) > 0 { @@ -736,8 +745,11 @@ func (DAOProposalTallied) ContractEventName() string { // Solidity: event ProposalTallied(uint256 proposalID, int256 result, uint256 quorum, bool active) func (dAO *DAO) UnpackProposalTalliedEvent(log *types.Log) (*DAOProposalTallied, error) { event := "ProposalTallied" - if len(log.Topics) == 0 || log.Topics[0] != dAO.abi.Events[event].ID { - return nil, errors.New("event signature mismatch") + if len(log.Topics) == 0 { + return nil, bind.ErrNoEventSignature + } + if log.Topics[0] != dAO.abi.Events[event].ID { + return nil, bind.ErrEventSignatureMismatch } out := new(DAOProposalTallied) if len(log.Data) > 0 { @@ -780,8 +792,11 @@ func (DAOVoted) ContractEventName() string { // Solidity: event Voted(uint256 proposalID, bool position, address voter, string justification) func (dAO *DAO) UnpackVotedEvent(log *types.Log) (*DAOVoted, error) { event := "Voted" - if len(log.Topics) == 0 || log.Topics[0] != dAO.abi.Events[event].ID { - return nil, errors.New("event signature mismatch") + if len(log.Topics) == 0 { + return nil, bind.ErrNoEventSignature + } + if log.Topics[0] != dAO.abi.Events[event].ID { + return nil, bind.ErrEventSignatureMismatch } out := new(DAOVoted) if len(log.Data) > 0 { diff --git a/accounts/abi/abigen/testdata/v2/eventchecker.go.txt b/accounts/abi/abigen/testdata/v2/eventchecker.go.txt index 8ad59e63b1..d0600d7c3e 100644 --- a/accounts/abi/abigen/testdata/v2/eventchecker.go.txt +++ b/accounts/abi/abigen/testdata/v2/eventchecker.go.txt @@ -72,8 +72,11 @@ func (EventCheckerDynamic) ContractEventName() string { // Solidity: event dynamic(string indexed idxStr, bytes indexed idxDat, string str, bytes dat) func (eventChecker *EventChecker) UnpackDynamicEvent(log *types.Log) (*EventCheckerDynamic, error) { event := "dynamic" - if len(log.Topics) == 0 || log.Topics[0] != eventChecker.abi.Events[event].ID { - return nil, errors.New("event signature mismatch") + if len(log.Topics) == 0 { + return nil, bind.ErrNoEventSignature + } + if log.Topics[0] != eventChecker.abi.Events[event].ID { + return nil, bind.ErrEventSignatureMismatch } out := new(EventCheckerDynamic) if len(log.Data) > 0 { @@ -112,8 +115,11 @@ func (EventCheckerEmpty) ContractEventName() string { // Solidity: event empty() func (eventChecker *EventChecker) UnpackEmptyEvent(log *types.Log) (*EventCheckerEmpty, error) { event := "empty" - if len(log.Topics) == 0 || log.Topics[0] != eventChecker.abi.Events[event].ID { - return nil, errors.New("event signature mismatch") + if len(log.Topics) == 0 { + return nil, bind.ErrNoEventSignature + } + if log.Topics[0] != eventChecker.abi.Events[event].ID { + return nil, bind.ErrEventSignatureMismatch } out := new(EventCheckerEmpty) if len(log.Data) > 0 { @@ -154,8 +160,11 @@ func (EventCheckerIndexed) ContractEventName() string { // Solidity: event indexed(address indexed addr, int256 indexed num) func (eventChecker *EventChecker) UnpackIndexedEvent(log *types.Log) (*EventCheckerIndexed, error) { event := "indexed" - if len(log.Topics) == 0 || log.Topics[0] != eventChecker.abi.Events[event].ID { - return nil, errors.New("event signature mismatch") + if len(log.Topics) == 0 { + return nil, bind.ErrNoEventSignature + } + if log.Topics[0] != eventChecker.abi.Events[event].ID { + return nil, bind.ErrEventSignatureMismatch } out := new(EventCheckerIndexed) if len(log.Data) > 0 { @@ -196,8 +205,11 @@ func (EventCheckerMixed) ContractEventName() string { // Solidity: event mixed(address indexed addr, int256 num) func (eventChecker *EventChecker) UnpackMixedEvent(log *types.Log) (*EventCheckerMixed, error) { event := "mixed" - if len(log.Topics) == 0 || log.Topics[0] != eventChecker.abi.Events[event].ID { - return nil, errors.New("event signature mismatch") + if len(log.Topics) == 0 { + return nil, bind.ErrNoEventSignature + } + if log.Topics[0] != eventChecker.abi.Events[event].ID { + return nil, bind.ErrEventSignatureMismatch } out := new(EventCheckerMixed) if len(log.Data) > 0 { @@ -238,8 +250,11 @@ func (EventCheckerUnnamed) ContractEventName() string { // Solidity: event unnamed(uint256 indexed arg0, uint256 indexed arg1) func (eventChecker *EventChecker) UnpackUnnamedEvent(log *types.Log) (*EventCheckerUnnamed, error) { event := "unnamed" - if len(log.Topics) == 0 || log.Topics[0] != eventChecker.abi.Events[event].ID { - return nil, errors.New("event signature mismatch") + if len(log.Topics) == 0 { + return nil, bind.ErrNoEventSignature + } + if log.Topics[0] != eventChecker.abi.Events[event].ID { + return nil, bind.ErrEventSignatureMismatch } out := new(EventCheckerUnnamed) if len(log.Data) > 0 { diff --git a/accounts/abi/abigen/testdata/v2/nameconflict.go.txt b/accounts/abi/abigen/testdata/v2/nameconflict.go.txt index 3fbabee5a5..5e4a9ecaf0 100644 --- a/accounts/abi/abigen/testdata/v2/nameconflict.go.txt +++ b/accounts/abi/abigen/testdata/v2/nameconflict.go.txt @@ -134,8 +134,11 @@ func (NameConflictLog) ContractEventName() string { // Solidity: event log(int256 msg, int256 _msg) func (nameConflict *NameConflict) UnpackLogEvent(log *types.Log) (*NameConflictLog, error) { event := "log" - if len(log.Topics) == 0 || log.Topics[0] != nameConflict.abi.Events[event].ID { - return nil, errors.New("event signature mismatch") + if len(log.Topics) == 0 { + return nil, bind.ErrNoEventSignature + } + if log.Topics[0] != nameConflict.abi.Events[event].ID { + return nil, bind.ErrEventSignatureMismatch } out := new(NameConflictLog) if len(log.Data) > 0 { diff --git a/accounts/abi/abigen/testdata/v2/numericmethodname.go.txt b/accounts/abi/abigen/testdata/v2/numericmethodname.go.txt index d962583e48..0af31a1cfb 100644 --- a/accounts/abi/abigen/testdata/v2/numericmethodname.go.txt +++ b/accounts/abi/abigen/testdata/v2/numericmethodname.go.txt @@ -136,8 +136,11 @@ func (NumericMethodNameE1TestEvent) ContractEventName() string { // Solidity: event _1TestEvent(address _param) func (numericMethodName *NumericMethodName) UnpackE1TestEventEvent(log *types.Log) (*NumericMethodNameE1TestEvent, error) { event := "_1TestEvent" - if len(log.Topics) == 0 || log.Topics[0] != numericMethodName.abi.Events[event].ID { - return nil, errors.New("event signature mismatch") + if len(log.Topics) == 0 { + return nil, bind.ErrNoEventSignature + } + if log.Topics[0] != numericMethodName.abi.Events[event].ID { + return nil, bind.ErrEventSignatureMismatch } out := new(NumericMethodNameE1TestEvent) if len(log.Data) > 0 { diff --git a/accounts/abi/abigen/testdata/v2/overload.go.txt b/accounts/abi/abigen/testdata/v2/overload.go.txt index ddddd10186..563edf7842 100644 --- a/accounts/abi/abigen/testdata/v2/overload.go.txt +++ b/accounts/abi/abigen/testdata/v2/overload.go.txt @@ -114,8 +114,11 @@ func (OverloadBar) ContractEventName() string { // Solidity: event bar(uint256 i) func (overload *Overload) UnpackBarEvent(log *types.Log) (*OverloadBar, error) { event := "bar" - if len(log.Topics) == 0 || log.Topics[0] != overload.abi.Events[event].ID { - return nil, errors.New("event signature mismatch") + if len(log.Topics) == 0 { + return nil, bind.ErrNoEventSignature + } + if log.Topics[0] != overload.abi.Events[event].ID { + return nil, bind.ErrEventSignatureMismatch } out := new(OverloadBar) if len(log.Data) > 0 { @@ -156,8 +159,11 @@ func (OverloadBar0) ContractEventName() string { // Solidity: event bar(uint256 i, uint256 j) func (overload *Overload) UnpackBar0Event(log *types.Log) (*OverloadBar0, error) { event := "bar0" - if len(log.Topics) == 0 || log.Topics[0] != overload.abi.Events[event].ID { - return nil, errors.New("event signature mismatch") + if len(log.Topics) == 0 { + return nil, bind.ErrNoEventSignature + } + if log.Topics[0] != overload.abi.Events[event].ID { + return nil, bind.ErrEventSignatureMismatch } out := new(OverloadBar0) if len(log.Data) > 0 { diff --git a/accounts/abi/abigen/testdata/v2/token.go.txt b/accounts/abi/abigen/testdata/v2/token.go.txt index 6ebc96861b..3bd60a6cdd 100644 --- a/accounts/abi/abigen/testdata/v2/token.go.txt +++ b/accounts/abi/abigen/testdata/v2/token.go.txt @@ -386,8 +386,11 @@ func (TokenTransfer) ContractEventName() string { // Solidity: event Transfer(address indexed from, address indexed to, uint256 value) func (token *Token) UnpackTransferEvent(log *types.Log) (*TokenTransfer, error) { event := "Transfer" - if len(log.Topics) == 0 || log.Topics[0] != token.abi.Events[event].ID { - return nil, errors.New("event signature mismatch") + if len(log.Topics) == 0 { + return nil, bind.ErrNoEventSignature + } + if log.Topics[0] != token.abi.Events[event].ID { + return nil, bind.ErrEventSignatureMismatch } out := new(TokenTransfer) if len(log.Data) > 0 { diff --git a/accounts/abi/abigen/testdata/v2/tuple.go.txt b/accounts/abi/abigen/testdata/v2/tuple.go.txt index 4724fdd351..10b634f3db 100644 --- a/accounts/abi/abigen/testdata/v2/tuple.go.txt +++ b/accounts/abi/abigen/testdata/v2/tuple.go.txt @@ -193,8 +193,11 @@ func (TupleTupleEvent) ContractEventName() string { // Solidity: event TupleEvent((uint256,uint256[],(uint256,uint256)[]) a, (uint256,uint256)[2][] b, (uint256,uint256)[][2] c, (uint256,uint256[],(uint256,uint256)[])[] d, uint256[] e) func (tuple *Tuple) UnpackTupleEventEvent(log *types.Log) (*TupleTupleEvent, error) { event := "TupleEvent" - if len(log.Topics) == 0 || log.Topics[0] != tuple.abi.Events[event].ID { - return nil, errors.New("event signature mismatch") + if len(log.Topics) == 0 { + return nil, bind.ErrNoEventSignature + } + if log.Topics[0] != tuple.abi.Events[event].ID { + return nil, bind.ErrEventSignatureMismatch } out := new(TupleTupleEvent) if len(log.Data) > 0 { @@ -234,8 +237,11 @@ func (TupleTupleEvent2) ContractEventName() string { // Solidity: event TupleEvent2((uint8,uint8)[] arg0) func (tuple *Tuple) UnpackTupleEvent2Event(log *types.Log) (*TupleTupleEvent2, error) { event := "TupleEvent2" - if len(log.Topics) == 0 || log.Topics[0] != tuple.abi.Events[event].ID { - return nil, errors.New("event signature mismatch") + if len(log.Topics) == 0 { + return nil, bind.ErrNoEventSignature + } + if log.Topics[0] != tuple.abi.Events[event].ID { + return nil, bind.ErrEventSignatureMismatch } out := new(TupleTupleEvent2) if len(log.Data) > 0 { diff --git a/accounts/abi/bind/v2/base.go b/accounts/abi/bind/v2/base.go index 4f2013b4a3..862175ee32 100644 --- a/accounts/abi/bind/v2/base.go +++ b/accounts/abi/bind/v2/base.go @@ -35,8 +35,8 @@ import ( const basefeeWiggleMultiplier = 2 var ( - errNoEventSignature = errors.New("no event signature") - errEventSignatureMismatch = errors.New("event signature mismatch") + ErrNoEventSignature = errors.New("no event signature") + ErrEventSignatureMismatch = errors.New("event signature mismatch") ) // SignerFn is a signer function callback when a contract requires a method to @@ -536,10 +536,10 @@ func (c *BoundContract) WatchLogs(opts *WatchOpts, name string, query ...[]any) func (c *BoundContract) UnpackLog(out any, event string, log types.Log) error { // Anonymous events are not supported. if len(log.Topics) == 0 { - return errNoEventSignature + return ErrNoEventSignature } if log.Topics[0] != c.abi.Events[event].ID { - return errEventSignatureMismatch + return ErrEventSignatureMismatch } if len(log.Data) > 0 { if err := c.abi.UnpackIntoInterface(out, event, log.Data); err != nil { @@ -559,10 +559,10 @@ func (c *BoundContract) UnpackLog(out any, event string, log types.Log) error { func (c *BoundContract) UnpackLogIntoMap(out map[string]any, event string, log types.Log) error { // Anonymous events are not supported. if len(log.Topics) == 0 { - return errNoEventSignature + return ErrNoEventSignature } if log.Topics[0] != c.abi.Events[event].ID { - return errEventSignatureMismatch + return ErrEventSignatureMismatch } if len(log.Data) > 0 { if err := c.abi.UnpackIntoMap(out, event, log.Data); err != nil { diff --git a/accounts/abi/bind/v2/internal/contracts/db/bindings.go b/accounts/abi/bind/v2/internal/contracts/db/bindings.go index 4ac1652ff7..2fc57fba6d 100644 --- a/accounts/abi/bind/v2/internal/contracts/db/bindings.go +++ b/accounts/abi/bind/v2/internal/contracts/db/bindings.go @@ -276,8 +276,11 @@ func (DBInsert) ContractEventName() string { // Solidity: event Insert(uint256 key, uint256 value, uint256 length) func (dB *DB) UnpackInsertEvent(log *types.Log) (*DBInsert, error) { event := "Insert" - if len(log.Topics) == 0 || log.Topics[0] != dB.abi.Events[event].ID { - return nil, errors.New("event signature mismatch") + if len(log.Topics) == 0 { + return nil, bind.ErrNoEventSignature + } + if log.Topics[0] != dB.abi.Events[event].ID { + return nil, bind.ErrEventSignatureMismatch } out := new(DBInsert) if len(log.Data) > 0 { @@ -318,8 +321,11 @@ func (DBKeyedInsert) ContractEventName() string { // Solidity: event KeyedInsert(uint256 indexed key, uint256 value) func (dB *DB) UnpackKeyedInsertEvent(log *types.Log) (*DBKeyedInsert, error) { event := "KeyedInsert" - if len(log.Topics) == 0 || log.Topics[0] != dB.abi.Events[event].ID { - return nil, errors.New("event signature mismatch") + if len(log.Topics) == 0 { + return nil, bind.ErrNoEventSignature + } + if log.Topics[0] != dB.abi.Events[event].ID { + return nil, bind.ErrEventSignatureMismatch } out := new(DBKeyedInsert) if len(log.Data) > 0 { diff --git a/accounts/abi/bind/v2/internal/contracts/events/bindings.go b/accounts/abi/bind/v2/internal/contracts/events/bindings.go index 40d2c44a44..2eb5751f23 100644 --- a/accounts/abi/bind/v2/internal/contracts/events/bindings.go +++ b/accounts/abi/bind/v2/internal/contracts/events/bindings.go @@ -115,8 +115,11 @@ func (CBasic1) ContractEventName() string { // Solidity: event basic1(uint256 indexed id, uint256 data) func (c *C) UnpackBasic1Event(log *types.Log) (*CBasic1, error) { event := "basic1" - if len(log.Topics) == 0 || log.Topics[0] != c.abi.Events[event].ID { - return nil, errors.New("event signature mismatch") + if len(log.Topics) == 0 { + return nil, bind.ErrNoEventSignature + } + if log.Topics[0] != c.abi.Events[event].ID { + return nil, bind.ErrEventSignatureMismatch } out := new(CBasic1) if len(log.Data) > 0 { @@ -157,8 +160,11 @@ func (CBasic2) ContractEventName() string { // Solidity: event basic2(bool indexed flag, uint256 data) func (c *C) UnpackBasic2Event(log *types.Log) (*CBasic2, error) { event := "basic2" - if len(log.Topics) == 0 || log.Topics[0] != c.abi.Events[event].ID { - return nil, errors.New("event signature mismatch") + if len(log.Topics) == 0 { + return nil, bind.ErrNoEventSignature + } + if log.Topics[0] != c.abi.Events[event].ID { + return nil, bind.ErrEventSignatureMismatch } out := new(CBasic2) if len(log.Data) > 0 { diff --git a/accounts/abi/bind/v2/lib_test.go b/accounts/abi/bind/v2/lib_test.go index 11360fc7dd..38dfa74dfa 100644 --- a/accounts/abi/bind/v2/lib_test.go +++ b/accounts/abi/bind/v2/lib_test.go @@ -379,16 +379,16 @@ func TestEventUnpackEmptyTopics(t *testing.T) { if err == nil { t.Fatal("expected error when unpacking event with empty topics, got nil") } - if err.Error() != "event signature mismatch" { - t.Fatalf("expected 'event signature mismatch' error, got: %v", err) + if err != bind.ErrNoEventSignature { + t.Fatalf("expected 'no event signature' error, got: %v", err) } _, err = c.UnpackBasic2Event(log) if err == nil { t.Fatal("expected error when unpacking event with empty topics, got nil") } - if err.Error() != "event signature mismatch" { - t.Fatalf("expected 'event signature mismatch' error, got: %v", err) + if err != bind.ErrNoEventSignature { + t.Fatalf("expected 'no event signature' error, got: %v", err) } } } From 4da1e29320a75cd22accf835b26a696bae2bad68 Mon Sep 17 00:00:00 2001 From: Conor Patrick Date: Mon, 13 Apr 2026 09:30:36 -0400 Subject: [PATCH 16/40] signer/core/apitypes: fix encoding of opening parenthesis (#33702) This fixes a truncation bug that results in an invalid serialization of empty EIP712. For example: ```json { "method": "eth_signTypedData_v4", "request": { "types": { "EIP712Domain": [ { "name": "version", "type": "string" } ], "Empty": [] }, "primaryType": "Empty", "domain": { "version": "0" }, "message": {} } } ``` When calculating the type-hash for the stuct-hash, it will incorrectly use `Empty)` instead of `Empty()` --- signer/core/apitypes/types.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/signer/core/apitypes/types.go b/signer/core/apitypes/types.go index 401f4fba07..ee12bb263e 100644 --- a/signer/core/apitypes/types.go +++ b/signer/core/apitypes/types.go @@ -454,7 +454,9 @@ func (typedData *TypedData) EncodeType(primaryType string) hexutil.Bytes { buffer.WriteString(obj.Name) buffer.WriteString(",") } - buffer.Truncate(buffer.Len() - 1) + if len(typedData.Types[dep]) > 0 { + buffer.Truncate(buffer.Len() - 1) + } buffer.WriteString(")") } return buffer.Bytes() From 527ea11e501c5eadfd3e465f490333f8c2de999c Mon Sep 17 00:00:00 2001 From: phrwlk Date: Mon, 13 Apr 2026 16:46:13 +0300 Subject: [PATCH 17/40] core/vm/runtime: don't overwrite user input with default value (#33510) runtime.setDefaults was unconditionally assigning cfg.Random = &common.Hash{}, which silently overwrote any caller-provided Random value. This made it impossible to simulate a specific PREVRANDAO and also forced post-merge rules whenever London was active, regardless of the intended environment. This change only initializes cfg.Random when it is nil, matching how other fields in Config are defaulted. Existing callers that did not set Random keep the same behavior (a non-nil zero hash still enables post-merge semantics), while callers that explicitly set Random now get their value respected. --- core/vm/runtime/runtime.go | 4 +++- core/vm/runtime/runtime_test.go | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/core/vm/runtime/runtime.go b/core/vm/runtime/runtime.go index b40e99d047..af394aa054 100644 --- a/core/vm/runtime/runtime.go +++ b/core/vm/runtime/runtime.go @@ -109,7 +109,9 @@ func setDefaults(cfg *Config) { if cfg.BlobBaseFee == nil { cfg.BlobBaseFee = big.NewInt(params.BlobTxMinBlobGasprice) } - cfg.Random = &(common.Hash{}) + if cfg.Random == nil { + cfg.Random = new(common.Hash) + } } // Execute executes the code using the input as call data during the execution. diff --git a/core/vm/runtime/runtime_test.go b/core/vm/runtime/runtime_test.go index a001d81623..40fb770454 100644 --- a/core/vm/runtime/runtime_test.go +++ b/core/vm/runtime/runtime_test.go @@ -65,6 +65,21 @@ func TestDefaults(t *testing.T) { if cfg.BlockNumber == nil { t.Error("expected block number to be non nil") } + if cfg.Random == nil { + t.Error("expected Random to be non nil") + } +} + +func TestDefaultsPreserveRandom(t *testing.T) { + h := common.HexToHash("0x01") + cfg := &Config{Random: &h} + setDefaults(cfg) + if cfg.Random == nil { + t.Fatal("expected Random to remain non-nil") + } + if *cfg.Random != h { + t.Fatalf("expected Random to be preserved, got %x, want %x", *cfg.Random, h) + } } func TestEVM(t *testing.T) { From 01e33d14be264178c4fce4776ecd16f155e94ae2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marius=20Kj=C3=A6rstad?= Date: Mon, 13 Apr 2026 16:32:40 +0200 Subject: [PATCH 18/40] build: upgrade -dlgo version to Go 1.25.9 (#34707) --- build/checksums.txt | 84 ++++++++++++++++++++++----------------------- 1 file changed, 42 insertions(+), 42 deletions(-) diff --git a/build/checksums.txt b/build/checksums.txt index ba80a3e201..1832ce41dd 100644 --- a/build/checksums.txt +++ b/build/checksums.txt @@ -5,49 +5,49 @@ # https://github.com/ethereum/execution-spec-tests/releases/download/v5.1.0 a3192784375acec7eaec492799d5c5d0c47a2909a3cc40178898e4ecd20cc416 fixtures_develop.tar.gz -# version:golang 1.25.7 +# version:golang 1.25.9 # https://go.dev/dl/ -178f2832820274b43e177d32f06a3ebb0129e427dd20a5e4c88df2c1763cf10a go1.25.7.src.tar.gz -81bf2a1f20633f62d55d826d82dde3b0570cf1408a91e15781b266037299285b go1.25.7.aix-ppc64.tar.gz -bf5050a2152f4053837b886e8d9640c829dbacbc3370f913351eb0904cb706f5 go1.25.7.darwin-amd64.tar.gz -ff18369ffad05c57d5bed888b660b31385f3c913670a83ef557cdfd98ea9ae1b go1.25.7.darwin-arm64.tar.gz -c5dccd7f192dd7b305dc209fb316ac1917776d74bd8e4d532ef2772f305bf42a go1.25.7.dragonfly-amd64.tar.gz -a2de97c8ac74bf64b0ae73fe9d379e61af530e061bc7f8f825044172ffe61a8b go1.25.7.freebsd-386.tar.gz -055f9e138787dcafa81eb0314c8ff70c6dd0f6dba1e8a6957fef5d5efd1ab8fd go1.25.7.freebsd-amd64.tar.gz -60e7f7a7c990f0b9539ac8ed668155746997d404643a4eecd47b3dee1b7e710b go1.25.7.freebsd-arm.tar.gz -631e03d5fd4c526e2f499154d8c6bf4cb081afb2fff171c428722afc9539d53a go1.25.7.freebsd-arm64.tar.gz -8a264fd685823808140672812e3ad9c43f6ad59444c0dc14cdd3a1351839ddd5 go1.25.7.freebsd-riscv64.tar.gz -57c672447d906a1bcab98f2b11492d54521a791aacbb4994a25169e59cbe289a go1.25.7.illumos-amd64.tar.gz -2866517e9ca81e6a2e85a930e9b11bc8a05cfeb2fc6dc6cb2765e7fb3c14b715 go1.25.7.linux-386.tar.gz -12e6d6a191091ae27dc31f6efc630e3a3b8ba409baf3573d955b196fdf086005 go1.25.7.linux-amd64.tar.gz -ba611a53534135a81067240eff9508cd7e256c560edd5d8c2fef54f083c07129 go1.25.7.linux-arm64.tar.gz -1ba07e0eb86b839e72467f4b5c7a5597d07f30bcf5563c951410454f7cda5266 go1.25.7.linux-armv6l.tar.gz -775753fc5952a334c415f08768df2f0b73a3228a16e8f5f63d545daacb4e3357 go1.25.7.linux-loong64.tar.gz -1a023bb367c5fbb4c637a2f6dc23ff17c6591ad929ce16ea88c74d857153b307 go1.25.7.linux-mips.tar.gz -a8e97223d8aa6fdfd45f132a4784d2f536bbac5f3d63a24b63d33b6bfe1549af go1.25.7.linux-mips64.tar.gz -eb9edb6223330d5e20275667c65dea076b064c08e595fe4eba5d7d6055cfaccf go1.25.7.linux-mips64le.tar.gz -9c1e693552a5f9bb9e0012d1c5e01456ecefbc59bef53a77305222ce10aba368 go1.25.7.linux-mipsle.tar.gz -28a788798e7329acbbc0ac2caa5e4368b1e5ede646cc24429c991214cfb45c63 go1.25.7.linux-ppc64.tar.gz -42124c0edc92464e2b37b2d7fcd3658f0c47ebd6a098732415a522be8cb88e3f go1.25.7.linux-ppc64le.tar.gz -88d59c6893c8425875d6eef8e3434bc2fa2552e5ad4c058c6cd8cd710a0301c8 go1.25.7.linux-riscv64.tar.gz -c6b77facf666dc68195ecab05dbf0ebb4e755b2a8b7734c759880557f1c29b0c go1.25.7.linux-s390x.tar.gz -f14c184d9ade0ee04c7735d4071257b90896ecbde1b32adae84135f055e6399b go1.25.7.netbsd-386.tar.gz -7e7389e404dca1088c31f0fc07f1dd60891d7182bcd621469c14f7e79eceb3ff go1.25.7.netbsd-amd64.tar.gz -70388bb3ef2f03dbf1357e9056bd09034a67e018262557354f8cf549766b3f9d go1.25.7.netbsd-arm.tar.gz -8c1cda9d25bfc9b18d24d5f95fc23949dd3ff99fa408a6cfa40e2cf12b07e362 go1.25.7.netbsd-arm64.tar.gz -42f0d1bfbe39b8401cccb84dd66b30795b97bfc9620dfdc17c5cd4fcf6495cb0 go1.25.7.openbsd-386.tar.gz -e514879c0a28bc32123cd52c4c093de912477fe83f36a6d07517d066ef55391a go1.25.7.openbsd-amd64.tar.gz -8cd22530695a0218232bf7efea8f162df1697a3106942ac4129b8c3de39ce4ef go1.25.7.openbsd-arm.tar.gz -938720f6ebc0d1c53d7840321d3a31f29fd02496e84a6538f442a9311dc1cc9a go1.25.7.openbsd-arm64.tar.gz -a4c378b73b98f89a3596c2ef51aabbb28783d9ca29f7e317d8ca07939660ce6f go1.25.7.openbsd-ppc64.tar.gz -937b58734fbeaa8c7941a0e4285e7e84b7885396e8d11c23f9ab1a8ff10ff20e go1.25.7.openbsd-riscv64.tar.gz -61a093c8c5244916f25740316386bb9f141545dcf01b06a79d1c78ece488403e go1.25.7.plan9-386.tar.gz -7fc8f6689c9de8ccb7689d2278035fa83c2d601409101840df6ddfe09ba58699 go1.25.7.plan9-amd64.tar.gz -9661dff8eaeeb62f1c3aadbc5ff189a2e6744e1ec885e32dbcb438f58a34def5 go1.25.7.plan9-arm.tar.gz -28ecba0e1d7950c8b29a4a04962dd49c3bf5221f55a44f17d98f369f82859cf4 go1.25.7.solaris-amd64.tar.gz -baa6b488291801642fa620026169e38bec2da2ac187cd3ae2145721cf826bbc3 go1.25.7.windows-386.zip -c75e5f4ff62d085cc0017be3ad19d5536f46825fa05db06ec468941f847e3228 go1.25.7.windows-amd64.zip -807033f85931bc4a589ca8497535dcbeb1f30d506e47fa200f5f04c4a71c3d9f go1.25.7.windows-arm64.zip +0ec9ef8ebcea097aac37decae9f09a7218b451cd96be7d6ed513d8e4bcf909cf go1.25.9.src.tar.gz +b9ede6378a8f8d3d22bf52e68beb69ef7abdb65929ab2456020383002da15846 go1.25.9.aix-ppc64.tar.gz +92cb78fba4796e218c1accb0ea0a214ef2094c382049a244ad6505505d015fbe go1.25.9.darwin-amd64.tar.gz +9528be7329b9770631a6bd09ca2f3a73ed7332bec01d87435e75e92d8f130363 go1.25.9.darwin-arm64.tar.gz +918e44a471c5524caa52f74185064240d5eb343aa8023d604776511fc7adffa6 go1.25.9.dragonfly-amd64.tar.gz +2d67dbdfd09c6fcaa0e64485367ef43b8837ea200c663d6417183237bcddf83d go1.25.9.freebsd-386.tar.gz +9152d0c0badbfeb0c0e148e47c12bec28099d8cf2db60958810c879e0b679d07 go1.25.9.freebsd-amd64.tar.gz +437dca59604ad4a806a6a88e3d7ec1cd98ac9b402a3671629f4e553dd8b9888f go1.25.9.freebsd-arm.tar.gz +4c0fe53977412036fc8081e8d0992bbaabe4d3e1926137271ba11c2f5753300f go1.25.9.freebsd-arm64.tar.gz +d6087cdd1c084bd186132f29e0d032852a745f3c7619003d0fd5612c1fa58c8a go1.25.9.freebsd-riscv64.tar.gz +f82e49037e195cb62beae6a6ad83497157b2af5a01bad2f1dcb65df41080aabb go1.25.9.illumos-amd64.tar.gz +1e14a73bc2b19e370e0d4c57ba87aabfe8aef1e435e14d246742d48a13254f36 go1.25.9.linux-386.tar.gz +00859d7bd6defe8bf84d9db9e57b9a4467b2887c18cd93ae7460e713db774bc1 go1.25.9.linux-amd64.tar.gz +ec342e7389b7f489564ed5463c63b16cf8040023dabc7861256677165a8c0e2b go1.25.9.linux-arm64.tar.gz +7d4f0d266d871301e08ef4ac31c56e66048688893b2848392e5c600276351ee8 go1.25.9.linux-armv6l.tar.gz +f3460d901a14496bc609636e4accf9110ee1869d41c64af7e29cd567cffcf49b go1.25.9.linux-loong64.tar.gz +1da96ea449382ff96c09c55cee74815324e01d687d5ac6d2ade58244b8574306 go1.25.9.linux-mips.tar.gz +311a7f5f01f9a4bd51288b575eb619dc8e28e1fbc0cd78256a428b3ca668ff01 go1.25.9.linux-mips64.tar.gz +0b4edaf9e2ba3f0a079547effda70ec6a4b51a6ca3271a1147652c87ebcf3735 go1.25.9.linux-mips64le.tar.gz +42667340df264896f20b12261429d954e736e9772ab83ba289e68c30cf6f9628 go1.25.9.linux-mipsle.tar.gz +b9cbb3a4894b5aca6966c23452608435e8535278ef019b18d8898fbbfab67e74 go1.25.9.linux-ppc64.tar.gz +b0c41c7da1fc8d39020d65296a0dc54167afd9f76d67064e22c31ce3d839a739 go1.25.9.linux-ppc64le.tar.gz +2a630be8f854177c13e5fa75f7812c721369ecb9bd6e4c0fb1bd1c708d08b37c go1.25.9.linux-riscv64.tar.gz +0cf55136ac7eaccfc36d849054f849510ea289c2d959ffbed7b3866b4f484d17 go1.25.9.linux-s390x.tar.gz +eaf8167ff10a6a3e5dd304ef5f2e020b3a7379e76fa1011dc49c895800bf367c go1.25.9.netbsd-386.tar.gz +3cc6a861e62e23feae660984e0f2f14a2efb5d1f655900afee1d51af98919ae4 go1.25.9.netbsd-amd64.tar.gz +c2c44dca10e882c30553f4aa2ab8f6722b670fb12882378c8f461a9105d40188 go1.25.9.netbsd-arm.tar.gz +f301b71a8ec448053a5d2597df2e178120204bc9a33266c81600dd5d020a61b4 go1.25.9.netbsd-arm64.tar.gz +c4543b7fdef9707b4896810c69b4160a43ecec210af45c300f3abd78aa0c9e72 go1.25.9.openbsd-386.tar.gz +37275325e314f5ab7cf8ae65c4efc7cbfdaf20b41c6849549739b57a3ac97544 go1.25.9.openbsd-amd64.tar.gz +f9c05b6b315e979ecdd47354dd287c01708d6a88dc6ae7af74c84df8fa00df94 go1.25.9.openbsd-arm.tar.gz +4e999f42cf959ff95ca84af1ea1db3771000f5e57e157904bc2ffc72c75e29a2 go1.25.9.openbsd-arm64.tar.gz +0c7fa6c7c2b1cc13ad32fa94fc31273b4adf39c1e0f0e5dcedac158ff526af3f go1.25.9.openbsd-ppc64.tar.gz +347b33953a4b6e8df17719296f360f60878fe48a2d482ceb3637a3dfd4950065 go1.25.9.openbsd-riscv64.tar.gz +889f77d567c06832e0d332fe2458653dc66d43cded7ddbca6f72ce0ca60029cc go1.25.9.plan9-386.tar.gz +978b1f931fadec2f2516237d2649ee845d93c8eaf47dd196cfd8d26c7b2706a1 go1.25.9.plan9-amd64.tar.gz +30b9565e5ad0a212fe00990ead700c751b416eb2ef8d7c91a204945a7ff83a48 go1.25.9.plan9-arm.tar.gz +9e9125ff84ab3c3522ec758cab9540a17e9cba12bfcc34b6bf556cb89b522591 go1.25.9.solaris-amd64.tar.gz +bf40515f5f4d834fa9ead31ff75581e61a38ac27bf49840b95c5c998d321c0f6 go1.25.9.windows-386.zip +a7a710e225467b34e9e09fb432b829c86c9b2da5821ee5418f7eb2e8ae1a22cc go1.25.9.windows-amd64.zip +33cd73cf1b3ceee655ef71bc96e94006c02ae3c617fdd67ac9be3dfae3957449 go1.25.9.windows-arm64.zip # version:golangci 2.10.1 # https://github.com/golangci/golangci-lint/releases/ From e1fe4a1a98e531ded82805dd1724b189b3f22037 Mon Sep 17 00:00:00 2001 From: Charles Dusek <38732970+cgdusek@users.noreply.github.com> Date: Tue, 14 Apr 2026 02:43:44 -0500 Subject: [PATCH 19/40] p2p/discover: fix flaky TestUDPv5_findnodeHandling (#34109) Fixes #34108 The UDPv5 test harness (`newUDPV5Test`) uses the default `PingInterval` of 3 seconds. When tests like `TestUDPv5_findnodeHandling` insert nodes into the routing table via `fillTable`, the table's revalidation loop may schedule PING packets for those nodes. Under the race detector or on slow CI runners, the test runs long enough for revalidation to fire, causing background pings to be written to the test pipe. The `close()` method then finds these as unmatched packets and fails. The fix sets `PingInterval` to a very large value in the test harness so revalidation never fires during tests. Verified locally: 100 iterations with `-race -count=100` pass reliably, where previously the test would fail within ~50 iterations. --- p2p/discover/v5_udp_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/p2p/discover/v5_udp_test.go b/p2p/discover/v5_udp_test.go index 9429fbaf0a..1c41941c70 100644 --- a/p2p/discover/v5_udp_test.go +++ b/p2p/discover/v5_udp_test.go @@ -937,6 +937,7 @@ func newUDPV5Test(t *testing.T) *udpV5Test { PrivateKey: test.localkey, Log: testlog.Logger(t, log.LvlTrace), ValidSchemes: enode.ValidSchemesForTesting, + PingInterval: 1000 * time.Hour, }) test.udp.codec = &testCodec{test: test, id: ln.ID()} test.table = test.udp.tab From c453b99a57abdce283864ee29f1c90a10e7c887a Mon Sep 17 00:00:00 2001 From: Csaba Kiraly Date: Tue, 14 Apr 2026 12:15:39 +0200 Subject: [PATCH 20/40] cmd/devp2p/internal/v5test: fix hive test for discv5 findnode results (#34043) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This fixes the remaining Hive discv5/FindnodeResults failures in the cmd/devp2p/internal/v5test fixture. The issue was in the simulator-side bystander behavior, not in production discovery logic. The existing fixture could get bystanders inserted into the remote table, but under current geth behavior they were not stable enough to remain valid FINDNODE results. In particular, the fixture still had a few protocol/behavior mismatches: - incomplete WHOAREYOU recovery - replies not consistently following the UDP envelope source - incorrect endpoint echoing in PONG - fixture-originated PING using the wrong ENR sequence - bystanders answering background FINDNODE with empty NODES That last point was important because current lookup accounting can treat repeatedly unhelpful FINDNODE interactions as failures. As a result, a bystander could become live via PING/PONG and still later be dropped from the table before the final FindnodeResults assertion. This change updates the fixture so that bystanders behave more like stable discv5 peers: - perform one explicit initial handshake, then switch to passive response handling - resend the exact challenged packet when handling WHOAREYOU - reply to the actual UDP packet source and mirror that source in PONG.ToIP / PONG.ToPort - use the bystander’s own ENR sequence in fixture-originated PING - prefill each bystander with the bystander ENR set and answer FINDNODE from that set The result is that the fixture now forms a small self-consistent lookup environment instead of a set of peers that are live but systematically poor lookup participants. --- cmd/devp2p/internal/v5test/discv5tests.go | 188 ++++++++++++++-------- cmd/devp2p/internal/v5test/framework.go | 50 ++++-- 2 files changed, 150 insertions(+), 88 deletions(-) diff --git a/cmd/devp2p/internal/v5test/discv5tests.go b/cmd/devp2p/internal/v5test/discv5tests.go index efe9144069..4dc2507693 100644 --- a/cmd/devp2p/internal/v5test/discv5tests.go +++ b/cmd/devp2p/internal/v5test/discv5tests.go @@ -257,34 +257,50 @@ that they are returned by FINDNODE.`) // Create bystanders. nodes := make([]*bystander, 5) - added := make(chan enode.ID, len(nodes)) + liveCh := make(chan enode.ID, len(nodes)) for i := range nodes { - nodes[i] = newBystander(t, s, added) + nodes[i] = newBystander(t, s, liveCh) defer nodes[i].close() } - // Get them added to the remote table. + // Prefill each bystander with the full bystander set so background FINDNODE + // lookups see useful routing data instead of empty responses. + known := make([]*enode.Node, 0, len(nodes)) + for _, bn := range nodes { + known = append(known, bn.conn.localNode.Node()) + } + for _, bn := range nodes { + bn.known = append([]*enode.Node(nil), known...) + } + + // Wait until enough bystanders have actually become live, i.e. the remote node + // has revalidated them by sending PING and receiving our PONG. + requiredLiveNodes := len(nodes) timeout := 60 * time.Second timeoutCh := time.After(timeout) - for count := 0; count < len(nodes); { + liveSet := make(map[enode.ID]*enode.Node) + for len(liveSet) < requiredLiveNodes { select { - case id := <-added: - t.Logf("bystander node %v added to remote table", id) - count++ + case id := <-liveCh: + for _, bn := range nodes { + if bn.id() == id { + liveSet[id] = bn.conn.localNode.Node() + break + } + } + t.Logf("bystander node %v became live", id) case <-timeoutCh: - t.Errorf("remote added %d bystander nodes in %v, need %d to continue", count, timeout, len(nodes)) - t.Logf("this can happen if the node has a non-empty table from previous runs") + t.Errorf("remote revalidated %d bystander nodes in %v, need %d to continue", len(liveSet), timeout, requiredLiveNodes) return } } - t.Logf("all %d bystander nodes were added", len(nodes)) + t.Logf("continuing after all %d bystander nodes became live", len(liveSet)) - // Collect our nodes by distance. + // Collect live nodes by distance. var dists []uint expect := make(map[enode.ID]*enode.Node) - for _, bn := range nodes { - n := bn.conn.localNode.Node() - expect[n.ID()] = n + for id, n := range liveSet { + expect[id] = n d := uint(enode.LogDist(n.ID(), s.Dest.ID())) if !slices.Contains(dists, d) { dists = append(dists, d) @@ -295,42 +311,63 @@ that they are returned by FINDNODE.`) t.Log("requesting nodes") conn, l1 := s.listen1(t) defer conn.close() - foundNodes, err := conn.findnode(l1, dists) - if err != nil { - t.Fatal(err) - } - t.Logf("remote returned %d nodes for distance list %v", len(foundNodes), dists) - for _, n := range foundNodes { - delete(expect, n.ID()) - } - if len(expect) > 0 { - t.Errorf("missing %d nodes in FINDNODE result", len(expect)) - t.Logf("this can happen if the test is run multiple times in quick succession") - t.Logf("and the remote node hasn't removed dead nodes from previous runs yet") - } else { - t.Logf("all %d expected nodes were returned", len(nodes)) + + const maxAttempts = 5 + const retryInterval = 2 * time.Second + + for attempt := 1; attempt <= maxAttempts; attempt++ { + foundNodes, err := conn.findnode(l1, dists) + if err != nil { + t.Fatal(err) + } + missing := make(map[enode.ID]struct{}) + for id := range expect { + missing[id] = struct{}{} + } + for _, n := range foundNodes { + delete(missing, n.ID()) + } + t.Logf("attempt %d: remote returned %d nodes for distance list %v, missing %d", attempt, len(foundNodes), dists, len(missing)) + if len(missing) == 0 { + t.Logf("all %d expected live nodes were returned", len(expect)) + return + } + if attempt < maxAttempts { + time.Sleep(retryInterval) + } } + t.Errorf("missing nodes in FINDNODE result after %d attempts", maxAttempts) + t.Logf("this can happen if the node has a non-empty table from previous runs") } // A bystander is a node whose only purpose is filling a spot in the remote table. type bystander struct { - dest *enode.Node - conn *conn - l net.PacketConn - - addedCh chan enode.ID - done sync.WaitGroup + dest *enode.Node + conn *conn + l net.PacketConn + known []*enode.Node + + liveCh chan enode.ID + sent map[v5wire.Nonce]v5wire.Packet + done sync.WaitGroup } -func newBystander(t *utesting.T, s *Suite, added chan enode.ID) *bystander { +func newBystander(t *utesting.T, s *Suite, live chan enode.ID) *bystander { conn, l := s.listen1(t) conn.setEndpoint(l) // bystander nodes need IP/port to get pinged bn := &bystander{ - conn: conn, - l: l, - dest: s.Dest, - addedCh: added, + conn: conn, + l: l, + dest: s.Dest, + liveCh: live, + sent: make(map[v5wire.Nonce]v5wire.Packet), } + // Establish an initial session and let the remote learn this node before + // switching to the passive responder loop below. + conn.reqresp(l, &v5wire.Ping{ + ReqID: conn.nextReqID(), + ENRSeq: conn.localNode.Seq(), + }) bn.done.Add(1) go bn.loop() return bn @@ -351,48 +388,57 @@ func (bn *bystander) close() { func (bn *bystander) loop() { defer bn.done.Done() - var ( - lastPing time.Time - wasAdded bool - ) for { - // Ping the remote node. - if !wasAdded && time.Since(lastPing) > 10*time.Second { - bn.conn.reqresp(bn.l, &v5wire.Ping{ - ReqID: bn.conn.nextReqID(), - ENRSeq: bn.dest.Seq(), - }) - lastPing = time.Now() - } - // Answer packets. - switch p := bn.conn.read(bn.l).(type) { + p, from := bn.conn.readFrom(bn.l) + switch p := p.(type) { + case *v5wire.Whoareyou: + p.Node = bn.dest + if resp, ok := bn.sent[p.Nonce]; ok { + nonce := bn.conn.writeTo(bn.l, resp, p, from) + delete(bn.sent, p.Nonce) + bn.sent[nonce] = resp + } else { + bn.conn.writeTo(bn.l, &v5wire.Ping{ + ReqID: bn.conn.nextReqID(), + ENRSeq: bn.conn.localNode.Seq(), + }, p, from) + } case *v5wire.Ping: - bn.conn.write(bn.l, &v5wire.Pong{ - ReqID: p.ReqID, + resp := &v5wire.Pong{ + ReqID: append([]byte(nil), p.ReqID...), ENRSeq: bn.conn.localNode.Seq(), - ToIP: bn.dest.IP(), - ToPort: uint16(bn.dest.UDP()), - }, nil) - wasAdded = true - bn.notifyAdded() + ToIP: from.IP, + ToPort: uint16(from.Port), + } + nonce := bn.conn.writeTo(bn.l, resp, nil, from) + bn.sent[nonce] = resp + bn.notifyLive() case *v5wire.Findnode: - bn.conn.write(bn.l, &v5wire.Nodes{ReqID: p.ReqID, RespCount: 1}, nil) - wasAdded = true - bn.notifyAdded() + resp := &v5wire.Nodes{ReqID: append([]byte(nil), p.ReqID...), RespCount: 1} + for _, n := range bn.known { + if slices.Contains(p.Distances, uint(enode.LogDist(n.ID(), bn.id()))) { + resp.Nodes = append(resp.Nodes, n.Record()) + } + } + nonce := bn.conn.writeTo(bn.l, resp, nil, from) + bn.sent[nonce] = resp case *v5wire.TalkRequest: - bn.conn.write(bn.l, &v5wire.TalkResponse{ReqID: p.ReqID}, nil) + resp := &v5wire.TalkResponse{ReqID: append([]byte(nil), p.ReqID...)} + nonce := bn.conn.writeTo(bn.l, resp, nil, from) + bn.sent[nonce] = resp case *readError: - if !netutil.IsTemporaryError(p.err) { - bn.conn.logf("shutting down: %v", p.err) - return + if netutil.IsTemporaryError(p.err) || v5wire.IsInvalidHeader(p.err) { + continue } + bn.conn.logf("shutting down: %v", p.err) + return } } } -func (bn *bystander) notifyAdded() { - if bn.addedCh != nil { - bn.addedCh <- bn.id() - bn.addedCh = nil +func (bn *bystander) notifyLive() { + if bn.liveCh != nil { + bn.liveCh <- bn.id() + bn.liveCh = nil } } diff --git a/cmd/devp2p/internal/v5test/framework.go b/cmd/devp2p/internal/v5test/framework.go index acab1eef79..0532dafb2c 100644 --- a/cmd/devp2p/internal/v5test/framework.go +++ b/cmd/devp2p/internal/v5test/framework.go @@ -127,14 +127,16 @@ func (tc *conn) nextReqID() []byte { // The request is retried if a handshake is requested. func (tc *conn) reqresp(c net.PacketConn, req v5wire.Packet) v5wire.Packet { reqnonce := tc.write(c, req, nil) - switch resp := tc.read(c).(type) { + resp, from := tc.readFrom(c) + switch resp := resp.(type) { case *v5wire.Whoareyou: if resp.Nonce != reqnonce { return readErrorf("wrong nonce %x in WHOAREYOU (want %x)", resp.Nonce[:], reqnonce[:]) } resp.Node = tc.remote - tc.write(c, req, resp) - return tc.read(c) + tc.writeTo(c, req, resp, from) + resp2, _ := tc.readFrom(c) + return resp2 default: return resp } @@ -150,21 +152,24 @@ func (tc *conn) findnode(c net.PacketConn, dists []uint) ([]*enode.Node, error) results []*enode.Node ) for n := 1; n > 0; { - switch resp := tc.read(c).(type) { + resp, from := tc.readFrom(c) + switch resp := resp.(type) { case *v5wire.Whoareyou: // Handle handshake. if resp.Nonce == reqnonce { resp.Node = tc.remote - tc.write(c, findnode, resp) + tc.writeTo(c, findnode, resp, from) } else { return nil, fmt.Errorf("unexpected WHOAREYOU (nonce %x), waiting for NODES", resp.Nonce[:]) } case *v5wire.Ping: // Handle ping from remote. - tc.write(c, &v5wire.Pong{ + tc.writeTo(c, &v5wire.Pong{ ReqID: resp.ReqID, ENRSeq: tc.localNode.Seq(), - }, nil) + ToIP: from.IP, + ToPort: uint16(from.Port), + }, nil, from) case *v5wire.Nodes: // Got NODES! Check request ID. if !bytes.Equal(resp.ReqID, findnode.ReqID) { @@ -200,11 +205,16 @@ func (tc *conn) findnode(c net.PacketConn, dists []uint) ([]*enode.Node, error) // write sends a packet on the given connection. func (tc *conn) write(c net.PacketConn, p v5wire.Packet, challenge *v5wire.Whoareyou) v5wire.Nonce { + return tc.writeTo(c, p, challenge, tc.remoteAddr) +} + +// writeTo sends a packet on the given connection to the given UDP address. +func (tc *conn) writeTo(c net.PacketConn, p v5wire.Packet, challenge *v5wire.Whoareyou, to *net.UDPAddr) v5wire.Nonce { packet, nonce, err := tc.codec.Encode(tc.remote.ID(), tc.remoteAddr.String(), p, challenge) if err != nil { panic(fmt.Errorf("can't encode %v packet: %v", p.Name(), err)) } - if _, err := c.WriteTo(packet, tc.remoteAddr); err != nil { + if _, err := c.WriteTo(packet, to); err != nil { tc.logf("Can't send %s: %v", p.Name(), err) } else { tc.logf(">> %s", p.Name()) @@ -214,24 +224,30 @@ func (tc *conn) write(c net.PacketConn, p v5wire.Packet, challenge *v5wire.Whoar // read waits for an incoming packet on the given connection. func (tc *conn) read(c net.PacketConn) v5wire.Packet { + p, _ := tc.readFrom(c) + return p +} + +// readFrom waits for an incoming packet and returns its source address. +func (tc *conn) readFrom(c net.PacketConn) (v5wire.Packet, *net.UDPAddr) { buf := make([]byte, 1280) if err := c.SetReadDeadline(time.Now().Add(waitTime)); err != nil { - return &readError{err} + return &readError{err}, nil } - n, _, err := c.ReadFrom(buf) + n, from, err := c.ReadFrom(buf) if err != nil { - return &readError{err} + return &readError{err}, nil } - // Always use tc.remoteAddr for session lookup. The actual source address of - // the packet may differ from tc.remoteAddr when the remote node is reachable - // via multiple networks (e.g. Docker bridge vs. overlay), but the codec's - // session cache is keyed by the address used during Encode. + udpFrom, _ := from.(*net.UDPAddr) + // Use tc.remoteAddr for codec/session lookup because the fixture keys sessions + // by the advertised endpoint, but return the actual UDP source so responses can + // comply with the spec and go back to the request envelope address. _, _, p, err := tc.codec.Decode(buf[:n], tc.remoteAddr.String()) if err != nil { - return &readError{err} + return &readError{err}, udpFrom } tc.logf("<< %s", p.Name()) - return p + return p, udpFrom } // logf prints to the test log. From c2234462a84c1bb6000cfcc6195404d1caabf2a7 Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Tue, 14 Apr 2026 14:47:43 +0200 Subject: [PATCH 21/40] .github/workflows: add freebsd test github action (#34078) This is meant to be run daily, in order to verify the FreeBSD build wasn't broken like last time. --- .github/workflows/freebsd.yml | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 .github/workflows/freebsd.yml diff --git a/.github/workflows/freebsd.yml b/.github/workflows/freebsd.yml new file mode 100644 index 0000000000..8f58ebee9a --- /dev/null +++ b/.github/workflows/freebsd.yml @@ -0,0 +1,29 @@ +on: + push: + branches: + - freebsd-github-action + + workflow_dispatch: + +jobs: + build: + name: FreeBSD-build + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v6 + with: + submodules: false + + - name: Test in FreeBSD + id: test + uses: vmactions/freebsd-vm@v1 + with: + release: "15.0" + usesh: true + prepare: | + pkg install -y go + run: | + freebsd-version + uname -a + go version + go run ./build/ci.go test -p 8 From c690d6041e058ffbe96ce7fbea9956de9aa964a1 Mon Sep 17 00:00:00 2001 From: jvn Date: Tue, 14 Apr 2026 18:28:27 +0530 Subject: [PATCH 22/40] cmd/geth: add Prague pruning points for hoodi (#34714) Adds config to add Prague prune point for the hoodi testnet. --- core/history/historymode.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core/history/historymode.go b/core/history/historymode.go index 1adfe014b2..2ba746e7dd 100644 --- a/core/history/historymode.go +++ b/core/history/historymode.go @@ -107,6 +107,10 @@ var staticPrunePoints = map[HistoryMode]map[common.Hash]*PrunePoint{ BlockNumber: 7836331, BlockHash: common.HexToHash("0xe6571beb68bf24dbd8a6ba354518996920c55a3f8d8fdca423e391b8ad071f22"), }, + params.HoodiGenesisHash: { + BlockNumber: 60412, + BlockHash: common.HexToHash("0x1562792812ef418eaafc8f1f093d84d9634971e9dd6b0771302eb5b9fd4d2c46"), + }, }, } From 2414861d36a23b7ddf91275d7a95bcdac636a0f4 Mon Sep 17 00:00:00 2001 From: cui Date: Tue, 14 Apr 2026 21:39:42 +0800 Subject: [PATCH 23/40] core/state: optimize transient storage (#33695) Optimizes the transient storage. Turns it from a map of maps into a single map keyed by . --- core/state/statedb_test.go | 2 +- core/state/transient_storage.go | 56 +++++++++++++++------------------ 2 files changed, 27 insertions(+), 31 deletions(-) diff --git a/core/state/statedb_test.go b/core/state/statedb_test.go index d29b262eea..680602631e 100644 --- a/core/state/statedb_test.go +++ b/core/state/statedb_test.go @@ -641,7 +641,7 @@ func (test *snapshotTest) checkEqual(state, checkstate *StateDB) error { { have := state.transientStorage want := checkstate.transientStorage - if !maps.EqualFunc(have, want, maps.Equal) { + if !maps.Equal(have, want) { return fmt.Errorf("transient storage differs ,have\n%v\nwant\n%v", have.PrettyPrint(), want.PrettyPrint()) diff --git a/core/state/transient_storage.go b/core/state/transient_storage.go index 3bb4955425..a3cfaceb3e 100644 --- a/core/state/transient_storage.go +++ b/core/state/transient_storage.go @@ -25,8 +25,13 @@ import ( "github.com/ethereum/go-ethereum/common" ) +type transientStorageKey struct { + addr common.Address + key common.Hash +} + // transientStorage is a representation of EIP-1153 "Transient Storage". -type transientStorage map[common.Address]Storage +type transientStorage map[transientStorageKey]common.Hash // newTransientStorage creates a new instance of a transientStorage. func newTransientStorage() transientStorage { @@ -35,52 +40,43 @@ func newTransientStorage() transientStorage { // Set sets the transient-storage `value` for `key` at the given `addr`. func (t transientStorage) Set(addr common.Address, key, value common.Hash) { + tsKey := transientStorageKey{addr: addr, key: key} if value == (common.Hash{}) { // this is a 'delete' - if _, ok := t[addr]; ok { - delete(t[addr], key) - if len(t[addr]) == 0 { - delete(t, addr) - } - } + delete(t, tsKey) } else { - if _, ok := t[addr]; !ok { - t[addr] = make(Storage) - } - t[addr][key] = value + t[tsKey] = value } } // Get gets the transient storage for `key` at the given `addr`. func (t transientStorage) Get(addr common.Address, key common.Hash) common.Hash { - val, ok := t[addr] - if !ok { - return common.Hash{} - } - return val[key] + tsKey := transientStorageKey{addr: addr, key: key} + return t[tsKey] } // Copy does a deep copy of the transientStorage func (t transientStorage) Copy() transientStorage { - storage := make(transientStorage) - for key, value := range t { - storage[key] = value.Copy() - } - return storage + return maps.Clone(t) } // PrettyPrint prints the contents of the access list in a human-readable form func (t transientStorage) PrettyPrint() string { out := new(strings.Builder) - sortedAddrs := slices.Collect(maps.Keys(t)) - slices.SortFunc(sortedAddrs, common.Address.Cmp) + sortedTSKeys := slices.Collect(maps.Keys(t)) + slices.SortFunc(sortedTSKeys, func(a, b transientStorageKey) int { + r := a.addr.Cmp(b.addr) + if r != 0 { + return r + } + return a.key.Cmp(b.key) + }) - for _, addr := range sortedAddrs { - fmt.Fprintf(out, "%#x:", addr) - storage := t[addr] - sortedKeys := slices.Collect(maps.Keys(storage)) - slices.SortFunc(sortedKeys, common.Hash.Cmp) - for _, key := range sortedKeys { - fmt.Fprintf(out, " %X : %X\n", key, storage[key]) + for i := 0; i < len(sortedTSKeys); { + tsKey := sortedTSKeys[i] + fmt.Fprintf(out, "%#x:", tsKey.addr) + for ; i < len(sortedTSKeys) && sortedTSKeys[i].addr == tsKey.addr; i++ { + tsKey2 := sortedTSKeys[i] + fmt.Fprintf(out, " %X : %X\n", tsKey2.key, t[tsKey2]) } } return out.String() From eb67d6193344db0a11cb6b344e4ad781e07bc51f Mon Sep 17 00:00:00 2001 From: rjl493456442 Date: Tue, 14 Apr 2026 21:54:36 +0800 Subject: [PATCH 24/40] cmd/geth, core/state, tests: rework EIP7610 check (#34718) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR simplifies the implementation of EIP-7610 by eliminating the need to check storage emptiness during contract deployment. EIP-7610 specifies that contract creation must be rejected if the destination account has a non-zero nonce, non-empty runtime code, or **non-empty storage**. After EIP-161, all newly deployed contracts are initialized with a nonce of one. As a result, such accounts are no longer eligible as deployment targets unless they are explicitly cleared. However, prior to EIP-161, contracts were initialized with a nonce of zero. This made it possible to end up with accounts that have: - zero nonce - empty runtime code - non-empty storage (created during constructor execution) - non-zero balance These edge-case accounts complicate the storage emptiness check. In practice, contract addresses are derived using one of the following formulas: - `Keccak256(rlp({sender, nonce}))[12:]` - `Keccak256([]byte{0xff}, sender, salt[:], initHash)[12:]` As such, an existing address is not selected as a deployment target unless a collision occurs, which is extremely unlikely. --- Previously, verifying storage emptiness relied on GetStorageRoot. However, with the transition to the block-based access list (BAL), the storage root is no longer available, as computing it would require reconstructing the full storage trie from all mutations of preceding transactions. To address this, this PR introduces a simplified approach: it hardcodes the set of known accounts that have zero nonce, empty runtime code, but non-empty storage and non-zero balance. During contract deployment, if the destination address belongs to this set, the deployment is rejected. This check is applied retroactively back to genesis. Since no address collision events have occurred in Ethereum’s history, this change does not alter existing behavior. Instead, it serves as a safeguard for future state transitions. --- cmd/geth/snapshot.go | 108 +++++++++++++++++++++++++++++ core/state/statedb.go | 3 + core/state/statedb_hooked.go | 4 -- core/state/statedb_test.go | 16 ++--- core/state/trie_prefetcher_test.go | 5 +- core/vm/eip7610.go | 98 ++++++++++++++++++++++++++ core/vm/eip7610_test.go | 62 +++++++++++++++++ core/vm/evm.go | 3 +- core/vm/interface.go | 1 - tests/block_test.go | 12 ++++ tests/state_test.go | 11 +++ 11 files changed, 304 insertions(+), 19 deletions(-) create mode 100644 core/vm/eip7610.go create mode 100644 core/vm/eip7610_test.go diff --git a/cmd/geth/snapshot.go b/cmd/geth/snapshot.go index c177fb5ea2..d168ee1d7d 100644 --- a/cmd/geth/snapshot.go +++ b/cmd/geth/snapshot.go @@ -18,15 +18,18 @@ package main import ( "bytes" + "encoding/hex" "encoding/json" "errors" "fmt" "os" "slices" + "sort" "time" "github.com/ethereum/go-ethereum/cmd/utils" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core/rawdb" "github.com/ethereum/go-ethereum/core/state" "github.com/ethereum/go-ethereum/core/state/pruner" @@ -168,6 +171,22 @@ block is used. Description: ` The export-preimages command exports hash preimages to a flat file, in exactly the expected order for the overlay tree migration. +`, + }, + { + Name: "list-eip-7610-accounts", + Aliases: []string{"eip7610"}, + Usage: "list EIP7610 eligible accounts", + Action: listEIP7610EligibleAccounts, + Flags: slices.Concat(utils.NetworkFlags, utils.DatabaseFlags), + Description: ` +geth snapshot list-eip-7610-accounts +traverses the post–EIP-161 state and returns all accounts that are eligible +under EIP-7610: accounts with zero nonce, empty runtime code, and non-empty +storage. The traversal will be aborted immediately if the state is prior to +EIP-161. + +The exported accounts are identified by their address. `, }, }, @@ -801,3 +820,92 @@ func checkAccount(ctx *cli.Context) error { log.Info("Checked the snapshot journalled storage", "time", common.PrettyDuration(time.Since(start))) return nil } + +// listEIP7610EligibleAccounts traverses the post–EIP-161 state and returns all +// accounts that are eligible under EIP-7610: accounts with zero nonce, empty +// runtime code, and non-empty storage. +// +// Such accounts could only have been created before EIP-161, since after that +// all newly created contracts are initialized with a nonce of one. +// +// This helper should be generally applicable to all networks, including the +// Ethereum mainnet. For most networks where EIP-161 was enabled from genesis, +// the resulting set is expected to be empty. Otherwise, network operators are +// responsible for generating the eligible account set themselves. +// +// Notably, the exported accounts are identified by their address. +func listEIP7610EligibleAccounts(ctx *cli.Context) error { + stack, _ := makeConfigNode(ctx) + defer stack.Close() + + chaindb := utils.MakeChainDatabase(ctx, stack, true) + defer chaindb.Close() + + headBlock := rawdb.ReadHeadBlock(chaindb) + if headBlock == nil { + log.Error("Failed to load head block") + return nil + } + config, _, err := core.LoadChainConfig(chaindb, utils.MakeGenesis(ctx)) + if err != nil { + log.Error("Failed to load chain config", "err", err) + return err + } + if !config.IsEIP158(headBlock.Number()) { + log.Info("Local head is prior to EIP-161", "head", headBlock.Number(), "eip-161", *config.EIP158Block) + return nil + } + triedb := utils.MakeTrieDatabase(ctx, stack, chaindb, false, true, false) + defer triedb.Close() + + if triedb.Scheme() != rawdb.PathScheme { + log.Error("Hash scheme is not supported") + return nil + } + iter, err := triedb.AccountIterator(headBlock.Root(), common.Hash{}) + if err != nil { + log.Error("Failed to get account iterator", "err", err) + return err + } + var ( + start = time.Now() + accounts []common.Address + ) + for iter.Next() { + blob := iter.Account() + if blob == nil { + log.Error("Failed to get account blob") + return nil + } + var account types.SlimAccount + if err := rlp.DecodeBytes(blob, &account); err != nil { + log.Error("Failed to decode", "err", err) + return err + } + // EIP-7610 account eligibility: + // - account.nonce == 0 + // - account.runtime_code == empty + // - account.storage != empty + if len(account.CodeHash) == 0 && account.Nonce == 0 && len(account.Root) != 0 { + preimage := rawdb.ReadPreimage(chaindb, iter.Hash()) + if preimage == nil { + log.Error("Failed to read preimage", "hash", iter.Hash().Hex()) + return nil + } + accounts = append(accounts, common.BytesToAddress(preimage)) + } + } + if len(accounts) == 0 { + log.Info("Traversed state", "eligible", len(accounts), "elapsed", common.PrettyDuration(time.Since(start))) + } else { + sort.Slice(accounts, func(i, j int) bool { + return accounts[i].Cmp(accounts[j]) < 0 + }) + buf := make([]byte, len(accounts)*common.AddressLength) + for i, h := range accounts { + copy(buf[i*common.AddressLength:], h[:]) + } + log.Info("Traversed state", "eligible", len(accounts), "elapsed", common.PrettyDuration(time.Since(start)), "output", hex.EncodeToString(buf)) + } + return nil +} diff --git a/core/state/statedb.go b/core/state/statedb.go index 8b09ea89f6..a4207a10c2 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -338,6 +338,9 @@ func (s *StateDB) GetNonce(addr common.Address) uint64 { // GetStorageRoot retrieves the storage root from the given address or empty // if object not found. +// +// Note: the storage root returned corresponds to the trie since last Intermediate +// operation, some recent in-memory changes are excluded. func (s *StateDB) GetStorageRoot(addr common.Address) common.Hash { stateObject := s.getStateObject(addr) if stateObject != nil { diff --git a/core/state/statedb_hooked.go b/core/state/statedb_hooked.go index 52cf98d19b..687c4bb52b 100644 --- a/core/state/statedb_hooked.go +++ b/core/state/statedb_hooked.go @@ -98,10 +98,6 @@ func (s *hookedStateDB) GetState(addr common.Address, hash common.Hash) common.H return s.inner.GetState(addr, hash) } -func (s *hookedStateDB) GetStorageRoot(addr common.Address) common.Hash { - return s.inner.GetStorageRoot(addr) -} - func (s *hookedStateDB) GetTransientState(addr common.Address, key common.Hash) common.Hash { return s.inner.GetTransientState(addr, key) } diff --git a/core/state/statedb_test.go b/core/state/statedb_test.go index 680602631e..6936372c50 100644 --- a/core/state/statedb_test.go +++ b/core/state/statedb_test.go @@ -247,16 +247,16 @@ func TestCopyWithDirtyJournal(t *testing.T) { orig.Finalise(true) for i := byte(0); i < 255; i++ { - root := orig.GetStorageRoot(common.BytesToAddress([]byte{i})) - if root != (common.Hash{}) { - t.Errorf("Unexpected storage root %x", root) + balance := orig.GetBalance(common.BytesToAddress([]byte{i})) + if !balance.IsZero() { + t.Errorf("Unexpected balance %x", root) } } cpy.Finalise(true) for i := byte(0); i < 255; i++ { - root := cpy.GetStorageRoot(common.BytesToAddress([]byte{i})) - if root != (common.Hash{}) { - t.Errorf("Unexpected storage root %x", root) + balance := cpy.GetBalance(common.BytesToAddress([]byte{i})) + if !balance.IsZero() { + t.Errorf("Unexpected balance %x", root) } } if cpy.IntermediateRoot(true) != orig.IntermediateRoot(true) { @@ -394,9 +394,7 @@ func newTestAction(addr common.Address, r *rand.Rand) testAction { } contractHash := s.GetCodeHash(addr) emptyCode := contractHash == (common.Hash{}) || contractHash == types.EmptyCodeHash - storageRoot := s.GetStorageRoot(addr) - emptyStorage := storageRoot == (common.Hash{}) || storageRoot == types.EmptyRootHash - if s.GetNonce(addr) == 0 && emptyCode && emptyStorage { + if s.GetNonce(addr) == 0 && emptyCode { s.CreateContract(addr) // We also set some code here, to prevent the // CreateContract action from being performed twice in a row, diff --git a/core/state/trie_prefetcher_test.go b/core/state/trie_prefetcher_test.go index 41349c0c0e..dad208d01a 100644 --- a/core/state/trie_prefetcher_test.go +++ b/core/state/trie_prefetcher_test.go @@ -86,18 +86,17 @@ func TestVerklePrefetcher(t *testing.T) { root, _ := state.Commit(0, true, false) state, _ = New(root, sdb) - sRoot := state.GetStorageRoot(addr) fetcher := newTriePrefetcher(sdb, root, "", false) // Read account fetcher.prefetch(common.Hash{}, root, common.Address{}, []common.Address{addr}, nil, false) // Read storage slot - fetcher.prefetch(crypto.Keccak256Hash(addr.Bytes()), sRoot, addr, nil, []common.Hash{skey}, false) + fetcher.prefetch(crypto.Keccak256Hash(addr.Bytes()), common.Hash{}, addr, nil, []common.Hash{skey}, false) fetcher.terminate(false) accountTrie := fetcher.trie(common.Hash{}, root) - storageTrie := fetcher.trie(crypto.Keccak256Hash(addr.Bytes()), sRoot) + storageTrie := fetcher.trie(crypto.Keccak256Hash(addr.Bytes()), common.Hash{}) rootA := accountTrie.Hash() rootB := storageTrie.Hash() diff --git a/core/vm/eip7610.go b/core/vm/eip7610.go new file mode 100644 index 0000000000..883f4502b5 --- /dev/null +++ b/core/vm/eip7610.go @@ -0,0 +1,98 @@ +// Copyright 2026 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package vm + +import ( + "math/big" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/params" +) + +// eip7610Accounts lists the addresses eligible for contract deployment +// rejection under EIP-7610, keyed by chain ID. Only networks that adopted +// EIP-158 after genesis need an entry; all others have no pre-existing +// address collisions to guard against. +var eip7610Accounts = map[uint64][]common.Address{ + params.MainnetChainConfig.ChainID.Uint64(): { + common.HexToAddress("0x02820E4bEE488C40f7455fDCa53125565148708F"), + common.HexToAddress("0x14725085d004f1b10Ee07234A4ab28c5Ad2a7b9E"), + common.HexToAddress("0x19272418753B90D9a3E3Efc8430b1612c55fcB3A"), + common.HexToAddress("0x2c081Ed1949D7Dd9447F9d96e509befE576D4461"), + common.HexToAddress("0x3311c08066580cb906a7287b6786E504C2EBD09f"), + common.HexToAddress("0x361d7a60b43587c7f6bbA4f9fD9642747F65210A"), + common.HexToAddress("0x40490C9c468622d5c89646D6F3097F8Eaf80c411"), + common.HexToAddress("0x4d149EB99BDEEFC1f858f8fd22289C6beAE99f2c"), + common.HexToAddress("0x5071cb62aA170b7f66b26cae8004d90E6078Bb1E"), + common.HexToAddress("0x50b1497068bAE652Df3562EB8Ea7677ff84477FA"), + common.HexToAddress("0x5983C6aC846DcF85fbBC4303F43eb91C379F79ae"), + common.HexToAddress("0x59EC0410867828E3b8c23Dd8A29d9796ef523b17"), + common.HexToAddress("0x5cC182faBFb81A056B6080d4200BC5150673D06f"), + common.HexToAddress("0x6f156dbf8Ed30e53F7C9Df73144E69f65cBB7E94"), + common.HexToAddress("0x7D6ae067De8d44Ae1A08750e7D626D61A623C44A"), + common.HexToAddress("0x8398fF6c618e9515468c1c4b198d53666CBe8462"), + common.HexToAddress("0xA21B22389bfC1cd6Bc7BA19A4Fc96aDC3D0FE074"), + common.HexToAddress("0xaDD92e0650457C5Db0c4c08cbf7cA580175d33d2"), + common.HexToAddress("0xAE3703584494Ade958AD27EC2d289b7a67c19E90"), + common.HexToAddress("0xb619f45637C39Ca49A41ac64c11637A0A194455E"), + common.HexToAddress("0xD8253352f6044cFE55bcC0748C3FA37b7dF81F98"), + common.HexToAddress("0xDB7C577B93Baeb56dAB50aF4D6f86F99A06B96a2"), + common.HexToAddress("0xdE425ad4B8d2d9E0E12F65CBcD6D55F447B44083"), + common.HexToAddress("0xe62dc49C92fA799033644d2A9aFD7e3BAbE5A80a"), + common.HexToAddress("0xF468BcBC4a0BFDB06336E773382C5202E674db71"), + common.HexToAddress("0xF4a835ec1364809003dE3925685F24cD360bdffe"), + common.HexToAddress("0xFc4465F84B29a1F8794Dc753F41BeF1F4b025ED2"), + common.HexToAddress("0xfeE7707fa4b8C0A923A0E40399Db3e7Ce26069C6"), + }, +} + +// eip7610AccountSets is the membership-lookup form of eip7610Accounts, +// built once at init for O(1) containment checks. +var eip7610AccountSets = func() map[uint64]map[common.Address]struct{} { + sets := make(map[uint64]map[common.Address]struct{}, len(eip7610Accounts)) + for chainID, addrs := range eip7610Accounts { + set := make(map[common.Address]struct{}, len(addrs)) + for _, a := range addrs { + set[a] = struct{}{} + } + sets[chainID] = set + } + return sets +}() + +// isEIP7610RejectedAccount reports whether the account identified by the +// address is eligible for contract deployment rejection due to having +// non-empty storage. +// +// Note that, historically, there has been no case where a contract deployment +// targets an already existing account in Ethereum. This situation would only +// occur in the event of an address collision, which is extremely unlikely. +// +// This check is skipped for blocks prior to EIP-158, serving as a safeguard +// against potential address collisions in the future. Chains that are not +// registered in eip7610Accounts are assumed to have no rejected accounts, +// and false is returned for them. +func isEIP7610RejectedAccount(chainID *big.Int, addr common.Address, isEIP158 bool) bool { + // Short circuit for blocks prior to EIP-158. + if !isEIP158 { + return false + } + // Unknown chains fall through as a nil set; the second lookup then + // returns the zero value (false), treating the chain as empty. + _, exist := eip7610AccountSets[chainID.Uint64()][addr] + return exist +} diff --git a/core/vm/eip7610_test.go b/core/vm/eip7610_test.go new file mode 100644 index 0000000000..f881020c5c --- /dev/null +++ b/core/vm/eip7610_test.go @@ -0,0 +1,62 @@ +// Copyright 2026 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package vm + +import ( + "fmt" + "slices" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/params" +) + +func Example_mainnetEIP7610Accounts() { + list := slices.Clone(eip7610Accounts[params.MainnetChainConfig.ChainID.Uint64()]) + slices.SortFunc(list, common.Address.Cmp) + for _, addr := range list { + fmt.Println(addr.Hex()) + } + // Output: + // 0x02820E4bEE488C40f7455fDCa53125565148708F + // 0x14725085d004f1b10Ee07234A4ab28c5Ad2a7b9E + // 0x19272418753B90D9a3E3Efc8430b1612c55fcB3A + // 0x2c081Ed1949D7Dd9447F9d96e509befE576D4461 + // 0x3311c08066580cb906a7287b6786E504C2EBD09f + // 0x361d7a60b43587c7f6bbA4f9fD9642747F65210A + // 0x40490C9c468622d5c89646D6F3097F8Eaf80c411 + // 0x4d149EB99BDEEFC1f858f8fd22289C6beAE99f2c + // 0x5071cb62aA170b7f66b26cae8004d90E6078Bb1E + // 0x50b1497068bAE652Df3562EB8Ea7677ff84477FA + // 0x5983C6aC846DcF85fbBC4303F43eb91C379F79ae + // 0x59EC0410867828E3b8c23Dd8A29d9796ef523b17 + // 0x5cC182faBFb81A056B6080d4200BC5150673D06f + // 0x6f156dbf8Ed30e53F7C9Df73144E69f65cBB7E94 + // 0x7D6ae067De8d44Ae1A08750e7D626D61A623C44A + // 0x8398fF6c618e9515468c1c4b198d53666CBe8462 + // 0xA21B22389bfC1cd6Bc7BA19A4Fc96aDC3D0FE074 + // 0xaDD92e0650457C5Db0c4c08cbf7cA580175d33d2 + // 0xAE3703584494Ade958AD27EC2d289b7a67c19E90 + // 0xb619f45637C39Ca49A41ac64c11637A0A194455E + // 0xD8253352f6044cFE55bcC0748C3FA37b7dF81F98 + // 0xDB7C577B93Baeb56dAB50aF4D6f86F99A06B96a2 + // 0xdE425ad4B8d2d9E0E12F65CBcD6D55F447B44083 + // 0xe62dc49C92fA799033644d2A9aFD7e3BAbE5A80a + // 0xF468BcBC4a0BFDB06336E773382C5202E674db71 + // 0xF4a835ec1364809003dE3925685F24cD360bdffe + // 0xFc4465F84B29a1F8794Dc753F41BeF1F4b025ED2 + // 0xfeE7707fa4b8C0A923A0E40399Db3e7Ce26069C6 +} diff --git a/core/vm/evm.go b/core/vm/evm.go index 4df2627486..cfc837fb62 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -532,10 +532,9 @@ func (evm *EVM) create(caller common.Address, code []byte, gas uint64, value *ui // - the code is non-empty // - the storage is non-empty contractHash := evm.StateDB.GetCodeHash(address) - storageRoot := evm.StateDB.GetStorageRoot(address) if evm.StateDB.GetNonce(address) != 0 || (contractHash != (common.Hash{}) && contractHash != types.EmptyCodeHash) || // non-empty code - (storageRoot != (common.Hash{}) && storageRoot != types.EmptyRootHash) { // non-empty storage + isEIP7610RejectedAccount(evm.ChainConfig().ChainID, address, evm.chainRules.IsEIP158) { if evm.Config.Tracer != nil && evm.Config.Tracer.OnGasChange != nil { evm.Config.Tracer.OnGasChange(gas, 0, tracing.GasChangeCallFailedExecution) } diff --git a/core/vm/interface.go b/core/vm/interface.go index d7c4340e06..41b52a10dc 100644 --- a/core/vm/interface.go +++ b/core/vm/interface.go @@ -52,7 +52,6 @@ type StateDB interface { GetStateAndCommittedState(common.Address, common.Hash) (common.Hash, common.Hash) GetState(common.Address, common.Hash) common.Hash SetState(common.Address, common.Hash, common.Hash) common.Hash - GetStorageRoot(addr common.Address) common.Hash GetTransientState(addr common.Address, key common.Hash) common.Hash SetTransientState(addr common.Address, key, value common.Hash) diff --git a/tests/block_test.go b/tests/block_test.go index c718b304b6..0f087967bb 100644 --- a/tests/block_test.go +++ b/tests/block_test.go @@ -66,6 +66,12 @@ func TestBlockchain(t *testing.T) { // This directory contains no test. bt.skipLoad(`.*\.meta/.*`) + // Broken tests + bt.skipLoad(`RevertInCreateInInit`) + bt.skipLoad(`InitCollisionParis`) + bt.skipLoad(`dynamicAccountOverwriteEmpty_Paris`) + bt.skipLoad(`create2collisionStorageParis`) + bt.walk(t, blockTestDir, func(t *testing.T, name string, test *BlockTest) { execBlockTest(t, bt, test) }) @@ -85,6 +91,12 @@ func TestExecutionSpecBlocktests(t *testing.T) { bt.skipLoad(".*prague/eip7251_consolidations/test_system_contract_deployment.json") bt.skipLoad(".*prague/eip7002_el_triggerable_withdrawals/test_system_contract_deployment.json") + // Broken tests + bt.skipLoad(`RevertInCreateInInit`) + bt.skipLoad(`InitCollisionParis`) + bt.skipLoad(`dynamicAccountOverwriteEmpty_Paris`) + bt.skipLoad(`create2collisionStorageParis`) + bt.walk(t, executionSpecBlockchainTestDir, func(t *testing.T, name string, test *BlockTest) { execBlockTest(t, bt, test) }) diff --git a/tests/state_test.go b/tests/state_test.go index f80bda4372..25e90d388d 100644 --- a/tests/state_test.go +++ b/tests/state_test.go @@ -57,6 +57,11 @@ func initMatcher(st *testMatcher) { // Broken tests: // EOF is not part of cancun st.skipLoad(`^stEOF/`) + + st.skipLoad(`RevertInCreateInInit`) + st.skipLoad(`InitCollisionParis`) + st.skipLoad(`dynamicAccountOverwriteEmpty_Paris`) + st.skipLoad(`create2collisionStorageParis`) } func TestState(t *testing.T) { @@ -92,6 +97,12 @@ func TestExecutionSpecState(t *testing.T) { } st := new(testMatcher) + // Broken tests + st.skipLoad(`RevertInCreateInInit`) + st.skipLoad(`InitCollisionParis`) + st.skipLoad(`dynamicAccountOverwriteEmpty_Paris`) + st.skipLoad(`create2collisionStorageParis`) + st.walk(t, executionSpecStateTestDir, func(t *testing.T, name string, test *StateTest) { execStateTest(t, st, test) }) From 2253fce48d29cf252ae57f28e8bbd14e133b14c3 Mon Sep 17 00:00:00 2001 From: cui Date: Tue, 14 Apr 2026 22:09:17 +0800 Subject: [PATCH 25/40] core/types: remove redundant ')' (#34719) --- core/types/tx_legacy.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/types/tx_legacy.go b/core/types/tx_legacy.go index 49f0a98809..eca9e210af 100644 --- a/core/types/tx_legacy.go +++ b/core/types/tx_legacy.go @@ -121,7 +121,7 @@ func (tx *LegacyTx) encode(*bytes.Buffer) error { } func (tx *LegacyTx) decode([]byte) error { - panic("decode called on LegacyTx)") + panic("decode called on LegacyTx") } // OBS: This is the post-EIP155 hash, the pre-EIP155 does not contain a chainID. From c9fea44616560d42c4a67704c1cb79a1445741f8 Mon Sep 17 00:00:00 2001 From: felipe Date: Tue, 14 Apr 2026 17:00:29 -0600 Subject: [PATCH 26/40] eth/catalyst: respect slot num if specified in payload attributes for `testing_buildBlockV1` (#34722) This is a copy of #34721 but against `master` (rather than `bal-devnet-3`), as requested by @jwasinger, since the slotnum logic now exists on `master` as well. --- eth/catalyst/api_testing.go | 1 + 1 file changed, 1 insertion(+) diff --git a/eth/catalyst/api_testing.go b/eth/catalyst/api_testing.go index 8586029468..2818d7f0bb 100644 --- a/eth/catalyst/api_testing.go +++ b/eth/catalyst/api_testing.go @@ -74,6 +74,7 @@ func (api *testingAPI) BuildBlockV1(parentHash common.Hash, payloadAttributes en Random: payloadAttributes.Random, Withdrawals: payloadAttributes.Withdrawals, BeaconRoot: payloadAttributes.BeaconRoot, + SlotNum: payloadAttributes.SlotNumber, } return api.eth.Miner().BuildTestingPayload(args, txs, buildEmpty, extra) } From ef0f1f96f96a90a3ff9ac78605ef7e28cb871b8f Mon Sep 17 00:00:00 2001 From: rjl493456442 Date: Wed, 15 Apr 2026 11:15:43 +0800 Subject: [PATCH 27/40] core/state: ignore the root returned in Commit function for simplicity (#34723) StateDB.Commit first commits all storage changes into the storage trie, then updates the account metadata with the new storage root into the account trie. Within StateDB.Commit, the new storage trie root has already been computed and applied as the storage root. This PR explicitly skips the redundant storage trie root assignment for readability. --- core/state/state_object.go | 5 +++-- core/state/statedb.go | 7 ++----- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/core/state/state_object.go b/core/state/state_object.go index ec0c511737..a4a9f5121b 100644 --- a/core/state/state_object.go +++ b/core/state/state_object.go @@ -482,8 +482,9 @@ func (s *stateObject) commit() (*accountUpdate, *trienode.NodeSet, error) { s.origin = s.data.Copy() return op, nil, nil } - root, nodes := s.trie.Commit(false) - s.data.Root = root + // The storage trie root is omitted, as it has already been updated in the + // previous updateRoot step. + _, nodes := s.trie.Commit(false) s.origin = s.data.Copy() return op, nodes, nil } diff --git a/core/state/statedb.go b/core/state/statedb.go index a4207a10c2..fc2da59a05 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -1162,7 +1162,7 @@ func (s *StateDB) commit(deleteEmptyObjects bool, noStorageWiping bool, blockNum return nil, fmt.Errorf("commit aborted due to earlier error: %v", s.dbErr) } // Finalize any pending changes and merge everything into the tries - s.IntermediateRoot(deleteEmptyObjects) + root := s.IntermediateRoot(deleteEmptyObjects) // Short circuit if any error occurs within the IntermediateRoot. if s.dbErr != nil { @@ -1224,7 +1224,6 @@ func (s *StateDB) commit(deleteEmptyObjects bool, noStorageWiping bool, blockNum // writes to run in parallel with the computations. var ( start = time.Now() - root common.Hash workers errgroup.Group ) // Schedule the account trie first since that will be the biggest, so give @@ -1238,9 +1237,7 @@ func (s *StateDB) commit(deleteEmptyObjects bool, noStorageWiping bool, blockNum // code didn't anticipate for. workers.Go(func() error { // Write the account trie changes, measuring the amount of wasted time - newroot, set := s.trie.Commit(true) - root = newroot - + _, set := s.trie.Commit(true) if err := merge(set); err != nil { return err } From 0b35ad95f5cf307e243a7ec338fc1f443d66cecc Mon Sep 17 00:00:00 2001 From: Rahman Date: Thu, 16 Apr 2026 06:17:01 +0300 Subject: [PATCH 28/40] cmd/utils: fix witness stats auto-enable to respect config file (#34729) Auto-enable logic for `StatelessSelfValidation` was reading CLI flag directly via `ctx.Bool()`, bypassing the merged `cfg.EnableWitnessStats` value. Now uses `cfg.EnableWitnessStats` so config file settings trigger the same auto-enable behavior as CLI flags. --- cmd/utils/flags.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index c1284044eb..b6f64bdc15 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -1899,7 +1899,7 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) { cfg.StatelessSelfValidation = ctx.Bool(VMStatelessSelfValidationFlag.Name) } // Auto-enable StatelessSelfValidation when witness stats are enabled - if ctx.Bool(VMWitnessStatsFlag.Name) { + if cfg.EnableWitnessStats { cfg.StatelessSelfValidation = true } From d07a946a5b9a94125e682bdc89cf4e49d43ae6c2 Mon Sep 17 00:00:00 2001 From: Jonny Rhea <5555162+jrhea@users.noreply.github.com> Date: Thu, 16 Apr 2026 01:53:08 -0500 Subject: [PATCH 29/40] =?UTF-8?q?log:=20allow=20=E2=80=93vmodule=20to=20do?= =?UTF-8?q?wngrade=20log=20levels=20(#33111)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Changes the log handler to check for vmodule level overrides even for messages above the current level. This enables the user to selectively hide messages from certain packages, among other things. Also fixes a bug where handler instances created by WithAttr would not follow the level setting anymore. The WithAttrs method is calledd by slog.Logger.With, which we also use in go-ethereum to create context specific loggers with pre-filled attributes. Under the previous implementation of WithAttrs, if the application created a long-lived logger (for example, for a specific peer), then that logger would not be affected by later level changes done on the top-level logger, leading to potentially missed events. Closes: #30717 --------- Co-authored-by: Marius van der Wijden Co-authored-by: Felix Lange --- log/handler_glog.go | 147 ++++++++++++++++++++++++-------------------- log/logger_test.go | 58 +++++++++++++++++ 2 files changed, 139 insertions(+), 66 deletions(-) diff --git a/log/handler_glog.go b/log/handler_glog.go index 739f8c5b42..13afb62ca4 100644 --- a/log/handler_glog.go +++ b/log/handler_glog.go @@ -19,9 +19,7 @@ package log import ( "context" "errors" - "fmt" "log/slog" - "maps" "regexp" "runtime" "strconv" @@ -38,22 +36,22 @@ var errVmoduleSyntax = errors.New("expect comma-separated list of filename=N") // matches; and requesting backtraces at certain positions. type GlogHandler struct { origin slog.Handler // The origin handler this wraps + lock sync.Mutex // synchronizes writes to config + config atomic.Pointer[glogConfig] +} - level atomic.Int32 // Current log level, atomically accessible - override atomic.Bool // Flag whether overrides are used, atomically accessible - - patterns []pattern // Current list of patterns to override with - siteCache map[uintptr]slog.Level // Cache of callsite pattern evaluations - location string // file:line location where to do a stackdump at - lock sync.RWMutex // Lock protecting the override pattern list +type glogConfig struct { + patterns []pattern + cache sync.Map + level slog.Level } // NewGlogHandler creates a new log handler with filtering functionality similar // to Google's glog logger. The returned handler implements Handler. -func NewGlogHandler(h slog.Handler) *GlogHandler { - return &GlogHandler{ - origin: h, - } +func NewGlogHandler(origin slog.Handler) *GlogHandler { + h := &GlogHandler{origin: origin} + h.config.Store(new(glogConfig)) + return h } // pattern contains a filter for the Vmodule option, holding a verbosity level @@ -66,7 +64,12 @@ type pattern struct { // Verbosity sets the glog verbosity ceiling. The verbosity of individual packages // and source files can be raised using Vmodule. func (h *GlogHandler) Verbosity(level slog.Level) { - h.level.Store(int32(level)) + h.lock.Lock() + defer h.lock.Unlock() + + cfg := h.config.Load() + newcfg := &glogConfig{level: level, patterns: cfg.patterns} + h.config.Store(newcfg) } // Vmodule sets the glog verbosity pattern. @@ -128,13 +131,13 @@ func (h *GlogHandler) Vmodule(ruleset string) error { re, _ := regexp.Compile(matcher) filter = append(filter, pattern{re, level}) } + // Swap out the vmodule pattern for the new filter system h.lock.Lock() - defer h.lock.Unlock() - - h.patterns = filter - h.siteCache = make(map[uintptr]slog.Level) - h.override.Store(len(filter) != 0) + cfg := h.config.Load() + newcfg := &glogConfig{level: cfg.level, patterns: filter} + h.config.Store(newcfg) + h.lock.Unlock() return nil } @@ -142,30 +145,9 @@ func (h *GlogHandler) Vmodule(ruleset string) error { // Enabled implements slog.Handler, reporting whether the handler handles records // at the given level. func (h *GlogHandler) Enabled(ctx context.Context, lvl slog.Level) bool { - // fast-track skipping logging if override not enabled and the provided verbosity is above configured - return h.override.Load() || slog.Level(h.level.Load()) <= lvl -} - -// WithAttrs implements slog.Handler, returning a new Handler whose attributes -// consist of both the receiver's attributes and the arguments. -func (h *GlogHandler) WithAttrs(attrs []slog.Attr) slog.Handler { - h.lock.RLock() - siteCache := maps.Clone(h.siteCache) - h.lock.RUnlock() - - patterns := []pattern{} - patterns = append(patterns, h.patterns...) - - res := GlogHandler{ - origin: h.origin.WithAttrs(attrs), - patterns: patterns, - siteCache: siteCache, - location: h.location, - } - - res.level.Store(h.level.Load()) - res.override.Store(h.override.Load()) - return &res + // fast-track skipping logging if vmodule is not enabled or level too low + cfg := h.config.Load() + return len(cfg.patterns) > 0 || cfg.level <= lvl } // WithGroup implements slog.Handler, returning a new Handler with the given @@ -178,37 +160,70 @@ func (h *GlogHandler) WithGroup(name string) slog.Handler { // Handle implements slog.Handler, filtering a log record through the global, // local and backtrace filters, finally emitting it if either allow it through. -func (h *GlogHandler) Handle(_ context.Context, r slog.Record) error { - // If the global log level allows, fast track logging - if slog.Level(h.level.Load()) <= r.Level { - return h.origin.Handle(context.Background(), r) - } - - // Check callsite cache for previously calculated log levels - h.lock.RLock() - lvl, ok := h.siteCache[r.PC] - h.lock.RUnlock() +func (h *GlogHandler) Handle(ctx context.Context, r slog.Record) error { + return h.handle(ctx, r, h.origin) +} - // If we didn't cache the callsite yet, calculate it - if !ok { - h.lock.Lock() +func (h *GlogHandler) handle(ctx context.Context, r slog.Record, origin slog.Handler) error { + cfg := h.config.Load() + var lvl slog.Level + cachedLvl, ok := cfg.cache.Load(r.PC) + if ok { + // Fast path: cache hit + lvl = cachedLvl.(slog.Level) + } else { + // Resolve the callsite file. fs := runtime.CallersFrames([]uintptr{r.PC}) frame, _ := fs.Next() - - for _, rule := range h.patterns { - if rule.pattern.MatchString(fmt.Sprintf("+%s", frame.File)) { - h.siteCache[r.PC], lvl, ok = rule.level, rule.level, true + file := frame.File + // Match against patterns and cache the level applied at this callsite. + lvl = cfg.level // default: use global level + for _, rule := range cfg.patterns { + if rule.pattern.MatchString("+" + file) { + lvl = rule.level } } - // If no rule matched, remember to drop log the next time - if !ok { - h.siteCache[r.PC] = 0 - } - h.lock.Unlock() + cfg.cache.Store(r.PC, lvl) } + + // Handle the message. if lvl <= r.Level { - return h.origin.Handle(context.Background(), r) + return origin.Handle(ctx, r) } return nil } + +// WithAttrs implements slog.Handler, returning a new Handler whose attributes +// consist of both the receiver's attributes and the arguments. +// +// Note the handler created here will still listen to Verbosity and Vmodule settings +// done on the original handler. +func (h *GlogHandler) WithAttrs(attrs []slog.Attr) slog.Handler { + return &glogWithAttrs{base: h, origin: h.origin.WithAttrs(attrs)} +} + +type glogWithAttrs struct { + base *GlogHandler + origin slog.Handler +} + +func (wh *glogWithAttrs) Enabled(ctx context.Context, lvl slog.Level) bool { + return wh.base.Enabled(ctx, lvl) +} + +func (wh *glogWithAttrs) Handle(ctx context.Context, r slog.Record) error { + return wh.base.handle(ctx, r, wh.origin) +} + +func (wh *glogWithAttrs) WithAttrs(attrs []slog.Attr) slog.Handler { + return &glogWithAttrs{base: wh.base, origin: wh.origin.WithAttrs(attrs)} +} + +// WithGroup implements slog.Handler, returning a new Handler with the given +// group appended to the receiver's existing groups. +// +// Note, this function is not implemented. +func (wh *glogWithAttrs) WithGroup(name string) slog.Handler { + panic("not implemented") +} diff --git a/log/logger_test.go b/log/logger_test.go index dae8497204..c585ddab66 100644 --- a/log/logger_test.go +++ b/log/logger_test.go @@ -33,6 +33,64 @@ func TestLoggingWithVmodule(t *testing.T) { } } +// TestLoggingWithVmoduleDowngrade checks that vmodule can be downgraded. +func TestLoggingWithVmoduleDowngrade(t *testing.T) { + out := new(bytes.Buffer) + glog := NewGlogHandler(NewTerminalHandlerWithLevel(out, LevelTrace, false)) + glog.Verbosity(LevelTrace) // Allow all logs globally + logger := NewLogger(glog) + + // This should appear (global level allows it) + logger.Info("before vmodule downgrade, this should be logged") + if !bytes.Contains(out.Bytes(), []byte("before vmodule downgrade")) { + t.Fatal("expected 'before vmodule downgrade' to be logged") + } + out.Reset() + + // Downgrade this file to only allow Warn and above + glog.Vmodule("logger_test.go=2") + + // Info should now be filtered out + logger.Info("after vmodule downgrade, this should be filtered") + if bytes.Contains(out.Bytes(), []byte("after vmodule downgrade, this should be filtered")) { + t.Fatal("expected 'after vmodule downgrade, this should be filtered' to NOT be logged after vmodule downgrade") + } + + // Warn should still appear + logger.Warn("after vmodule downgrade, this should be logged") + if !bytes.Contains(out.Bytes(), []byte("after vmodule downgrade, this should be logged")) { + t.Fatal("expected 'should appear' to be logged") + } +} + +// TestWithAttrsVerbosityChange checks that verbosity changes affect child loggers. +func TestWithAttrsVerbosityChange(t *testing.T) { + out := new(bytes.Buffer) + glog := NewGlogHandler(NewTerminalHandlerWithLevel(out, LevelTrace, false)) + glog.Verbosity(LevelInfo) + + // Create a child logger with an extra attribute. + child := slog.New(glog.WithAttrs([]slog.Attr{slog.String("peer", "foo")})) + + // Debug should be filtered at Info level. + child.Debug("this should be filtered") + if bytes.Contains(out.Bytes(), []byte("this should be filtered")) { + t.Fatal("expected debug message to be filtered at Info level") + } + + // Change verbosity on the parent to allow Debug. + glog.Verbosity(LevelDebug) + + // Child should pick up the new level and include its attributes. + child.Debug("this should be logged") + if !bytes.Contains(out.Bytes(), []byte("this should be logged")) { + t.Fatal("expected child logger to pick up verbosity change") + } + if !bytes.Contains(out.Bytes(), []byte("peer=foo")) { + t.Fatal("expected child logger to include WithAttrs attributes") + } +} + func TestTerminalHandlerWithAttrs(t *testing.T) { out := new(bytes.Buffer) glog := NewGlogHandler(NewTerminalHandlerWithLevel(out, LevelTrace, false).WithAttrs([]slog.Attr{slog.String("baz", "bat")})) From b1baab4427ef8d6f1b09c9465a89a46623d5fbde Mon Sep 17 00:00:00 2001 From: Daniel Liu <139250065@qq.com> Date: Thu, 16 Apr 2026 16:16:53 +0800 Subject: [PATCH 30/40] cmd/evm: fix trace.noreturndata usage string (#34731) `trace.noreturndata` is documented as "enable return data output" but the flag name/value imply it disables return data. This is confusing for users and likely inverted wording. Update the Usage string to reflect the actual behavior (disable return data output). --- cmd/evm/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/evm/main.go b/cmd/evm/main.go index 77a06bec26..2b77741738 100644 --- a/cmd/evm/main.go +++ b/cmd/evm/main.go @@ -115,7 +115,7 @@ var ( Name: "trace.noreturndata", Aliases: []string{"noreturndata"}, Value: true, - Usage: "enable return data output", + Usage: "disable return data output", Category: traceCategory, } From f63e9f3a80cd568f6d68c3c9858839784220d038 Mon Sep 17 00:00:00 2001 From: Sina M <1591639+s1na@users.noreply.github.com> Date: Thu, 16 Apr 2026 10:51:26 +0200 Subject: [PATCH 31/40] eth/tracers: fix codehash in prestate diffmode (#34675) Fixes https://github.com/ethereum/go-ethereum/issues/34648. --- .../eip7702_deauth.json | 98 +++++++++++++++++++ eth/tracers/native/gen_account_json.go | 6 +- eth/tracers/native/prestate.go | 38 ++++--- 3 files changed, 125 insertions(+), 17 deletions(-) create mode 100644 eth/tracers/internal/tracetest/testdata/prestate_tracer_with_diff_mode/eip7702_deauth.json diff --git a/eth/tracers/internal/tracetest/testdata/prestate_tracer_with_diff_mode/eip7702_deauth.json b/eth/tracers/internal/tracetest/testdata/prestate_tracer_with_diff_mode/eip7702_deauth.json new file mode 100644 index 0000000000..e376a98946 --- /dev/null +++ b/eth/tracers/internal/tracetest/testdata/prestate_tracer_with_diff_mode/eip7702_deauth.json @@ -0,0 +1,98 @@ +{ + "genesis": { + "blobGasUsed": "0", + "difficulty": "0", + "excessBlobGas": "0", + "extraData": "0x", + "gasLimit": "11511229", + "hash": "0x455b93a512baa4ed5e117508b184a6bb03904b94d665ce38931728eca9cdd8fe", + "miner": "0x71562b71999873db5b286df957af199ec94617f7", + "mixHash": "0x042877c4fab9f022d29590ae83bad89d6181afb1d6e107619911ea52e5901364", + "nonce": "0x0000000000000000", + "number": "1", + "parentBeaconBlockRoot": "0x0000000000000000000000000000000000000000000000000000000000000000", + "stateRoot": "0xc8688ad6433e6b9f4edeb82360d2b99c8e919f493a01cacbe7c4a97184f5d043", + "timestamp": "1775654796", + "alloc": { + "0x71562b71999873db5b286df957af199ec94617f7": { + "balance": "0xffffffffffffffffffffffffffffffffffffffffffffffffffffdb64910c3bf7", + "nonce": "1" + }, + "0xe85a1c0e9d5b1c9b417c6c1b34c22cd77f623f50": { + "balance": "0x0", + "code": "0xef0100d313d93607c016a85e63e557a11ca5ab0b53ad83", + "codeHash": "0x9eea9f41ed2b35e6234d1e1c14e88c1136f85d56ed1f32a7efc0096d998dad3d", + "nonce": "1" + } + }, + "config": { + "chainId": 1337, + "homesteadBlock": 0, + "eip150Block": 0, + "eip155Block": 0, + "eip158Block": 0, + "byzantiumBlock": 0, + "constantinopleBlock": 0, + "petersburgBlock": 0, + "istanbulBlock": 0, + "muirGlacierBlock": 0, + "berlinBlock": 0, + "londonBlock": 0, + "arrowGlacierBlock": 0, + "grayGlacierBlock": 0, + "shanghaiTime": 0, + "cancunTime": 0, + "pragueTime": 0, + "terminalTotalDifficulty": 0, + "blobSchedule": { + "cancun": { + "target": 3, + "max": 6, + "baseFeeUpdateFraction": 3338477 + }, + "prague": { + "target": 6, + "max": 9, + "baseFeeUpdateFraction": 5007716 + } + } + } + }, + "context": { + "number": "2", + "difficulty": "0", + "timestamp": "1775654797", + "gasLimit": "11522469", + "miner": "0x71562b71999873db5b286df957af199ec94617f7", + "baseFeePerGas": "766499147" + }, + "input": "0x04f8cd82053901843b9aca008477359400830186a09471562b71999873db5b286df957af199ec94617f78080c0f85ef85c8205399400000000000000000000000000000000000000000101a011fc0271f2566e7ebe5ddbff6d48ea97a19afa248452a392781096b7e3b89177a0020107ecefe99c90429b416fe4d1eead5a7fa253761e85cd7cdc7df6e5032d7f80a098495fb16c904f0b67b49afe868b28b0159c8df07522bed99ef6ff2cc2ac2935a048857a9c385d91735a9fdccabc66de7a5ea1897f523a5b9a352e281642a76e6b", + "tracerConfig": { + "diffMode": true + }, + "result": { + "post": { + "0x71562b71999873db5b286df957af199ec94617f7": { + "balance": "0xffffffffffffffffffffffffffffffffffffffffffffffffffffc1bd12c85eb7", + "nonce": 2 + }, + "0xe85a1c0e9d5b1c9b417c6c1b34c22cd77f623f50": { + "code": "0x", + "codeHash": "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470", + "nonce": 2 + } + }, + "pre": { + "0x71562b71999873db5b286df957af199ec94617f7": { + "balance": "0xffffffffffffffffffffffffffffffffffffffffffffffffffffdb64910c3bf7", + "nonce": 1 + }, + "0xe85a1c0e9d5b1c9b417c6c1b34c22cd77f623f50": { + "balance": "0x0", + "code": "0xef0100d313d93607c016a85e63e557a11ca5ab0b53ad83", + "codeHash": "0x9eea9f41ed2b35e6234d1e1c14e88c1136f85d56ed1f32a7efc0096d998dad3d", + "nonce": 1 + } + } + } +} diff --git a/eth/tracers/native/gen_account_json.go b/eth/tracers/native/gen_account_json.go index 5fec2648b7..9417536a23 100644 --- a/eth/tracers/native/gen_account_json.go +++ b/eth/tracers/native/gen_account_json.go @@ -16,14 +16,14 @@ var _ = (*accountMarshaling)(nil) func (a account) MarshalJSON() ([]byte, error) { type account struct { Balance *hexutil.Big `json:"balance,omitempty"` - Code hexutil.Bytes `json:"code,omitempty"` + Code *hexutil.Bytes `json:"code,omitempty"` CodeHash *common.Hash `json:"codeHash,omitempty"` Nonce uint64 `json:"nonce,omitempty"` Storage map[common.Hash]common.Hash `json:"storage,omitempty"` } var enc account enc.Balance = (*hexutil.Big)(a.Balance) - enc.Code = a.Code + enc.Code = (*hexutil.Bytes)(a.Code) enc.CodeHash = a.CodeHash enc.Nonce = a.Nonce enc.Storage = a.Storage @@ -47,7 +47,7 @@ func (a *account) UnmarshalJSON(input []byte) error { a.Balance = (*big.Int)(dec.Balance) } if dec.Code != nil { - a.Code = *dec.Code + a.Code = (*[]byte)(dec.Code) } if dec.CodeHash != nil { a.CodeHash = dec.CodeHash diff --git a/eth/tracers/native/prestate.go b/eth/tracers/native/prestate.go index 159a91b310..36cb16e44b 100644 --- a/eth/tracers/native/prestate.go +++ b/eth/tracers/native/prestate.go @@ -44,21 +44,24 @@ func init() { type stateMap = map[common.Address]*account type account struct { - Balance *big.Int `json:"balance,omitempty"` - Code []byte `json:"code,omitempty"` + Balance *big.Int `json:"balance,omitempty"` + // Code is a pointer so omitempty can omit unchanged code (nil) while + // still emitting "0x" when code is cleared (e.g. EIP-7702 deauth). + Code *[]byte `json:"code,omitempty"` CodeHash *common.Hash `json:"codeHash,omitempty"` Nonce uint64 `json:"nonce,omitempty"` Storage map[common.Hash]common.Hash `json:"storage,omitempty"` - empty bool + + empty bool } func (a *account) exists() bool { - return a.Nonce > 0 || len(a.Code) > 0 || len(a.Storage) > 0 || (a.Balance != nil && a.Balance.Sign() != 0) + return a.Nonce > 0 || (a.Code != nil && len(*a.Code) > 0) || len(a.Storage) > 0 || (a.Balance != nil && a.Balance.Sign() != 0) } type accountMarshaling struct { Balance *hexutil.Big - Code hexutil.Bytes + Code *hexutil.Bytes } type prestateTracer struct { @@ -266,24 +269,28 @@ func (t *prestateTracer) processDiffState() { modified = true postAccount.Nonce = newNonce } - prevCodeHash := common.Hash{} + // Empty code hashes are excluded from the prestate, so default + // to EmptyCodeHash to match what GetCodeHash returns for codeless accounts. + prevCodeHash := types.EmptyCodeHash if t.pre[addr].CodeHash != nil { prevCodeHash = *t.pre[addr].CodeHash } - // Empty code hashes are excluded from the prestate. Normalize - // the empty code hash to a zero hash to make it comparable. - if newCodeHash == types.EmptyCodeHash { - newCodeHash = common.Hash{} - } if newCodeHash != prevCodeHash { modified = true postAccount.CodeHash = &newCodeHash } if !t.config.DisableCode { newCode := t.env.StateDB.GetCode(addr) - if !bytes.Equal(newCode, t.pre[addr].Code) { + var prevCode []byte + if t.pre[addr].Code != nil { + prevCode = *t.pre[addr].Code + } + if !bytes.Equal(newCode, prevCode) { modified = true - postAccount.Code = newCode + if newCode == nil { + newCode = []byte{} + } + postAccount.Code = &newCode } } @@ -323,10 +330,13 @@ func (t *prestateTracer) lookupAccount(addr common.Address) { return } + code := t.env.StateDB.GetCode(addr) acc := &account{ Balance: t.env.StateDB.GetBalance(addr).ToBig(), Nonce: t.env.StateDB.GetNonce(addr), - Code: t.env.StateDB.GetCode(addr), + } + if len(code) > 0 { + acc.Code = &code } codeHash := t.env.StateDB.GetCodeHash(addr) // If the code is empty, we don't need to store it in the prestate. From ba215fd927b2e6377a64cb0b4ce668f106763119 Mon Sep 17 00:00:00 2001 From: Guillaume Ballet <3272758+gballet@users.noreply.github.com> Date: Fri, 17 Apr 2026 02:55:54 +0200 Subject: [PATCH 32/40] cmd, core, trie, triedb: split CachingDB into merkle + binary dbs. (#34700) This Pr implements some prerequisite changes for #34004 : split the `CachingDB` into a `MerkleDB` and a `UBTDB`, so that very different behaviors don't clash as much. The transition isn't handled by this PR, but after talking to Gary we agreed that `UBTDB` should receive another `triedb`, which will only be loaded if the `Ended` flag is set to false in the conversion contract. If this is too hard to achieve, it makes sense to load it regardless, and then loading can be prevented at a later stage by adding a `UBTTransitionFinalizationTime` in `ChainConfig`. --------- Co-authored-by: Gary Rong --- cmd/evm/internal/t8ntool/execution.go | 4 +- cmd/evm/internal/t8ntool/transition.go | 6 +- cmd/geth/bintrie_convert.go | 2 +- cmd/geth/bintrie_convert_test.go | 8 +- cmd/geth/chaincmd.go | 10 +- cmd/geth/config.go | 6 +- cmd/geth/main.go | 2 +- cmd/utils/flags.go | 10 +- core/bintrie_witness_test.go | 34 +-- core/blockchain.go | 30 ++- core/blockchain_reader.go | 35 ++- core/blockchain_test.go | 2 +- core/chain_makers.go | 12 +- core/genesis.go | 44 ++-- core/genesis_test.go | 12 +- core/overlay/state_transition.go | 6 +- core/state/database.go | 199 +++--------------- core/state/database_history.go | 6 + core/state/database_mpt.go | 178 ++++++++++++++++ core/state/database_ubt.go | 138 ++++++++++++ core/state/reader.go | 6 +- core/state/state_object.go | 4 +- core/state/statedb.go | 14 +- core/state/statedb_fuzz_test.go | 2 +- core/state/statedb_test.go | 6 +- core/state/trie_prefetcher.go | 16 +- core/state/trie_prefetcher_test.go | 2 +- core/state_processor.go | 4 +- core/txpool/blobpool/blobpool.go | 6 +- core/txpool/blobpool/blobpool_test.go | 7 +- core/txpool/blobpool/interface.go | 7 +- core/txpool/legacypool/legacypool.go | 13 +- core/txpool/legacypool/legacypool_test.go | 6 +- core/txpool/locals/tx_tracker_test.go | 4 +- core/txpool/txpool.go | 13 +- core/types/hashes.go | 3 - core/vm/contracts.go | 2 +- core/vm/evm.go | 2 +- core/vm/jump_table_export.go | 2 +- eth/api_backend.go | 8 +- eth/api_debug.go | 6 +- eth/backend.go | 4 +- eth/catalyst/api_test.go | 8 +- eth/ethconfig/config.go | 4 +- eth/ethconfig/gen_config.go | 10 +- eth/gasprice/gasprice_test.go | 2 +- eth/state_accessor.go | 7 +- eth/tracers/api.go | 4 +- eth/tracers/api_test.go | 2 +- .../tracetest/selfdestruct_state_test.go | 2 +- internal/ethapi/api_test.go | 2 +- internal/ethapi/simulate.go | 2 +- miner/miner_test.go | 6 +- miner/worker.go | 2 +- params/config.go | 64 +++--- tests/block_test_util.go | 4 +- tests/init.go | 2 +- tests/state_test_util.go | 2 +- trie/bintrie/trie.go | 4 +- trie/secure_trie.go | 2 +- trie/transitiontrie/transition.go | 4 +- triedb/database.go | 18 +- triedb/pathdb/database.go | 20 +- triedb/pathdb/database_test.go | 4 +- triedb/pathdb/history.go | 6 +- triedb/pathdb/layertree_test.go | 102 ++++----- triedb/pathdb/reader.go | 2 +- 67 files changed, 703 insertions(+), 463 deletions(-) create mode 100644 core/state/database_mpt.go create mode 100644 core/state/database_ubt.go diff --git a/cmd/evm/internal/t8ntool/execution.go b/cmd/evm/internal/t8ntool/execution.go index efe22d36f5..f17829ec53 100644 --- a/cmd/evm/internal/t8ntool/execution.go +++ b/cmd/evm/internal/t8ntool/execution.go @@ -146,7 +146,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig, return h } var ( - isEIP4762 = chainConfig.IsVerkle(big.NewInt(int64(pre.Env.Number)), pre.Env.Timestamp) + isEIP4762 = chainConfig.IsUBT(big.NewInt(int64(pre.Env.Number)), pre.Env.Timestamp) statedb = MakePreState(rawdb.NewMemoryDatabase(), pre.Pre, isEIP4762) signer = types.MakeSigner(chainConfig, new(big.Int).SetUint64(pre.Env.Number), pre.Env.Timestamp) gaspool = core.NewGasPool(pre.Env.GasLimit) @@ -378,7 +378,7 @@ func (pre *Prestate) Apply(vmConfig vm.Config, chainConfig *params.ChainConfig, } func MakePreState(db ethdb.Database, accounts types.GenesisAlloc, isBintrie bool) *state.StateDB { - tdb := triedb.NewDatabase(db, &triedb.Config{Preimages: true, IsVerkle: isBintrie}) + tdb := triedb.NewDatabase(db, &triedb.Config{Preimages: true, IsUBT: isBintrie}) sdb := state.NewDatabase(tdb, nil) root := types.EmptyRootHash diff --git a/cmd/evm/internal/t8ntool/transition.go b/cmd/evm/internal/t8ntool/transition.go index d7cdc98e74..6a23e9dc70 100644 --- a/cmd/evm/internal/t8ntool/transition.go +++ b/cmd/evm/internal/t8ntool/transition.go @@ -228,7 +228,7 @@ func Transition(ctx *cli.Context) error { collector = make(Alloc) btleaves map[common.Hash]hexutil.Bytes ) - isBinary := chainConfig.IsVerkle(big.NewInt(int64(prestate.Env.Number)), prestate.Env.Timestamp) + isBinary := chainConfig.IsUBT(big.NewInt(int64(prestate.Env.Number)), prestate.Env.Timestamp) if !isBinary { s.DumpToCollector(collector, nil) } else { @@ -452,7 +452,7 @@ func BinKeys(ctx *cli.Context) error { return err } } - db := triedb.NewDatabase(rawdb.NewMemoryDatabase(), triedb.VerkleDefaults) + db := triedb.NewDatabase(rawdb.NewMemoryDatabase(), triedb.UBTDefaults) defer db.Close() bt, err := genBinTrieFromAlloc(alloc, db) @@ -496,7 +496,7 @@ func BinTrieRoot(ctx *cli.Context) error { return err } } - db := triedb.NewDatabase(rawdb.NewMemoryDatabase(), triedb.VerkleDefaults) + db := triedb.NewDatabase(rawdb.NewMemoryDatabase(), triedb.UBTDefaults) defer db.Close() bt, err := genBinTrieFromAlloc(alloc, db) diff --git a/cmd/geth/bintrie_convert.go b/cmd/geth/bintrie_convert.go index 3730768697..43d2e629ac 100644 --- a/cmd/geth/bintrie_convert.go +++ b/cmd/geth/bintrie_convert.go @@ -144,7 +144,7 @@ func convertToBinaryTrie(ctx *cli.Context) error { defer srcTriedb.Close() destTriedb := triedb.NewDatabase(chaindb, &triedb.Config{ - IsVerkle: true, + IsUBT: true, PathDB: &pathdb.Config{ JournalDirectory: stack.ResolvePath("triedb-bintrie"), }, diff --git a/cmd/geth/bintrie_convert_test.go b/cmd/geth/bintrie_convert_test.go index 9b95f6a70f..50ae752358 100644 --- a/cmd/geth/bintrie_convert_test.go +++ b/cmd/geth/bintrie_convert_test.go @@ -82,8 +82,8 @@ func TestBintrieConvert(t *testing.T) { defer srcTriedb2.Close() destTriedb := triedb.NewDatabase(chaindb, &triedb.Config{ - IsVerkle: true, - PathDB: pathdb.Defaults, + IsUBT: true, + PathDB: pathdb.Defaults, }) defer destTriedb.Close() @@ -190,8 +190,8 @@ func TestBintrieConvertDeleteSource(t *testing.T) { }) destTriedb := triedb.NewDatabase(chaindb, &triedb.Config{ - IsVerkle: true, - PathDB: pathdb.Defaults, + IsUBT: true, + PathDB: pathdb.Defaults, }) bt, err := bintrie.NewBinaryTrie(types.EmptyBinaryHash, destTriedb) diff --git a/cmd/geth/chaincmd.go b/cmd/geth/chaincmd.go index 1084100f39..0aacb0878a 100644 --- a/cmd/geth/chaincmd.go +++ b/cmd/geth/chaincmd.go @@ -64,7 +64,7 @@ var ( utils.OverrideOsaka, utils.OverrideBPO1, utils.OverrideBPO2, - utils.OverrideVerkle, + utils.OverrideUBT, }, utils.DatabaseFlags), Description: ` The init command initializes a new genesis block and definition for the network. @@ -297,15 +297,15 @@ func initGenesis(ctx *cli.Context) error { v := ctx.Uint64(utils.OverrideBPO2.Name) overrides.OverrideBPO2 = &v } - if ctx.IsSet(utils.OverrideVerkle.Name) { - v := ctx.Uint64(utils.OverrideVerkle.Name) - overrides.OverrideVerkle = &v + if ctx.IsSet(utils.OverrideUBT.Name) { + v := ctx.Uint64(utils.OverrideUBT.Name) + overrides.OverrideUBT = &v } chaindb := utils.MakeChainDatabase(ctx, stack, false) defer chaindb.Close() - triedb := utils.MakeTrieDatabase(ctx, stack, chaindb, ctx.Bool(utils.CachePreimagesFlag.Name), false, genesis.IsVerkle()) + triedb := utils.MakeTrieDatabase(ctx, stack, chaindb, ctx.Bool(utils.CachePreimagesFlag.Name), false, genesis.IsUBT()) defer triedb.Close() _, hash, compatErr, err := core.SetupGenesisBlockWithOverride(chaindb, triedb, genesis, &overrides, nil) diff --git a/cmd/geth/config.go b/cmd/geth/config.go index 720d1ef9fc..8e2db32d76 100644 --- a/cmd/geth/config.go +++ b/cmd/geth/config.go @@ -235,9 +235,9 @@ func makeFullNode(ctx *cli.Context) *node.Node { v := ctx.Uint64(utils.OverrideBPO2.Name) cfg.Eth.OverrideBPO2 = &v } - if ctx.IsSet(utils.OverrideVerkle.Name) { - v := ctx.Uint64(utils.OverrideVerkle.Name) - cfg.Eth.OverrideVerkle = &v + if ctx.IsSet(utils.OverrideUBT.Name) { + v := ctx.Uint64(utils.OverrideUBT.Name) + cfg.Eth.OverrideUBT = &v } // Start metrics export if enabled. diff --git a/cmd/geth/main.go b/cmd/geth/main.go index e196ac8688..ae869ec970 100644 --- a/cmd/geth/main.go +++ b/cmd/geth/main.go @@ -64,7 +64,7 @@ var ( utils.OverrideOsaka, utils.OverrideBPO1, utils.OverrideBPO2, - utils.OverrideVerkle, + utils.OverrideUBT, utils.OverrideGenesisFlag, utils.EnablePersonal, // deprecated utils.TxPoolLocalsFlag, diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index b6f64bdc15..aff45087db 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -264,9 +264,9 @@ var ( Usage: "Manually specify the bpo2 fork timestamp, overriding the bundled setting", Category: flags.EthCategory, } - OverrideVerkle = &cli.Uint64Flag{ - Name: "override.verkle", - Usage: "Manually specify the Verkle fork timestamp, overriding the bundled setting", + OverrideUBT = &cli.Uint64Flag{ + Name: "override.ubt", + Usage: "Manually specify the UBT fork timestamp, overriding the bundled setting", Category: flags.EthCategory, } OverrideGenesisFlag = &cli.StringFlag{ @@ -2516,10 +2516,10 @@ func MakeConsolePreloads(ctx *cli.Context) []string { } // MakeTrieDatabase constructs a trie database based on the configured scheme. -func MakeTrieDatabase(ctx *cli.Context, stack *node.Node, disk ethdb.Database, preimage bool, readOnly bool, isVerkle bool) *triedb.Database { +func MakeTrieDatabase(ctx *cli.Context, stack *node.Node, disk ethdb.Database, preimage bool, readOnly bool, isUBT bool) *triedb.Database { config := &triedb.Config{ Preimages: preimage, - IsVerkle: isVerkle, + IsUBT: isUBT, } scheme, err := rawdb.ParseStateScheme(ctx.String(StateSchemeFlag.Name), disk) if err != nil { diff --git a/core/bintrie_witness_test.go b/core/bintrie_witness_test.go index 7704ba41fb..e4cb34cb56 100644 --- a/core/bintrie_witness_test.go +++ b/core/bintrie_witness_test.go @@ -36,7 +36,7 @@ import ( ) var ( - testVerkleChainConfig = ¶ms.ChainConfig{ + testUBTChainConfig = ¶ms.ChainConfig{ ChainID: big.NewInt(1), HomesteadBlock: big.NewInt(0), EIP150Block: big.NewInt(0), @@ -51,16 +51,16 @@ var ( LondonBlock: big.NewInt(0), Ethash: new(params.EthashConfig), ShanghaiTime: u64(0), - VerkleTime: u64(0), + UBTTime: u64(0), TerminalTotalDifficulty: common.Big0, - EnableVerkleAtGenesis: true, + EnableUBTAtGenesis: true, BlobScheduleConfig: ¶ms.BlobScheduleConfig{ - Verkle: params.DefaultPragueBlobConfig, + UBT: params.DefaultPragueBlobConfig, }, } ) -func TestProcessVerkle(t *testing.T) { +func TestProcessUBT(t *testing.T) { var ( code = common.FromHex(`6060604052600a8060106000396000f360606040526008565b00`) intrinsicContractCreationGas, _ = IntrinsicGas(code, nil, nil, true, true, true, true) @@ -69,12 +69,12 @@ func TestProcessVerkle(t *testing.T) { // Source: https://gist.github.com/gballet/a23db1e1cb4ed105616b5920feb75985 codeWithExtCodeCopy = common.FromHex(`0x60806040526040516100109061017b565b604051809103906000f08015801561002c573d6000803e3d6000fd5b506000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555034801561007857600080fd5b5060008067ffffffffffffffff8111156100955761009461024a565b5b6040519080825280601f01601f1916602001820160405280156100c75781602001600182028036833780820191505090505b50905060008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1690506020600083833c81610101906101e3565b60405161010d90610187565b61011791906101a3565b604051809103906000f080158015610133573d6000803e3d6000fd5b50600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550505061029b565b60d58061046783390190565b6102068061053c83390190565b61019d816101d9565b82525050565b60006020820190506101b86000830184610194565b92915050565b6000819050602082019050919050565b600081519050919050565b6000819050919050565b60006101ee826101ce565b826101f8846101be565b905061020381610279565b925060208210156102435761023e7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff8360200360080261028e565b831692505b5050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b600061028582516101d9565b80915050919050565b600082821b905092915050565b6101bd806102aa6000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c8063f566852414610030575b600080fd5b61003861004e565b6040516100459190610146565b60405180910390f35b6000600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff166381ca91d36040518163ffffffff1660e01b815260040160206040518083038186803b1580156100b857600080fd5b505afa1580156100cc573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906100f0919061010a565b905090565b60008151905061010481610170565b92915050565b6000602082840312156101205761011f61016b565b5b600061012e848285016100f5565b91505092915050565b61014081610161565b82525050565b600060208201905061015b6000830184610137565b92915050565b6000819050919050565b600080fd5b61017981610161565b811461018457600080fd5b5056fea2646970667358221220a6a0e11af79f176f9c421b7b12f441356b25f6489b83d38cc828a701720b41f164736f6c63430008070033608060405234801561001057600080fd5b5060b68061001f6000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c8063ab5ed15014602d575b600080fd5b60336047565b604051603e9190605d565b60405180910390f35b60006001905090565b6057816076565b82525050565b6000602082019050607060008301846050565b92915050565b600081905091905056fea26469706673582212203a14eb0d5cd07c277d3e24912f110ddda3e553245a99afc4eeefb2fbae5327aa64736f6c63430008070033608060405234801561001057600080fd5b5060405161020638038061020683398181016040528101906100329190610063565b60018160001c6100429190610090565b60008190555050610145565b60008151905061005d8161012e565b92915050565b60006020828403121561007957610078610129565b5b60006100878482850161004e565b91505092915050565b600061009b826100f0565b91506100a6836100f0565b9250827fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff038211156100db576100da6100fa565b5b828201905092915050565b6000819050919050565b6000819050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b600080fd5b610137816100e6565b811461014257600080fd5b50565b60b3806101536000396000f3fe6080604052348015600f57600080fd5b506004361060285760003560e01c806381ca91d314602d575b600080fd5b60336047565b604051603e9190605a565b60405180910390f35b60005481565b6054816073565b82525050565b6000602082019050606d6000830184604d565b92915050565b600081905091905056fea26469706673582212209bff7098a2f526de1ad499866f27d6d0d6f17b74a413036d6063ca6a0998ca4264736f6c63430008070033`) intrinsicCodeWithExtCodeCopyGas, _ = IntrinsicGas(codeWithExtCodeCopy, nil, nil, true, true, true, true) - signer = types.LatestSigner(testVerkleChainConfig) + signer = types.LatestSigner(testUBTChainConfig) testKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") bcdb = rawdb.NewMemoryDatabase() // Database for the blockchain coinbase = common.HexToAddress("0x71562b71999873DB5b286dF957af199Ec94617F7") gspec = &Genesis{ - Config: testVerkleChainConfig, + Config: testUBTChainConfig, Alloc: GenesisAlloc{ coinbase: { Balance: big.NewInt(1000000000000000000), // 1 ether @@ -87,7 +87,7 @@ func TestProcessVerkle(t *testing.T) { }, } ) - // Verkle trees use the snapshot, which must be enabled before the + // UBTs use the snapshot, which must be enabled before the // data is saved into the tree+database. // genesis := gspec.MustCommit(bcdb, triedb) options := DefaultConfig().WithStateScheme(rawdb.PathScheme) @@ -188,7 +188,7 @@ func TestProcessParentBlockHash(t *testing.T) { // block 1 parent hash is 0x0100.... // block 2 parent hash is 0x0200.... // etc - checkBlockHashes := func(statedb *state.StateDB, isVerkle bool) { + checkBlockHashes := func(statedb *state.StateDB, isUBT bool) { statedb.SetNonce(params.HistoryStorageAddress, 1, tracing.NonceChangeUnspecified) statedb.SetCode(params.HistoryStorageAddress, params.HistoryStorageCode, tracing.CodeChangeUnspecified) // Process n blocks, from 1 .. num @@ -196,8 +196,8 @@ func TestProcessParentBlockHash(t *testing.T) { for i := 1; i <= num; i++ { header := &types.Header{ParentHash: common.Hash{byte(i)}, Number: big.NewInt(int64(i)), Difficulty: new(big.Int)} chainConfig := params.MergedTestChainConfig - if isVerkle { - chainConfig = testVerkleChainConfig + if isUBT { + chainConfig = testUBTChainConfig } vmContext := NewEVMBlockContext(header, nil, new(common.Address)) evm := vm.NewEVM(vmContext, statedb, chainConfig, vm.Config{}) @@ -205,9 +205,9 @@ func TestProcessParentBlockHash(t *testing.T) { } // Read block hashes for block 0 .. num-1 for i := 0; i < num; i++ { - have, want := getContractStoredBlockHash(statedb, uint64(i), isVerkle), common.Hash{byte(i + 1)} + have, want := getContractStoredBlockHash(statedb, uint64(i), isUBT), common.Hash{byte(i + 1)} if have != want { - t.Errorf("block %d, verkle=%v, have parent hash %v, want %v", i, isVerkle, have, want) + t.Errorf("block %d, verkle=%v, have parent hash %v, want %v", i, isUBT, have, want) } } } @@ -215,22 +215,22 @@ func TestProcessParentBlockHash(t *testing.T) { statedb, _ := state.New(types.EmptyRootHash, state.NewDatabaseForTesting()) checkBlockHashes(statedb, false) }) - t.Run("Verkle", func(t *testing.T) { + t.Run("UBT", func(t *testing.T) { db := rawdb.NewMemoryDatabase() cacheConfig := DefaultConfig().WithStateScheme(rawdb.PathScheme) cacheConfig.SnapshotLimit = 0 triedb := triedb.NewDatabase(db, cacheConfig.triedbConfig(true)) - statedb, _ := state.New(types.EmptyVerkleHash, state.NewDatabase(triedb, nil)) + statedb, _ := state.New(types.EmptyBinaryHash, state.NewDatabase(triedb, nil)) checkBlockHashes(statedb, true) }) } // getContractStoredBlockHash is a utility method which reads the stored parent blockhash for block 'number' -func getContractStoredBlockHash(statedb *state.StateDB, number uint64, isVerkle bool) common.Hash { +func getContractStoredBlockHash(statedb *state.StateDB, number uint64, isUBT bool) common.Hash { ringIndex := number % params.HistoryServeWindow var key common.Hash binary.BigEndian.PutUint64(key[24:], ringIndex) - if isVerkle { + if isUBT { return statedb.GetState(params.HistoryStorageAddress, key) } return statedb.GetState(params.HistoryStorageAddress, key) diff --git a/core/blockchain.go b/core/blockchain.go index 35b2d35dc7..296ef6bc16 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -258,10 +258,10 @@ func (cfg BlockChainConfig) WithNoAsyncFlush(on bool) *BlockChainConfig { } // triedbConfig derives the configures for trie database. -func (cfg *BlockChainConfig) triedbConfig(isVerkle bool) *triedb.Config { +func (cfg *BlockChainConfig) triedbConfig(isUBT bool) *triedb.Config { config := &triedb.Config{ Preimages: cfg.Preimages, - IsVerkle: isVerkle, + IsUBT: isUBT, } if cfg.StateScheme == rawdb.HashScheme { config.HashDB = &hashdb.Config{ @@ -378,7 +378,7 @@ func NewBlockChain(db ethdb.Database, genesis *Genesis, engine consensus.Engine, } // Open trie database with provided config - enableVerkle, err := EnableVerkleAtGenesis(db, genesis) + enableVerkle, err := EnableUBTAtGenesis(db, genesis) if err != nil { return nil, err } @@ -2116,11 +2116,29 @@ func (bc *BlockChain) ProcessBlock(ctx context.Context, parentRoot common.Hash, startTime = time.Now() statedb *state.StateDB interrupt atomic.Bool - sdb = state.NewDatabase(bc.triedb, bc.codedb).WithSnapshot(bc.snaps) + sdb state.Database ) defer interrupt.Store(true) // terminate the prefetch at the end - if bc.cfg.NoPrefetch { + if bc.chainConfig.IsUBT(block.Number(), block.Time()) { + sdb = state.NewUBTDatabase(bc.triedb, bc.codedb) + } else { + sdb = state.NewMPTDatabase(bc.triedb, bc.codedb).WithSnapshot(bc.snaps) + } + // If prefetching is enabled, run that against the current state to pre-cache + // transactions and probabilistically some of the account/storage trie nodes. + // + // Note: the main processor and prefetcher share the same reader with a local + // cache for mitigating the overhead of state access. + type prewarmReader interface { + // ReadersWithCacheStats creates a pair of state readers that share the + // same underlying state reader and internal state cache, while maintaining + // separate statistics respectively. + ReadersWithCacheStats(stateRoot common.Hash) (state.Reader, state.Reader, error) + } + warmer, ok := sdb.(prewarmReader) + + if bc.cfg.NoPrefetch || !ok { statedb, err = state.New(parentRoot, sdb) if err != nil { return nil, err @@ -2131,7 +2149,7 @@ func (bc *BlockChain) ProcessBlock(ctx context.Context, parentRoot common.Hash, // // Note: the main processor and prefetcher share the same reader with a local // cache for mitigating the overhead of state access. - prefetch, process, err := sdb.ReadersWithCacheStats(parentRoot) + prefetch, process, err := warmer.ReadersWithCacheStats(parentRoot) if err != nil { return nil, err } diff --git a/core/blockchain_reader.go b/core/blockchain_reader.go index 3614702d1a..18afa9ce9d 100644 --- a/core/blockchain_reader.go +++ b/core/blockchain_reader.go @@ -416,19 +416,42 @@ func (bc *BlockChain) ContractCodeWithPrefix(hash common.Hash) []byte { // State returns a new mutable state based on the current HEAD block. func (bc *BlockChain) State() (*state.StateDB, error) { - return bc.StateAt(bc.CurrentBlock().Root) + return bc.StateAt(bc.CurrentBlock()) } // StateAt returns a new mutable state based on a particular point in time. -func (bc *BlockChain) StateAt(root common.Hash) (*state.StateDB, error) { - return state.New(root, state.NewDatabase(bc.triedb, bc.codedb).WithSnapshot(bc.snaps)) +func (bc *BlockChain) StateAt(header *types.Header) (*state.StateDB, error) { + if bc.chainConfig.IsUBT(header.Number, header.Time) { + return state.New(header.Root, state.NewUBTDatabase(bc.triedb, bc.codedb)) + } + return state.New(header.Root, state.NewMPTDatabase(bc.triedb, bc.codedb).WithSnapshot(bc.snaps)) +} + +// StateAtForkBoundary returns a new mutable state based on the parent state +// and the given header, handling the transition across the UBT fork. +func (bc *BlockChain) StateAtForkBoundary(parent *types.Header, header *types.Header) (*state.StateDB, error) { + // The parent is already in the UBT fork. + if bc.chainConfig.IsUBT(parent.Number, parent.Time) { + return state.New(parent.Root, state.NewUBTDatabase(bc.triedb, bc.codedb)) + } + // The current block is the first block in the UBT fork + // (i.e., the parent is the last MPT block). + if bc.chainConfig.IsUBT(header.Number, header.Time) { + // TODO(gballet): register chain context if needed + return state.New(parent.Root, state.NewUBTDatabase(bc.triedb, bc.codedb)) + } + // Both the parent and current block are in the MPT fork. + return state.New(parent.Root, state.NewMPTDatabase(bc.triedb, bc.codedb).WithSnapshot(bc.snaps)) } -// HistoricState returns a historic state specified by the given root. +// HistoricState returns a historic state specified by the given header. // Live states are not available and won't be served, please use `State` // or `StateAt` instead. -func (bc *BlockChain) HistoricState(root common.Hash) (*state.StateDB, error) { - return state.New(root, state.NewHistoricDatabase(bc.triedb, bc.codedb)) +func (bc *BlockChain) HistoricState(header *types.Header) (*state.StateDB, error) { + if bc.chainConfig.IsUBT(header.Number, header.Time) { + return nil, errors.New("historical state over ubt is not yet supported") + } + return state.New(header.Root, state.NewHistoricDatabase(bc.triedb, bc.codedb)) } // Config retrieves the chain's fork configuration. diff --git a/core/blockchain_test.go b/core/blockchain_test.go index d3ca21b2b3..1a2ee45291 100644 --- a/core/blockchain_test.go +++ b/core/blockchain_test.go @@ -3890,7 +3890,7 @@ func TestTransientStorageReset(t *testing.T) { t.Fatalf("failed to insert into chain: %v", err) } // Check the storage - state, err := chain.StateAt(chain.CurrentHeader().Root) + state, err := chain.StateAt(chain.CurrentHeader()) if err != nil { t.Fatalf("Failed to load state %v", err) } diff --git a/core/chain_makers.go b/core/chain_makers.go index 8f6eed1697..3bc7f6528b 100644 --- a/core/chain_makers.go +++ b/core/chain_makers.go @@ -126,7 +126,7 @@ func (b *BlockGen) addTx(bc *BlockChain, vmConfig vm.Config, tx *types.Transacti // Merge the tx-local access event into the "block-local" one, in order to collect // all values, so that the witness can be built. - if b.statedb.Database().TrieDB().IsVerkle() { + if b.statedb.Database().Type().Is(state.TypeUBT) { b.statedb.AccessEvents().Merge(evm.AccessEvents) } b.txs = append(b.txs, tx) @@ -392,7 +392,7 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse misc.ApplyDAOHardFork(statedb) } - if config.IsPrague(b.header.Number, b.header.Time) || config.IsVerkle(b.header.Number, b.header.Time) { + if config.IsPrague(b.header.Number, b.header.Time) || config.IsUBT(b.header.Number, b.header.Time) { // EIP-2935 blockContext := NewEVMBlockContext(b.header, cm, &b.header.Coinbase) blockContext.Random = &common.Hash{} // enable post-merge instruction set @@ -430,8 +430,8 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse // Forcibly use hash-based state scheme for retaining all nodes in disk. var triedbConfig *triedb.Config = triedb.HashDefaults - if config.IsVerkle(config.ChainID, 0) { - triedbConfig = triedb.VerkleDefaults + if config.IsUBT(config.ChainID, 0) { + triedbConfig = triedb.UBTDefaults } triedb := triedb.NewDatabase(db, triedbConfig) defer triedb.Close() @@ -479,8 +479,8 @@ func GenerateChain(config *params.ChainConfig, parent *types.Block, engine conse func GenerateChainWithGenesis(genesis *Genesis, engine consensus.Engine, n int, gen func(int, *BlockGen)) (ethdb.Database, []*types.Block, []types.Receipts) { db := rawdb.NewMemoryDatabase() var triedbConfig *triedb.Config = triedb.HashDefaults - if genesis.Config != nil && genesis.Config.IsVerkle(genesis.Config.ChainID, 0) { - triedbConfig = triedb.VerkleDefaults + if genesis.Config != nil && genesis.Config.IsUBT(genesis.Config.ChainID, 0) { + triedbConfig = triedb.UBTDefaults } genesisTriedb := triedb.NewDatabase(db, triedbConfig) block, err := genesis.Commit(db, genesisTriedb, nil) diff --git a/core/genesis.go b/core/genesis.go index 6edc6e6779..d77ea10d8c 100644 --- a/core/genesis.go +++ b/core/genesis.go @@ -129,22 +129,22 @@ func ReadGenesis(db ethdb.Database) (*Genesis, error) { } // hashAlloc computes the state root according to the genesis specification. -func hashAlloc(ga *types.GenesisAlloc, isVerkle bool) (common.Hash, error) { +func hashAlloc(ga *types.GenesisAlloc, isUBT bool) (common.Hash, error) { // If a genesis-time verkle trie is requested, create a trie config // with the verkle trie enabled so that the tree can be initialized // as such. var config *triedb.Config - if isVerkle { + if isUBT { config = &triedb.Config{ - PathDB: pathdb.Defaults, - IsVerkle: true, + PathDB: pathdb.Defaults, + IsUBT: true, } } // Create an ephemeral in-memory database for computing hash, // all the derived states will be discarded to not pollute disk. emptyRoot := types.EmptyRootHash - if isVerkle { - emptyRoot = types.EmptyVerkleHash + if isUBT { + emptyRoot = types.EmptyBinaryHash } db := rawdb.NewMemoryDatabase() statedb, err := state.New(emptyRoot, state.NewDatabase(triedb.NewDatabase(db, config), nil)) @@ -168,8 +168,8 @@ func hashAlloc(ga *types.GenesisAlloc, isVerkle bool) (common.Hash, error) { // generated states will be persisted into the given database. func flushAlloc(ga *types.GenesisAlloc, triedb *triedb.Database, tracer *tracing.Hooks) (common.Hash, error) { emptyRoot := types.EmptyRootHash - if triedb.IsVerkle() { - emptyRoot = types.EmptyVerkleHash + if triedb.IsUBT() { + emptyRoot = types.EmptyBinaryHash } statedb, err := state.New(emptyRoot, state.NewDatabase(triedb, nil)) if err != nil { @@ -276,10 +276,10 @@ func (e *GenesisMismatchError) Error() string { // ChainOverrides contains the changes to chain config. type ChainOverrides struct { - OverrideOsaka *uint64 - OverrideBPO1 *uint64 - OverrideBPO2 *uint64 - OverrideVerkle *uint64 + OverrideOsaka *uint64 + OverrideBPO1 *uint64 + OverrideBPO2 *uint64 + OverrideUBT *uint64 } // apply applies the chain overrides on the supplied chain config. @@ -296,8 +296,8 @@ func (o *ChainOverrides) apply(cfg *params.ChainConfig) error { if o.OverrideBPO2 != nil { cfg.BPO2Time = o.OverrideBPO2 } - if o.OverrideVerkle != nil { - cfg.VerkleTime = o.OverrideVerkle + if o.OverrideUBT != nil { + cfg.UBTTime = o.OverrideUBT } return cfg.CheckConfigForkOrder() } @@ -469,15 +469,15 @@ func (g *Genesis) chainConfigOrDefault(ghash common.Hash, stored *params.ChainCo } } -// IsVerkle indicates whether the state is already stored in a verkle +// IsUBT indicates whether the state is already stored in a verkle // tree at genesis time. -func (g *Genesis) IsVerkle() bool { - return g.Config.IsVerkleGenesis() +func (g *Genesis) IsUBT() bool { + return g.Config.IsUBTGenesis() } // ToBlock returns the genesis block according to genesis specification. func (g *Genesis) ToBlock() *types.Block { - root, err := hashAlloc(&g.Alloc, g.IsVerkle()) + root, err := hashAlloc(&g.Alloc, g.IsUBT()) if err != nil { panic(err) } @@ -609,24 +609,24 @@ func (g *Genesis) MustCommit(db ethdb.Database, triedb *triedb.Database) *types. return block } -// EnableVerkleAtGenesis indicates whether the verkle fork should be activated +// EnableUBTAtGenesis indicates whether the verkle fork should be activated // at genesis. This is a temporary solution only for verkle devnet testing, where // verkle fork is activated at genesis, and the configured activation date has // already passed. // // In production networks (mainnet and public testnets), verkle activation always // occurs after the genesis block, making this function irrelevant in those cases. -func EnableVerkleAtGenesis(db ethdb.Database, genesis *Genesis) (bool, error) { +func EnableUBTAtGenesis(db ethdb.Database, genesis *Genesis) (bool, error) { if genesis != nil { if genesis.Config == nil { return false, errGenesisNoConfig } - return genesis.Config.EnableVerkleAtGenesis, nil + return genesis.Config.EnableUBTAtGenesis, nil } if ghash := rawdb.ReadCanonicalHash(db, 0); ghash != (common.Hash{}) { chainCfg := rawdb.ReadChainConfig(db, ghash) if chainCfg != nil { - return chainCfg.EnableVerkleAtGenesis, nil + return chainCfg.EnableUBTAtGenesis, nil } } return false, nil diff --git a/core/genesis_test.go b/core/genesis_test.go index 2ff64e8d21..e15ad00222 100644 --- a/core/genesis_test.go +++ b/core/genesis_test.go @@ -285,16 +285,16 @@ func TestVerkleGenesisCommit(t *testing.T) { CancunTime: &verkleTime, PragueTime: &verkleTime, OsakaTime: &verkleTime, - VerkleTime: &verkleTime, + UBTTime: &verkleTime, TerminalTotalDifficulty: big.NewInt(0), - EnableVerkleAtGenesis: true, + EnableUBTAtGenesis: true, Ethash: nil, Clique: nil, BlobScheduleConfig: ¶ms.BlobScheduleConfig{ Cancun: params.DefaultCancunBlobConfig, Prague: params.DefaultPragueBlobConfig, Osaka: params.DefaultOsakaBlobConfig, - Verkle: params.DefaultPragueBlobConfig, + UBT: params.DefaultPragueBlobConfig, }, } @@ -320,8 +320,8 @@ func TestVerkleGenesisCommit(t *testing.T) { config.NoAsyncFlush = true triedb := triedb.NewDatabase(db, &triedb.Config{ - IsVerkle: true, - PathDB: &config, + IsUBT: true, + PathDB: &config, }) block := genesis.MustCommit(db, triedb) if !bytes.Equal(block.Root().Bytes(), expected) { @@ -329,7 +329,7 @@ func TestVerkleGenesisCommit(t *testing.T) { } // Test that the trie is verkle - if !triedb.IsVerkle() { + if !triedb.IsUBT() { t.Fatalf("expected trie to be verkle") } vdb := rawdb.NewTable(db, string(rawdb.VerklePrefix)) diff --git a/core/overlay/state_transition.go b/core/overlay/state_transition.go index a52d9139c9..afd2bab017 100644 --- a/core/overlay/state_transition.go +++ b/core/overlay/state_transition.go @@ -71,7 +71,7 @@ func (ts *TransitionState) Copy() *TransitionState { // LoadTransitionState retrieves the Verkle transition state associated with // the given state root hash from the database. -func LoadTransitionState(db ethdb.KeyValueReader, root common.Hash, isVerkle bool) *TransitionState { +func LoadTransitionState(db ethdb.KeyValueReader, root common.Hash, isUBT bool) *TransitionState { var ts *TransitionState data, _ := rawdb.ReadVerkleTransitionState(db, root) @@ -97,10 +97,10 @@ func LoadTransitionState(db ethdb.KeyValueReader, root common.Hash, isVerkle boo // Initialize the first transition state, with the "ended" // field set to true if the database was created // as a verkle database. - log.Debug("no transition state found, starting fresh", "verkle", isVerkle) + log.Debug("no transition state found, starting fresh", "verkle", isUBT) // Start with a fresh state - ts = &TransitionState{Ended: isVerkle} + ts = &TransitionState{Ended: isUBT} } return ts } diff --git a/core/state/database.go b/core/state/database.go index c603e3ad7a..6de58af63b 100644 --- a/core/state/database.go +++ b/core/state/database.go @@ -20,22 +20,36 @@ import ( "fmt" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/overlay" "github.com/ethereum/go-ethereum/core/rawdb" - "github.com/ethereum/go-ethereum/core/state/snapshot" "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" - "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/trie" - "github.com/ethereum/go-ethereum/trie/bintrie" "github.com/ethereum/go-ethereum/trie/transitiontrie" "github.com/ethereum/go-ethereum/trie/trienode" "github.com/ethereum/go-ethereum/triedb" ) +// DatabaseType represents the type of trie backing the state database. +type DatabaseType int + +const ( + // TypeMPT indicates a Merkle Patricia Trie (MPT) backed database. + TypeMPT DatabaseType = iota + + // TypeUBT indicates a Unified Binary Trie (UBT) backed database. + TypeUBT +) + +// Is returns the flag indicating the database type equals to the given one. +func (typ DatabaseType) Is(t DatabaseType) bool { + return typ == t +} + // Database wraps access to tries and contract code. type Database interface { + // Type returns the trie type backing this database (MPT or UBT). + Type() DatabaseType + // Reader returns a state reader associated with the specified state root. Reader(root common.Hash) (Reader, error) @@ -139,184 +153,27 @@ type Trie interface { // with the node that proves the absence of the key. Prove(key []byte, proofDb ethdb.KeyValueWriter) error - // IsVerkle returns true if the trie is verkle-tree based - IsVerkle() bool -} - -// CachingDB is an implementation of Database interface. It leverages both trie and -// state snapshot to provide functionalities for state access. It's meant to be a -// long-live object and has a few caches inside for sharing between blocks. -type CachingDB struct { - triedb *triedb.Database - codedb *CodeDB - snap *snapshot.Tree + // IsUBT returns true if the trie is unified binary trie based. + IsUBT() bool } // NewDatabase creates a state database with the provided data sources. -func NewDatabase(triedb *triedb.Database, codedb *CodeDB) *CachingDB { - if codedb == nil { - codedb = NewCodeDB(triedb.Disk()) - } - return &CachingDB{ - triedb: triedb, - codedb: codedb, +// +// Deprecated, please use NewMPTDatabase or NewUBTDatabase directly. +func NewDatabase(tdb *triedb.Database, codedb *CodeDB) Database { + if tdb.IsUBT() { + return NewUBTDatabase(tdb, codedb) } + return NewMPTDatabase(tdb, codedb) } // NewDatabaseForTesting is similar to NewDatabase, but it initializes the caching // db by using an ephemeral memory db with default config for testing. -func NewDatabaseForTesting() *CachingDB { +func NewDatabaseForTesting() Database { db := rawdb.NewMemoryDatabase() return NewDatabase(triedb.NewDatabase(db, nil), NewCodeDB(db)) } -// WithSnapshot configures the provided contract code cache. Note that this -// registration must be performed before the cachingDB is used. -func (db *CachingDB) WithSnapshot(snapshot *snapshot.Tree) *CachingDB { - db.snap = snapshot - return db -} - -// StateReader returns a state reader associated with the specified state root. -func (db *CachingDB) StateReader(stateRoot common.Hash) (StateReader, error) { - var readers []StateReader - - // Configure the state reader using the standalone snapshot in hash mode. - // This reader offers improved performance but is optional and only - // partially useful if the snapshot is not fully generated. - if db.TrieDB().Scheme() == rawdb.HashScheme && db.snap != nil { - snap := db.snap.Snapshot(stateRoot) - if snap != nil { - readers = append(readers, newFlatReader(snap)) - } - } - // Configure the state reader using the path database in path mode. - // This reader offers improved performance but is optional and only - // partially useful if the snapshot data in path database is not - // fully generated. - if db.TrieDB().Scheme() == rawdb.PathScheme { - reader, err := db.triedb.StateReader(stateRoot) - if err == nil { - readers = append(readers, newFlatReader(reader)) - } - } - // Configure the trie reader, which is expected to be available as the - // gatekeeper unless the state is corrupted. - tr, err := newTrieReader(stateRoot, db.triedb) - if err != nil { - return nil, err - } - readers = append(readers, tr) - - return newMultiStateReader(readers...) -} - -// Reader implements Database, returning a reader associated with the specified -// state root. -func (db *CachingDB) Reader(stateRoot common.Hash) (Reader, error) { - sr, err := db.StateReader(stateRoot) - if err != nil { - return nil, err - } - return newReader(db.codedb.Reader(), sr), nil -} - -// ReadersWithCacheStats creates a pair of state readers that share the same -// underlying state reader and internal state cache, while maintaining separate -// statistics respectively. -func (db *CachingDB) ReadersWithCacheStats(stateRoot common.Hash) (Reader, Reader, error) { - r, err := db.StateReader(stateRoot) - if err != nil { - return nil, nil, err - } - sr := newStateReaderWithCache(r) - ra := newReader(db.codedb.Reader(), newStateReaderWithStats(sr)) - rb := newReader(db.codedb.Reader(), newStateReaderWithStats(sr)) - return ra, rb, nil -} - -// OpenTrie opens the main account trie at a specific root hash. -func (db *CachingDB) OpenTrie(root common.Hash) (Trie, error) { - if db.triedb.IsVerkle() { - ts := overlay.LoadTransitionState(db.TrieDB().Disk(), root, db.triedb.IsVerkle()) - if ts.InTransition() { - panic("state tree transition isn't supported yet") - } - if ts.Transitioned() { - // Use BinaryTrie instead of VerkleTrie when IsVerkle is set - // (IsVerkle actually means Binary Trie mode in this codebase) - return bintrie.NewBinaryTrie(root, db.triedb) - } - } - tr, err := trie.NewStateTrie(trie.StateTrieID(root), db.triedb) - if err != nil { - return nil, err - } - return tr, nil -} - -// OpenStorageTrie opens the storage trie of an account. -func (db *CachingDB) OpenStorageTrie(stateRoot common.Hash, address common.Address, root common.Hash, self Trie) (Trie, error) { - if db.triedb.IsVerkle() { - return self, nil - } - tr, err := trie.NewStateTrie(trie.StorageTrieID(stateRoot, crypto.Keccak256Hash(address.Bytes()), root), db.triedb) - if err != nil { - return nil, err - } - return tr, nil -} - -// TrieDB retrieves any intermediate trie-node caching layer. -func (db *CachingDB) TrieDB() *triedb.Database { - return db.triedb -} - -// Snapshot returns the underlying state snapshot. -func (db *CachingDB) Snapshot() *snapshot.Tree { - return db.snap -} - -// Commit flushes all pending writes and finalizes the state transition, -// committing the changes to the underlying storage. It returns an error -// if the commit fails. -func (db *CachingDB) Commit(update *stateUpdate) error { - // Short circuit if nothing to commit - if update.empty() { - return nil - } - // Commit dirty contract code if any exists - if len(update.codes) > 0 { - batch := db.codedb.NewBatchWithSize(len(update.codes)) - for _, code := range update.codes { - batch.Put(code.hash, code.blob) - } - if err := batch.Commit(); err != nil { - return err - } - } - // If snapshotting is enabled, update the snapshot tree with this new version - if db.snap != nil && db.snap.Snapshot(update.originRoot) != nil { - if err := db.snap.Update(update.root, update.originRoot, update.accounts, update.storages); err != nil { - log.Warn("Failed to update snapshot tree", "from", update.originRoot, "to", update.root, "err", err) - } - // Keep 128 diff layers in the memory, persistent layer is 129th. - // - head layer is paired with HEAD state - // - head-1 layer is paired with HEAD-1 state - // - head-127 layer(bottom-most diff layer) is paired with HEAD-127 state - if err := db.snap.Cap(update.root, TriesInMemory); err != nil { - log.Warn("Failed to cap snapshot tree", "root", update.root, "layers", TriesInMemory, "err", err) - } - } - return db.triedb.Update(update.root, update.originRoot, update.blockNumber, update.nodes, update.stateSet()) -} - -// Iteratee returns a state iteratee associated with the specified state root, -// through which the account iterator and storage iterator can be created. -func (db *CachingDB) Iteratee(root common.Hash) (Iteratee, error) { - return newStateIteratee(!db.triedb.IsVerkle(), root, db.triedb, db.snap) -} - // mustCopyTrie returns a deep-copied trie. func mustCopyTrie(t Trie) Trie { switch t := t.(type) { diff --git a/core/state/database_history.go b/core/state/database_history.go index 0dbb8cc546..d1ed2fe194 100644 --- a/core/state/database_history.go +++ b/core/state/database_history.go @@ -223,6 +223,12 @@ type HistoricDB struct { codedb *CodeDB } +// Type returns the trie type of the underlying database. +func (db *HistoricDB) Type() DatabaseType { + // TODO(rjl493456442) support UBT in the future + return TypeMPT +} + // NewHistoricDatabase creates a historic state database. func NewHistoricDatabase(triedb *triedb.Database, codedb *CodeDB) *HistoricDB { return &HistoricDB{ diff --git a/core/state/database_mpt.go b/core/state/database_mpt.go new file mode 100644 index 0000000000..bf9c7c9fff --- /dev/null +++ b/core/state/database_mpt.go @@ -0,0 +1,178 @@ +// Copyright 2026 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package state + +import ( + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/rawdb" + "github.com/ethereum/go-ethereum/core/state/snapshot" + "github.com/ethereum/go-ethereum/crypto" + "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/trie" + "github.com/ethereum/go-ethereum/triedb" +) + +// MPTDatabase is an implementation of Database interface for Merkle Patricia Tries. +// It leverages both trie and state snapshot to provide functionalities for state +// access. +type MPTDatabase struct { + triedb *triedb.Database + codedb *CodeDB + snap *snapshot.Tree +} + +// Type returns Merkle, indicating this database is backed by a Merkle Patricia Trie. +func (db *MPTDatabase) Type() DatabaseType { return TypeMPT } + +// NewMPTDatabase creates a state database with the Merkle Patricia Trie manner. +func NewMPTDatabase(tdb *triedb.Database, codedb *CodeDB) *MPTDatabase { + if codedb == nil { + codedb = NewCodeDB(tdb.Disk()) + } + return &MPTDatabase{ + triedb: tdb, + codedb: codedb, + } +} + +// WithSnapshot configures the provided state snapshot. Note that this +// registration must be performed before the MPTDatabase is used. +func (db *MPTDatabase) WithSnapshot(snapshot *snapshot.Tree) Database { + db.snap = snapshot + return db +} + +// StateReader returns a state reader associated with the specified state root. +func (db *MPTDatabase) StateReader(stateRoot common.Hash) (StateReader, error) { + var readers []StateReader + + // Configure the state reader using the standalone snapshot in hash mode. + // This reader offers improved performance but is optional and only + // partially useful if the snapshot is not fully generated. + if db.TrieDB().Scheme() == rawdb.HashScheme && db.snap != nil { + snap := db.snap.Snapshot(stateRoot) + if snap != nil { + readers = append(readers, newFlatReader(snap)) + } + } + // Configure the state reader using the path database in path mode. + // This reader offers improved performance but is optional and only + // partially useful if the snapshot data in path database is not + // fully generated. + if db.TrieDB().Scheme() == rawdb.PathScheme { + reader, err := db.triedb.StateReader(stateRoot) + if err == nil { + readers = append(readers, newFlatReader(reader)) + } + } + // Configure the trie reader, which is expected to be available as the + // gatekeeper unless the state is corrupted. + tr, err := newTrieReader(stateRoot, db.triedb) + if err != nil { + return nil, err + } + readers = append(readers, tr) + + return newMultiStateReader(readers...) +} + +// Reader implements Database, returning a reader associated with the specified +// state root. +func (db *MPTDatabase) Reader(stateRoot common.Hash) (Reader, error) { + sr, err := db.StateReader(stateRoot) + if err != nil { + return nil, err + } + return newReader(db.codedb.Reader(), sr), nil +} + +// ReadersWithCacheStats creates a pair of state readers that share the same +// underlying state reader and internal state cache, while maintaining separate +// statistics respectively. +func (db *MPTDatabase) ReadersWithCacheStats(stateRoot common.Hash) (Reader, Reader, error) { + r, err := db.StateReader(stateRoot) + if err != nil { + return nil, nil, err + } + sr := newStateReaderWithCache(r) + ra := newReader(db.codedb.Reader(), newStateReaderWithStats(sr)) + rb := newReader(db.codedb.Reader(), newStateReaderWithStats(sr)) + return ra, rb, nil +} + +// OpenTrie opens the main account trie at a specific root hash. +func (db *MPTDatabase) OpenTrie(root common.Hash) (Trie, error) { + tr, err := trie.NewStateTrie(trie.StateTrieID(root), db.triedb) + if err != nil { + return nil, err + } + return tr, nil +} + +// OpenStorageTrie opens the storage trie of an account. +func (db *MPTDatabase) OpenStorageTrie(stateRoot common.Hash, address common.Address, root common.Hash, self Trie) (Trie, error) { + tr, err := trie.NewStateTrie(trie.StorageTrieID(stateRoot, crypto.Keccak256Hash(address.Bytes()), root), db.triedb) + if err != nil { + return nil, err + } + return tr, nil +} + +// TrieDB retrieves any intermediate trie-node caching layer. +func (db *MPTDatabase) TrieDB() *triedb.Database { + return db.triedb +} + +// Commit flushes all pending writes and finalizes the state transition, +// committing the changes to the underlying storage. It returns an error +// if the commit fails. +func (db *MPTDatabase) Commit(update *stateUpdate) error { + // Short circuit if nothing to commit + if update.empty() { + return nil + } + // Commit dirty contract code if any exists + if len(update.codes) > 0 { + batch := db.codedb.NewBatchWithSize(len(update.codes)) + for _, code := range update.codes { + batch.Put(code.hash, code.blob) + } + if err := batch.Commit(); err != nil { + return err + } + } + // If snapshotting is enabled, update the snapshot tree with this new version + if db.snap != nil && db.snap.Snapshot(update.originRoot) != nil { + if err := db.snap.Update(update.root, update.originRoot, update.accounts, update.storages); err != nil { + log.Warn("Failed to update snapshot tree", "from", update.originRoot, "to", update.root, "err", err) + } + // Keep 128 diff layers in the memory, persistent layer is 129th. + // - head layer is paired with HEAD state + // - head-1 layer is paired with HEAD-1 state + // - head-127 layer(bottom-most diff layer) is paired with HEAD-127 state + if err := db.snap.Cap(update.root, TriesInMemory); err != nil { + log.Warn("Failed to cap snapshot tree", "root", update.root, "layers", TriesInMemory, "err", err) + } + } + return db.triedb.Update(update.root, update.originRoot, update.blockNumber, update.nodes, update.stateSet()) +} + +// Iteratee returns a state iteratee associated with the specified state root, +// through which the account iterator and storage iterator can be created. +func (db *MPTDatabase) Iteratee(root common.Hash) (Iteratee, error) { + return newStateIteratee(true, root, db.triedb, db.snap) +} diff --git a/core/state/database_ubt.go b/core/state/database_ubt.go new file mode 100644 index 0000000000..39aa64508b --- /dev/null +++ b/core/state/database_ubt.go @@ -0,0 +1,138 @@ +// Copyright 2026 The go-ethereum Authors +// This file is part of the go-ethereum library. +// +// The go-ethereum library is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The go-ethereum library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the go-ethereum library. If not, see . + +package state + +import ( + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/rawdb" + "github.com/ethereum/go-ethereum/trie/bintrie" + "github.com/ethereum/go-ethereum/triedb" +) + +// UBTDatabase is an implementation of Database interface for Unified Binary Trie. +// It provides the same functionality as MPTDatabase but uses unified binary +// trie for state hashing instead of Merkle Patricia Tries. +type UBTDatabase struct { + triedb *triedb.Database + codedb *CodeDB +} + +// Type returns Binary, indicating this database is backed by a Universal Binary Trie. +func (db *UBTDatabase) Type() DatabaseType { return TypeUBT } + +// NewUBTDatabase creates a state database with the Unified binary trie manner. +func NewUBTDatabase(triedb *triedb.Database, codedb *CodeDB) *UBTDatabase { + if codedb == nil { + codedb = NewCodeDB(triedb.Disk()) + } + return &UBTDatabase{ + triedb: triedb, + codedb: codedb, + } +} + +// StateReader returns a state reader associated with the specified state root. +func (db *UBTDatabase) StateReader(stateRoot common.Hash) (StateReader, error) { + var readers []StateReader + + // Configure the state reader using the path database in path mode. + // This reader offers improved performance but is optional and only + // partially useful if the snapshot data in path database is not + // fully generated. + if db.TrieDB().Scheme() == rawdb.PathScheme { + reader, err := db.triedb.StateReader(stateRoot) + if err == nil { + readers = append(readers, newFlatReader(reader)) + } + } + // Configure the trie reader, which is expected to be available as the + // gatekeeper unless the state is corrupted. + tr, err := newTrieReader(stateRoot, db.triedb) + if err != nil { + return nil, err + } + readers = append(readers, tr) + + return newMultiStateReader(readers...) +} + +// Reader implements Database, returning a reader associated with the specified +// state root. +func (db *UBTDatabase) Reader(stateRoot common.Hash) (Reader, error) { + sr, err := db.StateReader(stateRoot) + if err != nil { + return nil, err + } + return newReader(db.codedb.Reader(), sr), nil +} + +// ReadersWithCacheStats creates a pair of state readers that share the same +// underlying state reader and internal state cache, while maintaining separate +// statistics respectively. +func (db *UBTDatabase) ReadersWithCacheStats(stateRoot common.Hash) (Reader, Reader, error) { + r, err := db.StateReader(stateRoot) + if err != nil { + return nil, nil, err + } + sr := newStateReaderWithCache(r) + ra := newReader(db.codedb.Reader(), newStateReaderWithStats(sr)) + rb := newReader(db.codedb.Reader(), newStateReaderWithStats(sr)) + return ra, rb, nil +} + +// OpenTrie opens the main account trie at a specific root hash. +func (db *UBTDatabase) OpenTrie(root common.Hash) (Trie, error) { + return bintrie.NewBinaryTrie(root, db.triedb) +} + +// OpenStorageTrie opens the storage trie of an account. In binary trie mode, +// all state objects share one unified trie, so the main trie is returned. +func (db *UBTDatabase) OpenStorageTrie(stateRoot common.Hash, address common.Address, root common.Hash, self Trie) (Trie, error) { + return self, nil +} + +// TrieDB retrieves any intermediate trie-node caching layer. +func (db *UBTDatabase) TrieDB() *triedb.Database { + return db.triedb +} + +// Commit flushes all pending writes and finalizes the state transition, +// committing the changes to the underlying storage. It returns an error +// if the commit fails. +func (db *UBTDatabase) Commit(update *stateUpdate) error { + // Short circuit if nothing to commit + if update.empty() { + return nil + } + // Commit dirty contract code if any exists + if len(update.codes) > 0 { + batch := db.codedb.NewBatchWithSize(len(update.codes)) + for _, code := range update.codes { + batch.Put(code.hash, code.blob) + } + if err := batch.Commit(); err != nil { + return err + } + } + return db.triedb.Update(update.root, update.originRoot, update.blockNumber, update.nodes, update.stateSet()) +} + +// Iteratee returns a state iteratee associated with the specified state root, +// through which the account iterator and storage iterator can be created. +func (db *UBTDatabase) Iteratee(root common.Hash) (Iteratee, error) { + return newStateIteratee(false, root, db.triedb, nil) +} diff --git a/core/state/reader.go b/core/state/reader.go index fe0ec71f2d..b837c6a0a1 100644 --- a/core/state/reader.go +++ b/core/state/reader.go @@ -172,10 +172,10 @@ func newTrieReader(root common.Hash, db *triedb.Database) (*trieReader, error) { tr Trie err error ) - if !db.IsVerkle() { + if !db.IsUBT() { tr, err = trie.NewStateTrie(trie.StateTrieID(root), db) } else { - // When IsVerkle() is true, create a BinaryTrie wrapped in TransitionTrie + // When IsUBT() is true, create a BinaryTrie wrapped in TransitionTrie binTrie, binErr := bintrie.NewBinaryTrie(root, db) if binErr != nil { return nil, binErr @@ -259,7 +259,7 @@ func (r *trieReader) Storage(addr common.Address, key common.Hash) (common.Hash, found bool value common.Hash ) - if r.db.IsVerkle() { + if r.db.IsUBT() { tr = r.mainTrie } else { tr, found = r.subTries[addr] diff --git a/core/state/state_object.go b/core/state/state_object.go index a4a9f5121b..a812359368 100644 --- a/core/state/state_object.go +++ b/core/state/state_object.go @@ -154,7 +154,7 @@ func (s *stateObject) getTrie() (Trie, error) { func (s *stateObject) getPrefetchedTrie() Trie { // If there's nothing to meaningfully return, let the user figure it out by // pulling the trie from disk. - if (s.data.Root == types.EmptyRootHash && !s.db.db.TrieDB().IsVerkle()) || s.db.prefetcher == nil { + if (s.data.Root == types.EmptyRootHash && s.db.db.Type().Is(TypeMPT)) || s.db.prefetcher == nil { return nil } // Attempt to retrieve the trie from the prefetcher @@ -478,7 +478,7 @@ func (s *stateObject) commit() (*accountUpdate, *trienode.NodeSet, error) { // The main account trie commit in stateDB.commit() already calls // CollectNodes on this trie, so calling Commit here again would // redundantly traverse and serialize the entire tree per dirty account. - if s.db.GetTrie().IsVerkle() { + if s.db.GetTrie().IsUBT() { s.origin = s.data.Copy() return op, nil, nil } diff --git a/core/state/statedb.go b/core/state/statedb.go index fc2da59a05..956871472d 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -190,7 +190,7 @@ func NewWithReader(root common.Hash, db Database, reader Reader) (*StateDB, erro accessList: newAccessList(), transientStorage: newTransientStorage(), } - if db.TrieDB().IsVerkle() { + if db.Type().Is(TypeUBT) { sdb.accessEvents = NewAccessEvents() } return sdb, nil @@ -861,7 +861,7 @@ func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash { start = time.Now() workers errgroup.Group ) - if s.db.TrieDB().IsVerkle() { + if s.db.Type().Is(TypeUBT) { // Bypass per-account updateTrie() for binary trie. In binary trie mode // there is only one unified trie (OpenStorageTrie returns self), so the // per-account trie setup in updateTrie() (getPrefetchedTrie, getTrie, @@ -925,9 +925,9 @@ func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash { } } // If witness building is enabled, gather all the read-only accesses. - // Skip witness collection in Verkle mode, they will be gathered - // together at the end. - if s.witness != nil && !s.db.TrieDB().IsVerkle() { + // Skip witness collection in Unified-binary-trie mode, they will be + // gathered together at the end. + if s.witness != nil && s.db.Type().Is(TypeMPT) { // Pull in anything that has been accessed before destruction for _, obj := range s.stateObjectsDestruct { // Skip any objects that haven't touched their storage @@ -968,7 +968,7 @@ func (s *StateDB) IntermediateRoot(deleteEmptyObjects bool) common.Hash { // only a single trie is used for state hashing. Replacing a non-nil verkle tree // here could result in losing uncommitted changes from storage. start = time.Now() - if s.prefetcher != nil && !s.db.TrieDB().IsVerkle() { + if s.prefetcher != nil && s.db.Type().Is(TypeMPT) { if trie := s.prefetcher.trie(common.Hash{}, s.originalRoot); trie == nil { log.Error("Failed to retrieve account pre-fetcher trie") } else { @@ -1129,7 +1129,7 @@ func (s *StateDB) handleDestruction(noStorageWiping bool) (map[common.Hash]*acco deletes[addrHash] = op // Short circuit if the origin storage was empty. - if prev.Root == types.EmptyRootHash || s.db.TrieDB().IsVerkle() { + if prev.Root == types.EmptyRootHash || s.db.Type().Is(TypeUBT) { continue } if noStorageWiping { diff --git a/core/state/statedb_fuzz_test.go b/core/state/statedb_fuzz_test.go index 3582185344..a8017e5568 100644 --- a/core/state/statedb_fuzz_test.go +++ b/core/state/statedb_fuzz_test.go @@ -209,7 +209,7 @@ func (test *stateTest) run() bool { if i != 0 { root = roots[len(roots)-1] } - state, err := New(root, NewDatabase(tdb, nil).WithSnapshot(snaps)) + state, err := New(root, NewMPTDatabase(tdb, nil).WithSnapshot(snaps)) if err != nil { panic(err) } diff --git a/core/state/statedb_test.go b/core/state/statedb_test.go index 6936372c50..601970ff20 100644 --- a/core/state/statedb_test.go +++ b/core/state/statedb_test.go @@ -1274,7 +1274,7 @@ func TestDeleteStorage(t *testing.T) { disk = rawdb.NewMemoryDatabase() tdb = triedb.NewDatabase(disk, nil) snaps, _ = snapshot.New(snapshot.Config{CacheSize: 10}, disk, tdb, types.EmptyRootHash) - db = NewDatabase(tdb, nil).WithSnapshot(snaps) + db = NewMPTDatabase(tdb, nil).WithSnapshot(snaps) state, _ = New(types.EmptyRootHash, db) addr = common.HexToAddress("0x1") ) @@ -1288,8 +1288,8 @@ func TestDeleteStorage(t *testing.T) { } root, _ := state.Commit(0, true, false) // Init phase done, create two states, one with snap and one without - fastState, _ := New(root, NewDatabase(tdb, nil).WithSnapshot(snaps)) - slowState, _ := New(root, NewDatabase(tdb, nil)) + fastState, _ := New(root, NewMPTDatabase(tdb, nil).WithSnapshot(snaps)) + slowState, _ := New(root, NewMPTDatabase(tdb, nil)) obj := fastState.getOrNewStateObject(addr) storageRoot := obj.data.Root diff --git a/core/state/trie_prefetcher.go b/core/state/trie_prefetcher.go index a9faddcdff..a0310eb3b3 100644 --- a/core/state/trie_prefetcher.go +++ b/core/state/trie_prefetcher.go @@ -40,7 +40,7 @@ var ( // // Note, the prefetcher's API is not thread safe. type triePrefetcher struct { - verkle bool // Flag whether the prefetcher is in verkle mode + isUBT bool // Flag whether the prefetcher is in UBT mode db Database // Database to fetch trie nodes through root common.Hash // Root hash of the account trie for metrics fetchers map[string]*subfetcher // Subfetchers for each trie @@ -67,7 +67,7 @@ type triePrefetcher struct { func newTriePrefetcher(db Database, root common.Hash, namespace string, noreads bool) *triePrefetcher { prefix := triePrefetchMetricsPrefix + namespace return &triePrefetcher{ - verkle: db.TrieDB().IsVerkle(), + isUBT: db.Type().Is(TypeUBT), db: db, root: root, fetchers: make(map[string]*subfetcher), // Active prefetchers use the fetchers map @@ -206,8 +206,8 @@ func (p *triePrefetcher) used(owner common.Hash, root common.Hash, usedAddr []co // trieID returns an unique trie identifier consists the trie owner and root hash. func (p *triePrefetcher) trieID(owner common.Hash, root common.Hash) string { - // The trie in verkle is only identified by state root - if p.verkle { + // The trie in ubt is only identified by state root + if p.isUBT { return p.root.Hex() } // The trie in merkle is either identified by state root (account trie), @@ -340,12 +340,12 @@ func (sf *subfetcher) terminate(async bool) { // openTrie resolves the target trie from database for prefetching. func (sf *subfetcher) openTrie() error { - // Open the verkle tree if the sub-fetcher is in verkle mode. Note, there is - // only a single fetcher for verkle. - if sf.db.TrieDB().IsVerkle() { + // Open the ubt tree if the sub-fetcher is in ubt mode. Note, there is + // only a single fetcher for ubt. + if sf.db.Type().Is(TypeUBT) { tr, err := sf.db.OpenTrie(sf.state) if err != nil { - log.Warn("Trie prefetcher failed opening verkle trie", "root", sf.root, "err", err) + log.Warn("Trie prefetcher failed opening UBT trie", "root", sf.root, "err", err) return err } sf.trie = tr diff --git a/core/state/trie_prefetcher_test.go b/core/state/trie_prefetcher_test.go index dad208d01a..8a03d93a08 100644 --- a/core/state/trie_prefetcher_test.go +++ b/core/state/trie_prefetcher_test.go @@ -68,7 +68,7 @@ func TestUseAfterTerminate(t *testing.T) { func TestVerklePrefetcher(t *testing.T) { disk := rawdb.NewMemoryDatabase() - db := triedb.NewDatabase(disk, triedb.VerkleDefaults) + db := triedb.NewDatabase(disk, triedb.UBTDefaults) sdb := NewDatabase(db, nil) state, err := New(types.EmptyRootHash, sdb) diff --git a/core/state_processor.go b/core/state_processor.go index bbb1341299..0a324379f9 100644 --- a/core/state_processor.go +++ b/core/state_processor.go @@ -90,7 +90,7 @@ func (p *StateProcessor) Process(ctx context.Context, block *types.Block, stated if beaconRoot := block.BeaconRoot(); beaconRoot != nil { ProcessBeaconBlockRoot(*beaconRoot, evm) } - if config.IsPrague(block.Number(), block.Time()) || config.IsVerkle(block.Number(), block.Time()) { + if config.IsPrague(block.Number(), block.Time()) || config.IsUBT(block.Number(), block.Time()) { ProcessParentBlockHash(block.ParentHash(), evm) } @@ -182,7 +182,7 @@ func ApplyTransactionWithEVM(msg *Message, gp *GasPool, statedb *state.StateDB, } // Merge the tx-local access event into the "block-local" one, in order to collect // all values, so that the witness can be built. - if statedb.Database().TrieDB().IsVerkle() { + if statedb.Database().Type().Is(state.TypeUBT) { statedb.AccessEvents().Merge(evm.AccessEvents) } return MakeReceipt(evm, result, statedb, blockNumber, blockHash, blockTime, tx, gp.CumulativeUsed(), root), nil diff --git a/core/txpool/blobpool/blobpool.go b/core/txpool/blobpool/blobpool.go index 7155a67a9b..4030a0c339 100644 --- a/core/txpool/blobpool/blobpool.go +++ b/core/txpool/blobpool/blobpool.go @@ -441,9 +441,9 @@ func (p *BlobPool) Init(gasTip uint64, head *types.Header, reserver txpool.Reser // Initialize the state with head block, or fallback to empty one in // case the head state is not available (might occur when node is not // fully synced). - state, err := p.chain.StateAt(head.Root) + state, err := p.chain.StateAt(head) if err != nil { - state, err = p.chain.StateAt(types.EmptyRootHash) + state, err = p.chain.StateAt(p.chain.Genesis().Header()) } if err != nil { return err @@ -894,7 +894,7 @@ func (p *BlobPool) Reset(oldHead, newHead *types.Header) { // Handle reorg buffer timeouts evicting old gapped transactions p.evictGapped() - statedb, err := p.chain.StateAt(newHead.Root) + statedb, err := p.chain.StateAt(newHead) if err != nil { log.Error("Failed to reset blobpool state", "err", err) return diff --git a/core/txpool/blobpool/blobpool_test.go b/core/txpool/blobpool/blobpool_test.go index ba96bea8ed..7c57755401 100644 --- a/core/txpool/blobpool/blobpool_test.go +++ b/core/txpool/blobpool/blobpool_test.go @@ -45,6 +45,7 @@ import ( "github.com/ethereum/go-ethereum/internal/testrand" "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" + "github.com/ethereum/go-ethereum/trie" "github.com/holiman/billy" "github.com/holiman/uint256" ) @@ -180,10 +181,14 @@ func (bc *testBlockChain) GetBlock(hash common.Hash, number uint64) *types.Block return bc.blocks[number] } -func (bc *testBlockChain) StateAt(common.Hash) (*state.StateDB, error) { +func (bc *testBlockChain) StateAt(header *types.Header) (*state.StateDB, error) { return bc.statedb, nil } +func (bc *testBlockChain) Genesis() *types.Block { + return types.NewBlock(bc.CurrentBlock(), nil, nil, trie.NewStackTrie(nil)) +} + // reserver is a utility struct to sanity check that accounts are // properly reserved by the blobpool (no duplicate reserves or unreserves). type reserver struct { diff --git a/core/txpool/blobpool/interface.go b/core/txpool/blobpool/interface.go index 6f296a54bd..d7beae9b25 100644 --- a/core/txpool/blobpool/interface.go +++ b/core/txpool/blobpool/interface.go @@ -32,6 +32,9 @@ type BlockChain interface { // CurrentBlock returns the current head of the chain. CurrentBlock() *types.Header + // Genesis returns the genesis block of the chain. + Genesis() *types.Block + // CurrentFinalBlock returns the current block below which blobs should not // be maintained anymore for reorg purposes. CurrentFinalBlock() *types.Header @@ -39,6 +42,6 @@ type BlockChain interface { // GetBlock retrieves a specific block, used during pool resets. GetBlock(hash common.Hash, number uint64) *types.Block - // StateAt returns a state database for a given root hash (generally the head). - StateAt(root common.Hash) (*state.StateDB, error) + // StateAt returns a state database for a given chain header (generally the head). + StateAt(header *types.Header) (*state.StateDB, error) } diff --git a/core/txpool/legacypool/legacypool.go b/core/txpool/legacypool/legacypool.go index 93b3cb5be2..78a0161c41 100644 --- a/core/txpool/legacypool/legacypool.go +++ b/core/txpool/legacypool/legacypool.go @@ -129,11 +129,14 @@ type BlockChain interface { // CurrentBlock returns the current head of the chain. CurrentBlock() *types.Header + // Genesis returns the genesis block of the chain. + Genesis() *types.Block + // GetBlock retrieves a specific block, used during pool resets. GetBlock(hash common.Hash, number uint64) *types.Block - // StateAt returns a state database for a given root hash (generally the head). - StateAt(root common.Hash) (*state.StateDB, error) + // StateAt returns a state database for a given chain header (generally the head). + StateAt(header *types.Header) (*state.StateDB, error) } // Config are the configuration parameters of the transaction pool. @@ -317,9 +320,9 @@ func (pool *LegacyPool) Init(gasTip uint64, head *types.Header, reserver txpool. // Initialize the state with head block, or fallback to empty one in // case the head state is not available (might occur when node is not // fully synced). - statedb, err := pool.chain.StateAt(head.Root) + statedb, err := pool.chain.StateAt(head) if err != nil { - statedb, err = pool.chain.StateAt(types.EmptyRootHash) + statedb, err = pool.chain.StateAt(pool.chain.Genesis().Header()) } if err != nil { return err @@ -1379,7 +1382,7 @@ func (pool *LegacyPool) reset(oldHead, newHead *types.Header) { if newHead == nil { newHead = pool.chain.CurrentBlock() // Special case during testing } - statedb, err := pool.chain.StateAt(newHead.Root) + statedb, err := pool.chain.StateAt(newHead) if err != nil { log.Error("Failed to reset txpool state", "err", err) return diff --git a/core/txpool/legacypool/legacypool_test.go b/core/txpool/legacypool/legacypool_test.go index fb994d8208..f8592ba001 100644 --- a/core/txpool/legacypool/legacypool_test.go +++ b/core/txpool/legacypool/legacypool_test.go @@ -91,10 +91,14 @@ func (bc *testBlockChain) GetBlock(hash common.Hash, number uint64) *types.Block return types.NewBlock(bc.CurrentBlock(), nil, nil, trie.NewStackTrie(nil)) } -func (bc *testBlockChain) StateAt(common.Hash) (*state.StateDB, error) { +func (bc *testBlockChain) StateAt(header *types.Header) (*state.StateDB, error) { return bc.statedb, nil } +func (bc *testBlockChain) Genesis() *types.Block { + return types.NewBlock(bc.CurrentBlock(), nil, nil, trie.NewStackTrie(nil)) +} + func (bc *testBlockChain) SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription { return bc.chainHeadFeed.Subscribe(ch) } diff --git a/core/txpool/locals/tx_tracker_test.go b/core/txpool/locals/tx_tracker_test.go index dde8754605..34fb4d0b74 100644 --- a/core/txpool/locals/tx_tracker_test.go +++ b/core/txpool/locals/tx_tracker_test.go @@ -102,7 +102,7 @@ func (env *testEnv) setGasTip(gasTip uint64) { func (env *testEnv) makeTx(nonce uint64, gasPrice *big.Int) *types.Transaction { if nonce == 0 { head := env.chain.CurrentHeader() - state, _ := env.chain.StateAt(head.Root) + state, _ := env.chain.StateAt(head) nonce = state.GetNonce(address) } if gasPrice == nil { @@ -114,7 +114,7 @@ func (env *testEnv) makeTx(nonce uint64, gasPrice *big.Int) *types.Transaction { func (env *testEnv) makeTxs(n int) []*types.Transaction { head := env.chain.CurrentHeader() - state, _ := env.chain.StateAt(head.Root) + state, _ := env.chain.StateAt(head) nonce := state.GetNonce(address) var txs []*types.Transaction diff --git a/core/txpool/txpool.go b/core/txpool/txpool.go index 25647e0cce..9c78748422 100644 --- a/core/txpool/txpool.go +++ b/core/txpool/txpool.go @@ -50,11 +50,14 @@ type BlockChain interface { // CurrentBlock returns the current head of the chain. CurrentBlock() *types.Header + // Genesis returns the genesis block of the chain. + Genesis() *types.Block + // SubscribeChainHeadEvent subscribes to new blocks being added to the chain. SubscribeChainHeadEvent(ch chan<- core.ChainHeadEvent) event.Subscription - // StateAt returns a state database for a given root hash (generally the head). - StateAt(root common.Hash) (*state.StateDB, error) + // StateAt returns a state database for a given chain header (generally the head). + StateAt(header *types.Header) (*state.StateDB, error) } // TxPool is an aggregator for various transaction specific pools, collectively @@ -87,9 +90,9 @@ func New(gasTip uint64, chain BlockChain, subpools []SubPool) (*TxPool, error) { // Initialize the state with head block, or fallback to empty one in // case the head state is not available (might occur when node is not // fully synced). - statedb, err := chain.StateAt(head.Root) + statedb, err := chain.StateAt(head) if err != nil { - statedb, err = chain.StateAt(types.EmptyRootHash) + statedb, err = chain.StateAt(chain.Genesis().Header()) } if err != nil { return nil, err @@ -185,7 +188,7 @@ func (p *TxPool) loop(head *types.Header) { case resetBusy <- struct{}{}: // Updates the statedb with the new chain head. The head state may be // unavailable if the initial state sync has not yet completed. - if statedb, err := p.chain.StateAt(newHead.Root); err != nil { + if statedb, err := p.chain.StateAt(newHead); err != nil { log.Error("Failed to reset txpool state", "err", err) } else { p.stateLock.Lock() diff --git a/core/types/hashes.go b/core/types/hashes.go index 22f1f946dc..db8912a66f 100644 --- a/core/types/hashes.go +++ b/core/types/hashes.go @@ -43,9 +43,6 @@ var ( // EmptyRequestsHash is the known hash of an empty request set, sha256(""). EmptyRequestsHash = common.HexToHash("e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855") - // EmptyVerkleHash is the known hash of an empty verkle trie. - EmptyVerkleHash = common.Hash{} - // EmptyBinaryHash is the known hash of an empty binary trie. EmptyBinaryHash = common.Hash{} ) diff --git a/core/vm/contracts.go b/core/vm/contracts.go index 010f477337..b4cddf0bca 100644 --- a/core/vm/contracts.go +++ b/core/vm/contracts.go @@ -213,7 +213,7 @@ func init() { func activePrecompiledContracts(rules params.Rules) PrecompiledContracts { switch { - case rules.IsVerkle: + case rules.IsUBT: return PrecompiledContractsVerkle case rules.IsOsaka: return PrecompiledContractsOsaka diff --git a/core/vm/evm.go b/core/vm/evm.go index cfc837fb62..8f68478842 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -149,7 +149,7 @@ func NewEVM(blockCtx BlockContext, statedb StateDB, chainConfig *params.ChainCon evm.table = &amsterdamInstructionSet case evm.chainRules.IsOsaka: evm.table = &osakaInstructionSet - case evm.chainRules.IsVerkle: + case evm.chainRules.IsUBT: // TODO replace with proper instruction set when fork is specified evm.table = &verkleInstructionSet case evm.chainRules.IsPrague: diff --git a/core/vm/jump_table_export.go b/core/vm/jump_table_export.go index fdf814d64c..a4a99ea498 100644 --- a/core/vm/jump_table_export.go +++ b/core/vm/jump_table_export.go @@ -26,7 +26,7 @@ import ( // the rules. func LookupInstructionSet(rules params.Rules) (JumpTable, error) { switch { - case rules.IsVerkle: + case rules.IsUBT: return newCancunInstructionSet(), errors.New("verkle-fork not defined yet") case rules.IsAmsterdam: return newAmsterdamInstructionSet(), nil diff --git a/eth/api_backend.go b/eth/api_backend.go index a4e976b1b8..33fe4fe5d9 100644 --- a/eth/api_backend.go +++ b/eth/api_backend.go @@ -236,9 +236,9 @@ func (b *EthAPIBackend) StateAndHeaderByNumber(ctx context.Context, number rpc.B if header == nil { return nil, nil, errors.New("header not found") } - stateDb, err := b.eth.BlockChain().StateAt(header.Root) + stateDb, err := b.eth.BlockChain().StateAt(header) if err != nil { - stateDb, err = b.eth.BlockChain().HistoricState(header.Root) + stateDb, err = b.eth.BlockChain().HistoricState(header) if err != nil { return nil, nil, err } @@ -261,9 +261,9 @@ func (b *EthAPIBackend) StateAndHeaderByNumberOrHash(ctx context.Context, blockN if blockNrOrHash.RequireCanonical && b.eth.blockchain.GetCanonicalHash(header.Number.Uint64()) != hash { return nil, nil, errors.New("hash is not currently canonical") } - stateDb, err := b.eth.BlockChain().StateAt(header.Root) + stateDb, err := b.eth.BlockChain().StateAt(header) if err != nil { - stateDb, err = b.eth.BlockChain().HistoricState(header.Root) + stateDb, err = b.eth.BlockChain().HistoricState(header) if err != nil { return nil, nil, err } diff --git a/eth/api_debug.go b/eth/api_debug.go index 5dd535e672..260e24c2ee 100644 --- a/eth/api_debug.go +++ b/eth/api_debug.go @@ -82,7 +82,7 @@ func (api *DebugAPI) DumpBlock(blockNr rpc.BlockNumber) (state.Dump, error) { if header == nil { return state.Dump{}, fmt.Errorf("block #%d not found", blockNr) } - stateDb, err := api.eth.BlockChain().StateAt(header.Root) + stateDb, err := api.eth.BlockChain().StateAt(header) if err != nil { return state.Dump{}, err } @@ -167,7 +167,7 @@ func (api *DebugAPI) AccountRange(blockNrOrHash rpc.BlockNumberOrHash, start hex if header == nil { return state.Dump{}, fmt.Errorf("block #%d not found", number) } - stateDb, err = api.eth.BlockChain().StateAt(header.Root) + stateDb, err = api.eth.BlockChain().StateAt(header) if err != nil { return state.Dump{}, err } @@ -177,7 +177,7 @@ func (api *DebugAPI) AccountRange(blockNrOrHash rpc.BlockNumberOrHash, start hex if block == nil { return state.Dump{}, fmt.Errorf("block %s not found", hash.Hex()) } - stateDb, err = api.eth.BlockChain().StateAt(block.Root()) + stateDb, err = api.eth.BlockChain().StateAt(block.Header()) if err != nil { return state.Dump{}, err } diff --git a/eth/backend.go b/eth/backend.go index e9bea59734..08a3c70c9d 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -277,8 +277,8 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) { if config.OverrideBPO2 != nil { overrides.OverrideBPO2 = config.OverrideBPO2 } - if config.OverrideVerkle != nil { - overrides.OverrideVerkle = config.OverrideVerkle + if config.OverrideUBT != nil { + overrides.OverrideUBT = config.OverrideUBT } options.Overrides = &overrides diff --git a/eth/catalyst/api_test.go b/eth/catalyst/api_test.go index d126c362fe..1f38c4dd8a 100644 --- a/eth/catalyst/api_test.go +++ b/eth/catalyst/api_test.go @@ -299,7 +299,7 @@ func TestEth2NewBlock(t *testing.T) { ethservice.BlockChain().SubscribeRemovedLogsEvent(rmLogsCh) for i := 0; i < 10; i++ { - statedb, _ := ethservice.BlockChain().StateAt(parent.Root()) + statedb, _ := ethservice.BlockChain().StateAt(parent.Header()) nonce := statedb.GetNonce(testAddr) tx, _ := types.SignTx(types.NewContractCreation(nonce, new(big.Int), 1000000, big.NewInt(2*params.InitialBaseFee), logCode), types.LatestSigner(ethservice.BlockChain().Config()), testKey) ethservice.TxPool().Add([]*types.Transaction{tx}, true) @@ -478,7 +478,7 @@ func TestFullAPI(t *testing.T) { ) callback := func(parent *types.Header) { - statedb, _ := ethservice.BlockChain().StateAt(parent.Root) + statedb, _ := ethservice.BlockChain().StateAt(parent) nonce := statedb.GetNonce(testAddr) tx, _ := types.SignTx(types.NewContractCreation(nonce, new(big.Int), 1000000, big.NewInt(2*params.InitialBaseFee), logCode), types.LatestSigner(ethservice.BlockChain().Config()), testKey) ethservice.TxPool().Add([]*types.Transaction{tx}, false) @@ -604,7 +604,7 @@ func TestNewPayloadOnInvalidChain(t *testing.T) { logCode = common.Hex2Bytes("60606040525b7f24ec1d3ff24c2f6ff210738839dbc339cd45a5294d85c79361016243157aae7b60405180905060405180910390a15b600a8060416000396000f360606040526008565b00") ) for i := 0; i < 10; i++ { - statedb, _ := ethservice.BlockChain().StateAt(parent.Root) + statedb, _ := ethservice.BlockChain().StateAt(parent) tx := types.MustSignNewTx(testKey, signer, &types.LegacyTx{ Nonce: statedb.GetNonce(testAddr), Value: new(big.Int), @@ -1263,7 +1263,7 @@ func setupBodies(t *testing.T) (*node.Node, *eth.Ethereum, []*types.Block) { // Each block, this callback will include two txs that generate body values like logs and requests. callback := func(parent *types.Header) { var ( - statedb, _ = ethservice.BlockChain().StateAt(parent.Root) + statedb, _ = ethservice.BlockChain().StateAt(parent) // Create tx to trigger log generator. tx1, _ = types.SignTx(types.NewContractCreation(statedb.GetNonce(testAddr), new(big.Int), 1000000, big.NewInt(2*params.InitialBaseFee), logCode), types.LatestSigner(ethservice.BlockChain().Config()), testKey) // Create tx to trigger deposit generator. diff --git a/eth/ethconfig/config.go b/eth/ethconfig/config.go index 01aaaa751b..dd7436bf52 100644 --- a/eth/ethconfig/config.go +++ b/eth/ethconfig/config.go @@ -200,8 +200,8 @@ type Config struct { // OverrideBPO2 (TODO: remove after the fork) OverrideBPO2 *uint64 `toml:",omitempty"` - // OverrideVerkle (TODO: remove after the fork) - OverrideVerkle *uint64 `toml:",omitempty"` + // OverrideUBT (TODO: remove after the fork) + OverrideUBT *uint64 `toml:",omitempty"` // EIP-7966: eth_sendRawTransactionSync timeouts TxSyncDefaultTimeout time.Duration `toml:",omitempty"` diff --git a/eth/ethconfig/gen_config.go b/eth/ethconfig/gen_config.go index 6f94a409e5..ed85562f44 100644 --- a/eth/ethconfig/gen_config.go +++ b/eth/ethconfig/gen_config.go @@ -64,7 +64,7 @@ func (c Config) MarshalTOML() (interface{}, error) { OverrideOsaka *uint64 `toml:",omitempty"` OverrideBPO1 *uint64 `toml:",omitempty"` OverrideBPO2 *uint64 `toml:",omitempty"` - OverrideVerkle *uint64 `toml:",omitempty"` + OverrideUBT *uint64 `toml:",omitempty"` TxSyncDefaultTimeout time.Duration `toml:",omitempty"` TxSyncMaxTimeout time.Duration `toml:",omitempty"` RangeLimit uint64 `toml:",omitempty"` @@ -117,7 +117,7 @@ func (c Config) MarshalTOML() (interface{}, error) { enc.OverrideOsaka = c.OverrideOsaka enc.OverrideBPO1 = c.OverrideBPO1 enc.OverrideBPO2 = c.OverrideBPO2 - enc.OverrideVerkle = c.OverrideVerkle + enc.OverrideUBT = c.OverrideUBT enc.TxSyncDefaultTimeout = c.TxSyncDefaultTimeout enc.TxSyncMaxTimeout = c.TxSyncMaxTimeout enc.RangeLimit = c.RangeLimit @@ -174,7 +174,7 @@ func (c *Config) UnmarshalTOML(unmarshal func(interface{}) error) error { OverrideOsaka *uint64 `toml:",omitempty"` OverrideBPO1 *uint64 `toml:",omitempty"` OverrideBPO2 *uint64 `toml:",omitempty"` - OverrideVerkle *uint64 `toml:",omitempty"` + OverrideUBT *uint64 `toml:",omitempty"` TxSyncDefaultTimeout *time.Duration `toml:",omitempty"` TxSyncMaxTimeout *time.Duration `toml:",omitempty"` RangeLimit *uint64 `toml:",omitempty"` @@ -324,8 +324,8 @@ func (c *Config) UnmarshalTOML(unmarshal func(interface{}) error) error { if dec.OverrideBPO2 != nil { c.OverrideBPO2 = dec.OverrideBPO2 } - if dec.OverrideVerkle != nil { - c.OverrideVerkle = dec.OverrideVerkle + if dec.OverrideUBT != nil { + c.OverrideUBT = dec.OverrideUBT } if dec.TxSyncDefaultTimeout != nil { c.TxSyncDefaultTimeout = *dec.TxSyncDefaultTimeout diff --git a/eth/gasprice/gasprice_test.go b/eth/gasprice/gasprice_test.go index 02a25bc4d8..e57c6e11c5 100644 --- a/eth/gasprice/gasprice_test.go +++ b/eth/gasprice/gasprice_test.go @@ -104,7 +104,7 @@ func (b *testBackend) GetReceipts(ctx context.Context, hash common.Hash) (types. func (b *testBackend) Pending() (*types.Block, types.Receipts, *state.StateDB) { if b.pending { block := b.chain.GetBlockByNumber(testHead + 1) - state, _ := b.chain.StateAt(block.Root()) + state, _ := b.chain.StateAt(block.Header()) return block, b.chain.GetReceiptsByHash(block.Hash()), state } return nil, nil, nil diff --git a/eth/state_accessor.go b/eth/state_accessor.go index 04aac321cb..7467e1e590 100644 --- a/eth/state_accessor.go +++ b/eth/state_accessor.go @@ -56,7 +56,7 @@ func (eth *Ethereum) hashState(ctx context.Context, block *types.Block, base *st // The state is available in live database, create a reference // on top to prevent garbage collection and return a release // function to deref it. - if statedb, err = eth.blockchain.StateAt(block.Root()); err == nil { + if statedb, err = eth.blockchain.StateAt(block.Header()); err == nil { eth.blockchain.TrieDB().Reference(block.Root(), common.Hash{}) return statedb, func() { eth.blockchain.TrieDB().Dereference(block.Root()) @@ -182,11 +182,12 @@ func (eth *Ethereum) hashState(ctx context.Context, block *types.Block, base *st func (eth *Ethereum) pathState(block *types.Block) (*state.StateDB, func(), error) { // Check if the requested state is available in the live chain. - statedb, err := eth.blockchain.StateAt(block.Root()) + header := block.Header() + statedb, err := eth.blockchain.StateAt(header) if err == nil { return statedb, noopReleaser, nil } - statedb, err = eth.blockchain.HistoricState(block.Root()) + statedb, err = eth.blockchain.HistoricState(header) if err == nil { return statedb, noopReleaser, nil } diff --git a/eth/tracers/api.go b/eth/tracers/api.go index 53a09087e4..b5ddc78a10 100644 --- a/eth/tracers/api.go +++ b/eth/tracers/api.go @@ -1086,8 +1086,8 @@ func overrideConfig(original *params.ChainConfig, override *params.ChainConfig) copy.OsakaTime = timestamp canon = false } - if timestamp := override.VerkleTime; timestamp != nil { - copy.VerkleTime = timestamp + if timestamp := override.UBTTime; timestamp != nil { + copy.UBTTime = timestamp canon = false } diff --git a/eth/tracers/api_test.go b/eth/tracers/api_test.go index ecf3c99c8f..0e62b9631d 100644 --- a/eth/tracers/api_test.go +++ b/eth/tracers/api_test.go @@ -152,7 +152,7 @@ func (b *testBackend) teardown() { } func (b *testBackend) StateAtBlock(ctx context.Context, block *types.Block, base *state.StateDB, readOnly bool, preferDisk bool) (*state.StateDB, StateReleaseFunc, error) { - statedb, err := b.chain.StateAt(block.Root()) + statedb, err := b.chain.StateAt(block.Header()) if err != nil { return nil, nil, errStateNotFound } diff --git a/eth/tracers/internal/tracetest/selfdestruct_state_test.go b/eth/tracers/internal/tracetest/selfdestruct_state_test.go index bb1a3d9f18..692c5eb775 100644 --- a/eth/tracers/internal/tracetest/selfdestruct_state_test.go +++ b/eth/tracers/internal/tracetest/selfdestruct_state_test.go @@ -162,7 +162,7 @@ func setupTestBlockchain(t *testing.T, genesis *core.Genesis, tx *types.Transact if genesisBlock == nil { t.Fatalf("failed to get genesis block") } - statedb, err := blockchain.StateAt(genesisBlock.Root()) + statedb, err := blockchain.StateAt(genesisBlock.Header()) if err != nil { t.Fatalf("failed to get state: %v", err) } diff --git a/internal/ethapi/api_test.go b/internal/ethapi/api_test.go index b010eeaa08..6cf52d636a 100644 --- a/internal/ethapi/api_test.go +++ b/internal/ethapi/api_test.go @@ -572,7 +572,7 @@ func (b testBackend) StateAndHeaderByNumber(ctx context.Context, number rpc.Bloc if header == nil { return nil, nil, errors.New("header not found") } - stateDb, err := b.chain.StateAt(header.Root) + stateDb, err := b.chain.StateAt(header) return stateDb, header, err } func (b testBackend) StateAndHeaderByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*state.StateDB, *types.Header, error) { diff --git a/internal/ethapi/simulate.go b/internal/ethapi/simulate.go index eacb296132..90e88e83ee 100644 --- a/internal/ethapi/simulate.go +++ b/internal/ethapi/simulate.go @@ -317,7 +317,7 @@ func (sim *simulator) processBlock(ctx context.Context, block *simBlock, header, if precompiles != nil { evm.SetPrecompiles(precompiles) } - if sim.chainConfig.IsPrague(header.Number, header.Time) || sim.chainConfig.IsVerkle(header.Number, header.Time) { + if sim.chainConfig.IsPrague(header.Number, header.Time) || sim.chainConfig.IsUBT(header.Number, header.Time) { core.ProcessParentBlockHash(header.ParentHash, evm) } if header.ParentBeaconRoot != nil { diff --git a/miner/miner_test.go b/miner/miner_test.go index 13475a19b6..5411418b13 100644 --- a/miner/miner_test.go +++ b/miner/miner_test.go @@ -80,10 +80,14 @@ func (bc *testBlockChain) GetBlock(hash common.Hash, number uint64) *types.Block return types.NewBlock(bc.CurrentBlock(), nil, nil, trie.NewStackTrie(nil)) } -func (bc *testBlockChain) StateAt(common.Hash) (*state.StateDB, error) { +func (bc *testBlockChain) StateAt(header *types.Header) (*state.StateDB, error) { return bc.statedb, nil } +func (bc *testBlockChain) Genesis() *types.Block { + return types.NewBlock(bc.CurrentBlock(), nil, nil, trie.NewStackTrie(nil)) +} + func (bc *testBlockChain) HasState(root common.Hash) bool { return bc.root == root } diff --git a/miner/worker.go b/miner/worker.go index 39a61de318..1d648f0ee1 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -324,7 +324,7 @@ func (miner *Miner) prepareWork(ctx context.Context, genParams *generateParams, // makeEnv creates a new environment for the sealing block. func (miner *Miner) makeEnv(parent *types.Header, header *types.Header, coinbase common.Address, witness bool) (*environment, error) { // Retrieve the parent state to execute on top. - state, err := miner.chain.StateAt(parent.Root) + state, err := miner.chain.StateAtForkBoundary(parent, header) if err != nil { return nil, err } diff --git a/params/config.go b/params/config.go index 197ed56f8a..17508cbf27 100644 --- a/params/config.go +++ b/params/config.go @@ -207,7 +207,7 @@ var ( CancunTime: nil, PragueTime: nil, OsakaTime: nil, - VerkleTime: nil, + UBTTime: nil, Ethash: new(EthashConfig), Clique: nil, } @@ -263,7 +263,7 @@ var ( CancunTime: nil, PragueTime: nil, OsakaTime: nil, - VerkleTime: nil, + UBTTime: nil, TerminalTotalDifficulty: big.NewInt(math.MaxInt64), Ethash: nil, Clique: &CliqueConfig{Period: 0, Epoch: 30000}, @@ -293,7 +293,7 @@ var ( CancunTime: nil, PragueTime: nil, OsakaTime: nil, - VerkleTime: nil, + UBTTime: nil, TerminalTotalDifficulty: big.NewInt(math.MaxInt64), Ethash: new(EthashConfig), Clique: nil, @@ -323,7 +323,7 @@ var ( CancunTime: newUint64(0), PragueTime: newUint64(0), OsakaTime: newUint64(0), - VerkleTime: nil, + UBTTime: nil, TerminalTotalDifficulty: big.NewInt(0), Ethash: new(EthashConfig), Clique: nil, @@ -358,7 +358,7 @@ var ( CancunTime: nil, PragueTime: nil, OsakaTime: nil, - VerkleTime: nil, + UBTTime: nil, TerminalTotalDifficulty: big.NewInt(math.MaxInt64), Ethash: new(EthashConfig), Clique: nil, @@ -466,7 +466,7 @@ type ChainConfig struct { BPO4Time *uint64 `json:"bpo4Time,omitempty"` // BPO4 switch time (nil = no fork, 0 = already on bpo4) BPO5Time *uint64 `json:"bpo5Time,omitempty"` // BPO5 switch time (nil = no fork, 0 = already on bpo5) AmsterdamTime *uint64 `json:"amsterdamTime,omitempty"` // Amsterdam switch time (nil = no fork, 0 = already on amsterdam) - VerkleTime *uint64 `json:"verkleTime,omitempty"` // Verkle switch time (nil = no fork, 0 = already on verkle) + UBTTime *uint64 `json:"ubtTime,omitempty"` // UBT switch time (nil = no fork, 0 = already on UBT) // TerminalTotalDifficulty is the amount of total difficulty reached by // the network that triggers the consensus upgrade. @@ -474,18 +474,18 @@ type ChainConfig struct { DepositContractAddress common.Address `json:"depositContractAddress,omitempty"` - // EnableVerkleAtGenesis is a flag that specifies whether the network uses + // EnableUBTAtGenesis is a flag that specifies whether the network uses // the Verkle tree starting from the genesis block. If set to true, the - // genesis state will be committed using the Verkle tree, eliminating the - // need for any Verkle transition later. + // genesis state will be committed using the Binary tree, eliminating the + // need for any Binary transition later. // - // This is a temporary flag only for verkle devnet testing, where verkle is + // This is a temporary flag only for binary devnet testing, where binary is // activated at genesis, and the configured activation date has already passed. // - // In production networks (mainnet and public testnets), verkle activation + // In production networks (mainnet and public testnets), binary activation // always occurs after the genesis block, making this flag irrelevant in // those cases. - EnableVerkleAtGenesis bool `json:"enableVerkleAtGenesis,omitempty"` + EnableUBTAtGenesis bool `json:"enableUBTAtGenesis,omitempty"` // Various consensus engines Ethash *EthashConfig `json:"ethash,omitempty"` @@ -595,8 +595,8 @@ func (c *ChainConfig) String() string { if c.AmsterdamTime != nil { result += fmt.Sprintf(", AmsterdamTime: %v", *c.AmsterdamTime) } - if c.VerkleTime != nil { - result += fmt.Sprintf(", VerkleTime: %v", *c.VerkleTime) + if c.UBTTime != nil { + result += fmt.Sprintf(", UBTTime: %v", *c.UBTTime) } result += "}" return result @@ -690,8 +690,8 @@ func (c *ChainConfig) Description() string { if c.AmsterdamTime != nil { banner += fmt.Sprintf(" - Amsterdam: @%-10v blob: (%s)\n", *c.AmsterdamTime, c.BlobScheduleConfig.Amsterdam) } - if c.VerkleTime != nil { - banner += fmt.Sprintf(" - Verkle: @%-10v blob: (%s)\n", *c.VerkleTime, c.BlobScheduleConfig.Verkle) + if c.UBTTime != nil { + banner += fmt.Sprintf(" - UBT: @%-10v blob: (%s)\n", *c.UBTTime, c.BlobScheduleConfig.UBT) } banner += fmt.Sprintf("\nAll fork specifications can be found at https://ethereum.github.io/execution-specs/src/ethereum/forks/\n") return banner @@ -717,13 +717,13 @@ type BlobScheduleConfig struct { Cancun *BlobConfig `json:"cancun,omitempty"` Prague *BlobConfig `json:"prague,omitempty"` Osaka *BlobConfig `json:"osaka,omitempty"` - Verkle *BlobConfig `json:"verkle,omitempty"` BPO1 *BlobConfig `json:"bpo1,omitempty"` BPO2 *BlobConfig `json:"bpo2,omitempty"` BPO3 *BlobConfig `json:"bpo3,omitempty"` BPO4 *BlobConfig `json:"bpo4,omitempty"` BPO5 *BlobConfig `json:"bpo5,omitempty"` Amsterdam *BlobConfig `json:"amsterdam,omitempty"` + UBT *BlobConfig `json:"ubt,omitempty"` } // IsHomestead returns whether num is either equal to the homestead block or greater. @@ -866,12 +866,12 @@ func (c *ChainConfig) IsAmsterdam(num *big.Int, time uint64) bool { return c.IsLondon(num) && isTimestampForked(c.AmsterdamTime, time) } -// IsVerkle returns whether time is either equal to the Verkle fork time or greater. -func (c *ChainConfig) IsVerkle(num *big.Int, time uint64) bool { - return c.IsLondon(num) && isTimestampForked(c.VerkleTime, time) +// IsUBT returns whether time is either equal to the Verkle fork time or greater. +func (c *ChainConfig) IsUBT(num *big.Int, time uint64) bool { + return c.IsLondon(num) && isTimestampForked(c.UBTTime, time) } -// IsVerkleGenesis checks whether the verkle fork is activated at the genesis block. +// IsUBTGenesis checks whether the verkle fork is activated at the genesis block. // // Verkle mode is considered enabled if the verkle fork time is configured, // regardless of whether the local time has surpassed the fork activation time. @@ -881,13 +881,13 @@ func (c *ChainConfig) IsVerkle(num *big.Int, time uint64) bool { // In production networks (mainnet and public testnets), verkle activation // always occurs after the genesis block, making this function irrelevant in // those cases. -func (c *ChainConfig) IsVerkleGenesis() bool { - return c.EnableVerkleAtGenesis +func (c *ChainConfig) IsUBTGenesis() bool { + return c.EnableUBTAtGenesis } // IsEIP4762 returns whether eip 4762 has been activated at given block. func (c *ChainConfig) IsEIP4762(num *big.Int, time uint64) bool { - return c.IsVerkle(num, time) + return c.IsUBT(num, time) } // CheckCompatible checks whether scheduled fork transitions have been imported @@ -945,7 +945,7 @@ func (c *ChainConfig) CheckConfigForkOrder() error { {name: "cancunTime", timestamp: c.CancunTime, optional: true}, {name: "pragueTime", timestamp: c.PragueTime, optional: true}, {name: "osakaTime", timestamp: c.OsakaTime, optional: true}, - {name: "verkleTime", timestamp: c.VerkleTime, optional: true}, + {name: "ubtTime", timestamp: c.UBTTime, optional: true}, {name: "bpo1", timestamp: c.BPO1Time, optional: true}, {name: "bpo2", timestamp: c.BPO2Time, optional: true}, {name: "bpo3", timestamp: c.BPO3Time, optional: true}, @@ -1104,8 +1104,8 @@ func (c *ChainConfig) checkCompatible(newcfg *ChainConfig, headNumber *big.Int, if isForkTimestampIncompatible(c.OsakaTime, newcfg.OsakaTime, headTimestamp) { return newTimestampCompatError("Osaka fork timestamp", c.OsakaTime, newcfg.OsakaTime) } - if isForkTimestampIncompatible(c.VerkleTime, newcfg.VerkleTime, headTimestamp) { - return newTimestampCompatError("Verkle fork timestamp", c.VerkleTime, newcfg.VerkleTime) + if isForkTimestampIncompatible(c.UBTTime, newcfg.UBTTime, headTimestamp) { + return newTimestampCompatError("UBT fork timestamp", c.UBTTime, newcfg.UBTTime) } if isForkTimestampIncompatible(c.BPO1Time, newcfg.BPO1Time, headTimestamp) { return newTimestampCompatError("BPO1 fork timestamp", c.BPO1Time, newcfg.BPO1Time) @@ -1380,14 +1380,14 @@ type Rules struct { IsByzantium, IsConstantinople, IsPetersburg, IsIstanbul bool IsBerlin, IsLondon bool IsMerge, IsShanghai, IsCancun, IsPrague, IsOsaka bool - IsAmsterdam, IsVerkle bool + IsAmsterdam, IsUBT bool } // Rules ensures c's ChainID is not nil. func (c *ChainConfig) Rules(num *big.Int, isMerge bool, timestamp uint64) Rules { // disallow setting Merge out of order isMerge = isMerge && c.IsLondon(num) - isVerkle := isMerge && c.IsVerkle(num, timestamp) + isUBT := isMerge && c.IsUBT(num, timestamp) return Rules{ IsHomestead: c.IsHomestead(num), IsEIP150: c.IsEIP150(num), @@ -1398,7 +1398,7 @@ func (c *ChainConfig) Rules(num *big.Int, isMerge bool, timestamp uint64) Rules IsPetersburg: c.IsPetersburg(num), IsIstanbul: c.IsIstanbul(num), IsBerlin: c.IsBerlin(num), - IsEIP2929: c.IsBerlin(num) && !isVerkle, + IsEIP2929: c.IsBerlin(num) && !isUBT, IsLondon: c.IsLondon(num), IsMerge: isMerge, IsShanghai: isMerge && c.IsShanghai(num, timestamp), @@ -1406,7 +1406,7 @@ func (c *ChainConfig) Rules(num *big.Int, isMerge bool, timestamp uint64) Rules IsPrague: isMerge && c.IsPrague(num, timestamp), IsOsaka: isMerge && c.IsOsaka(num, timestamp), IsAmsterdam: isMerge && c.IsAmsterdam(num, timestamp), - IsVerkle: isVerkle, - IsEIP4762: isVerkle, + IsUBT: isUBT, + IsEIP4762: isUBT, } } diff --git a/tests/block_test_util.go b/tests/block_test_util.go index 00411073e2..bece8ae610 100644 --- a/tests/block_test_util.go +++ b/tests/block_test_util.go @@ -126,10 +126,10 @@ func (t *BlockTest) Run(snapshotter bool, scheme string, witness bool, tracer *t db = rawdb.NewMemoryDatabase() tconf = &triedb.Config{ Preimages: true, - IsVerkle: gspec.Config.VerkleTime != nil && *gspec.Config.VerkleTime <= gspec.Timestamp, + IsUBT: gspec.Config.UBTTime != nil && *gspec.Config.UBTTime <= gspec.Timestamp, } ) - if scheme == rawdb.PathScheme || tconf.IsVerkle { + if scheme == rawdb.PathScheme || tconf.IsUBT { tconf.PathDB = pathdb.Defaults } else { tconf.HashDB = hashdb.Defaults diff --git a/tests/init.go b/tests/init.go index f115e427a5..3db988a993 100644 --- a/tests/init.go +++ b/tests/init.go @@ -774,7 +774,7 @@ var Forks = map[string]*params.ChainConfig{ MergeNetsplitBlock: big.NewInt(0), TerminalTotalDifficulty: big.NewInt(0), ShanghaiTime: u64(0), - VerkleTime: u64(0), + UBTTime: u64(0), }, } diff --git a/tests/state_test_util.go b/tests/state_test_util.go index 1dd1bf6a04..569cc37913 100644 --- a/tests/state_test_util.go +++ b/tests/state_test_util.go @@ -544,7 +544,7 @@ func MakePreState(db ethdb.Database, accounts types.GenesisAlloc, snapshotter bo } snaps, _ = snapshot.New(snapconfig, db, triedb, root) } - sdb = state.NewDatabase(triedb, nil).WithSnapshot(snaps) + sdb = state.NewMPTDatabase(triedb, nil).WithSnapshot(snaps) statedb, _ = state.New(root, sdb) return StateTestState{statedb, triedb, snaps} } diff --git a/trie/bintrie/trie.go b/trie/bintrie/trie.go index b1e3c991c0..23d014eb33 100644 --- a/trie/bintrie/trie.go +++ b/trie/bintrie/trie.go @@ -377,8 +377,8 @@ func (t *BinaryTrie) Copy() *BinaryTrie { } } -// IsVerkle returns true if the trie is a Verkle tree. -func (t *BinaryTrie) IsVerkle() bool { +// IsUBT returns true if the trie is a Verkle tree. +func (t *BinaryTrie) IsUBT() bool { // TODO @gballet This is technically NOT a verkle tree, but it has the same // behavior and basic structure, so for all intents and purposes, it can be // treated as such. Rename this when verkle gets removed. diff --git a/trie/secure_trie.go b/trie/secure_trie.go index 1f150ede8c..4d03ca45f0 100644 --- a/trie/secure_trie.go +++ b/trie/secure_trie.go @@ -324,6 +324,6 @@ func (t *StateTrie) MustNodeIterator(start []byte) NodeIterator { return t.trie.MustNodeIterator(start) } -func (t *StateTrie) IsVerkle() bool { +func (t *StateTrie) IsUBT() bool { return false } diff --git a/trie/transitiontrie/transition.go b/trie/transitiontrie/transition.go index 4c73022082..3e5511be9e 100644 --- a/trie/transitiontrie/transition.go +++ b/trie/transitiontrie/transition.go @@ -202,8 +202,8 @@ func (t *TransitionTrie) Prove(key []byte, proofDb ethdb.KeyValueWriter) error { panic("not implemented") // TODO: Implement } -// IsVerkle returns true if the trie is verkle-tree based -func (t *TransitionTrie) IsVerkle() bool { +// IsUBT returns true if the trie is verkle-tree based +func (t *TransitionTrie) IsUBT() bool { // For all intents and purposes, the calling code should treat this as a verkle trie return true } diff --git a/triedb/database.go b/triedb/database.go index c1abe93462..533097c9e3 100644 --- a/triedb/database.go +++ b/triedb/database.go @@ -32,7 +32,7 @@ import ( // Config defines all necessary options for database. type Config struct { Preimages bool // Flag whether the preimage of node key is recorded - IsVerkle bool // Flag whether the db is holding a verkle tree + IsUBT bool // Flag whether the db is holding a verkle tree HashDB *hashdb.Config // Configs for hash-based scheme PathDB *pathdb.Config // Configs for experimental path-based scheme } @@ -41,15 +41,15 @@ type Config struct { // default settings. var HashDefaults = &Config{ Preimages: false, - IsVerkle: false, + IsUBT: false, HashDB: hashdb.Defaults, } -// VerkleDefaults represents a config for holding verkle trie data +// UBTDefaults represents a config for holding verkle trie data // using path-based scheme with default settings. -var VerkleDefaults = &Config{ +var UBTDefaults = &Config{ Preimages: false, - IsVerkle: true, + IsUBT: true, PathDB: pathdb.Defaults, } @@ -109,7 +109,7 @@ func NewDatabase(diskdb ethdb.Database, config *Config) *Database { log.Crit("Both 'hash' and 'path' mode are configured") } if config.PathDB != nil { - db.backend = pathdb.New(diskdb, config.PathDB, config.IsVerkle) + db.backend = pathdb.New(diskdb, config.PathDB, config.IsUBT) } else { db.backend = hashdb.New(diskdb, config.HashDB) } @@ -375,9 +375,9 @@ func (db *Database) IndexProgress() (uint64, uint64, error) { return pdb.IndexProgress() } -// IsVerkle returns the indicator if the database is holding a verkle tree. -func (db *Database) IsVerkle() bool { - return db.config.IsVerkle +// IsUBT returns the indicator if the database is holding a verkle tree. +func (db *Database) IsUBT() bool { + return db.config.IsUBT } // Disk returns the underlying disk database. diff --git a/triedb/pathdb/database.go b/triedb/pathdb/database.go index a61d302b1d..04c76cfd53 100644 --- a/triedb/pathdb/database.go +++ b/triedb/pathdb/database.go @@ -100,7 +100,7 @@ func merkleNodeHasher(blob []byte) (common.Hash, error) { // binaryNodeHasher computes the hash of the given verkle node. func binaryNodeHasher(blob []byte) (common.Hash, error) { if len(blob) == 0 { - return types.EmptyVerkleHash, nil + return types.EmptyBinaryHash, nil } n, err := bintrie.DeserializeNode(blob, 0) if err != nil { @@ -127,7 +127,7 @@ type Database struct { // the shutdown to reject all following unexpected mutations. readOnly bool // Flag if database is opened in read only mode waitSync bool // Flag if database is deactivated due to initial state sync - isVerkle bool // Flag if database is used for verkle tree + isUBT bool // Flag if database is used for verkle tree hasher nodeHasher // Trie node hasher config *Config // Configuration for database @@ -146,7 +146,7 @@ type Database struct { // New attempts to load an already existing layer from a persistent key-value // store (with a number of memory layers from a journal). If the journal is not // matched with the base persistent layer, all the recorded diff layers are discarded. -func New(diskdb ethdb.Database, config *Config, isVerkle bool) *Database { +func New(diskdb ethdb.Database, config *Config, isUBT bool) *Database { if config == nil { config = Defaults } @@ -154,7 +154,7 @@ func New(diskdb ethdb.Database, config *Config, isVerkle bool) *Database { db := &Database{ readOnly: config.ReadOnly, - isVerkle: isVerkle, + isUBT: isUBT, config: config, diskdb: diskdb, hasher: merkleNodeHasher, @@ -164,7 +164,7 @@ func New(diskdb ethdb.Database, config *Config, isVerkle bool) *Database { // important to note that the introduction of a prefix won't lead to // substantial storage overhead, as the underlying database will efficiently // compress the shared key prefix. - if isVerkle { + if isUBT { db.diskdb = rawdb.NewTable(diskdb, string(rawdb.VerklePrefix)) db.hasher = binaryNodeHasher } @@ -174,7 +174,7 @@ func New(diskdb ethdb.Database, config *Config, isVerkle bool) *Database { // Repair the history, which might not be aligned with the persistent // state in the key-value store due to an unclean shutdown. - states, trienodes, err := repairHistory(db.diskdb, isVerkle, db.config.ReadOnly, db.tree.bottom().stateID(), db.config.TrienodeHistory >= 0) + states, trienodes, err := repairHistory(db.diskdb, isUBT, db.config.ReadOnly, db.tree.bottom().stateID(), db.config.TrienodeHistory >= 0) if err != nil { log.Crit("Failed to repair history", "err", err) } @@ -196,7 +196,7 @@ func New(diskdb ethdb.Database, config *Config, isVerkle bool) *Database { db.setHistoryIndexer() fields := config.fields() - if db.isVerkle { + if db.isUBT { fields = append(fields, "verkle", true) } log.Info("Initialized path database", fields...) @@ -265,7 +265,7 @@ func (db *Database) setStateGenerator() error { // - the database is opened in read only mode // - the snapshot build is explicitly disabled // - the database is opened in verkle tree mode - noBuild := db.readOnly || db.config.SnapshotNoBuild || db.isVerkle + noBuild := db.readOnly || db.config.SnapshotNoBuild || db.isUBT // Construct the generator and link it to the disk layer, ensuring that the // generation progress is resolved to prevent accessing uncovered states @@ -408,7 +408,7 @@ func (db *Database) Enable(root common.Hash) error { // Re-construct a new disk layer backed by persistent state // and schedule the state snapshot generation if it's permitted. - db.tree.init(generateSnapshot(db, root, db.isVerkle || db.config.SnapshotNoBuild)) + db.tree.init(generateSnapshot(db, root, db.isUBT || db.config.SnapshotNoBuild)) // After snap sync, the state of the database may have changed completely. // To ensure the history indexer always matches the current state, we must: @@ -586,7 +586,7 @@ func (db *Database) journalPath() string { return "" } var fname string - if db.isVerkle { + if db.isUBT { fname = fmt.Sprintf("verkle.journal") } else { fname = fmt.Sprintf("merkle.journal") diff --git a/triedb/pathdb/database_test.go b/triedb/pathdb/database_test.go index e70a3ec2a2..8ceb22eaba 100644 --- a/triedb/pathdb/database_test.go +++ b/triedb/pathdb/database_test.go @@ -143,7 +143,7 @@ type testerConfig struct { layers int // Number of state transitions to generate for enableIndex bool // Enable state history indexing or not journalDir string // Directory path for persisting journal files - isVerkle bool // Enables Verkle trie mode if true + isUBT bool // Enables Verkle trie mode if true writeBuffer *int // Optional, the size of memory allocated for write buffer trieCache *int // Optional, the size of memory allocated for trie cache @@ -183,7 +183,7 @@ func newTester(t *testing.T, config *testerConfig) *tester { NoAsyncFlush: true, JournalDirectory: config.journalDir, NoHistoryIndexDelay: true, - }, config.isVerkle) + }, config.isUBT) obj = &tester{ db: db, diff --git a/triedb/pathdb/history.go b/triedb/pathdb/history.go index 0a9f7091fa..7f5b0e35ba 100644 --- a/triedb/pathdb/history.go +++ b/triedb/pathdb/history.go @@ -376,7 +376,7 @@ func syncHistory(stores ...ethdb.AncientWriter) error { // persistent state may appear if the trienode history was disabled during the // previous run. This process detects and resolves such gaps, preventing // unexpected panics. -func repairHistory(db ethdb.Database, isVerkle bool, readOnly bool, stateID uint64, enableTrienode bool) (ethdb.ResettableAncientStore, ethdb.ResettableAncientStore, error) { +func repairHistory(db ethdb.Database, isUBT bool, readOnly bool, stateID uint64, enableTrienode bool) (ethdb.ResettableAncientStore, ethdb.ResettableAncientStore, error) { ancient, err := db.AncientDatadir() if err != nil { // TODO error out if ancient store is disabled. A tons of unit tests @@ -386,7 +386,7 @@ func repairHistory(db ethdb.Database, isVerkle bool, readOnly bool, stateID uint } // State history is mandatory as it is the key component that ensures // resilience to deep reorgs. - states, err := rawdb.NewStateFreezer(ancient, isVerkle, readOnly) + states, err := rawdb.NewStateFreezer(ancient, isUBT, readOnly) if err != nil { log.Crit("Failed to open state history freezer", "err", err) } @@ -395,7 +395,7 @@ func repairHistory(db ethdb.Database, isVerkle bool, readOnly bool, stateID uint // node with state proofs. var trienodes ethdb.ResettableAncientStore if enableTrienode { - trienodes, err = rawdb.NewTrienodeFreezer(ancient, isVerkle, readOnly) + trienodes, err = rawdb.NewTrienodeFreezer(ancient, isUBT, readOnly) if err != nil { log.Crit("Failed to open trienode history freezer", "err", err) } diff --git a/triedb/pathdb/layertree_test.go b/triedb/pathdb/layertree_test.go index 82eb182990..0dcfd7aae8 100644 --- a/triedb/pathdb/layertree_test.go +++ b/triedb/pathdb/layertree_test.go @@ -55,9 +55,9 @@ func TestLayerCap(t *testing.T) { layers: 2, base: common.Hash{0x2}, snapshot: map[common.Hash]struct{}{ - common.Hash{0x2}: {}, - common.Hash{0x3}: {}, - common.Hash{0x4}: {}, + {0x2}: {}, + {0x3}: {}, + {0x4}: {}, }, }, { @@ -76,8 +76,8 @@ func TestLayerCap(t *testing.T) { layers: 1, base: common.Hash{0x3}, snapshot: map[common.Hash]struct{}{ - common.Hash{0x3}: {}, - common.Hash{0x4}: {}, + {0x3}: {}, + {0x4}: {}, }, }, { @@ -96,7 +96,7 @@ func TestLayerCap(t *testing.T) { layers: 0, base: common.Hash{0x4}, snapshot: map[common.Hash]struct{}{ - common.Hash{0x4}: {}, + {0x4}: {}, }, }, { @@ -119,9 +119,9 @@ func TestLayerCap(t *testing.T) { layers: 2, base: common.Hash{0x2a}, snapshot: map[common.Hash]struct{}{ - common.Hash{0x4a}: {}, - common.Hash{0x3a}: {}, - common.Hash{0x2a}: {}, + {0x4a}: {}, + {0x3a}: {}, + {0x2a}: {}, }, }, { @@ -144,8 +144,8 @@ func TestLayerCap(t *testing.T) { layers: 1, base: common.Hash{0x3a}, snapshot: map[common.Hash]struct{}{ - common.Hash{0x4a}: {}, - common.Hash{0x3a}: {}, + {0x4a}: {}, + {0x3a}: {}, }, }, { @@ -168,11 +168,11 @@ func TestLayerCap(t *testing.T) { layers: 2, base: common.Hash{0x2}, snapshot: map[common.Hash]struct{}{ - common.Hash{0x4a}: {}, - common.Hash{0x3a}: {}, - common.Hash{0x4b}: {}, - common.Hash{0x3b}: {}, - common.Hash{0x2}: {}, + {0x4a}: {}, + {0x3a}: {}, + {0x4b}: {}, + {0x3b}: {}, + {0x2}: {}, }, }, } @@ -261,7 +261,7 @@ func TestDescendant(t *testing.T) { return tr }, snapshotA: map[common.Hash]map[common.Hash]struct{}{ - common.Hash{0x1}: { + {0x1}: { common.Hash{0x2}: {}, }, }, @@ -271,11 +271,11 @@ func TestDescendant(t *testing.T) { tr.add(common.Hash{0x3}, common.Hash{0x2}, 2, NewNodeSetWithOrigin(nil, nil), NewStateSetWithOrigin(nil, nil, nil, nil, false)) }, snapshotB: map[common.Hash]map[common.Hash]struct{}{ - common.Hash{0x1}: { + {0x1}: { common.Hash{0x2}: {}, common.Hash{0x3}: {}, }, - common.Hash{0x2}: { + {0x2}: { common.Hash{0x3}: {}, }, }, @@ -291,16 +291,16 @@ func TestDescendant(t *testing.T) { return tr }, snapshotA: map[common.Hash]map[common.Hash]struct{}{ - common.Hash{0x1}: { + {0x1}: { common.Hash{0x2}: {}, common.Hash{0x3}: {}, common.Hash{0x4}: {}, }, - common.Hash{0x2}: { + {0x2}: { common.Hash{0x3}: {}, common.Hash{0x4}: {}, }, - common.Hash{0x3}: { + {0x3}: { common.Hash{0x4}: {}, }, }, @@ -310,11 +310,11 @@ func TestDescendant(t *testing.T) { tr.cap(common.Hash{0x4}, 2) }, snapshotB: map[common.Hash]map[common.Hash]struct{}{ - common.Hash{0x2}: { + {0x2}: { common.Hash{0x3}: {}, common.Hash{0x4}: {}, }, - common.Hash{0x3}: { + {0x3}: { common.Hash{0x4}: {}, }, }, @@ -330,16 +330,16 @@ func TestDescendant(t *testing.T) { return tr }, snapshotA: map[common.Hash]map[common.Hash]struct{}{ - common.Hash{0x1}: { + {0x1}: { common.Hash{0x2}: {}, common.Hash{0x3}: {}, common.Hash{0x4}: {}, }, - common.Hash{0x2}: { + {0x2}: { common.Hash{0x3}: {}, common.Hash{0x4}: {}, }, - common.Hash{0x3}: { + {0x3}: { common.Hash{0x4}: {}, }, }, @@ -349,7 +349,7 @@ func TestDescendant(t *testing.T) { tr.cap(common.Hash{0x4}, 1) }, snapshotB: map[common.Hash]map[common.Hash]struct{}{ - common.Hash{0x3}: { + {0x3}: { common.Hash{0x4}: {}, }, }, @@ -365,16 +365,16 @@ func TestDescendant(t *testing.T) { return tr }, snapshotA: map[common.Hash]map[common.Hash]struct{}{ - common.Hash{0x1}: { + {0x1}: { common.Hash{0x2}: {}, common.Hash{0x3}: {}, common.Hash{0x4}: {}, }, - common.Hash{0x2}: { + {0x2}: { common.Hash{0x3}: {}, common.Hash{0x4}: {}, }, - common.Hash{0x3}: { + {0x3}: { common.Hash{0x4}: {}, }, }, @@ -400,7 +400,7 @@ func TestDescendant(t *testing.T) { return tr }, snapshotA: map[common.Hash]map[common.Hash]struct{}{ - common.Hash{0x1}: { + {0x1}: { common.Hash{0x2a}: {}, common.Hash{0x3a}: {}, common.Hash{0x4a}: {}, @@ -408,18 +408,18 @@ func TestDescendant(t *testing.T) { common.Hash{0x3b}: {}, common.Hash{0x4b}: {}, }, - common.Hash{0x2a}: { + {0x2a}: { common.Hash{0x3a}: {}, common.Hash{0x4a}: {}, }, - common.Hash{0x3a}: { + {0x3a}: { common.Hash{0x4a}: {}, }, - common.Hash{0x2b}: { + {0x2b}: { common.Hash{0x3b}: {}, common.Hash{0x4b}: {}, }, - common.Hash{0x3b}: { + {0x3b}: { common.Hash{0x4b}: {}, }, }, @@ -429,11 +429,11 @@ func TestDescendant(t *testing.T) { tr.cap(common.Hash{0x4a}, 2) }, snapshotB: map[common.Hash]map[common.Hash]struct{}{ - common.Hash{0x2a}: { + {0x2a}: { common.Hash{0x3a}: {}, common.Hash{0x4a}: {}, }, - common.Hash{0x3a}: { + {0x3a}: { common.Hash{0x4a}: {}, }, }, @@ -453,7 +453,7 @@ func TestDescendant(t *testing.T) { return tr }, snapshotA: map[common.Hash]map[common.Hash]struct{}{ - common.Hash{0x1}: { + {0x1}: { common.Hash{0x2a}: {}, common.Hash{0x3a}: {}, common.Hash{0x4a}: {}, @@ -461,18 +461,18 @@ func TestDescendant(t *testing.T) { common.Hash{0x3b}: {}, common.Hash{0x4b}: {}, }, - common.Hash{0x2a}: { + {0x2a}: { common.Hash{0x3a}: {}, common.Hash{0x4a}: {}, }, - common.Hash{0x3a}: { + {0x3a}: { common.Hash{0x4a}: {}, }, - common.Hash{0x2b}: { + {0x2b}: { common.Hash{0x3b}: {}, common.Hash{0x4b}: {}, }, - common.Hash{0x3b}: { + {0x3b}: { common.Hash{0x4b}: {}, }, }, @@ -482,7 +482,7 @@ func TestDescendant(t *testing.T) { tr.cap(common.Hash{0x4a}, 1) }, snapshotB: map[common.Hash]map[common.Hash]struct{}{ - common.Hash{0x3a}: { + {0x3a}: { common.Hash{0x4a}: {}, }, }, @@ -501,23 +501,23 @@ func TestDescendant(t *testing.T) { return tr }, snapshotA: map[common.Hash]map[common.Hash]struct{}{ - common.Hash{0x1}: { + {0x1}: { common.Hash{0x2}: {}, common.Hash{0x3a}: {}, common.Hash{0x4a}: {}, common.Hash{0x3b}: {}, common.Hash{0x4b}: {}, }, - common.Hash{0x2}: { + {0x2}: { common.Hash{0x3a}: {}, common.Hash{0x4a}: {}, common.Hash{0x3b}: {}, common.Hash{0x4b}: {}, }, - common.Hash{0x3a}: { + {0x3a}: { common.Hash{0x4a}: {}, }, - common.Hash{0x3b}: { + {0x3b}: { common.Hash{0x4b}: {}, }, }, @@ -528,16 +528,16 @@ func TestDescendant(t *testing.T) { tr.cap(common.Hash{0x4a}, 2) }, snapshotB: map[common.Hash]map[common.Hash]struct{}{ - common.Hash{0x2}: { + {0x2}: { common.Hash{0x3a}: {}, common.Hash{0x4a}: {}, common.Hash{0x3b}: {}, common.Hash{0x4b}: {}, }, - common.Hash{0x3a}: { + {0x3a}: { common.Hash{0x4a}: {}, }, - common.Hash{0x3b}: { + {0x3b}: { common.Hash{0x4b}: {}, }, }, diff --git a/triedb/pathdb/reader.go b/triedb/pathdb/reader.go index e3cfbcba8a..18ec5f2028 100644 --- a/triedb/pathdb/reader.go +++ b/triedb/pathdb/reader.go @@ -177,7 +177,7 @@ func (db *Database) NodeReader(root common.Hash) (database.NodeReader, error) { return &reader{ db: db, state: root, - noHashCheck: db.isVerkle, + noHashCheck: db.isUBT, layer: layer, }, nil } From 89c1c16a46ce6390d53f8d17cd56caca9c2b15ac Mon Sep 17 00:00:00 2001 From: Edgar Date: Fri, 17 Apr 2026 03:53:00 +0200 Subject: [PATCH 33/40] cmd/geth: add code exporter for db export (#34696) Adds a 'code' exporter to 'geth db export' that iterates over all contract bytecode entries (CodePrefix + code_hash -> bytecode). Usage: geth --datadir db export code code.rlp This enables exporting contract bytecode. --- cmd/geth/dbcmd.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/cmd/geth/dbcmd.go b/cmd/geth/dbcmd.go index 455dd05aca..173fc97def 100644 --- a/cmd/geth/dbcmd.go +++ b/cmd/geth/dbcmd.go @@ -806,6 +806,24 @@ func (iter *snapshotIterator) Release() { iter.storage.Release() } +type codeIterator struct { + iter ethdb.Iterator +} + +func (iter *codeIterator) Next() (byte, []byte, []byte, bool) { + for iter.iter.Next() { + key := iter.iter.Key() + if bytes.HasPrefix(key, rawdb.CodePrefix) && len(key) == (len(rawdb.CodePrefix)+common.HashLength) { + return utils.OpBatchAdd, key, iter.iter.Value(), true + } + } + return 0, nil, nil, false +} + +func (iter *codeIterator) Release() { + iter.iter.Release() +} + // chainExporters defines the export scheme for all exportable chain data. var chainExporters = map[string]func(db ethdb.Database) utils.ChainDataIterator{ "preimage": func(db ethdb.Database) utils.ChainDataIterator { @@ -817,6 +835,10 @@ var chainExporters = map[string]func(db ethdb.Database) utils.ChainDataIterator{ storage := db.NewIterator(rawdb.SnapshotStoragePrefix, nil) return &snapshotIterator{account: account, storage: storage} }, + "code": func(db ethdb.Database) utils.ChainDataIterator { + iter := db.NewIterator(rawdb.CodePrefix, nil) + return &codeIterator{iter: iter} + }, } func exportChaindata(ctx *cli.Context) error { From 4c03a0631e6bc270c4481159ff9aa641ec513731 Mon Sep 17 00:00:00 2001 From: Daniel Liu <139250065@qq.com> Date: Fri, 17 Apr 2026 19:43:53 +0800 Subject: [PATCH 34/40] cmd/evm: compare errors by value in timedExec instead of interface identity (#34733) `timedExec` compares errors by direct interface inequality (haveErr != err). If execFunc returns newly constructed errors with the same message each run, this will panic even though behavior is equivalent. --- cmd/evm/runner.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/cmd/evm/runner.go b/cmd/evm/runner.go index ebb3e04461..82e7bdff3d 100644 --- a/cmd/evm/runner.go +++ b/cmd/evm/runner.go @@ -166,8 +166,11 @@ func timedExec(bench bool, execFunc func() ([]byte, uint64, error)) ([]byte, exe if haveGasUsed != gasUsed { panic(fmt.Sprintf("gas differs, have %v want %v", haveGasUsed, gasUsed)) } - if haveErr != err { - panic(fmt.Sprintf("err differs, have %v want %v", haveErr, err)) + if (haveErr == nil) != (err == nil) { + panic(fmt.Sprintf("err differs in nil-ness, have %v want %v", haveErr, err)) + } + if haveErr != nil && err != nil && haveErr.Error() != err.Error() { + panic(fmt.Sprintf("err differs, have %q want %q", haveErr.Error(), err.Error())) } } }) From 573d94013c92cec161f23aef1d74549dcb236ae1 Mon Sep 17 00:00:00 2001 From: Snehendu Roy <81818503+snehendu098@users.noreply.github.com> Date: Fri, 17 Apr 2026 17:15:03 +0530 Subject: [PATCH 35/40] core/rawdb: fix incorrect fsync ordering for index file truncation (#34728) --- core/rawdb/freezer_utils.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/core/rawdb/freezer_utils.go b/core/rawdb/freezer_utils.go index 7f1a978b63..eb9aeab451 100644 --- a/core/rawdb/freezer_utils.go +++ b/core/rawdb/freezer_utils.go @@ -76,6 +76,11 @@ func copyFrom(srcPath, destPath string, offset uint64, before func(f *os.File) e // we do the final move. src.Close() + // Permanently persist the content into disk + if err := f.Sync(); err != nil { + return err + } + if err := f.Close(); err != nil { return err } From 61bfacc52f79df35f2e3caa5c72f5dc9e650c685 Mon Sep 17 00:00:00 2001 From: CPerezz <37264926+CPerezz@users.noreply.github.com> Date: Sat, 18 Apr 2026 11:42:58 +0200 Subject: [PATCH 36/40] trie/bintrie: skip clean nodes in CollectNodes to reduce commit write amplification (#34754) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Problem `BinaryTrie.Commit` unconditionally walked every resolved in-memory node and flushed it into the `NodeSet`, producing one Pebble write per resolved internal + stem node on every block — even when the node's on-disk blob was bitwise identical to the previous commit. On a warm 400M-state workload this meant tens of thousands of redundant 65-byte writes per block, compounding Pebble compaction pressure on every commit. The existing `mustRecompute` flag tracks *hash* staleness, not *disk-blob* staleness: after `Hash()` completes, `mustRecompute` is cleared even though the fresh blob has not been persisted. It is therefore insufficient for a skip-flush optimization. ## Fix Mirror the MPT committer pattern (`trie/committer.go:51-56`) by adding a `dirty` flag on `InternalNode` and `StemNode` with the semantics *the on-disk blob is stale*. The flag is: - set to `true` wherever the node is created or structurally modified (the same call sites that already set `mustRecompute = true`); - set to `false` only after the node has been passed to the `flushfn` inside `CollectNodes`; - left `false` on nodes produced by `DeserializeNodeWithHash`, matching the *loaded from disk, already persisted* semantics. `CollectNodes` short-circuits on `!dirty` subtrees. The propagation invariant (an ancestor of any dirty node is itself dirty) is already maintained by the existing `InsertValuesAtStem` / `Insert` paths, which now mirror every `mustRecompute = true` setter with a `dirty = true` setter. ## Benchmark New `BenchmarkCollectNodes_SparseWrite` measures commit cost when only one leaf changes between blocks — the common case for state updates. 10,000-stem trie, one-leaf modification + Commit per iteration, Apple M4 Pro: | | before | after | delta | |---|---|---|---| | time / op | 12,653,000 ns | 7,336 ns | **~1,725×** | | bytes / op | 107,224,740 B | 37,774 B | **~2,839×** | | allocs / op | 80,953 | 134 | **~604×** | End-to-end impact on a real workload depends on the resolved-footprint-to-dirty-path ratio; the new `TestBinaryTrieCommitIncremental` provides a structural regression guard (asserts that a Commit following a single-leaf modification flushes a root-to-leaf path, not the whole tree). --- Found all of this stuff while bloating my #34706 DB to make some benchmarks. And saw we were spending A LOT OF TIME on hashing. Hope this helps the perf a bit. Will rebase the flat-state PR on top of this once merged. --- trie/bintrie/binary_node.go | 8 +- trie/bintrie/empty.go | 2 + trie/bintrie/empty_test.go | 42 ++++++++++ trie/bintrie/internal_node.go | 11 ++- trie/bintrie/internal_node_test.go | 52 ++++++++++++- trie/bintrie/stem_node.go | 24 +++++- trie/bintrie/stem_node_test.go | 42 ++++++++++ trie/bintrie/trie_test.go | 120 +++++++++++++++++++++++++++++ 8 files changed, 292 insertions(+), 9 deletions(-) diff --git a/trie/bintrie/binary_node.go b/trie/bintrie/binary_node.go index a7392ec958..8905a82285 100644 --- a/trie/bintrie/binary_node.go +++ b/trie/bintrie/binary_node.go @@ -93,15 +93,15 @@ var invalidSerializedLength = errors.New("invalid serialized node length") // DeserializeNode deserializes a binary trie node from a byte slice. The // hash will be recomputed from the deserialized data. func DeserializeNode(serialized []byte, depth int) (BinaryNode, error) { - return deserializeNode(serialized, depth, common.Hash{}, true) + return deserializeNode(serialized, depth, common.Hash{}, true, true) } // DeserializeNodeWithHash deserializes a binary trie node from a byte slice, using the provided hash. func DeserializeNodeWithHash(serialized []byte, depth int, hn common.Hash) (BinaryNode, error) { - return deserializeNode(serialized, depth, hn, false) + return deserializeNode(serialized, depth, hn, false, false) } -func deserializeNode(serialized []byte, depth int, hn common.Hash, mustRecompute bool) (BinaryNode, error) { +func deserializeNode(serialized []byte, depth int, hn common.Hash, mustRecompute, dirty bool) (BinaryNode, error) { if len(serialized) == 0 { return Empty{}, nil } @@ -117,6 +117,7 @@ func deserializeNode(serialized []byte, depth int, hn common.Hash, mustRecompute right: HashedNode(common.BytesToHash(serialized[33:65])), hash: hn, mustRecompute: mustRecompute, + dirty: dirty, }, nil case nodeTypeStem: if len(serialized) < 64 { @@ -141,6 +142,7 @@ func deserializeNode(serialized []byte, depth int, hn common.Hash, mustRecompute depth: depth, hash: hn, mustRecompute: mustRecompute, + dirty: dirty, }, nil default: return nil, errors.New("invalid node type") diff --git a/trie/bintrie/empty.go b/trie/bintrie/empty.go index 252146a4a7..c47e284dac 100644 --- a/trie/bintrie/empty.go +++ b/trie/bintrie/empty.go @@ -36,6 +36,7 @@ func (e Empty) Insert(key []byte, value []byte, _ NodeResolverFn, depth int) (Bi Values: values[:], depth: depth, mustRecompute: true, + dirty: true, }, nil } @@ -58,6 +59,7 @@ func (e Empty) InsertValuesAtStem(key []byte, values [][]byte, _ NodeResolverFn, Values: values, depth: depth, mustRecompute: true, + dirty: true, }, nil } diff --git a/trie/bintrie/empty_test.go b/trie/bintrie/empty_test.go index 574ae1830b..4da1ed15a0 100644 --- a/trie/bintrie/empty_test.go +++ b/trie/bintrie/empty_test.go @@ -220,3 +220,45 @@ func TestEmptyGetHeight(t *testing.T) { t.Errorf("Expected height 0 for empty node, got %d", height) } } + +// TestEmptyInsertMarksDirty verifies that a StemNode produced by Empty.Insert +// is marked dirty. Without this, CollectNodes would skip the freshly created +// stem and its blob would never reach disk, producing "missing trie node" +// errors on subsequent reads. +func TestEmptyInsertMarksDirty(t *testing.T) { + key := make([]byte, 32) + key[0] = 0xaa + val := make([]byte, 32) + val[0] = 0xbb + n, err := Empty{}.Insert(key, val, nil, 0) + if err != nil { + t.Fatalf("Insert: %v", err) + } + sn, ok := n.(*StemNode) + if !ok { + t.Fatalf("expected *StemNode, got %T", n) + } + if !sn.dirty { + t.Fatalf("stem produced by Empty.Insert must have dirty=true") + } +} + +// TestEmptyInsertValuesAtStemMarksDirty is the analogous guard for the +// bulk-insert entry point. Fresh stems created here must be dirty. +func TestEmptyInsertValuesAtStemMarksDirty(t *testing.T) { + key := make([]byte, 32) + key[0] = 0xcc + values := make([][]byte, 256) + values[0] = make([]byte, 32) + n, err := Empty{}.InsertValuesAtStem(key, values, nil, 3) + if err != nil { + t.Fatalf("InsertValuesAtStem: %v", err) + } + sn, ok := n.(*StemNode) + if !ok { + t.Fatalf("expected *StemNode, got %T", n) + } + if !sn.dirty { + t.Fatalf("stem produced by Empty.InsertValuesAtStem must have dirty=true") + } +} diff --git a/trie/bintrie/internal_node.go b/trie/bintrie/internal_node.go index 946203bcfb..811f65bcd8 100644 --- a/trie/bintrie/internal_node.go +++ b/trie/bintrie/internal_node.go @@ -62,6 +62,7 @@ type InternalNode struct { depth int mustRecompute bool // true if the hash needs to be recomputed + dirty bool // true if the node's on-disk blob is stale (needs flush) hash common.Hash // cached hash when mustRecompute == false } @@ -135,6 +136,7 @@ func (bt *InternalNode) Copy() BinaryNode { right: bt.right.Copy(), depth: bt.depth, mustRecompute: bt.mustRecompute, + dirty: bt.dirty, hash: bt.hash, } } @@ -213,6 +215,7 @@ func (bt *InternalNode) InsertValuesAtStem(stem []byte, values [][]byte, resolve bt.left, err = bt.left.InsertValuesAtStem(stem, values, resolver, depth+1) bt.mustRecompute = true + bt.dirty = true return bt, err } @@ -238,12 +241,17 @@ func (bt *InternalNode) InsertValuesAtStem(stem []byte, values [][]byte, resolve bt.right, err = bt.right.InsertValuesAtStem(stem, values, resolver, depth+1) bt.mustRecompute = true + bt.dirty = true return bt, err } // CollectNodes collects all child nodes at a given path, and flushes it -// into the provided node collector. +// into the provided node collector. Clean subtrees (dirty == false) are +// skipped. func (bt *InternalNode) CollectNodes(path []byte, flushfn NodeFlushFn) error { + if !bt.dirty { + return nil + } if bt.left != nil { var p [256]byte copy(p[:], path) @@ -263,6 +271,7 @@ func (bt *InternalNode) CollectNodes(path []byte, flushfn NodeFlushFn) error { } } flushfn(path, bt) + bt.dirty = false return nil } diff --git a/trie/bintrie/internal_node_test.go b/trie/bintrie/internal_node_test.go index 69097483fd..ddcec8085d 100644 --- a/trie/bintrie/internal_node_test.go +++ b/trie/bintrie/internal_node_test.go @@ -351,17 +351,20 @@ func TestInternalNodeInsertValuesAtStem(t *testing.T) { // TestInternalNodeCollectNodes tests CollectNodes method func TestInternalNodeCollectNodes(t *testing.T) { - // Create an internal node with two stem children + // Create an internal node with two stem children. All three are + // marked dirty to mirror production semantics — see CollectNodes. leftStem := &StemNode{ Stem: make([]byte, 31), Values: make([][]byte, 256), depth: 1, + dirty: true, } rightStem := &StemNode{ Stem: make([]byte, 31), Values: make([][]byte, 256), depth: 1, + dirty: true, } rightStem.Stem[0] = 0x80 @@ -369,6 +372,7 @@ func TestInternalNodeCollectNodes(t *testing.T) { depth: 0, left: leftStem, right: rightStem, + dirty: true, } var collectedPaths [][]byte @@ -405,6 +409,52 @@ func TestInternalNodeCollectNodes(t *testing.T) { } } +// TestInternalNodeCollectNodesSkipsClean verifies clean subtrees are not +// flushed. A dirty internal node over clean children only flushes itself; +// a fully clean tree flushes nothing. +func TestInternalNodeCollectNodesSkipsClean(t *testing.T) { + leftStem := &StemNode{ + Stem: make([]byte, 31), + Values: make([][]byte, 256), + depth: 1, + } + rightStem := &StemNode{ + Stem: make([]byte, 31), + Values: make([][]byte, 256), + depth: 1, + } + rightStem.Stem[0] = 0x80 + + dirtyParent := &InternalNode{ + depth: 0, + left: leftStem, + right: rightStem, + dirty: true, + } + + var collected []BinaryNode + flushFn := func(_ []byte, n BinaryNode) { collected = append(collected, n) } + + if err := dirtyParent.CollectNodes([]byte{1}, flushFn); err != nil { + t.Fatalf("CollectNodes: %v", err) + } + if len(collected) != 1 || collected[0] != dirtyParent { + t.Fatalf("expected only the dirty parent to be flushed, got %d nodes", len(collected)) + } + if dirtyParent.dirty { + t.Errorf("parent dirty flag should be cleared after flush") + } + + // Second call on the same tree should be a no-op: everything is clean. + collected = nil + if err := dirtyParent.CollectNodes([]byte{1}, flushFn); err != nil { + t.Fatalf("CollectNodes (second call): %v", err) + } + if len(collected) != 0 { + t.Errorf("expected no nodes to be flushed on clean tree, got %d", len(collected)) + } +} + // TestInternalNodeGetHeight tests GetHeight method func TestInternalNodeGetHeight(t *testing.T) { // Create a tree with different heights diff --git a/trie/bintrie/stem_node.go b/trie/bintrie/stem_node.go index e5729e6182..0ceae6b062 100644 --- a/trie/bintrie/stem_node.go +++ b/trie/bintrie/stem_node.go @@ -32,6 +32,7 @@ type StemNode struct { depth int // Depth of the node mustRecompute bool // true if the hash needs to be recomputed + dirty bool // true if the node's on-disk blob is stale (needs flush) hash common.Hash // cached hash when mustRecompute == false } @@ -48,8 +49,11 @@ func (bt *StemNode) Insert(key []byte, value []byte, _ NodeResolverFn, depth int if !bytes.Equal(bt.Stem, key[:StemSize]) { bitStem := bt.Stem[bt.depth/8] >> (7 - (bt.depth % 8)) & 1 - n := &InternalNode{depth: bt.depth, mustRecompute: true} + n := &InternalNode{depth: bt.depth, mustRecompute: true, dirty: true} bt.depth++ + // bt is re-parented under n and sits at a new path — rewrite its blob. + bt.mustRecompute = true + bt.dirty = true var child, other *BinaryNode if bitStem == 0 { n.left = bt @@ -77,6 +81,7 @@ func (bt *StemNode) Insert(key []byte, value []byte, _ NodeResolverFn, depth int Values: values[:], depth: depth + 1, mustRecompute: true, + dirty: true, } } return n, nil @@ -86,6 +91,7 @@ func (bt *StemNode) Insert(key []byte, value []byte, _ NodeResolverFn, depth int } bt.Values[key[StemSize]] = value bt.mustRecompute = true + bt.dirty = true return bt, nil } @@ -101,6 +107,7 @@ func (bt *StemNode) Copy() BinaryNode { depth: bt.depth, hash: bt.hash, mustRecompute: bt.mustRecompute, + dirty: bt.dirty, } } @@ -151,10 +158,14 @@ func (bt *StemNode) Hash() common.Hash { return bt.hash } -// CollectNodes collects all child nodes at a given path, and flushes it -// into the provided node collector. +// CollectNodes flushes the stem via the collector when dirty; clean stems +// are skipped. func (bt *StemNode) CollectNodes(path []byte, flush NodeFlushFn) error { + if !bt.dirty { + return nil + } flush(path, bt) + bt.dirty = false return nil } @@ -172,8 +183,11 @@ func (bt *StemNode) InsertValuesAtStem(key []byte, values [][]byte, _ NodeResolv if !bytes.Equal(bt.Stem, key[:StemSize]) { bitStem := bt.Stem[bt.depth/8] >> (7 - (bt.depth % 8)) & 1 - n := &InternalNode{depth: bt.depth, mustRecompute: true} + n := &InternalNode{depth: bt.depth, mustRecompute: true, dirty: true} bt.depth++ + // bt is re-parented under n and sits at a new path — rewrite its blob. + bt.mustRecompute = true + bt.dirty = true var child, other *BinaryNode if bitStem == 0 { n.left = bt @@ -199,6 +213,7 @@ func (bt *StemNode) InsertValuesAtStem(key []byte, values [][]byte, _ NodeResolv Values: values, depth: n.depth + 1, mustRecompute: true, + dirty: true, } } return n, nil @@ -209,6 +224,7 @@ func (bt *StemNode) InsertValuesAtStem(key []byte, values [][]byte, _ NodeResolv if v != nil { bt.Values[i] = v bt.mustRecompute = true + bt.dirty = true } } return bt, nil diff --git a/trie/bintrie/stem_node_test.go b/trie/bintrie/stem_node_test.go index 310c553d39..2743e7ce9b 100644 --- a/trie/bintrie/stem_node_test.go +++ b/trie/bintrie/stem_node_test.go @@ -379,6 +379,7 @@ func TestStemNodeCollectNodes(t *testing.T) { Stem: stem, Values: values[:], depth: 0, + dirty: true, } var collectedPaths [][]byte @@ -412,3 +413,44 @@ func TestStemNodeCollectNodes(t *testing.T) { t.Errorf("Path mismatch: expected [0, 1, 0], got %v", collectedPaths[0]) } } + +// TestStemNodeCollectNodesSkipsClean verifies that a clean stem is not +// flushed, and that flushing a dirty stem clears its dirty flag so that +// a subsequent CollectNodes on the same node is a no-op. +func TestStemNodeCollectNodesSkipsClean(t *testing.T) { + stem := make([]byte, 31) + node := &StemNode{ + Stem: stem, + Values: make([][]byte, 256), + depth: 0, + } + + var collected []BinaryNode + flushFn := func(_ []byte, n BinaryNode) { collected = append(collected, n) } + + if err := node.CollectNodes([]byte{0}, flushFn); err != nil { + t.Fatalf("CollectNodes on clean stem: %v", err) + } + if len(collected) != 0 { + t.Fatalf("expected clean stem not to be flushed, got %d", len(collected)) + } + + node.dirty = true + if err := node.CollectNodes([]byte{0}, flushFn); err != nil { + t.Fatalf("CollectNodes on dirty stem: %v", err) + } + if len(collected) != 1 { + t.Fatalf("expected dirty stem to be flushed once, got %d", len(collected)) + } + if node.dirty { + t.Errorf("stem dirty flag should be cleared after flush") + } + + collected = nil + if err := node.CollectNodes([]byte{0}, flushFn); err != nil { + t.Fatalf("CollectNodes after flush: %v", err) + } + if len(collected) != 0 { + t.Errorf("expected no flush on clean stem, got %d", len(collected)) + } +} diff --git a/trie/bintrie/trie_test.go b/trie/bintrie/trie_test.go index 5b104ddde4..c436501464 100644 --- a/trie/bintrie/trie_test.go +++ b/trie/bintrie/trie_test.go @@ -779,3 +779,123 @@ func TestGetStorageNonMembershipInternalRoot(t *testing.T) { t.Fatalf("expected nil/zero for non-existent storage, got %x", got) } } + +// commitKeyN derives a distinct 32-byte key from a seed integer. Used by +// TestBinaryTrieCommitIncremental and BenchmarkCollectNodes_SparseWrite to +// populate a trie with many disjoint stems. +func commitKeyN(i int) [HashSize]byte { + var k [HashSize]byte + binary.BigEndian.PutUint64(k[:8], uint64(i)*0x9e3779b97f4a7c15) + binary.BigEndian.PutUint64(k[8:16], uint64(i)*0xc2b2ae3d27d4eb4f) + binary.BigEndian.PutUint64(k[16:24], uint64(i)*0x165667b19e3779f9) + binary.BigEndian.PutUint64(k[24:32], uint64(i)*0x85ebca77c2b2ae63) + return k +} + +// TestBinaryTrieCommitIncremental verifies that a second Commit with only a +// single modified leaf flushes only the path from that leaf to the root, +// not the entire tree. +func TestBinaryTrieCommitIncremental(t *testing.T) { + tr := &BinaryTrie{ + root: NewBinaryNode(), + tracer: trie.NewPrevalueTracer(), + } + + const n = 512 + keys := make([][HashSize]byte, n) + for i := range n { + keys[i] = commitKeyN(i + 1) + var v [HashSize]byte + binary.BigEndian.PutUint64(v[24:], uint64(i+1)) + var err error + tr.root, err = tr.root.Insert(keys[i][:], v[:], nil, 0) + if err != nil { + t.Fatalf("Insert %d: %v", i, err) + } + } + + _, ns1 := tr.Commit(false) + if len(ns1.Nodes) == 0 { + t.Fatal("first Commit produced empty NodeSet") + } + if len(ns1.Nodes) < n { + t.Fatalf("first Commit: expected at least %d nodes, got %d", n, len(ns1.Nodes)) + } + + // Second Commit on the same trie with no modifications: NodeSet must + // be empty because every subtree is clean. + _, nsNoop := tr.Commit(false) + if len(nsNoop.Nodes) != 0 { + t.Fatalf("no-op Commit: expected empty NodeSet, got %d nodes", len(nsNoop.Nodes)) + } + + // Modify a single leaf's value. Only the path from that leaf to the + // root should appear in the next Commit's NodeSet. + var newVal [HashSize]byte + newVal[0] = 0xff + var err error + tr.root, err = tr.root.Insert(keys[n/2][:], newVal[:], nil, 0) + if err != nil { + t.Fatalf("Insert (modify): %v", err) + } + _, ns2 := tr.Commit(false) + + // Path length for a binary trie of n=512 stems is bounded by the + // internal depth at which the modified stem sits. Allow generous + // slack: up to 64 nodes is fine, anywhere near n (512) is a regression. + if len(ns2.Nodes) == 0 { + t.Fatal("modified Commit produced empty NodeSet") + } + if len(ns2.Nodes) > 64 { + t.Fatalf("modified Commit: expected small NodeSet, got %d nodes (first Commit had %d)", len(ns2.Nodes), len(ns1.Nodes)) + } + if len(ns2.Nodes) >= len(ns1.Nodes) { + t.Fatalf("expected second NodeSet (%d) to be smaller than first (%d)", len(ns2.Nodes), len(ns1.Nodes)) + } +} + +// BenchmarkCollectNodes_SparseWrite measures Commit cost when only one leaf +// changes between blocks — the common case for state updates. After warm-up +// (populate + initial Commit), each iteration modifies a single leaf and +// re-Commits. Under the skip-clean optimization, each iteration flushes +// only the root-to-leaf path; pre-fix behavior would re-flush the entire +// tree every iteration. +func BenchmarkCollectNodes_SparseWrite(b *testing.B) { + const n = 10_000 + + tr := &BinaryTrie{ + root: NewBinaryNode(), + tracer: trie.NewPrevalueTracer(), + } + keys := make([][HashSize]byte, n) + for i := range n { + keys[i] = commitKeyN(i + 1) + var v [HashSize]byte + binary.BigEndian.PutUint64(v[24:], uint64(i+1)) + var err error + tr.root, err = tr.root.Insert(keys[i][:], v[:], nil, 0) + if err != nil { + b.Fatalf("Insert %d: %v", i, err) + } + } + // Flush the initial tree so subsequent Commits reflect the + // single-modification workload we want to measure. + _, _ = tr.Commit(false) + + var newVal [HashSize]byte + b.ReportAllocs() + b.ResetTimer() + for i := 0; i < b.N; i++ { + idx := i % n + binary.BigEndian.PutUint64(newVal[24:], uint64(i+1)) + var err error + tr.root, err = tr.root.Insert(keys[idx][:], newVal[:], nil, 0) + if err != nil { + b.Fatalf("Insert at iter %d: %v", i, err) + } + _, ns := tr.Commit(false) + if len(ns.Nodes) == 0 { + b.Fatalf("iter %d: empty NodeSet", i) + } + } +} From 53ff723cc70de40dd31e819053fdb7e054f683d0 Mon Sep 17 00:00:00 2001 From: CPerezz <37264926+CPerezz@users.noreply.github.com> Date: Sat, 18 Apr 2026 18:47:22 +0200 Subject: [PATCH 37/40] core/state: handle *bintrie.BinaryTrie in mustCopyTrie (#34758) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Problem `mustCopyTrie` in `core/state/database.go` panics on any trie type not in its type switch: ```go func mustCopyTrie(t Trie) Trie { switch t := t.(type) { case *trie.StateTrie: return t.Copy() case *transitiontrie.TransitionTrie: return t.Copy() default: panic(fmt.Errorf("unknown trie type %T", t)) } } ``` On UBT-backed databases (`state.NewUBTDatabase(...)`, used by `blockchain.go:2124` when the triedb is configured for binary trie), `StateDB.trie` is `*bintrie.BinaryTrie` — so every `StateDB.Copy()` call (hit from `statedb.go:699` and the `*trie.StateTrie` branch of `state_object.go:546`) crashes with `unknown trie type *bintrie.BinaryTrie`. ## Fix Add the `*bintrie.BinaryTrie` case. `BinaryTrie.Copy()` already exists at `trie/bintrie/trie.go:372` and produces a correct deep copy — this just wires it into the switch. --- core/state/database.go | 3 +++ core/state/statedb_test.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/core/state/database.go b/core/state/database.go index 6de58af63b..b9ccff658f 100644 --- a/core/state/database.go +++ b/core/state/database.go @@ -24,6 +24,7 @@ import ( "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/trie" + "github.com/ethereum/go-ethereum/trie/bintrie" "github.com/ethereum/go-ethereum/trie/transitiontrie" "github.com/ethereum/go-ethereum/trie/trienode" "github.com/ethereum/go-ethereum/triedb" @@ -181,6 +182,8 @@ func mustCopyTrie(t Trie) Trie { return t.Copy() case *transitiontrie.TransitionTrie: return t.Copy() + case *bintrie.BinaryTrie: + return t.Copy() default: panic(fmt.Errorf("unknown trie type %T", t)) } diff --git a/core/state/statedb_test.go b/core/state/statedb_test.go index 601970ff20..b5ef42b3e0 100644 --- a/core/state/statedb_test.go +++ b/core/state/statedb_test.go @@ -1366,3 +1366,38 @@ func TestStorageDirtiness(t *testing.T) { state.RevertToSnapshot(snap) checkDirty(common.Hash{0x1}, common.Hash{0x1}, true) } + +// TestStateDBCopyUBT exercises StateDB.Copy on a UBT-backed state database. +// Before the mustCopyTrie fix, this panicked with "unknown trie type +// *bintrie.BinaryTrie" because the type switch in mustCopyTrie only covered +// *trie.StateTrie and *transitiontrie.TransitionTrie. +func TestStateDBCopyUBT(t *testing.T) { + disk := rawdb.NewMemoryDatabase() + tdb := triedb.NewDatabase(disk, triedb.UBTDefaults) + sdb := NewDatabase(tdb, nil) + + orig, err := New(types.EmptyRootHash, sdb) + if err != nil { + t.Fatalf("New: %v", err) + } + + // Touch the trie so StateDB.Copy actually has to copy it. + addr := common.HexToAddress("0x1111111111111111111111111111111111111111") + orig.SetBalance(addr, uint256.NewInt(1_000), tracing.BalanceChangeUnspecified) + + // Must not panic. + cpy := orig.Copy() + if cpy == nil { + t.Fatal("Copy returned nil") + } + + // The copy must be independent: mutating the copy does not affect the + // original. Use balance as an observable. + cpy.SetBalance(addr, uint256.NewInt(2_000), tracing.BalanceChangeUnspecified) + if got, want := orig.GetBalance(addr), uint256.NewInt(1_000); got.Cmp(want) != 0 { + t.Fatalf("original balance mutated through copy: got %s, want %s", got, want) + } + if got, want := cpy.GetBalance(addr), uint256.NewInt(2_000); got.Cmp(want) != 0 { + t.Fatalf("copy balance did not update: got %s, want %s", got, want) + } +} From 78505e48ddd27ce9e401b2f6f3bbb31d394acc99 Mon Sep 17 00:00:00 2001 From: cui Date: Mon, 20 Apr 2026 10:07:41 +0800 Subject: [PATCH 38/40] triedb/pathdb: fix typo (#34762) --- triedb/pathdb/reader.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/triedb/pathdb/reader.go b/triedb/pathdb/reader.go index 18ec5f2028..e087ef26ed 100644 --- a/triedb/pathdb/reader.go +++ b/triedb/pathdb/reader.go @@ -229,7 +229,7 @@ func (db *Database) HistoricReader(root common.Hash) (*HistoricalStateReader, er return nil, err // e.g., the referred state history has been pruned } if meta.parent != root { - return nil, fmt.Errorf("state %#x is not canonincal", root) + return nil, fmt.Errorf("state %#x is not canonical", root) } return &HistoricalStateReader{ id: *id, @@ -352,7 +352,7 @@ func (db *Database) HistoricNodeReader(root common.Hash) (*HistoricalNodeReader, return nil, fmt.Errorf("state %#x is not available", root) // e.g., the referred trienode history has been pruned } if meta.parent != root { - return nil, fmt.Errorf("state %#x is not canonincal", root) + return nil, fmt.Errorf("state %#x is not canonical", root) } return &HistoricalNodeReader{ id: *id, From 8c7d61fcfe8465a762a6b5e6da7a16732f6e9649 Mon Sep 17 00:00:00 2001 From: cui Date: Mon, 20 Apr 2026 11:02:42 +0800 Subject: [PATCH 39/40] tests: fix invalid eip parse error (#34750) --- tests/state_test_util.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/state_test_util.go b/tests/state_test_util.go index 569cc37913..f7cf1c0697 100644 --- a/tests/state_test_util.go +++ b/tests/state_test_util.go @@ -173,7 +173,7 @@ func GetChainConfig(forkString string) (baseConfig *params.ChainConfig, eips []i } for _, eip := range eipsStrings { if eipNum, err := strconv.Atoi(eip); err != nil { - return nil, nil, fmt.Errorf("syntax error, invalid eip number %v", eipNum) + return nil, nil, fmt.Errorf("syntax error, invalid eip number %v", eip) } else { if !vm.ValidEip(eipNum) { return nil, nil, fmt.Errorf("syntax error, invalid eip number %v", eipNum) From 5af5510b1ec05803593aa3d34e6c31264bdc1bd1 Mon Sep 17 00:00:00 2001 From: rayoo Date: Mon, 20 Apr 2026 11:06:17 +0800 Subject: [PATCH 40/40] core/rawdb: fix file descriptor leak in freezer error paths (#34735) In openFreezerFileForAppend, if Seek fails after the file is successfully opened, the file handle is not closed, leaking a descriptor. Similarly in newTable, if opening the meta file fails, the already-opened index file is not closed. And if newMetadata fails, both the index and meta files are leaked. Under repeated error conditions (e.g., corrupted filesystem), these leaks accumulate and may exhaust the OS file descriptor limit, causing cascading failures. --- core/rawdb/freezer_table.go | 4 ++++ core/rawdb/freezer_utils.go | 1 + 2 files changed, 5 insertions(+) diff --git a/core/rawdb/freezer_table.go b/core/rawdb/freezer_table.go index 280f6e1aaa..c770e89989 100644 --- a/core/rawdb/freezer_table.go +++ b/core/rawdb/freezer_table.go @@ -157,6 +157,7 @@ func newTable(path string, name string, readMeter, writeMeter *metrics.Meter, si } meta, err = openFreezerFileForReadOnly(filepath.Join(path, fmt.Sprintf("%s.meta", name))) if err != nil { + index.Close() return nil, err } } else { @@ -166,6 +167,7 @@ func newTable(path string, name string, readMeter, writeMeter *metrics.Meter, si } meta, err = openFreezerFileForAppend(filepath.Join(path, fmt.Sprintf("%s.meta", name))) if err != nil { + index.Close() return nil, err } } @@ -173,6 +175,8 @@ func newTable(path string, name string, readMeter, writeMeter *metrics.Meter, si // is detected. metadata, err := newMetadata(meta) if err != nil { + meta.Close() + index.Close() return nil, err } // Create the table and repair any past inconsistency diff --git a/core/rawdb/freezer_utils.go b/core/rawdb/freezer_utils.go index eb9aeab451..cd5239adc0 100644 --- a/core/rawdb/freezer_utils.go +++ b/core/rawdb/freezer_utils.go @@ -134,6 +134,7 @@ func openFreezerFileForAppend(filename string) (*os.File, error) { } // Seek to end for append if _, err = file.Seek(0, io.SeekEnd); err != nil { + file.Close() return nil, err } return file, nil