iterator.go 16 KB

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