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