secure_trie_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 2015 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. "fmt"
  20. "runtime"
  21. "sync"
  22. "testing"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/crypto"
  25. "github.com/ethereum/go-ethereum/ethdb/memorydb"
  26. )
  27. func newEmptySecure() *StateTrie {
  28. trie, _ := NewStateTrie(common.Hash{}, common.Hash{}, NewDatabase(memorydb.New()))
  29. return trie
  30. }
  31. // makeTestStateTrie creates a large enough secure trie for testing.
  32. func makeTestStateTrie() (*Database, *StateTrie, map[string][]byte) {
  33. // Create an empty trie
  34. triedb := NewDatabase(memorydb.New())
  35. trie, _ := NewStateTrie(common.Hash{}, common.Hash{}, triedb)
  36. // Fill it with some arbitrary data
  37. content := make(map[string][]byte)
  38. for i := byte(0); i < 255; i++ {
  39. // Map the same data under multiple keys
  40. key, val := common.LeftPadBytes([]byte{1, i}, 32), []byte{i}
  41. content[string(key)] = val
  42. trie.Update(key, val)
  43. key, val = common.LeftPadBytes([]byte{2, i}, 32), []byte{i}
  44. content[string(key)] = val
  45. trie.Update(key, val)
  46. // Add some other data to inflate the trie
  47. for j := byte(3); j < 13; j++ {
  48. key, val = common.LeftPadBytes([]byte{j, i}, 32), []byte{j, i}
  49. content[string(key)] = val
  50. trie.Update(key, val)
  51. }
  52. }
  53. root, nodes, err := trie.Commit(false)
  54. if err != nil {
  55. panic(fmt.Errorf("failed to commit trie %v", err))
  56. }
  57. if err := triedb.Update(NewWithNodeSet(nodes)); err != nil {
  58. panic(fmt.Errorf("failed to commit db %v", err))
  59. }
  60. // Re-create the trie based on the new state
  61. trie, _ = NewSecure(common.Hash{}, root, triedb)
  62. return triedb, trie, content
  63. }
  64. func TestSecureDelete(t *testing.T) {
  65. trie := newEmptySecure()
  66. vals := []struct{ k, v string }{
  67. {"do", "verb"},
  68. {"ether", "wookiedoo"},
  69. {"horse", "stallion"},
  70. {"shaman", "horse"},
  71. {"doge", "coin"},
  72. {"ether", ""},
  73. {"dog", "puppy"},
  74. {"shaman", ""},
  75. }
  76. for _, val := range vals {
  77. if val.v != "" {
  78. trie.Update([]byte(val.k), []byte(val.v))
  79. } else {
  80. trie.Delete([]byte(val.k))
  81. }
  82. }
  83. hash := trie.Hash()
  84. exp := common.HexToHash("29b235a58c3c25ab83010c327d5932bcf05324b7d6b1185e650798034783ca9d")
  85. if hash != exp {
  86. t.Errorf("expected %x got %x", exp, hash)
  87. }
  88. }
  89. func TestSecureGetKey(t *testing.T) {
  90. trie := newEmptySecure()
  91. trie.Update([]byte("foo"), []byte("bar"))
  92. key := []byte("foo")
  93. value := []byte("bar")
  94. seckey := crypto.Keccak256(key)
  95. if !bytes.Equal(trie.Get(key), value) {
  96. t.Errorf("Get did not return bar")
  97. }
  98. if k := trie.GetKey(seckey); !bytes.Equal(k, key) {
  99. t.Errorf("GetKey returned %q, want %q", k, key)
  100. }
  101. }
  102. func TestStateTrieConcurrency(t *testing.T) {
  103. // Create an initial trie and copy if for concurrent access
  104. _, trie, _ := makeTestStateTrie()
  105. threads := runtime.NumCPU()
  106. tries := make([]*StateTrie, threads)
  107. for i := 0; i < threads; i++ {
  108. tries[i] = trie.Copy()
  109. }
  110. // Start a batch of goroutines interacting with the trie
  111. pend := new(sync.WaitGroup)
  112. pend.Add(threads)
  113. for i := 0; i < threads; i++ {
  114. go func(index int) {
  115. defer pend.Done()
  116. for j := byte(0); j < 255; j++ {
  117. // Map the same data under multiple keys
  118. key, val := common.LeftPadBytes([]byte{byte(index), 1, j}, 32), []byte{j}
  119. tries[index].Update(key, val)
  120. key, val = common.LeftPadBytes([]byte{byte(index), 2, j}, 32), []byte{j}
  121. tries[index].Update(key, val)
  122. // Add some other data to inflate the trie
  123. for k := byte(3); k < 13; k++ {
  124. key, val = common.LeftPadBytes([]byte{byte(index), k, j}, 32), []byte{k, j}
  125. tries[index].Update(key, val)
  126. }
  127. }
  128. tries[index].Commit(false)
  129. }(i)
  130. }
  131. // Wait for all threads to finish
  132. pend.Wait()
  133. }