iterator.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. "github.com/ethereum/go-ethereum/common"
  21. )
  22. // Iterator is a key-value trie iterator that traverses a Trie.
  23. type Iterator struct {
  24. nodeIt NodeIterator
  25. Key []byte // Current data key on which the iterator is positioned on
  26. Value []byte // Current data value on which the iterator is positioned on
  27. }
  28. // NewIterator creates a new key-value iterator.
  29. func NewIterator(trie *Trie) *Iterator {
  30. return &Iterator{
  31. nodeIt: NewNodeIterator(trie),
  32. }
  33. }
  34. // FromNodeIterator creates a new key-value iterator from a node iterator
  35. func NewIteratorFromNodeIterator(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. if it.nodeIt.Leaf() {
  44. it.Key = decodeCompact(it.nodeIt.Path())
  45. it.Value = it.nodeIt.LeafBlob()
  46. return true
  47. }
  48. }
  49. it.Key = nil
  50. it.Value = nil
  51. return false
  52. }
  53. // NodeIterator is an iterator to traverse the trie pre-order.
  54. type NodeIterator interface {
  55. // Hash returns the hash of the current node
  56. Hash() common.Hash
  57. // Parent returns the hash of the parent of the current node
  58. Parent() common.Hash
  59. // Leaf returns true iff the current node is a leaf node.
  60. Leaf() bool
  61. // LeafBlob returns the contents of the node, if it is a leaf.
  62. // Callers must not retain references to the return value after calling Next()
  63. LeafBlob() []byte
  64. // Path returns the hex-encoded path to the current node.
  65. // Callers must not retain references to the return value after calling Next()
  66. Path() []byte
  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. // Error returns the error status of the iterator.
  71. Error() error
  72. }
  73. // nodeIteratorState represents the iteration state at one particular node of the
  74. // trie, which can be resumed at a later invocation.
  75. type nodeIteratorState struct {
  76. hash common.Hash // Hash of the node being iterated (nil if not standalone)
  77. node node // Trie node being iterated
  78. parent common.Hash // Hash of the first full ancestor node (nil if current is the root)
  79. child int // Child to be processed next
  80. pathlen int // Length of the path to this node
  81. }
  82. type nodeIterator struct {
  83. trie *Trie // Trie being iterated
  84. stack []*nodeIteratorState // Hierarchy of trie nodes persisting the iteration state
  85. err error // Failure set in case of an internal error in the iterator
  86. path []byte // Path to the current node
  87. }
  88. // NewNodeIterator creates an post-order trie iterator.
  89. func NewNodeIterator(trie *Trie) NodeIterator {
  90. if trie.Hash() == emptyState {
  91. return new(nodeIterator)
  92. }
  93. return &nodeIterator{trie: trie}
  94. }
  95. // Hash returns the hash of the current node
  96. func (it *nodeIterator) Hash() common.Hash {
  97. if len(it.stack) == 0 {
  98. return common.Hash{}
  99. }
  100. return it.stack[len(it.stack)-1].hash
  101. }
  102. // Parent returns the hash of the parent node
  103. func (it *nodeIterator) Parent() common.Hash {
  104. if len(it.stack) == 0 {
  105. return common.Hash{}
  106. }
  107. return it.stack[len(it.stack)-1].parent
  108. }
  109. // Leaf returns true if the current node is a leaf
  110. func (it *nodeIterator) Leaf() bool {
  111. if len(it.stack) == 0 {
  112. return false
  113. }
  114. _, ok := it.stack[len(it.stack)-1].node.(valueNode)
  115. return ok
  116. }
  117. // LeafBlob returns the data for the current node, if it is a leaf
  118. func (it *nodeIterator) LeafBlob() []byte {
  119. if len(it.stack) == 0 {
  120. return nil
  121. }
  122. if node, ok := it.stack[len(it.stack)-1].node.(valueNode); ok {
  123. return []byte(node)
  124. }
  125. return nil
  126. }
  127. // Path returns the hex-encoded path to the current node
  128. func (it *nodeIterator) Path() []byte {
  129. return it.path
  130. }
  131. // Error returns the error set in case of an internal error in the iterator
  132. func (it *nodeIterator) Error() error {
  133. return it.err
  134. }
  135. // Next moves the iterator to the next node, returning whether there are any
  136. // further nodes. In case of an internal error this method returns false and
  137. // sets the Error field to the encountered failure. If `descend` is false,
  138. // skips iterating over any subnodes of the current node.
  139. func (it *nodeIterator) Next(descend bool) bool {
  140. // If the iterator failed previously, don't do anything
  141. if it.err != nil {
  142. return false
  143. }
  144. // Otherwise step forward with the iterator and report any errors
  145. if err := it.step(descend); err != nil {
  146. it.err = err
  147. return false
  148. }
  149. return it.trie != nil
  150. }
  151. // step moves the iterator to the next node of the trie.
  152. func (it *nodeIterator) step(descend bool) error {
  153. if it.trie == nil {
  154. // Abort if we reached the end of the iteration
  155. return nil
  156. }
  157. if len(it.stack) == 0 {
  158. // Initialize the iterator if we've just started.
  159. root := it.trie.Hash()
  160. state := &nodeIteratorState{node: it.trie.root, child: -1}
  161. if root != emptyRoot {
  162. state.hash = root
  163. }
  164. it.stack = append(it.stack, state)
  165. return nil
  166. }
  167. if !descend {
  168. // If we're skipping children, pop the current node first
  169. it.path = it.path[:it.stack[len(it.stack)-1].pathlen]
  170. it.stack = it.stack[:len(it.stack)-1]
  171. }
  172. // Continue iteration to the next child
  173. outer:
  174. for {
  175. if len(it.stack) == 0 {
  176. it.trie = nil
  177. return nil
  178. }
  179. parent := it.stack[len(it.stack)-1]
  180. ancestor := parent.hash
  181. if (ancestor == common.Hash{}) {
  182. ancestor = parent.parent
  183. }
  184. if node, ok := parent.node.(*fullNode); ok {
  185. // Full node, iterate over children
  186. for parent.child++; parent.child < len(node.Children); parent.child++ {
  187. child := node.Children[parent.child]
  188. if child != nil {
  189. hash, _ := child.cache()
  190. it.stack = append(it.stack, &nodeIteratorState{
  191. hash: common.BytesToHash(hash),
  192. node: child,
  193. parent: ancestor,
  194. child: -1,
  195. pathlen: len(it.path),
  196. })
  197. it.path = append(it.path, byte(parent.child))
  198. break outer
  199. }
  200. }
  201. } else if node, ok := parent.node.(*shortNode); ok {
  202. // Short node, return the pointer singleton child
  203. if parent.child < 0 {
  204. parent.child++
  205. hash, _ := node.Val.cache()
  206. it.stack = append(it.stack, &nodeIteratorState{
  207. hash: common.BytesToHash(hash),
  208. node: node.Val,
  209. parent: ancestor,
  210. child: -1,
  211. pathlen: len(it.path),
  212. })
  213. if hasTerm(node.Key) {
  214. it.path = append(it.path, node.Key[:len(node.Key)-1]...)
  215. } else {
  216. it.path = append(it.path, node.Key...)
  217. }
  218. break
  219. }
  220. } else if hash, ok := parent.node.(hashNode); ok {
  221. // Hash node, resolve the hash child from the database
  222. if parent.child < 0 {
  223. parent.child++
  224. node, err := it.trie.resolveHash(hash, nil, nil)
  225. if err != nil {
  226. return err
  227. }
  228. it.stack = append(it.stack, &nodeIteratorState{
  229. hash: common.BytesToHash(hash),
  230. node: node,
  231. parent: ancestor,
  232. child: -1,
  233. pathlen: len(it.path),
  234. })
  235. break
  236. }
  237. }
  238. it.path = it.path[:parent.pathlen]
  239. it.stack = it.stack[:len(it.stack)-1]
  240. }
  241. return nil
  242. }
  243. func compareNodes(a, b NodeIterator) int {
  244. cmp := bytes.Compare(a.Path(), b.Path())
  245. if cmp != 0 {
  246. return cmp
  247. }
  248. if a.Leaf() && !b.Leaf() {
  249. return -1
  250. } else if b.Leaf() && !a.Leaf() {
  251. return 1
  252. }
  253. cmp = bytes.Compare(a.Hash().Bytes(), b.Hash().Bytes())
  254. if cmp != 0 {
  255. return cmp
  256. }
  257. return bytes.Compare(a.LeafBlob(), b.LeafBlob())
  258. }
  259. type differenceIterator struct {
  260. a, b NodeIterator // Nodes returned are those in b - a.
  261. eof bool // Indicates a has run out of elements
  262. count int // Number of nodes scanned on either trie
  263. }
  264. // NewDifferenceIterator constructs a NodeIterator that iterates over elements in b that
  265. // are not in a. Returns the iterator, and a pointer to an integer recording the number
  266. // of nodes seen.
  267. func NewDifferenceIterator(a, b NodeIterator) (NodeIterator, *int) {
  268. a.Next(true)
  269. it := &differenceIterator{
  270. a: a,
  271. b: b,
  272. }
  273. return it, &it.count
  274. }
  275. func (it *differenceIterator) Hash() common.Hash {
  276. return it.b.Hash()
  277. }
  278. func (it *differenceIterator) Parent() common.Hash {
  279. return it.b.Parent()
  280. }
  281. func (it *differenceIterator) Leaf() bool {
  282. return it.b.Leaf()
  283. }
  284. func (it *differenceIterator) LeafBlob() []byte {
  285. return it.b.LeafBlob()
  286. }
  287. func (it *differenceIterator) Path() []byte {
  288. return it.b.Path()
  289. }
  290. func (it *differenceIterator) Next(bool) bool {
  291. // Invariants:
  292. // - We always advance at least one element in b.
  293. // - At the start of this function, a's path is lexically greater than b's.
  294. if !it.b.Next(true) {
  295. return false
  296. }
  297. it.count += 1
  298. if it.eof {
  299. // a has reached eof, so we just return all elements from b
  300. return true
  301. }
  302. for {
  303. switch compareNodes(it.a, it.b) {
  304. case -1:
  305. // b jumped past a; advance a
  306. if !it.a.Next(true) {
  307. it.eof = true
  308. return true
  309. }
  310. it.count += 1
  311. case 1:
  312. // b is before a
  313. return true
  314. case 0:
  315. // a and b are identical; skip this whole subtree if the nodes have hashes
  316. hasHash := it.a.Hash() == common.Hash{}
  317. if !it.b.Next(hasHash) {
  318. return false
  319. }
  320. it.count += 1
  321. if !it.a.Next(hasHash) {
  322. it.eof = true
  323. return true
  324. }
  325. it.count += 1
  326. }
  327. }
  328. }
  329. func (it *differenceIterator) Error() error {
  330. if err := it.a.Error(); err != nil {
  331. return err
  332. }
  333. return it.b.Error()
  334. }
  335. type nodeIteratorHeap []NodeIterator
  336. func (h nodeIteratorHeap) Len() int { return len(h) }
  337. func (h nodeIteratorHeap) Less(i, j int) bool { return compareNodes(h[i], h[j]) < 0 }
  338. func (h nodeIteratorHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
  339. func (h *nodeIteratorHeap) Push(x interface{}) { *h = append(*h, x.(NodeIterator)) }
  340. func (h *nodeIteratorHeap) Pop() interface{} {
  341. n := len(*h)
  342. x := (*h)[n-1]
  343. *h = (*h)[0 : n-1]
  344. return x
  345. }
  346. type unionIterator struct {
  347. items *nodeIteratorHeap // Nodes returned are the union of the ones in these iterators
  348. count int // Number of nodes scanned across all tries
  349. err error // The error, if one has been encountered
  350. }
  351. // NewUnionIterator constructs a NodeIterator that iterates over elements in the union
  352. // of the provided NodeIterators. Returns the iterator, and a pointer to an integer
  353. // recording the number of nodes visited.
  354. func NewUnionIterator(iters []NodeIterator) (NodeIterator, *int) {
  355. h := make(nodeIteratorHeap, len(iters))
  356. copy(h, iters)
  357. heap.Init(&h)
  358. ui := &unionIterator{
  359. items: &h,
  360. }
  361. return ui, &ui.count
  362. }
  363. func (it *unionIterator) Hash() common.Hash {
  364. return (*it.items)[0].Hash()
  365. }
  366. func (it *unionIterator) Parent() common.Hash {
  367. return (*it.items)[0].Parent()
  368. }
  369. func (it *unionIterator) Leaf() bool {
  370. return (*it.items)[0].Leaf()
  371. }
  372. func (it *unionIterator) LeafBlob() []byte {
  373. return (*it.items)[0].LeafBlob()
  374. }
  375. func (it *unionIterator) Path() []byte {
  376. return (*it.items)[0].Path()
  377. }
  378. // Next returns the next node in the union of tries being iterated over.
  379. //
  380. // It does this by maintaining a heap of iterators, sorted by the iteration
  381. // order of their next elements, with one entry for each source trie. Each
  382. // time Next() is called, it takes the least element from the heap to return,
  383. // advancing any other iterators that also point to that same element. These
  384. // iterators are called with descend=false, since we know that any nodes under
  385. // these nodes will also be duplicates, found in the currently selected iterator.
  386. // Whenever an iterator is advanced, it is pushed back into the heap if it still
  387. // has elements remaining.
  388. //
  389. // In the case that descend=false - eg, we're asked to ignore all subnodes of the
  390. // current node - we also advance any iterators in the heap that have the current
  391. // path as a prefix.
  392. func (it *unionIterator) Next(descend bool) bool {
  393. if len(*it.items) == 0 {
  394. return false
  395. }
  396. // Get the next key from the union
  397. least := heap.Pop(it.items).(NodeIterator)
  398. // Skip over other nodes as long as they're identical, or, if we're not descending, as
  399. // long as they have the same prefix as the current node.
  400. for len(*it.items) > 0 && ((!descend && bytes.HasPrefix((*it.items)[0].Path(), least.Path())) || compareNodes(least, (*it.items)[0]) == 0) {
  401. skipped := heap.Pop(it.items).(NodeIterator)
  402. // Skip the whole subtree if the nodes have hashes; otherwise just skip this node
  403. if skipped.Next(skipped.Hash() == common.Hash{}) {
  404. it.count += 1
  405. // If there are more elements, push the iterator back on the heap
  406. heap.Push(it.items, skipped)
  407. }
  408. }
  409. if least.Next(descend) {
  410. it.count += 1
  411. heap.Push(it.items, least)
  412. }
  413. return len(*it.items) > 0
  414. }
  415. func (it *unionIterator) Error() error {
  416. for i := 0; i < len(*it.items); i++ {
  417. if err := (*it.items)[i].Error(); err != nil {
  418. return err
  419. }
  420. }
  421. return nil
  422. }