trie_test.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. package trie
  2. import (
  3. "bytes"
  4. "fmt"
  5. "testing"
  6. "github.com/ethereum/go-ethereum/common"
  7. "github.com/ethereum/go-ethereum/crypto"
  8. )
  9. type Db map[string][]byte
  10. func (self Db) Get(k []byte) ([]byte, error) { return self[string(k)], nil }
  11. func (self Db) Put(k, v []byte) error { self[string(k)] = v; return nil }
  12. // Used for testing
  13. func NewEmpty() *Trie {
  14. return New(nil, make(Db))
  15. }
  16. func NewEmptySecure() *SecureTrie {
  17. return NewSecure(nil, make(Db))
  18. }
  19. func TestEmptyTrie(t *testing.T) {
  20. trie := NewEmpty()
  21. res := trie.Hash()
  22. exp := crypto.Sha3(common.Encode(""))
  23. if !bytes.Equal(res, exp) {
  24. t.Errorf("expected %x got %x", exp, res)
  25. }
  26. }
  27. func TestNull(t *testing.T) {
  28. trie := NewEmpty()
  29. key := make([]byte, 32)
  30. value := common.FromHex("0x823140710bf13990e4500136726d8b55")
  31. trie.Update(key, value)
  32. value = trie.Get(key)
  33. }
  34. func TestInsert(t *testing.T) {
  35. trie := NewEmpty()
  36. trie.UpdateString("doe", "reindeer")
  37. trie.UpdateString("dog", "puppy")
  38. trie.UpdateString("dogglesworth", "cat")
  39. exp := common.Hex2Bytes("8aad789dff2f538bca5d8ea56e8abe10f4c7ba3a5dea95fea4cd6e7c3a1168d3")
  40. root := trie.Hash()
  41. if !bytes.Equal(root, exp) {
  42. t.Errorf("exp %x got %x", exp, root)
  43. }
  44. trie = NewEmpty()
  45. trie.UpdateString("A", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
  46. exp = common.Hex2Bytes("d23786fb4a010da3ce639d66d5e904a11dbc02746d1ce25029e53290cabf28ab")
  47. root = trie.Hash()
  48. if !bytes.Equal(root, exp) {
  49. t.Errorf("exp %x got %x", exp, root)
  50. }
  51. }
  52. func TestGet(t *testing.T) {
  53. trie := NewEmpty()
  54. trie.UpdateString("doe", "reindeer")
  55. trie.UpdateString("dog", "puppy")
  56. trie.UpdateString("dogglesworth", "cat")
  57. res := trie.GetString("dog")
  58. if !bytes.Equal(res, []byte("puppy")) {
  59. t.Errorf("expected puppy got %x", res)
  60. }
  61. unknown := trie.GetString("unknown")
  62. if unknown != nil {
  63. t.Errorf("expected nil got %x", unknown)
  64. }
  65. }
  66. func TestDelete(t *testing.T) {
  67. trie := NewEmpty()
  68. vals := []struct{ k, v string }{
  69. {"do", "verb"},
  70. {"ether", "wookiedoo"},
  71. {"horse", "stallion"},
  72. {"shaman", "horse"},
  73. {"doge", "coin"},
  74. {"ether", ""},
  75. {"dog", "puppy"},
  76. {"shaman", ""},
  77. }
  78. for _, val := range vals {
  79. if val.v != "" {
  80. trie.UpdateString(val.k, val.v)
  81. } else {
  82. trie.DeleteString(val.k)
  83. }
  84. }
  85. hash := trie.Hash()
  86. exp := common.Hex2Bytes("5991bb8c6514148a29db676a14ac506cd2cd5775ace63c30a4fe457715e9ac84")
  87. if !bytes.Equal(hash, exp) {
  88. t.Errorf("expected %x got %x", exp, hash)
  89. }
  90. }
  91. func TestEmptyValues(t *testing.T) {
  92. trie := NewEmpty()
  93. vals := []struct{ k, v string }{
  94. {"do", "verb"},
  95. {"ether", "wookiedoo"},
  96. {"horse", "stallion"},
  97. {"shaman", "horse"},
  98. {"doge", "coin"},
  99. {"ether", ""},
  100. {"dog", "puppy"},
  101. {"shaman", ""},
  102. }
  103. for _, val := range vals {
  104. trie.UpdateString(val.k, val.v)
  105. }
  106. hash := trie.Hash()
  107. exp := common.Hex2Bytes("5991bb8c6514148a29db676a14ac506cd2cd5775ace63c30a4fe457715e9ac84")
  108. if !bytes.Equal(hash, exp) {
  109. t.Errorf("expected %x got %x", exp, hash)
  110. }
  111. }
  112. func TestReplication(t *testing.T) {
  113. trie := NewEmpty()
  114. vals := []struct{ k, v string }{
  115. {"do", "verb"},
  116. {"ether", "wookiedoo"},
  117. {"horse", "stallion"},
  118. {"shaman", "horse"},
  119. {"doge", "coin"},
  120. {"ether", ""},
  121. {"dog", "puppy"},
  122. {"shaman", ""},
  123. {"somethingveryoddindeedthis is", "myothernodedata"},
  124. }
  125. for _, val := range vals {
  126. trie.UpdateString(val.k, val.v)
  127. }
  128. trie.Commit()
  129. trie2 := New(trie.Root(), trie.cache.backend)
  130. if string(trie2.GetString("horse")) != "stallion" {
  131. t.Error("expected to have horse => stallion")
  132. }
  133. hash := trie2.Hash()
  134. exp := trie.Hash()
  135. if !bytes.Equal(hash, exp) {
  136. t.Errorf("root failure. expected %x got %x", exp, hash)
  137. }
  138. }
  139. func TestReset(t *testing.T) {
  140. trie := NewEmpty()
  141. vals := []struct{ k, v string }{
  142. {"do", "verb"},
  143. {"ether", "wookiedoo"},
  144. {"horse", "stallion"},
  145. }
  146. for _, val := range vals {
  147. trie.UpdateString(val.k, val.v)
  148. }
  149. trie.Commit()
  150. before := common.CopyBytes(trie.roothash)
  151. trie.UpdateString("should", "revert")
  152. trie.Hash()
  153. // Should have no effect
  154. trie.Hash()
  155. trie.Hash()
  156. // ###
  157. trie.Reset()
  158. after := common.CopyBytes(trie.roothash)
  159. if !bytes.Equal(before, after) {
  160. t.Errorf("expected roots to be equal. %x - %x", before, after)
  161. }
  162. }
  163. func TestParanoia(t *testing.T) {
  164. t.Skip()
  165. trie := NewEmpty()
  166. vals := []struct{ k, v string }{
  167. {"do", "verb"},
  168. {"ether", "wookiedoo"},
  169. {"horse", "stallion"},
  170. {"shaman", "horse"},
  171. {"doge", "coin"},
  172. {"ether", ""},
  173. {"dog", "puppy"},
  174. {"shaman", ""},
  175. {"somethingveryoddindeedthis is", "myothernodedata"},
  176. }
  177. for _, val := range vals {
  178. trie.UpdateString(val.k, val.v)
  179. }
  180. trie.Commit()
  181. ok, t2 := ParanoiaCheck(trie, trie.cache.backend)
  182. if !ok {
  183. t.Errorf("trie paranoia check failed %x %x", trie.roothash, t2.roothash)
  184. }
  185. }
  186. // Not an actual test
  187. func TestOutput(t *testing.T) {
  188. t.Skip()
  189. base := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
  190. trie := NewEmpty()
  191. for i := 0; i < 50; i++ {
  192. trie.UpdateString(fmt.Sprintf("%s%d", base, i), "valueeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee")
  193. }
  194. fmt.Println("############################## FULL ################################")
  195. fmt.Println(trie.root)
  196. trie.Commit()
  197. fmt.Println("############################## SMALL ################################")
  198. trie2 := New(trie.roothash, trie.cache.backend)
  199. trie2.GetString(base + "20")
  200. fmt.Println(trie2.root)
  201. }
  202. func BenchmarkGets(b *testing.B) {
  203. trie := NewEmpty()
  204. vals := []struct{ k, v string }{
  205. {"do", "verb"},
  206. {"ether", "wookiedoo"},
  207. {"horse", "stallion"},
  208. {"shaman", "horse"},
  209. {"doge", "coin"},
  210. {"ether", ""},
  211. {"dog", "puppy"},
  212. {"shaman", ""},
  213. {"somethingveryoddindeedthis is", "myothernodedata"},
  214. }
  215. for _, val := range vals {
  216. trie.UpdateString(val.k, val.v)
  217. }
  218. b.ResetTimer()
  219. for i := 0; i < b.N; i++ {
  220. trie.Get([]byte("horse"))
  221. }
  222. }
  223. func BenchmarkUpdate(b *testing.B) {
  224. trie := NewEmpty()
  225. b.ResetTimer()
  226. for i := 0; i < b.N; i++ {
  227. trie.UpdateString(fmt.Sprintf("aaaaaaaaa%d", i), "value")
  228. }
  229. trie.Hash()
  230. }
  231. type kv struct {
  232. k, v []byte
  233. t bool
  234. }
  235. func TestLargeData(t *testing.T) {
  236. trie := NewEmpty()
  237. vals := make(map[string]*kv)
  238. for i := byte(0); i < 255; i++ {
  239. value := &kv{common.LeftPadBytes([]byte{i}, 32), []byte{i}, false}
  240. value2 := &kv{common.LeftPadBytes([]byte{10, i}, 32), []byte{i}, false}
  241. trie.Update(value.k, value.v)
  242. trie.Update(value2.k, value2.v)
  243. vals[string(value.k)] = value
  244. vals[string(value2.k)] = value2
  245. }
  246. it := trie.Iterator()
  247. for it.Next() {
  248. vals[string(it.Key)].t = true
  249. }
  250. var untouched []*kv
  251. for _, value := range vals {
  252. if !value.t {
  253. untouched = append(untouched, value)
  254. }
  255. }
  256. if len(untouched) > 0 {
  257. t.Errorf("Missed %d nodes", len(untouched))
  258. for _, value := range untouched {
  259. t.Error(value)
  260. }
  261. }
  262. }
  263. func TestSecureDelete(t *testing.T) {
  264. trie := NewEmptySecure()
  265. vals := []struct{ k, v string }{
  266. {"do", "verb"},
  267. {"ether", "wookiedoo"},
  268. {"horse", "stallion"},
  269. {"shaman", "horse"},
  270. {"doge", "coin"},
  271. {"ether", ""},
  272. {"dog", "puppy"},
  273. {"shaman", ""},
  274. }
  275. for _, val := range vals {
  276. if val.v != "" {
  277. trie.UpdateString(val.k, val.v)
  278. } else {
  279. trie.DeleteString(val.k)
  280. }
  281. }
  282. hash := trie.Hash()
  283. exp := common.Hex2Bytes("29b235a58c3c25ab83010c327d5932bcf05324b7d6b1185e650798034783ca9d")
  284. if !bytes.Equal(hash, exp) {
  285. t.Errorf("expected %x got %x", exp, hash)
  286. }
  287. }