github.com/tacshi/go-ethereum@v0.0.0-20230616113857-84a434e20921/tests/fuzzers/stacktrie/trie_fuzzer.go (about) 1 // Copyright 2020 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 stacktrie 18 19 import ( 20 "bytes" 21 "encoding/binary" 22 "errors" 23 "fmt" 24 "hash" 25 "io" 26 "sort" 27 28 "github.com/tacshi/go-ethereum/common" 29 "github.com/tacshi/go-ethereum/core/rawdb" 30 "github.com/tacshi/go-ethereum/crypto" 31 "github.com/tacshi/go-ethereum/ethdb" 32 "github.com/tacshi/go-ethereum/trie" 33 "golang.org/x/crypto/sha3" 34 ) 35 36 type fuzzer struct { 37 input io.Reader 38 exhausted bool 39 debugging bool 40 } 41 42 func (f *fuzzer) read(size int) []byte { 43 out := make([]byte, size) 44 if _, err := f.input.Read(out); err != nil { 45 f.exhausted = true 46 } 47 return out 48 } 49 50 func (f *fuzzer) readSlice(min, max int) []byte { 51 var a uint16 52 binary.Read(f.input, binary.LittleEndian, &a) 53 size := min + int(a)%(max-min) 54 out := make([]byte, size) 55 if _, err := f.input.Read(out); err != nil { 56 f.exhausted = true 57 } 58 return out 59 } 60 61 // spongeDb is a dummy db backend which accumulates writes in a sponge 62 type spongeDb struct { 63 sponge hash.Hash 64 debug bool 65 } 66 67 func (s *spongeDb) Has(key []byte) (bool, error) { panic("implement me") } 68 func (s *spongeDb) Get(key []byte) ([]byte, error) { return nil, errors.New("no such elem") } 69 func (s *spongeDb) Delete(key []byte) error { panic("implement me") } 70 func (s *spongeDb) NewBatch() ethdb.Batch { return &spongeBatch{s} } 71 func (s *spongeDb) NewBatchWithSize(size int) ethdb.Batch { return &spongeBatch{s} } 72 func (s *spongeDb) NewSnapshot() (ethdb.Snapshot, error) { panic("implement me") } 73 func (s *spongeDb) Stat(property string) (string, error) { panic("implement me") } 74 func (s *spongeDb) Compact(start []byte, limit []byte) error { panic("implement me") } 75 func (s *spongeDb) Close() error { return nil } 76 77 func (s *spongeDb) Put(key []byte, value []byte) error { 78 if s.debug { 79 fmt.Printf("db.Put %x : %x\n", key, value) 80 } 81 s.sponge.Write(key) 82 s.sponge.Write(value) 83 return nil 84 } 85 func (s *spongeDb) NewIterator(prefix []byte, start []byte) ethdb.Iterator { panic("implement me") } 86 87 // spongeBatch is a dummy batch which immediately writes to the underlying spongedb 88 type spongeBatch struct { 89 db *spongeDb 90 } 91 92 func (b *spongeBatch) Put(key, value []byte) error { 93 b.db.Put(key, value) 94 return nil 95 } 96 func (b *spongeBatch) Delete(key []byte) error { panic("implement me") } 97 func (b *spongeBatch) ValueSize() int { return 100 } 98 func (b *spongeBatch) Write() error { return nil } 99 func (b *spongeBatch) Reset() {} 100 func (b *spongeBatch) Replay(w ethdb.KeyValueWriter) error { return nil } 101 102 type kv struct { 103 k, v []byte 104 } 105 type kvs []kv 106 107 func (k kvs) Len() int { 108 return len(k) 109 } 110 111 func (k kvs) Less(i, j int) bool { 112 return bytes.Compare(k[i].k, k[j].k) < 0 113 } 114 115 func (k kvs) Swap(i, j int) { 116 k[j], k[i] = k[i], k[j] 117 } 118 119 // Fuzz is the fuzzing entry-point. 120 // The function must return 121 // 122 // - 1 if the fuzzer should increase priority of the 123 // given input during subsequent fuzzing (for example, the input is lexically 124 // correct and was parsed successfully); 125 // - -1 if the input must not be added to corpus even if gives new coverage; and 126 // - 0 otherwise 127 // 128 // other values are reserved for future use. 129 func Fuzz(data []byte) int { 130 f := fuzzer{ 131 input: bytes.NewReader(data), 132 exhausted: false, 133 } 134 return f.fuzz() 135 } 136 137 func Debug(data []byte) int { 138 f := fuzzer{ 139 input: bytes.NewReader(data), 140 exhausted: false, 141 debugging: true, 142 } 143 return f.fuzz() 144 } 145 146 func (f *fuzzer) fuzz() int { 147 // This spongeDb is used to check the sequence of disk-db-writes 148 var ( 149 spongeA = &spongeDb{sponge: sha3.NewLegacyKeccak256()} 150 dbA = trie.NewDatabase(rawdb.NewDatabase(spongeA)) 151 trieA = trie.NewEmpty(dbA) 152 spongeB = &spongeDb{sponge: sha3.NewLegacyKeccak256()} 153 dbB = trie.NewDatabase(rawdb.NewDatabase(spongeB)) 154 trieB = trie.NewStackTrie(func(owner common.Hash, path []byte, hash common.Hash, blob []byte) { 155 rawdb.WriteTrieNode(spongeB, owner, path, hash, blob, dbB.Scheme()) 156 }) 157 vals kvs 158 useful bool 159 maxElements = 10000 160 // operate on unique keys only 161 keys = make(map[string]struct{}) 162 ) 163 // Fill the trie with elements 164 for i := 0; !f.exhausted && i < maxElements; i++ { 165 k := f.read(32) 166 v := f.readSlice(1, 500) 167 if f.exhausted { 168 // If it was exhausted while reading, the value may be all zeroes, 169 // thus 'deletion' which is not supported on stacktrie 170 break 171 } 172 if _, present := keys[string(k)]; present { 173 // This key is a duplicate, ignore it 174 continue 175 } 176 keys[string(k)] = struct{}{} 177 vals = append(vals, kv{k: k, v: v}) 178 trieA.Update(k, v) 179 useful = true 180 } 181 if !useful { 182 return 0 183 } 184 // Flush trie -> database 185 rootA, nodes := trieA.Commit(false) 186 if nodes != nil { 187 dbA.Update(trie.NewWithNodeSet(nodes)) 188 } 189 // Flush memdb -> disk (sponge) 190 dbA.Commit(rootA, false) 191 192 // Stacktrie requires sorted insertion 193 sort.Sort(vals) 194 for _, kv := range vals { 195 if f.debugging { 196 fmt.Printf("{\"%#x\" , \"%#x\"} // stacktrie.Update\n", kv.k, kv.v) 197 } 198 trieB.Update(kv.k, kv.v) 199 } 200 rootB := trieB.Hash() 201 trieB.Commit() 202 if rootA != rootB { 203 panic(fmt.Sprintf("roots differ: (trie) %x != %x (stacktrie)", rootA, rootB)) 204 } 205 sumA := spongeA.sponge.Sum(nil) 206 sumB := spongeB.sponge.Sum(nil) 207 if !bytes.Equal(sumA, sumB) { 208 panic(fmt.Sprintf("sequence differ: (trie) %x != %x (stacktrie)", sumA, sumB)) 209 } 210 211 // Ensure all the nodes are persisted correctly 212 var ( 213 nodeset = make(map[string][]byte) // path -> blob 214 trieC = trie.NewStackTrie(func(owner common.Hash, path []byte, hash common.Hash, blob []byte) { 215 if crypto.Keccak256Hash(blob) != hash { 216 panic("invalid node blob") 217 } 218 if owner != (common.Hash{}) { 219 panic("invalid node owner") 220 } 221 nodeset[string(path)] = common.CopyBytes(blob) 222 }) 223 checked int 224 ) 225 for _, kv := range vals { 226 trieC.Update(kv.k, kv.v) 227 } 228 rootC, _ := trieC.Commit() 229 if rootA != rootC { 230 panic(fmt.Sprintf("roots differ: (trie) %x != %x (stacktrie)", rootA, rootC)) 231 } 232 trieA, _ = trie.New(trie.TrieID(rootA), dbA) 233 iterA := trieA.NodeIterator(nil) 234 for iterA.Next(true) { 235 if iterA.Hash() == (common.Hash{}) { 236 if _, present := nodeset[string(iterA.Path())]; present { 237 panic("unexpected tiny node") 238 } 239 continue 240 } 241 nodeBlob, present := nodeset[string(iterA.Path())] 242 if !present { 243 panic("missing node") 244 } 245 if !bytes.Equal(nodeBlob, iterA.NodeBlob()) { 246 panic("node blob is not matched") 247 } 248 checked += 1 249 } 250 if checked != len(nodeset) { 251 panic("node number is not matched") 252 } 253 return 1 254 }