@@ -130,8 +130,7 @@ contract Manager is
130
130
/// @notice Returns the number of Endpoints that must attest to a msgId for
131
131
/// it to be considered valid and acted upon.
132
132
function getThreshold () public view returns (uint8 ) {
133
- _Threshold storage _threshold = _getThresholdStorage ();
134
- return _threshold.num;
133
+ return _getThresholdStorage ().num;
135
134
}
136
135
137
136
function setEndpoint (address endpoint ) external onlyOwner {
@@ -155,19 +154,17 @@ contract Manager is
155
154
_threshold.num = 1 ;
156
155
}
157
156
158
- address [] storage _enabledEndpoints = _getEnabledEndpointsStorage ();
159
-
160
- emit EndpointAdded (endpoint, _enabledEndpoints.length , _threshold.num);
157
+ emit EndpointAdded (endpoint, _getNumEndpointsStorage ().enabled, _threshold.num);
161
158
}
162
159
163
160
function removeEndpoint (address endpoint ) external onlyOwner {
164
161
_removeEndpoint (endpoint);
165
162
166
163
_Threshold storage _threshold = _getThresholdStorage ();
167
- address [] storage _enabledEndpoints = _getEnabledEndpointsStorage () ;
164
+ uint8 numEnabledEndpoints = _getNumEndpointsStorage ().enabled ;
168
165
169
- if (_enabledEndpoints. length < _threshold.num) {
170
- _threshold.num = uint8 (_enabledEndpoints. length ) ;
166
+ if (numEnabledEndpoints < _threshold.num) {
167
+ _threshold.num = numEnabledEndpoints ;
171
168
}
172
169
173
170
emit EndpointRemoved (endpoint, _threshold.num);
@@ -234,21 +231,24 @@ contract Manager is
234
231
/// This method should return an array of delivery prices corresponding to each endpoint.
235
232
function quoteDeliveryPrice (
236
233
uint16 recipientChain ,
237
- EndpointStructs.EndpointInstruction[] memory endpointInstructions
238
- ) public view returns (uint256 [] memory ) {
239
- address [] storage _enabledEndpoints = _getEnabledEndpointsStorage ();
234
+ EndpointStructs.EndpointInstruction[] memory endpointInstructions ,
235
+ address [] memory enabledEndpoints
236
+ ) public view returns (uint256 [] memory , uint256 ) {
237
+ uint256 numEnabledEndpoints = enabledEndpoints.length ;
240
238
mapping (address => EndpointInfo) storage endpointInfos = _getEndpointInfosStorage ();
241
239
242
- uint256 [] memory priceQuotes = new uint256 [](_enabledEndpoints.length );
243
- for (uint256 i = 0 ; i < _enabledEndpoints.length ; i++ ) {
244
- address endpointAddr = _enabledEndpoints[i];
240
+ uint256 [] memory priceQuotes = new uint256 [](numEnabledEndpoints);
241
+ uint256 totalPriceQuote = 0 ;
242
+ for (uint256 i = 0 ; i < numEnabledEndpoints; i++ ) {
243
+ address endpointAddr = enabledEndpoints[i];
245
244
uint8 registeredEndpointIndex = endpointInfos[endpointAddr].index;
246
- uint256 endpointPriceQuote = IEndpoint (_enabledEndpoints[i] ).quoteDeliveryPrice (
245
+ uint256 endpointPriceQuote = IEndpoint (endpointAddr ).quoteDeliveryPrice (
247
246
recipientChain, endpointInstructions[registeredEndpointIndex]
248
247
);
249
248
priceQuotes[i] = endpointPriceQuote;
249
+ totalPriceQuote += endpointPriceQuote;
250
250
}
251
- return priceQuotes;
251
+ return ( priceQuotes, totalPriceQuote) ;
252
252
}
253
253
254
254
/// @dev This will either cross-call or internal call, depending on
@@ -257,13 +257,14 @@ contract Manager is
257
257
uint16 recipientChain ,
258
258
uint256 [] memory priceQuotes ,
259
259
EndpointStructs.EndpointInstruction[] memory endpointInstructions ,
260
+ address [] memory enabledEndpoints ,
260
261
bytes memory managerMessage
261
262
) internal {
262
- address [] storage _enabledEndpoints = _getEnabledEndpointsStorage () ;
263
+ uint256 numEnabledEndpoints = enabledEndpoints. length ;
263
264
mapping (address => EndpointInfo) storage endpointInfos = _getEndpointInfosStorage ();
264
265
// call into endpoint contracts to send the message
265
- for (uint256 i = 0 ; i < _enabledEndpoints. length ; i++ ) {
266
- address endpointAddr = _enabledEndpoints [i];
266
+ for (uint256 i = 0 ; i < numEnabledEndpoints ; i++ ) {
267
+ address endpointAddr = enabledEndpoints [i];
267
268
uint8 registeredEndpointIndex = endpointInfos[endpointAddr].index;
268
269
// send it to the recipient manager based on the chain
269
270
IEndpoint (endpointAddr).sendMessage {value: priceQuotes[i]}(
@@ -415,9 +416,14 @@ contract Manager is
415
416
revert ZeroAmount ();
416
417
}
417
418
419
+ if (recipient == bytes32 (0 )) {
420
+ revert InvalidRecipient ();
421
+ }
422
+
418
423
// parse the instructions up front to ensure they:
419
424
// - are encoded correctly
420
425
// - follow payload length restrictions
426
+
421
427
EndpointStructs.parseEndpointInstructions (endpointInstructions);
422
428
423
429
{
@@ -528,10 +534,13 @@ contract Manager is
528
534
EndpointStructs.EndpointInstruction[] memory sortedInstructions = EndpointStructs
529
535
.sortEndpointInstructions (EndpointStructs.parseEndpointInstructions (endpointInstructions));
530
536
531
- uint256 [] memory priceQuotes = quoteDeliveryPrice (recipientChain, sortedInstructions);
537
+ // cache enabled endpoints to avoid multiple storage reads
538
+ address [] memory enabledEndpoints = _getEnabledEndpointsStorage ();
539
+
540
+ (uint256 [] memory priceQuotes , uint256 totalPriceQuote ) =
541
+ quoteDeliveryPrice (recipientChain, sortedInstructions, enabledEndpoints);
532
542
{
533
543
// check up front that msg.value will cover the delivery price
534
- uint256 totalPriceQuote = arraySum (priceQuotes);
535
544
if (msg .value < totalPriceQuote) {
536
545
revert DeliveryPaymentTooLow (totalPriceQuote, msg .value );
537
546
}
@@ -543,22 +552,22 @@ contract Manager is
543
552
}
544
553
}
545
554
546
- bytes memory encodedTransferPayload = EndpointStructs.encodeNativeTokenTransfer (
547
- EndpointStructs.NativeTokenTransfer (
548
- amount, toWormholeFormat (token), recipient, recipientChain
549
- )
550
- );
551
-
552
555
// construct the ManagerMessage payload
553
556
bytes memory encodedManagerPayload = EndpointStructs.encodeManagerMessage (
554
557
EndpointStructs.ManagerMessage (
555
- sequence, toWormholeFormat (sender), encodedTransferPayload
558
+ sequence,
559
+ toWormholeFormat (sender),
560
+ EndpointStructs.encodeNativeTokenTransfer (
561
+ EndpointStructs.NativeTokenTransfer (
562
+ amount, toWormholeFormat (token), recipient, recipientChain
563
+ )
564
+ )
556
565
)
557
566
);
558
567
559
568
// send the message
560
569
_sendMessageToEndpoints (
561
- recipientChain, priceQuotes, sortedInstructions, encodedManagerPayload
570
+ recipientChain, priceQuotes, sortedInstructions, enabledEndpoints, encodedManagerPayload
562
571
);
563
572
564
573
emit TransferSent (recipient, nttDenormalize (amount), recipientChain, sequence);
@@ -792,25 +801,24 @@ contract Manager is
792
801
}
793
802
794
803
function _checkRegisteredEndpointsInvariants () internal view {
795
- if (_getRegisteredEndpointsStorage ().length != _getNumRegisteredEndpointsStorage ().num ) {
804
+ if (_getRegisteredEndpointsStorage ().length != _getNumEndpointsStorage ().registered ) {
796
805
revert RetrievedIncorrectRegisteredEndpoints (
797
- _getRegisteredEndpointsStorage ().length , _getNumRegisteredEndpointsStorage ().num
806
+ _getRegisteredEndpointsStorage ().length , _getNumEndpointsStorage ().registered
798
807
);
799
808
}
800
809
}
801
810
802
811
function _checkThresholdInvariants () internal view {
803
- _Threshold storage _threshold = _getThresholdStorage ();
804
- address [] storage _enabledEndpoints = _getEnabledEndpointsStorage ();
805
- address [] storage _registeredEndpoints = _getRegisteredEndpointsStorage ();
812
+ uint8 threshold = _getThresholdStorage ().num;
813
+ _NumEndpoints memory numEndpoints = _getNumEndpointsStorage ();
806
814
807
815
// invariant: threshold <= enabledEndpoints.length
808
- if (_threshold.num > _enabledEndpoints. length ) {
809
- revert ThresholdTooHigh (_threshold.num, _enabledEndpoints. length );
816
+ if (threshold > numEndpoints.enabled ) {
817
+ revert ThresholdTooHigh (threshold, numEndpoints.enabled );
810
818
}
811
819
812
- if (_registeredEndpoints. length > 0 ) {
813
- if (_threshold.num == 0 ) {
820
+ if (numEndpoints.registered > 0 ) {
821
+ if (threshold == 0 ) {
814
822
revert ZeroThreshold ();
815
823
}
816
824
}
0 commit comments