-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathManagerBase.sol
515 lines (417 loc) · 18.8 KB
/
ManagerBase.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
// SPDX-License-Identifier: Apache 2
pragma solidity >=0.8.8 <0.9.0;
import "wormhole-solidity-sdk/Utils.sol";
import "wormhole-solidity-sdk/libraries/BytesParsing.sol";
import "../libraries/external/OwnableUpgradeable.sol";
import "../libraries/external/ReentrancyGuardUpgradeable.sol";
import "../libraries/TransceiverStructs.sol";
import "../libraries/TransceiverHelpers.sol";
import "../libraries/PausableOwnable.sol";
import "../libraries/Implementation.sol";
import "../interfaces/ITransceiver.sol";
import "../interfaces/IManagerBase.sol";
import "./TransceiverRegistry.sol";
import "forge-std/console.sol"; // TODO - remove this - MAX
abstract contract ManagerBase is
IManagerBase,
TransceiverRegistry,
PausableOwnable,
ReentrancyGuardUpgradeable,
Implementation
{
// =============== Immutables ============================================================
address public immutable token;
address immutable deployer;
Mode public immutable mode;
uint16 public immutable chainId;
uint256 immutable evmChainId;
// =============== Setup =================================================================
constructor(address _token, Mode _mode, uint16 _chainId) {
token = _token;
mode = _mode;
chainId = _chainId;
evmChainId = block.chainid;
// save the deployer (check this on initialization)
deployer = msg.sender;
}
modifier inboundNotPaused() {
if (_getUnilateralPauseStorage().inbound) {
revert InboundPaused();
}
_;
}
modifier outboundNotPaused() {
if (_getUnilateralPauseStorage().outbound) {
revert OutboundPaused();
}
_;
}
modifier outboundWhenPaused() {
if (_getUnilateralPauseStorage().outbound == false) {
revert NotPausedForUpdate();
}
_;
}
function _migrate() internal virtual override {
_checkThresholdInvariants();
_checkTransceiversInvariants();
}
// =============== Storage ==============================================================
bytes32 private constant MESSAGE_ATTESTATIONS_SLOT =
bytes32(uint256(keccak256("ntt.messageAttestations")) - 1);
bytes32 private constant MESSAGE_SEQUENCE_SLOT =
bytes32(uint256(keccak256("ntt.messageSequence")) - 1);
bytes32 private constant THRESHOLD_SLOT = bytes32(uint256(keccak256("ntt.threshold")) - 1);
bytes32 private constant UNILATERAL_PAUSE_SLOT =
bytes32(uint256(keccak256("ntt.unilateral_pause")) - 1);
// =============== Storage Getters/Setters ==============================================
function _getUnilateralPauseStorage() internal pure returns (UnilateralPause storage $) {
uint256 slot = uint256(UNILATERAL_PAUSE_SLOT);
assembly ("memory-safe") {
$.slot := slot
}
}
function _getThresholdStorage() private pure returns (_Threshold storage $) {
uint256 slot = uint256(THRESHOLD_SLOT);
assembly ("memory-safe") {
$.slot := slot
}
}
function _getMessageAttestationsStorage()
internal
pure
returns (mapping(bytes32 => AttestationInfo) storage $)
{
uint256 slot = uint256(MESSAGE_ATTESTATIONS_SLOT);
assembly ("memory-safe") {
$.slot := slot
}
}
function _getMessageSequenceStorage() internal pure returns (_Sequence storage $) {
uint256 slot = uint256(MESSAGE_SEQUENCE_SLOT);
assembly ("memory-safe") {
$.slot := slot
}
}
// =============== External Logic =============================================================
/// @inheritdoc IManagerBase
function quoteDeliveryPrice(
uint16 recipientChain,
bytes memory transceiverInstructions
) public view returns (uint256[] memory, uint256) {
address[] memory enabledTransceivers = _getEnabledTransceiversStorage();
TransceiverStructs.TransceiverInstruction[] memory instructions = TransceiverStructs
.parseTransceiverInstructions(transceiverInstructions, enabledTransceivers.length);
return _quoteDeliveryPrice(recipientChain, instructions, enabledTransceivers);
}
// =============== Internal Logic ===========================================================
function _quoteDeliveryPrice(
uint16 recipientChain,
TransceiverStructs.TransceiverInstruction[] memory transceiverInstructions,
address[] memory enabledTransceivers
) internal view returns (uint256[] memory, uint256) {
uint256 numEnabledTransceivers = enabledTransceivers.length;
mapping(address => TransceiverInfo) storage transceiverInfos = _getTransceiverInfosStorage();
uint256[] memory priceQuotes = new uint256[](numEnabledTransceivers);
uint256 totalPriceQuote = 0;
for (uint256 i = 0; i < numEnabledTransceivers; i++) {
address transceiverAddr = enabledTransceivers[i];
uint8 registeredTransceiverIndex = transceiverInfos[transceiverAddr].index;
uint256 transceiverPriceQuote = ITransceiver(transceiverAddr).quoteDeliveryPrice(
recipientChain, transceiverInstructions[registeredTransceiverIndex]
);
priceQuotes[i] = transceiverPriceQuote;
totalPriceQuote += transceiverPriceQuote;
}
return (priceQuotes, totalPriceQuote);
}
function _recordTransceiverAttestation(
uint16 sourceChainId,
TransceiverStructs.NttManagerMessage memory payload
) internal returns (bytes32) {
bytes32 nttManagerMessageHash =
TransceiverStructs.nttManagerMessageDigest(sourceChainId, payload);
// set the attested flag for this transceiver.
// NOTE: Attestation is idempotent (bitwise or 1), but we revert
// anyway to ensure that the client does not continue to initiate calls
// to receive the same message through the same transceiver.
if (
transceiverAttestedToMessage(
nttManagerMessageHash, _getTransceiverInfosStorage()[msg.sender].index
)
) {
revert TransceiverAlreadyAttestedToMessage(nttManagerMessageHash);
}
_setTransceiverAttestedToMessage(nttManagerMessageHash, msg.sender);
return nttManagerMessageHash;
}
function _isMessageExecuted(
uint16 sourceChainId,
bytes32 sourceNttManagerAddress,
TransceiverStructs.NttManagerMessage memory message
) internal returns (bytes32, bool) {
bytes32 digest = TransceiverStructs.nttManagerMessageDigest(sourceChainId, message);
if (!isMessageApproved(digest)) {
revert MessageNotApproved(digest);
}
bool msgAlreadyExecuted = _replayProtect(digest);
if (msgAlreadyExecuted) {
// end execution early to mitigate the possibility of race conditions from transceivers
// attempting to deliver the same message when (threshold < number of transceiver messages)
// notify client (off-chain process) so they don't attempt redundant msg delivery
emit MessageAlreadyExecuted(sourceNttManagerAddress, digest);
return (bytes32(0), msgAlreadyExecuted);
}
return (digest, msgAlreadyExecuted);
}
function _sendMessageToTransceivers(
uint16 recipientChain,
bytes32 refundAddress,
bytes32 peerAddress,
uint256[] memory priceQuotes,
TransceiverStructs.TransceiverInstruction[] memory transceiverInstructions,
address[] memory enabledTransceivers,
bytes memory nttManagerMessage
) internal {
uint256 numEnabledTransceivers = enabledTransceivers.length;
mapping(address => TransceiverInfo) storage transceiverInfos = _getTransceiverInfosStorage();
if (peerAddress == bytes32(0)) {
revert PeerNotRegistered(recipientChain);
}
// push onto the stack again to avoid stack too deep error
bytes32 refundRecipient = refundAddress;
// call into transceiver contracts to send the message
for (uint256 i = 0; i < numEnabledTransceivers; i++) {
address transceiverAddr = enabledTransceivers[i];
// send it to the recipient nttManager based on the chain
ITransceiver(transceiverAddr).sendMessage{value: priceQuotes[i]}(
recipientChain,
transceiverInstructions[transceiverInfos[transceiverAddr].index],
nttManagerMessage,
peerAddress,
refundRecipient
);
}
}
function _prepareForTransfer(
uint16 recipientChain,
bytes memory transceiverInstructions
)
internal
returns (
address[] memory,
TransceiverStructs.TransceiverInstruction[] memory,
uint256[] memory,
uint256
)
{
// cache enabled transceivers to avoid multiple storage reads
address[] memory enabledTransceivers = _getEnabledTransceiversStorage();
TransceiverStructs.TransceiverInstruction[] memory instructions;
{
uint256 numRegisteredTransceivers = _getRegisteredTransceiversStorage().length;
uint256 numEnabledTransceivers = enabledTransceivers.length;
if (numEnabledTransceivers == 0) {
revert NoEnabledTransceivers();
}
instructions = TransceiverStructs.parseTransceiverInstructions(
transceiverInstructions, numRegisteredTransceivers
);
}
(uint256[] memory priceQuotes, uint256 totalPriceQuote) =
_quoteDeliveryPrice(recipientChain, instructions, enabledTransceivers);
{
// check up front that msg.value will cover the delivery price
if (msg.value < totalPriceQuote) {
revert DeliveryPaymentTooLow(totalPriceQuote, msg.value);
}
// refund user extra excess value from msg.value
uint256 excessValue = msg.value - totalPriceQuote;
if (excessValue > 0) {
_refundToSender(excessValue);
}
}
return (enabledTransceivers, instructions, priceQuotes, totalPriceQuote);
}
function _refundToSender(uint256 refundAmount) internal {
// refund the price quote back to sender
(bool refundSuccessful,) = payable(msg.sender).call{value: refundAmount}("");
// check success
if (!refundSuccessful) {
revert RefundFailed(refundAmount);
}
}
// =============== Public Getters ========================================================
/// @inheritdoc IManagerBase
function getMode() public view returns (uint8) {
return uint8(mode);
}
/// @inheritdoc IManagerBase
function getThreshold() public view returns (uint8) {
return _getThresholdStorage().num;
}
/// @inheritdoc IManagerBase
function getUnilateralPause() public view returns (UnilateralPause memory) {
UnilateralPause storage u = _getUnilateralPauseStorage();
return UnilateralPause({inbound: u.inbound, outbound: u.outbound});
}
/// @inheritdoc IManagerBase
function isMessageApproved(bytes32 digest) public view returns (bool) {
uint8 threshold = getThreshold();
return messageAttestations(digest) >= threshold && threshold > 0;
}
/// @inheritdoc IManagerBase
function nextMessageSequence() external view returns (uint64) {
return _getMessageSequenceStorage().num;
}
/// @inheritdoc IManagerBase
function isMessageExecuted(bytes32 digest) public view returns (bool) {
return _getMessageAttestationsStorage()[digest].executed;
}
/// @inheritdoc IManagerBase
function transceiverAttestedToMessage(bytes32 digest, uint8 index) public view returns (bool) {
return
_getMessageAttestationsStorage()[digest].attestedTransceivers & uint64(1 << index) > 0;
}
/// @inheritdoc IManagerBase
function messageAttestations(bytes32 digest) public view returns (uint8 count) {
return countSetBits(_getMessageAttestations(digest));
}
// =============== Admin ==============================================================
/// @inheritdoc IManagerBase
function upgrade(address newImplementation) external onlyOwner {
_upgrade(newImplementation);
}
/// @inheritdoc IManagerBase
function pause() public onlyOwnerOrPauser {
_pause();
}
function unpause() public onlyOwnerOrPauser {
_unpause();
}
function setInboundPauseStatus(bool status) external onlyOwnerOrPauser {
_getUnilateralPauseStorage().inbound = status;
}
function setOutboundPauseStatus(bool status) external onlyOwnerOrPauser {
_getUnilateralPauseStorage().outbound = status;
}
/// @notice Transfer ownership of the Manager contract and all Transceiver contracts to a new owner.
function transferOwnership(address newOwner) public override onlyOwner {
super.transferOwnership(newOwner);
// loop through all the registered transceivers and set the new owner of each transceiver to the newOwner
address[] storage _registeredTransceivers = _getRegisteredTransceiversStorage();
_checkRegisteredTransceiversInvariants();
for (uint256 i = 0; i < _registeredTransceivers.length; i++) {
ITransceiver(_registeredTransceivers[i]).transferTransceiverOwnership(newOwner);
}
}
/// @inheritdoc IManagerBase
function setTransceiver(address transceiver) external onlyOwner {
_setTransceiver(transceiver);
_Threshold storage _threshold = _getThresholdStorage();
// We do not automatically increase the threshold here.
// Automatically increasing the threshold can result in a scenario
// where in-flight messages can't be redeemed.
// For example: Assume there is 1 Transceiver and the threshold is 1.
// If we were to add a new Transceiver, the threshold would increase to 2.
// However, all messages that are either in-flight or that are sent on
// a source chain that does not yet have 2 Transceivers will only have been
// sent from a single transceiver, so they would never be able to get
// redeemed.
// Instead, we leave it up to the owner to manually update the threshold
// after some period of time, ideally once all chains have the new Transceiver
// and transfers that were sent via the old configuration are all complete.
// However if the threshold is 0 (the initial case) we do increment to 1.
if (_threshold.num == 0) {
_threshold.num = 1;
}
emit TransceiverAdded(transceiver, _getNumTransceiversStorage().enabled, _threshold.num);
_checkThresholdInvariants();
}
/// @inheritdoc IManagerBase
function removeTransceiver(address transceiver) external onlyOwner outboundWhenPaused {
_removeTransceiver(transceiver);
_Threshold storage _threshold = _getThresholdStorage();
uint8 numEnabledTransceivers = _getNumTransceiversStorage().enabled;
if (numEnabledTransceivers < _threshold.num) {
_threshold.num = numEnabledTransceivers;
}
emit TransceiverRemoved(transceiver, _threshold.num);
_checkThresholdInvariants();
}
/// @inheritdoc IManagerBase
function setThreshold(uint8 threshold) external onlyOwner outboundWhenPaused {
if (threshold == 0) {
revert ZeroThreshold();
}
_Threshold storage _threshold = _getThresholdStorage();
uint8 oldThreshold = _threshold.num;
_threshold.num = threshold;
_checkThresholdInvariants();
emit ThresholdChanged(oldThreshold, threshold);
}
// =============== Internal ==============================================================
function _setTransceiverAttestedToMessage(bytes32 digest, uint8 index) internal {
_getMessageAttestationsStorage()[digest].attestedTransceivers |= uint64(1 << index);
}
function _setTransceiverAttestedToMessage(bytes32 digest, address transceiver) internal {
_setTransceiverAttestedToMessage(digest, _getTransceiverInfosStorage()[transceiver].index);
emit MessageAttestedTo(
digest, transceiver, _getTransceiverInfosStorage()[transceiver].index
);
}
/// @dev Returns the bitmap of attestations from enabled transceivers for a given message.
function _getMessageAttestations(bytes32 digest) internal view returns (uint64) {
uint64 enabledTransceiverBitmap = _getEnabledTransceiversBitmap();
return
_getMessageAttestationsStorage()[digest].attestedTransceivers & enabledTransceiverBitmap;
}
function _getEnabledTransceiverAttestedToMessage(
bytes32 digest,
uint8 index
) internal view returns (bool) {
return _getMessageAttestations(digest) & uint64(1 << index) != 0;
}
// @dev Mark a message as executed.
// This function will retuns `true` if the message has already been executed.
function _replayProtect(bytes32 digest) internal returns (bool) {
// check if this message has already been executed
if (isMessageExecuted(digest)) {
return true;
}
// mark this message as executed
_getMessageAttestationsStorage()[digest].executed = true;
return false;
}
function _useMessageSequence() internal returns (uint64 currentSequence) {
currentSequence = _getMessageSequenceStorage().num;
_getMessageSequenceStorage().num++;
}
/// ============== Invariants =============================================
/// @dev When we add new immutables, this function should be updated
function _checkImmutables() internal view virtual override {
assert(this.token() == token);
assert(this.mode() == mode);
assert(this.chainId() == chainId);
}
function _checkRegisteredTransceiversInvariants() internal view {
if (_getRegisteredTransceiversStorage().length != _getNumTransceiversStorage().registered) {
revert RetrievedIncorrectRegisteredTransceivers(
_getRegisteredTransceiversStorage().length, _getNumTransceiversStorage().registered
);
}
}
function _checkThresholdInvariants() internal view {
uint8 threshold = _getThresholdStorage().num;
_NumTransceivers memory numTransceivers = _getNumTransceiversStorage();
// invariant: threshold <= enabledTransceivers.length
if (threshold > numTransceivers.enabled) {
revert ThresholdTooHigh(threshold, numTransceivers.enabled);
}
if (numTransceivers.registered > 0) {
if (threshold == 0) {
revert ZeroThreshold();
}
}
}
}