github.com/carter-ya/go-ethereum@v0.0.0-20230628080049-d2309be3983b/tests/fuzzers/trie/trie-fuzzer.go (about) 1 // Copyright 2019 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 "encoding/binary" 22 "fmt" 23 24 "github.com/ethereum/go-ethereum/ethdb/memorydb" 25 "github.com/ethereum/go-ethereum/trie" 26 ) 27 28 // randTest performs random trie operations. 29 // Instances of this test are created by Generate. 30 type randTest []randTestStep 31 32 type randTestStep struct { 33 op int 34 key []byte // for opUpdate, opDelete, opGet 35 value []byte // for opUpdate 36 err error // for debugging 37 } 38 39 type proofDb struct{} 40 41 func (proofDb) Put(key []byte, value []byte) error { 42 return nil 43 } 44 45 func (proofDb) Delete(key []byte) error { 46 return nil 47 } 48 49 const ( 50 opUpdate = iota 51 opDelete 52 opGet 53 opHash 54 opCommit 55 opItercheckhash 56 opProve 57 opMax // boundary value, not an actual op 58 ) 59 60 type dataSource struct { 61 input []byte 62 reader *bytes.Reader 63 } 64 65 func newDataSource(input []byte) *dataSource { 66 return &dataSource{ 67 input, bytes.NewReader(input), 68 } 69 } 70 func (ds *dataSource) readByte() byte { 71 if b, err := ds.reader.ReadByte(); err != nil { 72 return 0 73 } else { 74 return b 75 } 76 } 77 func (ds *dataSource) Read(buf []byte) (int, error) { 78 return ds.reader.Read(buf) 79 } 80 func (ds *dataSource) Ended() bool { 81 return ds.reader.Len() == 0 82 } 83 84 func Generate(input []byte) randTest { 85 var allKeys [][]byte 86 r := newDataSource(input) 87 genKey := func() []byte { 88 if len(allKeys) < 2 || r.readByte() < 0x0f { 89 // new key 90 key := make([]byte, r.readByte()%50) 91 r.Read(key) 92 allKeys = append(allKeys, key) 93 return key 94 } 95 // use existing key 96 return allKeys[int(r.readByte())%len(allKeys)] 97 } 98 99 var steps randTest 100 101 for i := 0; !r.Ended(); i++ { 102 step := randTestStep{op: int(r.readByte()) % opMax} 103 switch step.op { 104 case opUpdate: 105 step.key = genKey() 106 step.value = make([]byte, 8) 107 binary.BigEndian.PutUint64(step.value, uint64(i)) 108 case opGet, opDelete, opProve: 109 step.key = genKey() 110 } 111 steps = append(steps, step) 112 if len(steps) > 500 { 113 break 114 } 115 } 116 117 return steps 118 } 119 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(input []byte) int { 130 program := Generate(input) 131 if len(program) == 0 { 132 return 0 133 } 134 if err := runRandTest(program); err != nil { 135 panic(err) 136 } 137 return 1 138 } 139 140 func runRandTest(rt randTest) error { 141 triedb := trie.NewDatabase(memorydb.New()) 142 143 tr := trie.NewEmpty(triedb) 144 values := make(map[string]string) // tracks content of the trie 145 146 for i, step := range rt { 147 switch step.op { 148 case opUpdate: 149 tr.Update(step.key, step.value) 150 values[string(step.key)] = string(step.value) 151 case opDelete: 152 tr.Delete(step.key) 153 delete(values, string(step.key)) 154 case opGet: 155 v := tr.Get(step.key) 156 want := values[string(step.key)] 157 if string(v) != want { 158 rt[i].err = fmt.Errorf("mismatch for key %#x, got %#x want %#x", step.key, v, want) 159 } 160 case opHash: 161 tr.Hash() 162 case opCommit: 163 hash, nodes, err := tr.Commit(false) 164 if err != nil { 165 return err 166 } 167 if nodes != nil { 168 if err := triedb.Update(trie.NewWithNodeSet(nodes)); err != nil { 169 return err 170 } 171 } 172 newtr, err := trie.New(trie.TrieID(hash), triedb) 173 if err != nil { 174 return err 175 } 176 tr = newtr 177 case opItercheckhash: 178 checktr := trie.NewEmpty(triedb) 179 it := trie.NewIterator(tr.NodeIterator(nil)) 180 for it.Next() { 181 checktr.Update(it.Key, it.Value) 182 } 183 if tr.Hash() != checktr.Hash() { 184 return fmt.Errorf("hash mismatch in opItercheckhash") 185 } 186 case opProve: 187 rt[i].err = tr.Prove(step.key, 0, proofDb{}) 188 } 189 // Abort the test on error. 190 if rt[i].err != nil { 191 return rt[i].err 192 } 193 } 194 return nil 195 }