iterator_test.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // Copyright 2014 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 trie
  17. import (
  18. "testing"
  19. "github.com/ethereum/go-ethereum/common"
  20. "github.com/ethereum/go-ethereum/ethdb"
  21. )
  22. func TestIterator(t *testing.T) {
  23. trie := newEmpty()
  24. vals := []struct{ k, v string }{
  25. {"do", "verb"},
  26. {"ether", "wookiedoo"},
  27. {"horse", "stallion"},
  28. {"shaman", "horse"},
  29. {"doge", "coin"},
  30. {"dog", "puppy"},
  31. {"somethingveryoddindeedthis is", "myothernodedata"},
  32. }
  33. all := make(map[string]string)
  34. for _, val := range vals {
  35. all[val.k] = val.v
  36. trie.Update([]byte(val.k), []byte(val.v))
  37. }
  38. trie.Commit()
  39. found := make(map[string]string)
  40. it := NewIterator(trie)
  41. for it.Next() {
  42. found[string(it.Key)] = string(it.Value)
  43. }
  44. for k, v := range all {
  45. if found[k] != v {
  46. t.Errorf("iterator value mismatch for %s: got %q want %q", k, found[k], v)
  47. }
  48. }
  49. }
  50. type kv struct {
  51. k, v []byte
  52. t bool
  53. }
  54. func TestIteratorLargeData(t *testing.T) {
  55. trie := newEmpty()
  56. vals := make(map[string]*kv)
  57. for i := byte(0); i < 255; i++ {
  58. value := &kv{common.LeftPadBytes([]byte{i}, 32), []byte{i}, false}
  59. value2 := &kv{common.LeftPadBytes([]byte{10, i}, 32), []byte{i}, false}
  60. trie.Update(value.k, value.v)
  61. trie.Update(value2.k, value2.v)
  62. vals[string(value.k)] = value
  63. vals[string(value2.k)] = value2
  64. }
  65. it := NewIterator(trie)
  66. for it.Next() {
  67. vals[string(it.Key)].t = true
  68. }
  69. var untouched []*kv
  70. for _, value := range vals {
  71. if !value.t {
  72. untouched = append(untouched, value)
  73. }
  74. }
  75. if len(untouched) > 0 {
  76. t.Errorf("Missed %d nodes", len(untouched))
  77. for _, value := range untouched {
  78. t.Error(value)
  79. }
  80. }
  81. }
  82. // Tests that the node iterator indeed walks over the entire database contents.
  83. func TestNodeIteratorCoverage(t *testing.T) {
  84. // Create some arbitrary test trie to iterate
  85. db, trie, _ := makeTestTrie()
  86. // Gather all the node hashes found by the iterator
  87. hashes := make(map[common.Hash]struct{})
  88. for it := NewNodeIterator(trie); it.Next(true); {
  89. if it.Hash() != (common.Hash{}) {
  90. hashes[it.Hash()] = struct{}{}
  91. }
  92. }
  93. // Cross check the hashes and the database itself
  94. for hash := range hashes {
  95. if _, err := db.Get(hash.Bytes()); err != nil {
  96. t.Errorf("failed to retrieve reported node %x: %v", hash, err)
  97. }
  98. }
  99. for _, key := range db.(*ethdb.MemDatabase).Keys() {
  100. if _, ok := hashes[common.BytesToHash(key)]; !ok {
  101. t.Errorf("state entry not reported %x", key)
  102. }
  103. }
  104. }
  105. var testdata1 = []struct{ k, v string }{
  106. {"bar", "b"},
  107. {"barb", "ba"},
  108. {"bars", "bb"},
  109. {"bard", "bc"},
  110. {"fab", "z"},
  111. {"foo", "a"},
  112. {"food", "ab"},
  113. {"foos", "aa"},
  114. }
  115. var testdata2 = []struct{ k, v string }{
  116. {"aardvark", "c"},
  117. {"bar", "b"},
  118. {"barb", "bd"},
  119. {"bars", "be"},
  120. {"fab", "z"},
  121. {"foo", "a"},
  122. {"foos", "aa"},
  123. {"food", "ab"},
  124. {"jars", "d"},
  125. }
  126. func TestDifferenceIterator(t *testing.T) {
  127. triea := newEmpty()
  128. for _, val := range testdata1 {
  129. triea.Update([]byte(val.k), []byte(val.v))
  130. }
  131. triea.Commit()
  132. trieb := newEmpty()
  133. for _, val := range testdata2 {
  134. trieb.Update([]byte(val.k), []byte(val.v))
  135. }
  136. trieb.Commit()
  137. found := make(map[string]string)
  138. di, _ := NewDifferenceIterator(NewNodeIterator(triea), NewNodeIterator(trieb))
  139. it := NewIteratorFromNodeIterator(di)
  140. for it.Next() {
  141. found[string(it.Key)] = string(it.Value)
  142. }
  143. all := []struct{ k, v string }{
  144. {"aardvark", "c"},
  145. {"barb", "bd"},
  146. {"bars", "be"},
  147. {"jars", "d"},
  148. }
  149. for _, item := range all {
  150. if found[item.k] != item.v {
  151. t.Errorf("iterator value mismatch for %s: got %v want %v", item.k, found[item.k], item.v)
  152. }
  153. }
  154. if len(found) != len(all) {
  155. t.Errorf("iterator count mismatch: got %d values, want %d", len(found), len(all))
  156. }
  157. }
  158. func TestUnionIterator(t *testing.T) {
  159. triea := newEmpty()
  160. for _, val := range testdata1 {
  161. triea.Update([]byte(val.k), []byte(val.v))
  162. }
  163. triea.Commit()
  164. trieb := newEmpty()
  165. for _, val := range testdata2 {
  166. trieb.Update([]byte(val.k), []byte(val.v))
  167. }
  168. trieb.Commit()
  169. di, _ := NewUnionIterator([]NodeIterator{NewNodeIterator(triea), NewNodeIterator(trieb)})
  170. it := NewIteratorFromNodeIterator(di)
  171. all := []struct{ k, v string }{
  172. {"aardvark", "c"},
  173. {"barb", "bd"},
  174. {"barb", "ba"},
  175. {"bard", "bc"},
  176. {"bars", "bb"},
  177. {"bars", "be"},
  178. {"bar", "b"},
  179. {"fab", "z"},
  180. {"food", "ab"},
  181. {"foos", "aa"},
  182. {"foo", "a"},
  183. {"jars", "d"},
  184. }
  185. for i, kv := range all {
  186. if !it.Next() {
  187. t.Errorf("Iterator ends prematurely at element %d", i)
  188. }
  189. if kv.k != string(it.Key) {
  190. t.Errorf("iterator value mismatch for element %d: got key %s want %s", i, it.Key, kv.k)
  191. }
  192. if kv.v != string(it.Value) {
  193. t.Errorf("iterator value mismatch for element %d: got value %s want %s", i, it.Value, kv.v)
  194. }
  195. }
  196. if it.Next() {
  197. t.Errorf("Iterator returned extra values.")
  198. }
  199. }