forked from OffchainLabs/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
txfetcher_fuzzer.go
205 lines (188 loc) · 5.47 KB
/
txfetcher_fuzzer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Copyright 2020 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package txfetcher
import (
"bytes"
"fmt"
"math/big"
"math/rand"
"time"
"github.com/coming-chat/go-ethereum-arbitrum/common"
"github.com/coming-chat/go-ethereum-arbitrum/common/mclock"
"github.com/coming-chat/go-ethereum-arbitrum/core/types"
"github.com/coming-chat/go-ethereum-arbitrum/eth/fetcher"
)
var (
peers []string
txs []*types.Transaction
)
func init() {
// Random is nice, but we need it deterministic
rand := rand.New(rand.NewSource(0x3a29))
peers = make([]string, 10)
for i := 0; i < len(peers); i++ {
peers[i] = fmt.Sprintf("Peer #%d", i)
}
txs = make([]*types.Transaction, 65536) // We need to bump enough to hit all the limits
for i := 0; i < len(txs); i++ {
txs[i] = types.NewTransaction(rand.Uint64(), common.Address{byte(rand.Intn(256))}, new(big.Int), 0, new(big.Int), nil)
}
}
func Fuzz(input []byte) int {
// Don't generate insanely large test cases, not much value in them
if len(input) > 16*1024 {
return 0
}
verbose := false
r := bytes.NewReader(input)
// Reduce the problem space for certain fuzz runs. Small tx space is better
// for testing clashes and in general the fetcher, but we should still run
// some tests with large spaces to hit potential issues on limits.
limit, err := r.ReadByte()
if err != nil {
return 0
}
switch limit % 4 {
case 0:
txs = txs[:4]
case 1:
txs = txs[:256]
case 2:
txs = txs[:4096]
case 3:
// Full run
}
// Create a fetcher and hook into it's simulated fields
clock := new(mclock.Simulated)
rand := rand.New(rand.NewSource(0x3a29)) // Same used in package tests!!!
f := fetcher.NewTxFetcherForTests(
func(common.Hash) bool { return false },
func(txs []*types.Transaction) []error {
return make([]error, len(txs))
},
func(string, []common.Hash) error { return nil },
clock, rand,
)
f.Start()
defer f.Stop()
// Try to throw random junk at the fetcher
for {
// Read the next command and abort if we're done
cmd, err := r.ReadByte()
if err != nil {
return 0
}
switch cmd % 4 {
case 0:
// Notify a new set of transactions:
// Byte 1: Peer index to announce with
// Byte 2: Number of hashes to announce
// Byte 3-4, 5-6, etc: Transaction indices (2 byte) to announce
peerIdx, err := r.ReadByte()
if err != nil {
return 0
}
peer := peers[int(peerIdx)%len(peers)]
announceCnt, err := r.ReadByte()
if err != nil {
return 0
}
announce := int(announceCnt) % (2 * len(txs)) // No point in generating too many duplicates
var (
announceIdxs = make([]int, announce)
announces = make([]common.Hash, announce)
)
for i := 0; i < len(announces); i++ {
annBuf := make([]byte, 2)
if n, err := r.Read(annBuf); err != nil || n != 2 {
return 0
}
announceIdxs[i] = (int(annBuf[0])*256 + int(annBuf[1])) % len(txs)
announces[i] = txs[announceIdxs[i]].Hash()
}
if verbose {
fmt.Println("Notify", peer, announceIdxs)
}
if err := f.Notify(peer, announces); err != nil {
panic(err)
}
case 1:
// Deliver a new set of transactions:
// Byte 1: Peer index to announce with
// Byte 2: Number of hashes to announce
// Byte 3-4, 5-6, etc: Transaction indices (2 byte) to announce
peerIdx, err := r.ReadByte()
if err != nil {
return 0
}
peer := peers[int(peerIdx)%len(peers)]
deliverCnt, err := r.ReadByte()
if err != nil {
return 0
}
deliver := int(deliverCnt) % (2 * len(txs)) // No point in generating too many duplicates
var (
deliverIdxs = make([]int, deliver)
deliveries = make([]*types.Transaction, deliver)
)
for i := 0; i < len(deliveries); i++ {
deliverBuf := make([]byte, 2)
if n, err := r.Read(deliverBuf); err != nil || n != 2 {
return 0
}
deliverIdxs[i] = (int(deliverBuf[0])*256 + int(deliverBuf[1])) % len(txs)
deliveries[i] = txs[deliverIdxs[i]]
}
directFlag, err := r.ReadByte()
if err != nil {
return 0
}
direct := (directFlag % 2) == 0
if verbose {
fmt.Println("Enqueue", peer, deliverIdxs, direct)
}
if err := f.Enqueue(peer, deliveries, direct); err != nil {
panic(err)
}
case 2:
// Drop a peer:
// Byte 1: Peer index to drop
peerIdx, err := r.ReadByte()
if err != nil {
return 0
}
peer := peers[int(peerIdx)%len(peers)]
if verbose {
fmt.Println("Drop", peer)
}
if err := f.Drop(peer); err != nil {
panic(err)
}
case 3:
// Move the simulated clock forward
// Byte 1: 100ms increment to move forward
tickCnt, err := r.ReadByte()
if err != nil {
return 0
}
tick := time.Duration(tickCnt) * 100 * time.Millisecond
if verbose {
fmt.Println("Sleep", tick)
}
clock.Run(tick)
}
}
}