iterator.go 19 KB

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