Skip to content

Commit bc1ec69

Browse files
authored
trie/pathdb: state iterator (snapshot integration pt 4) (#30654)
In this pull request, the state iterator is implemented. It's mostly a copy-paste from the original state snapshot package, but still has some important changes to highlight here: (a) The iterator for the disk layer consists of a diff iterator and a disk iterator. Originally, the disk layer in the state snapshot was a wrapper around the disk, and its corresponding iterator was also a wrapper around the disk iterator. However, due to structural differences, the disk layer iterator is divided into two parts: - The disk iterator, which traverses the content stored on disk. - The diff iterator, which traverses the aggregated state buffer. Checkout `BinaryIterator` and `FastIterator` for more details. (b) The staleness management is improved in the diffAccountIterator and diffStorageIterator Originally, in the `diffAccountIterator`, the layer’s staleness had to be checked within the Next function to ensure the iterator remained usable. Additionally, a read lock on the associated diff layer was required to first retrieve the account blob. This read lock protection is essential to prevent concurrent map read/write. Afterward, a staleness check was performed to ensure the retrieved data was not outdated. The entire logic can be simplified as follows: a loadAccount callback is provided to retrieve account data. If the corresponding state is immutable (e.g., diff layers in the path database), the staleness check can be skipped, and a single account data retrieval is sufficient. However, if the corresponding state is mutable (e.g., the disk layer in the path database), the callback can operate as follows: ```go func(hash common.Hash) ([]byte, error) { dl.lock.RLock() defer dl.lock.RUnlock() if dl.stale { return nil, errSnapshotStale } return dl.buffer.states.mustAccount(hash) } ``` The callback solution can eliminate the complexity for managing concurrency with the read lock for atomic operation.
1 parent f808d73 commit bc1ec69

File tree

10 files changed

+2634
-63
lines changed

10 files changed

+2634
-63
lines changed

triedb/pathdb/database.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,3 +555,15 @@ func (db *Database) StorageHistory(address common.Address, slot common.Hash, sta
555555
func (db *Database) HistoryRange() (uint64, uint64, error) {
556556
return historyRange(db.freezer)
557557
}
558+
559+
// AccountIterator creates a new account iterator for the specified root hash and
560+
// seeks to a starting account hash.
561+
func (db *Database) AccountIterator(root common.Hash, seek common.Hash) (AccountIterator, error) {
562+
return newFastAccountIterator(db, root, seek)
563+
}
564+
565+
// StorageIterator creates a new storage iterator for the specified root hash and
566+
// account. The iterator will be moved to the specific start position.
567+
func (db *Database) StorageIterator(root common.Hash, account common.Hash, seek common.Hash) (StorageIterator, error) {
568+
return newFastStorageIterator(db, root, account, seek)
569+
}

triedb/pathdb/holdable_iterator.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// Copyright 2024 The 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 pathdb
18+
19+
import (
20+
"github.com/ethereum/go-ethereum/common"
21+
"github.com/ethereum/go-ethereum/ethdb"
22+
)
23+
24+
// holdableIterator is a wrapper of underlying database iterator. It extends
25+
// the basic iterator interface by adding Hold which can hold the element
26+
// locally where the iterator is currently located and serve it up next time.
27+
type holdableIterator struct {
28+
it ethdb.Iterator
29+
key []byte
30+
val []byte
31+
atHeld bool
32+
}
33+
34+
// newHoldableIterator initializes the holdableIterator with the given iterator.
35+
func newHoldableIterator(it ethdb.Iterator) *holdableIterator {
36+
return &holdableIterator{it: it}
37+
}
38+
39+
// Hold holds the element locally where the iterator is currently located which
40+
// can be served up next time.
41+
func (it *holdableIterator) Hold() {
42+
if it.it.Key() == nil {
43+
return // nothing to hold
44+
}
45+
it.key = common.CopyBytes(it.it.Key())
46+
it.val = common.CopyBytes(it.it.Value())
47+
it.atHeld = false
48+
}
49+
50+
// Next moves the iterator to the next key/value pair. It returns whether the
51+
// iterator is exhausted.
52+
func (it *holdableIterator) Next() bool {
53+
if !it.atHeld && it.key != nil {
54+
it.atHeld = true
55+
} else if it.atHeld {
56+
it.atHeld = false
57+
it.key = nil
58+
it.val = nil
59+
}
60+
if it.key != nil {
61+
return true // shifted to locally held value
62+
}
63+
return it.it.Next()
64+
}
65+
66+
// Error returns any accumulated error. Exhausting all the key/value pairs
67+
// is not considered to be an error.
68+
func (it *holdableIterator) Error() error { return it.it.Error() }
69+
70+
// Release releases associated resources. Release should always succeed and can
71+
// be called multiple times without causing error.
72+
func (it *holdableIterator) Release() {
73+
it.atHeld = false
74+
it.key = nil
75+
it.val = nil
76+
it.it.Release()
77+
}
78+
79+
// Key returns the key of the current key/value pair, or nil if done. The caller
80+
// should not modify the contents of the returned slice, and its contents may
81+
// change on the next call to Next.
82+
func (it *holdableIterator) Key() []byte {
83+
if it.key != nil {
84+
return it.key
85+
}
86+
return it.it.Key()
87+
}
88+
89+
// Value returns the value of the current key/value pair, or nil if done. The
90+
// caller should not modify the contents of the returned slice, and its contents
91+
// may change on the next call to Next.
92+
func (it *holdableIterator) Value() []byte {
93+
if it.val != nil {
94+
return it.val
95+
}
96+
return it.it.Value()
97+
}
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
// Copyright 2024 The 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 pathdb
18+
19+
import (
20+
"bytes"
21+
"testing"
22+
23+
"github.com/ethereum/go-ethereum/common"
24+
"github.com/ethereum/go-ethereum/core/rawdb"
25+
"github.com/ethereum/go-ethereum/ethdb"
26+
"github.com/ethereum/go-ethereum/ethdb/memorydb"
27+
)
28+
29+
func TestIteratorHold(t *testing.T) {
30+
// Create the key-value data store
31+
var (
32+
content = map[string]string{"k1": "v1", "k2": "v2", "k3": "v3"}
33+
order = []string{"k1", "k2", "k3"}
34+
db = rawdb.NewMemoryDatabase()
35+
)
36+
for key, val := range content {
37+
if err := db.Put([]byte(key), []byte(val)); err != nil {
38+
t.Fatalf("failed to insert item %s:%s into database: %v", key, val, err)
39+
}
40+
}
41+
// Iterate over the database with the given configs and verify the results
42+
it, idx := newHoldableIterator(db.NewIterator(nil, nil)), 0
43+
44+
// Nothing should be affected for calling Discard on non-initialized iterator
45+
it.Hold()
46+
47+
for it.Next() {
48+
if len(content) <= idx {
49+
t.Errorf("more items than expected: checking idx=%d (key %q), expecting len=%d", idx, it.Key(), len(order))
50+
break
51+
}
52+
if !bytes.Equal(it.Key(), []byte(order[idx])) {
53+
t.Errorf("item %d: key mismatch: have %s, want %s", idx, string(it.Key()), order[idx])
54+
}
55+
if !bytes.Equal(it.Value(), []byte(content[order[idx]])) {
56+
t.Errorf("item %d: value mismatch: have %s, want %s", idx, string(it.Value()), content[order[idx]])
57+
}
58+
// Should be safe to call discard multiple times
59+
it.Hold()
60+
it.Hold()
61+
62+
// Shift iterator to the discarded element
63+
it.Next()
64+
if !bytes.Equal(it.Key(), []byte(order[idx])) {
65+
t.Errorf("item %d: key mismatch: have %s, want %s", idx, string(it.Key()), order[idx])
66+
}
67+
if !bytes.Equal(it.Value(), []byte(content[order[idx]])) {
68+
t.Errorf("item %d: value mismatch: have %s, want %s", idx, string(it.Value()), content[order[idx]])
69+
}
70+
71+
// Discard/Next combo should work always
72+
it.Hold()
73+
it.Next()
74+
if !bytes.Equal(it.Key(), []byte(order[idx])) {
75+
t.Errorf("item %d: key mismatch: have %s, want %s", idx, string(it.Key()), order[idx])
76+
}
77+
if !bytes.Equal(it.Value(), []byte(content[order[idx]])) {
78+
t.Errorf("item %d: value mismatch: have %s, want %s", idx, string(it.Value()), content[order[idx]])
79+
}
80+
idx++
81+
}
82+
if err := it.Error(); err != nil {
83+
t.Errorf("iteration failed: %v", err)
84+
}
85+
if idx != len(order) {
86+
t.Errorf("iteration terminated prematurely: have %d, want %d", idx, len(order))
87+
}
88+
db.Close()
89+
}
90+
91+
func TestReopenIterator(t *testing.T) {
92+
var (
93+
content = map[common.Hash]string{
94+
common.HexToHash("a1"): "v1",
95+
common.HexToHash("a2"): "v2",
96+
common.HexToHash("a3"): "v3",
97+
common.HexToHash("a4"): "v4",
98+
common.HexToHash("a5"): "v5",
99+
common.HexToHash("a6"): "v6",
100+
}
101+
order = []common.Hash{
102+
common.HexToHash("a1"),
103+
common.HexToHash("a2"),
104+
common.HexToHash("a3"),
105+
common.HexToHash("a4"),
106+
common.HexToHash("a5"),
107+
common.HexToHash("a6"),
108+
}
109+
db = rawdb.NewMemoryDatabase()
110+
111+
reopen = func(db ethdb.KeyValueStore, iter *holdableIterator) *holdableIterator {
112+
if !iter.Next() {
113+
iter.Release()
114+
return newHoldableIterator(memorydb.New().NewIterator(nil, nil))
115+
}
116+
next := iter.Key()
117+
iter.Release()
118+
return newHoldableIterator(db.NewIterator(rawdb.SnapshotAccountPrefix, next[1:]))
119+
}
120+
)
121+
for key, val := range content {
122+
rawdb.WriteAccountSnapshot(db, key, []byte(val))
123+
}
124+
checkVal := func(it *holdableIterator, index int) {
125+
if !bytes.Equal(it.Key(), append(rawdb.SnapshotAccountPrefix, order[index].Bytes()...)) {
126+
t.Fatalf("Unexpected data entry key, want %v got %v", order[index], it.Key())
127+
}
128+
if !bytes.Equal(it.Value(), []byte(content[order[index]])) {
129+
t.Fatalf("Unexpected data entry key, want %v got %v", []byte(content[order[index]]), it.Value())
130+
}
131+
}
132+
// Iterate over the database with the given configs and verify the results
133+
dbIter := db.NewIterator(rawdb.SnapshotAccountPrefix, nil)
134+
iter, idx := newHoldableIterator(rawdb.NewKeyLengthIterator(dbIter, 1+common.HashLength)), -1
135+
136+
idx++
137+
iter.Next()
138+
checkVal(iter, idx)
139+
140+
iter = reopen(db, iter)
141+
idx++
142+
iter.Next()
143+
checkVal(iter, idx)
144+
145+
// reopen twice
146+
iter = reopen(db, iter)
147+
iter = reopen(db, iter)
148+
idx++
149+
iter.Next()
150+
checkVal(iter, idx)
151+
152+
// reopen iterator with held value
153+
iter.Next()
154+
iter.Hold()
155+
iter = reopen(db, iter)
156+
idx++
157+
iter.Next()
158+
checkVal(iter, idx)
159+
160+
// reopen twice iterator with held value
161+
iter.Next()
162+
iter.Hold()
163+
iter = reopen(db, iter)
164+
iter = reopen(db, iter)
165+
idx++
166+
iter.Next()
167+
checkVal(iter, idx)
168+
169+
// shift to the end and reopen
170+
iter.Next() // the end
171+
iter = reopen(db, iter)
172+
iter.Next()
173+
if iter.Key() != nil {
174+
t.Fatal("Unexpected iterated entry")
175+
}
176+
}

0 commit comments

Comments
 (0)