github.com/aswedchain/aswed@v1.0.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/aswedchain/aswed/common" 27 "github.com/aswedchain/aswed/common/mclock" 28 "github.com/aswedchain/aswed/core/types" 29 "github.com/aswedchain/aswed/eth/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 -1 55 } 56 r := bytes.NewReader(input) 57 58 // Reduce the problem space for certain fuzz runs. Small tx space is better 59 // for testing clashes and in general the fetcher, but we should still run 60 // some tests with large spaces to hit potential issues on limits. 61 limit, err := r.ReadByte() 62 if err != nil { 63 return 0 64 } 65 switch limit % 4 { 66 case 0: 67 txs = txs[:4] 68 case 1: 69 txs = txs[:256] 70 case 2: 71 txs = txs[:4096] 72 case 3: 73 // Full run 74 } 75 // Create a fetcher and hook into it's simulated fields 76 clock := new(mclock.Simulated) 77 rand := rand.New(rand.NewSource(0x3a29)) // Same used in package tests!!! 78 79 f := fetcher.NewTxFetcherForTests( 80 func(common.Hash) bool { return false }, 81 func(txs []*types.Transaction) []error { 82 return make([]error, len(txs)) 83 }, 84 func(string, []common.Hash) error { return nil }, 85 clock, rand, 86 ) 87 f.Start() 88 defer f.Stop() 89 90 // Try to throw random junk at the fetcher 91 for { 92 // Read the next command and abort if we're done 93 cmd, err := r.ReadByte() 94 if err != nil { 95 return 0 96 } 97 switch cmd % 4 { 98 case 0: 99 // Notify a new set of transactions: 100 // Byte 1: Peer index to announce with 101 // Byte 2: Number of hashes to announce 102 // Byte 3-4, 5-6, etc: Transaction indices (2 byte) to announce 103 peerIdx, err := r.ReadByte() 104 if err != nil { 105 return 0 106 } 107 peer := peers[int(peerIdx)%len(peers)] 108 109 announceCnt, err := r.ReadByte() 110 if err != nil { 111 return 0 112 } 113 announce := int(announceCnt) % (2 * len(txs)) // No point in generating too many duplicates 114 115 var ( 116 announceIdxs = make([]int, announce) 117 announces = make([]common.Hash, announce) 118 ) 119 for i := 0; i < len(announces); i++ { 120 annBuf := make([]byte, 2) 121 if n, err := r.Read(annBuf); err != nil || n != 2 { 122 return 0 123 } 124 announceIdxs[i] = (int(annBuf[0])*256 + int(annBuf[1])) % len(txs) 125 announces[i] = txs[announceIdxs[i]].Hash() 126 } 127 fmt.Println("Notify", peer, announceIdxs) 128 if err := f.Notify(peer, announces); err != nil { 129 panic(err) 130 } 131 132 case 1: 133 // Deliver a new set of transactions: 134 // Byte 1: Peer index to announce with 135 // Byte 2: Number of hashes to announce 136 // Byte 3-4, 5-6, etc: Transaction indices (2 byte) to announce 137 peerIdx, err := r.ReadByte() 138 if err != nil { 139 return 0 140 } 141 peer := peers[int(peerIdx)%len(peers)] 142 143 deliverCnt, err := r.ReadByte() 144 if err != nil { 145 return 0 146 } 147 deliver := int(deliverCnt) % (2 * len(txs)) // No point in generating too many duplicates 148 149 var ( 150 deliverIdxs = make([]int, deliver) 151 deliveries = make([]*types.Transaction, deliver) 152 ) 153 for i := 0; i < len(deliveries); i++ { 154 deliverBuf := make([]byte, 2) 155 if n, err := r.Read(deliverBuf); err != nil || n != 2 { 156 return 0 157 } 158 deliverIdxs[i] = (int(deliverBuf[0])*256 + int(deliverBuf[1])) % len(txs) 159 deliveries[i] = txs[deliverIdxs[i]] 160 } 161 directFlag, err := r.ReadByte() 162 if err != nil { 163 return 0 164 } 165 direct := (directFlag % 2) == 0 166 167 fmt.Println("Enqueue", peer, deliverIdxs, direct) 168 if err := f.Enqueue(peer, deliveries, direct); err != nil { 169 panic(err) 170 } 171 172 case 2: 173 // Drop a peer: 174 // Byte 1: Peer index to drop 175 peerIdx, err := r.ReadByte() 176 if err != nil { 177 return 0 178 } 179 peer := peers[int(peerIdx)%len(peers)] 180 181 fmt.Println("Drop", peer) 182 if err := f.Drop(peer); err != nil { 183 panic(err) 184 } 185 186 case 3: 187 // Move the simulated clock forward 188 // Byte 1: 100ms increment to move forward 189 tickCnt, err := r.ReadByte() 190 if err != nil { 191 return 0 192 } 193 tick := time.Duration(tickCnt) * 100 * time.Millisecond 194 195 fmt.Println("Sleep", tick) 196 clock.Run(tick) 197 } 198 } 199 }