github.com/core-coin/go-core/v2@v2.1.9/trie/iterator.go (about) 1 // Copyright 2014 by the Authors 2 // This file is part of the go-core library. 3 // 4 // The go-core 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-core 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-core 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/core-coin/go-core/v2/common" 25 "github.com/core-coin/go-core/v2/rlp" 26 ) 27 28 // Iterator is a key-value trie iterator that traverses a Trie. 29 type Iterator struct { 30 nodeIt NodeIterator 31 32 Key []byte // Current data key on which the iterator is positioned on 33 Value []byte // Current data value on which the iterator is positioned on 34 Err error 35 } 36 37 // NewIterator creates a new key-value iterator from a node iterator. 38 // Note that the value returned by the iterator is raw. If the content is encoded 39 // (e.g. storage value is RLP-encoded), it's caller's duty to decode it. 40 func NewIterator(it NodeIterator) *Iterator { 41 return &Iterator{ 42 nodeIt: it, 43 } 44 } 45 46 // Next moves the iterator forward one key-value entry. 47 func (it *Iterator) Next() bool { 48 for it.nodeIt.Next(true) { 49 if it.nodeIt.Leaf() { 50 it.Key = it.nodeIt.LeafKey() 51 it.Value = it.nodeIt.LeafBlob() 52 return true 53 } 54 } 55 it.Key = nil 56 it.Value = nil 57 it.Err = it.nodeIt.Error() 58 return false 59 } 60 61 // Prove generates the Merkle proof for the leaf node the iterator is currently 62 // positioned on. 63 func (it *Iterator) Prove() [][]byte { 64 return it.nodeIt.LeafProof() 65 } 66 67 // NodeIterator is an iterator to traverse the trie pre-order. 68 type NodeIterator interface { 69 // Next moves the iterator to the next node. If the parameter is false, any child 70 // nodes will be skipped. 71 Next(bool) bool 72 73 // Error returns the error status of the iterator. 74 Error() error 75 76 // Hash returns the hash of the current node. 77 Hash() common.Hash 78 79 // Parent returns the hash of the parent of the current node. The hash may be the one 80 // grandparent if the immediate parent is an internal node with no hash. 81 Parent() common.Hash 82 83 // Path returns the hex-encoded path to the current node. 84 // Callers must not retain references to the return value after calling Next. 85 // For leaf nodes, the last element of the path is the 'terminator symbol' 0x10. 86 Path() []byte 87 88 // Leaf returns true iff the current node is a leaf node. 89 Leaf() bool 90 91 // LeafKey returns the key of the leaf. The method panics if the iterator is not 92 // positioned at a leaf. Callers must not retain references to the value after 93 // calling Next. 94 LeafKey() []byte 95 96 // LeafBlob returns the content of the leaf. The method panics if the iterator 97 // is not positioned at a leaf. Callers must not retain references to the value 98 // after calling Next. 99 LeafBlob() []byte 100 101 // LeafProof returns the Merkle proof of the leaf. The method panics if the 102 // iterator is not positioned at a leaf. Callers must not retain references 103 // to the value after calling Next. 104 LeafProof() [][]byte 105 } 106 107 // nodeIteratorState represents the iteration state at one particular node of the 108 // trie, which can be resumed at a later invocation. 109 type nodeIteratorState struct { 110 hash common.Hash // Hash of the node being iterated (nil if not standalone) 111 node node // Trie node being iterated 112 parent common.Hash // Hash of the first full ancestor node (nil if current is the root) 113 index int // Child to be processed next 114 pathlen int // Length of the path to this node 115 } 116 117 type nodeIterator struct { 118 trie *Trie // Trie being iterated 119 stack []*nodeIteratorState // Hierarchy of trie nodes persisting the iteration state 120 path []byte // Path to the current node 121 err error // Failure set in case of an internal error in the iterator 122 } 123 124 // errIteratorEnd is stored in nodeIterator.err when iteration is done. 125 var errIteratorEnd = errors.New("end of iteration") 126 127 // seekError is stored in nodeIterator.err if the initial seek has failed. 128 type seekError struct { 129 key []byte 130 err error 131 } 132 133 func (e seekError) Error() string { 134 return "seek error: " + e.err.Error() 135 } 136 137 func newNodeIterator(trie *Trie, start []byte) NodeIterator { 138 if trie.Hash() == emptyState { 139 return new(nodeIterator) 140 } 141 it := &nodeIterator{trie: trie} 142 it.err = it.seek(start) 143 return it 144 } 145 146 func (it *nodeIterator) Hash() common.Hash { 147 if len(it.stack) == 0 { 148 return common.Hash{} 149 } 150 return it.stack[len(it.stack)-1].hash 151 } 152 153 func (it *nodeIterator) Parent() common.Hash { 154 if len(it.stack) == 0 { 155 return common.Hash{} 156 } 157 return it.stack[len(it.stack)-1].parent 158 } 159 160 func (it *nodeIterator) Leaf() bool { 161 return hasTerm(it.path) 162 } 163 164 func (it *nodeIterator) LeafKey() []byte { 165 if len(it.stack) > 0 { 166 if _, ok := it.stack[len(it.stack)-1].node.(valueNode); ok { 167 return hexToKeybytes(it.path) 168 } 169 } 170 panic("not at leaf") 171 } 172 173 func (it *nodeIterator) LeafBlob() []byte { 174 if len(it.stack) > 0 { 175 if node, ok := it.stack[len(it.stack)-1].node.(valueNode); ok { 176 return node 177 } 178 } 179 panic("not at leaf") 180 } 181 182 func (it *nodeIterator) LeafProof() [][]byte { 183 if len(it.stack) > 0 { 184 if _, ok := it.stack[len(it.stack)-1].node.(valueNode); ok { 185 hasher := newHasher(false) 186 defer returnHasherToPool(hasher) 187 proofs := make([][]byte, 0, len(it.stack)) 188 189 for i, item := range it.stack[:len(it.stack)-1] { 190 // Gather nodes that end up as hash nodes (or the root) 191 node, hashed := hasher.proofHash(item.node) 192 if _, ok := hashed.(hashNode); ok || i == 0 { 193 enc, _ := rlp.EncodeToBytes(node) 194 proofs = append(proofs, enc) 195 } 196 } 197 return proofs 198 } 199 } 200 panic("not at leaf") 201 } 202 203 func (it *nodeIterator) Path() []byte { 204 return it.path 205 } 206 207 func (it *nodeIterator) Error() error { 208 if it.err == errIteratorEnd { 209 return nil 210 } 211 if seek, ok := it.err.(seekError); ok { 212 return seek.err 213 } 214 return it.err 215 } 216 217 // Next moves the iterator to the next node, returning whether there are any 218 // further nodes. In case of an internal error this method returns false and 219 // sets the Error field to the encountered failure. If `descend` is false, 220 // skips iterating over any subnodes of the current node. 221 func (it *nodeIterator) Next(descend bool) bool { 222 if it.err == errIteratorEnd { 223 return false 224 } 225 if seek, ok := it.err.(seekError); ok { 226 if it.err = it.seek(seek.key); it.err != nil { 227 return false 228 } 229 } 230 // Otherwise step forward with the iterator and report any errors. 231 state, parentIndex, path, err := it.peek(descend) 232 it.err = err 233 if it.err != nil { 234 return false 235 } 236 it.push(state, parentIndex, path) 237 return true 238 } 239 240 func (it *nodeIterator) seek(prefix []byte) error { 241 // The path we're looking for is the hex encoded key without terminator. 242 key := keybytesToHex(prefix) 243 key = key[:len(key)-1] 244 // Move forward until we're just before the closest match to key. 245 for { 246 state, parentIndex, path, err := it.peek(bytes.HasPrefix(key, it.path)) 247 if err == errIteratorEnd { 248 return errIteratorEnd 249 } else if err != nil { 250 return seekError{prefix, err} 251 } else if bytes.Compare(path, key) >= 0 { 252 return nil 253 } 254 it.push(state, parentIndex, path) 255 } 256 } 257 258 // peek creates the next state of the iterator. 259 func (it *nodeIterator) peek(descend bool) (*nodeIteratorState, *int, []byte, error) { 260 if len(it.stack) == 0 { 261 // Initialize the iterator if we've just started. 262 root := it.trie.Hash() 263 state := &nodeIteratorState{node: it.trie.root, index: -1} 264 if root != emptyRoot { 265 state.hash = root 266 } 267 err := state.resolve(it.trie, nil) 268 return state, nil, nil, err 269 } 270 if !descend { 271 // If we're skipping children, pop the current node first 272 it.pop() 273 } 274 275 // Continue iteration to the next child 276 for len(it.stack) > 0 { 277 parent := it.stack[len(it.stack)-1] 278 ancestor := parent.hash 279 if (ancestor == common.Hash{}) { 280 ancestor = parent.parent 281 } 282 state, path, ok := it.nextChild(parent, ancestor) 283 if ok { 284 if err := state.resolve(it.trie, path); err != nil { 285 return parent, &parent.index, path, err 286 } 287 return state, &parent.index, path, nil 288 } 289 // No more child nodes, move back up. 290 it.pop() 291 } 292 return nil, nil, nil, errIteratorEnd 293 } 294 295 func (st *nodeIteratorState) resolve(tr *Trie, path []byte) error { 296 if hash, ok := st.node.(hashNode); ok { 297 resolved, err := tr.resolveHash(hash, path) 298 if err != nil { 299 return err 300 } 301 st.node = resolved 302 st.hash = common.BytesToHash(hash) 303 } 304 return nil 305 } 306 307 func (it *nodeIterator) nextChild(parent *nodeIteratorState, ancestor common.Hash) (*nodeIteratorState, []byte, bool) { 308 switch node := parent.node.(type) { 309 case *fullNode: 310 // Full node, move to the first non-nil child. 311 for i := parent.index + 1; i < len(node.Children); i++ { 312 child := node.Children[i] 313 if child != nil { 314 hash, _ := child.cache() 315 state := &nodeIteratorState{ 316 hash: common.BytesToHash(hash), 317 node: child, 318 parent: ancestor, 319 index: -1, 320 pathlen: len(it.path), 321 } 322 path := append(it.path, byte(i)) 323 parent.index = i - 1 324 return state, path, true 325 } 326 } 327 case *shortNode: 328 // Short node, return the pointer singleton child 329 if parent.index < 0 { 330 hash, _ := node.Val.cache() 331 state := &nodeIteratorState{ 332 hash: common.BytesToHash(hash), 333 node: node.Val, 334 parent: ancestor, 335 index: -1, 336 pathlen: len(it.path), 337 } 338 path := append(it.path, node.Key...) 339 return state, path, true 340 } 341 } 342 return parent, it.path, false 343 } 344 345 func (it *nodeIterator) push(state *nodeIteratorState, parentIndex *int, path []byte) { 346 it.path = path 347 it.stack = append(it.stack, state) 348 if parentIndex != nil { 349 *parentIndex++ 350 } 351 } 352 353 func (it *nodeIterator) pop() { 354 parent := it.stack[len(it.stack)-1] 355 it.path = it.path[:parent.pathlen] 356 it.stack = it.stack[:len(it.stack)-1] 357 } 358 359 func compareNodes(a, b NodeIterator) int { 360 if cmp := bytes.Compare(a.Path(), b.Path()); cmp != 0 { 361 return cmp 362 } 363 if a.Leaf() && !b.Leaf() { 364 return -1 365 } else if b.Leaf() && !a.Leaf() { 366 return 1 367 } 368 if cmp := bytes.Compare(a.Hash().Bytes(), b.Hash().Bytes()); cmp != 0 { 369 return cmp 370 } 371 if a.Leaf() && b.Leaf() { 372 return bytes.Compare(a.LeafBlob(), b.LeafBlob()) 373 } 374 return 0 375 } 376 377 type differenceIterator struct { 378 a, b NodeIterator // Nodes returned are those in b - a. 379 eof bool // Indicates a has run out of elements 380 count int // Number of nodes scanned on either trie 381 } 382 383 // NewDifferenceIterator constructs a NodeIterator that iterates over elements in b that 384 // are not in a. Returns the iterator, and a pointer to an integer recording the number 385 // of nodes seen. 386 func NewDifferenceIterator(a, b NodeIterator) (NodeIterator, *int) { 387 a.Next(true) 388 it := &differenceIterator{ 389 a: a, 390 b: b, 391 } 392 return it, &it.count 393 } 394 395 func (it *differenceIterator) Hash() common.Hash { 396 return it.b.Hash() 397 } 398 399 func (it *differenceIterator) Parent() common.Hash { 400 return it.b.Parent() 401 } 402 403 func (it *differenceIterator) Leaf() bool { 404 return it.b.Leaf() 405 } 406 407 func (it *differenceIterator) LeafKey() []byte { 408 return it.b.LeafKey() 409 } 410 411 func (it *differenceIterator) LeafBlob() []byte { 412 return it.b.LeafBlob() 413 } 414 415 func (it *differenceIterator) LeafProof() [][]byte { 416 return it.b.LeafProof() 417 } 418 419 func (it *differenceIterator) Path() []byte { 420 return it.b.Path() 421 } 422 423 func (it *differenceIterator) Next(bool) bool { 424 // Invariants: 425 // - We always advance at least one element in b. 426 // - At the start of this function, a's path is lexically greater than b's. 427 if !it.b.Next(true) { 428 return false 429 } 430 it.count++ 431 432 if it.eof { 433 // a has reached eof, so we just return all elements from b 434 return true 435 } 436 437 for { 438 switch compareNodes(it.a, it.b) { 439 case -1: 440 // b jumped past a; advance a 441 if !it.a.Next(true) { 442 it.eof = true 443 return true 444 } 445 it.count++ 446 case 1: 447 // b is before a 448 return true 449 case 0: 450 // a and b are identical; skip this whole subtree if the nodes have hashes 451 hasHash := it.a.Hash() == common.Hash{} 452 if !it.b.Next(hasHash) { 453 return false 454 } 455 it.count++ 456 if !it.a.Next(hasHash) { 457 it.eof = true 458 return true 459 } 460 it.count++ 461 } 462 } 463 } 464 465 func (it *differenceIterator) Error() error { 466 if err := it.a.Error(); err != nil { 467 return err 468 } 469 return it.b.Error() 470 } 471 472 type nodeIteratorHeap []NodeIterator 473 474 func (h nodeIteratorHeap) Len() int { return len(h) } 475 func (h nodeIteratorHeap) Less(i, j int) bool { return compareNodes(h[i], h[j]) < 0 } 476 func (h nodeIteratorHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] } 477 func (h *nodeIteratorHeap) Push(x interface{}) { *h = append(*h, x.(NodeIterator)) } 478 func (h *nodeIteratorHeap) Pop() interface{} { 479 n := len(*h) 480 x := (*h)[n-1] 481 *h = (*h)[0 : n-1] 482 return x 483 } 484 485 type unionIterator struct { 486 items *nodeIteratorHeap // Nodes returned are the union of the ones in these iterators 487 count int // Number of nodes scanned across all tries 488 } 489 490 // NewUnionIterator constructs a NodeIterator that iterates over elements in the union 491 // of the provided NodeIterators. Returns the iterator, and a pointer to an integer 492 // recording the number of nodes visited. 493 func NewUnionIterator(iters []NodeIterator) (NodeIterator, *int) { 494 h := make(nodeIteratorHeap, len(iters)) 495 copy(h, iters) 496 heap.Init(&h) 497 498 ui := &unionIterator{items: &h} 499 return ui, &ui.count 500 } 501 502 func (it *unionIterator) Hash() common.Hash { 503 return (*it.items)[0].Hash() 504 } 505 506 func (it *unionIterator) Parent() common.Hash { 507 return (*it.items)[0].Parent() 508 } 509 510 func (it *unionIterator) Leaf() bool { 511 return (*it.items)[0].Leaf() 512 } 513 514 func (it *unionIterator) LeafKey() []byte { 515 return (*it.items)[0].LeafKey() 516 } 517 518 func (it *unionIterator) LeafBlob() []byte { 519 return (*it.items)[0].LeafBlob() 520 } 521 522 func (it *unionIterator) LeafProof() [][]byte { 523 return (*it.items)[0].LeafProof() 524 } 525 526 func (it *unionIterator) Path() []byte { 527 return (*it.items)[0].Path() 528 } 529 530 // Next returns the next node in the union of tries being iterated over. 531 // 532 // It does this by maintaining a heap of iterators, sorted by the iteration 533 // order of their next elements, with one entry for each source trie. Each 534 // time Next() is called, it takes the least element from the heap to return, 535 // advancing any other iterators that also point to that same element. These 536 // iterators are called with descend=false, since we know that any nodes under 537 // these nodes will also be duplicates, found in the currently selected iterator. 538 // Whenever an iterator is advanced, it is pushed back into the heap if it still 539 // has elements remaining. 540 // 541 // In the case that descend=false - eg, we're asked to ignore all subnodes of the 542 // current node - we also advance any iterators in the heap that have the current 543 // path as a prefix. 544 func (it *unionIterator) Next(descend bool) bool { 545 if len(*it.items) == 0 { 546 return false 547 } 548 549 // Get the next key from the union 550 least := heap.Pop(it.items).(NodeIterator) 551 552 // Skip over other nodes as long as they're identical, or, if we're not descending, as 553 // long as they have the same prefix as the current node. 554 for len(*it.items) > 0 && ((!descend && bytes.HasPrefix((*it.items)[0].Path(), least.Path())) || compareNodes(least, (*it.items)[0]) == 0) { 555 skipped := heap.Pop(it.items).(NodeIterator) 556 // Skip the whole subtree if the nodes have hashes; otherwise just skip this node 557 if skipped.Next(skipped.Hash() == common.Hash{}) { 558 it.count++ 559 // If there are more elements, push the iterator back on the heap 560 heap.Push(it.items, skipped) 561 } 562 } 563 if least.Next(descend) { 564 it.count++ 565 heap.Push(it.items, least) 566 } 567 return len(*it.items) > 0 568 } 569 570 func (it *unionIterator) Error() error { 571 for i := 0; i < len(*it.items); i++ { 572 if err := (*it.items)[i].Error(); err != nil { 573 return err 574 } 575 } 576 return nil 577 }