@@ -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.
5558func (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.
0 commit comments