Skip to content

Commit

Permalink
improve pool tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RensR committed Nov 25, 2024
1 parent a267825 commit 0c233b7
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 158 deletions.
54 changes: 28 additions & 26 deletions contracts/gas-snapshots/ccip.gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -688,42 +688,44 @@ TokenPoolFactory_createTokenPool:test_createTokenPool_RemoteTokenHasDifferentDec
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)
TokenPoolWithAllowList_applyAllowListUpdates:test_AllowListNotEnabled_Revert() (gas: 2670523)
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: 84708)
TokenPool_constructor:test_ZeroAddressNotAllowed_Revert() (gas: 71386)
TokenPool_constructor:test_immutableFields_Success() (gas: 21902)
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)
TokenPool_setRateLimitAdmin:test_SetRateLimitAdmin_Revert() (gas: 11002)
TokenPool_setRateLimitAdmin:test_SetRateLimitAdmin_Success() (gas: 37606)
USDCBridgeMigrator_BurnLockedUSDC:test_PrimaryMechanism_Success() (gas: 136004)
USDCBridgeMigrator_BurnLockedUSDC:test_WhileMigrationPause_Revert() (gas: 109849)
USDCBridgeMigrator_BurnLockedUSDC:test_invalidPermissions_Revert() (gas: 39493)
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
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.24;

import {Router} from "../../../Router.sol";
import {Pool} from "../../../libraries/Pool.sol";
import {TokenPool} from "../../../pools/TokenPool.sol";
import {TokenPoolSetup} from "./TokenPoolSetup.t.sol";
Expand All @@ -24,65 +25,93 @@ contract TokenPool_addRemotePool is TokenPoolSetup {
assertEq(remotePools[1], remotePool);
}

// function test_addRemotePool_MultipleActive() public {
// bytes[] memory remotePools = new bytes[](3);
// remotePools[0] = abi.encode(makeAddr("remotePool1"));
// remotePools[1] = abi.encode(makeAddr("remotePool2"));
// remotePools[2] = abi.encode(makeAddr("remotePool3"));
//
// address fakeOffRamp = makeAddr("fakeOffRamp");
//
// vm.mockCall(
// address(s_sourceRouter), abi.encodeCall(Router.isOffRamp, (DEST_CHAIN_SELECTOR, fakeOffRamp)), abi.encode(true)
// );
//
// vm.startPrank(fakeOffRamp);
//
// vm.expectRevert(abi.encodeWithSelector(TokenPool.InvalidSourcePoolAddress.selector, remotePools[0]));
// s_tokenPool.releaseOrMint(_getReleaseOrMintInV1(remotePools[0]));
//
// // There's already one pool setup through the test setup
// assertEq(s_tokenPool.getRemotePoolHashes().length, 1);
//
// vm.startPrank(OWNER);
// s_tokenPool.addRemotePool(DEST_CHAIN_SELECTOR, remotePools[0]);
//
// vm.startPrank(fakeOffRamp);
// s_tokenPool.releaseOrMint(_getReleaseOrMintInV1(remotePools[0]));
//
// // Adding an additional pool does not remove the previous one
// vm.startPrank(OWNER);
// s_tokenPool.addRemotePool(DEST_CHAIN_SELECTOR, remotePools[1]);
//
// // Both should now work
// assertEq(s_tokenPool.getRemotePoolHashes().length, 3);
// vm.startPrank(fakeOffRamp);
// s_tokenPool.releaseOrMint(_getReleaseOrMintInV1(remotePools[0]));
// s_tokenPool.releaseOrMint(_getReleaseOrMintInV1(remotePools[1]));
//
// // Adding a third pool, and removing the first one
// vm.startPrank(OWNER);
// s_tokenPool.addRemotePool(DEST_CHAIN_SELECTOR, remotePools[2]);
// assertEq(s_tokenPool.getRemotePoolHashes().length, 4);
// s_tokenPool.removeRemotePool(DEST_CHAIN_SELECTOR, remotePools[0]);
// assertEq(s_tokenPool.getRemotePoolHashes().length, 3);
//
// // Only the last two should work
// vm.startPrank(fakeOffRamp);
// vm.expectRevert(abi.encodeWithSelector(TokenPool.InvalidSourcePoolAddress.selector, remotePools[0]));
// s_tokenPool.releaseOrMint(_getReleaseOrMintInV1(remotePools[0]));
// s_tokenPool.releaseOrMint(_getReleaseOrMintInV1(remotePools[1]));
// s_tokenPool.releaseOrMint(_getReleaseOrMintInV1(remotePools[2]));
//
// // Removing the chain removes all associated pool hashes
// vm.startPrank(OWNER);
//
// uint64[] memory chainSelectorsToRemove = new uint64[](1);
// chainSelectorsToRemove[0] = DEST_CHAIN_SELECTOR;
// s_tokenPool.applyChainUpdates(chainSelectorsToRemove, new TokenPool.ChainUpdate[](0));
//
// assertEq(s_tokenPool.getRemotePoolHashes().length, 0);
// }
function test_addRemotePool_MultipleActive() public {
bytes[] memory remotePools = new bytes[](3);
remotePools[0] = abi.encode(makeAddr("remotePool1"));
remotePools[1] = abi.encode(makeAddr("remotePool2"));
remotePools[2] = abi.encode(makeAddr("remotePool3"));

address fakeOffRamp = makeAddr("fakeOffRamp");

vm.mockCall(
address(s_sourceRouter), abi.encodeCall(Router.isOffRamp, (DEST_CHAIN_SELECTOR, fakeOffRamp)), abi.encode(true)
);

vm.startPrank(fakeOffRamp);

vm.expectRevert(abi.encodeWithSelector(TokenPool.InvalidSourcePoolAddress.selector, remotePools[0]));
s_tokenPool.releaseOrMint(_getReleaseOrMintInV1(remotePools[0]));

// There's already one pool setup through the test setup
assertEq(s_tokenPool.getRemotePools(DEST_CHAIN_SELECTOR).length, 1);

vm.startPrank(OWNER);
s_tokenPool.addRemotePool(DEST_CHAIN_SELECTOR, remotePools[0]);

vm.startPrank(fakeOffRamp);
s_tokenPool.releaseOrMint(_getReleaseOrMintInV1(remotePools[0]));

// Adding an additional pool does not remove the previous one
vm.startPrank(OWNER);
s_tokenPool.addRemotePool(DEST_CHAIN_SELECTOR, remotePools[1]);

// Both should now work
assertEq(s_tokenPool.getRemotePools(DEST_CHAIN_SELECTOR).length, 3);
vm.startPrank(fakeOffRamp);
s_tokenPool.releaseOrMint(_getReleaseOrMintInV1(remotePools[0]));
s_tokenPool.releaseOrMint(_getReleaseOrMintInV1(remotePools[1]));

// Adding a third pool, and removing the first one
vm.startPrank(OWNER);
s_tokenPool.addRemotePool(DEST_CHAIN_SELECTOR, remotePools[2]);
assertEq(s_tokenPool.getRemotePools(DEST_CHAIN_SELECTOR).length, 4);
s_tokenPool.removeRemotePool(DEST_CHAIN_SELECTOR, remotePools[0]);
assertEq(s_tokenPool.getRemotePools(DEST_CHAIN_SELECTOR).length, 3);

// Only the last two should work
vm.startPrank(fakeOffRamp);
vm.expectRevert(abi.encodeWithSelector(TokenPool.InvalidSourcePoolAddress.selector, remotePools[0]));
s_tokenPool.releaseOrMint(_getReleaseOrMintInV1(remotePools[0]));
s_tokenPool.releaseOrMint(_getReleaseOrMintInV1(remotePools[1]));
s_tokenPool.releaseOrMint(_getReleaseOrMintInV1(remotePools[2]));

// Removing the chain removes all associated pool hashes
vm.startPrank(OWNER);

uint64[] memory chainSelectorsToRemove = new uint64[](1);
chainSelectorsToRemove[0] = DEST_CHAIN_SELECTOR;
s_tokenPool.applyChainUpdates(chainSelectorsToRemove, new TokenPool.ChainUpdate[](0));

assertEq(s_tokenPool.getRemotePools(DEST_CHAIN_SELECTOR).length, 0);

vm.expectRevert(abi.encodeWithSelector(TokenPool.ChainNotAllowed.selector, DEST_CHAIN_SELECTOR));
s_tokenPool.releaseOrMint(_getReleaseOrMintInV1(remotePools[0]));
vm.expectRevert(abi.encodeWithSelector(TokenPool.ChainNotAllowed.selector, DEST_CHAIN_SELECTOR));
s_tokenPool.releaseOrMint(_getReleaseOrMintInV1(remotePools[1]));
vm.expectRevert(abi.encodeWithSelector(TokenPool.ChainNotAllowed.selector, DEST_CHAIN_SELECTOR));
s_tokenPool.releaseOrMint(_getReleaseOrMintInV1(remotePools[2]));

// Adding the chain back should NOT allow the previous pools to work again
TokenPool.ChainUpdate[] memory chainUpdate = new TokenPool.ChainUpdate[](1);
chainUpdate[0] = TokenPool.ChainUpdate({
remoteChainSelector: DEST_CHAIN_SELECTOR,
remotePoolAddresses: new bytes[](0),
remoteTokenAddress: abi.encode(s_initialRemoteToken),
outboundRateLimiterConfig: _getOutboundRateLimiterConfig(),
inboundRateLimiterConfig: _getInboundRateLimiterConfig()
});

vm.startPrank(OWNER);
s_tokenPool.applyChainUpdates(new uint64[](0), chainUpdate);

vm.startPrank(fakeOffRamp);
vm.expectRevert(abi.encodeWithSelector(TokenPool.InvalidSourcePoolAddress.selector, remotePools[0]));
s_tokenPool.releaseOrMint(_getReleaseOrMintInV1(remotePools[0]));
vm.expectRevert(abi.encodeWithSelector(TokenPool.InvalidSourcePoolAddress.selector, remotePools[1]));
s_tokenPool.releaseOrMint(_getReleaseOrMintInV1(remotePools[1]));
vm.expectRevert(abi.encodeWithSelector(TokenPool.InvalidSourcePoolAddress.selector, remotePools[2]));
s_tokenPool.releaseOrMint(_getReleaseOrMintInV1(remotePools[2]));
}

function _getReleaseOrMintInV1(
bytes memory sourcePoolAddress
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,74 +136,67 @@ contract TokenPool_applyChainUpdates is RouterSetup {
s_tokenPool.applyChainUpdates(new uint64[](0), singleChainConfigured);
}

// function test_applyChainUpdates_UpdatesRemotePoolHashes() public {
// assertEq(s_tokenPool.getRemotePoolHashes().length, 0);
//
// uint64 selector1 = 789;
// uint64 selector2 = 123;
// uint64 selector3 = 456;
//
// bytes memory pool1 = abi.encode(makeAddr("pool1"));
// bytes memory pool2 = abi.encode(makeAddr("pool2"));
// bytes memory pool3 = abi.encode(makeAddr("pool3"));
//
// TokenPool.ChainUpdate[] memory chainUpdates = new TokenPool.ChainUpdate[](3);
// chainUpdates[0] = TokenPool.ChainUpdate({
// remoteChainSelector: selector1,
// remotePoolAddress: pool1,
// remoteTokenAddress: pool1,
// outboundRateLimiterConfig: _getOutboundRateLimiterConfig(),
// inboundRateLimiterConfig: _getInboundRateLimiterConfig()
// });
// chainUpdates[1] = TokenPool.ChainUpdate({
// remoteChainSelector: selector2,
// remotePoolAddress: pool2,
// remoteTokenAddress: pool2,
// outboundRateLimiterConfig: _getOutboundRateLimiterConfig(),
// inboundRateLimiterConfig: _getInboundRateLimiterConfig()
// });
// chainUpdates[2] = TokenPool.ChainUpdate({
// remoteChainSelector: selector3,
// remotePoolAddress: pool3,
// remoteTokenAddress: pool3,
// outboundRateLimiterConfig: _getOutboundRateLimiterConfig(),
// inboundRateLimiterConfig: _getInboundRateLimiterConfig()
// });
//
// s_tokenPool.applyChainUpdates(new uint64[](0), chainUpdates);
//
// assertEq(s_tokenPool.getRemotePoolHashes().length, 3);
//
// // This adds 3 for the first chain, 2 for the second, and 1 for the third for a total of 6.
// // Since each chain already had one, the totals are 4 + 3 + 2
// for (uint256 i = 0; i < chainUpdates.length; ++i) {
// for (uint256 j = i; j < chainUpdates.length; ++j) {
// s_tokenPool.addRemotePool(chainUpdates[i].remoteChainSelector, abi.encode(i, j));
// }
// }
//
// assertEq(s_tokenPool.getRemotePoolHashes().length, 4 + 3 + 2);
//
// // Removing a chain should remove all associated pool hashes
// uint64[] memory chainRemoves = new uint64[](1);
// chainRemoves[0] = selector1;
//
// s_tokenPool.applyChainUpdates(chainRemoves, new TokenPool.ChainUpdate[](0));
//
// assertEq(s_tokenPool.getRemotePoolHashes().length, 3 + 2);
//
// chainRemoves[0] = selector2;
//
// s_tokenPool.applyChainUpdates(chainRemoves, new TokenPool.ChainUpdate[](0));
//
// assertEq(s_tokenPool.getRemotePoolHashes().length, 2);
//
// chainRemoves[0] = selector3;
//
// s_tokenPool.applyChainUpdates(chainRemoves, new TokenPool.ChainUpdate[](0));
//
// assertEq(s_tokenPool.getRemotePoolHashes().length, 0);
// }
function test_applyChainUpdates_UpdatesRemotePoolHashes() public {
assertEq(s_tokenPool.getRemotePools(DEST_CHAIN_SELECTOR).length, 0);

uint64 selector1 = 789;
uint64 selector2 = 123;
uint64 selector3 = 456;

bytes memory pool1 = abi.encode(makeAddr("pool1"));
bytes memory pool2 = abi.encode(makeAddr("pool2"));
bytes memory pool3 = abi.encode(makeAddr("pool3"));

TokenPool.ChainUpdate[] memory chainUpdates = new TokenPool.ChainUpdate[](3);
chainUpdates[0] = TokenPool.ChainUpdate({
remoteChainSelector: selector1,
remotePoolAddresses: new bytes[](0),
remoteTokenAddress: pool1,
outboundRateLimiterConfig: _getOutboundRateLimiterConfig(),
inboundRateLimiterConfig: _getInboundRateLimiterConfig()
});
chainUpdates[1] = TokenPool.ChainUpdate({
remoteChainSelector: selector2,
remotePoolAddresses: new bytes[](0),
remoteTokenAddress: pool2,
outboundRateLimiterConfig: _getOutboundRateLimiterConfig(),
inboundRateLimiterConfig: _getInboundRateLimiterConfig()
});
chainUpdates[2] = TokenPool.ChainUpdate({
remoteChainSelector: selector3,
remotePoolAddresses: new bytes[](0),
remoteTokenAddress: pool3,
outboundRateLimiterConfig: _getOutboundRateLimiterConfig(),
inboundRateLimiterConfig: _getInboundRateLimiterConfig()
});

s_tokenPool.applyChainUpdates(new uint64[](0), chainUpdates);

// This adds 3 for the first chain, 2 for the second, and 1 for the third for a total of 6.
for (uint256 i = 0; i < chainUpdates.length; ++i) {
for (uint256 j = i; j < chainUpdates.length; ++j) {
s_tokenPool.addRemotePool(chainUpdates[i].remoteChainSelector, abi.encode(i, j));
}
assertEq(s_tokenPool.getRemotePools(chainUpdates[i].remoteChainSelector).length, 3 - i);
}

// Removing a chain should remove all associated pool hashes
uint64[] memory chainRemoves = new uint64[](1);
chainRemoves[0] = selector1;

s_tokenPool.applyChainUpdates(chainRemoves, new TokenPool.ChainUpdate[](0));

assertEq(s_tokenPool.getRemotePools(selector1).length, 0);

chainRemoves[0] = selector2;

s_tokenPool.applyChainUpdates(chainRemoves, new TokenPool.ChainUpdate[](0));

assertEq(s_tokenPool.getRemotePools(selector2).length, 0);

// The above deletions should not have affected the third chain
assertEq(s_tokenPool.getRemotePools(selector3).length, 1);
}

// Reverts

Expand Down

0 comments on commit 0c233b7

Please sign in to comment.