Skip to content

Commit

Permalink
reduce test code
Browse files Browse the repository at this point in the history
  • Loading branch information
RensR committed Nov 19, 2024
1 parent 5e4e6e1 commit d2fbf3f
Show file tree
Hide file tree
Showing 12 changed files with 320 additions and 478 deletions.
454 changes: 228 additions & 226 deletions contracts/gas-snapshots/ccip.gas-snapshot

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,31 @@ import {TokenPoolSetup} from "./TokenPoolSetup.t.sol";

contract TokenPool_addRemotePool is TokenPoolSetup {
function test_addRemotePool_Success() public {
uint64 chainSelector = DEST_CHAIN_SELECTOR;
// Use a longer data type to ensure it also works for non-evm
bytes memory remotePool = abi.encode(makeAddr("non-evm-1"), makeAddr("non-evm-2"));

address initialPool = makeAddr("remotePool");
address remoteToken = makeAddr("remoteToken");
bytes memory remotePool = abi.encode(type(uint256).max);
bytes32 remotePairHash = keccak256(abi.encode(DEST_CHAIN_SELECTOR, remotePool));

bytes32 remotePairHash = keccak256(abi.encode(chainSelector, remotePool));

TokenPool.ChainUpdate[] memory chainUpdates = new TokenPool.ChainUpdate[](1);
chainUpdates[0] = TokenPool.ChainUpdate({
remoteChainSelector: chainSelector,
remotePoolAddress: abi.encode(initialPool),
remoteTokenAddress: abi.encode(remoteToken),
outboundRateLimiterConfig: _getOutboundRateLimiterConfig(),
inboundRateLimiterConfig: _getInboundRateLimiterConfig()
});
s_tokenPool.applyChainUpdates(new uint64[](0), chainUpdates);
vm.startPrank(OWNER);

vm.expectEmit();
emit TokenPool.RemotePoolSet(chainSelector, remotePool, remotePairHash);

vm.startPrank(OWNER);
emit TokenPool.RemotePoolSet(DEST_CHAIN_SELECTOR, remotePool, remotePairHash);

s_tokenPool.addRemotePool(chainSelector, remotePool);
s_tokenPool.addRemotePool(DEST_CHAIN_SELECTOR, remotePool);

bytes[] memory remotePools = s_tokenPool.getRemotePools(chainSelector);
bytes[] memory remotePools = s_tokenPool.getRemotePools(DEST_CHAIN_SELECTOR);

assertEq(remotePools.length, 2);
assertEq(remotePools[0], abi.encode(initialPool));
assertEq(remotePools[0], abi.encode(s_initialRemotePool));
assertEq(remotePools[1], remotePool);
}

function test_addRemotePool_MultipleActive() public {}

// Reverts

function test_NonExistentChain_Revert() public {
uint64 chainSelector = DEST_CHAIN_SELECTOR;
uint64 chainSelector = DEST_CHAIN_SELECTOR + 1;
bytes memory remotePool = abi.encode(type(uint256).max);

vm.expectRevert(abi.encodeWithSelector(TokenPool.NonExistentChain.selector, chainSelector));
Expand All @@ -50,67 +39,28 @@ contract TokenPool_addRemotePool is TokenPoolSetup {
}

function test_ZeroAddressNotAllowed_Revert() public {
uint64 chainSelector = DEST_CHAIN_SELECTOR;
bytes memory remotePool = abi.encode(address(0));

address initialPool = makeAddr("remotePool");
address remoteToken = makeAddr("remoteToken");

TokenPool.ChainUpdate[] memory chainUpdates = new TokenPool.ChainUpdate[](1);
chainUpdates[0] = TokenPool.ChainUpdate({
remoteChainSelector: chainSelector,
remotePoolAddress: abi.encode(initialPool),
remoteTokenAddress: abi.encode(remoteToken),
outboundRateLimiterConfig: _getOutboundRateLimiterConfig(),
inboundRateLimiterConfig: _getInboundRateLimiterConfig()
});
s_tokenPool.applyChainUpdates(new uint64[](0), chainUpdates);

vm.expectRevert(abi.encodeWithSelector(TokenPool.ZeroAddressNotAllowed.selector));

s_tokenPool.addRemotePool(chainSelector, remotePool);
s_tokenPool.addRemotePool(DEST_CHAIN_SELECTOR, remotePool);
}

function test_ZeroLengthAddressNotAllowed_Revert() public {
uint64 chainSelector = DEST_CHAIN_SELECTOR;
bytes memory remotePool = "";
address initialPool = makeAddr("remotePool");
address remoteToken = makeAddr("remoteToken");

TokenPool.ChainUpdate[] memory chainUpdates = new TokenPool.ChainUpdate[](1);
chainUpdates[0] = TokenPool.ChainUpdate({
remoteChainSelector: chainSelector,
remotePoolAddress: abi.encode(initialPool),
remoteTokenAddress: abi.encode(remoteToken),
outboundRateLimiterConfig: _getOutboundRateLimiterConfig(),
inboundRateLimiterConfig: _getInboundRateLimiterConfig()
});
s_tokenPool.applyChainUpdates(new uint64[](0), chainUpdates);

vm.expectRevert(abi.encodeWithSelector(TokenPool.ZeroAddressNotAllowed.selector));

s_tokenPool.addRemotePool(chainSelector, remotePool);
s_tokenPool.addRemotePool(DEST_CHAIN_SELECTOR, remotePool);
}

function test_PoolAlreadyAdded_Revert() public {
uint64 chainSelector = DEST_CHAIN_SELECTOR;

address initialPool = makeAddr("remotePool");
address remoteToken = makeAddr("remoteToken");
bytes memory remotePool = abi.encode(type(uint256).max);

bytes32 remotePairHash = keccak256(abi.encode(chainSelector, remotePool));

TokenPool.ChainUpdate[] memory chainUpdates = new TokenPool.ChainUpdate[](1);
chainUpdates[0] = TokenPool.ChainUpdate({
remoteChainSelector: chainSelector,
remotePoolAddress: abi.encode(initialPool),
remoteTokenAddress: abi.encode(remoteToken),
outboundRateLimiterConfig: _getOutboundRateLimiterConfig(),
inboundRateLimiterConfig: _getInboundRateLimiterConfig()
});
s_tokenPool.applyChainUpdates(new uint64[](0), chainUpdates);

vm.expectEmit();
emit TokenPool.RemotePoolSet(chainSelector, remotePool, remotePairHash);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,33 @@
pragma solidity 0.8.24;

import {Ownable2Step} from "../../../../shared/access/Ownable2Step.sol";
import {BurnMintERC677} from "../../../../shared/token/ERC677/BurnMintERC677.sol";
import {RateLimiter} from "../../../libraries/RateLimiter.sol";
import {TokenPool} from "../../../pools/TokenPool.sol";
import {TokenPoolSetup} from "./TokenPoolSetup.t.sol";

contract TokenPool_applyChainUpdates is TokenPoolSetup {
import {TokenPoolHelper} from "../../helpers/TokenPoolHelper.sol";
import {RouterSetup} from "../../router/Router/RouterSetup.t.sol";

import {IERC20} from "../../../../vendor/openzeppelin-solidity/v4.8.3/contracts/token/ERC20/IERC20.sol";

contract TokenPool_applyChainUpdates is RouterSetup {
IERC20 internal s_token;
TokenPoolHelper internal s_tokenPool;

function setUp() public virtual override {
RouterSetup.setUp();
s_token = new BurnMintERC677("LINK", "LNK", 18, 0);
deal(address(s_token), OWNER, type(uint256).max);

s_tokenPool = new TokenPoolHelper(s_token, new address[](0), address(s_mockRMN), address(s_sourceRouter));
}

function assertState(
TokenPool.ChainUpdate[] memory chainUpdates
) public view {
uint64[] memory chainSelectors = s_tokenPool.getSupportedChains();
for (uint256 i = 0; i < chainUpdates.length; i++) {
assertEq(chainUpdates[i].remoteChainSelector, chainSelectors[i]);
for (uint256 i = 0; i < chainUpdates.length; ++i) {
assertEq(chainUpdates[i].remoteChainSelector, chainSelectors[i], "Chain selector mismatch");
}

for (uint256 i = 0; i < chainUpdates.length; ++i) {
Expand All @@ -37,7 +53,7 @@ contract TokenPool_applyChainUpdates is TokenPoolSetup {
RateLimiter.Config memory inboundRateLimit2 = RateLimiter.Config({isEnabled: true, capacity: 100e27, rate: 1e17});

// EVM chain, which uses the 160 bit evm address space
uint64 evmChainSelector = 1;
uint64 evmChainSelector = 1789142;
bytes memory evmRemotePool = abi.encode(makeAddr("evm_remote_pool"));
bytes memory evmRemoteToken = abi.encode(makeAddr("evm_remote_token"));

Expand Down Expand Up @@ -155,9 +171,11 @@ contract TokenPool_applyChainUpdates is TokenPoolSetup {
}

function test_applyChainUpdates_InvalidRateLimitRate_Revert() public {
uint64 unusedChainSelector = 2 ** 64 - 1;

TokenPool.ChainUpdate[] memory chainUpdates = new TokenPool.ChainUpdate[](1);
chainUpdates[0] = TokenPool.ChainUpdate({
remoteChainSelector: 1,
remoteChainSelector: unusedChainSelector,
remotePoolAddress: abi.encode(address(1)),
remoteTokenAddress: abi.encode(address(2)),
outboundRateLimiterConfig: RateLimiter.Config({isEnabled: true, capacity: 0, rate: 0}),
Expand Down Expand Up @@ -190,7 +208,7 @@ contract TokenPool_applyChainUpdates is TokenPoolSetup {
s_tokenPool.applyChainUpdates(new uint64[](0), chainUpdates);

// Change the chain selector as adding the same one would revert
chainUpdates[0].remoteChainSelector = 2;
chainUpdates[0].remoteChainSelector = unusedChainSelector - 1;

// Inbound

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,26 @@ import {TokenPool} from "../../../pools/TokenPool.sol";
import {TokenPoolSetup} from "./TokenPoolSetup.t.sol";

contract TokenPool_getRemotePool is TokenPoolSetup {
// function test_getRemotePool_Success() public {
// uint64 chainSelector = 123124;
// address remotePool = makeAddr("remotePool");
// address remoteToken = makeAddr("remoteToken");
//
// // Zero indicates nothing is set
// assertEq(0, s_tokenPool.getRemotePool(chainSelector).length);
//
// TokenPool.ChainUpdate[] memory chainUpdates = new TokenPool.ChainUpdate[](1);
// chainUpdates[0] = TokenPool.ChainUpdate({
// remoteChainSelector: chainSelector,
// remotePoolAddress: abi.encode(remotePool),
// remoteTokenAddress: abi.encode(remoteToken),
// allowed: true,
// outboundRateLimiterConfig: _getOutboundRateLimiterConfig(),
// inboundRateLimiterConfig: _getInboundRateLimiterConfig()
// });
// s_tokenPool.applyChainUpdates(chainUpdates);
//
// assertEq(remotePool, abi.decode(s_tokenPool.getRemotePool(chainSelector), (address)));
// }
function test_getRemotePools() public {
uint64 chainSelector = DEST_CHAIN_SELECTOR + 1;
bytes memory remotePool = abi.encode(makeAddr("remotePool"));
bytes memory remoteToken = abi.encode(makeAddr("remoteToken"));

// Zero indicates nothing is set
assertEq(0, s_tokenPool.getRemotePools(chainSelector).length);

TokenPool.ChainUpdate[] memory chainUpdates = new TokenPool.ChainUpdate[](1);
chainUpdates[0] = TokenPool.ChainUpdate({
remoteChainSelector: chainSelector,
remotePoolAddress: remotePool,
remoteTokenAddress: remoteToken,
outboundRateLimiterConfig: _getOutboundRateLimiterConfig(),
inboundRateLimiterConfig: _getInboundRateLimiterConfig()
});
s_tokenPool.applyChainUpdates(new uint64[](0), chainUpdates);

bytes[] memory remotePools = s_tokenPool.getRemotePools(chainSelector);
assertEq(1, remotePools.length);
assertEq(remotePool, remotePools[0]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,14 @@
pragma solidity 0.8.24;

import {Router} from "../../../Router.sol";
import {RateLimiter} from "../../../libraries/RateLimiter.sol";
import {TokenPool} from "../../../pools/TokenPool.sol";
import {TokenPoolSetup} from "./TokenPoolSetup.t.sol";

contract TokenPool_onlyOffRamp is TokenPoolSetup {
function test_onlyOffRamp_Success() public {
uint64 chainSelector = 13377;
uint64 chainSelector = DEST_CHAIN_SELECTOR;
address offRamp = makeAddr("onRamp");

TokenPool.ChainUpdate[] memory chainUpdate = new TokenPool.ChainUpdate[](1);
chainUpdate[0] = TokenPool.ChainUpdate({
remoteChainSelector: chainSelector,
remotePoolAddress: abi.encode(address(1)),
remoteTokenAddress: abi.encode(address(2)),
outboundRateLimiterConfig: _getOutboundRateLimiterConfig(),
inboundRateLimiterConfig: _getInboundRateLimiterConfig()
});
s_tokenPool.applyChainUpdates(new uint64[](0), chainUpdate);

Router.OffRamp[] memory offRampUpdates = new Router.OffRamp[](1);
offRampUpdates[0] = Router.OffRamp({sourceChainSelector: chainSelector, offRamp: offRamp});
s_sourceRouter.applyRampUpdates(new Router.OnRamp[](0), new Router.OffRamp[](0), offRampUpdates);
Expand All @@ -31,7 +20,7 @@ contract TokenPool_onlyOffRamp is TokenPoolSetup {
}

function test_ChainNotAllowed_Revert() public {
uint64 chainSelector = 13377;
uint64 chainSelector = DEST_CHAIN_SELECTOR + 1;
address offRamp = makeAddr("onRamp");

vm.startPrank(offRamp);
Expand Down Expand Up @@ -72,19 +61,9 @@ contract TokenPool_onlyOffRamp is TokenPoolSetup {
}

function test_CallerIsNotARampOnRouter_Revert() public {
uint64 chainSelector = 13377;
uint64 chainSelector = DEST_CHAIN_SELECTOR;
address offRamp = makeAddr("offRamp");

TokenPool.ChainUpdate[] memory chainUpdate = new TokenPool.ChainUpdate[](1);
chainUpdate[0] = TokenPool.ChainUpdate({
remoteChainSelector: chainSelector,
remotePoolAddress: abi.encode(address(1)),
remoteTokenAddress: abi.encode(address(2)),
outboundRateLimiterConfig: _getOutboundRateLimiterConfig(),
inboundRateLimiterConfig: _getInboundRateLimiterConfig()
});
s_tokenPool.applyChainUpdates(new uint64[](0), chainUpdate);

vm.startPrank(offRamp);

vm.expectRevert(abi.encodeWithSelector(TokenPool.CallerIsNotARampOnRouter.selector, offRamp));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,14 @@
pragma solidity 0.8.24;

import {Router} from "../../../Router.sol";
import {RateLimiter} from "../../../libraries/RateLimiter.sol";
import {TokenPool} from "../../../pools/TokenPool.sol";
import {TokenPoolSetup} from "./TokenPoolSetup.t.sol";

contract TokenPool_onlyOnRamp is TokenPoolSetup {
function test_onlyOnRamp_Success() public {
uint64 chainSelector = 13377;
uint64 chainSelector = DEST_CHAIN_SELECTOR;
address onRamp = makeAddr("onRamp");

TokenPool.ChainUpdate[] memory chainUpdate = new TokenPool.ChainUpdate[](1);
chainUpdate[0] = TokenPool.ChainUpdate({
remoteChainSelector: chainSelector,
remotePoolAddress: abi.encode(address(1)),
remoteTokenAddress: abi.encode(address(2)),
outboundRateLimiterConfig: _getOutboundRateLimiterConfig(),
inboundRateLimiterConfig: _getInboundRateLimiterConfig()
});
s_tokenPool.applyChainUpdates(new uint64[](0), chainUpdate);

Router.OnRamp[] memory onRampUpdates = new Router.OnRamp[](1);
onRampUpdates[0] = Router.OnRamp({destChainSelector: chainSelector, onRamp: onRamp});
s_sourceRouter.applyRampUpdates(onRampUpdates, new Router.OffRamp[](0), new Router.OffRamp[](0));
Expand All @@ -31,7 +20,7 @@ contract TokenPool_onlyOnRamp is TokenPoolSetup {
}

function test_ChainNotAllowed_Revert() public {
uint64 chainSelector = 13377;
uint64 chainSelector = DEST_CHAIN_SELECTOR + 1;
address onRamp = makeAddr("onRamp");

vm.startPrank(onRamp);
Expand Down Expand Up @@ -72,19 +61,9 @@ contract TokenPool_onlyOnRamp is TokenPoolSetup {
}

function test_CallerIsNotARampOnRouter_Revert() public {
uint64 chainSelector = 13377;
uint64 chainSelector = DEST_CHAIN_SELECTOR;
address onRamp = makeAddr("onRamp");

TokenPool.ChainUpdate[] memory chainUpdate = new TokenPool.ChainUpdate[](1);
chainUpdate[0] = TokenPool.ChainUpdate({
remoteChainSelector: chainSelector,
remotePoolAddress: abi.encode(address(1)),
remoteTokenAddress: abi.encode(address(2)),
outboundRateLimiterConfig: _getOutboundRateLimiterConfig(),
inboundRateLimiterConfig: _getInboundRateLimiterConfig()
});
s_tokenPool.applyChainUpdates(new uint64[](0), chainUpdate);

vm.startPrank(onRamp);

vm.expectRevert(abi.encodeWithSelector(TokenPool.CallerIsNotARampOnRouter.selector, onRamp));
Expand Down
Loading

0 comments on commit d2fbf3f

Please sign in to comment.