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