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