iterator_binary.go 3.4 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.newAccountIterator()
  39. }
  40. l := &binaryAccountIterator{
  41. a: dl.newAccountIterator(),
  42. b: parent.newBinaryAccountIterator(),
  43. }
  44. l.aDone = !l.a.Next()
  45. l.bDone = !l.b.Next()
  46. return l
  47. }
  48. // Seek steps the iterator forward as many elements as needed, so that after
  49. // calling Next(), the iterator will be at a key higher than the given hash.
  50. func (it *binaryAccountIterator) Seek(key common.Hash) {
  51. panic("todo: implement")
  52. }
  53. // Next steps the iterator forward one element, returning false if exhausted,
  54. // or an error if iteration failed for some reason (e.g. root being iterated
  55. // becomes stale and garbage collected).
  56. func (it *binaryAccountIterator) Next() bool {
  57. if it.aDone && it.bDone {
  58. return false
  59. }
  60. nextB := it.b.Key()
  61. first:
  62. nextA := it.a.Key()
  63. if it.aDone {
  64. it.bDone = !it.b.Next()
  65. it.k = nextB
  66. return true
  67. }
  68. if it.bDone {
  69. it.aDone = !it.a.Next()
  70. it.k = nextA
  71. return true
  72. }
  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. // Key returns the hash of the account the iterator is currently at.
  92. func (it *binaryAccountIterator) Key() common.Hash {
  93. return it.k
  94. }
  95. // Value 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) Value() []byte {
  99. blob, err := it.a.layer.AccountRLP(it.k)
  100. if err != nil {
  101. it.fail = err
  102. return nil
  103. }
  104. return blob
  105. }