iterator_binary.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. // binaryAccountIterator is a simplistic iterator to step over the accounts in
  22. // a snapshot, which may or may npt be composed of multiple layers. Performance
  23. // wise this iterator is slow, it's meant for cross validating the fast one,
  24. type binaryAccountIterator struct {
  25. a *diffAccountIterator
  26. b AccountIterator
  27. aDone bool
  28. bDone bool
  29. k common.Hash
  30. fail error
  31. }
  32. // newBinaryAccountIterator creates a simplistic account iterator to step over
  33. // all the accounts in a slow, but eaily verifyable way.
  34. func (dl *diffLayer) newBinaryAccountIterator() AccountIterator {
  35. parent, ok := dl.parent.(*diffLayer)
  36. if !ok {
  37. // parent is the disk layer
  38. return dl.AccountIterator(common.Hash{})
  39. }
  40. l := &binaryAccountIterator{
  41. a: dl.AccountIterator(common.Hash{}).(*diffAccountIterator),
  42. b: parent.newBinaryAccountIterator(),
  43. }
  44. l.aDone = !l.a.Next()
  45. l.bDone = !l.b.Next()
  46. return l
  47. }
  48. // Next steps the iterator forward one element, returning false if exhausted,
  49. // or an error if iteration failed for some reason (e.g. root being iterated
  50. // becomes stale and garbage collected).
  51. func (it *binaryAccountIterator) Next() bool {
  52. if it.aDone && it.bDone {
  53. return false
  54. }
  55. nextB := it.b.Hash()
  56. first:
  57. nextA := it.a.Hash()
  58. if it.aDone {
  59. it.bDone = !it.b.Next()
  60. it.k = nextB
  61. return true
  62. }
  63. if it.bDone {
  64. it.aDone = !it.a.Next()
  65. it.k = nextA
  66. return true
  67. }
  68. if diff := bytes.Compare(nextA[:], nextB[:]); diff < 0 {
  69. it.aDone = !it.a.Next()
  70. it.k = nextA
  71. return true
  72. } else if diff == 0 {
  73. // Now we need to advance one of them
  74. it.aDone = !it.a.Next()
  75. goto first
  76. }
  77. it.bDone = !it.b.Next()
  78. it.k = nextB
  79. return true
  80. }
  81. // Error returns any failure that occurred during iteration, which might have
  82. // caused a premature iteration exit (e.g. snapshot stack becoming stale).
  83. func (it *binaryAccountIterator) Error() error {
  84. return it.fail
  85. }
  86. // Hash returns the hash of the account the iterator is currently at.
  87. func (it *binaryAccountIterator) Hash() common.Hash {
  88. return it.k
  89. }
  90. // Account returns the RLP encoded slim account the iterator is currently at, or
  91. // nil if the iterated snapshot stack became stale (you can check Error after
  92. // to see if it failed or not).
  93. func (it *binaryAccountIterator) Account() []byte {
  94. blob, err := it.a.layer.AccountRLP(it.k)
  95. if err != nil {
  96. it.fail = err
  97. return nil
  98. }
  99. return blob
  100. }
  101. // Release recursively releases all the iterators in the stack.
  102. func (it *binaryAccountIterator) Release() {
  103. it.a.Release()
  104. it.b.Release()
  105. }