trie_test.go 7.9 KB

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