iterator_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. func TestDifferenceIterator(t *testing.T) {
  106. triea := newEmpty()
  107. valsa := []struct{ k, v string }{
  108. {"bar", "b"},
  109. {"barb", "ba"},
  110. {"bars", "bb"},
  111. {"bard", "bc"},
  112. {"fab", "z"},
  113. {"foo", "a"},
  114. {"food", "ab"},
  115. {"foos", "aa"},
  116. }
  117. for _, val := range valsa {
  118. triea.Update([]byte(val.k), []byte(val.v))
  119. }
  120. triea.Commit()
  121. trieb := newEmpty()
  122. valsb := []struct{ k, v string }{
  123. {"aardvark", "c"},
  124. {"bar", "b"},
  125. {"barb", "bd"},
  126. {"bars", "be"},
  127. {"fab", "z"},
  128. {"foo", "a"},
  129. {"foos", "aa"},
  130. {"food", "ab"},
  131. {"jars", "d"},
  132. }
  133. for _, val := range valsb {
  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 %q want %q", 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. }