trie_test.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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. "bytes"
  19. "encoding/binary"
  20. "errors"
  21. "fmt"
  22. "io/ioutil"
  23. "math/big"
  24. "math/rand"
  25. "os"
  26. "reflect"
  27. "testing"
  28. "testing/quick"
  29. "github.com/davecgh/go-spew/spew"
  30. "github.com/ethereum/go-ethereum/common"
  31. "github.com/ethereum/go-ethereum/crypto"
  32. "github.com/ethereum/go-ethereum/ethdb"
  33. "github.com/ethereum/go-ethereum/ethdb/leveldb"
  34. "github.com/ethereum/go-ethereum/ethdb/memorydb"
  35. "github.com/ethereum/go-ethereum/rlp"
  36. )
  37. func init() {
  38. spew.Config.Indent = " "
  39. spew.Config.DisableMethods = false
  40. }
  41. // Used for testing
  42. func newEmpty() *Trie {
  43. trie, _ := New(common.Hash{}, NewDatabase(memorydb.New()))
  44. return trie
  45. }
  46. func TestEmptyTrie(t *testing.T) {
  47. var trie Trie
  48. res := trie.Hash()
  49. exp := emptyRoot
  50. if res != common.Hash(exp) {
  51. t.Errorf("expected %x got %x", exp, res)
  52. }
  53. }
  54. func TestNull(t *testing.T) {
  55. var trie Trie
  56. key := make([]byte, 32)
  57. value := []byte("test")
  58. trie.Update(key, value)
  59. if !bytes.Equal(trie.Get(key), value) {
  60. t.Fatal("wrong value")
  61. }
  62. }
  63. func TestMissingRoot(t *testing.T) {
  64. trie, err := New(common.HexToHash("0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"), NewDatabase(memorydb.New()))
  65. if trie != nil {
  66. t.Error("New returned non-nil trie for invalid root")
  67. }
  68. if _, ok := err.(*MissingNodeError); !ok {
  69. t.Errorf("New returned wrong error: %v", err)
  70. }
  71. }
  72. func TestMissingNodeDisk(t *testing.T) { testMissingNode(t, false) }
  73. func TestMissingNodeMemonly(t *testing.T) { testMissingNode(t, true) }
  74. func testMissingNode(t *testing.T, memonly bool) {
  75. diskdb := memorydb.New()
  76. triedb := NewDatabase(diskdb)
  77. trie, _ := New(common.Hash{}, triedb)
  78. updateString(trie, "120000", "qwerqwerqwerqwerqwerqwerqwerqwer")
  79. updateString(trie, "123456", "asdfasdfasdfasdfasdfasdfasdfasdf")
  80. root, _ := trie.Commit(nil)
  81. if !memonly {
  82. triedb.Commit(root, true)
  83. }
  84. trie, _ = New(root, triedb)
  85. _, err := trie.TryGet([]byte("120000"))
  86. if err != nil {
  87. t.Errorf("Unexpected error: %v", err)
  88. }
  89. trie, _ = New(root, triedb)
  90. _, err = trie.TryGet([]byte("120099"))
  91. if err != nil {
  92. t.Errorf("Unexpected error: %v", err)
  93. }
  94. trie, _ = New(root, triedb)
  95. _, err = trie.TryGet([]byte("123456"))
  96. if err != nil {
  97. t.Errorf("Unexpected error: %v", err)
  98. }
  99. trie, _ = New(root, triedb)
  100. err = trie.TryUpdate([]byte("120099"), []byte("zxcvzxcvzxcvzxcvzxcvzxcvzxcvzxcv"))
  101. if err != nil {
  102. t.Errorf("Unexpected error: %v", err)
  103. }
  104. trie, _ = New(root, triedb)
  105. err = trie.TryDelete([]byte("123456"))
  106. if err != nil {
  107. t.Errorf("Unexpected error: %v", err)
  108. }
  109. hash := common.HexToHash("0xe1d943cc8f061a0c0b98162830b970395ac9315654824bf21b73b891365262f9")
  110. if memonly {
  111. delete(triedb.dirties, hash)
  112. } else {
  113. diskdb.Delete(hash[:])
  114. }
  115. trie, _ = New(root, triedb)
  116. _, err = trie.TryGet([]byte("120000"))
  117. if _, ok := err.(*MissingNodeError); !ok {
  118. t.Errorf("Wrong error: %v", err)
  119. }
  120. trie, _ = New(root, triedb)
  121. _, err = trie.TryGet([]byte("120099"))
  122. if _, ok := err.(*MissingNodeError); !ok {
  123. t.Errorf("Wrong error: %v", err)
  124. }
  125. trie, _ = New(root, triedb)
  126. _, err = trie.TryGet([]byte("123456"))
  127. if err != nil {
  128. t.Errorf("Unexpected error: %v", err)
  129. }
  130. trie, _ = New(root, triedb)
  131. err = trie.TryUpdate([]byte("120099"), []byte("zxcv"))
  132. if _, ok := err.(*MissingNodeError); !ok {
  133. t.Errorf("Wrong error: %v", err)
  134. }
  135. trie, _ = New(root, triedb)
  136. err = trie.TryDelete([]byte("123456"))
  137. if _, ok := err.(*MissingNodeError); !ok {
  138. t.Errorf("Wrong error: %v", err)
  139. }
  140. }
  141. func TestInsert(t *testing.T) {
  142. trie := newEmpty()
  143. updateString(trie, "doe", "reindeer")
  144. updateString(trie, "dog", "puppy")
  145. updateString(trie, "dogglesworth", "cat")
  146. exp := common.HexToHash("8aad789dff2f538bca5d8ea56e8abe10f4c7ba3a5dea95fea4cd6e7c3a1168d3")
  147. root := trie.Hash()
  148. if root != exp {
  149. t.Errorf("exp %x got %x", exp, root)
  150. }
  151. trie = newEmpty()
  152. updateString(trie, "A", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
  153. exp = common.HexToHash("d23786fb4a010da3ce639d66d5e904a11dbc02746d1ce25029e53290cabf28ab")
  154. root, err := trie.Commit(nil)
  155. if err != nil {
  156. t.Fatalf("commit error: %v", err)
  157. }
  158. if root != exp {
  159. t.Errorf("exp %x got %x", exp, root)
  160. }
  161. }
  162. func TestGet(t *testing.T) {
  163. trie := newEmpty()
  164. updateString(trie, "doe", "reindeer")
  165. updateString(trie, "dog", "puppy")
  166. updateString(trie, "dogglesworth", "cat")
  167. for i := 0; i < 2; i++ {
  168. res := getString(trie, "dog")
  169. if !bytes.Equal(res, []byte("puppy")) {
  170. t.Errorf("expected puppy got %x", res)
  171. }
  172. unknown := getString(trie, "unknown")
  173. if unknown != nil {
  174. t.Errorf("expected nil got %x", unknown)
  175. }
  176. if i == 1 {
  177. return
  178. }
  179. trie.Commit(nil)
  180. }
  181. }
  182. func TestDelete(t *testing.T) {
  183. trie := newEmpty()
  184. vals := []struct{ k, v string }{
  185. {"do", "verb"},
  186. {"ether", "wookiedoo"},
  187. {"horse", "stallion"},
  188. {"shaman", "horse"},
  189. {"doge", "coin"},
  190. {"ether", ""},
  191. {"dog", "puppy"},
  192. {"shaman", ""},
  193. }
  194. for _, val := range vals {
  195. if val.v != "" {
  196. updateString(trie, val.k, val.v)
  197. } else {
  198. deleteString(trie, val.k)
  199. }
  200. }
  201. hash := trie.Hash()
  202. exp := common.HexToHash("5991bb8c6514148a29db676a14ac506cd2cd5775ace63c30a4fe457715e9ac84")
  203. if hash != exp {
  204. t.Errorf("expected %x got %x", exp, hash)
  205. }
  206. }
  207. func TestEmptyValues(t *testing.T) {
  208. trie := newEmpty()
  209. vals := []struct{ k, v string }{
  210. {"do", "verb"},
  211. {"ether", "wookiedoo"},
  212. {"horse", "stallion"},
  213. {"shaman", "horse"},
  214. {"doge", "coin"},
  215. {"ether", ""},
  216. {"dog", "puppy"},
  217. {"shaman", ""},
  218. }
  219. for _, val := range vals {
  220. updateString(trie, val.k, val.v)
  221. }
  222. hash := trie.Hash()
  223. exp := common.HexToHash("5991bb8c6514148a29db676a14ac506cd2cd5775ace63c30a4fe457715e9ac84")
  224. if hash != exp {
  225. t.Errorf("expected %x got %x", exp, hash)
  226. }
  227. }
  228. func TestReplication(t *testing.T) {
  229. trie := newEmpty()
  230. vals := []struct{ k, v string }{
  231. {"do", "verb"},
  232. {"ether", "wookiedoo"},
  233. {"horse", "stallion"},
  234. {"shaman", "horse"},
  235. {"doge", "coin"},
  236. {"dog", "puppy"},
  237. {"somethingveryoddindeedthis is", "myothernodedata"},
  238. }
  239. for _, val := range vals {
  240. updateString(trie, val.k, val.v)
  241. }
  242. exp, err := trie.Commit(nil)
  243. if err != nil {
  244. t.Fatalf("commit error: %v", err)
  245. }
  246. // create a new trie on top of the database and check that lookups work.
  247. trie2, err := New(exp, trie.db)
  248. if err != nil {
  249. t.Fatalf("can't recreate trie at %x: %v", exp, err)
  250. }
  251. for _, kv := range vals {
  252. if string(getString(trie2, kv.k)) != kv.v {
  253. t.Errorf("trie2 doesn't have %q => %q", kv.k, kv.v)
  254. }
  255. }
  256. hash, err := trie2.Commit(nil)
  257. if err != nil {
  258. t.Fatalf("commit error: %v", err)
  259. }
  260. if hash != exp {
  261. t.Errorf("root failure. expected %x got %x", exp, hash)
  262. }
  263. // perform some insertions on the new trie.
  264. vals2 := []struct{ k, v string }{
  265. {"do", "verb"},
  266. {"ether", "wookiedoo"},
  267. {"horse", "stallion"},
  268. // {"shaman", "horse"},
  269. // {"doge", "coin"},
  270. // {"ether", ""},
  271. // {"dog", "puppy"},
  272. // {"somethingveryoddindeedthis is", "myothernodedata"},
  273. // {"shaman", ""},
  274. }
  275. for _, val := range vals2 {
  276. updateString(trie2, val.k, val.v)
  277. }
  278. if hash := trie2.Hash(); hash != exp {
  279. t.Errorf("root failure. expected %x got %x", exp, hash)
  280. }
  281. }
  282. func TestLargeValue(t *testing.T) {
  283. trie := newEmpty()
  284. trie.Update([]byte("key1"), []byte{99, 99, 99, 99})
  285. trie.Update([]byte("key2"), bytes.Repeat([]byte{1}, 32))
  286. trie.Hash()
  287. }
  288. type countingDB struct {
  289. ethdb.KeyValueStore
  290. gets map[string]int
  291. }
  292. func (db *countingDB) Get(key []byte) ([]byte, error) {
  293. db.gets[string(key)]++
  294. return db.KeyValueStore.Get(key)
  295. }
  296. // TestCacheUnload checks that decoded nodes are unloaded after a
  297. // certain number of commit operations.
  298. func TestCacheUnload(t *testing.T) {
  299. // Create test trie with two branches.
  300. trie := newEmpty()
  301. key1 := "---------------------------------"
  302. key2 := "---some other branch"
  303. updateString(trie, key1, "this is the branch of key1.")
  304. updateString(trie, key2, "this is the branch of key2.")
  305. root, _ := trie.Commit(nil)
  306. trie.db.Commit(root, true)
  307. // Commit the trie repeatedly and access key1.
  308. // The branch containing it is loaded from DB exactly two times:
  309. // in the 0th and 6th iteration.
  310. diskdb := &countingDB{KeyValueStore: trie.db.diskdb, gets: make(map[string]int)}
  311. triedb := NewDatabase(diskdb)
  312. trie, _ = New(root, triedb)
  313. trie.SetCacheLimit(5)
  314. for i := 0; i < 12; i++ {
  315. getString(trie, key1)
  316. trie.Commit(nil)
  317. }
  318. // Check that it got loaded two times.
  319. for dbkey, count := range diskdb.gets {
  320. if count != 2 {
  321. t.Errorf("db key %x loaded %d times, want %d times", []byte(dbkey), count, 2)
  322. }
  323. }
  324. }
  325. // randTest performs random trie operations.
  326. // Instances of this test are created by Generate.
  327. type randTest []randTestStep
  328. type randTestStep struct {
  329. op int
  330. key []byte // for opUpdate, opDelete, opGet
  331. value []byte // for opUpdate
  332. err error // for debugging
  333. }
  334. const (
  335. opUpdate = iota
  336. opDelete
  337. opGet
  338. opCommit
  339. opHash
  340. opReset
  341. opItercheckhash
  342. opCheckCacheInvariant
  343. opMax // boundary value, not an actual op
  344. )
  345. func (randTest) Generate(r *rand.Rand, size int) reflect.Value {
  346. var allKeys [][]byte
  347. genKey := func() []byte {
  348. if len(allKeys) < 2 || r.Intn(100) < 10 {
  349. // new key
  350. key := make([]byte, r.Intn(50))
  351. r.Read(key)
  352. allKeys = append(allKeys, key)
  353. return key
  354. }
  355. // use existing key
  356. return allKeys[r.Intn(len(allKeys))]
  357. }
  358. var steps randTest
  359. for i := 0; i < size; i++ {
  360. step := randTestStep{op: r.Intn(opMax)}
  361. switch step.op {
  362. case opUpdate:
  363. step.key = genKey()
  364. step.value = make([]byte, 8)
  365. binary.BigEndian.PutUint64(step.value, uint64(i))
  366. case opGet, opDelete:
  367. step.key = genKey()
  368. }
  369. steps = append(steps, step)
  370. }
  371. return reflect.ValueOf(steps)
  372. }
  373. func runRandTest(rt randTest) bool {
  374. triedb := NewDatabase(memorydb.New())
  375. tr, _ := New(common.Hash{}, triedb)
  376. values := make(map[string]string) // tracks content of the trie
  377. for i, step := range rt {
  378. switch step.op {
  379. case opUpdate:
  380. tr.Update(step.key, step.value)
  381. values[string(step.key)] = string(step.value)
  382. case opDelete:
  383. tr.Delete(step.key)
  384. delete(values, string(step.key))
  385. case opGet:
  386. v := tr.Get(step.key)
  387. want := values[string(step.key)]
  388. if string(v) != want {
  389. rt[i].err = fmt.Errorf("mismatch for key 0x%x, got 0x%x want 0x%x", step.key, v, want)
  390. }
  391. case opCommit:
  392. _, rt[i].err = tr.Commit(nil)
  393. case opHash:
  394. tr.Hash()
  395. case opReset:
  396. hash, err := tr.Commit(nil)
  397. if err != nil {
  398. rt[i].err = err
  399. return false
  400. }
  401. newtr, err := New(hash, triedb)
  402. if err != nil {
  403. rt[i].err = err
  404. return false
  405. }
  406. tr = newtr
  407. case opItercheckhash:
  408. checktr, _ := New(common.Hash{}, triedb)
  409. it := NewIterator(tr.NodeIterator(nil))
  410. for it.Next() {
  411. checktr.Update(it.Key, it.Value)
  412. }
  413. if tr.Hash() != checktr.Hash() {
  414. rt[i].err = fmt.Errorf("hash mismatch in opItercheckhash")
  415. }
  416. case opCheckCacheInvariant:
  417. rt[i].err = checkCacheInvariant(tr.root, nil, tr.cachegen, false, 0)
  418. }
  419. // Abort the test on error.
  420. if rt[i].err != nil {
  421. return false
  422. }
  423. }
  424. return true
  425. }
  426. func checkCacheInvariant(n, parent node, parentCachegen uint16, parentDirty bool, depth int) error {
  427. var children []node
  428. var flag nodeFlag
  429. switch n := n.(type) {
  430. case *shortNode:
  431. flag = n.flags
  432. children = []node{n.Val}
  433. case *fullNode:
  434. flag = n.flags
  435. children = n.Children[:]
  436. default:
  437. return nil
  438. }
  439. errorf := func(format string, args ...interface{}) error {
  440. msg := fmt.Sprintf(format, args...)
  441. msg += fmt.Sprintf("\nat depth %d node %s", depth, spew.Sdump(n))
  442. msg += fmt.Sprintf("parent: %s", spew.Sdump(parent))
  443. return errors.New(msg)
  444. }
  445. if flag.gen > parentCachegen {
  446. return errorf("cache invariant violation: %d > %d\n", flag.gen, parentCachegen)
  447. }
  448. if depth > 0 && !parentDirty && flag.dirty {
  449. return errorf("cache invariant violation: %d > %d\n", flag.gen, parentCachegen)
  450. }
  451. for _, child := range children {
  452. if err := checkCacheInvariant(child, n, flag.gen, flag.dirty, depth+1); err != nil {
  453. return err
  454. }
  455. }
  456. return nil
  457. }
  458. func TestRandom(t *testing.T) {
  459. if err := quick.Check(runRandTest, nil); err != nil {
  460. if cerr, ok := err.(*quick.CheckError); ok {
  461. t.Fatalf("random test iteration %d failed: %s", cerr.Count, spew.Sdump(cerr.In))
  462. }
  463. t.Fatal(err)
  464. }
  465. }
  466. func BenchmarkGet(b *testing.B) { benchGet(b, false) }
  467. func BenchmarkGetDB(b *testing.B) { benchGet(b, true) }
  468. func BenchmarkUpdateBE(b *testing.B) { benchUpdate(b, binary.BigEndian) }
  469. func BenchmarkUpdateLE(b *testing.B) { benchUpdate(b, binary.LittleEndian) }
  470. const benchElemCount = 20000
  471. func benchGet(b *testing.B, commit bool) {
  472. trie := new(Trie)
  473. if commit {
  474. _, tmpdb := tempDB()
  475. trie, _ = New(common.Hash{}, tmpdb)
  476. }
  477. k := make([]byte, 32)
  478. for i := 0; i < benchElemCount; i++ {
  479. binary.LittleEndian.PutUint64(k, uint64(i))
  480. trie.Update(k, k)
  481. }
  482. binary.LittleEndian.PutUint64(k, benchElemCount/2)
  483. if commit {
  484. trie.Commit(nil)
  485. }
  486. b.ResetTimer()
  487. for i := 0; i < b.N; i++ {
  488. trie.Get(k)
  489. }
  490. b.StopTimer()
  491. if commit {
  492. ldb := trie.db.diskdb.(*leveldb.LevelDBDatabase)
  493. ldb.Close()
  494. os.RemoveAll(ldb.Path())
  495. }
  496. }
  497. func benchUpdate(b *testing.B, e binary.ByteOrder) *Trie {
  498. trie := newEmpty()
  499. k := make([]byte, 32)
  500. for i := 0; i < b.N; i++ {
  501. e.PutUint64(k, uint64(i))
  502. trie.Update(k, k)
  503. }
  504. return trie
  505. }
  506. // Benchmarks the trie hashing. Since the trie caches the result of any operation,
  507. // we cannot use b.N as the number of hashing rouns, since all rounds apart from
  508. // the first one will be NOOP. As such, we'll use b.N as the number of account to
  509. // insert into the trie before measuring the hashing.
  510. func BenchmarkHash(b *testing.B) {
  511. // Make the random benchmark deterministic
  512. random := rand.New(rand.NewSource(0))
  513. // Create a realistic account trie to hash
  514. addresses := make([][20]byte, b.N)
  515. for i := 0; i < len(addresses); i++ {
  516. for j := 0; j < len(addresses[i]); j++ {
  517. addresses[i][j] = byte(random.Intn(256))
  518. }
  519. }
  520. accounts := make([][]byte, len(addresses))
  521. for i := 0; i < len(accounts); i++ {
  522. var (
  523. nonce = uint64(random.Int63())
  524. balance = new(big.Int).Rand(random, new(big.Int).Exp(common.Big2, common.Big256, nil))
  525. root = emptyRoot
  526. code = crypto.Keccak256(nil)
  527. )
  528. accounts[i], _ = rlp.EncodeToBytes([]interface{}{nonce, balance, root, code})
  529. }
  530. // Insert the accounts into the trie and hash it
  531. trie := newEmpty()
  532. for i := 0; i < len(addresses); i++ {
  533. trie.Update(crypto.Keccak256(addresses[i][:]), accounts[i])
  534. }
  535. b.ResetTimer()
  536. b.ReportAllocs()
  537. trie.Hash()
  538. }
  539. func tempDB() (string, *Database) {
  540. dir, err := ioutil.TempDir("", "trie-bench")
  541. if err != nil {
  542. panic(fmt.Sprintf("can't create temporary directory: %v", err))
  543. }
  544. diskdb, err := leveldb.New(dir, 256, 0, "")
  545. if err != nil {
  546. panic(fmt.Sprintf("can't create temporary database: %v", err))
  547. }
  548. return dir, NewDatabase(diskdb)
  549. }
  550. func getString(trie *Trie, k string) []byte {
  551. return trie.Get([]byte(k))
  552. }
  553. func updateString(trie *Trie, k, v string) {
  554. trie.Update([]byte(k), []byte(v))
  555. }
  556. func deleteString(trie *Trie, k string) {
  557. trie.Delete([]byte(k))
  558. }
  559. func TestDecodeNode(t *testing.T) {
  560. t.Parallel()
  561. var (
  562. hash = make([]byte, 20)
  563. elems = make([]byte, 20)
  564. )
  565. for i := 0; i < 5000000; i++ {
  566. rand.Read(hash)
  567. rand.Read(elems)
  568. decodeNode(hash, elems, 1)
  569. }
  570. }