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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package derive
import (
"bytes"
"math/big"
"testing"
"github.com/ethereum-optimism/optimism/op-bindings/bindings"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/state"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/core/vm/runtime"
"github.com/ethereum/go-ethereum/crypto"
"github.com/google/go-cmp/cmp"
)
var (
pk, _ = crypto.GenerateKey()
addr = common.Address{0x42, 0xff}
opts, _ = bind.NewKeyedTransactorWithChainID(pk, common.Big1)
from = crypto.PubkeyToAddress(pk.PublicKey)
portalContract, _ = bindings.NewOptimismPortal(addr, nil)
l1BlockInfoContract, _ = bindings.NewL1Block(addr, nil)
)
func cap_byte_slice(b []byte, c int) []byte {
if len(b) <= c {
return b
} else {
return b[:c]
}
}
func BytesToBigInt(b []byte) *big.Int {
return new(big.Int).SetBytes(cap_byte_slice(b, 32))
}
func BigEqual(a, b *big.Int) bool {
if a == nil || b == nil {
return a == b
} else {
return a.Cmp(b) == 0
}
}
// FuzzL1InfoRoundTrip checks that our encoder round trips properly
func FuzzL1InfoRoundTrip(f *testing.F) {
f.Fuzz(func(t *testing.T, number, time uint64, baseFee, hash []byte, seqNumber uint64) {
in := L1BlockInfo{
Number: number,
Time: time,
BaseFee: BytesToBigInt(baseFee),
BlockHash: common.BytesToHash(hash),
SequenceNumber: seqNumber,
}
enc, err := in.MarshalBinary()
if err != nil {
t.Fatalf("Failed to marshal binary: %v", err)
}
var out L1BlockInfo
err = out.UnmarshalBinary(enc)
if err != nil {
t.Fatalf("Failed to unmarshal binary: %v", err)
}
if !cmp.Equal(in, out, cmp.Comparer(BigEqual)) {
t.Fatalf("The data did not round trip correctly. in: %v. out: %v", in, out)
}
})
}
// FuzzL1InfoAgainstContract checks the custom marshalling functions against the contract
// bindings to ensure that our functions are up to date and match the bindings.
func FuzzL1InfoAgainstContract(f *testing.F) {
f.Fuzz(func(t *testing.T, number, time uint64, baseFee, hash []byte, seqNumber uint64) {
expected := L1BlockInfo{
Number: number,
Time: time,
BaseFee: BytesToBigInt(baseFee),
BlockHash: common.BytesToHash(hash),
SequenceNumber: seqNumber,
}
// Setup opts
opts.GasPrice = big.NewInt(100)
opts.GasLimit = 100_000
opts.NoSend = true
opts.Nonce = common.Big0
// Create the SetL1BlockValues transaction
tx, err := l1BlockInfoContract.SetL1BlockValues(
opts,
number,
time,
BytesToBigInt(baseFee),
common.BytesToHash(hash),
seqNumber,
)
if err != nil {
t.Fatalf("Failed to create the transaction: %v", err)
}
// Check that our encoder produces the same value and that we
// can decode the contract values exactly
enc, err := expected.MarshalBinary()
if err != nil {
t.Fatalf("Failed to marshal binary: %v", err)
}
if !bytes.Equal(enc, tx.Data()) {
t.Fatalf("Custom marshal does not match contract bindings")
}
var actual L1BlockInfo
err = actual.UnmarshalBinary(tx.Data())
if err != nil {
t.Fatalf("Failed to unmarshal binary: %v", err)
}
if !cmp.Equal(expected, actual, cmp.Comparer(BigEqual)) {
t.Fatalf("The data did not round trip correctly. expected: %v. actual: %v", expected, actual)
}
})
}
// FuzzUnmarshallLogEvent runs a deposit event through the EVM and checks that output of the abigen parsing matches
// what was inputted and what we parsed during the UnmarshalDepositLogEvent function (which turns it into a deposit tx)
// The purpose is to check that we can never create a transaction that emits a log that we cannot parse as well
// as ensuring that our custom marshalling matches abigen.
func FuzzUnmarshallLogEvent(f *testing.F) {
b := func(i int64) []byte {
return big.NewInt(i).Bytes()
}
type setup struct {
to common.Address
mint int64
value int64
gasLimit uint64
data string
isCreation bool
}
cases := []setup{
{
mint: 100,
value: 50,
gasLimit: 100000,
},
}
for _, c := range cases {
f.Add(c.to.Bytes(), b(c.mint), b(c.value), []byte(c.data), c.gasLimit, c.isCreation)
}
f.Fuzz(func(t *testing.T, _to, _mint, _value, data []byte, l2GasLimit uint64, isCreation bool) {
to := common.BytesToAddress(_to)
mint := BytesToBigInt(_mint)
value := BytesToBigInt(_value)
// Setup opts
opts.Value = mint
opts.GasPrice = common.Big2
opts.GasLimit = 500_000
opts.NoSend = true
opts.Nonce = common.Big0
// Create the deposit transaction
tx, err := portalContract.DepositTransaction(opts, to, value, l2GasLimit, isCreation, data)
if err != nil {
t.Fatal(err)
}
state, err := state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
if err != nil {
t.Fatal(err)
}
state.SetBalance(from, BytesToBigInt([]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}))
state.SetCode(addr, common.FromHex(bindings.OptimismPortalDeployedBin))
_, err = state.Commit(false)
if err != nil {
t.Fatal(err)
}
cfg := runtime.Config{
Origin: from,
Value: tx.Value(),
State: state,
GasLimit: opts.GasLimit,
}
_, _, err = runtime.Call(addr, tx.Data(), &cfg)
logs := state.Logs()
if err == nil && len(logs) != 1 {
t.Fatal("No logs or error after execution")
} else if err != nil {
return
}
// Test that our custom parsing matches the ABI parsing
depositEvent, err := portalContract.ParseTransactionDeposited(*(logs[0]))
if err != nil {
t.Fatalf("Could not parse log that was emitted by the deposit contract: %v", err)
}
depositEvent.Raw = types.Log{} // Clear out the log
// Verify that is passes our custom unmarshalling logic
dep, err := UnmarshalDepositLogEvent(logs[0])
if err != nil {
t.Fatalf("Could not unmarshal log that was emitted by the deposit contract: %v", err)
}
reconstructed := &bindings.OptimismPortalTransactionDeposited{
From: dep.From,
Value: dep.Value,
GasLimit: dep.Gas,
IsCreation: dep.To == nil,
Data: dep.Data,
Raw: types.Log{},
}
if dep.To != nil {
reconstructed.To = *dep.To
}
if dep.Mint != nil {
reconstructed.Mint = dep.Mint
} else {
reconstructed.Mint = common.Big0
}
if !cmp.Equal(depositEvent, reconstructed, cmp.Comparer(BigEqual)) {
t.Fatalf("The deposit tx did not match. tx: %v. actual: %v", reconstructed, depositEvent)
}
inputArgs := &bindings.OptimismPortalTransactionDeposited{
From: from,
To: to,
Mint: mint,
Value: value,
GasLimit: l2GasLimit,
IsCreation: isCreation,
Data: data,
Raw: types.Log{},
}
if !cmp.Equal(depositEvent, inputArgs, cmp.Comparer(BigEqual)) {
t.Fatalf("The input args did not match. input: %v. actual: %v", inputArgs, depositEvent)
}
})
}