iterator.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. "sort"
  20. "github.com/ethereum/go-ethereum/common"
  21. )
  22. // AccountIterator is an iterator to step over all the accounts in a snapshot,
  23. // which may or may npt be composed of multiple layers.
  24. type AccountIterator interface {
  25. // Seek steps the iterator forward as many elements as needed, so that after
  26. // calling Next(), the iterator will be at a key higher than the given hash.
  27. Seek(hash common.Hash)
  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. // Key returns the hash of the account the iterator is currently at.
  36. Key() common.Hash
  37. // Value 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. Value() []byte
  40. }
  41. // diffAccountIterator is an account iterator that steps over the accounts (both
  42. // live and deleted) contained within a single
  43. type diffAccountIterator struct {
  44. layer *diffLayer
  45. index int
  46. }
  47. func (dl *diffLayer) newAccountIterator() *diffAccountIterator {
  48. dl.AccountList()
  49. return &diffAccountIterator{layer: dl, index: -1}
  50. }
  51. // Seek steps the iterator forward as many elements as needed, so that after
  52. // calling Next(), the iterator will be at a key higher than the given hash.
  53. func (it *diffAccountIterator) Seek(key common.Hash) {
  54. // Search uses binary search to find and return the smallest index i
  55. // in [0, n) at which f(i) is true
  56. index := sort.Search(len(it.layer.accountList), func(i int) bool {
  57. return bytes.Compare(key[:], it.layer.accountList[i][:]) < 0
  58. })
  59. it.index = index - 1
  60. }
  61. // Next steps the iterator forward one element, returning false if exhausted.
  62. func (it *diffAccountIterator) Next() bool {
  63. if it.index < len(it.layer.accountList) {
  64. it.index++
  65. }
  66. return it.index < len(it.layer.accountList)
  67. }
  68. // Error returns any failure that occurred during iteration, which might have
  69. // caused a premature iteration exit (e.g. snapshot stack becoming stale).
  70. //
  71. // A diff layer is immutable after creation content wise and can always be fully
  72. // iterated without error, so this method always returns nil.
  73. func (it *diffAccountIterator) Error() error {
  74. return nil
  75. }
  76. // Key returns the hash of the account the iterator is currently at.
  77. func (it *diffAccountIterator) Key() common.Hash {
  78. if it.index < len(it.layer.accountList) {
  79. return it.layer.accountList[it.index]
  80. }
  81. return common.Hash{}
  82. }
  83. // Value returns the RLP encoded slim account the iterator is currently at.
  84. func (it *diffAccountIterator) Value() []byte {
  85. it.layer.lock.RLock()
  86. defer it.layer.lock.RUnlock()
  87. hash := it.layer.accountList[it.index]
  88. if data, ok := it.layer.accountData[hash]; ok {
  89. return data
  90. }
  91. panic("iterator references non-existent layer account")
  92. }
  93. func (dl *diffLayer) iterators() []AccountIterator {
  94. if parent, ok := dl.parent.(*diffLayer); ok {
  95. iterators := parent.iterators()
  96. return append(iterators, dl.newAccountIterator())
  97. }
  98. return []AccountIterator{dl.newAccountIterator()}
  99. }