Skip to content

Commit a7e1edb

Browse files
committed
save state
Signed-off-by: Guillaume Ballet <[email protected]>
1 parent e9dca3b commit a7e1edb

File tree

2 files changed

+210
-1
lines changed

2 files changed

+210
-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.(*trie.SecureTrie), tr.(*trie.VerkleTrie), false), nil
257+
}
246258
}
247259
if err != nil {
248260
return nil, err

trie/transition.go

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
// Copyright 2021 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+
type TransitionTrie struct {
30+
overlay *VerkleTrie
31+
base *SecureTrie
32+
storage bool
33+
}
34+
35+
func NewTransitionTree(base *SecureTrie, overlay *VerkleTrie, st bool) *TransitionTrie {
36+
return &TransitionTrie{
37+
overlay: overlay,
38+
base: base,
39+
storage: st,
40+
}
41+
}
42+
43+
func (t *TransitionTrie) Base() *SecureTrie {
44+
return t.base
45+
}
46+
47+
// TODO(gballet/jsign): consider removing this API.
48+
func (t *TransitionTrie) Overlay() *VerkleTrie {
49+
return t.overlay
50+
}
51+
52+
// GetKey returns the sha3 preimage of a hashed key that was previously used
53+
// to store a value.
54+
//
55+
// TODO(fjl): remove this when StateTrie is removed
56+
func (t *TransitionTrie) GetKey(key []byte) []byte {
57+
if key := t.overlay.GetKey(key); key != nil {
58+
return key
59+
}
60+
return t.base.GetKey(key)
61+
}
62+
63+
// Get returns the value for key stored in the trie. The value bytes must
64+
// not be modified by the caller. If a node was not found in the database, a
65+
// trie.MissingNodeError is returned.
66+
func (t *TransitionTrie) GetStorage(addr common.Address, key []byte) ([]byte, error) {
67+
val, err := t.overlay.GetStorage(addr, key)
68+
if err != nil {
69+
return nil, fmt.Errorf("get storage from overlay: %s", err)
70+
}
71+
if len(val) != 0 {
72+
return val, nil
73+
}
74+
// TODO also insert value into overlay
75+
return t.base.GetStorage(addr, key)
76+
}
77+
78+
// GetAccount abstract an account read from the trie.
79+
func (t *TransitionTrie) GetAccount(address common.Address) (*types.StateAccount, error) {
80+
data, err := t.overlay.GetAccount(address)
81+
if err != nil {
82+
// Post cancun, no indicator needs to be used to indicate that
83+
// an account was deleted in the overlay tree. If an error is
84+
// returned, then it's a genuine error, and not an indicator
85+
// that a tombstone was found.
86+
return nil, err
87+
}
88+
if data != nil {
89+
if t.overlay.db.HasStorageRootConversion(address) {
90+
data.Root = t.overlay.db.StorageRootConversion(address)
91+
}
92+
return data, nil
93+
}
94+
return t.base.GetAccount(address)
95+
}
96+
97+
// Update 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. If a node was not found in the
100+
// database, a trie.MissingNodeError is returned.
101+
func (t *TransitionTrie) UpdateStorage(address common.Address, key []byte, value []byte) error {
102+
var v []byte
103+
if len(value) >= 32 {
104+
v = value[:32]
105+
} else {
106+
var val [32]byte
107+
copy(val[32-len(value):], value[:])
108+
v = val[:]
109+
}
110+
return t.overlay.UpdateStorage(address, key, v)
111+
}
112+
113+
// UpdateAccount abstract an account write to the trie.
114+
func (t *TransitionTrie) UpdateAccount(addr common.Address, account *types.StateAccount, codeLen int) error {
115+
if account.Root != (common.Hash{}) && account.Root != types.EmptyRootHash {
116+
t.overlay.db.SetStorageRootConversion(addr, account.Root)
117+
}
118+
return t.overlay.UpdateAccount(addr, account, codeLen)
119+
}
120+
121+
// Delete removes any existing value for key from the trie. If a node was not
122+
// found in the database, a trie.MissingNodeError is returned.
123+
func (t *TransitionTrie) DeleteStorage(addr common.Address, key []byte) error {
124+
return t.overlay.DeleteStorage(addr, key)
125+
}
126+
127+
// DeleteAccount abstracts an account deletion from the trie.
128+
func (t *TransitionTrie) DeleteAccount(key common.Address) error {
129+
return t.overlay.DeleteAccount(key)
130+
}
131+
132+
// Hash returns the root hash of the trie. It does not write to the database and
133+
// can be used even if the trie doesn't have one.
134+
func (t *TransitionTrie) Hash() common.Hash {
135+
return t.overlay.Hash()
136+
}
137+
138+
// Commit collects all dirty nodes in the trie and replace them with the
139+
// corresponding node hash. All collected nodes(including dirty leaves if
140+
// collectLeaf is true) will be encapsulated into a nodeset for return.
141+
// The returned nodeset can be nil if the trie is clean(nothing to commit).
142+
// Once the trie is committed, it's not usable anymore. A new trie must
143+
// be created with new root and updated trie database for following usage
144+
func (t *TransitionTrie) Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet) {
145+
// Just return if the trie is a storage trie: otherwise,
146+
// the overlay trie will be committed as many times as
147+
// there are storage tries. This would kill performance.
148+
if t.storage {
149+
return common.Hash{}, nil
150+
}
151+
return t.overlay.Commit(collectLeaf)
152+
}
153+
154+
// NodeIterator returns an iterator that returns nodes of the trie. Iteration
155+
// starts at the key after the given start key.
156+
func (t *TransitionTrie) NodeIterator(startKey []byte) (NodeIterator, error) {
157+
panic("not implemented") // TODO: Implement
158+
}
159+
160+
// Prove constructs a Merkle proof for key. The result contains all encoded nodes
161+
// on the path to the value at key. The value itself is also included in the last
162+
// node and can be retrieved by verifying the proof.
163+
//
164+
// If the trie does not contain a value for key, the returned proof contains all
165+
// nodes of the longest existing prefix of the key (at least the root), ending
166+
// with the node that proves the absence of the key.
167+
func (t *TransitionTrie) Prove(key []byte, proofDb ethdb.KeyValueWriter) error {
168+
panic("not implemented") // TODO: Implement
169+
}
170+
171+
// IsVerkle returns true if the trie is verkle-tree based
172+
func (t *TransitionTrie) IsVerkle() bool {
173+
// For all intents and purposes, the calling code should treat this as a verkle trie
174+
return true
175+
}
176+
177+
func (t *TransitionTrie) UpdateStem(key []byte, values [][]byte) error {
178+
trie := t.overlay
179+
switch root := trie.root.(type) {
180+
case *verkle.InternalNode:
181+
return root.InsertValuesAtStem(key, values, t.overlay.FlatdbNodeResolver)
182+
default:
183+
panic("invalid root type")
184+
}
185+
}
186+
187+
func (t *TransitionTrie) Copy() *TransitionTrie {
188+
return &TransitionTrie{
189+
overlay: t.overlay.Copy(),
190+
base: t.base.Copy(),
191+
storage: t.storage,
192+
}
193+
}
194+
195+
func (t *TransitionTrie) UpdateContractCode(addr common.Address, codeHash common.Hash, code []byte) error {
196+
return t.overlay.UpdateContractCode(addr, codeHash, code)
197+
}

0 commit comments

Comments
 (0)