github.com/carter-ya/go-ethereum@v0.0.0-20230628080049-d2309be3983b/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 "fmt" 22 "runtime" 23 "sync" 24 "testing" 25 26 "github.com/ethereum/go-ethereum/common" 27 "github.com/ethereum/go-ethereum/crypto" 28 "github.com/ethereum/go-ethereum/ethdb/memorydb" 29 ) 30 31 func newEmptySecure() *StateTrie { 32 trie, _ := NewStateTrie(TrieID(common.Hash{}), NewDatabase(memorydb.New())) 33 return trie 34 } 35 36 // makeTestStateTrie creates a large enough secure trie for testing. 37 func makeTestStateTrie() (*Database, *StateTrie, map[string][]byte) { 38 // Create an empty trie 39 triedb := NewDatabase(memorydb.New()) 40 trie, _ := NewStateTrie(TrieID(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 root, nodes, err := trie.Commit(false) 62 if err != nil { 63 panic(fmt.Errorf("failed to commit trie %v", err)) 64 } 65 if err := triedb.Update(NewWithNodeSet(nodes)); err != nil { 66 panic(fmt.Errorf("failed to commit db %v", err)) 67 } 68 // Re-create the trie based on the new state 69 trie, _ = NewStateTrie(TrieID(root), triedb) 70 return triedb, trie, content 71 } 72 73 func TestSecureDelete(t *testing.T) { 74 trie := newEmptySecure() 75 vals := []struct{ k, v string }{ 76 {"do", "verb"}, 77 {"ether", "wookiedoo"}, 78 {"horse", "stallion"}, 79 {"shaman", "horse"}, 80 {"doge", "coin"}, 81 {"ether", ""}, 82 {"dog", "puppy"}, 83 {"shaman", ""}, 84 } 85 for _, val := range vals { 86 if val.v != "" { 87 trie.Update([]byte(val.k), []byte(val.v)) 88 } else { 89 trie.Delete([]byte(val.k)) 90 } 91 } 92 hash := trie.Hash() 93 exp := common.HexToHash("29b235a58c3c25ab83010c327d5932bcf05324b7d6b1185e650798034783ca9d") 94 if hash != exp { 95 t.Errorf("expected %x got %x", exp, hash) 96 } 97 } 98 99 func TestSecureGetKey(t *testing.T) { 100 trie := newEmptySecure() 101 trie.Update([]byte("foo"), []byte("bar")) 102 103 key := []byte("foo") 104 value := []byte("bar") 105 seckey := crypto.Keccak256(key) 106 107 if !bytes.Equal(trie.Get(key), value) { 108 t.Errorf("Get did not return bar") 109 } 110 if k := trie.GetKey(seckey); !bytes.Equal(k, key) { 111 t.Errorf("GetKey returned %q, want %q", k, key) 112 } 113 } 114 115 func TestStateTrieConcurrency(t *testing.T) { 116 // Create an initial trie and copy if for concurrent access 117 _, trie, _ := makeTestStateTrie() 118 119 threads := runtime.NumCPU() 120 tries := make([]*StateTrie, threads) 121 for i := 0; i < threads; i++ { 122 tries[i] = trie.Copy() 123 } 124 // Start a batch of goroutines interacting with the trie 125 pend := new(sync.WaitGroup) 126 pend.Add(threads) 127 for i := 0; i < threads; i++ { 128 go func(index int) { 129 defer pend.Done() 130 131 for j := byte(0); j < 255; j++ { 132 // Map the same data under multiple keys 133 key, val := common.LeftPadBytes([]byte{byte(index), 1, j}, 32), []byte{j} 134 tries[index].Update(key, val) 135 136 key, val = common.LeftPadBytes([]byte{byte(index), 2, j}, 32), []byte{j} 137 tries[index].Update(key, val) 138 139 // Add some other data to inflate the trie 140 for k := byte(3); k < 13; k++ { 141 key, val = common.LeftPadBytes([]byte{byte(index), k, j}, 32), []byte{k, j} 142 tries[index].Update(key, val) 143 } 144 } 145 tries[index].Commit(false) 146 }(i) 147 } 148 // Wait for all threads to finish 149 pend.Wait() 150 }