secure_trie_test.go 4.0 KB

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