@@ -19,10 +19,13 @@ package trie
1919import (
2020 "bytes"
2121 "container/heap"
22+ "errors"
2223
2324 "github.com/ethereum/go-ethereum/common"
2425)
2526
27+ var iteratorEnd = errors .New ("end of iteration" )
28+
2629// Iterator is a key-value trie iterator that traverses a Trie.
2730type Iterator struct {
2831 nodeIt NodeIterator
@@ -79,25 +82,24 @@ type nodeIteratorState struct {
7982 hash common.Hash // Hash of the node being iterated (nil if not standalone)
8083 node node // Trie node being iterated
8184 parent common.Hash // Hash of the first full ancestor node (nil if current is the root)
82- child int // Child to be processed next
85+ index int // Child to be processed next
8386 pathlen int // Length of the path to this node
8487}
8588
8689type nodeIterator struct {
8790 trie * Trie // Trie being iterated
8891 stack []* nodeIteratorState // Hierarchy of trie nodes persisting the iteration state
89-
90- err error // Failure set in case of an internal error in the iterator
91-
92- path []byte // Path to the current node
92+ err error // Failure set in case of an internal error in the iterator
93+ path []byte // Path to the current node
9394}
9495
95- // newNodeIterator creates an post-order trie iterator.
96- func newNodeIterator (trie * Trie ) NodeIterator {
96+ func newNodeIterator (trie * Trie , start []byte ) NodeIterator {
9797 if trie .Hash () == emptyState {
9898 return new (nodeIterator )
9999 }
100- return & nodeIterator {trie : trie }
100+ it := & nodeIterator {trie : trie }
101+ it .seek (start )
102+ return it
101103}
102104
103105// Hash returns the hash of the current node
@@ -147,6 +149,9 @@ func (it *nodeIterator) Path() []byte {
147149
148150// Error returns the error set in case of an internal error in the iterator
149151func (it * nodeIterator ) Error () error {
152+ if it .err == iteratorEnd {
153+ return nil
154+ }
150155 return it .err
151156}
152157
@@ -155,111 +160,131 @@ func (it *nodeIterator) Error() error {
155160// sets the Error field to the encountered failure. If `descend` is false,
156161// skips iterating over any subnodes of the current node.
157162func (it * nodeIterator ) Next (descend bool ) bool {
158- // If the iterator failed previously, don't do anything
159163 if it .err != nil {
160164 return false
161165 }
162166 // Otherwise step forward with the iterator and report any errors
163- if err := it .step (descend ); err != nil {
167+ state , parentIndex , path , err := it .peek (descend )
168+ if err != nil {
164169 it .err = err
165170 return false
166171 }
167- return it .trie != nil
172+ it .push (state , parentIndex , path )
173+ return true
168174}
169175
170- // step moves the iterator to the next node of the trie.
171- func (it * nodeIterator ) step (descend bool ) error {
172- if it .trie == nil {
173- // Abort if we reached the end of the iteration
174- return nil
176+ func (it * nodeIterator ) seek (prefix []byte ) {
177+ // The path we're looking for is the hex encoded key without terminator.
178+ key := keybytesToHex (prefix )
179+ key = key [:len (key )- 1 ]
180+ // Move forward until we're just before the closest match to key.
181+ for {
182+ state , parentIndex , path , err := it .peek (bytes .HasPrefix (key , it .path ))
183+ if err != nil || bytes .Compare (path , key ) >= 0 {
184+ it .err = err
185+ return
186+ }
187+ it .push (state , parentIndex , path )
175188 }
189+ }
190+
191+ // peek creates the next state of the iterator.
192+ func (it * nodeIterator ) peek (descend bool ) (* nodeIteratorState , * int , []byte , error ) {
176193 if len (it .stack ) == 0 {
177194 // Initialize the iterator if we've just started.
178195 root := it .trie .Hash ()
179- state := & nodeIteratorState {node : it .trie .root , child : - 1 }
196+ state := & nodeIteratorState {node : it .trie .root , index : - 1 }
180197 if root != emptyRoot {
181198 state .hash = root
182199 }
183- it .stack = append (it .stack , state )
184- return nil
200+ return state , nil , nil , nil
185201 }
186-
187202 if ! descend {
188203 // If we're skipping children, pop the current node first
189- it .path = it .path [:it .stack [len (it .stack )- 1 ].pathlen ]
190- it .stack = it .stack [:len (it .stack )- 1 ]
204+ it .pop ()
191205 }
192206
193207 // Continue iteration to the next child
194- outer:
195208 for {
196209 if len (it .stack ) == 0 {
197- it .trie = nil
198- return nil
210+ return nil , nil , nil , iteratorEnd
199211 }
200212 parent := it .stack [len (it .stack )- 1 ]
201213 ancestor := parent .hash
202214 if (ancestor == common.Hash {}) {
203215 ancestor = parent .parent
204216 }
205217 if node , ok := parent .node .(* fullNode ); ok {
206- // Full node, iterate over children
207- for parent .child ++ ; parent . child < len (node .Children ); parent . child ++ {
208- child := node .Children [parent . child ]
218+ // Full node, move to the first non-nil child.
219+ for i := parent .index + 1 ; i < len (node .Children ); i ++ {
220+ child := node .Children [i ]
209221 if child != nil {
210222 hash , _ := child .cache ()
211- it . stack = append ( it . stack , & nodeIteratorState {
223+ state := & nodeIteratorState {
212224 hash : common .BytesToHash (hash ),
213225 node : child ,
214226 parent : ancestor ,
215- child : - 1 ,
227+ index : - 1 ,
216228 pathlen : len (it .path ),
217- })
218- it .path = append (it .path , byte (parent .child ))
219- break outer
229+ }
230+ path := append (it .path , byte (i ))
231+ parent .index = i - 1
232+ return state , & parent .index , path , nil
220233 }
221234 }
222235 } else if node , ok := parent .node .(* shortNode ); ok {
223236 // Short node, return the pointer singleton child
224- if parent .child < 0 {
225- parent .child ++
237+ if parent .index < 0 {
226238 hash , _ := node .Val .cache ()
227- it . stack = append ( it . stack , & nodeIteratorState {
239+ state := & nodeIteratorState {
228240 hash : common .BytesToHash (hash ),
229241 node : node .Val ,
230242 parent : ancestor ,
231- child : - 1 ,
243+ index : - 1 ,
232244 pathlen : len (it .path ),
233- })
245+ }
246+ var path []byte
234247 if hasTerm (node .Key ) {
235- it . path = append (it .path , node .Key [:len (node .Key )- 1 ]... )
248+ path = append (it .path , node .Key [:len (node .Key )- 1 ]... )
236249 } else {
237- it . path = append (it .path , node .Key ... )
250+ path = append (it .path , node .Key ... )
238251 }
239- break
252+ return state , & parent . index , path , nil
240253 }
241254 } else if hash , ok := parent .node .(hashNode ); ok {
242255 // Hash node, resolve the hash child from the database
243- if parent .child < 0 {
244- parent .child ++
256+ if parent .index < 0 {
245257 node , err := it .trie .resolveHash (hash , nil , nil )
246258 if err != nil {
247- return err
259+ return it . stack [ len ( it . stack ) - 1 ], & parent . index , it . path , err
248260 }
249- it . stack = append ( it . stack , & nodeIteratorState {
261+ state := & nodeIteratorState {
250262 hash : common .BytesToHash (hash ),
251263 node : node ,
252264 parent : ancestor ,
253- child : - 1 ,
265+ index : - 1 ,
254266 pathlen : len (it .path ),
255- })
256- break
267+ }
268+ return state , & parent . index , it . path , nil
257269 }
258270 }
259- it . path = it . path [: parent . pathlen ]
260- it .stack = it . stack [: len ( it . stack ) - 1 ]
271+ // No more child nodes, move back up.
272+ it .pop ()
261273 }
262- return nil
274+ }
275+
276+ func (it * nodeIterator ) push (state * nodeIteratorState , parentIndex * int , path []byte ) {
277+ it .path = path
278+ it .stack = append (it .stack , state )
279+ if parentIndex != nil {
280+ * parentIndex += 1
281+ }
282+ }
283+
284+ func (it * nodeIterator ) pop () {
285+ parent := it .stack [len (it .stack )- 1 ]
286+ it .path = it .path [:parent .pathlen ]
287+ it .stack = it .stack [:len (it .stack )- 1 ]
263288}
264289
265290func compareNodes (a , b NodeIterator ) int {
0 commit comments