iterator.go 13 KB

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