forked from MariusVanDerWijden/tx-fuzz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transactions.go
180 lines (171 loc) · 5.31 KB
/
transactions.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
package txfuzz
import (
"context"
"errors"
"math/big"
"math/rand"
"github.com/MariusVanDerWijden/FuzzyVM/filler"
"github.com/MariusVanDerWijden/FuzzyVM/generator"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/rpc"
)
// RandomCode creates a random byte code from the passed filler.
func RandomCode(f *filler.Filler) []byte {
_, code := generator.GenerateProgram(f)
return code
}
// RandomTx creates a random transaction.
func RandomTx(f *filler.Filler) (*types.Transaction, error) {
nonce := uint64(rand.Int63())
gasPrice := big.NewInt(rand.Int63())
chainID := big.NewInt(rand.Int63())
return RandomValidTx(nil, f, common.Address{}, nonce, gasPrice, chainID, false)
}
// RandomValidTx creates a random valid transaction.
// It does not mean that the transaction will succeed, but that it is well-formed.
// If gasPrice is not set, we will try to get it from the rpc
// If chainID is not set, we will try to get it from the rpc
func RandomValidTx(rpc *rpc.Client, f *filler.Filler, sender common.Address, nonce uint64, gasPrice, chainID *big.Int, al bool) (*types.Transaction, error) {
// Set fields if non-nil
if rpc != nil {
client := ethclient.NewClient(rpc)
var err error
if gasPrice == nil {
gasPrice, err = client.SuggestGasPrice(context.Background())
if err != nil {
gasPrice = big.NewInt(1)
}
}
if chainID == nil {
chainID, err = client.ChainID(context.Background())
if err != nil {
chainID = big.NewInt(1)
}
}
}
gas := uint64(100000)
to := randomAddress()
code := RandomCode(f)
value := big.NewInt(0)
if len(code) > 128 {
code = code[:128]
}
mod := 10
if al {
mod = 6
}
switch f.Byte() % byte(mod) {
case 0:
// Legacy contract creation
return types.NewContractCreation(nonce, value, gas, gasPrice, code), nil
case 1:
// Legacy transaction
return types.NewTransaction(nonce, to, value, gas, gasPrice, code), nil
case 2:
// AccessList contract creation
return newALTx(nonce, nil, gas, chainID, gasPrice, value, code, make(types.AccessList, 0)), nil
case 3:
// AccessList transaction
return newALTx(nonce, &to, gas, chainID, gasPrice, value, code, make(types.AccessList, 0)), nil
case 4:
// 1559 contract creation
tip, feecap, err := getCaps(rpc, gasPrice)
if err != nil {
return nil, err
}
return new1559Tx(nonce, nil, gas, chainID, tip, feecap, value, code, make(types.AccessList, 0)), nil
case 5:
// 1559 transaction
tip, feecap, err := getCaps(rpc, gasPrice)
if err != nil {
return nil, err
}
return new1559Tx(nonce, &to, gas, chainID, tip, feecap, value, code, make(types.AccessList, 0)), nil
case 6:
// AccessList contract creation with AL
tx := types.NewContractCreation(nonce, value, gas, gasPrice, code)
al, err := CreateAccessList(rpc, tx, sender)
if err != nil {
return nil, err
}
return newALTx(nonce, nil, gas, chainID, gasPrice, value, code, *al), nil
case 7:
// AccessList transaction with AL
tx := types.NewTransaction(nonce, to, value, gas, gasPrice, code)
al, err := CreateAccessList(rpc, tx, sender)
if err != nil {
return nil, err
}
return newALTx(nonce, &to, gas, chainID, gasPrice, value, code, *al), nil
case 8:
// 1559 contract creation with AL
tx := types.NewContractCreation(nonce, value, gas, gasPrice, code)
al, err := CreateAccessList(rpc, tx, sender)
if err != nil {
return nil, err
}
tip, feecap, err := getCaps(rpc, gasPrice)
if err != nil {
return nil, err
}
return new1559Tx(nonce, nil, gas, chainID, tip, feecap, value, code, *al), nil
case 9:
// 1559 tx with AL
tx := types.NewTransaction(nonce, to, value, gas, gasPrice, code)
al, err := CreateAccessList(rpc, tx, sender)
if err != nil {
return nil, err
}
tip, feecap, err := getCaps(rpc, gasPrice)
if err != nil {
return nil, err
}
return new1559Tx(nonce, &to, gas, chainID, tip, feecap, value, code, *al), nil
}
return nil, errors.New("asdf")
}
func newALTx(nonce uint64, to *common.Address, gasLimit uint64, chainID, gasPrice, value *big.Int, code []byte, al types.AccessList) *types.Transaction {
return types.NewTx(&types.AccessListTx{
ChainID: chainID,
Nonce: nonce,
GasPrice: gasPrice,
Gas: gasLimit,
To: to,
Value: value,
Data: code,
AccessList: al,
})
}
func new1559Tx(nonce uint64, to *common.Address, gasLimit uint64, chainID, tip, feeCap, value *big.Int, code []byte, al types.AccessList) *types.Transaction {
return types.NewTx(&types.DynamicFeeTx{
ChainID: chainID,
Nonce: nonce,
GasTipCap: tip,
GasFeeCap: feeCap,
Gas: gasLimit,
To: to,
Value: value,
Data: code,
AccessList: al,
})
}
func getCaps(rpc *rpc.Client, defaultGasPrice *big.Int) (*big.Int, *big.Int, error) {
if rpc == nil {
tip := new(big.Int).Mul(big.NewInt(1), big.NewInt(params.GWei))
if defaultGasPrice.Cmp(tip) >= 0 {
feeCap := new(big.Int).Sub(defaultGasPrice, tip)
return tip, feeCap, nil
}
return big.NewInt(0), defaultGasPrice, nil
}
client := ethclient.NewClient(rpc)
tip, err := client.SuggestGasTipCap(context.Background())
if err != nil {
return nil, nil, err
}
feeCap, err := client.SuggestGasPrice(context.Background())
return tip, feeCap, err
}