forked from scroll-tech/scroll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnforcedTxGateway.spec.ts
454 lines (416 loc) · 15.9 KB
/
EnforcedTxGateway.spec.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/* eslint-disable node/no-unpublished-import */
/* eslint-disable node/no-missing-import */
import { expect } from "chai";
import { BigNumberish, BytesLike, constants } from "ethers";
import { ethers } from "hardhat";
import { EnforcedTxGateway, L1MessageQueue, L2GasPriceOracle, MockCaller } from "../typechain";
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
import { arrayify } from "ethers/lib/utils";
describe("EnforcedTxGateway.spec", async () => {
let deployer: SignerWithAddress;
let feeVault: SignerWithAddress;
let signer: SignerWithAddress;
let caller: MockCaller;
let gateway: EnforcedTxGateway;
let oracle: L2GasPriceOracle;
let queue: L1MessageQueue;
const deployProxy = async (name: string, admin: string): Promise<string> => {
const TransparentUpgradeableProxy = await ethers.getContractFactory("TransparentUpgradeableProxy", deployer);
const Factory = await ethers.getContractFactory(name, deployer);
const impl = await Factory.deploy();
await impl.deployed();
const proxy = await TransparentUpgradeableProxy.deploy(impl.address, admin, "0x");
await proxy.deployed();
return proxy.address;
};
beforeEach(async () => {
[deployer, feeVault, signer] = await ethers.getSigners();
const ProxyAdmin = await ethers.getContractFactory("ProxyAdmin", deployer);
const admin = await ProxyAdmin.deploy();
await admin.deployed();
queue = await ethers.getContractAt("L1MessageQueue", await deployProxy("L1MessageQueue", admin.address), deployer);
oracle = await ethers.getContractAt(
"L2GasPriceOracle",
await deployProxy("L2GasPriceOracle", admin.address),
deployer
);
gateway = await ethers.getContractAt(
"EnforcedTxGateway",
await deployProxy("EnforcedTxGateway", admin.address),
deployer
);
const MockCaller = await ethers.getContractFactory("MockCaller", deployer);
caller = await MockCaller.deploy();
await caller.deployed();
await queue.initialize(constants.AddressZero, constants.AddressZero, gateway.address, oracle.address, 10000000);
await gateway.initialize(queue.address, feeVault.address);
await oracle.initialize(21000, 51000, 8, 16);
const Whitelist = await ethers.getContractFactory("Whitelist", deployer);
const whitelist = await Whitelist.deploy(deployer.address);
await whitelist.deployed();
await whitelist.updateWhitelistStatus([deployer.address], true);
await oracle.updateWhitelist(whitelist.address);
await oracle.setL2BaseFee(1);
});
context("auth", async () => {
it("should initialize correctly", async () => {
expect(await gateway.owner()).to.eq(deployer.address);
expect(await gateway.messageQueue()).to.eq(queue.address);
expect(await gateway.feeVault()).to.eq(feeVault.address);
expect(await gateway.paused()).to.eq(false);
});
it("should revert, when initialize again", async () => {
await expect(gateway.initialize(constants.AddressZero, constants.AddressZero)).to.revertedWith(
"Initializable: contract is already initialized"
);
});
context("#updateFeeVault", async () => {
it("should revert, when non-owner call", async () => {
await expect(gateway.connect(signer).updateFeeVault(constants.AddressZero)).to.revertedWith(
"Ownable: caller is not the owner"
);
});
it("should succeed", async () => {
expect(await gateway.feeVault()).to.eq(feeVault.address);
await expect(gateway.updateFeeVault(deployer.address))
.to.emit(gateway, "UpdateFeeVault")
.withArgs(feeVault.address, deployer.address);
expect(await gateway.feeVault()).to.eq(deployer.address);
});
});
context("#setPause", async () => {
it("should revert, when non-owner call", async () => {
await expect(gateway.connect(signer).setPause(false)).to.revertedWith("Ownable: caller is not the owner");
});
it("should succeed", async () => {
expect(await gateway.paused()).to.eq(false);
await expect(gateway.setPause(true)).to.emit(gateway, "Paused").withArgs(deployer.address);
expect(await gateway.paused()).to.eq(true);
await expect(gateway.setPause(false)).to.emit(gateway, "Unpaused").withArgs(deployer.address);
expect(await gateway.paused()).to.eq(false);
});
});
});
context("#sendTransaction, by EOA", async () => {
it("should revert, when contract is paused", async () => {
await gateway.setPause(true);
await expect(
gateway.connect(signer)["sendTransaction(address,uint256,uint256,bytes)"](signer.address, 0, 0, "0x")
).to.revertedWith("Pausable: paused");
});
it("should revert, when call is not EOA", async () => {
const tx = await gateway.populateTransaction["sendTransaction(address,uint256,uint256,bytes)"](
signer.address,
0,
0,
"0x"
);
await expect(caller.callTarget(gateway.address, tx.data!)).to.revertedWith(
"Only EOA senders are allowed to send enforced transaction"
);
});
it("should revert, when insufficient value for fee", async () => {
const fee = await queue.estimateCrossDomainMessageFee(1000000);
await expect(
gateway
.connect(signer)
["sendTransaction(address,uint256,uint256,bytes)"](signer.address, 0, 1000000, "0x", { value: fee.sub(1) })
).to.revertedWith("Insufficient value for fee");
});
it("should revert, when failed to deduct the fee", async () => {
await gateway.updateFeeVault(gateway.address);
const fee = await queue.estimateCrossDomainMessageFee(1000000);
await expect(
gateway
.connect(signer)
["sendTransaction(address,uint256,uint256,bytes)"](signer.address, 0, 1000000, "0x", { value: fee })
).to.revertedWith("Failed to deduct the fee");
});
it("should succeed, no refund", async () => {
const fee = await queue.estimateCrossDomainMessageFee(1000000);
const feeVaultBalanceBefore = await ethers.provider.getBalance(feeVault.address);
await expect(
gateway
.connect(signer)
["sendTransaction(address,uint256,uint256,bytes)"](deployer.address, 0, 1000000, "0x", { value: fee })
)
.to.emit(queue, "QueueTransaction")
.withArgs(signer.address, deployer.address, 0, 0, 1000000, "0x");
const feeVaultBalanceAfter = await ethers.provider.getBalance(feeVault.address);
expect(feeVaultBalanceAfter.sub(feeVaultBalanceBefore)).to.eq(fee);
});
it("should succeed, with refund", async () => {
const fee = await queue.estimateCrossDomainMessageFee(1000000);
const feeVaultBalanceBefore = await ethers.provider.getBalance(feeVault.address);
const signerBalanceBefore = await ethers.provider.getBalance(signer.address);
const tx = await gateway
.connect(signer)
["sendTransaction(address,uint256,uint256,bytes)"](deployer.address, 0, 1000000, "0x", { value: fee.add(100) });
await expect(tx)
.to.emit(queue, "QueueTransaction")
.withArgs(signer.address, deployer.address, 0, 0, 1000000, "0x");
const receipt = await tx.wait();
const feeVaultBalanceAfter = await ethers.provider.getBalance(feeVault.address);
const signerBalanceAfter = await ethers.provider.getBalance(signer.address);
expect(feeVaultBalanceAfter.sub(feeVaultBalanceBefore)).to.eq(fee);
expect(signerBalanceBefore.sub(signerBalanceAfter)).to.eq(
receipt.gasUsed.mul(receipt.effectiveGasPrice).add(fee)
);
});
});
context("#sendTransaction, with signatures", async () => {
const getSignature = async (
signer: SignerWithAddress,
target: string,
value: BigNumberish,
gasLimit: BigNumberish,
data: BytesLike
) => {
const enforcedTx = {
sender: signer.address,
target: target,
value: value,
gasLimit: gasLimit,
data: arrayify(data),
nonce: await gateway.nonces(signer.address),
deadline: constants.MaxUint256,
};
const domain = {
name: "EnforcedTxGateway",
version: "1",
chainId: (await ethers.provider.getNetwork()).chainId,
verifyingContract: gateway.address,
};
const types = {
EnforcedTransaction: [
{
name: "sender",
type: "address",
},
{
name: "target",
type: "address",
},
{
name: "value",
type: "uint256",
},
{
name: "gasLimit",
type: "uint256",
},
{
name: "data",
type: "bytes",
},
{
name: "nonce",
type: "uint256",
},
{
name: "deadline",
type: "uint256",
},
],
};
const signature = await signer._signTypedData(domain, types, enforcedTx);
return signature;
};
it("should revert, when contract is paused", async () => {
await gateway.setPause(true);
await expect(
gateway
.connect(deployer)
["sendTransaction(address,address,uint256,uint256,bytes,uint256,bytes,address)"](
signer.address,
signer.address,
0,
0,
"0x",
constants.MaxUint256,
"0x",
constants.AddressZero
)
).to.revertedWith("Pausable: paused");
});
it("should revert, when signature expired", async () => {
const timestamp = (await ethers.provider.getBlock("latest")).timestamp;
await expect(
gateway
.connect(deployer)
["sendTransaction(address,address,uint256,uint256,bytes,uint256,bytes,address)"](
signer.address,
signer.address,
0,
0,
"0x",
timestamp - 1,
"0x",
constants.AddressZero
)
).to.revertedWith("signature expired");
});
it("should revert, when signature is wrong", async () => {
const signature = await signer.signMessage("0x00");
await expect(
gateway
.connect(deployer)
["sendTransaction(address,address,uint256,uint256,bytes,uint256,bytes,address)"](
signer.address,
signer.address,
0,
0,
"0x",
constants.MaxUint256,
signature,
constants.AddressZero
)
).to.revertedWith("Incorrect signature");
});
it("should revert, when insufficient value for fee", async () => {
const signature = await getSignature(signer, signer.address, 0, 1000000, "0x");
const fee = await queue.estimateCrossDomainMessageFee(1000000);
await expect(
gateway
.connect(deployer)
["sendTransaction(address,address,uint256,uint256,bytes,uint256,bytes,address)"](
signer.address,
signer.address,
0,
1000000,
"0x",
constants.MaxUint256,
signature,
signer.address,
{ value: fee.sub(1) }
)
).to.revertedWith("Insufficient value for fee");
});
it("should revert, when failed to deduct the fee", async () => {
await gateway.updateFeeVault(gateway.address);
const signature = await getSignature(signer, signer.address, 0, 1000000, "0x");
const fee = await queue.estimateCrossDomainMessageFee(1000000);
await expect(
gateway
.connect(deployer)
["sendTransaction(address,address,uint256,uint256,bytes,uint256,bytes,address)"](
signer.address,
signer.address,
0,
1000000,
"0x",
constants.MaxUint256,
signature,
signer.address,
{ value: fee }
)
).to.revertedWith("Failed to deduct the fee");
});
it("should succeed, no refund", async () => {
const signature = await getSignature(signer, deployer.address, 0, 1000000, "0x");
const fee = await queue.estimateCrossDomainMessageFee(1000000);
const feeVaultBalanceBefore = await ethers.provider.getBalance(feeVault.address);
expect(await gateway.nonces(signer.address)).to.eq(0);
await expect(
gateway
.connect(deployer)
["sendTransaction(address,address,uint256,uint256,bytes,uint256,bytes,address)"](
signer.address,
deployer.address,
0,
1000000,
"0x",
constants.MaxUint256,
signature,
signer.address,
{ value: fee }
)
)
.to.emit(queue, "QueueTransaction")
.withArgs(signer.address, deployer.address, 0, 0, 1000000, "0x");
expect(await gateway.nonces(signer.address)).to.eq(1);
const feeVaultBalanceAfter = await ethers.provider.getBalance(feeVault.address);
expect(feeVaultBalanceAfter.sub(feeVaultBalanceBefore)).to.eq(fee);
// use the same nonce to sign should fail
await expect(
gateway
.connect(deployer)
["sendTransaction(address,address,uint256,uint256,bytes,uint256,bytes,address)"](
signer.address,
deployer.address,
0,
1000000,
"0x",
constants.MaxUint256,
signature,
signer.address,
{ value: fee }
)
).to.revertedWith("Incorrect signature");
});
it("should succeed, with refund", async () => {
const signature = await getSignature(signer, deployer.address, 0, 1000000, "0x");
const fee = await queue.estimateCrossDomainMessageFee(1000000);
const feeVaultBalanceBefore = await ethers.provider.getBalance(feeVault.address);
const signerBalanceBefore = await ethers.provider.getBalance(signer.address);
expect(await gateway.nonces(signer.address)).to.eq(0);
await expect(
gateway
.connect(deployer)
["sendTransaction(address,address,uint256,uint256,bytes,uint256,bytes,address)"](
signer.address,
deployer.address,
0,
1000000,
"0x",
constants.MaxUint256,
signature,
signer.address,
{ value: fee.add(100) }
)
)
.to.emit(queue, "QueueTransaction")
.withArgs(signer.address, deployer.address, 0, 0, 1000000, "0x");
expect(await gateway.nonces(signer.address)).to.eq(1);
const feeVaultBalanceAfter = await ethers.provider.getBalance(feeVault.address);
const signerBalanceAfter = await ethers.provider.getBalance(signer.address);
expect(feeVaultBalanceAfter.sub(feeVaultBalanceBefore)).to.eq(fee);
expect(signerBalanceAfter.sub(signerBalanceBefore)).to.eq(100);
// use the same nonce to sign should fail
await expect(
gateway
.connect(deployer)
["sendTransaction(address,address,uint256,uint256,bytes,uint256,bytes,address)"](
signer.address,
deployer.address,
0,
1000000,
"0x",
constants.MaxUint256,
signature,
signer.address,
{ value: fee.add(100) }
)
).to.revertedWith("Incorrect signature");
});
it("should revert, when refund failed", async () => {
const signature = await getSignature(signer, signer.address, 0, 1000000, "0x1234");
const fee = await queue.estimateCrossDomainMessageFee(1000000);
await expect(
gateway
.connect(deployer)
["sendTransaction(address,address,uint256,uint256,bytes,uint256,bytes,address)"](
signer.address,
signer.address,
0,
1000000,
"0x1234",
constants.MaxUint256,
signature,
gateway.address,
{ value: fee.add(100) }
)
).to.revertedWith("Failed to refund the fee");
});
});
});