secure_trie_test.go 4.0 KB

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