-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathIntegrationManual.t.sol
308 lines (258 loc) · 11.4 KB
/
IntegrationManual.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
// SPDX-License-Identifier: Apache 2
pragma solidity 0.8.19;
import "forge-std/Test.sol";
import "forge-std/console.sol";
import {WormholeRelayerBasicTest} from "wormhole-solidity-sdk/testing/WormholeRelayerTest.sol";
import "./libraries/IntegrationHelpers.sol";
import "wormhole-solidity-sdk/testing/helpers/WormholeSimulator.sol";
import "../src/NttManager/NttManager.sol";
import "./mocks/MockNttManager.sol";
import "./mocks/MockTransceivers.sol";
import "openzeppelin-contracts/contracts/proxy/ERC1967/ERC1967Proxy.sol";
contract TestRelayerEndToEndManual is IntegrationHelpers, IRateLimiterEvents {
NttManager nttManagerChain1;
NttManager nttManagerChain2;
using TrimmedAmountLib for uint256;
using TrimmedAmountLib for TrimmedAmount;
uint16 constant chainId1 = 4;
uint16 constant chainId2 = 5;
uint8 constant FAST_CONSISTENCY_LEVEL = 200;
uint256 constant GAS_LIMIT = 500000;
uint256 constant DEVNET_GUARDIAN_PK =
0xcfb12303a19cde580bb4dd771639b0d26bc68353645571a8cff516ab2ee113a0;
WormholeSimulator guardian;
uint256 initialBlockTimestamp;
address userA = address(0x123);
address userB = address(0x456);
address userC = address(0x789);
address userD = address(0xABC);
address relayer = address(0x80aC94316391752A193C1c47E27D382b507c93F3);
IWormhole wormhole = IWormhole(0x68605AD7b15c732a30b1BbC62BE8F2A509D74b4D);
function setUp() public {
string memory url = "https://bsc-testnet-rpc.publicnode.com";
vm.createSelectFork(url);
initialBlockTimestamp = vm.getBlockTimestamp();
guardian = new WormholeSimulator(address(wormhole), DEVNET_GUARDIAN_PK);
vm.chainId(chainId1);
DummyToken t1 = new DummyToken();
NttManager implementation = new MockNttManagerContract(
address(t1), IManagerBase.Mode.LOCKING, chainId1, 1 days, false
);
nttManagerChain1 =
MockNttManagerContract(address(new ERC1967Proxy(address(implementation), "")));
nttManagerChain1.initialize();
wormholeTransceiverChain1 = new MockWormholeTransceiverContract(
address(nttManagerChain1),
address(wormhole),
address(relayer),
address(0x0),
FAST_CONSISTENCY_LEVEL,
GAS_LIMIT
);
wormholeTransceiverChain1 = MockWormholeTransceiverContract(
address(new ERC1967Proxy(address(wormholeTransceiverChain1), ""))
);
wormholeTransceiverChain1.initialize();
nttManagerChain1.setTransceiver(address(wormholeTransceiverChain1));
nttManagerChain1.setOutboundLimit(type(uint64).max);
nttManagerChain1.setInboundLimit(type(uint64).max, chainId2);
// Chain 2 setup
vm.chainId(chainId2);
DummyToken t2 = new DummyTokenMintAndBurn();
NttManager implementationChain2 = new MockNttManagerContract(
address(t2), IManagerBase.Mode.BURNING, chainId2, 1 days, false
);
nttManagerChain2 =
MockNttManagerContract(address(new ERC1967Proxy(address(implementationChain2), "")));
nttManagerChain2.initialize();
wormholeTransceiverChain2 = new MockWormholeTransceiverContract(
address(nttManagerChain2),
address(wormhole),
address(relayer), // TODO - add support for this later
address(0x0), // TODO - add support for this later
FAST_CONSISTENCY_LEVEL,
GAS_LIMIT
);
wormholeTransceiverChain2 = MockWormholeTransceiverContract(
address(new ERC1967Proxy(address(wormholeTransceiverChain2), ""))
);
wormholeTransceiverChain2.initialize();
nttManagerChain2.setTransceiver(address(wormholeTransceiverChain2));
nttManagerChain2.setOutboundLimit(type(uint64).max);
nttManagerChain2.setInboundLimit(type(uint64).max, chainId1);
// Register peer contracts for the nttManager and transceiver. Transceivers and nttManager each have the concept of peers here.
nttManagerChain1.setPeer(
chainId2, bytes32(uint256(uint160(address(nttManagerChain2)))), 9, type(uint64).max
);
nttManagerChain2.setPeer(
chainId1, bytes32(uint256(uint160(address(nttManagerChain1)))), 7, type(uint64).max
);
}
function test_relayerTransceiverAuth() public {
nttManagerChain1.setInboundPauseStatus(false);
nttManagerChain1.setOutboundPauseStatus(false);
nttManagerChain2.setInboundPauseStatus(false);
nttManagerChain2.setOutboundPauseStatus(false);
// Set up sensible WH transceiver peers
_setTransceiverPeers(
[wormholeTransceiverChain1, wormholeTransceiverChain2],
[wormholeTransceiverChain2, wormholeTransceiverChain1],
[chainId2, chainId1]
);
vm.recordLogs();
vm.chainId(chainId1);
// Setting up the transfer
DummyToken token1 = DummyToken(nttManagerChain1.token());
uint8 decimals = token1.decimals();
uint256 sendingAmount = 5 * 10 ** decimals;
_prepareTransfer(token1, userA, address(nttManagerChain1), sendingAmount);
vm.deal(userA, 1 ether);
WormholeTransceiver[] memory transceiver = new WormholeTransceiver[](1);
transceiver[0] = wormholeTransceiverChain1;
// Send token through the relayer
transferToken(userB, userA, nttManagerChain1, sendingAmount, chainId2, transceiver, false);
// Get the messages from the logs for the sender
vm.chainId(chainId2);
bytes[] memory encodedVMs = _getWormholeMessage(guardian, vm.getRecordedLogs(), chainId1);
IWormhole.VM memory vaa = wormhole.parseVM(encodedVMs[0]);
vm.stopPrank();
vm.chainId(chainId2);
// Set bad manager peer (0x1)
nttManagerChain2.setOutboundPauseStatus(true);
nttManagerChain2.setPeer(chainId1, toWormholeFormat(address(0x1)), 9, type(uint64).max);
nttManagerChain2.setOutboundPauseStatus(false);
vm.startPrank(relayer);
bytes[] memory a;
vm.expectRevert(
abi.encodeWithSelector(
INttManager.InvalidPeer.selector, chainId1, address(nttManagerChain1)
)
);
_receiveWormholeMessage(
vaa, wormholeTransceiverChain1, wormholeTransceiverChain2, vaa.emitterChainId, a
);
vm.stopPrank();
nttManagerChain2.setOutboundPauseStatus(true);
_setManagerPeer(nttManagerChain2, nttManagerChain1, chainId1, 9, type(uint64).max);
nttManagerChain2.setOutboundPauseStatus(false);
// Wrong caller - aka not relayer contract
vm.prank(userD);
vm.expectRevert(
abi.encodeWithSelector(IWormholeTransceiverState.CallerNotRelayer.selector, userD)
);
_receiveWormholeMessage(
vaa, wormholeTransceiverChain1, wormholeTransceiverChain2, vaa.emitterChainId, a
);
vm.startPrank(relayer);
// Bad chain ID for a given transceiver
vm.expectRevert(
abi.encodeWithSelector(
IWormholeTransceiver.InvalidWormholePeer.selector,
0xFF,
address(wormholeTransceiverChain1)
)
);
wormholeTransceiverChain2.receiveWormholeMessages(
vaa.payload,
a,
bytes32(uint256(uint160(address(wormholeTransceiverChain1)))),
0xFF,
vaa.hash
);
/*
This information is assumed to be trusted since ONLY the relayer on a given chain can call it.
However, it's still good to test various things.
This attempt should actually work this time.
*/
wormholeTransceiverChain2.receiveWormholeMessages(
vaa.payload, // Verified
a, // Should be zero
bytes32(uint256(uint160(address(wormholeTransceiverChain1)))), // Must be a wormhole peers
vaa.emitterChainId, // ChainID from the call
vaa.hash // Hash of the VAA being used
);
// Should from sending a *duplicate* message
vm.expectRevert(
abi.encodeWithSelector(IWormholeTransceiver.TransferAlreadyCompleted.selector, vaa.hash)
);
wormholeTransceiverChain2.receiveWormholeMessages(
vaa.payload,
a, // Should be zero
bytes32(uint256(uint160(address(wormholeTransceiverChain1)))), // Must be a wormhole peers
vaa.emitterChainId, // ChainID from the call
vaa.hash // Hash of the VAA being used
);
}
function test_relayerWithInvalidWHTransceiver() public {
require(
nttManagerChain1.getUnilateralPause().inbound == true,
"Inbound pause not true by default"
);
require(
nttManagerChain1.getUnilateralPause().outbound == true,
"Outbound pause not true by default"
);
nttManagerChain1.setInboundPauseStatus(false);
nttManagerChain1.setOutboundPauseStatus(false);
nttManagerChain2.setInboundPauseStatus(false);
nttManagerChain2.setOutboundPauseStatus(false);
// Set up dodgy wormhole transceiver peers
wormholeTransceiverChain2.setWormholePeer(chainId1, bytes32(uint256(uint160(address(0x1)))));
wormholeTransceiverChain1.setWormholePeer(
chainId2, bytes32(uint256(uint160(address(wormholeTransceiverChain2))))
);
vm.recordLogs();
vm.chainId(chainId1);
// Setting up the transfer
DummyToken token1 = DummyToken(nttManagerChain1.token());
uint8 decimals = token1.decimals();
uint256 sendingAmount = 5 * 10 ** decimals;
token1.mintDummy(address(userA), 5 * 10 ** decimals);
vm.startPrank(userA);
token1.approve(address(nttManagerChain1), sendingAmount);
// Send token through the relayer
{
vm.deal(userA, 1 ether);
nttManagerChain1.transfer{
value: wormholeTransceiverChain1.quoteDeliveryPrice(
chainId2, buildTransceiverInstruction(false)
)
}(
sendingAmount,
chainId2,
bytes32(uint256(uint160(userB))),
bytes32(uint256(uint160(userA))),
false,
encodeTransceiverInstruction(false)
);
}
// Get the messages from the logs for the sender
vm.chainId(chainId2);
Vm.Log[] memory entries = guardian.fetchWormholeMessageFromLog(vm.getRecordedLogs());
bytes[] memory encodedVMs = new bytes[](entries.length);
for (uint256 i = 0; i < encodedVMs.length; i++) {
encodedVMs[i] = guardian.fetchSignedMessageFromLogs(entries[i], chainId1);
}
IWormhole.VM memory vaa = wormhole.parseVM(encodedVMs[0]);
vm.stopPrank();
vm.chainId(chainId2);
// Caller is not proper who to receive messages from
bytes[] memory a;
vm.startPrank(relayer);
vm.expectRevert(
abi.encodeWithSelector(
IWormholeTransceiver.InvalidWormholePeer.selector,
chainId1,
address(wormholeTransceiverChain1)
)
);
wormholeTransceiverChain2.receiveWormholeMessages(
vaa.payload,
a,
bytes32(uint256(uint160(address(wormholeTransceiverChain1)))),
vaa.emitterChainId,
vaa.hash
);
vm.stopPrank();
}
}