iterator.go 14 KB

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