iterator_fast.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. // Copyright 2019 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 snapshot
  17. import (
  18. "bytes"
  19. "fmt"
  20. "sort"
  21. "github.com/ethereum/go-ethereum/common"
  22. )
  23. // weightedIterator is a iterator with an assigned weight. It is used to prioritise
  24. // which account or storage slot is the correct one if multiple iterators find the
  25. // same one (modified in multiple consecutive blocks).
  26. type weightedIterator struct {
  27. it Iterator
  28. priority int
  29. }
  30. // weightedIterators is a set of iterators implementing the sort.Interface.
  31. type weightedIterators []*weightedIterator
  32. // Len implements sort.Interface, returning the number of active iterators.
  33. func (its weightedIterators) Len() int { return len(its) }
  34. // Less implements sort.Interface, returning which of two iterators in the stack
  35. // is before the other.
  36. func (its weightedIterators) Less(i, j int) bool {
  37. // Order the iterators primarily by the account hashes
  38. hashI := its[i].it.Hash()
  39. hashJ := its[j].it.Hash()
  40. switch bytes.Compare(hashI[:], hashJ[:]) {
  41. case -1:
  42. return true
  43. case 1:
  44. return false
  45. }
  46. // Same account/storage-slot in multiple layers, split by priority
  47. return its[i].priority < its[j].priority
  48. }
  49. // Swap implements sort.Interface, swapping two entries in the iterator stack.
  50. func (its weightedIterators) Swap(i, j int) {
  51. its[i], its[j] = its[j], its[i]
  52. }
  53. // fastIterator is a more optimized multi-layer iterator which maintains a
  54. // direct mapping of all iterators leading down to the bottom layer.
  55. type fastIterator struct {
  56. tree *Tree // Snapshot tree to reinitialize stale sub-iterators with
  57. root common.Hash // Root hash to reinitialize stale sub-iterators through
  58. curAccount []byte
  59. curSlot []byte
  60. iterators weightedIterators
  61. initiated bool
  62. account bool
  63. fail error
  64. }
  65. // newFastIterator creates a new hierarchical account or storage iterator with one
  66. // element per diff layer. The returned combo iterator can be used to walk over
  67. // the entire snapshot diff stack simultaneously.
  68. func newFastIterator(tree *Tree, root common.Hash, account common.Hash, seek common.Hash, accountIterator bool) (*fastIterator, error) {
  69. snap := tree.Snapshot(root)
  70. if snap == nil {
  71. return nil, fmt.Errorf("unknown snapshot: %x", root)
  72. }
  73. fi := &fastIterator{
  74. tree: tree,
  75. root: root,
  76. account: accountIterator,
  77. }
  78. current := snap.(snapshot)
  79. for depth := 0; current != nil; depth++ {
  80. if accountIterator {
  81. fi.iterators = append(fi.iterators, &weightedIterator{
  82. it: current.AccountIterator(seek),
  83. priority: depth,
  84. })
  85. } else {
  86. // If the whole storage is destructed in this layer, don't
  87. // bother deeper layer anymore. But we should still keep
  88. // the iterator for this layer, since the iterator can contain
  89. // some valid slots which belongs to the re-created account.
  90. it, destructed := current.StorageIterator(account, seek)
  91. fi.iterators = append(fi.iterators, &weightedIterator{
  92. it: it,
  93. priority: depth,
  94. })
  95. if destructed {
  96. break
  97. }
  98. }
  99. current = current.Parent()
  100. }
  101. fi.init()
  102. return fi, nil
  103. }
  104. // init walks over all the iterators and resolves any clashes between them, after
  105. // which it prepares the stack for step-by-step iteration.
  106. func (fi *fastIterator) init() {
  107. // Track which account hashes are iterators positioned on
  108. var positioned = make(map[common.Hash]int)
  109. // Position all iterators and track how many remain live
  110. for i := 0; i < len(fi.iterators); i++ {
  111. // Retrieve the first element and if it clashes with a previous iterator,
  112. // advance either the current one or the old one. Repeat until nothing is
  113. // clashing any more.
  114. it := fi.iterators[i]
  115. for {
  116. // If the iterator is exhausted, drop it off the end
  117. if !it.it.Next() {
  118. it.it.Release()
  119. last := len(fi.iterators) - 1
  120. fi.iterators[i] = fi.iterators[last]
  121. fi.iterators[last] = nil
  122. fi.iterators = fi.iterators[:last]
  123. i--
  124. break
  125. }
  126. // The iterator is still alive, check for collisions with previous ones
  127. hash := it.it.Hash()
  128. if other, exist := positioned[hash]; !exist {
  129. positioned[hash] = i
  130. break
  131. } else {
  132. // Iterators collide, one needs to be progressed, use priority to
  133. // determine which.
  134. //
  135. // This whole else-block can be avoided, if we instead
  136. // do an initial priority-sort of the iterators. If we do that,
  137. // then we'll only wind up here if a lower-priority (preferred) iterator
  138. // has the same value, and then we will always just continue.
  139. // However, it costs an extra sort, so it's probably not better
  140. if fi.iterators[other].priority < it.priority {
  141. // The 'it' should be progressed
  142. continue
  143. } else {
  144. // The 'other' should be progressed, swap them
  145. it = fi.iterators[other]
  146. fi.iterators[other], fi.iterators[i] = fi.iterators[i], fi.iterators[other]
  147. continue
  148. }
  149. }
  150. }
  151. }
  152. // Re-sort the entire list
  153. sort.Sort(fi.iterators)
  154. fi.initiated = false
  155. }
  156. // Next steps the iterator forward one element, returning false if exhausted.
  157. func (fi *fastIterator) Next() bool {
  158. if len(fi.iterators) == 0 {
  159. return false
  160. }
  161. if !fi.initiated {
  162. // Don't forward first time -- we had to 'Next' once in order to
  163. // do the sorting already
  164. fi.initiated = true
  165. if fi.account {
  166. fi.curAccount = fi.iterators[0].it.(AccountIterator).Account()
  167. } else {
  168. fi.curSlot = fi.iterators[0].it.(StorageIterator).Slot()
  169. }
  170. if innerErr := fi.iterators[0].it.Error(); innerErr != nil {
  171. fi.fail = innerErr
  172. return false
  173. }
  174. if fi.curAccount != nil || fi.curSlot != nil {
  175. return true
  176. }
  177. // Implicit else: we've hit a nil-account or nil-slot, and need to
  178. // fall through to the loop below to land on something non-nil
  179. }
  180. // If an account or a slot is deleted in one of the layers, the key will
  181. // still be there, but the actual value will be nil. However, the iterator
  182. // should not export nil-values (but instead simply omit the key), so we
  183. // need to loop here until we either
  184. // - get a non-nil value,
  185. // - hit an error,
  186. // - or exhaust the iterator
  187. for {
  188. if !fi.next(0) {
  189. return false // exhausted
  190. }
  191. if fi.account {
  192. fi.curAccount = fi.iterators[0].it.(AccountIterator).Account()
  193. } else {
  194. fi.curSlot = fi.iterators[0].it.(StorageIterator).Slot()
  195. }
  196. if innerErr := fi.iterators[0].it.Error(); innerErr != nil {
  197. fi.fail = innerErr
  198. return false // error
  199. }
  200. if fi.curAccount != nil || fi.curSlot != nil {
  201. break // non-nil value found
  202. }
  203. }
  204. return true
  205. }
  206. // next handles the next operation internally and should be invoked when we know
  207. // that two elements in the list may have the same value.
  208. //
  209. // For example, if the iterated hashes become [2,3,5,5,8,9,10], then we should
  210. // invoke next(3), which will call Next on elem 3 (the second '5') and will
  211. // cascade along the list, applying the same operation if needed.
  212. func (fi *fastIterator) next(idx int) bool {
  213. // If this particular iterator got exhausted, remove it and return true (the
  214. // next one is surely not exhausted yet, otherwise it would have been removed
  215. // already).
  216. if it := fi.iterators[idx].it; !it.Next() {
  217. it.Release()
  218. fi.iterators = append(fi.iterators[:idx], fi.iterators[idx+1:]...)
  219. return len(fi.iterators) > 0
  220. }
  221. // If there's no one left to cascade into, return
  222. if idx == len(fi.iterators)-1 {
  223. return true
  224. }
  225. // We next-ed the iterator at 'idx', now we may have to re-sort that element
  226. var (
  227. cur, next = fi.iterators[idx], fi.iterators[idx+1]
  228. curHash, nextHash = cur.it.Hash(), next.it.Hash()
  229. )
  230. if diff := bytes.Compare(curHash[:], nextHash[:]); diff < 0 {
  231. // It is still in correct place
  232. return true
  233. } else if diff == 0 && cur.priority < next.priority {
  234. // So still in correct place, but we need to iterate on the next
  235. fi.next(idx + 1)
  236. return true
  237. }
  238. // At this point, the iterator is in the wrong location, but the remaining
  239. // list is sorted. Find out where to move the item.
  240. clash := -1
  241. index := sort.Search(len(fi.iterators), func(n int) bool {
  242. // The iterator always advances forward, so anything before the old slot
  243. // is known to be behind us, so just skip them altogether. This actually
  244. // is an important clause since the sort order got invalidated.
  245. if n < idx {
  246. return false
  247. }
  248. if n == len(fi.iterators)-1 {
  249. // Can always place an elem last
  250. return true
  251. }
  252. nextHash := fi.iterators[n+1].it.Hash()
  253. if diff := bytes.Compare(curHash[:], nextHash[:]); diff < 0 {
  254. return true
  255. } else if diff > 0 {
  256. return false
  257. }
  258. // The elem we're placing it next to has the same value,
  259. // so whichever winds up on n+1 will need further iteraton
  260. clash = n + 1
  261. return cur.priority < fi.iterators[n+1].priority
  262. })
  263. fi.move(idx, index)
  264. if clash != -1 {
  265. fi.next(clash)
  266. }
  267. return true
  268. }
  269. // move advances an iterator to another position in the list.
  270. func (fi *fastIterator) move(index, newpos int) {
  271. elem := fi.iterators[index]
  272. copy(fi.iterators[index:], fi.iterators[index+1:newpos+1])
  273. fi.iterators[newpos] = elem
  274. }
  275. // Error returns any failure that occurred during iteration, which might have
  276. // caused a premature iteration exit (e.g. snapshot stack becoming stale).
  277. func (fi *fastIterator) Error() error {
  278. return fi.fail
  279. }
  280. // Hash returns the current key
  281. func (fi *fastIterator) Hash() common.Hash {
  282. return fi.iterators[0].it.Hash()
  283. }
  284. // Account returns the current account blob.
  285. // Note the returned account is not a copy, please don't modify it.
  286. func (fi *fastIterator) Account() []byte {
  287. return fi.curAccount
  288. }
  289. // Slot returns the current storage slot.
  290. // Note the returned slot is not a copy, please don't modify it.
  291. func (fi *fastIterator) Slot() []byte {
  292. return fi.curSlot
  293. }
  294. // Release iterates over all the remaining live layer iterators and releases each
  295. // of thme individually.
  296. func (fi *fastIterator) Release() {
  297. for _, it := range fi.iterators {
  298. it.it.Release()
  299. }
  300. fi.iterators = nil
  301. }
  302. // Debug is a convencience helper during testing
  303. func (fi *fastIterator) Debug() {
  304. for _, it := range fi.iterators {
  305. fmt.Printf("[p=%v v=%v] ", it.priority, it.it.Hash()[0])
  306. }
  307. fmt.Println()
  308. }
  309. // newFastAccountIterator creates a new hierarchical account iterator with one
  310. // element per diff layer. The returned combo iterator can be used to walk over
  311. // the entire snapshot diff stack simultaneously.
  312. func newFastAccountIterator(tree *Tree, root common.Hash, seek common.Hash) (AccountIterator, error) {
  313. return newFastIterator(tree, root, common.Hash{}, seek, true)
  314. }
  315. // newFastStorageIterator creates a new hierarchical storage iterator with one
  316. // element per diff layer. The returned combo iterator can be used to walk over
  317. // the entire snapshot diff stack simultaneously.
  318. func newFastStorageIterator(tree *Tree, root common.Hash, account common.Hash, seek common.Hash) (StorageIterator, error) {
  319. return newFastIterator(tree, root, account, seek, false)
  320. }