Skip to content

Commit b8d59d9

Browse files
committed
core/state, trie: switch iterator panics to error fields
1 parent 151c7be commit b8d59d9

4 files changed

Lines changed: 62 additions & 45 deletions

File tree

‎core/state/iterator.go‎

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ type NodeIterator struct {
4141
Hash common.Hash // Hash of the current entry being iterated (nil if not standalone)
4242
Entry interface{} // Current state entry being iterated (internal representation)
4343
Parent common.Hash // Hash of the first full ancestor node (nil if current is the root)
44+
45+
Error error // Failure set in case of an internal error in the iterator
4446
}
4547

4648
// NewNodeIterator creates an post-order state node iterator.
@@ -51,17 +53,26 @@ func NewNodeIterator(state *StateDB) *NodeIterator {
5153
}
5254

5355
// Next moves the iterator to the next node, returning whether there are any
54-
// further nodes.
56+
// further nodes. In case of an internal error this method returns false and
57+
// sets the Error field to the encountered failure.
5558
func (it *NodeIterator) Next() bool {
56-
it.step()
59+
// If the iterator failed previously, don't do anything
60+
if it.Error != nil {
61+
return false
62+
}
63+
// Otherwise step forward with the iterator and report any errors
64+
if err := it.step(); err != nil {
65+
it.Error = err
66+
return false
67+
}
5768
return it.retrieve()
5869
}
5970

6071
// step moves the iterator to the next entry of the state trie.
61-
func (it *NodeIterator) step() {
72+
func (it *NodeIterator) step() error {
6273
// Abort if we reached the end of the iteration
6374
if it.state == nil {
64-
return
75+
return nil
6576
}
6677
// Initialize the iterator if we've just started
6778
if it.stateIt == nil {
@@ -70,23 +81,29 @@ func (it *NodeIterator) step() {
7081
// If we had data nodes previously, we surely have at least state nodes
7182
if it.dataIt != nil {
7283
if cont := it.dataIt.Next(); !cont {
84+
if it.dataIt.Error != nil {
85+
return it.dataIt.Error
86+
}
7387
it.dataIt = nil
7488
}
75-
return
89+
return nil
7690
}
7791
// If we had source code previously, discard that
7892
if it.code != nil {
7993
it.code = nil
80-
return
94+
return nil
8195
}
8296
// Step to the next state trie node, terminating if we're out of nodes
8397
if cont := it.stateIt.Next(); !cont {
98+
if it.stateIt.Error != nil {
99+
return it.stateIt.Error
100+
}
84101
it.state, it.stateIt = nil, nil
85-
return
102+
return nil
86103
}
87104
// If the state trie node is an internal entry, leave as is
88105
if !it.stateIt.Leaf {
89-
return
106+
return nil
90107
}
91108
// Otherwise we've reached an account node, initiate data iteration
92109
var account struct {
@@ -95,13 +112,12 @@ func (it *NodeIterator) step() {
95112
Root common.Hash
96113
CodeHash []byte
97114
}
98-
err := rlp.Decode(bytes.NewReader(it.stateIt.LeafBlob), &account)
99-
if err != nil {
100-
panic(err)
115+
if err := rlp.Decode(bytes.NewReader(it.stateIt.LeafBlob), &account); err != nil {
116+
return err
101117
}
102118
dataTrie, err := trie.New(account.Root, it.state.db)
103119
if err != nil {
104-
panic(err)
120+
return err
105121
}
106122
it.dataIt = trie.NewNodeIterator(dataTrie)
107123
if !it.dataIt.Next() {
@@ -111,10 +127,11 @@ func (it *NodeIterator) step() {
111127
it.codeHash = common.BytesToHash(account.CodeHash)
112128
it.code, err = it.state.db.Get(account.CodeHash)
113129
if err != nil {
114-
panic(fmt.Sprintf("code %x: %v", account.CodeHash, err))
130+
return fmt.Errorf("code %x: %v", account.CodeHash, err)
115131
}
116132
}
117133
it.accountHash = it.stateIt.Parent
134+
return nil
118135
}
119136

120137
// retrieve pulls and caches the current state entry the iterator is traversing.

‎core/state/sync_test.go‎

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package state
1818

1919
import (
2020
"bytes"
21-
"fmt"
2221
"math/big"
2322
"testing"
2423

@@ -97,28 +96,23 @@ func checkStateAccounts(t *testing.T, db ethdb.Database, root common.Hash, accou
9796
}
9897
}
9998

100-
// checkStateConsistency checks that all nodes in a state trie and indeed present.
101-
func checkStateConsistency(db ethdb.Database, root common.Hash) (failure error) {
102-
// Capture any panics by the iterator
103-
defer func() {
104-
if r := recover(); r != nil {
105-
failure = fmt.Errorf("%v", r)
106-
}
107-
}()
99+
// checkStateConsistency checks that all nodes in a state trie are indeed present.
100+
func checkStateConsistency(db ethdb.Database, root common.Hash) error {
108101
// Remove any potentially cached data from the test state creation or previous checks
109102
trie.ClearGlobalCache()
110103

111104
// Create and iterate a state trie rooted in a sub-node
112105
if _, err := db.Get(root.Bytes()); err != nil {
113-
return
106+
return nil // Consider a non existent state consistent
114107
}
115108
state, err := New(root, db)
116109
if err != nil {
117-
return
110+
return err
118111
}
119-
for it := NewNodeIterator(state); it.Next(); {
112+
it := NewNodeIterator(state)
113+
for it.Next() {
120114
}
121-
return nil
115+
return it.Error
122116
}
123117

124118
// Tests that an empty state is not scheduled for syncing.

‎trie/iterator.go‎

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ type NodeIterator struct {
169169
Parent common.Hash // Hash of the first full ancestor node (nil if current is the root)
170170
Leaf bool // Flag whether the current node is a value (data) node
171171
LeafBlob []byte // Data blob contained within a leaf (otherwise nil)
172+
173+
Error error // Failure set in case of an internal error in the iterator
172174
}
173175

174176
// NewNodeIterator creates an post-order trie iterator.
@@ -180,29 +182,38 @@ func NewNodeIterator(trie *Trie) *NodeIterator {
180182
}
181183

182184
// Next moves the iterator to the next node, returning whether there are any
183-
// further nodes.
185+
// further nodes. In case of an internal error this method returns false and
186+
// sets the Error field to the encountered failure.
184187
func (it *NodeIterator) Next() bool {
185-
it.step()
188+
// If the iterator failed previously, don't do anything
189+
if it.Error != nil {
190+
return false
191+
}
192+
// Otherwise step forward with the iterator and report any errors
193+
if err := it.step(); err != nil {
194+
it.Error = err
195+
return false
196+
}
186197
return it.retrieve()
187198
}
188199

189200
// step moves the iterator to the next node of the trie.
190-
func (it *NodeIterator) step() {
201+
func (it *NodeIterator) step() error {
191202
// Abort if we reached the end of the iteration
192203
if it.trie == nil {
193-
return
204+
return nil
194205
}
195206
// Initialize the iterator if we've just started, or pop off the old node otherwise
196207
if len(it.stack) == 0 {
197208
it.stack = append(it.stack, &nodeIteratorState{node: it.trie.root, child: -1})
198209
if it.stack[0].node == nil {
199-
panic(fmt.Sprintf("root node missing: %x", it.trie.Root()))
210+
return fmt.Errorf("root node missing: %x", it.trie.Root())
200211
}
201212
} else {
202213
it.stack = it.stack[:len(it.stack)-1]
203214
if len(it.stack) == 0 {
204215
it.trie = nil
205-
return
216+
return nil
206217
}
207218
}
208219
// Continue iteration to the next child
@@ -239,13 +250,14 @@ func (it *NodeIterator) step() {
239250

240251
node, err := it.trie.resolveHash(hash, nil, nil)
241252
if err != nil {
242-
panic(err)
253+
return err
243254
}
244255
it.stack = append(it.stack, &nodeIteratorState{hash: common.BytesToHash(hash), node: node, parent: ancestor, child: -1})
245256
} else {
246257
break
247258
}
248259
}
260+
return nil
249261
}
250262

251263
// retrieve pulls and caches the current trie node the iterator is traversing.

‎trie/sync_test.go‎

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package trie
1818

1919
import (
2020
"bytes"
21-
"fmt"
2221
"testing"
2322

2423
"github.com/ethereum/go-ethereum/common"
@@ -80,25 +79,20 @@ func checkTrieContents(t *testing.T, db Database, root []byte, content map[strin
8079
}
8180
}
8281

83-
// checkTrieConsistency checks that all nodes in a trie and indeed present.
84-
func checkTrieConsistency(db Database, root common.Hash) (failure error) {
85-
// Capture any panics by the iterator
86-
defer func() {
87-
if r := recover(); r != nil {
88-
failure = fmt.Errorf("%v", r)
89-
}
90-
}()
82+
// checkTrieConsistency checks that all nodes in a trie are indeed present.
83+
func checkTrieConsistency(db Database, root common.Hash) error {
9184
// Remove any potentially cached data from the test trie creation or previous checks
9285
globalCache.Clear()
9386

9487
// Create and iterate a trie rooted in a subnode
9588
trie, err := New(root, db)
9689
if err != nil {
97-
return
90+
return nil // // Consider a non existent state consistent
9891
}
99-
for it := NewNodeIterator(trie); it.Next(); {
92+
it := NewNodeIterator(trie)
93+
for it.Next() {
10094
}
101-
return nil
95+
return it.Error
10296
}
10397

10498
// Tests that an empty trie is not scheduled for syncing.

0 commit comments

Comments
 (0)