Skip to content

Commit ea3a717

Browse files
trie, core/state: add the transition tree (verkle transition part 2) (#32366)
This add some of the changes that were missing from #31634. It introduces the `TransitionTrie`, which is a façade pattern between the current MPT trie and the overlay tree. --------- Signed-off-by: Guillaume Ballet <[email protected]> Co-authored-by: rjl493456442 <[email protected]>
1 parent 2dbb580 commit ea3a717

File tree

2 files changed

+218
-1
lines changed

2 files changed

+218
-1
lines changed

core/state/reader.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"github.com/ethereum/go-ethereum/common"
2525
"github.com/ethereum/go-ethereum/common/lru"
26+
"github.com/ethereum/go-ethereum/core/overlay"
2627
"github.com/ethereum/go-ethereum/core/rawdb"
2728
"github.com/ethereum/go-ethereum/core/types"
2829
"github.com/ethereum/go-ethereum/crypto"
@@ -241,8 +242,19 @@ func newTrieReader(root common.Hash, db *triedb.Database, cache *utils.PointCach
241242
if !db.IsVerkle() {
242243
tr, err = trie.NewStateTrie(trie.StateTrieID(root), db)
243244
} else {
244-
// TODO @gballet determine the trie type (verkle or overlay) by transition state
245245
tr, err = trie.NewVerkleTrie(root, db, cache)
246+
247+
// Based on the transition status, determine if the overlay
248+
// tree needs to be created, or if a single, target tree is
249+
// to be picked.
250+
ts := overlay.LoadTransitionState(db.Disk(), root, true)
251+
if ts.InTransition() {
252+
mpt, err := trie.NewStateTrie(trie.StateTrieID(ts.BaseRoot), db)
253+
if err != nil {
254+
return nil, err
255+
}
256+
tr = trie.NewTransitionTree(mpt, tr.(*trie.VerkleTrie), false)
257+
}
246258
}
247259
if err != nil {
248260
return nil, err

trie/transition.go

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
// Copyright 2025 go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package trie
18+
19+
import (
20+
"fmt"
21+
22+
"github.com/ethereum/go-ethereum/common"
23+
"github.com/ethereum/go-ethereum/core/types"
24+
"github.com/ethereum/go-ethereum/ethdb"
25+
"github.com/ethereum/go-ethereum/trie/trienode"
26+
"github.com/ethereum/go-verkle"
27+
)
28+
29+
// TransitionTrie is a trie that implements a façade design pattern, presenting
30+
// a single interface to the old MPT trie and the new verkle/binary trie. Reads
31+
// first from the overlay trie, and falls back to the base trie if the key isn't
32+
// found. All writes go to the overlay trie.
33+
type TransitionTrie struct {
34+
overlay *VerkleTrie
35+
base *SecureTrie
36+
storage bool
37+
}
38+
39+
// NewTransitionTrie creates a new TransitionTrie.
40+
func NewTransitionTree(base *SecureTrie, overlay *VerkleTrie, st bool) *TransitionTrie {
41+
return &TransitionTrie{
42+
overlay: overlay,
43+
base: base,
44+
storage: st,
45+
}
46+
}
47+
48+
// Base returns the base trie.
49+
func (t *TransitionTrie) Base() *SecureTrie {
50+
return t.base
51+
}
52+
53+
// Overlay returns the overlay trie.
54+
func (t *TransitionTrie) Overlay() *VerkleTrie {
55+
return t.overlay
56+
}
57+
58+
// GetKey returns the sha3 preimage of a hashed key that was previously used
59+
// to store a value.
60+
func (t *TransitionTrie) GetKey(key []byte) []byte {
61+
if key := t.overlay.GetKey(key); key != nil {
62+
return key
63+
}
64+
return t.base.GetKey(key)
65+
}
66+
67+
// GetStorage returns the value for key stored in the trie. The value bytes must
68+
// not be modified by the caller.
69+
func (t *TransitionTrie) GetStorage(addr common.Address, key []byte) ([]byte, error) {
70+
val, err := t.overlay.GetStorage(addr, key)
71+
if err != nil {
72+
return nil, fmt.Errorf("get storage from overlay: %s", err)
73+
}
74+
if len(val) != 0 {
75+
return val, nil
76+
}
77+
// TODO also insert value into overlay
78+
return t.base.GetStorage(addr, key)
79+
}
80+
81+
// GetAccount abstract an account read from the trie.
82+
func (t *TransitionTrie) GetAccount(address common.Address) (*types.StateAccount, error) {
83+
data, err := t.overlay.GetAccount(address)
84+
if err != nil {
85+
// Post cancun, no indicator needs to be used to indicate that
86+
// an account was deleted in the overlay tree. If an error is
87+
// returned, then it's a genuine error, and not an indicator
88+
// that a tombstone was found.
89+
return nil, err
90+
}
91+
if data != nil {
92+
return data, nil
93+
}
94+
return t.base.GetAccount(address)
95+
}
96+
97+
// UpdateStorage associates key with value in the trie. If value has length zero, any
98+
// existing value is deleted from the trie. The value bytes must not be modified
99+
// by the caller while they are stored in the trie.
100+
func (t *TransitionTrie) UpdateStorage(address common.Address, key []byte, value []byte) error {
101+
var v []byte
102+
if len(value) >= 32 {
103+
v = value[:32]
104+
} else {
105+
var val [32]byte
106+
copy(val[32-len(value):], value[:])
107+
v = val[:]
108+
}
109+
return t.overlay.UpdateStorage(address, key, v)
110+
}
111+
112+
// UpdateAccount abstract an account write to the trie.
113+
func (t *TransitionTrie) UpdateAccount(addr common.Address, account *types.StateAccount, codeLen int) error {
114+
// NOTE: before the rebase, this was saving the state root, so that OpenStorageTrie
115+
// could still work during a replay. This is no longer needed, as OpenStorageTrie
116+
// only needs to know what the account trie does now.
117+
return t.overlay.UpdateAccount(addr, account, codeLen)
118+
}
119+
120+
// DeleteStorage removes any existing value for key from the trie. If a node was not
121+
// found in the database, a trie.MissingNodeError is returned.
122+
func (t *TransitionTrie) DeleteStorage(addr common.Address, key []byte) error {
123+
return t.overlay.DeleteStorage(addr, key)
124+
}
125+
126+
// DeleteAccount abstracts an account deletion from the trie.
127+
func (t *TransitionTrie) DeleteAccount(key common.Address) error {
128+
return t.overlay.DeleteAccount(key)
129+
}
130+
131+
// Hash returns the root hash of the trie. It does not write to the database and
132+
// can be used even if the trie doesn't have one.
133+
func (t *TransitionTrie) Hash() common.Hash {
134+
return t.overlay.Hash()
135+
}
136+
137+
// Commit collects all dirty nodes in the trie and replace them with the
138+
// corresponding node hash. All collected nodes(including dirty leaves if
139+
// collectLeaf is true) will be encapsulated into a nodeset for return.
140+
// The returned nodeset can be nil if the trie is clean(nothing to commit).
141+
// Once the trie is committed, it's not usable anymore. A new trie must
142+
// be created with new root and updated trie database for following usage
143+
func (t *TransitionTrie) Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet) {
144+
// Just return if the trie is a storage trie: otherwise,
145+
// the overlay trie will be committed as many times as
146+
// there are storage tries. This would kill performance.
147+
if t.storage {
148+
return common.Hash{}, nil
149+
}
150+
return t.overlay.Commit(collectLeaf)
151+
}
152+
153+
// NodeIterator returns an iterator that returns nodes of the trie. Iteration
154+
// starts at the key after the given start key.
155+
func (t *TransitionTrie) NodeIterator(startKey []byte) (NodeIterator, error) {
156+
panic("not implemented") // TODO: Implement
157+
}
158+
159+
// Prove constructs a Merkle proof for key. The result contains all encoded nodes
160+
// on the path to the value at key. The value itself is also included in the last
161+
// node and can be retrieved by verifying the proof.
162+
//
163+
// If the trie does not contain a value for key, the returned proof contains all
164+
// nodes of the longest existing prefix of the key (at least the root), ending
165+
// with the node that proves the absence of the key.
166+
func (t *TransitionTrie) Prove(key []byte, proofDb ethdb.KeyValueWriter) error {
167+
panic("not implemented") // TODO: Implement
168+
}
169+
170+
// IsVerkle returns true if the trie is verkle-tree based
171+
func (t *TransitionTrie) IsVerkle() bool {
172+
// For all intents and purposes, the calling code should treat this as a verkle trie
173+
return true
174+
}
175+
176+
// UpdateStems updates a group of values, given the stem they are using. If
177+
// a value already exists, it is overwritten.
178+
func (t *TransitionTrie) UpdateStem(key []byte, values [][]byte) error {
179+
trie := t.overlay
180+
switch root := trie.root.(type) {
181+
case *verkle.InternalNode:
182+
return root.InsertValuesAtStem(key, values, t.overlay.nodeResolver)
183+
default:
184+
panic("invalid root type")
185+
}
186+
}
187+
188+
// Copy creates a deep copy of the transition trie.
189+
func (t *TransitionTrie) Copy() *TransitionTrie {
190+
return &TransitionTrie{
191+
overlay: t.overlay.Copy(),
192+
base: t.base.Copy(),
193+
storage: t.storage,
194+
}
195+
}
196+
197+
// UpdateContractCode updates the contract code for the given address.
198+
func (t *TransitionTrie) UpdateContractCode(addr common.Address, codeHash common.Hash, code []byte) error {
199+
return t.overlay.UpdateContractCode(addr, codeHash, code)
200+
}
201+
202+
// Witness returns a set containing all trie nodes that have been accessed.
203+
func (t *TransitionTrie) Witness() map[string]struct{} {
204+
panic("not implemented")
205+
}

0 commit comments

Comments
 (0)