Skip to content

Commit

Permalink
add helper to reduce code dup
Browse files Browse the repository at this point in the history
  • Loading branch information
RensR committed Oct 20, 2023
1 parent 3ad476d commit e436a9f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 37 deletions.
62 changes: 31 additions & 31 deletions contracts/gas-snapshots/ccip.gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,15 @@ CommitStore_verify:testPausedReverts() (gas: 18438)
CommitStore_verify:testTooManyLeavesReverts() (gas: 36830)
DefensiveExampleTest:testHappyPathSuccess() (gas: 174862)
DefensiveExampleTest:testRecovery() (gas: 399786)
E2E:testE2E_3MessagesSuccess_gas() (gas: 885971)
E2E:testE2E_3MessagesSuccess_gas() (gas: 886142)
EVM2EVMOffRamp__releaseOrMintTokens:testRateLimitErrorsReverts() (gas: 443866)
EVM2EVMOffRamp__releaseOrMintTokens:testTokenHandlingErrorReverts() (gas: 103420)
EVM2EVMOffRamp__releaseOrMintTokens:testUnsupportedTokenReverts() (gas: 18202)
EVM2EVMOffRamp__releaseOrMintTokens:test_releaseOrMintTokensSuccess() (gas: 140615)
EVM2EVMOffRamp__report:testReportSuccess() (gas: 127066)
EVM2EVMOffRamp__report:testReportSuccess() (gas: 127123)
EVM2EVMOffRamp__trialExecute:testRateLimitErrorSuccess() (gas: 174918)
EVM2EVMOffRamp__trialExecute:testTokenHandlingErrorIsCaughtSuccess() (gas: 183240)
EVM2EVMOffRamp__trialExecute:test_trialExecuteSuccess() (gas: 236606)
EVM2EVMOffRamp__trialExecute:test_trialExecuteSuccess() (gas: 236663)
EVM2EVMOffRamp_applyPoolUpdates:testApplyPoolUpdatesSuccess() (gas: 2435136)
EVM2EVMOffRamp_applyPoolUpdates:testInvalidTokenPoolConfigReverts() (gas: 17445)
EVM2EVMOffRamp_applyPoolUpdates:testOnlyCallableByOwnerReverts() (gas: 16012)
Expand All @@ -111,53 +111,53 @@ EVM2EVMOffRamp_constructor:testCommitStoreAlreadyInUseReverts() (gas: 168848)
EVM2EVMOffRamp_constructor:testConstructorSuccess() (gas: 5728147)
EVM2EVMOffRamp_constructor:testTokenConfigMismatchReverts() (gas: 145215)
EVM2EVMOffRamp_constructor:testZeroOnRampAddressReverts() (gas: 2538111)
EVM2EVMOffRamp_execute:testAlreadyExecutedReverts() (gas: 137118)
EVM2EVMOffRamp_execute:testAlreadyExecutedReverts() (gas: 137175)
EVM2EVMOffRamp_execute:testEmptyReportReverts() (gas: 19016)
EVM2EVMOffRamp_execute:testInvalidMessageIdReverts() (gas: 34063)
EVM2EVMOffRamp_execute:testInvalidSourceChainReverts() (gas: 49347)
EVM2EVMOffRamp_execute:testManualExecutionNotYetEnabledReverts() (gas: 44104)
EVM2EVMOffRamp_execute:testMessageTooLargeReverts() (gas: 150111)
EVM2EVMOffRamp_execute:testPausedReverts() (gas: 74942)
EVM2EVMOffRamp_execute:testReceiverErrorSuccess() (gas: 163484)
EVM2EVMOffRamp_execute:testReceiverErrorSuccess() (gas: 163541)
EVM2EVMOffRamp_execute:testRootNotCommittedReverts() (gas: 38980)
EVM2EVMOffRamp_execute:testRouterYULCallReverts() (gas: 413553)
EVM2EVMOffRamp_execute:testSingleMessageNoTokensSuccess() (gas: 173125)
EVM2EVMOffRamp_execute:testRouterYULCallReverts() (gas: 413610)
EVM2EVMOffRamp_execute:testSingleMessageNoTokensSuccess() (gas: 173239)
EVM2EVMOffRamp_execute:testSingleMessageToNonCCIPReceiverSuccess() (gas: 246243)
EVM2EVMOffRamp_execute:testSingleMessagesNoTokensSuccess_gas() (gas: 114462)
EVM2EVMOffRamp_execute:testSkippedIncorrectNonceStillExecutesSuccess() (gas: 323885)
EVM2EVMOffRamp_execute:testSingleMessagesNoTokensSuccess_gas() (gas: 114519)
EVM2EVMOffRamp_execute:testSkippedIncorrectNonceStillExecutesSuccess() (gas: 323942)
EVM2EVMOffRamp_execute:testSkippedIncorrectNonceSuccess() (gas: 51868)
EVM2EVMOffRamp_execute:testStrictUntouchedToSuccessSuccess() (gas: 130684)
EVM2EVMOffRamp_execute:testStrictUntouchedToSuccessSuccess() (gas: 130741)
EVM2EVMOffRamp_execute:testTokenDataMismatchReverts() (gas: 49702)
EVM2EVMOffRamp_execute:testTwoMessagesWithTokensAndGESuccess() (gas: 442944)
EVM2EVMOffRamp_execute:testTwoMessagesWithTokensSuccess_gas() (gas: 402118)
EVM2EVMOffRamp_execute:testTwoMessagesWithTokensAndGESuccess() (gas: 443058)
EVM2EVMOffRamp_execute:testTwoMessagesWithTokensSuccess_gas() (gas: 402232)
EVM2EVMOffRamp_execute:testUnexpectedTokenDataReverts() (gas: 33032)
EVM2EVMOffRamp_execute:testUnhealthyReverts() (gas: 419545)
EVM2EVMOffRamp_execute:testUnhealthyReverts() (gas: 419659)
EVM2EVMOffRamp_execute:testUnsupportedNumberOfTokensReverts() (gas: 61416)
EVM2EVMOffRamp_execute:testUnsupportedTokenReverts() (gas: 130374)
EVM2EVMOffRamp_executeSingleMessage:testMessageSenderReverts() (gas: 20545)
EVM2EVMOffRamp_executeSingleMessage:testNoTokensSuccess() (gas: 47863)
EVM2EVMOffRamp_executeSingleMessage:testNoTokensSuccess() (gas: 47920)
EVM2EVMOffRamp_executeSingleMessage:testNonContractSuccess() (gas: 20105)
EVM2EVMOffRamp_executeSingleMessage:testNonContractWithTokensSuccess() (gas: 205101)
EVM2EVMOffRamp_executeSingleMessage:testTokenHandlingErrorReverts() (gas: 140016)
EVM2EVMOffRamp_executeSingleMessage:testTokensSuccess() (gas: 233891)
EVM2EVMOffRamp_executeSingleMessage:testZeroGasDONExecutionReverts() (gas: 48411)
EVM2EVMOffRamp_execute_upgrade:testV2NonceNewSenderStartsAtZeroSuccess() (gas: 231687)
EVM2EVMOffRamp_execute_upgrade:testV2NonceStartsAtV1NonceSuccess() (gas: 279402)
EVM2EVMOffRamp_execute_upgrade:testV2OffRampNonceSkipsIfMsgInFlightSuccess() (gas: 260739)
EVM2EVMOffRamp_execute_upgrade:testV2SenderNoncesReadsPreviousRampSuccess() (gas: 224692)
EVM2EVMOffRamp_execute_upgrade:testV2Success() (gas: 131199)
EVM2EVMOffRamp_executeSingleMessage:testTokensSuccess() (gas: 233948)
EVM2EVMOffRamp_executeSingleMessage:testZeroGasDONExecutionReverts() (gas: 48468)
EVM2EVMOffRamp_execute_upgrade:testV2NonceNewSenderStartsAtZeroSuccess() (gas: 231801)
EVM2EVMOffRamp_execute_upgrade:testV2NonceStartsAtV1NonceSuccess() (gas: 279573)
EVM2EVMOffRamp_execute_upgrade:testV2OffRampNonceSkipsIfMsgInFlightSuccess() (gas: 260853)
EVM2EVMOffRamp_execute_upgrade:testV2SenderNoncesReadsPreviousRampSuccess() (gas: 224863)
EVM2EVMOffRamp_execute_upgrade:testV2Success() (gas: 131256)
EVM2EVMOffRamp_getDestinationToken:testGetDestinationTokenSuccess() (gas: 32786)
EVM2EVMOffRamp_getDestinationToken:testUnsupportedTokenReverts() (gas: 13763)
EVM2EVMOffRamp_getDestinationTokens:testGetDestinationTokensSuccess() (gas: 26091)
EVM2EVMOffRamp_getExecutionState:testFillExecutionStateSuccess() (gas: 3047481)
EVM2EVMOffRamp_getExecutionState:test_GetExecutionStateSuccess() (gas: 76354)
EVM2EVMOffRamp_manuallyExecute:testLowGasLimitManualExecSuccess() (gas: 496539)
EVM2EVMOffRamp_manuallyExecute:testLowGasLimitManualExecSuccess() (gas: 496653)
EVM2EVMOffRamp_manuallyExecute:testManualExecForkedChainReverts() (gas: 25977)
EVM2EVMOffRamp_manuallyExecute:testManualExecGasLimitMismatchReverts() (gas: 43615)
EVM2EVMOffRamp_manuallyExecute:testManualExecInvalidGasLimitReverts() (gas: 26018)
EVM2EVMOffRamp_manuallyExecute:testManualExecSuccess() (gas: 189219)
EVM2EVMOffRamp_manuallyExecute:testManualExecWithGasOverrideSuccess() (gas: 189846)
EVM2EVMOffRamp_manuallyExecute:testReentrancyManualExecuteFAILS() (gas: 1856032)
EVM2EVMOffRamp_manuallyExecute:testManualExecSuccess() (gas: 189333)
EVM2EVMOffRamp_manuallyExecute:testManualExecWithGasOverrideSuccess() (gas: 189960)
EVM2EVMOffRamp_manuallyExecute:testReentrancyManualExecuteFAILS() (gas: 1856089)
EVM2EVMOffRamp_metadataHash:testMetadataHashSuccess() (gas: 6086)
EVM2EVMOffRamp_setDynamicConfig:testNonOwnerReverts() (gas: 44539)
EVM2EVMOffRamp_setDynamicConfig:testRouterZeroAddressReverts() (gas: 38587)
Expand Down Expand Up @@ -337,8 +337,8 @@ RateLimiter_consume:testTokenRateLimitReachedReverts() (gas: 24448)
RateLimiter_currentTokenBucketState:testCurrentTokenBucketStateSuccess() (gas: 32403)
RateLimiter_currentTokenBucketState:testRefillSuccess() (gas: 41673)
RateLimiter_setTokenBucketConfig:testSetRateLimiterConfigSuccess() (gas: 35459)
Router_applyRampUpdates:testOffRampDisable() (gas: 158599)
Router_applyRampUpdates:testOffRampMismatchReverts() (gas: 89265)
Router_applyRampUpdates:testOffRampDisable() (gas: 158798)
Router_applyRampUpdates:testOffRampMismatchReverts() (gas: 89355)
Router_applyRampUpdates:testOnRampDisable() (gas: 52531)
Router_applyRampUpdates:testOnlyOwnerReverts() (gas: 12260)
Router_ccipSend:testCCIPSendLinkFeeNoTokenSuccess_gas() (gas: 110664)
Expand Down Expand Up @@ -366,10 +366,10 @@ Router_recoverTokens:testRecoverTokensNoFundsReverts() (gas: 17770)
Router_recoverTokens:testRecoverTokensNonOwnerReverts() (gas: 15354)
Router_recoverTokens:testRecoverTokensSuccess() (gas: 48644)
Router_recoverTokens:testRecoverTokensValueReceiverReverts() (gas: 444763)
Router_routeMessage:testAutoExecSuccess() (gas: 39163)
Router_routeMessage:testExecutionEventSuccess() (gas: 209995)
Router_routeMessage:testManualExecSuccess() (gas: 31317)
Router_routeMessage:testOnlyOffRampReverts() (gas: 27213)
Router_routeMessage:testAutoExecSuccess() (gas: 39277)
Router_routeMessage:testExecutionEventSuccess() (gas: 210166)
Router_routeMessage:testManualExecSuccess() (gas: 31374)
Router_routeMessage:testOnlyOffRampReverts() (gas: 27270)
Router_routeMessage:testWhenNotHealthyReverts() (gas: 43002)
Router_setWrappedNative:testOnlyOwnerReverts() (gas: 10978)
ThirdPartyBurnMintTokenPool_lockOrBurn:testLockOrBurnWithAllowListReverts() (gas: 21834)
Expand Down
17 changes: 13 additions & 4 deletions contracts/src/v0.8/ccip/Router.sol
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ contract Router is IRouter, IRouterClient, ITypeAndVersion, OwnerIsCreator {
) external override whenHealthy returns (bool success, bytes memory retData) {
// We only permit offRamps to call this function. We have to encode the sourceChainSelector
// and msg.sender into a uint256 to use as a key in the set.
if (!s_chainSelectorAndOffRamps.contains((uint256(message.sourceChainSelector) << 160) + uint160(msg.sender)))
if (!s_chainSelectorAndOffRamps.contains(_mergeChainSelectorAndOffRamp(message.sourceChainSelector, msg.sender)))
revert OnlyOffRamp();

// We encode here instead of the offRamps to constrain specifically what functions
Expand All @@ -178,6 +178,15 @@ contract Router is IRouter, IRouterClient, ITypeAndVersion, OwnerIsCreator {
return (success, retData);
}

// @notice Merges a chain selector and offRamp address into a single uint256 by shifting the
// chain selector 160 bits to the left.
function _mergeChainSelectorAndOffRamp(
uint64 sourceChainSelector,
address offRampAddress
) internal pure returns (uint256) {
return (uint256(sourceChainSelector) << 160) + uint160(offRampAddress);
}

// ================================================================
// │ Config │
// ================================================================
Expand Down Expand Up @@ -221,7 +230,7 @@ contract Router is IRouter, IRouterClient, ITypeAndVersion, OwnerIsCreator {
}

function isOffRamp(address offRamp, uint64 sourceChainSelector) external view returns (bool) {
return s_chainSelectorAndOffRamps.contains((uint256(sourceChainSelector) << 160) + uint160(offRamp));
return s_chainSelectorAndOffRamps.contains(_mergeChainSelectorAndOffRamp(sourceChainSelector, offRamp));
}

/// @notice applyRampUpdates applies a set of ramp changes which provides
Expand All @@ -245,7 +254,7 @@ contract Router is IRouter, IRouterClient, ITypeAndVersion, OwnerIsCreator {
address offRampAddress = offRampRemoves[i].offRamp;

// If the selector-offRamp pair does not exist, revert.
if (!s_chainSelectorAndOffRamps.remove((uint256(sourceChainSelector) << 160) + uint160(offRampAddress)))
if (!s_chainSelectorAndOffRamps.remove(_mergeChainSelectorAndOffRamp(sourceChainSelector, offRampAddress)))
revert OffRampMismatch(sourceChainSelector, offRampAddress);

emit OffRampRemoved(sourceChainSelector, offRampAddress);
Expand All @@ -255,7 +264,7 @@ contract Router is IRouter, IRouterClient, ITypeAndVersion, OwnerIsCreator {
uint64 sourceChainSelector = offRampAdds[i].sourceChainSelector;
address offRampAddress = offRampAdds[i].offRamp;

if (s_chainSelectorAndOffRamps.add((uint256(sourceChainSelector) << 160) + uint160(offRampAddress))) {
if (s_chainSelectorAndOffRamps.add(_mergeChainSelectorAndOffRamp(sourceChainSelector, offRampAddress))) {
emit OffRampAdded(sourceChainSelector, offRampAddress);
}
}
Expand Down
Loading

0 comments on commit e436a9f

Please sign in to comment.