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