Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extra check for decimal math #15419

Merged
merged 9 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions contracts/.changeset/three-dogs-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@chainlink/contracts': patch
---

extra validation on decimal logic in token pools
100 changes: 51 additions & 49 deletions contracts/gas-snapshots/ccip.gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,11 @@ HybridLockReleaseUSDCTokenPool_lockOrBurn:test_onLockReleaseMechanism_thenSwitch
HybridLockReleaseUSDCTokenPool_releaseOrMint:test_OnLockReleaseMechanism_Success() (gas: 213127)
HybridLockReleaseUSDCTokenPool_releaseOrMint:test_WhileMigrationPause_Revert() (gas: 109646)
HybridLockReleaseUSDCTokenPool_releaseOrMint:test_incomingMessageWithPrimaryMechanism() (gas: 265910)
LockReleaseTokenPool_canAcceptLiquidity:test_CanAcceptLiquidity_Success() (gas: 3053405)
LockReleaseTokenPool_canAcceptLiquidity:test_CanAcceptLiquidity_Success() (gas: 3123780)
LockReleaseTokenPool_lockOrBurn:test_LockOrBurnWithAllowList_Revert() (gas: 29734)
LockReleaseTokenPool_lockOrBurn:test_LockOrBurnWithAllowList_Success() (gas: 80647)
LockReleaseTokenPool_lockOrBurn:test_PoolBurnRevertNotHealthy_Revert() (gas: 59227)
LockReleaseTokenPool_provideLiquidity:test_LiquidityNotAccepted_Revert() (gas: 3049734)
LockReleaseTokenPool_provideLiquidity:test_LiquidityNotAccepted_Revert() (gas: 3120109)
LockReleaseTokenPool_provideLiquidity:test_Unauthorized_Revert() (gas: 11511)
LockReleaseTokenPool_releaseOrMint:test_ChainNotAllowed_Revert() (gas: 74108)
LockReleaseTokenPool_releaseOrMint:test_PoolMintNotHealthy_Revert() (gas: 54745)
Expand Down Expand Up @@ -535,7 +535,7 @@ OnRamp_forwardFromRouter:test_ShouldIncrementNonceOnlyOnOrdered_Success() (gas:
OnRamp_forwardFromRouter:test_ShouldIncrementSeqNumAndNonce_Success() (gas: 213012)
OnRamp_forwardFromRouter:test_ShouldStoreLinkFees() (gas: 147026)
OnRamp_forwardFromRouter:test_ShouldStoreNonLinkFees() (gas: 161215)
OnRamp_forwardFromRouter:test_SourceTokenDataTooLarge_Revert() (gas: 3919758)
OnRamp_forwardFromRouter:test_SourceTokenDataTooLarge_Revert() (gas: 3987624)
OnRamp_forwardFromRouter:test_UnAllowedOriginalSender_Revert() (gas: 24015)
OnRamp_forwardFromRouter:test_UnsupportedToken_Revert() (gas: 75832)
OnRamp_forwardFromRouter:test_forwardFromRouter_UnsupportedToken_Revert() (gas: 38588)
Expand Down Expand Up @@ -681,63 +681,65 @@ TokenAdminRegistry_setPool:test_setPool_ZeroAddressRemovesPool_Success() (gas: 3
TokenAdminRegistry_transferAdminRole:test_transferAdminRole_OnlyAdministrator_Revert() (gas: 18202)
TokenAdminRegistry_transferAdminRole:test_transferAdminRole_Success() (gas: 49592)
TokenPoolFactory_constructor:test_constructor_Revert() (gas: 1121653)
TokenPoolFactory_createTokenPool:test_createTokenPoolLockRelease_ExistingToken_predict_Success() (gas: 12293466)
TokenPoolFactory_createTokenPool:test_createTokenPool_BurnFromMintTokenPool_Success() (gas: 6290905)
TokenPoolFactory_createTokenPool:test_createTokenPool_ExistingRemoteToken_AndPredictPool_Success() (gas: 13043750)
TokenPoolFactory_createTokenPool:test_createTokenPool_RemoteTokenHasDifferentDecimals_Success() (gas: 13051052)
TokenPoolFactory_createTokenPool:test_createTokenPool_WithNoExistingRemoteContracts_predict_Success() (gas: 13380456)
TokenPoolFactory_createTokenPool:test_createTokenPool_WithNoExistingTokenOnRemoteChain_Success() (gas: 6075765)
TokenPoolFactory_createTokenPool:test_createTokenPool_WithRemoteTokenAndRemotePool_Success() (gas: 6287377)
TokenPoolWithAllowList_applyAllowListUpdates:test_AllowListNotEnabled_Revert() (gas: 2688992)
TokenPoolWithAllowList_applyAllowListUpdates:test_OnlyOwner_Revert() (gas: 12141)
TokenPoolWithAllowList_applyAllowListUpdates:test_SetAllowListSkipsZero_Success() (gas: 23589)
TokenPoolWithAllowList_applyAllowListUpdates:test_SetAllowList_Success() (gas: 178451)
TokenPoolFactory_createTokenPool:test_createTokenPoolLockRelease_ExistingToken_predict_Success() (gas: 12436024)
TokenPoolFactory_createTokenPool:test_createTokenPool_BurnFromMintTokenPool_Success() (gas: 6400000)
TokenPoolFactory_createTokenPool:test_createTokenPool_ExistingRemoteToken_AndPredictPool_Success() (gas: 13228940)
TokenPoolFactory_createTokenPool:test_createTokenPool_RemoteTokenHasDifferentDecimals_Success() (gas: 13236242)
TokenPoolFactory_createTokenPool:test_createTokenPool_WithNoExistingRemoteContracts_predict_Success() (gas: 13565664)
TokenPoolFactory_createTokenPool:test_createTokenPool_WithNoExistingTokenOnRemoteChain_Success() (gas: 6186359)
TokenPoolFactory_createTokenPool:test_createTokenPool_WithRemoteTokenAndRemotePool_Success() (gas: 6396544)
TokenPoolWithAllowList_applyAllowListUpdates:test_AllowListNotEnabled_Revert() (gas: 2740884)
TokenPoolWithAllowList_applyAllowListUpdates:test_OnlyOwner_Revert() (gas: 12119)
TokenPoolWithAllowList_applyAllowListUpdates:test_SetAllowListSkipsZero_Success() (gas: 23567)
TokenPoolWithAllowList_applyAllowListUpdates:test_SetAllowList_Success() (gas: 178398)
TokenPoolWithAllowList_getAllowList:test_GetAllowList_Success() (gas: 23929)
TokenPoolWithAllowList_getAllowListEnabled:test_GetAllowListEnabled_Success() (gas: 8408)
TokenPoolWithAllowList_setRouter:test_SetRouter_Success() (gas: 25005)
TokenPoolWithAllowList_setRouter:test_ZeroAddressNotAllowed_Revert() (gas: 10729)
TokenPool_addRemotePool:test_NonExistentChain_Revert() (gas: 14311)
TokenPool_addRemotePool:test_PoolAlreadyAdded_Revert() (gas: 117249)
TokenPool_addRemotePool:test_ZeroLengthAddressNotAllowed_Revert() (gas: 14036)
TokenPool_addRemotePool:test_addRemotePool_Success() (gas: 157117)
TokenPool_addRemotePool:test_NonExistentChain_Revert() (gas: 14222)
TokenPool_addRemotePool:test_PoolAlreadyAdded_Revert() (gas: 117205)
TokenPool_addRemotePool:test_ZeroLengthAddressNotAllowed_Revert() (gas: 14014)
TokenPool_addRemotePool:test_addRemotePool_MultipleActive() (gas: 472820)
TokenPool_addRemotePool:test_addRemotePool_Success() (gas: 157095)
TokenPool_applyChainUpdates:test_applyChainUpdates_InvalidRateLimitRate_Revert() (gas: 455575)
TokenPool_applyChainUpdates:test_applyChainUpdates_NonExistentChain_Revert() (gas: 15054)
TokenPool_applyChainUpdates:test_applyChainUpdates_OnlyCallableByOwner_Revert() (gas: 11885)
TokenPool_applyChainUpdates:test_applyChainUpdates_Success() (gas: 592195)
TokenPool_applyChainUpdates:test_applyChainUpdates_ZeroAddressNotAllowed_Revert() (gas: 226494)
TokenPool_calculateLocalAmount:test_calculateLocalAmount() (gas: 84730)
TokenPool_constructor:test_ZeroAddressNotAllowed_Revert() (gas: 71410)
TokenPool_constructor:test_immutableFields_Success() (gas: 21946)
TokenPool_applyChainUpdates:test_applyChainUpdates_NonExistentChain_Revert() (gas: 15032)
TokenPool_applyChainUpdates:test_applyChainUpdates_OnlyCallableByOwner_Revert() (gas: 11863)
TokenPool_applyChainUpdates:test_applyChainUpdates_Success() (gas: 592089)
TokenPool_applyChainUpdates:test_applyChainUpdates_UpdatesRemotePoolHashes() (gas: 1077776)
TokenPool_applyChainUpdates:test_applyChainUpdates_ZeroAddressNotAllowed_Revert() (gas: 226472)
TokenPool_calculateLocalAmount:test_calculateLocalAmount() (gas: 97995)
TokenPool_constructor:test_constructor() (gas: 21930)
TokenPool_constructor:test_constructor_DecimalCallFails() (gas: 2737192)
TokenPool_getRemotePool:test_getRemotePools() (gas: 330500)
TokenPool_onlyOffRamp:test_CallerIsNotARampOnRouter_Revert() (gas: 21526)
TokenPool_onlyOffRamp:test_ChainNotAllowed_Revert() (gas: 240488)
TokenPool_onlyOffRamp:test_onlyOffRamp_Success() (gas: 94313)
TokenPool_onlyOnRamp:test_CallerIsNotARampOnRouter_Revert() (gas: 21090)
TokenPool_onlyOnRamp:test_ChainNotAllowed_Revert() (gas: 204288)
TokenPool_onlyOnRamp:test_onlyOnRamp_Success() (gas: 49172)
TokenPool_parseRemoteDecimals:test_parseRemoteDecimals() (gas: 14086)
TokenPool_parseRemoteDecimals:test_parseRemoteDecimals_NoDecimalsDefaultsToLocalDecimals() (gas: 9771)
TokenPool_onlyOffRamp:test_CallerIsNotARampOnRouter_Revert() (gas: 21504)
TokenPool_onlyOffRamp:test_ChainNotAllowed_Revert() (gas: 240435)
TokenPool_onlyOffRamp:test_onlyOffRamp_Success() (gas: 94291)
TokenPool_onlyOnRamp:test_CallerIsNotARampOnRouter_Revert() (gas: 21156)
TokenPool_onlyOnRamp:test_ChainNotAllowed_Revert() (gas: 204376)
TokenPool_onlyOnRamp:test_onlyOnRamp_Success() (gas: 49238)
TokenPool_parseRemoteDecimals:test_parseRemoteDecimals() (gas: 14020)
TokenPool_parseRemoteDecimals:test_parseRemoteDecimals_NoDecimalsDefaultsToLocalDecimals() (gas: 9727)
TokenPool_removeRemotePool:test_InvalidRemotePoolForChain_Revert() (gas: 17499)
TokenPool_removeRemotePool:test_NonExistentChain_Revert() (gas: 14344)
TokenPool_removeRemotePool:test_removeRemotePool_Success() (gas: 188431)
TokenPool_removeRemotePool:test_removeRemotePool_Success() (gas: 188387)
TokenPool_setChainRateLimiterConfig:test_NonExistentChain_Revert() (gas: 17214)
TokenPool_setChainRateLimiterConfig:test_OnlyOwnerOrRateLimitAdmin_Revert() (gas: 15307)
TokenPool_setRateLimitAdmin:test_SetRateLimitAdmin_Revert() (gas: 11024)
TokenPool_setRateLimitAdmin:test_SetRateLimitAdmin_Success() (gas: 37672)
USDCBridgeMigrator_BurnLockedUSDC:test_PrimaryMechanism_Success() (gas: 136004)
USDCBridgeMigrator_BurnLockedUSDC:test_WhileMigrationPause_Revert() (gas: 109849)
TokenPool_setRateLimitAdmin:test_SetRateLimitAdmin_Revert() (gas: 11002)
TokenPool_setRateLimitAdmin:test_SetRateLimitAdmin_Success() (gas: 37606)
USDCBridgeMigrator_BurnLockedUSDC:test_PrimaryMechanism_Success() (gas: 135869)
USDCBridgeMigrator_BurnLockedUSDC:test_WhileMigrationPause_Revert() (gas: 109729)
USDCBridgeMigrator_BurnLockedUSDC:test_invalidPermissions_Revert() (gas: 39493)
USDCBridgeMigrator_BurnLockedUSDC:test_lockOrBurn_then_BurnInCCTPMigration_Success() (gas: 310057)
USDCBridgeMigrator_BurnLockedUSDC:test_onLockReleaseMechanism_Success() (gas: 147035)
USDCBridgeMigrator_BurnLockedUSDC:test_onLockReleaseMechanism_thenSwitchToPrimary_Success() (gas: 209377)
USDCBridgeMigrator_BurnLockedUSDC:test_lockOrBurn_then_BurnInCCTPMigration_Success() (gas: 309798)
USDCBridgeMigrator_BurnLockedUSDC:test_onLockReleaseMechanism_Success() (gas: 146939)
USDCBridgeMigrator_BurnLockedUSDC:test_onLockReleaseMechanism_thenSwitchToPrimary_Success() (gas: 209089)
USDCBridgeMigrator_cancelMigrationProposal:test_cancelExistingCCTPMigrationProposal_Success() (gas: 56155)
USDCBridgeMigrator_cancelMigrationProposal:test_cannotCancelANonExistentMigrationProposal_Revert() (gas: 12669)
USDCBridgeMigrator_excludeTokensFromBurn:test_excludeTokensWhenNoMigrationProposalPending_Revert() (gas: 13579)
USDCBridgeMigrator_proposeMigration:test_ChainNotUsingLockRelease_Revert() (gas: 15765)
USDCBridgeMigrator_provideLiquidity:test_PrimaryMechanism_Success() (gas: 135986)
USDCBridgeMigrator_provideLiquidity:test_WhileMigrationPause_Revert() (gas: 109871)
USDCBridgeMigrator_provideLiquidity:test_cannotModifyLiquidityWithoutPermissions_Revert() (gas: 13379)
USDCBridgeMigrator_provideLiquidity:test_cannotProvideLiquidityWhenMigrationProposalPending_Revert() (gas: 67417)
USDCBridgeMigrator_provideLiquidity:test_cannotModifyLiquidityWithoutPermissions_Revert() (gas: 13390)
USDCBridgeMigrator_provideLiquidity:test_cannotProvideLiquidityWhenMigrationProposalPending_Revert() (gas: 67428)
USDCBridgeMigrator_provideLiquidity:test_cannotProvideLiquidity_AfterMigration_Revert() (gas: 313898)
USDCBridgeMigrator_provideLiquidity:test_invalidPermissions_Revert() (gas: 39493)
USDCBridgeMigrator_provideLiquidity:test_lockOrBurn_then_BurnInCCTPMigration_Success() (gas: 310057)
Expand All @@ -756,13 +758,13 @@ USDCBridgeMigrator_updateChainSelectorMechanism:test_lockOrBurn_then_BurnInCCTPM
USDCBridgeMigrator_updateChainSelectorMechanism:test_onLockReleaseMechanism_Success() (gas: 147035)
USDCBridgeMigrator_updateChainSelectorMechanism:test_onLockReleaseMechanism_thenSwitchToPrimary_Success() (gas: 209448)
USDCTokenPool__validateMessage:test_ValidateInvalidMessage_Revert() (gas: 26049)
USDCTokenPool_lockOrBurn:test_CallerIsNotARampOnRouter_Revert() (gas: 35369)
USDCTokenPool_lockOrBurn:test_LockOrBurnWithAllowList_Revert() (gas: 29947)
USDCTokenPool_lockOrBurn:test_LockOrBurn_Success() (gas: 133581)
USDCTokenPool_lockOrBurn:test_UnknownDomain_Revert() (gas: 433813)
USDCTokenPool_releaseOrMint:test_ReleaseOrMintRealTx_Success() (gas: 265825)
USDCTokenPool_lockOrBurn:test_CallerIsNotARampOnRouter_Revert() (gas: 35297)
USDCTokenPool_lockOrBurn:test_LockOrBurnWithAllowList_Revert() (gas: 29875)
USDCTokenPool_lockOrBurn:test_LockOrBurn_Success() (gas: 133408)
USDCTokenPool_lockOrBurn:test_UnknownDomain_Revert() (gas: 433408)
USDCTokenPool_releaseOrMint:test_ReleaseOrMintRealTx_Success() (gas: 265695)
USDCTokenPool_releaseOrMint:test_TokenMaxCapacityExceeded_Revert() (gas: 47231)
USDCTokenPool_releaseOrMint:test_UnlockingUSDCFailed_Revert() (gas: 95315)
USDCTokenPool_releaseOrMint:test_UnlockingUSDCFailed_Revert() (gas: 95262)
USDCTokenPool_setDomains:test_InvalidDomain_Revert() (gas: 66437)
USDCTokenPool_setDomains:test_OnlyOwner_Revert() (gas: 11314)
USDCTokenPool_supportsInterface:test_SupportsInterface_Success() (gas: 10107)
8 changes: 4 additions & 4 deletions contracts/gas-snapshots/liquiditymanager.gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ LiquidityManager_addLiquidity:test_addLiquiditySuccess() (gas: 279198)
LiquidityManager_rebalanceLiquidity:test_InsufficientLiquidityReverts() (gas: 206764)
LiquidityManager_rebalanceLiquidity:test_InvalidRemoteChainReverts() (gas: 192374)
LiquidityManager_rebalanceLiquidity:test_rebalanceBetweenPoolsSuccess() (gas: 9141798)
LiquidityManager_rebalanceLiquidity:test_rebalanceBetweenPoolsSuccess_AlreadyFinalized() (gas: 9246772)
LiquidityManager_rebalanceLiquidity:test_rebalanceBetweenPools_MultiStageFinalization() (gas: 9241912)
LiquidityManager_rebalanceLiquidity:test_rebalanceBetweenPools_NativeRewrap() (gas: 9169653)
LiquidityManager_rebalanceLiquidity:test_rebalanceBetweenPoolsSuccess_AlreadyFinalized() (gas: 9330687)
LiquidityManager_rebalanceLiquidity:test_rebalanceBetweenPools_MultiStageFinalization() (gas: 9325827)
LiquidityManager_rebalanceLiquidity:test_rebalanceBetweenPools_NativeRewrap() (gas: 9255660)
LiquidityManager_rebalanceLiquidity:test_rebalanceLiquiditySuccess() (gas: 382928)
LiquidityManager_receive:test_receive_success() (gas: 21182)
LiquidityManager_removeLiquidity:test_InsufficientLiquidityReverts() (gas: 184959)
Expand All @@ -19,7 +19,7 @@ LiquidityManager_setFinanceRole:test_OnlyOwnerReverts() (gas: 10987)
LiquidityManager_setFinanceRole:test_setFinanceRoleSuccess() (gas: 21836)
LiquidityManager_setLocalLiquidityContainer:test_OnlyOwnerReverts() (gas: 11030)
LiquidityManager_setLocalLiquidityContainer:test_ReverstWhen_CalledWithTheZeroAddress() (gas: 10621)
LiquidityManager_setLocalLiquidityContainer:test_setLocalLiquidityContainerSuccess() (gas: 3784709)
LiquidityManager_setLocalLiquidityContainer:test_setLocalLiquidityContainerSuccess() (gas: 3871124)
LiquidityManager_setMinimumLiquidity:test_OnlyOwnerReverts() (gas: 10925)
LiquidityManager_setMinimumLiquidity:test_setMinimumLiquiditySuccess() (gas: 36389)
LiquidityManager_withdrawERC20:test_withdrawERC20Reverts() (gas: 180396)
Expand Down
1 change: 0 additions & 1 deletion contracts/scripts/lcov_prune
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ exclusion_list_ccip=(
"src/v0.8/ConfirmedOwnerWithProposal.sol"
"src/v0.8/tests/MockV3Aggregator.sol"
"src/v0.8/ccip/applications/CCIPClientExample.sol"
"src/v0.8/ccip/pools/BurnWithFromMintTokenPool.sol"
)

exclusion_list_shared=(
Expand Down
36 changes: 33 additions & 3 deletions contracts/src/v0.8/ccip/pools/TokenPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {Pool} from "../libraries/Pool.sol";
import {RateLimiter} from "../libraries/RateLimiter.sol";

import {IERC20} from "../../vendor/openzeppelin-solidity/v4.8.3/contracts/token/ERC20/IERC20.sol";
import {IERC20Metadata} from
"../../vendor/openzeppelin-solidity/v4.8.3/contracts/token/ERC20/extensions/IERC20Metadata.sol";
import {IERC165} from "../../vendor/openzeppelin-solidity/v5.0.2/contracts/utils/introspection/IERC165.sol";
import {EnumerableSet} from "../../vendor/openzeppelin-solidity/v5.0.2/contracts/utils/structs/EnumerableSet.sol";

Expand Down Expand Up @@ -49,6 +51,8 @@ abstract contract TokenPool is IPoolV1, Ownable2StepMsgSender {
error PoolAlreadyAdded(uint64 remoteChainSelector, bytes remotePoolAddress);
error InvalidRemotePoolForChain(uint64 remoteChainSelector, bytes remotePoolAddress);
error InvalidRemoteChainDecimals(bytes sourcePoolData);
error OverflowDetected(uint8 remoteDecimals, uint8 localDecimals, uint256 remoteAmount);
error InvalidDecimalArgs(uint8 expected, uint8 actual);

event Locked(address indexed sender, uint256 amount);
event Burned(address indexed sender, uint256 amount);
Expand Down Expand Up @@ -119,7 +123,17 @@ abstract contract TokenPool is IPoolV1, Ownable2StepMsgSender {
if (address(token) == address(0) || router == address(0) || rmnProxy == address(0)) revert ZeroAddressNotAllowed();
i_token = token;
i_rmnProxy = rmnProxy;

try IERC20Metadata(address(token)).decimals() returns (uint8 actualTokenDecimals) {
if (localTokenDecimals != actualTokenDecimals) {
revert InvalidDecimalArgs(localTokenDecimals, actualTokenDecimals);
}
} catch {
// The decimals function doesn't exist, which is possible since it's optional in the ERC20 spec. We skip the check and
// assume the supplied token decimals are correct.
}
i_tokenDecimals = localTokenDecimals;

s_router = IRouter(router);

// Pool can be set as permissioned or permissionless at deployment time only to save hot-path gas.
Expand Down Expand Up @@ -256,17 +270,33 @@ abstract contract TokenPool is IPoolV1, Ownable2StepMsgSender {
/// @param remoteAmount The amount on the remote chain.
/// @param remoteDecimals The decimals of the token on the remote chain.
/// @return The local amount.
/// @dev This function assumes the inputs don't overflow and does no checks to avoid this. For any normal inputs, this
/// should not be a problem. The only way to overflow is when the given arguments cannot be represented in the uint256
/// type, which means the inputs are invalid.
/// @dev This function protects against overflows. If there is a transaction that hits the overflow check, it is
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// @dev This function protects against overflows. If there is a transaction that hits the overflow check, it is
/// @dev This function protects against overflows. If there is a transaction that hits the overflow check, it is

/// probably incorrect as that means the amount cannot be represented on this chain. If the local decimals have been
/// wrongly configured, the token issuer could redeploy the pool with the correct decimals and manually re-execute the
/// CCIP tx to fix the issue.
function _calculateLocalAmount(uint256 remoteAmount, uint8 remoteDecimals) internal view virtual returns (uint256) {
if (remoteDecimals == i_tokenDecimals) {
return remoteAmount;
}
if (remoteDecimals > i_tokenDecimals) {
if (remoteDecimals - i_tokenDecimals > 77) {
// This is a safety check to prevent overflow in the next calculation.
revert OverflowDetected(remoteDecimals, i_tokenDecimals, remoteAmount);
}
// Solidity rounds down so there is no risk of minting more tokens than the remote chain sent.
return remoteAmount / (10 ** (remoteDecimals - i_tokenDecimals));
}

// This is a safety check to prevent overflow in the next calculation.
// More than 77 would never fit in a uint256 and would cause an overflow. We also check if the resulting amount
// would overflow.
if (
i_tokenDecimals - remoteDecimals > 77
|| remoteAmount > type(uint256).max / (10 ** (i_tokenDecimals - remoteDecimals))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do these calculations multiple times
Can we compute then once and then use it further

Example

decimalsDifference = remoteDecimals - i_tokenDecimals;

factor = 10 ** decimalsDifference;

) {
revert OverflowDetected(remoteDecimals, i_tokenDecimals, remoteAmount);
}

return remoteAmount * (10 ** (i_tokenDecimals - remoteDecimals));
}

Expand Down
4 changes: 0 additions & 4 deletions contracts/src/v0.8/ccip/test/helpers/TokenPoolHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ contract TokenPoolHelper is TokenPool {
address router
) TokenPool(token, localTokenDecimals, allowlist, rmnProxy, router) {}

function getRemotePoolHashes() external view returns (bytes32[] memory) {
return new bytes32[](0); // s_remotePoolHashes.values();
}

function lockOrBurn(
Pool.LockOrBurnInV1 calldata lockOrBurnIn
) external override returns (Pool.LockOrBurnOutV1 memory) {
Expand Down
Loading
Loading