iterator_test.go 6.4 KB

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