Skip to content

Commit 5a057a8

Browse files
committed
core/state, trie: surface iterator entry hashes
1 parent 7e29b0b commit 5a057a8

6 files changed

Lines changed: 114 additions & 20 deletions

File tree

‎core/state/iterator.go‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@ type NodeIterator struct {
3333

3434
stateIt *trie.NodeIterator // Primary iterator for the global state trie
3535
dataIt *trie.NodeIterator // Secondary iterator for the data trie of a contract
36-
code []byte // Source code associated with a contract
3736

37+
codeHash common.Hash // Hash of the contract source code
38+
code []byte // Source code associated with a contract
39+
40+
Hash common.Hash // Hash of the current entry being iterated (nil if not standalone)
3841
Entry interface{} // Current state entry being iterated (internal representation)
3942
}
4043

@@ -103,6 +106,7 @@ func (it *NodeIterator) step() {
103106
it.dataIt = nil
104107
}
105108
if bytes.Compare(account.CodeHash, emptyCodeHash) != 0 {
109+
it.codeHash = common.BytesToHash(account.CodeHash)
106110
it.code, err = it.state.db.Get(account.CodeHash)
107111
if err != nil {
108112
panic(fmt.Sprintf("code %x: %v", account.CodeHash, err))
@@ -114,7 +118,7 @@ func (it *NodeIterator) step() {
114118
// The method returns whether there are any more data left for inspection.
115119
func (it *NodeIterator) retrieve() bool {
116120
// Clear out any previously set values
117-
it.Entry = nil
121+
it.Hash, it.Entry = common.Hash{}, nil
118122

119123
// If the iteration's done, return no available data
120124
if it.state == nil {
@@ -123,11 +127,11 @@ func (it *NodeIterator) retrieve() bool {
123127
// Otherwise retrieve the current entry
124128
switch {
125129
case it.dataIt != nil:
126-
it.Entry = it.dataIt.Node
130+
it.Hash, it.Entry = it.dataIt.Hash, it.dataIt.Node
127131
case it.code != nil:
128-
it.Entry = it.code
132+
it.Hash, it.Entry = it.codeHash, it.code
129133
case it.stateIt != nil:
130-
it.Entry = it.stateIt.Node
134+
it.Hash, it.Entry = it.stateIt.Hash, it.stateIt.Node
131135
}
132136
return true
133137
}

‎core/state/iterator_test.go‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2015 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 state
18+
19+
import (
20+
"bytes"
21+
"testing"
22+
23+
"github.com/ethereum/go-ethereum/common"
24+
"github.com/ethereum/go-ethereum/ethdb"
25+
)
26+
27+
// Tests that the node iterator indeed walks over the entire database contents.
28+
func TestNodeIteratorCoverage(t *testing.T) {
29+
// Create some arbitrary test state to iterate
30+
db, root, _ := makeTestState()
31+
32+
state, err := New(root, db)
33+
if err != nil {
34+
t.Fatalf("failed to create state trie at %x: %v", root, err)
35+
}
36+
// Gather all the node hashes found by the iterator
37+
hashes := make(map[common.Hash]struct{})
38+
for it := NewNodeIterator(state); it.Next(); {
39+
if it.Hash != (common.Hash{}) {
40+
hashes[it.Hash] = struct{}{}
41+
}
42+
}
43+
// Cross check the hashes and the database itself
44+
for hash, _ := range hashes {
45+
if _, err := db.Get(hash.Bytes()); err != nil {
46+
t.Errorf("failed to retrieve reported node %x: %v", hash, err)
47+
}
48+
}
49+
for _, key := range db.(*ethdb.MemDatabase).Keys() {
50+
if bytes.HasPrefix(key, []byte("secure-key-")) {
51+
continue
52+
}
53+
if _, ok := hashes[common.BytesToHash(key)]; !ok {
54+
t.Errorf("state entry not reported %x", key)
55+
}
56+
}
57+
}

‎core/state/sync_test.go‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,7 @@ func checkStateConsistency(db ethdb.Database, root common.Hash) (failure error)
116116
if err != nil {
117117
return
118118
}
119-
it := NewNodeIterator(state)
120-
for it.Next() {
119+
for it := NewNodeIterator(state); it.Next(); {
121120
}
122121
return nil
123122
}

‎trie/iterator.go‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bytes"
2121
"fmt"
2222

23+
"github.com/ethereum/go-ethereum/common"
2324
"github.com/ethereum/go-ethereum/logger"
2425
"github.com/ethereum/go-ethereum/logger/glog"
2526
)
@@ -152,18 +153,20 @@ func (self *Iterator) key(node interface{}) []byte {
152153
// nodeIteratorState represents the iteration state at one particular node of the
153154
// trie, which can be resumed at a later invocation.
154155
type nodeIteratorState struct {
155-
node node // Trie node being iterated
156-
child int // Child to be processed next
156+
hash common.Hash // Hash of the node being iterated (nil if not standalone)
157+
node node // Trie node being iterated
158+
child int // Child to be processed next
157159
}
158160

159161
// NodeIterator is an iterator to traverse the trie post-order.
160162
type NodeIterator struct {
161163
trie *Trie // Trie being iterated
162164
stack []*nodeIteratorState // Hierarchy of trie nodes persisting the iteration state
163165

164-
Node node // Current node being iterated (internal representation)
165-
Leaf bool // Flag whether the current node is a value (data) node
166-
LeafBlob []byte // Data blob contained within a leaf (otherwise nil)
166+
Hash common.Hash // Hash of the current node being iterated (nil if not standalone)
167+
Node node // Current node being iterated (internal representation)
168+
Leaf bool // Flag whether the current node is a value (data) node
169+
LeafBlob []byte // Data blob contained within a leaf (otherwise nil)
167170
}
168171

169172
// NewNodeIterator creates an post-order trie iterator.
@@ -221,18 +224,18 @@ func (it *NodeIterator) step() {
221224
}
222225
parent.child++
223226
it.stack = append(it.stack, &nodeIteratorState{node: node.Val, child: -1})
224-
} else if node, ok := parent.node.(hashNode); ok {
227+
} else if hash, ok := parent.node.(hashNode); ok {
225228
// Hash node, resolve the hash child from the database, then the node itself
226229
if parent.child >= 0 {
227230
break
228231
}
229232
parent.child++
230233

231-
node, err := it.trie.resolveHash(node, nil, nil)
234+
node, err := it.trie.resolveHash(hash, nil, nil)
232235
if err != nil {
233236
panic(err)
234237
}
235-
it.stack = append(it.stack, &nodeIteratorState{node: node, child: -1})
238+
it.stack = append(it.stack, &nodeIteratorState{hash: common.BytesToHash(hash), node: node, child: -1})
236239
} else {
237240
break
238241
}
@@ -246,14 +249,16 @@ func (it *NodeIterator) step() {
246249
// The method returns whether there are any more data left for inspection.
247250
func (it *NodeIterator) retrieve() bool {
248251
// Clear out any previously set values
249-
it.Node, it.Leaf, it.LeafBlob = nil, false, nil
252+
it.Hash, it.Node, it.Leaf, it.LeafBlob = common.Hash{}, nil, false, nil
250253

251254
// If the iteration's done, return no available data
252255
if it.trie == nil {
253256
return false
254257
}
255258
// Otherwise retrieve the current node and resolve leaf accessors
256-
it.Node = it.stack[len(it.stack)-1].node
259+
state := it.stack[len(it.stack)-1]
260+
261+
it.Hash, it.Node = state.hash, state.node
257262
if value, ok := it.Node.(valueNode); ok {
258263
it.Leaf, it.LeafBlob = true, []byte(value)
259264
}

‎trie/iterator_test.go‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616

1717
package trie
1818

19-
import "testing"
19+
import (
20+
"testing"
21+
22+
"github.com/ethereum/go-ethereum/common"
23+
"github.com/ethereum/go-ethereum/ethdb"
24+
)
2025

2126
func TestIterator(t *testing.T) {
2227
trie := newEmpty()
@@ -47,3 +52,28 @@ func TestIterator(t *testing.T) {
4752
}
4853
}
4954
}
55+
56+
// Tests that the node iterator indeed walks over the entire database contents.
57+
func TestNodeIteratorCoverage(t *testing.T) {
58+
// Create some arbitrary test trie to iterate
59+
db, trie, _ := makeTestTrie()
60+
61+
// Gather all the node hashes found by the iterator
62+
hashes := make(map[common.Hash]struct{})
63+
for it := NewNodeIterator(trie); it.Next(); {
64+
if it.Hash != (common.Hash{}) {
65+
hashes[it.Hash] = struct{}{}
66+
}
67+
}
68+
// Cross check the hashes and the database itself
69+
for hash, _ := range hashes {
70+
if _, err := db.Get(hash.Bytes()); err != nil {
71+
t.Errorf("failed to retrieve reported node %x: %v", hash, err)
72+
}
73+
}
74+
for _, key := range db.(*ethdb.MemDatabase).Keys() {
75+
if _, ok := hashes[common.BytesToHash(key)]; !ok {
76+
t.Errorf("state entry not reported %x", key)
77+
}
78+
}
79+
}

‎trie/sync_test.go‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,7 @@ func checkTrieConsistency(db Database, root common.Hash) (failure error) {
9696
if err != nil {
9797
return
9898
}
99-
it := NewNodeIterator(trie)
100-
for it.Next() {
99+
for it := NewNodeIterator(trie); it.Next(); {
101100
}
102101
return nil
103102
}

0 commit comments

Comments
 (0)