forked from scroll-tech/scroll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
L1ERC721Gateway.t.sol
952 lines (815 loc) · 34.9 KB
/
L1ERC721Gateway.t.sol
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
// SPDX-License-Identifier: MIT
pragma solidity =0.8.24;
import {MockERC721} from "solmate/test/utils/mocks/MockERC721.sol";
import {ERC721TokenReceiver} from "solmate/tokens/ERC721.sol";
import {ITransparentUpgradeableProxy} from "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";
import {IL1ERC721Gateway, L1ERC721Gateway} from "../L1/gateways/L1ERC721Gateway.sol";
import {IL1ScrollMessenger} from "../L1/IL1ScrollMessenger.sol";
import {IL2ERC721Gateway, L2ERC721Gateway} from "../L2/gateways/L2ERC721Gateway.sol";
import {AddressAliasHelper} from "../libraries/common/AddressAliasHelper.sol";
import {ScrollConstants} from "../libraries/constants/ScrollConstants.sol";
import {L1GatewayTestBase} from "./L1GatewayTestBase.t.sol";
import {MockScrollMessenger} from "./mocks/MockScrollMessenger.sol";
import {MockERC721Recipient} from "./mocks/MockERC721Recipient.sol";
contract L1ERC721GatewayTest is L1GatewayTestBase, ERC721TokenReceiver {
// from L1ERC721Gateway
event FinalizeWithdrawERC721(
address indexed _l1Token,
address indexed _l2Token,
address indexed _from,
address _to,
uint256 _tokenId
);
event FinalizeBatchWithdrawERC721(
address indexed _l1Token,
address indexed _l2Token,
address indexed _from,
address _to,
uint256[] _tokenIds
);
event DepositERC721(
address indexed _l1Token,
address indexed _l2Token,
address indexed _from,
address _to,
uint256 _tokenId
);
event BatchDepositERC721(
address indexed _l1Token,
address indexed _l2Token,
address indexed _from,
address _to,
uint256[] _tokenIds
);
event RefundERC721(address indexed token, address indexed recipient, uint256 tokenId);
event BatchRefundERC721(address indexed token, address indexed recipient, uint256[] tokenIds);
uint256 private constant TOKEN_COUNT = 100;
L1ERC721Gateway private gateway;
L2ERC721Gateway private counterpartGateway;
MockERC721 private l1Token;
MockERC721 private l2Token;
MockERC721Recipient private mockRecipient;
function setUp() public {
__L1GatewayTestBase_setUp();
// Deploy tokens
l1Token = new MockERC721("Mock L1", "ML1");
l2Token = new MockERC721("Mock L2", "ML1");
// Deploy L2 contracts
counterpartGateway = new L2ERC721Gateway(address(1), address(1));
// Deploy L1 contracts
gateway = _deployGateway(address(l1Messenger));
// Initialize L1 contracts
gateway.initialize(address(counterpartGateway), address(l1Messenger));
// Prepare token balances
for (uint256 i = 0; i < TOKEN_COUNT; i++) {
l1Token.mint(address(this), i);
}
l1Token.setApprovalForAll(address(gateway), true);
mockRecipient = new MockERC721Recipient();
}
function testInitialized() public {
assertEq(address(counterpartGateway), gateway.counterpart());
assertEq(address(0), gateway.router());
assertEq(address(l1Messenger), gateway.messenger());
assertEq(address(0), gateway.tokenMapping(address(l1Token)));
hevm.expectRevert("Initializable: contract is already initialized");
gateway.initialize(address(1), address(l1Messenger));
}
function testUpdateTokenMappingFailed(address token1) public {
// call by non-owner, should revert
hevm.startPrank(address(1));
hevm.expectRevert("Ownable: caller is not the owner");
gateway.updateTokenMapping(token1, token1);
hevm.stopPrank();
// l2 token is zero, should revert
hevm.expectRevert("token address cannot be 0");
gateway.updateTokenMapping(token1, address(0));
}
function testUpdateTokenMappingSuccess(address token1, address token2) public {
hevm.assume(token2 != address(0));
assertEq(gateway.tokenMapping(token1), address(0));
gateway.updateTokenMapping(token1, token2);
assertEq(gateway.tokenMapping(token1), token2);
}
function testDepositERC721(
uint256 tokenId,
uint256 gasLimit,
uint256 feePerGas
) public {
_testDepositERC721(tokenId, gasLimit, feePerGas);
}
function testDepositERC721WithRecipient(
uint256 tokenId,
address to,
uint256 gasLimit,
uint256 feePerGas
) public {
_testDepositERC721WithRecipient(tokenId, to, gasLimit, feePerGas);
}
function testBatchDepositERC721WithGatewaySuccess(
uint256 tokenCount,
uint256 gasLimit,
uint256 feePerGas
) public {
_testBatchDepositERC721(tokenCount, gasLimit, feePerGas);
}
/// @dev batch deposit erc721 with recipient
function testBatchDepositERC721WithGatewaySuccess(
uint256 tokenCount,
address recipient,
uint256 gasLimit,
uint256 feePerGas
) public {
_testBatchDepositERC721WithRecipient(tokenCount, recipient, gasLimit, feePerGas);
}
function testDropMessageMocking() public {
MockScrollMessenger mockMessenger = new MockScrollMessenger();
gateway = _deployGateway(address(mockMessenger));
gateway.initialize(address(counterpartGateway), address(mockMessenger));
// only messenger can call, revert
hevm.expectRevert(ErrorCallerIsNotMessenger.selector);
gateway.onDropMessage(new bytes(0));
// only called in drop context, revert
hevm.expectRevert(ErrorNotInDropMessageContext.selector);
mockMessenger.callTarget(
address(gateway),
abi.encodeWithSelector(gateway.onDropMessage.selector, new bytes(0))
);
mockMessenger.setXDomainMessageSender(ScrollConstants.DROP_XDOMAIN_MESSAGE_SENDER);
// invalid selector, revert
hevm.expectRevert("invalid selector");
mockMessenger.callTarget(
address(gateway),
abi.encodeWithSelector(gateway.onDropMessage.selector, new bytes(4))
);
bytes memory message = abi.encodeWithSelector(
IL2ERC721Gateway.finalizeDepositERC721.selector,
address(l1Token),
address(l2Token),
address(this),
address(this),
0
);
// nonzero msg.value, revert
hevm.expectRevert("nonzero msg.value");
mockMessenger.callTarget{value: 1}(
address(gateway),
abi.encodeWithSelector(gateway.onDropMessage.selector, message)
);
}
function testDropMessage(uint256 tokenId) public {
gateway.updateTokenMapping(address(l1Token), address(l2Token));
tokenId = bound(tokenId, 0, TOKEN_COUNT - 1);
bytes memory message = abi.encodeWithSelector(
IL2ERC721Gateway.finalizeDepositERC721.selector,
address(l1Token),
address(l2Token),
address(this),
address(this),
tokenId
);
gateway.depositERC721(address(l1Token), tokenId, defaultGasLimit);
// skip message 0
hevm.startPrank(address(rollup));
messageQueue.popCrossDomainMessage(0, 1, 0x1);
assertEq(messageQueue.pendingQueueIndex(), 1);
hevm.stopPrank();
// drop message 0
hevm.expectEmit(true, true, false, true);
emit RefundERC721(address(l1Token), address(this), tokenId);
assertEq(l1Token.ownerOf(tokenId), address(gateway));
l1Messenger.dropMessage(address(gateway), address(counterpartGateway), 0, 0, message);
assertEq(l1Token.ownerOf(tokenId), address(this));
}
function testDropMessageBatch(uint256 tokenCount) public {
tokenCount = bound(tokenCount, 1, TOKEN_COUNT);
gateway.updateTokenMapping(address(l1Token), address(l2Token));
uint256[] memory _tokenIds = new uint256[](tokenCount);
for (uint256 i = 0; i < tokenCount; i++) {
_tokenIds[i] = i;
}
bytes memory message = abi.encodeWithSelector(
IL2ERC721Gateway.finalizeBatchDepositERC721.selector,
address(l1Token),
address(l2Token),
address(this),
address(this),
_tokenIds
);
gateway.batchDepositERC721(address(l1Token), _tokenIds, defaultGasLimit);
// skip message 0
hevm.startPrank(address(rollup));
messageQueue.popCrossDomainMessage(0, 1, 0x1);
assertEq(messageQueue.pendingQueueIndex(), 1);
hevm.stopPrank();
// drop message 0
hevm.expectEmit(true, true, false, true);
emit BatchRefundERC721(address(l1Token), address(this), _tokenIds);
for (uint256 i = 0; i < tokenCount; i++) {
assertEq(l1Token.ownerOf(_tokenIds[i]), address(gateway));
}
l1Messenger.dropMessage(address(gateway), address(counterpartGateway), 0, 0, message);
for (uint256 i = 0; i < tokenCount; i++) {
assertEq(l1Token.ownerOf(_tokenIds[i]), address(this));
}
}
function testFinalizeWithdrawERC721FailedMocking(
address sender,
address recipient,
uint256 tokenId
) public {
tokenId = bound(tokenId, 0, TOKEN_COUNT - 1);
// revert when caller is not messenger
hevm.expectRevert(ErrorCallerIsNotMessenger.selector);
gateway.finalizeWithdrawERC721(address(l1Token), address(l2Token), sender, recipient, tokenId);
MockScrollMessenger mockMessenger = new MockScrollMessenger();
gateway = _deployGateway(address(mockMessenger));
gateway.initialize(address(counterpartGateway), address(mockMessenger));
// only call by counterpart
hevm.expectRevert(ErrorCallerIsNotCounterpartGateway.selector);
mockMessenger.callTarget(
address(gateway),
abi.encodeWithSelector(
gateway.finalizeWithdrawERC721.selector,
address(l1Token),
address(l2Token),
sender,
recipient,
tokenId
)
);
mockMessenger.setXDomainMessageSender(address(counterpartGateway));
// msg.value mismatch
hevm.expectRevert("l2 token mismatch");
mockMessenger.callTarget(
address(gateway),
abi.encodeWithSelector(
gateway.finalizeWithdrawERC721.selector,
address(l1Token),
address(l2Token),
sender,
recipient,
tokenId
)
);
}
function testFinalizeWithdrawERC721Failed(
address sender,
address recipient,
uint256 tokenId
) public {
hevm.assume(recipient != address(0));
tokenId = bound(tokenId, 0, TOKEN_COUNT - 1);
gateway.updateTokenMapping(address(l1Token), address(l2Token));
gateway.depositERC721(address(l1Token), tokenId, defaultGasLimit);
// do finalize withdraw token
bytes memory message = abi.encodeWithSelector(
IL1ERC721Gateway.finalizeWithdrawERC721.selector,
address(l1Token),
address(l2Token),
sender,
recipient,
tokenId
);
bytes memory xDomainCalldata = abi.encodeWithSignature(
"relayMessage(address,address,uint256,uint256,bytes)",
address(uint160(address(counterpartGateway)) + 1),
address(gateway),
0,
0,
message
);
prepareL2MessageRoot(keccak256(xDomainCalldata));
IL1ScrollMessenger.L2MessageProof memory proof;
proof.batchIndex = rollup.lastFinalizedBatchIndex();
// counterpart is not L2WETHGateway
// emit FailedRelayedMessage from L1ScrollMessenger
hevm.expectEmit(true, false, false, true);
emit FailedRelayedMessage(keccak256(xDomainCalldata));
assertEq(address(gateway), l1Token.ownerOf(tokenId));
uint256 gatewayBalance = l1Token.balanceOf(address(gateway));
uint256 recipientBalance = l1Token.balanceOf(recipient);
assertBoolEq(false, l1Messenger.isL2MessageExecuted(keccak256(xDomainCalldata)));
l1Messenger.relayMessageWithProof(
address(uint160(address(counterpartGateway)) + 1),
address(gateway),
0,
0,
message,
proof
);
assertEq(address(gateway), l1Token.ownerOf(tokenId));
assertEq(gatewayBalance, l1Token.balanceOf(address(gateway)));
assertEq(recipientBalance, l1Token.balanceOf(recipient));
assertBoolEq(false, l1Messenger.isL2MessageExecuted(keccak256(xDomainCalldata)));
}
function testFinalizeWithdrawERC721(
address sender,
address recipient,
uint256 tokenId
) public {
uint256 size;
assembly {
size := extcodesize(recipient)
}
hevm.assume(size == 0);
hevm.assume(recipient != address(0));
tokenId = bound(tokenId, 0, TOKEN_COUNT - 1);
gateway.updateTokenMapping(address(l1Token), address(l2Token));
gateway.depositERC721(address(l1Token), tokenId, defaultGasLimit);
// do finalize withdraw token
bytes memory message = abi.encodeWithSelector(
IL1ERC721Gateway.finalizeWithdrawERC721.selector,
address(l1Token),
address(l2Token),
sender,
recipient,
tokenId
);
bytes memory xDomainCalldata = abi.encodeWithSignature(
"relayMessage(address,address,uint256,uint256,bytes)",
address(counterpartGateway),
address(gateway),
0,
0,
message
);
prepareL2MessageRoot(keccak256(xDomainCalldata));
IL1ScrollMessenger.L2MessageProof memory proof;
proof.batchIndex = rollup.lastFinalizedBatchIndex();
// emit FinalizeWithdrawERC721 from L1ERC721Gateway
{
hevm.expectEmit(true, true, true, true);
emit FinalizeWithdrawERC721(address(l1Token), address(l2Token), sender, recipient, tokenId);
}
// emit RelayedMessage from L1ScrollMessenger
{
hevm.expectEmit(true, false, false, true);
emit RelayedMessage(keccak256(xDomainCalldata));
}
assertEq(address(gateway), l1Token.ownerOf(tokenId));
uint256 gatewayBalance = l1Token.balanceOf(address(gateway));
uint256 recipientBalance = l1Token.balanceOf(recipient);
assertBoolEq(false, l1Messenger.isL2MessageExecuted(keccak256(xDomainCalldata)));
l1Messenger.relayMessageWithProof(address(counterpartGateway), address(gateway), 0, 0, message, proof);
assertEq(recipient, l1Token.ownerOf(tokenId));
assertEq(gatewayBalance - 1, l1Token.balanceOf(address(gateway)));
assertEq(recipientBalance + 1, l1Token.balanceOf(recipient));
assertBoolEq(true, l1Messenger.isL2MessageExecuted(keccak256(xDomainCalldata)));
}
function testFinalizeBatchWithdrawERC721FailedMocking(
address sender,
address recipient,
uint256 tokenCount
) public {
tokenCount = bound(tokenCount, 1, TOKEN_COUNT);
uint256[] memory _tokenIds = new uint256[](tokenCount);
for (uint256 i = 0; i < tokenCount; i++) {
_tokenIds[i] = i;
}
// revert when caller is not messenger
hevm.expectRevert(ErrorCallerIsNotMessenger.selector);
gateway.finalizeBatchWithdrawERC721(address(l1Token), address(l2Token), sender, recipient, _tokenIds);
MockScrollMessenger mockMessenger = new MockScrollMessenger();
gateway = _deployGateway(address(mockMessenger));
gateway.initialize(address(counterpartGateway), address(mockMessenger));
// only call by counterpart
hevm.expectRevert(ErrorCallerIsNotCounterpartGateway.selector);
mockMessenger.callTarget(
address(gateway),
abi.encodeWithSelector(
gateway.finalizeBatchWithdrawERC721.selector,
address(l1Token),
address(l2Token),
sender,
recipient,
_tokenIds
)
);
mockMessenger.setXDomainMessageSender(address(counterpartGateway));
// msg.value mismatch
hevm.expectRevert("l2 token mismatch");
mockMessenger.callTarget(
address(gateway),
abi.encodeWithSelector(
gateway.finalizeBatchWithdrawERC721.selector,
address(l1Token),
address(l2Token),
sender,
recipient,
_tokenIds
)
);
}
function testFinalizeBatchWithdrawERC721Failed(
address sender,
address recipient,
uint256 tokenCount
) public {
hevm.assume(recipient != address(0));
tokenCount = bound(tokenCount, 1, TOKEN_COUNT);
uint256[] memory _tokenIds = new uint256[](tokenCount);
for (uint256 i = 0; i < tokenCount; i++) {
_tokenIds[i] = i;
}
gateway.updateTokenMapping(address(l1Token), address(l2Token));
gateway.batchDepositERC721(address(l1Token), _tokenIds, defaultGasLimit);
// do finalize withdraw token
bytes memory message = abi.encodeWithSelector(
IL1ERC721Gateway.finalizeBatchWithdrawERC721.selector,
address(l1Token),
address(l2Token),
sender,
recipient,
_tokenIds
);
bytes memory xDomainCalldata = abi.encodeWithSignature(
"relayMessage(address,address,uint256,uint256,bytes)",
address(uint160(address(counterpartGateway)) + 1),
address(gateway),
0,
0,
message
);
prepareL2MessageRoot(keccak256(xDomainCalldata));
IL1ScrollMessenger.L2MessageProof memory proof;
proof.batchIndex = rollup.lastFinalizedBatchIndex();
// counterpart is not L2WETHGateway
// emit FailedRelayedMessage from L1ScrollMessenger
hevm.expectEmit(true, false, false, true);
emit FailedRelayedMessage(keccak256(xDomainCalldata));
for (uint256 i = 0; i < tokenCount; i++) {
assertEq(address(gateway), l1Token.ownerOf(_tokenIds[i]));
}
uint256 gatewayBalance = l1Token.balanceOf(address(gateway));
uint256 recipientBalance = l1Token.balanceOf(recipient);
assertBoolEq(false, l1Messenger.isL2MessageExecuted(keccak256(xDomainCalldata)));
l1Messenger.relayMessageWithProof(
address(uint160(address(counterpartGateway)) + 1),
address(gateway),
0,
0,
message,
proof
);
for (uint256 i = 0; i < tokenCount; i++) {
assertEq(address(gateway), l1Token.ownerOf(_tokenIds[i]));
}
assertEq(gatewayBalance, l1Token.balanceOf(address(gateway)));
assertEq(recipientBalance, l1Token.balanceOf(recipient));
assertBoolEq(false, l1Messenger.isL2MessageExecuted(keccak256(xDomainCalldata)));
}
function testFinalizeBatchWithdrawERC721(
address sender,
address recipient,
uint256 tokenCount
) public {
uint256 size;
assembly {
size := extcodesize(recipient)
}
hevm.assume(size == 0);
hevm.assume(recipient != address(0));
tokenCount = bound(tokenCount, 1, TOKEN_COUNT);
uint256[] memory _tokenIds = new uint256[](tokenCount);
for (uint256 i = 0; i < tokenCount; i++) {
_tokenIds[i] = i;
}
gateway.updateTokenMapping(address(l1Token), address(l2Token));
gateway.batchDepositERC721(address(l1Token), _tokenIds, defaultGasLimit);
// do finalize withdraw token
bytes memory message = abi.encodeWithSelector(
IL1ERC721Gateway.finalizeBatchWithdrawERC721.selector,
address(l1Token),
address(l2Token),
sender,
recipient,
_tokenIds
);
bytes memory xDomainCalldata = abi.encodeWithSignature(
"relayMessage(address,address,uint256,uint256,bytes)",
address(counterpartGateway),
address(gateway),
0,
0,
message
);
prepareL2MessageRoot(keccak256(xDomainCalldata));
IL1ScrollMessenger.L2MessageProof memory proof;
proof.batchIndex = rollup.lastFinalizedBatchIndex();
// emit FinalizeBatchWithdrawERC721 from L1ERC721Gateway
{
hevm.expectEmit(true, true, true, true);
emit FinalizeBatchWithdrawERC721(address(l1Token), address(l2Token), sender, recipient, _tokenIds);
}
// emit RelayedMessage from L1ScrollMessenger
{
hevm.expectEmit(true, false, false, true);
emit RelayedMessage(keccak256(xDomainCalldata));
}
for (uint256 i = 0; i < tokenCount; i++) {
assertEq(address(gateway), l1Token.ownerOf(_tokenIds[i]));
}
uint256 gatewayBalance = l1Token.balanceOf(address(gateway));
uint256 recipientBalance = l1Token.balanceOf(recipient);
assertBoolEq(false, l1Messenger.isL2MessageExecuted(keccak256(xDomainCalldata)));
l1Messenger.relayMessageWithProof(address(counterpartGateway), address(gateway), 0, 0, message, proof);
for (uint256 i = 0; i < tokenCount; i++) {
assertEq(recipient, l1Token.ownerOf(_tokenIds[i]));
}
assertEq(gatewayBalance - tokenCount, l1Token.balanceOf(address(gateway)));
assertEq(recipientBalance + tokenCount, l1Token.balanceOf(recipient));
assertBoolEq(true, l1Messenger.isL2MessageExecuted(keccak256(xDomainCalldata)));
}
function testReentranceWhenFinalizeWithdraw(address from, uint256 tokenId) public {
tokenId = bound(tokenId, 0, TOKEN_COUNT - 1);
MockScrollMessenger mockMessenger = new MockScrollMessenger();
gateway = _deployGateway(address(mockMessenger));
gateway.initialize(address(counterpartGateway), address(mockMessenger));
l1Token.setApprovalForAll(address(gateway), true);
// deposit first
gateway.updateTokenMapping(address(l1Token), address(l2Token));
gateway.depositERC721(address(l1Token), tokenId, defaultGasLimit);
mockRecipient.setCall(
address(gateway),
0,
abi.encodeWithSignature(
"depositERC721(address,uint256,uint256)",
address(l1Token),
tokenId,
defaultGasLimit
)
);
// finalize withdraw
mockMessenger.setXDomainMessageSender(address(counterpartGateway));
hevm.expectRevert("ReentrancyGuard: reentrant call");
mockMessenger.callTarget(
address(gateway),
abi.encodeWithSelector(
L1ERC721Gateway.finalizeWithdrawERC721.selector,
address(l1Token),
address(l2Token),
from,
address(mockRecipient),
tokenId
)
);
// finalize batch withdraw
mockMessenger.setXDomainMessageSender(address(counterpartGateway));
hevm.expectRevert("ReentrancyGuard: reentrant call");
uint256[] memory tokenIds = new uint256[](1);
tokenIds[0] = tokenId;
mockMessenger.callTarget(
address(gateway),
abi.encodeWithSelector(
L1ERC721Gateway.finalizeBatchWithdrawERC721.selector,
address(l1Token),
address(l2Token),
from,
address(mockRecipient),
tokenIds
)
);
}
function _testDepositERC721(
uint256 tokenId,
uint256 gasLimit,
uint256 feePerGas
) internal {
tokenId = bound(tokenId, 0, TOKEN_COUNT - 1);
gasLimit = bound(gasLimit, defaultGasLimit / 2, defaultGasLimit);
feePerGas = bound(feePerGas, 0, 1000);
messageQueue.setL2BaseFee(feePerGas);
uint256 feeToPay = feePerGas * gasLimit;
hevm.expectRevert("no corresponding l2 token");
gateway.depositERC721(address(l1Token), tokenId, gasLimit);
bytes memory message = abi.encodeWithSelector(
IL2ERC721Gateway.finalizeDepositERC721.selector,
address(l1Token),
address(l2Token),
address(this),
address(this),
tokenId
);
bytes memory xDomainCalldata = abi.encodeWithSignature(
"relayMessage(address,address,uint256,uint256,bytes)",
address(gateway),
address(counterpartGateway),
0,
0,
message
);
gateway.updateTokenMapping(address(l1Token), address(l2Token));
// emit QueueTransaction from L1MessageQueue
{
hevm.expectEmit(true, true, false, true);
address sender = AddressAliasHelper.applyL1ToL2Alias(address(l1Messenger));
emit QueueTransaction(sender, address(l2Messenger), 0, 0, gasLimit, xDomainCalldata);
}
// emit SentMessage from L1ScrollMessenger
{
hevm.expectEmit(true, true, false, true);
emit SentMessage(address(gateway), address(counterpartGateway), 0, 0, gasLimit, message);
}
// emit FinalizeWithdrawERC721 from L1ERC721Gateway
hevm.expectEmit(true, true, true, true);
emit DepositERC721(address(l1Token), address(l2Token), address(this), address(this), tokenId);
assertEq(l1Token.ownerOf(tokenId), address(this));
uint256 gatewayBalance = l1Token.balanceOf(address(gateway));
uint256 feeVaultBalance = address(feeVault).balance;
assertEq(l1Messenger.messageSendTimestamp(keccak256(xDomainCalldata)), 0);
gateway.depositERC721{value: feeToPay + extraValue}(address(l1Token), tokenId, gasLimit);
assertEq(address(gateway), l1Token.ownerOf(tokenId));
assertEq(1 + gatewayBalance, l1Token.balanceOf(address(gateway)));
assertEq(feeToPay + feeVaultBalance, address(feeVault).balance);
assertGt(l1Messenger.messageSendTimestamp(keccak256(xDomainCalldata)), 0);
}
function _testDepositERC721WithRecipient(
uint256 tokenId,
address recipient,
uint256 gasLimit,
uint256 feePerGas
) internal {
tokenId = bound(tokenId, 0, TOKEN_COUNT - 1);
gasLimit = bound(gasLimit, defaultGasLimit / 2, defaultGasLimit);
feePerGas = bound(feePerGas, 0, 1000);
messageQueue.setL2BaseFee(feePerGas);
uint256 feeToPay = feePerGas * gasLimit;
hevm.expectRevert("no corresponding l2 token");
gateway.depositERC721(address(l1Token), tokenId, gasLimit);
bytes memory message = abi.encodeWithSelector(
IL2ERC721Gateway.finalizeDepositERC721.selector,
address(l1Token),
address(l2Token),
address(this),
recipient,
tokenId
);
bytes memory xDomainCalldata = abi.encodeWithSignature(
"relayMessage(address,address,uint256,uint256,bytes)",
address(gateway),
address(counterpartGateway),
0,
0,
message
);
gateway.updateTokenMapping(address(l1Token), address(l2Token));
// emit QueueTransaction from L1MessageQueue
{
hevm.expectEmit(true, true, false, true);
address sender = AddressAliasHelper.applyL1ToL2Alias(address(l1Messenger));
emit QueueTransaction(sender, address(l2Messenger), 0, 0, gasLimit, xDomainCalldata);
}
// emit SentMessage from L1ScrollMessenger
{
hevm.expectEmit(true, true, false, true);
emit SentMessage(address(gateway), address(counterpartGateway), 0, 0, gasLimit, message);
}
// emit FinalizeWithdrawERC721 from L1ERC721Gateway
hevm.expectEmit(true, true, true, true);
emit DepositERC721(address(l1Token), address(l2Token), address(this), recipient, tokenId);
assertEq(l1Token.ownerOf(tokenId), address(this));
uint256 gatewayBalance = l1Token.balanceOf(address(gateway));
uint256 feeVaultBalance = address(feeVault).balance;
assertEq(l1Messenger.messageSendTimestamp(keccak256(xDomainCalldata)), 0);
gateway.depositERC721{value: feeToPay + extraValue}(address(l1Token), recipient, tokenId, gasLimit);
assertEq(address(gateway), l1Token.ownerOf(tokenId));
assertEq(1 + gatewayBalance, l1Token.balanceOf(address(gateway)));
assertEq(feeToPay + feeVaultBalance, address(feeVault).balance);
assertGt(l1Messenger.messageSendTimestamp(keccak256(xDomainCalldata)), 0);
}
function _testBatchDepositERC721(
uint256 tokenCount,
uint256 gasLimit,
uint256 feePerGas
) internal {
tokenCount = bound(tokenCount, 1, TOKEN_COUNT);
gasLimit = bound(gasLimit, defaultGasLimit / 2, defaultGasLimit);
feePerGas = bound(feePerGas, 0, 1000);
messageQueue.setL2BaseFee(feePerGas);
uint256 feeToPay = feePerGas * gasLimit;
uint256[] memory _tokenIds = new uint256[](tokenCount);
for (uint256 i = 0; i < tokenCount; i++) {
_tokenIds[i] = i;
}
hevm.expectRevert("no token to deposit");
gateway.batchDepositERC721(address(l1Token), new uint256[](0), gasLimit);
hevm.expectRevert("no corresponding l2 token");
gateway.batchDepositERC721(address(l1Token), _tokenIds, gasLimit);
bytes memory message = abi.encodeWithSelector(
IL2ERC721Gateway.finalizeBatchDepositERC721.selector,
address(l1Token),
address(l2Token),
address(this),
address(this),
_tokenIds
);
bytes memory xDomainCalldata = abi.encodeWithSignature(
"relayMessage(address,address,uint256,uint256,bytes)",
address(gateway),
address(counterpartGateway),
0,
0,
message
);
gateway.updateTokenMapping(address(l1Token), address(l2Token));
// emit QueueTransaction from L1MessageQueue
{
hevm.expectEmit(true, true, false, true);
address sender = AddressAliasHelper.applyL1ToL2Alias(address(l1Messenger));
emit QueueTransaction(sender, address(l2Messenger), 0, 0, gasLimit, xDomainCalldata);
}
// emit SentMessage from L1ScrollMessenger
{
hevm.expectEmit(true, true, false, true);
emit SentMessage(address(gateway), address(counterpartGateway), 0, 0, gasLimit, message);
}
// emit FinalizeWithdrawERC721 from L1ERC721Gateway
hevm.expectEmit(true, true, true, true);
emit BatchDepositERC721(address(l1Token), address(l2Token), address(this), address(this), _tokenIds);
for (uint256 i = 0; i < tokenCount; i++) {
assertEq(l1Token.ownerOf(i), address(this));
}
uint256 gatewayBalance = l1Token.balanceOf(address(gateway));
uint256 feeVaultBalance = address(feeVault).balance;
assertEq(l1Messenger.messageSendTimestamp(keccak256(xDomainCalldata)), 0);
gateway.batchDepositERC721{value: feeToPay + extraValue}(address(l1Token), _tokenIds, gasLimit);
for (uint256 i = 0; i < tokenCount; i++) {
assertEq(l1Token.ownerOf(i), address(gateway));
}
assertEq(tokenCount + gatewayBalance, l1Token.balanceOf(address(gateway)));
assertEq(feeToPay + feeVaultBalance, address(feeVault).balance);
assertGt(l1Messenger.messageSendTimestamp(keccak256(xDomainCalldata)), 0);
}
function _testBatchDepositERC721WithRecipient(
uint256 tokenCount,
address recipient,
uint256 gasLimit,
uint256 feePerGas
) internal {
tokenCount = bound(tokenCount, 1, TOKEN_COUNT);
gasLimit = bound(gasLimit, defaultGasLimit / 2, defaultGasLimit);
feePerGas = bound(feePerGas, 0, 1000);
messageQueue.setL2BaseFee(feePerGas);
uint256 feeToPay = feePerGas * gasLimit;
uint256[] memory _tokenIds = new uint256[](tokenCount);
for (uint256 i = 0; i < tokenCount; i++) {
_tokenIds[i] = i;
}
hevm.expectRevert("no token to deposit");
gateway.batchDepositERC721(address(l1Token), recipient, new uint256[](0), gasLimit);
hevm.expectRevert("no corresponding l2 token");
gateway.batchDepositERC721(address(l1Token), recipient, _tokenIds, gasLimit);
bytes memory message = abi.encodeWithSelector(
IL2ERC721Gateway.finalizeBatchDepositERC721.selector,
address(l1Token),
address(l2Token),
address(this),
recipient,
_tokenIds
);
bytes memory xDomainCalldata = abi.encodeWithSignature(
"relayMessage(address,address,uint256,uint256,bytes)",
address(gateway),
address(counterpartGateway),
0,
0,
message
);
gateway.updateTokenMapping(address(l1Token), address(l2Token));
// emit QueueTransaction from L1MessageQueue
{
hevm.expectEmit(true, true, false, true);
address sender = AddressAliasHelper.applyL1ToL2Alias(address(l1Messenger));
emit QueueTransaction(sender, address(l2Messenger), 0, 0, gasLimit, xDomainCalldata);
}
// emit SentMessage from L1ScrollMessenger
{
hevm.expectEmit(true, true, false, true);
emit SentMessage(address(gateway), address(counterpartGateway), 0, 0, gasLimit, message);
}
// emit FinalizeWithdrawERC721 from L1ERC721Gateway
hevm.expectEmit(true, true, true, true);
emit BatchDepositERC721(address(l1Token), address(l2Token), address(this), recipient, _tokenIds);
for (uint256 i = 0; i < tokenCount; i++) {
assertEq(l1Token.ownerOf(i), address(this));
}
uint256 gatewayBalance = l1Token.balanceOf(address(gateway));
uint256 feeVaultBalance = address(feeVault).balance;
assertEq(l1Messenger.messageSendTimestamp(keccak256(xDomainCalldata)), 0);
gateway.batchDepositERC721{value: feeToPay + extraValue}(address(l1Token), recipient, _tokenIds, gasLimit);
for (uint256 i = 0; i < tokenCount; i++) {
assertEq(l1Token.ownerOf(i), address(gateway));
}
assertEq(tokenCount + gatewayBalance, l1Token.balanceOf(address(gateway)));
assertEq(feeToPay + feeVaultBalance, address(feeVault).balance);
assertGt(l1Messenger.messageSendTimestamp(keccak256(xDomainCalldata)), 0);
}
function _deployGateway(address messenger) internal returns (L1ERC721Gateway _gateway) {
_gateway = L1ERC721Gateway(_deployProxy(address(0)));
admin.upgrade(
ITransparentUpgradeableProxy(address(_gateway)),
address(new L1ERC721Gateway(address(counterpartGateway), address(messenger)))
);
}
}