iterator_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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(); {
  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. }