github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/trie/secure_trie_test.go (about) 1 // This file is part of the go-sberex library. The go-sberex library is 2 // free software: you can redistribute it and/or modify it under the terms 3 // of the GNU Lesser General Public License as published by the Free 4 // Software Foundation, either version 3 of the License, or (at your option) 5 // any later version. 6 // 7 // The go-sberex library is distributed in the hope that it will be useful, 8 // but WITHOUT ANY WARRANTY; without even the implied warranty of 9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 10 // General Public License <http://www.gnu.org/licenses/> for more details. 11 12 package trie 13 14 import ( 15 "bytes" 16 "runtime" 17 "sync" 18 "testing" 19 20 "github.com/Sberex/go-sberex/common" 21 "github.com/Sberex/go-sberex/crypto" 22 "github.com/Sberex/go-sberex/ethdb" 23 ) 24 25 func newEmptySecure() *SecureTrie { 26 diskdb, _ := ethdb.NewMemDatabase() 27 triedb := NewDatabase(diskdb) 28 29 trie, _ := NewSecure(common.Hash{}, triedb, 0) 30 return trie 31 } 32 33 // makeTestSecureTrie creates a large enough secure trie for testing. 34 func makeTestSecureTrie() (*Database, *SecureTrie, map[string][]byte) { 35 // Create an empty trie 36 diskdb, _ := ethdb.NewMemDatabase() 37 triedb := NewDatabase(diskdb) 38 39 trie, _ := NewSecure(common.Hash{}, triedb, 0) 40 41 // Fill it with some arbitrary data 42 content := make(map[string][]byte) 43 for i := byte(0); i < 255; i++ { 44 // Map the same data under multiple keys 45 key, val := common.LeftPadBytes([]byte{1, i}, 32), []byte{i} 46 content[string(key)] = val 47 trie.Update(key, val) 48 49 key, val = common.LeftPadBytes([]byte{2, i}, 32), []byte{i} 50 content[string(key)] = val 51 trie.Update(key, val) 52 53 // Add some other data to inflate the trie 54 for j := byte(3); j < 13; j++ { 55 key, val = common.LeftPadBytes([]byte{j, i}, 32), []byte{j, i} 56 content[string(key)] = val 57 trie.Update(key, val) 58 } 59 } 60 trie.Commit(nil) 61 62 // Return the generated trie 63 return triedb, trie, content 64 } 65 66 func TestSecureDelete(t *testing.T) { 67 trie := newEmptySecure() 68 vals := []struct{ k, v string }{ 69 {"do", "verb"}, 70 {"sbr", "wookiedoo"}, 71 {"horse", "stallion"}, 72 {"shaman", "horse"}, 73 {"doge", "coin"}, 74 {"sbr", ""}, 75 {"dog", "puppy"}, 76 {"shaman", ""}, 77 } 78 for _, val := range vals { 79 if val.v != "" { 80 trie.Update([]byte(val.k), []byte(val.v)) 81 } else { 82 trie.Delete([]byte(val.k)) 83 } 84 } 85 hash := trie.Hash() 86 exp := common.HexToHash("29b235a58c3c25ab83010c327d5932bcf05324b7d6b1185e650798034783ca9d") 87 if hash != exp { 88 t.Errorf("expected %x got %x", exp, hash) 89 } 90 } 91 92 func TestSecureGetKey(t *testing.T) { 93 trie := newEmptySecure() 94 trie.Update([]byte("foo"), []byte("bar")) 95 96 key := []byte("foo") 97 value := []byte("bar") 98 seckey := crypto.Keccak256(key) 99 100 if !bytes.Equal(trie.Get(key), value) { 101 t.Errorf("Get did not return bar") 102 } 103 if k := trie.GetKey(seckey); !bytes.Equal(k, key) { 104 t.Errorf("GetKey returned %q, want %q", k, key) 105 } 106 } 107 108 func TestSecureTrieConcurrency(t *testing.T) { 109 // Create an initial trie and copy if for concurrent access 110 _, trie, _ := makeTestSecureTrie() 111 112 threads := runtime.NumCPU() 113 tries := make([]*SecureTrie, threads) 114 for i := 0; i < threads; i++ { 115 cpy := *trie 116 tries[i] = &cpy 117 } 118 // Start a batch of goroutines interactng with the trie 119 pend := new(sync.WaitGroup) 120 pend.Add(threads) 121 for i := 0; i < threads; i++ { 122 go func(index int) { 123 defer pend.Done() 124 125 for j := byte(0); j < 255; j++ { 126 // Map the same data under multiple keys 127 key, val := common.LeftPadBytes([]byte{byte(index), 1, j}, 32), []byte{j} 128 tries[index].Update(key, val) 129 130 key, val = common.LeftPadBytes([]byte{byte(index), 2, j}, 32), []byte{j} 131 tries[index].Update(key, val) 132 133 // Add some other data to inflate the trie 134 for k := byte(3); k < 13; k++ { 135 key, val = common.LeftPadBytes([]byte{byte(index), k, j}, 32), []byte{k, j} 136 tries[index].Update(key, val) 137 } 138 } 139 tries[index].Commit(nil) 140 }(i) 141 } 142 // Wait for all threads to finish 143 pend.Wait() 144 }