-
Notifications
You must be signed in to change notification settings - Fork 446
/
retryable_test.go
367 lines (310 loc) · 12.4 KB
/
retryable_test.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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// Copyright 2021-2022, Offchain Labs, Inc.
// For license information, see https://github.com/nitro/blob/master/LICENSE
package arbtest
import (
"context"
"math/big"
"testing"
"time"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"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/offchainlabs/nitro/arbnode"
"github.com/offchainlabs/nitro/arbos/util"
"github.com/offchainlabs/nitro/arbos/l2pricing"
"github.com/offchainlabs/nitro/solgen/go/bridgegen"
"github.com/offchainlabs/nitro/solgen/go/mocksgen"
"github.com/offchainlabs/nitro/solgen/go/node_interfacegen"
"github.com/offchainlabs/nitro/solgen/go/precompilesgen"
"github.com/offchainlabs/nitro/util/arbmath"
"github.com/offchainlabs/nitro/util/colors"
)
func retryableSetup(t *testing.T) (
*BlockchainTestInfo,
*BlockchainTestInfo,
*ethclient.Client,
*ethclient.Client,
*bridgegen.Inbox,
func(*types.Receipt) common.Hash,
context.Context,
func(),
) {
ctx, cancel := context.WithCancel(context.Background())
l2info, _, l2client, l1info, _, l1client, stack := CreateTestNodeOnL1(t, ctx, true)
l2info.GenerateAccount("User2")
l2info.GenerateAccount("Beneficiary")
l2info.GenerateAccount("Burn")
delayedInbox, err := bridgegen.NewInbox(l1info.GetAddress("Inbox"), l1client)
Require(t, err)
delayedBridge, err := arbnode.NewDelayedBridge(l1client, l1info.GetAddress("Bridge"), 0)
Require(t, err)
lookupSubmitRetryableL2TxHash := func(l1Receipt *types.Receipt) common.Hash {
messages, err := delayedBridge.LookupMessagesInRange(ctx, l1Receipt.BlockNumber, l1Receipt.BlockNumber)
Require(t, err)
if len(messages) != 1 {
Fail(t, "expected 1 message from retryable submission, found", len(messages))
}
txs, err := messages[0].Message.ParseL2Transactions(params.ArbitrumDevTestChainConfig().ChainID)
Require(t, err)
if len(txs) != 1 {
Fail(t, "expected 1 tx from retryable submission, found", len(txs))
}
return txs[0].Hash()
}
// burn some gas so that the faucet's Callvalue + Balance never exceeds a uint256
discard := arbmath.BigMul(big.NewInt(1e12), big.NewInt(1e12))
TransferBalance(t, "Faucet", "Burn", discard, l2info, l2client, ctx)
teardown := func() {
// check the integrity of the RPC
blockNum, err := l2client.BlockNumber(ctx)
Require(t, err, "failed to get L2 block number")
for number := uint64(0); number < blockNum; number++ {
block, err := l2client.BlockByNumber(ctx, arbmath.UintToBig(number))
Require(t, err, "failed to get L2 block", number, "of", blockNum)
if block.Number().Uint64() != number {
Fail(t, "block number mismatch", number, block.Number().Uint64())
}
}
cancel()
stack.Close()
}
return l2info, l1info, l2client, l1client, delayedInbox, lookupSubmitRetryableL2TxHash, ctx, teardown
}
func TestRetryableNoExist(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
_, _, l2client := CreateTestL2(t, ctx)
arbRetryableTx, err := precompilesgen.NewArbRetryableTx(common.HexToAddress("6e"), l2client)
Require(t, err)
_, err = arbRetryableTx.GetTimeout(&bind.CallOpts{}, common.Hash{})
if err.Error() != "error NoTicketWithID()" {
Fail(t, "didn't get expected NoTicketWithID error")
}
}
func TestSubmitRetryableImmediateSuccess(t *testing.T) {
l2info, l1info, l2client, l1client, delayedInbox, lookupSubmitRetryableL2TxHash, ctx, teardown := retryableSetup(t)
defer teardown()
user2Address := l2info.GetAddress("User2")
beneficiaryAddress := l2info.GetAddress("Beneficiary")
deposit := arbmath.BigMul(big.NewInt(1e12), big.NewInt(1e12))
callValue := big.NewInt(1e6)
nodeInterface, err := node_interfacegen.NewNodeInterface(types.NodeInterfaceAddress, l2client)
Require(t, err, "failed to deploy NodeInterface")
// estimate the gas needed to auto-redeem the retryable
usertxoptsL2 := l2info.GetDefaultTransactOpts("Faucet", ctx)
usertxoptsL2.NoSend = true
usertxoptsL2.GasMargin = 0
tx, err := nodeInterface.EstimateRetryableTicket(
&usertxoptsL2,
usertxoptsL2.From,
deposit,
user2Address,
callValue,
beneficiaryAddress,
beneficiaryAddress,
[]byte{0x32, 0x42, 0x32, 0x88}, // increase the cost to beyond that of params.TxGas
)
Require(t, err, "failed to estimate retryable submission")
estimate := tx.Gas()
colors.PrintBlue("estimate: ", estimate)
// submit & auto-redeem the retryable using the gas estimate
usertxoptsL1 := l1info.GetDefaultTransactOpts("Faucet", ctx)
usertxoptsL1.Value = deposit
l1tx, err := delayedInbox.CreateRetryableTicket(
&usertxoptsL1,
user2Address,
callValue,
big.NewInt(1e16),
beneficiaryAddress,
beneficiaryAddress,
arbmath.UintToBig(estimate),
big.NewInt(l2pricing.InitialBaseFeeWei*2),
[]byte{0x32, 0x42, 0x32, 0x88},
)
Require(t, err)
l1receipt, err := EnsureTxSucceeded(ctx, l1client, l1tx)
Require(t, err)
if l1receipt.Status != types.ReceiptStatusSuccessful {
Fail(t, "l1receipt indicated failure")
}
waitForL1DelayBlocks(t, ctx, l1client, l1info)
receipt, err := WaitForTx(ctx, l2client, lookupSubmitRetryableL2TxHash(l1receipt), time.Second*5)
Require(t, err)
if receipt.Status != types.ReceiptStatusSuccessful {
Fail(t)
}
l2balance, err := l2client.BalanceAt(ctx, l2info.GetAddress("User2"), nil)
Require(t, err)
if !arbmath.BigEquals(l2balance, big.NewInt(1e6)) {
Fail(t, "Unexpected balance:", l2balance)
}
}
func TestSubmitRetryableFailThenRetry(t *testing.T) {
l2info, l1info, l2client, l1client, delayedInbox, lookupSubmitRetryableL2TxHash, ctx, teardown := retryableSetup(t)
defer teardown()
ownerTxOpts := l2info.GetDefaultTransactOpts("Owner", ctx)
usertxopts := l1info.GetDefaultTransactOpts("Faucet", ctx)
usertxopts.Value = arbmath.BigMul(big.NewInt(1e12), big.NewInt(1e12))
simpleAddr, _, simple, err := mocksgen.DeploySimple(&ownerTxOpts, l2client)
Require(t, err)
simpleABI, err := mocksgen.SimpleMetaData.GetAbi()
Require(t, err)
beneficiaryAddress := l2info.GetAddress("Beneficiary")
l1tx, err := delayedInbox.CreateRetryableTicket(
&usertxopts,
simpleAddr,
common.Big0,
big.NewInt(1e16),
beneficiaryAddress,
beneficiaryAddress,
// send enough L2 gas for intrinsic but not compute
big.NewInt(int64(params.TxGas+params.TxDataNonZeroGasEIP2028*4)),
big.NewInt(l2pricing.InitialBaseFeeWei*2),
simpleABI.Methods["increment"].ID,
)
Require(t, err)
l1receipt, err := EnsureTxSucceeded(ctx, l1client, l1tx)
Require(t, err)
if l1receipt.Status != types.ReceiptStatusSuccessful {
Fail(t, "l1receipt indicated failure")
}
waitForL1DelayBlocks(t, ctx, l1client, l1info)
receipt, err := WaitForTx(ctx, l2client, lookupSubmitRetryableL2TxHash(l1receipt), time.Second*5)
Require(t, err)
if receipt.Status != types.ReceiptStatusSuccessful {
Fail(t)
}
if len(receipt.Logs) != 2 {
Fail(t, len(receipt.Logs))
}
ticketId := receipt.Logs[0].Topics[1]
firstRetryTxId := receipt.Logs[1].Topics[2]
// get receipt for the auto-redeem, make sure it failed
receipt, err = WaitForTx(ctx, l2client, firstRetryTxId, time.Second*5)
Require(t, err)
if receipt.Status != types.ReceiptStatusFailed {
Fail(t, receipt.GasUsed)
}
arbRetryableTx, err := precompilesgen.NewArbRetryableTx(common.HexToAddress("6e"), l2client)
Require(t, err)
tx, err := arbRetryableTx.Redeem(&ownerTxOpts, ticketId)
Require(t, err)
receipt, err = EnsureTxSucceeded(ctx, l2client, tx)
Require(t, err)
retryTxId := receipt.Logs[0].Topics[2]
// check the receipt for the retry
receipt, err = WaitForTx(ctx, l2client, retryTxId, time.Second*1)
Require(t, err)
if receipt.Status != 1 {
Fail(t, receipt.Status)
}
// verify that the increment happened, so we know the retry succeeded
counter, err := simple.Counter(&bind.CallOpts{})
Require(t, err)
if counter != 1 {
Fail(t, "Unexpected counter:", counter)
}
}
func TestSubmissionGasCosts(t *testing.T) {
l2info, l1info, l2client, l1client, delayedInbox, _, ctx, teardown := retryableSetup(t)
defer teardown()
usertxopts := l1info.GetDefaultTransactOpts("Faucet", ctx)
usertxopts.Value = arbmath.BigMul(big.NewInt(1e12), big.NewInt(1e12))
l2info.GenerateAccount("Refund")
l2info.GenerateAccount("Recieve")
faucetAddress := util.RemapL1Address(l1info.GetAddress("Faucet"))
beneficiaryAddress := l2info.GetAddress("Beneficiary")
feeRefundAddress := l2info.GetAddress("Refund")
receiveAddress := l2info.GetAddress("Recieve")
colors.PrintBlue("Faucet ", faucetAddress)
colors.PrintBlue("Receive ", receiveAddress)
colors.PrintBlue("Beneficiary ", beneficiaryAddress)
colors.PrintBlue("Fee Refund ", feeRefundAddress)
fundsBeforeSubmit, err := l2client.BalanceAt(ctx, faucetAddress, nil)
Require(t, err)
usefulGas := params.TxGas
excessGas := uint64(808)
maxSubmissionFee := big.NewInt(1e13)
retryableGas := arbmath.UintToBig(usefulGas + excessGas) // will only burn the intrinsic cost
retryableL2CallValue := big.NewInt(1e4)
retryableCallData := []byte{}
l1tx, err := delayedInbox.CreateRetryableTicket(
&usertxopts,
receiveAddress,
retryableL2CallValue,
maxSubmissionFee,
feeRefundAddress,
beneficiaryAddress,
retryableGas,
big.NewInt(l2pricing.InitialBaseFeeWei*2),
retryableCallData,
)
Require(t, err)
l1receipt, err := EnsureTxSucceeded(ctx, l1client, l1tx)
Require(t, err)
if l1receipt.Status != types.ReceiptStatusSuccessful {
Fail(t, "l1receipt indicated failure")
}
waitForL1DelayBlocks(t, ctx, l1client, l1info)
l2BaseFee := GetBaseFee(t, l2client, ctx)
excessWei := arbmath.BigMulByUint(l2BaseFee, excessGas)
l1HeaderAfterSubmit, err := l1client.HeaderByHash(ctx, l1receipt.BlockHash)
Require(t, err)
l1BaseFee := l1HeaderAfterSubmit.BaseFee
submitFee := arbmath.BigMulByUint(l1BaseFee, uint64(1400+6*len(retryableCallData)))
submissionFeeRefund := arbmath.BigSub(maxSubmissionFee, submitFee)
fundsAfterSubmit, err := l2client.BalanceAt(ctx, faucetAddress, nil)
Require(t, err)
beneficiaryFunds, err := l2client.BalanceAt(ctx, beneficiaryAddress, nil)
Require(t, err)
refundFunds, err := l2client.BalanceAt(ctx, feeRefundAddress, nil)
Require(t, err)
receiveFunds, err := l2client.BalanceAt(ctx, receiveAddress, nil)
Require(t, err)
colors.PrintBlue("CallGas ", retryableGas)
colors.PrintMint("Gas cost ", arbmath.BigMul(retryableGas, l2BaseFee))
colors.PrintBlue("Payment ", usertxopts.Value)
colors.PrintMint("Faucet before ", fundsBeforeSubmit)
colors.PrintMint("Faucet after ", fundsAfterSubmit)
// the retryable should pay the receiver the supplied callvalue
colors.PrintMint("Receive ", receiveFunds)
colors.PrintBlue("L2 Call Value ", retryableL2CallValue)
if !arbmath.BigEquals(receiveFunds, retryableL2CallValue) {
Fail(t, "Recipient didn't receive the right funds")
}
// the beneficiary should receive nothing
colors.PrintMint("Beneficiary ", beneficiaryFunds)
if beneficiaryFunds.Sign() != 0 {
Fail(t, "The beneficiary shouldn't have received funds")
}
// the fee refund address should recieve the excess gas
colors.PrintBlue("Base Fee ", l2BaseFee)
colors.PrintBlue("Excess Gas ", excessGas)
colors.PrintBlue("Excess Wei ", excessWei)
colors.PrintMint("Fee Refund ", refundFunds)
if !arbmath.BigEquals(refundFunds, arbmath.BigAdd(excessWei, submissionFeeRefund)) {
Fail(t, "The Fee Refund Address didn't receive the right funds")
}
// the faucet must pay for both the gas used and the call value supplied
expectedGasChange := arbmath.BigMul(l2BaseFee, retryableGas)
expectedGasChange = arbmath.BigSub(expectedGasChange, usertxopts.Value) // the user is credited this
expectedGasChange = arbmath.BigAdd(expectedGasChange, maxSubmissionFee)
expectedGasChange = arbmath.BigAdd(expectedGasChange, retryableL2CallValue)
if !arbmath.BigEquals(fundsBeforeSubmit, arbmath.BigAdd(fundsAfterSubmit, expectedGasChange)) {
diff := arbmath.BigSub(fundsBeforeSubmit, fundsAfterSubmit)
colors.PrintRed("Expected ", expectedGasChange)
colors.PrintRed("Observed ", diff)
colors.PrintRed("Off by ", arbmath.BigSub(expectedGasChange, diff))
Fail(t, "Supplied gas was improperly deducted\n", fundsBeforeSubmit, "\n", fundsAfterSubmit)
}
}
func waitForL1DelayBlocks(t *testing.T, ctx context.Context, l1client *ethclient.Client, l1info *BlockchainTestInfo) {
// sending l1 messages creates l1 blocks.. make enough to get that delayed inbox message in
for i := 0; i < 30; i++ {
SendWaitTestTransactions(t, ctx, l1client, []*types.Transaction{
l1info.PrepareTx("Faucet", "User", 30000, big.NewInt(1e12), nil),
})
}
}