Skip to content

Commit c6df8c1

Browse files
committed
done with impact analysis
1 parent 3cb93a7 commit c6df8c1

File tree

11 files changed

+535
-28
lines changed

11 files changed

+535
-28
lines changed

cmd/geth/dbcmd.go

+493
Large diffs are not rendered by default.

cmd/geth/verkle.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,10 @@ func expandVerkle(ctx *cli.Context) error {
202202

203203
for i, key := range keylist {
204204
log.Info("Reading key", "index", i, "key", keylist[0])
205-
root.Get(key, chaindb.Get)
205+
root.Get(key, 0, chaindb.Get)
206206
}
207207

208-
if err := os.WriteFile("dump.dot", []byte(verkle.ToDot(root)), 0600); err != nil {
208+
if err := os.WriteFile("dump.dot", []byte(verkle.ToDot(root)), 0o600); err != nil {
209209
log.Error("Failed to dump file", "err", err)
210210
} else {
211211
log.Info("Tree was dumped to file", "file", "dump.dot")

core/rawdb/accessors_snapshot.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/ethereum/go-ethereum/common"
2323
"github.com/ethereum/go-ethereum/ethdb"
2424
"github.com/ethereum/go-ethereum/log"
25+
"github.com/syndtr/goleveldb/leveldb"
2526
)
2627

2728
// ReadSnapshotDisabled retrieves if the snapshot maintenance is disabled.
@@ -93,9 +94,12 @@ func DeleteAccountSnapshot(db ethdb.KeyValueWriter, hash common.Hash) {
9394
}
9495

9596
func ReadAccountSnapshotMeta(db ethdb.KeyValueReader, hash common.Hash) uint64 {
96-
data, _ := db.Get(accountSnapshotMetaKey(hash))
97-
if len(data) != 8 {
98-
log.Crit("Failed to read account snapshot meta")
97+
data, err := db.Get(accountSnapshotMetaKey(hash))
98+
if err != nil {
99+
if err == leveldb.ErrNotFound {
100+
return 0
101+
}
102+
log.Crit("Failed to read account snapshot meta", "err", err)
99103
}
100104
return binary.BigEndian.Uint64(data)
101105
}
@@ -135,8 +139,10 @@ func DeleteStorageSnapshot(db ethdb.KeyValueWriter, accountHash, storageHash com
135139
func ReadStorageSnapshotMeta(db ethdb.KeyValueReader, accountHash, storageHash common.Hash) uint64 {
136140
data, err := db.Get(storageSnapshotMetaKey(accountHash, storageHash))
137141
if err != nil {
138-
log.Info("Failed to read storage snapshot meta", "err", err)
139-
return 0
142+
if err == leveldb.ErrNotFound {
143+
return 0
144+
}
145+
log.Crit("Failed to read storage snapshot meta", "err", err)
140146
}
141147
return binary.BigEndian.Uint64(data)
142148
}

core/state/statedb.go

+3
Original file line numberDiff line numberDiff line change
@@ -1220,6 +1220,9 @@ func (s *StateDB) commit(deleteEmptyObjects bool, noStorageWiping bool) (*stateU
12201220
// more expensive than it should be, so let's fix that and revisit this todo.
12211221
for addr, op := range s.mutations {
12221222
if op.isDelete() {
1223+
addrHash := common.BytesToHash(addr.Bytes())
1224+
delete(accountsMeta, addrHash)
1225+
delete(storagesMeta, addrHash)
12231226
continue
12241227
}
12251228
// Write any contract code associated with the state object

core/verkle_witness_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,11 @@ func TestProcessVerkle(t *testing.T) {
193193
})
194194

195195
// Check proof for both blocks
196-
err := verkle.Verify(proofs[0], gspec.ToBlock().Root().Bytes(), chain[0].Root().Bytes(), statediffs[0])
196+
err := verkle.Verify(proofs[0], gspec.ToBlock().Root().Bytes(), chain[0].Root().Bytes(), statediffs[0], 0)
197197
if err != nil {
198198
t.Fatal(err)
199199
}
200-
err = verkle.Verify(proofs[1], chain[0].Root().Bytes(), chain[1].Root().Bytes(), statediffs[1])
200+
err = verkle.Verify(proofs[1], chain[0].Root().Bytes(), chain[1].Root().Bytes(), statediffs[1], 0)
201201
if err != nil {
202202
t.Fatal(err)
203203
}

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,5 @@ require (
147147
gopkg.in/yaml.v2 v2.4.0 // indirect
148148
rsc.io/tmplfunc v0.0.3 // indirect
149149
)
150+
151+
replace github.com/ethereum/go-verkle => github.com/weiihann/go-verkle v0.11.3-0.20250221075231-54a369960b8d

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,6 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m
166166
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
167167
github.com/ethereum/c-kzg-4844 v1.0.0 h1:0X1LBXxaEtYD9xsyj9B9ctQEZIpnvVDeoBx8aHEwTNA=
168168
github.com/ethereum/c-kzg-4844 v1.0.0/go.mod h1:VewdlzQmpT5QSrVhbBuGoCdFJkpaJlO1aQputP83wc0=
169-
github.com/ethereum/go-verkle v0.2.2 h1:I2W0WjnrFUIzzVPwm8ykY+7pL2d4VhlsePn4j7cnFk8=
170-
github.com/ethereum/go-verkle v0.2.2/go.mod h1:M3b90YRnzqKyyzBEWJGqj8Qff4IDeXnzFw0P9bFw3uk=
171169
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
172170
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
173171
github.com/ferranbt/fastssz v0.1.2 h1:Dky6dXlngF6Qjc+EfDipAkE83N5I5DE68bY6O0VLNPk=
@@ -514,6 +512,8 @@ github.com/urfave/cli/v2 v2.27.5/go.mod h1:3Sevf16NykTbInEnD0yKkjDAeZDS0A6bzhBH5
514512
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
515513
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
516514
github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
515+
github.com/weiihann/go-verkle v0.11.3-0.20250221075231-54a369960b8d h1:YCVvFQJfSX8HAPJgns4wNLlXlnPoxrJ/cQfQhygx5/8=
516+
github.com/weiihann/go-verkle v0.11.3-0.20250221075231-54a369960b8d/go.mod h1:M3b90YRnzqKyyzBEWJGqj8Qff4IDeXnzFw0P9bFw3uk=
517517
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
518518
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=
519519
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=

trie/sync.go

+1
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,7 @@ func (s *Sync) scheduleCodeRequest(req *codeRequest) {
537537

538538
// children retrieves all the missing children of a state trie entry for future
539539
// retrieval scheduling.
540+
// TODO(weiihann): this is where it determines what trie nodes are missing!
540541
func (s *Sync) children(req *nodeRequest, object node) ([]*nodeRequest, error) {
541542
// Gather all the children of the node, irrelevant whether known or not
542543
type childNode struct {

trie/utils/verkle.go

+4
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,10 @@ func pointToHash(evaluated *verkle.Point, suffix byte) []byte {
278278
return retb[:]
279279
}
280280

281+
func EvaluateAddressPoint(address []byte) *verkle.Point {
282+
return evaluateAddressPoint(address)
283+
}
284+
281285
func evaluateAddressPoint(address []byte) *verkle.Point {
282286
if len(address) < 32 {
283287
var aligned [32]byte

trie/verkle.go

+13-15
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ import (
3232
"github.com/holiman/uint256"
3333
)
3434

35-
var (
36-
errInvalidRootType = errors.New("invalid node type for root")
37-
)
35+
var errInvalidRootType = errors.New("invalid node type for root")
3836

3937
// VerkleTrie is a wrapper around VerkleNode that implements the trie.Trie
4038
// interface so that Verkle trees can be reused verbatim.
@@ -90,7 +88,7 @@ func (t *VerkleTrie) GetAccount(addr common.Address) (*types.StateAccount, error
9088
)
9189
switch n := t.root.(type) {
9290
case *verkle.InternalNode:
93-
values, err = n.GetValuesAtStem(t.cache.GetStem(addr[:]), t.nodeResolver)
91+
values, err = n.GetValuesAtStem(t.cache.GetStem(addr[:]), 0, t.nodeResolver)
9492
if err != nil {
9593
return nil, fmt.Errorf("GetAccount (%x) error: %v", addr, err)
9694
}
@@ -114,7 +112,7 @@ func (t *VerkleTrie) GetAccount(addr common.Address) (*types.StateAccount, error
114112
// nil will be returned. If the tree is corrupted, an error will be returned.
115113
func (t *VerkleTrie) GetStorage(addr common.Address, key []byte) ([]byte, error) {
116114
k := utils.StorageSlotKeyWithEvaluatedAddress(t.cache.Get(addr.Bytes()), key)
117-
val, err := t.root.Get(k, t.nodeResolver)
115+
val, err := t.root.Get(k, 0, t.nodeResolver)
118116
if err != nil {
119117
return nil, err
120118
}
@@ -145,7 +143,7 @@ func (t *VerkleTrie) UpdateAccount(addr common.Address, acc *types.StateAccount,
145143

146144
switch root := t.root.(type) {
147145
case *verkle.InternalNode:
148-
err = root.InsertValuesAtStem(stem, values, t.nodeResolver)
146+
err = root.InsertValuesAtStem(stem, values, 0, true, t.nodeResolver)
149147
default:
150148
return errInvalidRootType
151149
}
@@ -167,7 +165,7 @@ func (t *VerkleTrie) UpdateStorage(address common.Address, key, value []byte) er
167165
copy(v[32-len(value):], value[:])
168166
}
169167
k := utils.StorageSlotKeyWithEvaluatedAddress(t.cache.Get(address.Bytes()), key)
170-
return t.root.Insert(k, v[:], t.nodeResolver)
168+
return t.root.Insert(k, v[:], 0, t.nodeResolver)
171169
}
172170

173171
// DeleteAccount leaves the account untouched, as no account deletion can happen
@@ -179,7 +177,7 @@ func (t *VerkleTrie) UpdateStorage(address common.Address, key, value []byte) er
179177
// the balance with 0. This will be removed once the spec has been clarified.
180178
func (t *VerkleTrie) DeleteAccount(addr common.Address) error {
181179
k := utils.BasicDataKeyWithEvaluatedAddress(t.cache.Get(addr.Bytes()))
182-
values, err := t.root.(*verkle.InternalNode).GetValuesAtStem(k, t.nodeResolver)
180+
values, err := t.root.(*verkle.InternalNode).GetValuesAtStem(k, 0, t.nodeResolver)
183181
if err != nil {
184182
return fmt.Errorf("Error getting data at %x in delete: %w", k, err)
185183
}
@@ -198,7 +196,7 @@ func (t *VerkleTrie) DeleteAccount(addr common.Address) error {
198196
}
199197
}
200198
if prefunded {
201-
t.root.Insert(k, common.Hash{}.Bytes(), t.nodeResolver)
199+
t.root.Insert(k, common.Hash{}.Bytes(), 0, t.nodeResolver)
202200
}
203201
return nil
204202
}
@@ -210,7 +208,7 @@ func (t *VerkleTrie) RollBackAccount(addr common.Address) error {
210208
evaluatedAddr = t.cache.Get(addr.Bytes())
211209
basicDataKey = utils.BasicDataKeyWithEvaluatedAddress(evaluatedAddr)
212210
)
213-
basicDataBytes, err := t.root.Get(basicDataKey, t.nodeResolver)
211+
basicDataBytes, err := t.root.Get(basicDataKey, 0, t.nodeResolver)
214212
if err != nil {
215213
return fmt.Errorf("rollback: error finding code size: %w", err)
216214
}
@@ -223,7 +221,7 @@ func (t *VerkleTrie) RollBackAccount(addr common.Address) error {
223221
codeSize := binary.BigEndian.Uint32(basicDataBytes[utils.BasicDataCodeSizeOffset-1:])
224222

225223
// Delete the account header + first 64 slots + first 128 code chunks
226-
_, err = t.root.(*verkle.InternalNode).DeleteAtStem(basicDataKey[:31], t.nodeResolver)
224+
_, err = t.root.(*verkle.InternalNode).DeleteAtStem(basicDataKey[:31], 0, t.nodeResolver)
227225
if err != nil {
228226
return fmt.Errorf("error rolling back account header: %w", err)
229227
}
@@ -234,7 +232,7 @@ func (t *VerkleTrie) RollBackAccount(addr common.Address) error {
234232
offset := uint256.NewInt(chunknr)
235233
key := utils.CodeChunkKeyWithEvaluatedAddress(evaluatedAddr, offset)
236234

237-
if _, err = t.root.(*verkle.InternalNode).DeleteAtStem(key[:], t.nodeResolver); err != nil {
235+
if _, err = t.root.(*verkle.InternalNode).DeleteAtStem(key[:], 0, t.nodeResolver); err != nil {
238236
return fmt.Errorf("error deleting code chunk stem (addr=%x, offset=%d) error: %w", addr[:], offset, err)
239237
}
240238
}
@@ -247,7 +245,7 @@ func (t *VerkleTrie) RollBackAccount(addr common.Address) error {
247245
func (t *VerkleTrie) DeleteStorage(addr common.Address, key []byte) error {
248246
var zero [32]byte
249247
k := utils.StorageSlotKeyWithEvaluatedAddress(t.cache.Get(addr.Bytes()), key)
250-
return t.root.Insert(k, zero[:], t.nodeResolver)
248+
return t.root.Insert(k, zero[:], 0, t.nodeResolver)
251249
}
252250

253251
// Hash returns the root hash of the tree. It does not write to the database and
@@ -317,7 +315,7 @@ func (t *VerkleTrie) Proof(posttrie *VerkleTrie, keys [][]byte) (*verkle.VerkleP
317315
if posttrie != nil {
318316
postroot = posttrie.root
319317
}
320-
proof, _, _, _, err := verkle.MakeVerkleMultiProof(t.root, postroot, keys, t.FlatdbNodeResolver)
318+
proof, _, _, _, err := verkle.MakeVerkleMultiProof(t.root, postroot, keys, 0, t.FlatdbNodeResolver)
321319
if err != nil {
322320
return nil, nil, err
323321
}
@@ -404,7 +402,7 @@ func (t *VerkleTrie) UpdateContractCode(addr common.Address, codeHash common.Has
404402
if groupOffset == 255 || len(chunks)-i <= 32 {
405403
switch root := t.root.(type) {
406404
case *verkle.InternalNode:
407-
err = root.InsertValuesAtStem(key[:31], values, t.nodeResolver)
405+
err = root.InsertValuesAtStem(key[:31], values, 0, true, t.nodeResolver)
408406
if err != nil {
409407
return fmt.Errorf("UpdateContractCode (addr=%x) error: %w", addr[:], err)
410408
}

trie/verkle_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func TestVerkleRollBack(t *testing.T) {
138138

139139
// ensure there is some code in the 2nd group of the 1st account
140140
keyOf2ndGroup := utils.CodeChunkKeyWithEvaluatedAddress(tr.cache.Get(common.Address{1}.Bytes()), uint256.NewInt(128))
141-
chunk, err := tr.root.Get(keyOf2ndGroup, nil)
141+
chunk, err := tr.root.Get(keyOf2ndGroup, 0, nil)
142142
if err != nil {
143143
t.Fatalf("Failed to get account, %v", err)
144144
}
@@ -163,7 +163,7 @@ func TestVerkleRollBack(t *testing.T) {
163163
}
164164

165165
// ensure that the last code chunk is also gone from the tree
166-
chunk, err = tr.root.Get(keyOf2ndGroup, nil)
166+
chunk, err = tr.root.Get(keyOf2ndGroup, 0, nil)
167167
if err != nil {
168168
t.Fatalf("Failed to get account, %v", err)
169169
}

0 commit comments

Comments
 (0)