github.com/theQRL/go-zond@v0.1.1/tests/fuzzers/txfetcher/txfetcher_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 txfetcher 18 19 import ( 20 "bytes" 21 "fmt" 22 "math/big" 23 "math/rand" 24 "time" 25 26 "github.com/theQRL/go-zond/common" 27 "github.com/theQRL/go-zond/common/mclock" 28 "github.com/theQRL/go-zond/core/types" 29 "github.com/theQRL/go-zond/zond/fetcher" 30 ) 31 32 var ( 33 peers []string 34 txs []*types.Transaction 35 ) 36 37 func init() { 38 // Random is nice, but we need it deterministic 39 rand := rand.New(rand.NewSource(0x3a29)) 40 41 peers = make([]string, 10) 42 for i := 0; i < len(peers); i++ { 43 peers[i] = fmt.Sprintf("Peer #%d", i) 44 } 45 txs = make([]*types.Transaction, 65536) // We need to bump enough to hit all the limits 46 for i := 0; i < len(txs); i++ { 47 txs[i] = types.NewTransaction(rand.Uint64(), common.Address{byte(rand.Intn(256))}, new(big.Int), 0, new(big.Int), nil) 48 } 49 } 50 51 func Fuzz(input []byte) int { 52 // Don't generate insanely large test cases, not much value in them 53 if len(input) > 16*1024 { 54 return 0 55 } 56 verbose := false 57 r := bytes.NewReader(input) 58 59 // Reduce the problem space for certain fuzz runs. Small tx space is better 60 // for testing clashes and in general the fetcher, but we should still run 61 // some tests with large spaces to hit potential issues on limits. 62 limit, err := r.ReadByte() 63 if err != nil { 64 return 0 65 } 66 switch limit % 4 { 67 case 0: 68 txs = txs[:4] 69 case 1: 70 txs = txs[:256] 71 case 2: 72 txs = txs[:4096] 73 case 3: 74 // Full run 75 } 76 // Create a fetcher and hook into it's simulated fields 77 clock := new(mclock.Simulated) 78 rand := rand.New(rand.NewSource(0x3a29)) // Same used in package tests!!! 79 80 f := fetcher.NewTxFetcherForTests( 81 func(common.Hash) bool { return false }, 82 func(txs []*types.Transaction) []error { 83 return make([]error, len(txs)) 84 }, 85 func(string, []common.Hash) error { return nil }, 86 clock, rand, 87 ) 88 f.Start() 89 defer f.Stop() 90 91 // Try to throw random junk at the fetcher 92 for { 93 // Read the next command and abort if we're done 94 cmd, err := r.ReadByte() 95 if err != nil { 96 return 0 97 } 98 switch cmd % 4 { 99 case 0: 100 // Notify a new set of transactions: 101 // Byte 1: Peer index to announce with 102 // Byte 2: Number of hashes to announce 103 // Byte 3-4, 5-6, etc: Transaction indices (2 byte) to announce 104 peerIdx, err := r.ReadByte() 105 if err != nil { 106 return 0 107 } 108 peer := peers[int(peerIdx)%len(peers)] 109 110 announceCnt, err := r.ReadByte() 111 if err != nil { 112 return 0 113 } 114 announce := int(announceCnt) % (2 * len(txs)) // No point in generating too many duplicates 115 116 var ( 117 announceIdxs = make([]int, announce) 118 announces = make([]common.Hash, announce) 119 ) 120 for i := 0; i < len(announces); i++ { 121 annBuf := make([]byte, 2) 122 if n, err := r.Read(annBuf); err != nil || n != 2 { 123 return 0 124 } 125 announceIdxs[i] = (int(annBuf[0])*256 + int(annBuf[1])) % len(txs) 126 announces[i] = txs[announceIdxs[i]].Hash() 127 } 128 if verbose { 129 fmt.Println("Notify", peer, announceIdxs) 130 } 131 if err := f.Notify(peer, announces); err != nil { 132 panic(err) 133 } 134 135 case 1: 136 // Deliver a new set of transactions: 137 // Byte 1: Peer index to announce with 138 // Byte 2: Number of hashes to announce 139 // Byte 3-4, 5-6, etc: Transaction indices (2 byte) to announce 140 peerIdx, err := r.ReadByte() 141 if err != nil { 142 return 0 143 } 144 peer := peers[int(peerIdx)%len(peers)] 145 146 deliverCnt, err := r.ReadByte() 147 if err != nil { 148 return 0 149 } 150 deliver := int(deliverCnt) % (2 * len(txs)) // No point in generating too many duplicates 151 152 var ( 153 deliverIdxs = make([]int, deliver) 154 deliveries = make([]*types.Transaction, deliver) 155 ) 156 for i := 0; i < len(deliveries); i++ { 157 deliverBuf := make([]byte, 2) 158 if n, err := r.Read(deliverBuf); err != nil || n != 2 { 159 return 0 160 } 161 deliverIdxs[i] = (int(deliverBuf[0])*256 + int(deliverBuf[1])) % len(txs) 162 deliveries[i] = txs[deliverIdxs[i]] 163 } 164 directFlag, err := r.ReadByte() 165 if err != nil { 166 return 0 167 } 168 direct := (directFlag % 2) == 0 169 if verbose { 170 fmt.Println("Enqueue", peer, deliverIdxs, direct) 171 } 172 if err := f.Enqueue(peer, deliveries, direct); err != nil { 173 panic(err) 174 } 175 176 case 2: 177 // Drop a peer: 178 // Byte 1: Peer index to drop 179 peerIdx, err := r.ReadByte() 180 if err != nil { 181 return 0 182 } 183 peer := peers[int(peerIdx)%len(peers)] 184 if verbose { 185 fmt.Println("Drop", peer) 186 } 187 if err := f.Drop(peer); err != nil { 188 panic(err) 189 } 190 191 case 3: 192 // Move the simulated clock forward 193 // Byte 1: 100ms increment to move forward 194 tickCnt, err := r.ReadByte() 195 if err != nil { 196 return 0 197 } 198 tick := time.Duration(tickCnt) * 100 * time.Millisecond 199 if verbose { 200 fmt.Println("Sleep", tick) 201 } 202 clock.Run(tick) 203 } 204 } 205 }