github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/trie/secure_trie_test.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-aigar library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 package trie 19 20 import ( 21 "bytes" 22 "runtime" 23 "sync" 24 "testing" 25 26 "github.com/AigarNetwork/aigar/common" 27 "github.com/AigarNetwork/aigar/crypto" 28 "github.com/AigarNetwork/aigar/ethdb/memorydb" 29 ) 30 31 func newEmptySecure() *SecureTrie { 32 trie, _ := NewSecure(common.Hash{}, NewDatabase(memorydb.New())) 33 return trie 34 } 35 36 // makeTestSecureTrie creates a large enough secure trie for testing. 37 func makeTestSecureTrie() (*Database, *SecureTrie, map[string][]byte) { 38 // Create an empty trie 39 triedb := NewDatabase(memorydb.New()) 40 trie, _ := NewSecure(common.Hash{}, triedb) 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(nil) 62 63 // Return the generated trie 64 return triedb, 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(nil) 141 }(i) 142 } 143 // Wait for all threads to finish 144 pend.Wait() 145 }