forked from scroll-tech/scroll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
L1GatewayRouter.t.sol
201 lines (167 loc) · 7.7 KB
/
L1GatewayRouter.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
// SPDX-License-Identifier: MIT
pragma solidity =0.8.24;
import {MockERC20} from "solmate/test/utils/mocks/MockERC20.sol";
import {ITransparentUpgradeableProxy} from "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";
import {L1ETHGateway} from "../L1/gateways/L1ETHGateway.sol";
import {L1GatewayRouter} from "../L1/gateways/L1GatewayRouter.sol";
import {L1StandardERC20Gateway} from "../L1/gateways/L1StandardERC20Gateway.sol";
import {L2ETHGateway} from "../L2/gateways/L2ETHGateway.sol";
import {L2StandardERC20Gateway} from "../L2/gateways/L2StandardERC20Gateway.sol";
import {ScrollStandardERC20} from "../libraries/token/ScrollStandardERC20.sol";
import {ScrollStandardERC20Factory} from "../libraries/token/ScrollStandardERC20Factory.sol";
import {L1GatewayTestBase} from "./L1GatewayTestBase.t.sol";
import {TransferReentrantToken} from "./mocks/tokens/TransferReentrantToken.sol";
contract L1GatewayRouterTest is L1GatewayTestBase {
// from L1GatewayRouter
event SetETHGateway(address indexed oldETHGateway, address indexed newEthGateway);
event SetDefaultERC20Gateway(address indexed oldDefaultERC20Gateway, address indexed newDefaultERC20Gateway);
event SetERC20Gateway(address indexed token, address indexed oldGateway, address indexed newGateway);
ScrollStandardERC20 private template;
ScrollStandardERC20Factory private factory;
L1StandardERC20Gateway private l1StandardERC20Gateway;
L2StandardERC20Gateway private l2StandardERC20Gateway;
L1ETHGateway private l1ETHGateway;
L2ETHGateway private l2ETHGateway;
L1GatewayRouter private router;
MockERC20 private l1Token;
function setUp() public {
__L1GatewayTestBase_setUp();
// Deploy tokens
l1Token = new MockERC20("Mock", "M", 18);
// Deploy L2 contracts
template = new ScrollStandardERC20();
factory = new ScrollStandardERC20Factory(address(template));
l2StandardERC20Gateway = new L2StandardERC20Gateway(address(1), address(1), address(1), address(factory));
l2ETHGateway = new L2ETHGateway(address(1), address(1), address(1));
// Deploy L1 contracts
l1StandardERC20Gateway = L1StandardERC20Gateway(_deployProxy(address(0)));
l1ETHGateway = L1ETHGateway(_deployProxy(address(0)));
router = L1GatewayRouter(_deployProxy(address(new L1GatewayRouter())));
admin.upgrade(
ITransparentUpgradeableProxy(address(l1StandardERC20Gateway)),
address(
new L1StandardERC20Gateway(
address(l2StandardERC20Gateway),
address(router),
address(l1Messenger),
address(template),
address(factory)
)
)
);
admin.upgrade(
ITransparentUpgradeableProxy(address(l1ETHGateway)),
address(new L1ETHGateway(address(l2ETHGateway), address(router), address(l1Messenger)))
);
// Initialize L1 contracts
l1StandardERC20Gateway.initialize(
address(l2StandardERC20Gateway),
address(router),
address(l1Messenger),
address(template),
address(factory)
);
l1ETHGateway.initialize(address(l2ETHGateway), address(router), address(l1Messenger));
router.initialize(address(l1ETHGateway), address(l1StandardERC20Gateway));
}
function testOwnership() public {
assertEq(address(this), router.owner());
}
function testInitialized() public {
assertEq(address(l1StandardERC20Gateway), router.defaultERC20Gateway());
assertEq(
factory.computeL2TokenAddress(address(l2StandardERC20Gateway), address(l1Token)),
router.getL2ERC20Address(address(l1Token))
);
assertEq(address(l1StandardERC20Gateway), router.getERC20Gateway(address(l1Token)));
hevm.expectRevert("Initializable: contract is already initialized");
router.initialize(address(l1ETHGateway), address(l1StandardERC20Gateway));
}
function testSetDefaultERC20Gateway() public {
router.setDefaultERC20Gateway(address(0));
// set by non-owner, should revert
hevm.startPrank(address(1));
hevm.expectRevert("Ownable: caller is not the owner");
router.setDefaultERC20Gateway(address(l1StandardERC20Gateway));
hevm.stopPrank();
// set by owner, should succeed
hevm.expectEmit(true, true, false, true);
emit SetDefaultERC20Gateway(address(0), address(l1StandardERC20Gateway));
assertEq(address(0), router.getERC20Gateway(address(l1Token)));
assertEq(address(0), router.defaultERC20Gateway());
router.setDefaultERC20Gateway(address(l1StandardERC20Gateway));
assertEq(address(l1StandardERC20Gateway), router.getERC20Gateway(address(l1Token)));
assertEq(address(l1StandardERC20Gateway), router.defaultERC20Gateway());
}
function testSetERC20Gateway() public {
router.setDefaultERC20Gateway(address(0));
// length mismatch, should revert
address[] memory empty = new address[](0);
address[] memory single = new address[](1);
hevm.expectRevert("length mismatch");
router.setERC20Gateway(empty, single);
hevm.expectRevert("length mismatch");
router.setERC20Gateway(single, empty);
// set by owner, should succeed
address[] memory _tokens = new address[](1);
address[] memory _gateways = new address[](1);
_tokens[0] = address(l1Token);
_gateways[0] = address(l1StandardERC20Gateway);
hevm.expectEmit(true, true, true, true);
emit SetERC20Gateway(address(l1Token), address(0), address(l1StandardERC20Gateway));
assertEq(address(0), router.getERC20Gateway(address(l1Token)));
router.setERC20Gateway(_tokens, _gateways);
assertEq(address(l1StandardERC20Gateway), router.getERC20Gateway(address(l1Token)));
}
function testFinalizeWithdrawERC20() public {
hevm.expectRevert("should never be called");
router.finalizeWithdrawERC20(address(0), address(0), address(0), address(0), 0, "");
}
function testFinalizeWithdrawETH() public {
hevm.expectRevert("should never be called");
router.finalizeWithdrawETH(address(0), address(0), 0, "");
}
function testRequestERC20(
address _sender,
address _token,
uint256 _amount
) public {
hevm.expectRevert("Only in deposit context");
router.requestERC20(_sender, _token, _amount);
}
function testReentrant() public {
TransferReentrantToken reentrantToken = new TransferReentrantToken("Reentrant", "R", 18);
reentrantToken.mint(address(this), type(uint128).max);
reentrantToken.approve(address(router), type(uint256).max);
reentrantToken.setReentrantCall(
address(router),
0,
abi.encodeWithSelector(
router.depositERC20AndCall.selector,
address(reentrantToken),
address(this),
0,
new bytes(0),
0
),
true
);
hevm.expectRevert("Only not in context");
router.depositERC20(address(reentrantToken), 1, 0);
reentrantToken.setReentrantCall(
address(router),
0,
abi.encodeWithSelector(
router.depositERC20AndCall.selector,
address(reentrantToken),
address(this),
0,
new bytes(0),
0
),
false
);
hevm.expectRevert("Only not in context");
router.depositERC20(address(reentrantToken), 1, 0);
}
}