util_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 trie
  17. import (
  18. "testing"
  19. "github.com/ethereum/go-ethereum/common"
  20. "github.com/ethereum/go-ethereum/core/rawdb"
  21. )
  22. // Tests if the trie diffs are tracked correctly.
  23. func TestTrieTracer(t *testing.T) {
  24. db := NewDatabase(rawdb.NewMemoryDatabase())
  25. trie := NewEmpty(db)
  26. trie.tracer = newTracer()
  27. // Insert a batch of entries, all the nodes should be marked as inserted
  28. vals := []struct{ k, v string }{
  29. {"do", "verb"},
  30. {"ether", "wookiedoo"},
  31. {"horse", "stallion"},
  32. {"shaman", "horse"},
  33. {"doge", "coin"},
  34. {"dog", "puppy"},
  35. {"somethingveryoddindeedthis is", "myothernodedata"},
  36. }
  37. for _, val := range vals {
  38. trie.Update([]byte(val.k), []byte(val.v))
  39. }
  40. trie.Hash()
  41. seen := make(map[string]struct{})
  42. it := trie.NodeIterator(nil)
  43. for it.Next(true) {
  44. if it.Leaf() {
  45. continue
  46. }
  47. seen[string(it.Path())] = struct{}{}
  48. }
  49. inserted := trie.tracer.insertList()
  50. if len(inserted) != len(seen) {
  51. t.Fatalf("Unexpected inserted node tracked want %d got %d", len(seen), len(inserted))
  52. }
  53. for _, k := range inserted {
  54. _, ok := seen[string(k)]
  55. if !ok {
  56. t.Fatalf("Unexpected inserted node")
  57. }
  58. }
  59. deleted := trie.tracer.deleteList()
  60. if len(deleted) != 0 {
  61. t.Fatalf("Unexpected deleted node tracked %d", len(deleted))
  62. }
  63. // Commit the changes and re-create with new root
  64. root, nodes, _ := trie.Commit(false)
  65. db.Update(NewWithNodeSet(nodes))
  66. trie, _ = New(common.Hash{}, root, db)
  67. trie.tracer = newTracer()
  68. // Delete all the elements, check deletion set
  69. for _, val := range vals {
  70. trie.Delete([]byte(val.k))
  71. }
  72. trie.Hash()
  73. inserted = trie.tracer.insertList()
  74. if len(inserted) != 0 {
  75. t.Fatalf("Unexpected inserted node tracked %d", len(inserted))
  76. }
  77. deleted = trie.tracer.deleteList()
  78. if len(deleted) != len(seen) {
  79. t.Fatalf("Unexpected deleted node tracked want %d got %d", len(seen), len(deleted))
  80. }
  81. for _, k := range deleted {
  82. _, ok := seen[string(k)]
  83. if !ok {
  84. t.Fatalf("Unexpected inserted node")
  85. }
  86. }
  87. }
  88. func TestTrieTracerNoop(t *testing.T) {
  89. trie := NewEmpty(NewDatabase(rawdb.NewMemoryDatabase()))
  90. trie.tracer = newTracer()
  91. // Insert a batch of entries, all the nodes should be marked as inserted
  92. vals := []struct{ k, v string }{
  93. {"do", "verb"},
  94. {"ether", "wookiedoo"},
  95. {"horse", "stallion"},
  96. {"shaman", "horse"},
  97. {"doge", "coin"},
  98. {"dog", "puppy"},
  99. {"somethingveryoddindeedthis is", "myothernodedata"},
  100. }
  101. for _, val := range vals {
  102. trie.Update([]byte(val.k), []byte(val.v))
  103. }
  104. for _, val := range vals {
  105. trie.Delete([]byte(val.k))
  106. }
  107. if len(trie.tracer.insertList()) != 0 {
  108. t.Fatalf("Unexpected inserted node tracked %d", len(trie.tracer.insertList()))
  109. }
  110. if len(trie.tracer.deleteList()) != 0 {
  111. t.Fatalf("Unexpected deleted node tracked %d", len(trie.tracer.deleteList()))
  112. }
  113. }