holdable_iterator_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright 2022 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. "testing"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/core/rawdb"
  22. )
  23. func TestIteratorHold(t *testing.T) {
  24. // Create the key-value data store
  25. var (
  26. content = map[string]string{"k1": "v1", "k2": "v2", "k3": "v3"}
  27. order = []string{"k1", "k2", "k3"}
  28. db = rawdb.NewMemoryDatabase()
  29. )
  30. for key, val := range content {
  31. if err := db.Put([]byte(key), []byte(val)); err != nil {
  32. t.Fatalf("failed to insert item %s:%s into database: %v", key, val, err)
  33. }
  34. }
  35. // Iterate over the database with the given configs and verify the results
  36. it, idx := newHoldableIterator(db.NewIterator(nil, nil)), 0
  37. // Nothing should be affected for calling Discard on non-initialized iterator
  38. it.Hold()
  39. for it.Next() {
  40. if len(content) <= idx {
  41. t.Errorf("more items than expected: checking idx=%d (key %q), expecting len=%d", idx, it.Key(), len(order))
  42. break
  43. }
  44. if !bytes.Equal(it.Key(), []byte(order[idx])) {
  45. t.Errorf("item %d: key mismatch: have %s, want %s", idx, string(it.Key()), order[idx])
  46. }
  47. if !bytes.Equal(it.Value(), []byte(content[order[idx]])) {
  48. t.Errorf("item %d: value mismatch: have %s, want %s", idx, string(it.Value()), content[order[idx]])
  49. }
  50. // Should be safe to call discard multiple times
  51. it.Hold()
  52. it.Hold()
  53. // Shift iterator to the discarded element
  54. it.Next()
  55. if !bytes.Equal(it.Key(), []byte(order[idx])) {
  56. t.Errorf("item %d: key mismatch: have %s, want %s", idx, string(it.Key()), order[idx])
  57. }
  58. if !bytes.Equal(it.Value(), []byte(content[order[idx]])) {
  59. t.Errorf("item %d: value mismatch: have %s, want %s", idx, string(it.Value()), content[order[idx]])
  60. }
  61. // Discard/Next combo should work always
  62. it.Hold()
  63. it.Next()
  64. if !bytes.Equal(it.Key(), []byte(order[idx])) {
  65. t.Errorf("item %d: key mismatch: have %s, want %s", idx, string(it.Key()), order[idx])
  66. }
  67. if !bytes.Equal(it.Value(), []byte(content[order[idx]])) {
  68. t.Errorf("item %d: value mismatch: have %s, want %s", idx, string(it.Value()), content[order[idx]])
  69. }
  70. idx++
  71. }
  72. if err := it.Error(); err != nil {
  73. t.Errorf("iteration failed: %v", err)
  74. }
  75. if idx != len(order) {
  76. t.Errorf("iteration terminated prematurely: have %d, want %d", idx, len(order))
  77. }
  78. db.Close()
  79. }
  80. func TestReopenIterator(t *testing.T) {
  81. var (
  82. content = map[common.Hash]string{
  83. common.HexToHash("a1"): "v1",
  84. common.HexToHash("a2"): "v2",
  85. common.HexToHash("a3"): "v3",
  86. common.HexToHash("a4"): "v4",
  87. common.HexToHash("a5"): "v5",
  88. common.HexToHash("a6"): "v6",
  89. }
  90. order = []common.Hash{
  91. common.HexToHash("a1"),
  92. common.HexToHash("a2"),
  93. common.HexToHash("a3"),
  94. common.HexToHash("a4"),
  95. common.HexToHash("a5"),
  96. common.HexToHash("a6"),
  97. }
  98. db = rawdb.NewMemoryDatabase()
  99. )
  100. for key, val := range content {
  101. rawdb.WriteAccountSnapshot(db, key, []byte(val))
  102. }
  103. checkVal := func(it *holdableIterator, index int) {
  104. if !bytes.Equal(it.Key(), append(rawdb.SnapshotAccountPrefix, order[index].Bytes()...)) {
  105. t.Fatalf("Unexpected data entry key, want %v got %v", order[index], it.Key())
  106. }
  107. if !bytes.Equal(it.Value(), []byte(content[order[index]])) {
  108. t.Fatalf("Unexpected data entry key, want %v got %v", []byte(content[order[index]]), it.Value())
  109. }
  110. }
  111. // Iterate over the database with the given configs and verify the results
  112. ctx, idx := newGeneratorContext(&generatorStats{}, db, nil, nil), -1
  113. idx++
  114. ctx.account.Next()
  115. checkVal(ctx.account, idx)
  116. ctx.reopenIterator(snapAccount)
  117. idx++
  118. ctx.account.Next()
  119. checkVal(ctx.account, idx)
  120. // reopen twice
  121. ctx.reopenIterator(snapAccount)
  122. ctx.reopenIterator(snapAccount)
  123. idx++
  124. ctx.account.Next()
  125. checkVal(ctx.account, idx)
  126. // reopen iterator with held value
  127. ctx.account.Next()
  128. ctx.account.Hold()
  129. ctx.reopenIterator(snapAccount)
  130. idx++
  131. ctx.account.Next()
  132. checkVal(ctx.account, idx)
  133. // reopen twice iterator with held value
  134. ctx.account.Next()
  135. ctx.account.Hold()
  136. ctx.reopenIterator(snapAccount)
  137. ctx.reopenIterator(snapAccount)
  138. idx++
  139. ctx.account.Next()
  140. checkVal(ctx.account, idx)
  141. // shift to the end and reopen
  142. ctx.account.Next() // the end
  143. ctx.reopenIterator(snapAccount)
  144. ctx.account.Next()
  145. if ctx.account.Key() != nil {
  146. t.Fatal("Unexpected iterated entry")
  147. }
  148. }