github.com/mprishchepo/go-ethereum@v1.9.7-0.20191031044858-21506be82b68/trie/secure_trie_test.go (about) 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 17 package trie 18 19 import ( 20 "bytes" 21 "runtime" 22 "sync" 23 "testing" 24 25 "github.com/Fantom-foundation/go-ethereum/common" 26 "github.com/Fantom-foundation/go-ethereum/crypto" 27 "github.com/Fantom-foundation/go-ethereum/ethdb/memorydb" 28 ) 29 30 func newEmptySecure() *SecureTrie { 31 trie, _ := NewSecure(common.Hash{}, NewDatabase(memorydb.New())) 32 return trie 33 } 34 35 // makeTestSecureTrie creates a large enough secure trie for testing. 36 func makeTestSecureTrie() (*Database, *SecureTrie, map[string][]byte) { 37 // Create an empty trie 38 triedb := NewDatabase(memorydb.New()) 39 trie, _ := NewSecure(common.Hash{}, triedb) 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 {"ether", "wookiedoo"}, 71 {"horse", "stallion"}, 72 {"shaman", "horse"}, 73 {"doge", "coin"}, 74 {"ether", ""}, 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 }