iterator_binary.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. "github.com/ethereum/go-ethereum/common"
  20. )
  21. // binaryIterator is a simplistic iterator to step over the accounts or storage
  22. // in a snapshot, which may or may not be composed of multiple layers. Performance
  23. // wise this iterator is slow, it's meant for cross validating the fast one,
  24. type binaryIterator struct {
  25. a Iterator
  26. b Iterator
  27. aDone bool
  28. bDone bool
  29. accountIterator bool
  30. k common.Hash
  31. account common.Hash
  32. fail error
  33. }
  34. // initBinaryAccountIterator creates a simplistic iterator to step over all the
  35. // accounts in a slow, but easily verifiable way. Note this function is used for
  36. // initialization, use `newBinaryAccountIterator` as the API.
  37. func (dl *diffLayer) initBinaryAccountIterator() Iterator {
  38. parent, ok := dl.parent.(*diffLayer)
  39. if !ok {
  40. l := &binaryIterator{
  41. a: dl.AccountIterator(common.Hash{}),
  42. b: dl.Parent().AccountIterator(common.Hash{}),
  43. accountIterator: true,
  44. }
  45. l.aDone = !l.a.Next()
  46. l.bDone = !l.b.Next()
  47. return l
  48. }
  49. l := &binaryIterator{
  50. a: dl.AccountIterator(common.Hash{}),
  51. b: parent.initBinaryAccountIterator(),
  52. accountIterator: true,
  53. }
  54. l.aDone = !l.a.Next()
  55. l.bDone = !l.b.Next()
  56. return l
  57. }
  58. // initBinaryStorageIterator creates a simplistic iterator to step over all the
  59. // storage slots in a slow, but easily verifiable way. Note this function is used
  60. // for initialization, use `newBinaryStorageIterator` as the API.
  61. func (dl *diffLayer) initBinaryStorageIterator(account common.Hash) Iterator {
  62. parent, ok := dl.parent.(*diffLayer)
  63. if !ok {
  64. // If the storage in this layer is already destructed, discard all
  65. // deeper layers but still return an valid single-branch iterator.
  66. a, destructed := dl.StorageIterator(account, common.Hash{})
  67. if destructed {
  68. l := &binaryIterator{
  69. a: a,
  70. account: account,
  71. }
  72. l.aDone = !l.a.Next()
  73. l.bDone = true
  74. return l
  75. }
  76. // The parent is disk layer, don't need to take care "destructed"
  77. // anymore.
  78. b, _ := dl.Parent().StorageIterator(account, common.Hash{})
  79. l := &binaryIterator{
  80. a: a,
  81. b: b,
  82. account: account,
  83. }
  84. l.aDone = !l.a.Next()
  85. l.bDone = !l.b.Next()
  86. return l
  87. }
  88. // If the storage in this layer is already destructed, discard all
  89. // deeper layers but still return an valid single-branch iterator.
  90. a, destructed := dl.StorageIterator(account, common.Hash{})
  91. if destructed {
  92. l := &binaryIterator{
  93. a: a,
  94. account: account,
  95. }
  96. l.aDone = !l.a.Next()
  97. l.bDone = true
  98. return l
  99. }
  100. l := &binaryIterator{
  101. a: a,
  102. b: parent.initBinaryStorageIterator(account),
  103. account: account,
  104. }
  105. l.aDone = !l.a.Next()
  106. l.bDone = !l.b.Next()
  107. return l
  108. }
  109. // Next steps the iterator forward one element, returning false if exhausted,
  110. // or an error if iteration failed for some reason (e.g. root being iterated
  111. // becomes stale and garbage collected).
  112. func (it *binaryIterator) Next() bool {
  113. if it.aDone && it.bDone {
  114. return false
  115. }
  116. first:
  117. if it.aDone {
  118. it.k = it.b.Hash()
  119. it.bDone = !it.b.Next()
  120. return true
  121. }
  122. if it.bDone {
  123. it.k = it.a.Hash()
  124. it.aDone = !it.a.Next()
  125. return true
  126. }
  127. nextA, nextB := it.a.Hash(), it.b.Hash()
  128. if diff := bytes.Compare(nextA[:], nextB[:]); diff < 0 {
  129. it.aDone = !it.a.Next()
  130. it.k = nextA
  131. return true
  132. } else if diff == 0 {
  133. // Now we need to advance one of them
  134. it.aDone = !it.a.Next()
  135. goto first
  136. }
  137. it.bDone = !it.b.Next()
  138. it.k = nextB
  139. return true
  140. }
  141. // Error returns any failure that occurred during iteration, which might have
  142. // caused a premature iteration exit (e.g. snapshot stack becoming stale).
  143. func (it *binaryIterator) Error() error {
  144. return it.fail
  145. }
  146. // Hash returns the hash of the account the iterator is currently at.
  147. func (it *binaryIterator) Hash() common.Hash {
  148. return it.k
  149. }
  150. // Account returns the RLP encoded slim account the iterator is currently at, or
  151. // nil if the iterated snapshot stack became stale (you can check Error after
  152. // to see if it failed or not).
  153. //
  154. // Note the returned account is not a copy, please don't modify it.
  155. func (it *binaryIterator) Account() []byte {
  156. if !it.accountIterator {
  157. return nil
  158. }
  159. // The topmost iterator must be `diffAccountIterator`
  160. blob, err := it.a.(*diffAccountIterator).layer.AccountRLP(it.k)
  161. if err != nil {
  162. it.fail = err
  163. return nil
  164. }
  165. return blob
  166. }
  167. // Slot returns the raw storage slot data the iterator is currently at, or
  168. // nil if the iterated snapshot stack became stale (you can check Error after
  169. // to see if it failed or not).
  170. //
  171. // Note the returned slot is not a copy, please don't modify it.
  172. func (it *binaryIterator) Slot() []byte {
  173. if it.accountIterator {
  174. return nil
  175. }
  176. blob, err := it.a.(*diffStorageIterator).layer.Storage(it.account, it.k)
  177. if err != nil {
  178. it.fail = err
  179. return nil
  180. }
  181. return blob
  182. }
  183. // Release recursively releases all the iterators in the stack.
  184. func (it *binaryIterator) Release() {
  185. it.a.Release()
  186. it.b.Release()
  187. }
  188. // newBinaryAccountIterator creates a simplistic account iterator to step over
  189. // all the accounts in a slow, but easily verifiable way.
  190. func (dl *diffLayer) newBinaryAccountIterator() AccountIterator {
  191. iter := dl.initBinaryAccountIterator()
  192. return iter.(AccountIterator)
  193. }
  194. // newBinaryStorageIterator creates a simplistic account iterator to step over
  195. // all the storage slots in a slow, but easily verifiable way.
  196. func (dl *diffLayer) newBinaryStorageIterator(account common.Hash) StorageIterator {
  197. iter := dl.initBinaryStorageIterator(account)
  198. return iter.(StorageIterator)
  199. }