-
Notifications
You must be signed in to change notification settings - Fork 333
/
Copy pathfailures.test.ts
372 lines (335 loc) · 13.1 KB
/
failures.test.ts
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
368
369
370
371
372
import {
type AccountWallet,
type AztecAddress,
Fr,
type FunctionCall,
FunctionSelector,
PrivateFeePaymentMethod,
PublicFeePaymentMethod,
TxStatus,
computeSecretHash,
sleep,
} from '@aztec/aztec.js';
import { Gas, GasSettings } from '@aztec/circuits.js';
import { FunctionType } from '@aztec/foundation/abi';
import { type TokenContract as BananaCoin, type FPCContract } from '@aztec/noir-contracts.js';
import { expectMapping } from '../fixtures/utils.js';
import { FeesTest } from './fees_test.js';
describe('e2e_fees failures', () => {
let aliceWallet: AccountWallet;
let aliceAddress: AztecAddress;
let sequencerAddress: AztecAddress;
let bananaCoin: BananaCoin;
let bananaFPC: FPCContract;
let gasSettings: GasSettings;
const t = new FeesTest('failures');
beforeAll(async () => {
await t.applyBaseSnapshots();
await t.applyFPCSetupSnapshot();
({ aliceWallet, aliceAddress, sequencerAddress, bananaCoin, bananaFPC, gasSettings } = await t.setup());
});
afterAll(async () => {
await t.teardown();
});
it('reverts transactions but still pays fees using PrivateFeePaymentMethod', async () => {
const OutrageousPublicAmountAliceDoesNotHave = BigInt(1e8);
const PrivateMintedAlicePrivateBananas = BigInt(1e15);
const [initialAlicePrivateBananas, initialFPCPrivateBananas] = await t.getBananaPrivateBalanceFn(
aliceAddress,
bananaFPC.address,
);
const [initialAlicePublicBananas, initialFPCPublicBananas] = await t.getBananaPublicBalanceFn(
aliceAddress,
bananaFPC.address,
);
const [initialAliceGas, initialFPCGas] = await t.getGasBalanceFn(aliceAddress, bananaFPC.address);
await t.mintPrivateBananas(PrivateMintedAlicePrivateBananas, aliceAddress);
// if we simulate locally, it throws an error
await expect(
bananaCoin.methods
// still use a public transfer so as to fail in the public app logic phase
.transfer_public(aliceAddress, sequencerAddress, OutrageousPublicAmountAliceDoesNotHave, 0)
.send({
fee: {
gasSettings,
paymentMethod: new PrivateFeePaymentMethod(bananaCoin.address, bananaFPC.address, aliceWallet),
},
})
.wait(),
).rejects.toThrow(/attempt to subtract with underflow 'hi == high'/);
// we did not pay the fee, because we did not submit the TX
await expectMapping(
t.getBananaPrivateBalanceFn,
[aliceAddress, bananaFPC.address],
[initialAlicePrivateBananas + PrivateMintedAlicePrivateBananas, initialFPCPrivateBananas],
);
await expectMapping(
t.getBananaPublicBalanceFn,
[aliceAddress, bananaFPC.address],
[initialAlicePublicBananas, initialFPCPublicBananas],
);
await expectMapping(t.getGasBalanceFn, [aliceAddress, bananaFPC.address], [initialAliceGas, initialFPCGas]);
// if we skip simulation, it includes the failed TX
const rebateSecret = Fr.random();
const currentSequencerL1Gas = await t.getCoinbaseBalance();
const txReceipt = await bananaCoin.methods
.transfer_public(aliceAddress, sequencerAddress, OutrageousPublicAmountAliceDoesNotHave, 0)
.send({
skipPublicSimulation: true,
fee: {
gasSettings,
paymentMethod: new PrivateFeePaymentMethod(bananaCoin.address, bananaFPC.address, aliceWallet, rebateSecret),
},
})
.wait({ dontThrowOnRevert: true });
expect(txReceipt.status).toBe(TxStatus.APP_LOGIC_REVERTED);
// We wait until the block is proven since that is when the payout happens.
const bn = await t.aztecNode.getBlockNumber();
while ((await t.aztecNode.getProvenBlockNumber()) < bn) {
await sleep(1000);
}
const feeAmount = txReceipt.transactionFee!;
const newSequencerL1FeeAssetBalance = await t.getCoinbaseBalance();
expect(newSequencerL1FeeAssetBalance).toEqual(currentSequencerL1Gas + feeAmount);
// and thus we paid the fee
await expectMapping(
t.getBananaPrivateBalanceFn,
[aliceAddress, bananaFPC.address],
[
// alice paid the maximum amount in private bananas
initialAlicePrivateBananas + PrivateMintedAlicePrivateBananas - gasSettings.getFeeLimit().toBigInt(),
initialFPCPrivateBananas,
],
);
await expectMapping(
t.getBananaPublicBalanceFn,
[aliceAddress, bananaFPC.address],
[initialAlicePublicBananas, initialFPCPublicBananas + feeAmount],
);
await expectMapping(
t.getGasBalanceFn,
[aliceAddress, bananaFPC.address],
[initialAliceGas, initialFPCGas - feeAmount],
);
// Alice can redeem her shield to get the rebate
const refund = gasSettings.getFeeLimit().toBigInt() - feeAmount;
expect(refund).toBeGreaterThan(0n);
const secretHashForRebate = computeSecretHash(rebateSecret);
await t.addPendingShieldNoteToPXE(t.aliceWallet, refund, secretHashForRebate, txReceipt.txHash);
await bananaCoin.methods.redeem_shield(aliceAddress, refund, rebateSecret).send().wait();
await expectMapping(
t.getBananaPrivateBalanceFn,
[aliceAddress, bananaFPC.address],
[initialAlicePrivateBananas + PrivateMintedAlicePrivateBananas - feeAmount, initialFPCPrivateBananas],
);
});
it('reverts transactions but still pays fees using PublicFeePaymentMethod', async () => {
const OutrageousPublicAmountAliceDoesNotHave = BigInt(1e15);
const PublicMintedAlicePublicBananas = BigInt(1e12);
const [initialAlicePrivateBananas, initialFPCPrivateBananas] = await t.getBananaPrivateBalanceFn(
aliceAddress,
bananaFPC.address,
);
const [initialAlicePublicBananas, initialFPCPublicBananas] = await t.getBananaPublicBalanceFn(
aliceAddress,
bananaFPC.address,
);
const [initialAliceGas, initialFPCGas, initialSequencerGas] = await t.getGasBalanceFn(
aliceAddress,
bananaFPC.address,
sequencerAddress,
);
await bananaCoin.methods.mint_public(aliceAddress, PublicMintedAlicePublicBananas).send().wait();
// if we simulate locally, it throws an error
await expect(
bananaCoin.methods
.transfer_public(aliceAddress, sequencerAddress, OutrageousPublicAmountAliceDoesNotHave, 0)
.send({
fee: {
gasSettings,
paymentMethod: new PublicFeePaymentMethod(bananaCoin.address, bananaFPC.address, aliceWallet),
},
})
.wait(),
).rejects.toThrow(/attempt to subtract with underflow 'hi == high'/);
// we did not pay the fee, because we did not submit the TX
await expectMapping(
t.getBananaPrivateBalanceFn,
[aliceAddress, bananaFPC.address, sequencerAddress],
[initialAlicePrivateBananas, initialFPCPrivateBananas, 0n],
);
await expectMapping(
t.getBananaPublicBalanceFn,
[aliceAddress, bananaFPC.address, sequencerAddress],
[initialAlicePublicBananas + PublicMintedAlicePublicBananas, initialFPCPublicBananas, 0n],
);
await expectMapping(
t.getGasBalanceFn,
[aliceAddress, bananaFPC.address, sequencerAddress],
[initialAliceGas, initialFPCGas, initialSequencerGas],
);
// if we skip simulation, it includes the failed TX
const txReceipt = await bananaCoin.methods
.transfer_public(aliceAddress, sequencerAddress, OutrageousPublicAmountAliceDoesNotHave, 0)
.send({
skipPublicSimulation: true,
fee: {
gasSettings,
paymentMethod: new PublicFeePaymentMethod(bananaCoin.address, bananaFPC.address, aliceWallet),
},
})
.wait({ dontThrowOnRevert: true });
expect(txReceipt.status).toBe(TxStatus.APP_LOGIC_REVERTED);
const feeAmount = txReceipt.transactionFee!;
// and thus we paid the fee
await expectMapping(
t.getBananaPrivateBalanceFn,
[aliceAddress, bananaFPC.address, sequencerAddress],
[initialAlicePrivateBananas, initialFPCPrivateBananas, 0n],
);
await expectMapping(
t.getBananaPublicBalanceFn,
[aliceAddress, bananaFPC.address, sequencerAddress],
[initialAlicePublicBananas + PublicMintedAlicePublicBananas - feeAmount, initialFPCPublicBananas + feeAmount, 0n],
);
await expectMapping(
t.getGasBalanceFn,
[aliceAddress, bananaFPC.address, sequencerAddress],
[initialAliceGas, initialFPCGas - feeAmount, initialSequencerGas],
);
});
it('fails transaction that error in setup', async () => {
const OutrageousPublicAmountAliceDoesNotHave = BigInt(100e12);
// simulation throws an error when setup fails
await expect(
bananaCoin.methods
.transfer_public(aliceAddress, sequencerAddress, OutrageousPublicAmountAliceDoesNotHave, 0)
.send({
fee: {
gasSettings,
paymentMethod: new BuggedSetupFeePaymentMethod(bananaCoin.address, bananaFPC.address, aliceWallet),
},
})
.wait(),
).rejects.toThrow(/unauthorized/);
// so does the sequencer
await expect(
bananaCoin.methods
.transfer_public(aliceAddress, sequencerAddress, OutrageousPublicAmountAliceDoesNotHave, 0)
.send({
skipPublicSimulation: true,
fee: {
gasSettings,
paymentMethod: new BuggedSetupFeePaymentMethod(bananaCoin.address, bananaFPC.address, aliceWallet),
},
})
.wait(),
).rejects.toThrow(/Transaction [0-9a-f]{64} was dropped\. Reason: Tx dropped by P2P node\./);
});
it('includes transaction that error in teardown', async () => {
/**
* We trigger an error in teardown by having the "FPC" call a function that reverts.
*/
const PublicMintedAlicePublicBananas = 100_000_000_000n;
const [initialAlicePrivateBananas, initialFPCPrivateBananas] = await t.getBananaPrivateBalanceFn(
aliceAddress,
bananaFPC.address,
);
const [initialAlicePublicBananas, initialFPCPublicBananas] = await t.getBananaPublicBalanceFn(
aliceAddress,
bananaFPC.address,
);
const [initialAliceGas, initialFPCGas, initialSequencerGas] = await t.getGasBalanceFn(
aliceAddress,
bananaFPC.address,
sequencerAddress,
);
await bananaCoin.methods.mint_public(aliceAddress, PublicMintedAlicePublicBananas).send().wait();
const badGas = GasSettings.from({
gasLimits: gasSettings.gasLimits,
inclusionFee: gasSettings.inclusionFee,
maxFeesPerGas: gasSettings.maxFeesPerGas,
teardownGasLimits: Gas.empty(),
});
await expect(
bananaCoin.methods
.mint_public(aliceAddress, 1n) // random operation
.send({
fee: {
gasSettings: badGas,
paymentMethod: new PublicFeePaymentMethod(bananaCoin.address, bananaFPC.address, aliceWallet),
},
})
.wait(),
).rejects.toThrow();
const receipt = await bananaCoin.methods
.mint_public(aliceAddress, 1n) // random operation
.send({
skipPublicSimulation: true,
fee: {
gasSettings: badGas,
paymentMethod: new PublicFeePaymentMethod(bananaCoin.address, bananaFPC.address, aliceWallet),
},
})
.wait({
dontThrowOnRevert: true,
});
expect(receipt.status).toEqual(TxStatus.TEARDOWN_REVERTED);
expect(receipt.transactionFee).toBeGreaterThan(0n);
await expectMapping(
t.getBananaPrivateBalanceFn,
[aliceAddress, bananaFPC.address, sequencerAddress],
[initialAlicePrivateBananas, initialFPCPrivateBananas, 0n],
);
// Since setup went through, Alice transferred to the FPC
await expectMapping(
t.getBananaPublicBalanceFn,
[aliceAddress, bananaFPC.address, sequencerAddress],
[
initialAlicePublicBananas + PublicMintedAlicePublicBananas - badGas.getFeeLimit().toBigInt(),
initialFPCPublicBananas + badGas.getFeeLimit().toBigInt(),
0n,
],
);
await expectMapping(
t.getGasBalanceFn,
[aliceAddress, bananaFPC.address, sequencerAddress],
[initialAliceGas, initialFPCGas - receipt.transactionFee!, initialSequencerGas],
);
});
});
class BuggedSetupFeePaymentMethod extends PublicFeePaymentMethod {
override getFunctionCalls(gasSettings: GasSettings): Promise<FunctionCall[]> {
const maxFee = gasSettings.getFeeLimit();
const nonce = Fr.random();
const tooMuchFee = new Fr(maxFee.toBigInt() * 2n);
return Promise.resolve([
this.wallet
.setPublicAuthWit(
{
caller: this.paymentContract,
action: {
name: 'transfer_public',
args: [this.wallet.getAddress(), this.paymentContract, maxFee, nonce],
selector: FunctionSelector.fromSignature('transfer_public((Field),(Field),Field,Field)'),
type: FunctionType.PUBLIC,
isStatic: false,
to: this.asset,
returnTypes: [],
},
},
true,
)
.request(),
{
name: 'fee_entrypoint_public',
to: this.paymentContract,
selector: FunctionSelector.fromSignature('fee_entrypoint_public(Field,(Field),Field)'),
type: FunctionType.PRIVATE,
isStatic: false,
args: [tooMuchFee, this.asset, nonce],
returnTypes: [],
},
]);
}
}