iterator_binary.go 3.5 KB

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