/
WormholeSpokeConnector.t.sol
330 lines (281 loc) · 9.9 KB
/
WormholeSpokeConnector.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
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity 0.8.17;
import {WormholeSpokeConnector} from "../../../../contracts/messaging/connectors/wormhole/WormholeSpokeConnector.sol";
import {BaseWormhole} from "../../../../contracts/messaging/connectors/wormhole/BaseWormhole.sol";
import "../../../../contracts/messaging/interfaces/ambs/wormhole/IWormholeRelayer.sol";
import {MerkleTreeManager} from "../../../../contracts/messaging/MerkleTreeManager.sol";
import "../../../utils/ConnectorHelper.sol";
import "../../../utils/Mock.sol";
contract WormholeSpokeConnectorTest is ConnectorHelper {
using stdStorage for StdStorage;
// ============ Storage ============
uint16 _chainIdL1 = 2;
uint16 _chainIdL2 = 4;
uint256 _gasCapL1 = 300000;
uint256 _gasCapL2 = 300000;
// ============ Test set up ============
function setUp() public {
vm.clearMockedCalls();
// Allow future contract mock
vm.etch(_amb, new bytes(0x42));
vm.etch(_rootManager, new bytes(0x42));
// Get the n+1 deployment address
_l1Connector = payable(address(bytes20(keccak256("_l1Connector"))));
_merkle = address(new MerkleTreeManager());
// Deploy
vm.prank(_owner);
_l2Connector = payable(
address(
new WormholeSpokeConnector(
_l1Domain,
_l2Domain,
_amb,
_rootManager,
_l1Connector,
_processGas,
_reserveGas,
0, // uint256 _delayBlocks
_merkle,
address(1), // watcher manager
_gasCapL1,
_chainIdL1
)
)
);
}
// ============ Utils ============
// ============ send ============
// Happy path L1
function test_WormholeSpokeConnector_send_sendAndEmitEvent(bytes32 _root) public {
vm.assume(_root != bytes32(0));
uint256 gasLimit = 200000;
bytes memory encodedData = abi.encode(gasLimit);
bytes memory _data = abi.encodePacked(_root);
// Mock the call to fees
vm.mockCall(
_amb,
abi.encodeWithSignature("quoteEVMDeliveryPrice(uint16,uint256,uint256)", _chainIdL1, 0, gasLimit),
abi.encode(100, 100)
);
vm.mockCall(_merkle, abi.encodeWithSelector(MerkleTreeManager.root.selector), abi.encode(_root));
// Mock the call to sendPayloadToEvm
vm.mockCall(
_amb,
100,
abi.encodeWithSignature(
"sendPayloadToEvm(uint16,address,bytes,uint256,uint256,uint16,address)",
_chainIdL1,
address(_l1Connector),
_data,
uint256(0),
gasLimit,
_chainIdL1,
_owner
),
abi.encode(uint64(1))
);
// Check: call to sendPayloadToEvm?
vm.expectCall(
_amb,
100,
abi.encodeWithSignature(
"sendPayloadToEvm(uint16,address,bytes,uint256,uint256,uint16,address)",
_chainIdL1,
address(_l1Connector),
_data,
0,
gasLimit,
_chainIdL1,
_owner
)
);
// Check: call to fees?
vm.expectCall(
_amb,
abi.encodeWithSignature("quoteEVMDeliveryPrice(uint16,uint256,uint256)", _chainIdL1, 0, gasLimit)
);
vm.deal(_owner, 1 ether);
vm.prank(_owner);
WormholeSpokeConnector(_l2Connector).send{value: 100}(encodedData);
}
function test_WormholeSpokeConnector_send_sendWithGasCap(bytes32 _root) public {
vm.assume(_root != bytes32(0));
uint256 gasLimit = _gasCapL1 + 1;
bytes memory encodedData = abi.encode(gasLimit);
bytes memory _data = abi.encodePacked(_root);
// Mock the call to fees
vm.mockCall(
_amb,
abi.encodeWithSignature("quoteEVMDeliveryPrice(uint16,uint256,uint256)", _chainIdL1, 0, _gasCapL1),
abi.encode(100, 100)
);
vm.mockCall(_merkle, abi.encodeWithSelector(MerkleTreeManager.root.selector), abi.encode(_root));
// Mock the call to sendPayloadToEvm
vm.mockCall(
_amb,
100,
abi.encodeWithSignature(
"sendPayloadToEvm(uint16,address,bytes,uint256,uint256,uint16,address)",
_chainIdL1,
address(_l1Connector),
_data,
uint256(0),
gasLimit,
_chainIdL1,
_owner
),
abi.encode(uint64(1))
);
// Check: call to sendPayloadToEvm?
vm.expectCall(
_amb,
100,
abi.encodeWithSignature(
"sendPayloadToEvm(uint16,address,bytes,uint256,uint256,uint16,address)",
_chainIdL1,
address(_l1Connector),
_data,
0,
gasLimit,
_chainIdL1,
_owner
)
);
// Check: call to fees?
vm.expectCall(
_amb,
abi.encodeWithSignature("quoteEVMDeliveryPrice(uint16,uint256,uint256)", _chainIdL1, 0, _gasCapL1)
);
vm.deal(_owner, 1 ether);
vm.prank(_owner);
WormholeSpokeConnector(_l2Connector).send{value: 100}(encodedData);
}
// data length
function test_WormholeSpokeConnector_send_failsIfBadDataLength(bytes memory _data) public {
vm.assume(_data.length != 32);
vm.expectRevert(bytes("!encoded data length"));
WormholeSpokeConnector(_l2Connector).send{value: 1}(_data);
}
// insufficient fees
function test_WormholeSpokeConnector_send_failsIfInsufficientFees() public {
// Mock the call to fees
uint256 gasLimit = 20000;
vm.mockCall(
_amb,
abi.encodeWithSignature("quoteEVMDeliveryPrice(uint16,uint256,uint256)", _chainIdL1, 0, gasLimit),
abi.encode(100, 100)
);
bytes memory _encodeData = abi.encode(gasLimit);
vm.expectRevert(bytes("!msg.value"));
WormholeSpokeConnector(_l2Connector).send{value: 10}(_encodeData);
}
// ============ processMessage ============
// Happy path L1
function test_WormholeSpokeConnector_processMessage_processMessageAndEmitEvent(bytes32 _root) public {
vm.assume(_root != bytes32(0x0));
// Resize fuzzed bytes to 32 bytes long
bytes memory _dataCorrectSize = abi.encodePacked(bytes32(_root));
vm.expectEmit(true, true, true, true);
emit MessageProcessed(_dataCorrectSize, _amb);
// should call from relayer addres
vm.prank(_amb);
WormholeSpokeConnector(_l2Connector).receiveWormholeMessages(
_dataCorrectSize,
new bytes[](0),
bytes32(uint256(uint160(address(_l1Connector)))),
_chainIdL1,
bytes32(keccak256(("deliveryHash")))
);
assertTrue(WormholeSpokeConnector(_l2Connector).processedWhMessages(bytes32(keccak256(("deliveryHash")))));
// Check: root is marked as pending
assertEq(WormholeSpokeConnector(_l2Connector).pendingAggregateRoots(bytes32(_dataCorrectSize)), block.number);
}
// msg.sender is not the bridge on L1
function test_WormholeSpokeConnector_processMessage_revertIfAmbIsNotMsgSender(
address _notAmb,
bytes calldata _data
) public {
vm.assume(_amb != _notAmb);
vm.assume(_data.length != 0);
// Resize fuzzed bytes to 32 bytes long
bytes memory _dataCorrectSize = abi.encodePacked(bytes32(_data));
vm.expectRevert(abi.encodePacked("!relayer"));
vm.prank(_notAmb);
WormholeSpokeConnector(_l2Connector).receiveWormholeMessages(
_dataCorrectSize,
new bytes[](0),
bytes32(uint256(uint160(address(_l1Connector)))),
_chainIdL1,
bytes32(keccak256(("deliveryHash")))
);
}
// message coming from a wrong sender on the origin chain to L1
function test_WormholeSpokeConnector_processMessage_revertIfWrongSourceAddress(
address _wrongMirror,
bytes calldata _data
) public {
vm.assume(_wrongMirror != _l1Connector);
vm.assume(_data.length != 0);
// Resize fuzzed bytes to 32 bytes long
bytes memory _dataCorrectSize = abi.encodePacked(bytes32(_data));
vm.expectRevert(abi.encodePacked("!mirrorConnector"));
vm.prank(_amb);
WormholeSpokeConnector(_l2Connector).receiveWormholeMessages(
_dataCorrectSize,
new bytes[](0),
bytes32(uint256(uint160(_wrongMirror))),
_chainIdL1,
bytes32(keccak256(("deliveryHash")))
);
}
// message coming from a wrong chain to L1
function test_WormholeSpokeConnector_processMessage_revertIfWrongSourceChain(uint16 _wrongId) public {
vm.assume(_wrongId != _chainIdL1);
// Resize fuzzed bytes to 32 bytes long
bytes memory _dataCorrectSize = abi.encodePacked(bytes32("0x12"));
vm.expectRevert(abi.encodePacked("!source chain"));
vm.prank(_amb);
WormholeSpokeConnector(_l2Connector).receiveWormholeMessages(
_dataCorrectSize,
new bytes[](0),
bytes32(uint256(uint160(address(_l1Connector)))),
_wrongId,
bytes32(keccak256(("deliveryHash")))
);
}
// message coming from a wrong chain to L1
function test_WormholeSpokeConnector_processMessage_revertIfAlreadyProcessed(bytes calldata _data) public {
vm.assume(_data.length != 0);
// Resize fuzzed bytes to 32 bytes long
bytes memory _dataCorrectSize = abi.encodePacked(bytes32(_data));
bytes32 deliveryHash = bytes32(keccak256(("deliveryHash")));
stdstore.target(address(_l2Connector)).sig("processedWhMessages(bytes32)").with_key(deliveryHash).checked_write(
true
);
vm.expectRevert(abi.encodePacked("already processed"));
vm.prank(_amb);
WormholeSpokeConnector(_l2Connector).receiveWormholeMessages(
_dataCorrectSize,
new bytes[](0),
bytes32(uint256(uint160(address(_l1Connector)))),
_chainIdL1,
deliveryHash
);
}
// message has a length > 32 bytes
function test_WormholeSpokeConnector_processMessage_revertIfWrongDataLength(uint8 callDataLength) public {
vm.assume(callDataLength != 32);
// Insert mock data in the payload
bytes memory _wrongLengthCalldata = new bytes(callDataLength);
for (uint256 i; i < callDataLength; i++) _wrongLengthCalldata[i] = bytes1(keccak256(abi.encode(i)));
vm.expectRevert(abi.encodePacked("!length"));
vm.prank(_amb);
WormholeSpokeConnector(_l2Connector).receiveWormholeMessages(
_wrongLengthCalldata,
new bytes[](0),
bytes32(uint256(uint160(address(_l1Connector)))),
_chainIdL1,
bytes32(keccak256(("deliveryHash")))
);
}
}