github.com/valorbit/go-ethereum@v1.9.11-rc4/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/valorbit/go-ethereum/common" 25 "github.com/valorbit/go-ethereum/ethdb/memorydb" 26 "github.com/valorbit/go-ethereum/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 opCommit 55 opHash 56 opReset 57 opItercheckhash 58 opProve 59 opMax // boundary value, not an actual op 60 ) 61 62 type dataSource struct { 63 input []byte 64 reader *bytes.Reader 65 } 66 67 func newDataSource(input []byte) *dataSource { 68 return &dataSource{ 69 input, bytes.NewReader(input), 70 } 71 } 72 func (ds *dataSource) ReadByte() byte { 73 if b, err := ds.reader.ReadByte(); err != nil { 74 return 0 75 } else { 76 return b 77 } 78 } 79 func (ds *dataSource) Read(buf []byte) (int, error) { 80 return ds.reader.Read(buf) 81 } 82 func (ds *dataSource) Ended() bool { 83 return ds.reader.Len() == 0 84 } 85 86 func Generate(input []byte) randTest { 87 88 var allKeys [][]byte 89 r := newDataSource(input) 90 genKey := func() []byte { 91 92 if len(allKeys) < 2 || r.ReadByte() < 0x0f { 93 // new key 94 key := make([]byte, r.ReadByte()%50) 95 r.Read(key) 96 allKeys = append(allKeys, key) 97 return key 98 } 99 // use existing key 100 return allKeys[int(r.ReadByte())%len(allKeys)] 101 } 102 103 var steps randTest 104 105 for i := 0; !r.Ended(); i++ { 106 107 step := randTestStep{op: int(r.ReadByte()) % opMax} 108 switch step.op { 109 case opUpdate: 110 step.key = genKey() 111 step.value = make([]byte, 8) 112 binary.BigEndian.PutUint64(step.value, uint64(i)) 113 case opGet, opDelete, opProve: 114 step.key = genKey() 115 } 116 steps = append(steps, step) 117 if len(steps) > 500 { 118 break 119 } 120 } 121 122 return steps 123 } 124 125 func Fuzz(input []byte) int { 126 program := Generate(input) 127 if len(program) == 0 { 128 return -1 129 } 130 if err := runRandTest(program); err != nil { 131 panic(err) 132 } 133 return 0 134 } 135 136 func runRandTest(rt randTest) error { 137 138 triedb := trie.NewDatabase(memorydb.New()) 139 140 tr, _ := trie.New(common.Hash{}, triedb) 141 values := make(map[string]string) // tracks content of the trie 142 143 for i, step := range rt { 144 switch step.op { 145 case opUpdate: 146 tr.Update(step.key, step.value) 147 values[string(step.key)] = string(step.value) 148 case opDelete: 149 tr.Delete(step.key) 150 delete(values, string(step.key)) 151 case opGet: 152 v := tr.Get(step.key) 153 want := values[string(step.key)] 154 if string(v) != want { 155 rt[i].err = fmt.Errorf("mismatch for key 0x%x, got 0x%x want 0x%x", step.key, v, want) 156 } 157 case opCommit: 158 _, rt[i].err = tr.Commit(nil) 159 case opHash: 160 tr.Hash() 161 case opReset: 162 hash, err := tr.Commit(nil) 163 if err != nil { 164 return err 165 } 166 newtr, err := trie.New(hash, triedb) 167 if err != nil { 168 return err 169 } 170 tr = newtr 171 case opItercheckhash: 172 checktr, _ := trie.New(common.Hash{}, triedb) 173 it := trie.NewIterator(tr.NodeIterator(nil)) 174 for it.Next() { 175 checktr.Update(it.Key, it.Value) 176 } 177 if tr.Hash() != checktr.Hash() { 178 return fmt.Errorf("hash mismatch in opItercheckhash") 179 } 180 case opProve: 181 rt[i].err = tr.Prove(step.key, 0, proofDb{}) 182 } 183 // Abort the test on error. 184 if rt[i].err != nil { 185 return rt[i].err 186 } 187 } 188 return nil 189 }