secure_trie_test.go 4.0 KB

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