github.com/cryptotooltop/go-ethereum@v0.0.0-20231103184714-151d1922f3e5/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 "golang.org/x/crypto/sha3" 29 30 "github.com/scroll-tech/go-ethereum/common" 31 "github.com/scroll-tech/go-ethereum/ethdb" 32 "github.com/scroll-tech/go-ethereum/trie" 33 ) 34 35 type fuzzer struct { 36 input io.Reader 37 exhausted bool 38 debugging bool 39 } 40 41 func (f *fuzzer) read(size int) []byte { 42 out := make([]byte, size) 43 if _, err := f.input.Read(out); err != nil { 44 f.exhausted = true 45 } 46 return out 47 } 48 49 func (f *fuzzer) readSlice(min, max int) []byte { 50 var a uint16 51 binary.Read(f.input, binary.LittleEndian, &a) 52 size := min + int(a)%(max-min) 53 out := make([]byte, size) 54 if _, err := f.input.Read(out); err != nil { 55 f.exhausted = true 56 } 57 return out 58 } 59 60 // spongeDb is a dummy db backend which accumulates writes in a sponge 61 type spongeDb struct { 62 sponge hash.Hash 63 debug bool 64 } 65 66 func (s *spongeDb) Has(key []byte) (bool, error) { panic("implement me") } 67 func (s *spongeDb) Get(key []byte) ([]byte, error) { return nil, errors.New("no such elem") } 68 func (s *spongeDb) Delete(key []byte) error { panic("implement me") } 69 func (s *spongeDb) NewBatch() ethdb.Batch { return &spongeBatch{s} } 70 func (s *spongeDb) Stat(property string) (string, error) { panic("implement me") } 71 func (s *spongeDb) Compact(start []byte, limit []byte) error { panic("implement me") } 72 func (s *spongeDb) Close() error { return nil } 73 74 func (s *spongeDb) Put(key []byte, value []byte) error { 75 if s.debug { 76 fmt.Printf("db.Put %x : %x\n", key, value) 77 } 78 s.sponge.Write(key) 79 s.sponge.Write(value) 80 return nil 81 } 82 func (s *spongeDb) NewIterator(prefix []byte, start []byte) ethdb.Iterator { panic("implement me") } 83 84 // spongeBatch is a dummy batch which immediately writes to the underlying spongedb 85 type spongeBatch struct { 86 db *spongeDb 87 } 88 89 func (b *spongeBatch) Put(key, value []byte) error { 90 b.db.Put(key, value) 91 return nil 92 } 93 func (b *spongeBatch) Delete(key []byte) error { panic("implement me") } 94 func (b *spongeBatch) ValueSize() int { return 100 } 95 func (b *spongeBatch) Write() error { return nil } 96 func (b *spongeBatch) Reset() {} 97 func (b *spongeBatch) Replay(w ethdb.KeyValueWriter) error { return nil } 98 99 type kv struct { 100 k, v []byte 101 } 102 type kvs []kv 103 104 func (k kvs) Len() int { 105 return len(k) 106 } 107 108 func (k kvs) Less(i, j int) bool { 109 return bytes.Compare(k[i].k, k[j].k) < 0 110 } 111 112 func (k kvs) Swap(i, j int) { 113 k[j], k[i] = k[i], k[j] 114 } 115 116 // The function must return 117 // 1 if the fuzzer should increase priority of the 118 // given input during subsequent fuzzing (for example, the input is lexically 119 // correct and was parsed successfully); 120 // -1 if the input must not be added to corpus even if gives new coverage; and 121 // 0 otherwise 122 // other values are reserved for future use. 123 func Fuzz(data []byte) int { 124 f := fuzzer{ 125 input: bytes.NewReader(data), 126 exhausted: false, 127 } 128 return f.fuzz() 129 } 130 131 func Debug(data []byte) int { 132 f := fuzzer{ 133 input: bytes.NewReader(data), 134 exhausted: false, 135 debugging: true, 136 } 137 return f.fuzz() 138 } 139 140 func (f *fuzzer) fuzz() int { 141 142 // This spongeDb is used to check the sequence of disk-db-writes 143 var ( 144 spongeA = &spongeDb{sponge: sha3.NewLegacyKeccak256()} 145 dbA = trie.NewDatabase(spongeA) 146 trieA, _ = trie.New(common.Hash{}, dbA) 147 spongeB = &spongeDb{sponge: sha3.NewLegacyKeccak256()} 148 trieB = trie.NewStackTrie(spongeB) 149 vals kvs 150 useful bool 151 maxElements = 10000 152 // operate on unique keys only 153 keys = make(map[string]struct{}) 154 ) 155 // Fill the trie with elements 156 for i := 0; !f.exhausted && i < maxElements; i++ { 157 k := f.read(32) 158 v := f.readSlice(1, 500) 159 if f.exhausted { 160 // If it was exhausted while reading, the value may be all zeroes, 161 // thus 'deletion' which is not supported on stacktrie 162 break 163 } 164 if _, present := keys[string(k)]; present { 165 // This key is a duplicate, ignore it 166 continue 167 } 168 keys[string(k)] = struct{}{} 169 vals = append(vals, kv{k: k, v: v}) 170 trieA.Update(k, v) 171 useful = true 172 } 173 if !useful { 174 return 0 175 } 176 // Flush trie -> database 177 rootA, _, err := trieA.Commit(nil) 178 if err != nil { 179 panic(err) 180 } 181 // Flush memdb -> disk (sponge) 182 dbA.Commit(rootA, false, nil) 183 184 // Stacktrie requires sorted insertion 185 sort.Sort(vals) 186 for _, kv := range vals { 187 if f.debugging { 188 fmt.Printf("{\"0x%x\" , \"0x%x\"} // stacktrie.Update\n", kv.k, kv.v) 189 } 190 trieB.Update(kv.k, kv.v) 191 } 192 rootB := trieB.Hash() 193 if _, err := trieB.Commit(); err != nil { 194 panic(err) 195 } 196 if rootA != rootB { 197 panic(fmt.Sprintf("roots differ: (trie) %x != %x (stacktrie)", rootA, rootB)) 198 } 199 sumA := spongeA.sponge.Sum(nil) 200 sumB := spongeB.sponge.Sum(nil) 201 if !bytes.Equal(sumA, sumB) { 202 panic(fmt.Sprintf("sequence differ: (trie) %x != %x (stacktrie)", sumA, sumB)) 203 } 204 return 1 205 }