github.com/ebceco/ebc@v1.8.19-0.20190309150932-8cb0b9e06484/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/ebceco/ebc/common" 25 "github.com/ebceco/ebc/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 defer returnHasherToPool(hasher) 185 186 proofs := make([][]byte, 0, len(it.stack)) 187 188 for i, item := range it.stack[:len(it.stack)-1] { 189 // Gather nodes that end up as hash nodes (or the root) 190 node, _, _ := hasher.hashChildren(item.node, nil) 191 hashed, _ := hasher.store(node, nil, false) 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 }