github.1485827954.workers.dev/ethereum/go-ethereum@v1.14.3/trie/iterator.go (about) 1 // Copyright 2014 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 trie 18 19 import ( 20 "bytes" 21 "container/heap" 22 "errors" 23 24 "github.com/ethereum/go-ethereum/common" 25 "github.com/ethereum/go-ethereum/core/types" 26 ) 27 28 // NodeResolver is used for looking up trie nodes before reaching into the real 29 // persistent layer. This is not mandatory, rather is an optimization for cases 30 // where trie nodes can be recovered from some external mechanism without reading 31 // from disk. In those cases, this resolver allows short circuiting accesses and 32 // returning them from memory. 33 type NodeResolver func(owner common.Hash, path []byte, hash common.Hash) []byte 34 35 // Iterator is a key-value trie iterator that traverses a Trie. 36 type Iterator struct { 37 nodeIt NodeIterator 38 39 Key []byte // Current data key on which the iterator is positioned on 40 Value []byte // Current data value on which the iterator is positioned on 41 Err error 42 } 43 44 // NewIterator creates a new key-value iterator from a node iterator. 45 // Note that the value returned by the iterator is raw. If the content is encoded 46 // (e.g. storage value is RLP-encoded), it's caller's duty to decode it. 47 func NewIterator(it NodeIterator) *Iterator { 48 return &Iterator{ 49 nodeIt: it, 50 } 51 } 52 53 // Next moves the iterator forward one key-value entry. 54 func (it *Iterator) Next() bool { 55 for it.nodeIt.Next(true) { 56 if it.nodeIt.Leaf() { 57 it.Key = it.nodeIt.LeafKey() 58 it.Value = it.nodeIt.LeafBlob() 59 return true 60 } 61 } 62 it.Key = nil 63 it.Value = nil 64 it.Err = it.nodeIt.Error() 65 return false 66 } 67 68 // Prove generates the Merkle proof for the leaf node the iterator is currently 69 // positioned on. 70 func (it *Iterator) Prove() [][]byte { 71 return it.nodeIt.LeafProof() 72 } 73 74 // NodeIterator is an iterator to traverse the trie pre-order. 75 type NodeIterator interface { 76 // Next moves the iterator to the next node. If the parameter is false, any child 77 // nodes will be skipped. 78 Next(bool) bool 79 80 // Error returns the error status of the iterator. 81 Error() error 82 83 // Hash returns the hash of the current node. 84 Hash() common.Hash 85 86 // Parent returns the hash of the parent of the current node. The hash may be the one 87 // grandparent if the immediate parent is an internal node with no hash. 88 Parent() common.Hash 89 90 // Path returns the hex-encoded path to the current node. 91 // Callers must not retain references to the return value after calling Next. 92 // For leaf nodes, the last element of the path is the 'terminator symbol' 0x10. 93 Path() []byte 94 95 // NodeBlob returns the rlp-encoded value of the current iterated node. 96 // If the node is an embedded node in its parent, nil is returned then. 97 NodeBlob() []byte 98 99 // Leaf returns true iff the current node is a leaf node. 100 Leaf() bool 101 102 // LeafKey returns the key of the leaf. The method panics if the iterator is not 103 // positioned at a leaf. Callers must not retain references to the value after 104 // calling Next. 105 LeafKey() []byte 106 107 // LeafBlob returns the content of the leaf. The method panics if the iterator 108 // is not positioned at a leaf. Callers must not retain references to the value 109 // after calling Next. 110 LeafBlob() []byte 111 112 // LeafProof returns the Merkle proof of the leaf. The method panics if the 113 // iterator is not positioned at a leaf. Callers must not retain references 114 // to the value after calling Next. 115 LeafProof() [][]byte 116 117 // AddResolver sets a node resolver to use for looking up trie nodes before 118 // reaching into the real persistent layer. 119 // 120 // This is not required for normal operation, rather is an optimization for 121 // cases where trie nodes can be recovered from some external mechanism without 122 // reading from disk. In those cases, this resolver allows short circuiting 123 // accesses and returning them from memory. 124 // 125 // Before adding a similar mechanism to any other place in Geth, consider 126 // making trie.Database an interface and wrapping at that level. It's a huge 127 // refactor, but it could be worth it if another occurrence arises. 128 AddResolver(NodeResolver) 129 } 130 131 // nodeIteratorState represents the iteration state at one particular node of the 132 // trie, which can be resumed at a later invocation. 133 type nodeIteratorState struct { 134 hash common.Hash // Hash of the node being iterated (nil if not standalone) 135 node node // Trie node being iterated 136 parent common.Hash // Hash of the first full ancestor node (nil if current is the root) 137 index int // Child to be processed next 138 pathlen int // Length of the path to this node 139 } 140 141 type nodeIterator struct { 142 trie *Trie // Trie being iterated 143 stack []*nodeIteratorState // Hierarchy of trie nodes persisting the iteration state 144 path []byte // Path to the current node 145 err error // Failure set in case of an internal error in the iterator 146 147 resolver NodeResolver // optional node resolver for avoiding disk hits 148 pool []*nodeIteratorState // local pool for iteratorstates 149 } 150 151 // errIteratorEnd is stored in nodeIterator.err when iteration is done. 152 var errIteratorEnd = errors.New("end of iteration") 153 154 // seekError is stored in nodeIterator.err if the initial seek has failed. 155 type seekError struct { 156 key []byte 157 err error 158 } 159 160 func (e seekError) Error() string { 161 return "seek error: " + e.err.Error() 162 } 163 164 func newNodeIterator(trie *Trie, start []byte) NodeIterator { 165 if trie.Hash() == types.EmptyRootHash { 166 return &nodeIterator{ 167 trie: trie, 168 err: errIteratorEnd, 169 } 170 } 171 it := &nodeIterator{trie: trie} 172 it.err = it.seek(start) 173 return it 174 } 175 176 func (it *nodeIterator) putInPool(item *nodeIteratorState) { 177 if len(it.pool) < 40 { 178 item.node = nil 179 it.pool = append(it.pool, item) 180 } 181 } 182 183 func (it *nodeIterator) getFromPool() *nodeIteratorState { 184 idx := len(it.pool) - 1 185 if idx < 0 { 186 return new(nodeIteratorState) 187 } 188 el := it.pool[idx] 189 it.pool[idx] = nil 190 it.pool = it.pool[:idx] 191 return el 192 } 193 194 func (it *nodeIterator) AddResolver(resolver NodeResolver) { 195 it.resolver = resolver 196 } 197 198 func (it *nodeIterator) Hash() common.Hash { 199 if len(it.stack) == 0 { 200 return common.Hash{} 201 } 202 return it.stack[len(it.stack)-1].hash 203 } 204 205 func (it *nodeIterator) Parent() common.Hash { 206 if len(it.stack) == 0 { 207 return common.Hash{} 208 } 209 return it.stack[len(it.stack)-1].parent 210 } 211 212 func (it *nodeIterator) Leaf() bool { 213 return hasTerm(it.path) 214 } 215 216 func (it *nodeIterator) LeafKey() []byte { 217 if len(it.stack) > 0 { 218 if _, ok := it.stack[len(it.stack)-1].node.(valueNode); ok { 219 return hexToKeybytes(it.path) 220 } 221 } 222 panic("not at leaf") 223 } 224 225 func (it *nodeIterator) LeafBlob() []byte { 226 if len(it.stack) > 0 { 227 if node, ok := it.stack[len(it.stack)-1].node.(valueNode); ok { 228 return node 229 } 230 } 231 panic("not at leaf") 232 } 233 234 func (it *nodeIterator) LeafProof() [][]byte { 235 if len(it.stack) > 0 { 236 if _, ok := it.stack[len(it.stack)-1].node.(valueNode); ok { 237 hasher := newHasher(false) 238 defer returnHasherToPool(hasher) 239 proofs := make([][]byte, 0, len(it.stack)) 240 241 for i, item := range it.stack[:len(it.stack)-1] { 242 // Gather nodes that end up as hash nodes (or the root) 243 node, hashed := hasher.proofHash(item.node) 244 if _, ok := hashed.(hashNode); ok || i == 0 { 245 proofs = append(proofs, nodeToBytes(node)) 246 } 247 } 248 return proofs 249 } 250 } 251 panic("not at leaf") 252 } 253 254 func (it *nodeIterator) Path() []byte { 255 return it.path 256 } 257 258 func (it *nodeIterator) NodeBlob() []byte { 259 if it.Hash() == (common.Hash{}) { 260 return nil // skip the non-standalone node 261 } 262 blob, err := it.resolveBlob(it.Hash().Bytes(), it.Path()) 263 if err != nil { 264 it.err = err 265 return nil 266 } 267 return blob 268 } 269 270 func (it *nodeIterator) Error() error { 271 if it.err == errIteratorEnd { 272 return nil 273 } 274 if seek, ok := it.err.(seekError); ok { 275 return seek.err 276 } 277 return it.err 278 } 279 280 // Next moves the iterator to the next node, returning whether there are any 281 // further nodes. In case of an internal error this method returns false and 282 // sets the Error field to the encountered failure. If `descend` is false, 283 // skips iterating over any subnodes of the current node. 284 func (it *nodeIterator) Next(descend bool) bool { 285 if it.err == errIteratorEnd { 286 return false 287 } 288 if seek, ok := it.err.(seekError); ok { 289 if it.err = it.seek(seek.key); it.err != nil { 290 return false 291 } 292 } 293 // Otherwise step forward with the iterator and report any errors. 294 state, parentIndex, path, err := it.peek(descend) 295 it.err = err 296 if it.err != nil { 297 return false 298 } 299 it.push(state, parentIndex, path) 300 return true 301 } 302 303 func (it *nodeIterator) seek(prefix []byte) error { 304 // The path we're looking for is the hex encoded key without terminator. 305 key := keybytesToHex(prefix) 306 key = key[:len(key)-1] 307 // Move forward until we're just before the closest match to key. 308 for { 309 state, parentIndex, path, err := it.peekSeek(key) 310 if err == errIteratorEnd { 311 return errIteratorEnd 312 } else if err != nil { 313 return seekError{prefix, err} 314 } else if bytes.Compare(path, key) >= 0 { 315 return nil 316 } 317 it.push(state, parentIndex, path) 318 } 319 } 320 321 // init initializes the iterator. 322 func (it *nodeIterator) init() (*nodeIteratorState, error) { 323 root := it.trie.Hash() 324 state := &nodeIteratorState{node: it.trie.root, index: -1} 325 if root != types.EmptyRootHash { 326 state.hash = root 327 } 328 return state, state.resolve(it, nil) 329 } 330 331 // peek creates the next state of the iterator. 332 func (it *nodeIterator) peek(descend bool) (*nodeIteratorState, *int, []byte, error) { 333 // Initialize the iterator if we've just started. 334 if len(it.stack) == 0 { 335 state, err := it.init() 336 return state, nil, nil, err 337 } 338 if !descend { 339 // If we're skipping children, pop the current node first 340 it.pop() 341 } 342 343 // Continue iteration to the next child 344 for len(it.stack) > 0 { 345 parent := it.stack[len(it.stack)-1] 346 ancestor := parent.hash 347 if (ancestor == common.Hash{}) { 348 ancestor = parent.parent 349 } 350 state, path, ok := it.nextChild(parent, ancestor) 351 if ok { 352 if err := state.resolve(it, path); err != nil { 353 return parent, &parent.index, path, err 354 } 355 return state, &parent.index, path, nil 356 } 357 // No more child nodes, move back up. 358 it.pop() 359 } 360 return nil, nil, nil, errIteratorEnd 361 } 362 363 // peekSeek is like peek, but it also tries to skip resolving hashes by skipping 364 // over the siblings that do not lead towards the desired seek position. 365 func (it *nodeIterator) peekSeek(seekKey []byte) (*nodeIteratorState, *int, []byte, error) { 366 // Initialize the iterator if we've just started. 367 if len(it.stack) == 0 { 368 state, err := it.init() 369 return state, nil, nil, err 370 } 371 if !bytes.HasPrefix(seekKey, it.path) { 372 // If we're skipping children, pop the current node first 373 it.pop() 374 } 375 376 // Continue iteration to the next child 377 for len(it.stack) > 0 { 378 parent := it.stack[len(it.stack)-1] 379 ancestor := parent.hash 380 if (ancestor == common.Hash{}) { 381 ancestor = parent.parent 382 } 383 state, path, ok := it.nextChildAt(parent, ancestor, seekKey) 384 if ok { 385 if err := state.resolve(it, path); err != nil { 386 return parent, &parent.index, path, err 387 } 388 return state, &parent.index, path, nil 389 } 390 // No more child nodes, move back up. 391 it.pop() 392 } 393 return nil, nil, nil, errIteratorEnd 394 } 395 396 func (it *nodeIterator) resolveHash(hash hashNode, path []byte) (node, error) { 397 if it.resolver != nil { 398 if blob := it.resolver(it.trie.owner, path, common.BytesToHash(hash)); len(blob) > 0 { 399 if resolved, err := decodeNode(hash, blob); err == nil { 400 return resolved, nil 401 } 402 } 403 } 404 // Retrieve the specified node from the underlying node reader. 405 // it.trie.resolveAndTrack is not used since in that function the 406 // loaded blob will be tracked, while it's not required here since 407 // all loaded nodes won't be linked to trie at all and track nodes 408 // may lead to out-of-memory issue. 409 blob, err := it.trie.reader.node(path, common.BytesToHash(hash)) 410 if err != nil { 411 return nil, err 412 } 413 // The raw-blob format nodes are loaded either from the 414 // clean cache or the database, they are all in their own 415 // copy and safe to use unsafe decoder. 416 return mustDecodeNodeUnsafe(hash, blob), nil 417 } 418 419 func (it *nodeIterator) resolveBlob(hash hashNode, path []byte) ([]byte, error) { 420 if it.resolver != nil { 421 if blob := it.resolver(it.trie.owner, path, common.BytesToHash(hash)); len(blob) > 0 { 422 return blob, nil 423 } 424 } 425 // Retrieve the specified node from the underlying node reader. 426 // it.trie.resolveAndTrack is not used since in that function the 427 // loaded blob will be tracked, while it's not required here since 428 // all loaded nodes won't be linked to trie at all and track nodes 429 // may lead to out-of-memory issue. 430 return it.trie.reader.node(path, common.BytesToHash(hash)) 431 } 432 433 func (st *nodeIteratorState) resolve(it *nodeIterator, path []byte) error { 434 if hash, ok := st.node.(hashNode); ok { 435 resolved, err := it.resolveHash(hash, path) 436 if err != nil { 437 return err 438 } 439 st.node = resolved 440 st.hash = common.BytesToHash(hash) 441 } 442 return nil 443 } 444 445 func (it *nodeIterator) findChild(n *fullNode, index int, ancestor common.Hash) (node, *nodeIteratorState, []byte, int) { 446 var ( 447 path = it.path 448 child node 449 state *nodeIteratorState 450 childPath []byte 451 ) 452 for ; index < len(n.Children); index++ { 453 if n.Children[index] != nil { 454 child = n.Children[index] 455 hash, _ := child.cache() 456 state = it.getFromPool() 457 state.hash = common.BytesToHash(hash) 458 state.node = child 459 state.parent = ancestor 460 state.index = -1 461 state.pathlen = len(path) 462 childPath = append(childPath, path...) 463 childPath = append(childPath, byte(index)) 464 return child, state, childPath, index 465 } 466 } 467 return nil, nil, nil, 0 468 } 469 470 func (it *nodeIterator) nextChild(parent *nodeIteratorState, ancestor common.Hash) (*nodeIteratorState, []byte, bool) { 471 switch node := parent.node.(type) { 472 case *fullNode: 473 // Full node, move to the first non-nil child. 474 if child, state, path, index := it.findChild(node, parent.index+1, ancestor); child != nil { 475 parent.index = index - 1 476 return state, path, true 477 } 478 case *shortNode: 479 // Short node, return the pointer singleton child 480 if parent.index < 0 { 481 hash, _ := node.Val.cache() 482 state := it.getFromPool() 483 state.hash = common.BytesToHash(hash) 484 state.node = node.Val 485 state.parent = ancestor 486 state.index = -1 487 state.pathlen = len(it.path) 488 path := append(it.path, node.Key...) 489 return state, path, true 490 } 491 } 492 return parent, it.path, false 493 } 494 495 // nextChildAt is similar to nextChild, except that it targets a child as close to the 496 // target key as possible, thus skipping siblings. 497 func (it *nodeIterator) nextChildAt(parent *nodeIteratorState, ancestor common.Hash, key []byte) (*nodeIteratorState, []byte, bool) { 498 switch n := parent.node.(type) { 499 case *fullNode: 500 // Full node, move to the first non-nil child before the desired key position 501 child, state, path, index := it.findChild(n, parent.index+1, ancestor) 502 if child == nil { 503 // No more children in this fullnode 504 return parent, it.path, false 505 } 506 // If the child we found is already past the seek position, just return it. 507 if bytes.Compare(path, key) >= 0 { 508 parent.index = index - 1 509 return state, path, true 510 } 511 // The child is before the seek position. Try advancing 512 for { 513 nextChild, nextState, nextPath, nextIndex := it.findChild(n, index+1, ancestor) 514 // If we run out of children, or skipped past the target, return the 515 // previous one 516 if nextChild == nil || bytes.Compare(nextPath, key) >= 0 { 517 parent.index = index - 1 518 return state, path, true 519 } 520 // We found a better child closer to the target 521 state, path, index = nextState, nextPath, nextIndex 522 } 523 case *shortNode: 524 // Short node, return the pointer singleton child 525 if parent.index < 0 { 526 hash, _ := n.Val.cache() 527 state := it.getFromPool() 528 state.hash = common.BytesToHash(hash) 529 state.node = n.Val 530 state.parent = ancestor 531 state.index = -1 532 state.pathlen = len(it.path) 533 path := append(it.path, n.Key...) 534 return state, path, true 535 } 536 } 537 return parent, it.path, false 538 } 539 540 func (it *nodeIterator) push(state *nodeIteratorState, parentIndex *int, path []byte) { 541 it.path = path 542 it.stack = append(it.stack, state) 543 if parentIndex != nil { 544 *parentIndex++ 545 } 546 } 547 548 func (it *nodeIterator) pop() { 549 last := it.stack[len(it.stack)-1] 550 it.path = it.path[:last.pathlen] 551 it.stack[len(it.stack)-1] = nil 552 it.stack = it.stack[:len(it.stack)-1] 553 // last is now unused 554 it.putInPool(last) 555 } 556 557 func compareNodes(a, b NodeIterator) int { 558 if cmp := bytes.Compare(a.Path(), b.Path()); cmp != 0 { 559 return cmp 560 } 561 if a.Leaf() && !b.Leaf() { 562 return -1 563 } else if b.Leaf() && !a.Leaf() { 564 return 1 565 } 566 if cmp := bytes.Compare(a.Hash().Bytes(), b.Hash().Bytes()); cmp != 0 { 567 return cmp 568 } 569 if a.Leaf() && b.Leaf() { 570 return bytes.Compare(a.LeafBlob(), b.LeafBlob()) 571 } 572 return 0 573 } 574 575 type differenceIterator struct { 576 a, b NodeIterator // Nodes returned are those in b - a. 577 eof bool // Indicates a has run out of elements 578 count int // Number of nodes scanned on either trie 579 } 580 581 // NewDifferenceIterator constructs a NodeIterator that iterates over elements in b that 582 // are not in a. Returns the iterator, and a pointer to an integer recording the number 583 // of nodes seen. 584 func NewDifferenceIterator(a, b NodeIterator) (NodeIterator, *int) { 585 a.Next(true) 586 it := &differenceIterator{ 587 a: a, 588 b: b, 589 } 590 return it, &it.count 591 } 592 593 func (it *differenceIterator) Hash() common.Hash { 594 return it.b.Hash() 595 } 596 597 func (it *differenceIterator) Parent() common.Hash { 598 return it.b.Parent() 599 } 600 601 func (it *differenceIterator) Leaf() bool { 602 return it.b.Leaf() 603 } 604 605 func (it *differenceIterator) LeafKey() []byte { 606 return it.b.LeafKey() 607 } 608 609 func (it *differenceIterator) LeafBlob() []byte { 610 return it.b.LeafBlob() 611 } 612 613 func (it *differenceIterator) LeafProof() [][]byte { 614 return it.b.LeafProof() 615 } 616 617 func (it *differenceIterator) Path() []byte { 618 return it.b.Path() 619 } 620 621 func (it *differenceIterator) NodeBlob() []byte { 622 return it.b.NodeBlob() 623 } 624 625 func (it *differenceIterator) AddResolver(resolver NodeResolver) { 626 panic("not implemented") 627 } 628 629 func (it *differenceIterator) Next(bool) bool { 630 // Invariants: 631 // - We always advance at least one element in b. 632 // - At the start of this function, a's path is lexically greater than b's. 633 if !it.b.Next(true) { 634 return false 635 } 636 it.count++ 637 638 if it.eof { 639 // a has reached eof, so we just return all elements from b 640 return true 641 } 642 643 for { 644 switch compareNodes(it.a, it.b) { 645 case -1: 646 // b jumped past a; advance a 647 if !it.a.Next(true) { 648 it.eof = true 649 return true 650 } 651 it.count++ 652 case 1: 653 // b is before a 654 return true 655 case 0: 656 // a and b are identical; skip this whole subtree if the nodes have hashes 657 hasHash := it.a.Hash() == common.Hash{} 658 if !it.b.Next(hasHash) { 659 return false 660 } 661 it.count++ 662 if !it.a.Next(hasHash) { 663 it.eof = true 664 return true 665 } 666 it.count++ 667 } 668 } 669 } 670 671 func (it *differenceIterator) Error() error { 672 if err := it.a.Error(); err != nil { 673 return err 674 } 675 return it.b.Error() 676 } 677 678 type nodeIteratorHeap []NodeIterator 679 680 func (h nodeIteratorHeap) Len() int { return len(h) } 681 func (h nodeIteratorHeap) Less(i, j int) bool { return compareNodes(h[i], h[j]) < 0 } 682 func (h nodeIteratorHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } 683 func (h *nodeIteratorHeap) Push(x interface{}) { *h = append(*h, x.(NodeIterator)) } 684 func (h *nodeIteratorHeap) Pop() interface{} { 685 n := len(*h) 686 x := (*h)[n-1] 687 *h = (*h)[0 : n-1] 688 return x 689 } 690 691 type unionIterator struct { 692 items *nodeIteratorHeap // Nodes returned are the union of the ones in these iterators 693 count int // Number of nodes scanned across all tries 694 } 695 696 // NewUnionIterator constructs a NodeIterator that iterates over elements in the union 697 // of the provided NodeIterators. Returns the iterator, and a pointer to an integer 698 // recording the number of nodes visited. 699 func NewUnionIterator(iters []NodeIterator) (NodeIterator, *int) { 700 h := make(nodeIteratorHeap, len(iters)) 701 copy(h, iters) 702 heap.Init(&h) 703 704 ui := &unionIterator{items: &h} 705 return ui, &ui.count 706 } 707 708 func (it *unionIterator) Hash() common.Hash { 709 return (*it.items)[0].Hash() 710 } 711 712 func (it *unionIterator) Parent() common.Hash { 713 return (*it.items)[0].Parent() 714 } 715 716 func (it *unionIterator) Leaf() bool { 717 return (*it.items)[0].Leaf() 718 } 719 720 func (it *unionIterator) LeafKey() []byte { 721 return (*it.items)[0].LeafKey() 722 } 723 724 func (it *unionIterator) LeafBlob() []byte { 725 return (*it.items)[0].LeafBlob() 726 } 727 728 func (it *unionIterator) LeafProof() [][]byte { 729 return (*it.items)[0].LeafProof() 730 } 731 732 func (it *unionIterator) Path() []byte { 733 return (*it.items)[0].Path() 734 } 735 736 func (it *unionIterator) NodeBlob() []byte { 737 return (*it.items)[0].NodeBlob() 738 } 739 740 func (it *unionIterator) AddResolver(resolver NodeResolver) { 741 panic("not implemented") 742 } 743 744 // Next returns the next node in the union of tries being iterated over. 745 // 746 // It does this by maintaining a heap of iterators, sorted by the iteration 747 // order of their next elements, with one entry for each source trie. Each 748 // time Next() is called, it takes the least element from the heap to return, 749 // advancing any other iterators that also point to that same element. These 750 // iterators are called with descend=false, since we know that any nodes under 751 // these nodes will also be duplicates, found in the currently selected iterator. 752 // Whenever an iterator is advanced, it is pushed back into the heap if it still 753 // has elements remaining. 754 // 755 // In the case that descend=false - eg, we're asked to ignore all subnodes of the 756 // current node - we also advance any iterators in the heap that have the current 757 // path as a prefix. 758 func (it *unionIterator) Next(descend bool) bool { 759 if len(*it.items) == 0 { 760 return false 761 } 762 763 // Get the next key from the union 764 least := heap.Pop(it.items).(NodeIterator) 765 766 // Skip over other nodes as long as they're identical, or, if we're not descending, as 767 // long as they have the same prefix as the current node. 768 for len(*it.items) > 0 && ((!descend && bytes.HasPrefix((*it.items)[0].Path(), least.Path())) || compareNodes(least, (*it.items)[0]) == 0) { 769 skipped := heap.Pop(it.items).(NodeIterator) 770 // Skip the whole subtree if the nodes have hashes; otherwise just skip this node 771 if skipped.Next(skipped.Hash() == common.Hash{}) { 772 it.count++ 773 // If there are more elements, push the iterator back on the heap 774 heap.Push(it.items, skipped) 775 } 776 } 777 if least.Next(descend) { 778 it.count++ 779 heap.Push(it.items, least) 780 } 781 return len(*it.items) > 0 782 } 783 784 func (it *unionIterator) Error() error { 785 for i := 0; i < len(*it.items); i++ { 786 if err := (*it.items)[i].Error(); err != nil { 787 return err 788 } 789 } 790 return nil 791 }