iterator.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. "github.com/ethereum/go-ethereum/core/rawdb"
  23. "github.com/ethereum/go-ethereum/ethdb"
  24. )
  25. // AccountIterator is an iterator to step over all the accounts in a snapshot,
  26. // which may or may npt be composed of multiple layers.
  27. type AccountIterator interface {
  28. // Next steps the iterator forward one element, returning false if exhausted,
  29. // or an error if iteration failed for some reason (e.g. root being iterated
  30. // becomes stale and garbage collected).
  31. Next() bool
  32. // Error returns any failure that occurred during iteration, which might have
  33. // caused a premature iteration exit (e.g. snapshot stack becoming stale).
  34. Error() error
  35. // Hash returns the hash of the account the iterator is currently at.
  36. Hash() common.Hash
  37. // Account returns the RLP encoded slim account the iterator is currently at.
  38. // An error will be returned if the iterator becomes invalid (e.g. snaph
  39. Account() []byte
  40. // Release releases associated resources. Release should always succeed and
  41. // can be called multiple times without causing error.
  42. Release()
  43. }
  44. // diffAccountIterator is an account iterator that steps over the accounts (both
  45. // live and deleted) contained within a single diff layer. Higher order iterators
  46. // will use the deleted accounts to skip deeper iterators.
  47. type diffAccountIterator struct {
  48. // curHash is the current hash the iterator is positioned on. The field is
  49. // explicitly tracked since the referenced diff layer might go stale after
  50. // the iterator was positioned and we don't want to fail accessing the old
  51. // hash as long as the iterator is not touched any more.
  52. curHash common.Hash
  53. // curAccount is the current value the iterator is positioned on. The field
  54. // is explicitly tracked since the referenced diff layer might go stale after
  55. // the iterator was positioned and we don't want to fail accessing the old
  56. // value as long as the iterator is not touched any more.
  57. //curAccount []byte
  58. layer *diffLayer // Live layer to retrieve values from
  59. keys []common.Hash // Keys left in the layer to iterate
  60. fail error // Any failures encountered (stale)
  61. }
  62. // AccountIterator creates an account iterator over a single diff layer.
  63. func (dl *diffLayer) AccountIterator(seek common.Hash) AccountIterator {
  64. // Seek out the requested starting account
  65. hashes := dl.AccountList()
  66. index := sort.Search(len(hashes), func(i int) bool {
  67. return bytes.Compare(seek[:], hashes[i][:]) < 0
  68. })
  69. // Assemble and returned the already seeked iterator
  70. return &diffAccountIterator{
  71. layer: dl,
  72. keys: hashes[index:],
  73. }
  74. }
  75. // Next steps the iterator forward one element, returning false if exhausted.
  76. func (it *diffAccountIterator) Next() bool {
  77. // If the iterator was already stale, consider it a programmer error. Although
  78. // we could just return false here, triggering this path would probably mean
  79. // somebody forgot to check for Error, so lets blow up instead of undefined
  80. // behavior that's hard to debug.
  81. if it.fail != nil {
  82. panic(fmt.Sprintf("called Next of failed iterator: %v", it.fail))
  83. }
  84. // Stop iterating if all keys were exhausted
  85. if len(it.keys) == 0 {
  86. return false
  87. }
  88. if it.layer.Stale() {
  89. it.fail, it.keys = ErrSnapshotStale, nil
  90. return false
  91. }
  92. // Iterator seems to be still alive, retrieve and cache the live hash
  93. it.curHash = it.keys[0]
  94. // key cached, shift the iterator and notify the user of success
  95. it.keys = it.keys[1:]
  96. return true
  97. }
  98. // Error returns any failure that occurred during iteration, which might have
  99. // caused a premature iteration exit (e.g. snapshot stack becoming stale).
  100. func (it *diffAccountIterator) Error() error {
  101. return it.fail
  102. }
  103. // Hash returns the hash of the account the iterator is currently at.
  104. func (it *diffAccountIterator) Hash() common.Hash {
  105. return it.curHash
  106. }
  107. // Account returns the RLP encoded slim account the iterator is currently at.
  108. // This method may _fail_, if the underlying layer has been flattened between
  109. // the call to Next and Acccount. That type of error will set it.Err.
  110. // This method assumes that flattening does not delete elements from
  111. // the accountdata mapping (writing nil into it is fine though), and will panic
  112. // if elements have been deleted.
  113. func (it *diffAccountIterator) Account() []byte {
  114. it.layer.lock.RLock()
  115. blob, ok := it.layer.accountData[it.curHash]
  116. if !ok {
  117. panic(fmt.Sprintf("iterator referenced non-existent account: %x", it.curHash))
  118. }
  119. it.layer.lock.RUnlock()
  120. if it.layer.Stale() {
  121. it.fail, it.keys = ErrSnapshotStale, nil
  122. }
  123. return blob
  124. }
  125. // Release is a noop for diff account iterators as there are no held resources.
  126. func (it *diffAccountIterator) Release() {}
  127. // diskAccountIterator is an account iterator that steps over the live accounts
  128. // contained within a disk layer.
  129. type diskAccountIterator struct {
  130. layer *diskLayer
  131. it ethdb.Iterator
  132. }
  133. // AccountIterator creates an account iterator over a disk layer.
  134. func (dl *diskLayer) AccountIterator(seek common.Hash) AccountIterator {
  135. return &diskAccountIterator{
  136. layer: dl,
  137. it: dl.diskdb.NewIteratorWithPrefix(append(rawdb.SnapshotAccountPrefix, seek[:]...)),
  138. }
  139. }
  140. // Next steps the iterator forward one element, returning false if exhausted.
  141. func (it *diskAccountIterator) Next() bool {
  142. // If the iterator was already exhausted, don't bother
  143. if it.it == nil {
  144. return false
  145. }
  146. // Try to advance the iterator and release it if we reahed the end
  147. if !it.it.Next() || !bytes.HasPrefix(it.it.Key(), rawdb.SnapshotAccountPrefix) {
  148. it.it.Release()
  149. it.it = nil
  150. return false
  151. }
  152. return true
  153. }
  154. // Error returns any failure that occurred during iteration, which might have
  155. // caused a premature iteration exit (e.g. snapshot stack becoming stale).
  156. //
  157. // A diff layer is immutable after creation content wise and can always be fully
  158. // iterated without error, so this method always returns nil.
  159. func (it *diskAccountIterator) Error() error {
  160. return it.it.Error()
  161. }
  162. // Hash returns the hash of the account the iterator is currently at.
  163. func (it *diskAccountIterator) Hash() common.Hash {
  164. return common.BytesToHash(it.it.Key())
  165. }
  166. // Account returns the RLP encoded slim account the iterator is currently at.
  167. func (it *diskAccountIterator) Account() []byte {
  168. return it.it.Value()
  169. }
  170. // Release releases the database snapshot held during iteration.
  171. func (it *diskAccountIterator) Release() {
  172. // The iterator is auto-released on exhaustion, so make sure it's still alive
  173. if it.it != nil {
  174. it.it.Release()
  175. it.it = nil
  176. }
  177. }