From 6bad8e963827ff616658b75fc25f7b4c332b7937 Mon Sep 17 00:00:00 2001 From: DhairyaSethi <55102840+DhairyaSethi@users.noreply.github.com> Date: Thu, 17 Oct 2024 18:38:27 +0530 Subject: [PATCH] test: for 1_2OnRamp, refactor _messageToEvent --- .../ForkBase.t.sol | 90 +++++++++---------- .../TokenPoolsUpgrade.t.sol | 60 ++++++++++--- 2 files changed, 92 insertions(+), 58 deletions(-) diff --git a/contracts/src/v0.8/ccip/test/pools/GHO/fork/GhoTokenPoolMigrate1_4To1_5/ForkBase.t.sol b/contracts/src/v0.8/ccip/test/pools/GHO/fork/GhoTokenPoolMigrate1_4To1_5/ForkBase.t.sol index 606e632eb5..06abaf2f48 100644 --- a/contracts/src/v0.8/ccip/test/pools/GHO/fork/GhoTokenPoolMigrate1_4To1_5/ForkBase.t.sol +++ b/contracts/src/v0.8/ccip/test/pools/GHO/fork/GhoTokenPoolMigrate1_4To1_5/ForkBase.t.sol @@ -5,6 +5,8 @@ import {Test} from "forge-std/Test.sol"; import {IERC20} from "../../../../../../vendor/openzeppelin-solidity/v4.8.3/contracts/token/ERC20/IERC20.sol"; import {ITypeAndVersion} from "../../../../../../shared/interfaces/ITypeAndVersion.sol"; import {IRouterClient} from "../../../../../interfaces/IRouterClient.sol"; +import {IEVM2AnyOnRamp} from "../../../../../interfaces/IEVM2AnyOnRamp.sol"; +import {IAny2EVMOffRamp} from "../../../../../interfaces/IAny2EVMOffRamp.sol"; import {IRouter as IRouterBase} from "../../../../../interfaces/IRouter.sol"; import {Client} from "../../../../../libraries/Client.sol"; import {Internal} from "../../../../../libraries/Internal.sol"; @@ -31,10 +33,10 @@ contract ForkBase is Test { UpgradeableLockReleaseTokenPool_Sepolia tokenPool; IRouter router; IERC20 token; - address EVM2EVMOnRamp1_2; - address EVM2EVMOnRamp1_5; - address EVM2EVMOffRamp1_2; - address EVM2EVMOffRamp1_5; + IEVM2AnyOnRamp EVM2EVMOnRamp1_2; + IEVM2AnyOnRamp EVM2EVMOnRamp1_5; + IAny2EVMOffRamp EVM2EVMOffRamp1_2; + IAny2EVMOffRamp EVM2EVMOffRamp1_5; address proxyPool; uint64 chainSelector; bytes32 metadataHash; @@ -44,10 +46,10 @@ contract ForkBase is Test { UpgradeableBurnMintTokenPool_ArbSepolia tokenPool; IRouter router; IERC20 token; - address EVM2EVMOnRamp1_2; - address EVM2EVMOnRamp1_5; - address EVM2EVMOffRamp1_2; - address EVM2EVMOffRamp1_5; + IEVM2AnyOnRamp EVM2EVMOnRamp1_2; + IEVM2AnyOnRamp EVM2EVMOnRamp1_5; + IAny2EVMOffRamp EVM2EVMOffRamp1_2; + IAny2EVMOffRamp EVM2EVMOffRamp1_5; address proxyPool; uint64 chainSelector; bytes32 metadataHash; @@ -69,10 +71,10 @@ contract ForkBase is Test { l1.router = IRouter(l1.tokenPool.getRouter()); l2.chainSelector = l1.tokenPool.getSupportedChains()[0]; l1.token = l1.tokenPool.getToken(); - l1.EVM2EVMOnRamp1_2 = 0x1f41c443Cf68750d5c195E2EA7051521d981fC77; // legacy on ramp - l1.EVM2EVMOnRamp1_5 = l1.router.getOnRamp(l2.chainSelector); - l1.EVM2EVMOffRamp1_2 = 0xF18896AB20a09A29e64fdEbA99FDb8EC328f43b1; - l1.EVM2EVMOffRamp1_5 = 0xD2f5edfD4561d6E7599F6c6888Bd353cAFd0c55E; + l1.EVM2EVMOnRamp1_2 = IEVM2AnyOnRamp(0xe4Dd3B16E09c016402585a8aDFdB4A18f772a07e); // legacy on ramp + l1.EVM2EVMOnRamp1_5 = IEVM2AnyOnRamp(l1.router.getOnRamp(l2.chainSelector)); + l1.EVM2EVMOffRamp1_2 = IAny2EVMOffRamp(0xF18896AB20a09A29e64fdEbA99FDb8EC328f43b1); + l1.EVM2EVMOffRamp1_5 = IAny2EVMOffRamp(0xD2f5edfD4561d6E7599F6c6888Bd353cAFd0c55E); vm.prank(alice); l1.token.approve(address(l1.router), type(uint256).max); deal(address(l1.token), alice, 1000e18); @@ -84,10 +86,10 @@ contract ForkBase is Test { l2.router = IRouter(l2.tokenPool.getRouter()); l1.chainSelector = l2.tokenPool.getSupportedChains()[0]; l2.token = l2.tokenPool.getToken(); - l2.EVM2EVMOnRamp1_2 = 0xc1eBd046A4086142479bE3Fc16A4791E2022909a; // legacy on ramp - l2.EVM2EVMOnRamp1_5 = l2.router.getOnRamp(l1.chainSelector); - l2.EVM2EVMOffRamp1_2 = 0x1c71f141b4630EBE52d6aF4894812960abE207eB; - l2.EVM2EVMOffRamp1_5 = 0xBed6e9131916d724418C8a6FE810F727302a5c00; + l2.EVM2EVMOnRamp1_2 = IEVM2AnyOnRamp(0x4205E1Ca0202A248A5D42F5975A8FE56F3E302e9); // legacy on ramp + l2.EVM2EVMOnRamp1_5 = IEVM2AnyOnRamp(l2.router.getOnRamp(l1.chainSelector)); + l2.EVM2EVMOffRamp1_2 = IAny2EVMOffRamp(0x1c71f141b4630EBE52d6aF4894812960abE207eB); + l2.EVM2EVMOffRamp1_5 = IAny2EVMOffRamp(0xBed6e9131916d724418C8a6FE810F727302a5c00); vm.prank(alice); l2.token.approve(address(l2.router), type(uint256).max); deal(address(l2.token), alice, 1000e18); @@ -102,12 +104,12 @@ contract ForkBase is Test { assertEq(l1.token.balanceOf(alice), 1000e18); assertEq(ITypeAndVersion(address(l1.router)).typeAndVersion(), "Router 1.2.0"); assertEq(ITypeAndVersion(l1.proxyPool).typeAndVersion(), "LockReleaseTokenPoolAndProxy 1.5.0"); - assertEq(ITypeAndVersion(l1.EVM2EVMOnRamp1_2).typeAndVersion(), "EVM2EVMOnRamp 1.2.0"); - assertEq(ITypeAndVersion(l1.EVM2EVMOnRamp1_5).typeAndVersion(), "EVM2EVMOnRamp 1.5.0"); - assertEq(ITypeAndVersion(l1.EVM2EVMOffRamp1_2).typeAndVersion(), "EVM2EVMOffRamp 1.2.0"); - assertEq(ITypeAndVersion(l1.EVM2EVMOffRamp1_5).typeAndVersion(), "EVM2EVMOffRamp 1.5.0"); - assertTrue(l1.router.isOffRamp(l2.chainSelector, l1.EVM2EVMOffRamp1_2)); - assertTrue(l1.router.isOffRamp(l2.chainSelector, l1.EVM2EVMOffRamp1_5)); + assertEq(ITypeAndVersion(address(l1.EVM2EVMOnRamp1_2)).typeAndVersion(), "EVM2EVMOnRamp 1.2.0"); + assertEq(ITypeAndVersion(address(l1.EVM2EVMOnRamp1_5)).typeAndVersion(), "EVM2EVMOnRamp 1.5.0"); + assertEq(ITypeAndVersion(address(l1.EVM2EVMOffRamp1_2)).typeAndVersion(), "EVM2EVMOffRamp 1.2.0"); + assertEq(ITypeAndVersion(address(l1.EVM2EVMOffRamp1_5)).typeAndVersion(), "EVM2EVMOffRamp 1.5.0"); + assertTrue(l1.router.isOffRamp(l2.chainSelector, address(l1.EVM2EVMOffRamp1_2))); + assertTrue(l1.router.isOffRamp(l2.chainSelector, address(l1.EVM2EVMOffRamp1_5))); vm.selectFork(l2.forkId); assertEq(l2.chainSelector, 3478487238524512106); @@ -115,12 +117,12 @@ contract ForkBase is Test { assertEq(l2.token.balanceOf(alice), 1000e18); assertEq(ITypeAndVersion(address(l2.router)).typeAndVersion(), "Router 1.2.0"); assertEq(ITypeAndVersion(l2.proxyPool).typeAndVersion(), "BurnMintTokenPoolAndProxy 1.5.0"); - assertEq(ITypeAndVersion(l2.EVM2EVMOnRamp1_2).typeAndVersion(), "EVM2EVMOnRamp 1.2.0"); - assertEq(ITypeAndVersion(l2.EVM2EVMOnRamp1_5).typeAndVersion(), "EVM2EVMOnRamp 1.5.0"); - assertEq(ITypeAndVersion(l2.EVM2EVMOffRamp1_2).typeAndVersion(), "EVM2EVMOffRamp 1.2.0"); - assertEq(ITypeAndVersion(l2.EVM2EVMOffRamp1_5).typeAndVersion(), "EVM2EVMOffRamp 1.5.0"); - assertTrue(l2.router.isOffRamp(l1.chainSelector, l2.EVM2EVMOffRamp1_2)); - assertTrue(l2.router.isOffRamp(l1.chainSelector, l2.EVM2EVMOffRamp1_5)); + assertEq(ITypeAndVersion(address(l2.EVM2EVMOnRamp1_2)).typeAndVersion(), "EVM2EVMOnRamp 1.2.0"); + assertEq(ITypeAndVersion(address(l2.EVM2EVMOnRamp1_5)).typeAndVersion(), "EVM2EVMOnRamp 1.5.0"); + assertEq(ITypeAndVersion(address(l2.EVM2EVMOffRamp1_2)).typeAndVersion(), "EVM2EVMOffRamp 1.2.0"); + assertEq(ITypeAndVersion(address(l2.EVM2EVMOffRamp1_5)).typeAndVersion(), "EVM2EVMOffRamp 1.5.0"); + assertTrue(l2.router.isOffRamp(l1.chainSelector, address(l2.EVM2EVMOffRamp1_2))); + assertTrue(l2.router.isOffRamp(l1.chainSelector, address(l2.EVM2EVMOffRamp1_5))); _label(); } @@ -141,17 +143,11 @@ contract ForkBase is Test { function _messageToEvent( Client.EVM2AnyMessage memory message, - uint64 seqNum, - uint64 nonce, + IEVM2AnyOnRamp onRamp, uint256 feeTokenAmount, address originalSender, - bytes32 metadataHash, - uint32 destGasAmount + bool isL1 ) public view returns (Internal.EVM2EVMMessage memory) { - address feeToken = metadataHash == l1.metadataHash ? l1.router.getWrappedNative() : l2.router.getWrappedNative(); - address destTokenAddress = metadataHash == l1.metadataHash ? address(l2.token) : address(l1.token); - address sourcePool = metadataHash == l1.metadataHash ? l1.proxyPool : l2.proxyPool; - // Slicing is only available for calldata. So we have to build a new bytes array. bytes memory args = new bytes(message.extraArgs.length - 4); for (uint256 i = 4; i < message.extraArgs.length; ++i) { @@ -159,18 +155,18 @@ contract ForkBase is Test { } Client.EVMExtraArgsV1 memory extraArgs = abi.decode(args, (Client.EVMExtraArgsV1)); Internal.EVM2EVMMessage memory messageEvent = Internal.EVM2EVMMessage({ - sequenceNumber: seqNum, + sequenceNumber: onRamp.getExpectedNextSequenceNumber(), feeTokenAmount: feeTokenAmount, sender: originalSender, - nonce: nonce, + nonce: onRamp.getSenderNonce(originalSender) + 1, gasLimit: extraArgs.gasLimit, strict: false, - sourceChainSelector: l1.chainSelector, + sourceChainSelector: isL1 ? l1.chainSelector : l2.chainSelector, receiver: abi.decode(message.receiver, (address)), data: message.data, tokenAmounts: message.tokenAmounts, sourceTokenData: new bytes[](message.tokenAmounts.length), - feeToken: feeToken, + feeToken: isL1 ? l1.router.getWrappedNative() : l2.router.getWrappedNative(), messageId: "" }); @@ -178,21 +174,21 @@ contract ForkBase is Test { // change introduced in 1.5 upgrade messageEvent.sourceTokenData[i] = abi.encode( SourceTokenData({ - sourcePoolAddress: abi.encode(sourcePool), - destTokenAddress: abi.encode(destTokenAddress), + sourcePoolAddress: abi.encode(isL1 ? l1.proxyPool : l2.proxyPool), + destTokenAddress: abi.encode(address(isL1 ? l2.token : l1.token)), extraData: "", - destGasAmount: destGasAmount + destGasAmount: 90000 }) ); } - messageEvent.messageId = Internal._hash(messageEvent, metadataHash); + messageEvent.messageId = Internal._hash(messageEvent, isL1 ? l1.metadataHash : l2.metadataHash); return messageEvent; } function _generateMetadataHash(uint64 sourceChainSelector) internal view returns (bytes32) { uint64 destChainSelector = sourceChainSelector == l1.chainSelector ? l2.chainSelector : l1.chainSelector; - address onRamp = sourceChainSelector == l1.chainSelector ? l1.EVM2EVMOnRamp1_5 : l2.EVM2EVMOnRamp1_5; + address onRamp = address(sourceChainSelector == l1.chainSelector ? l1.EVM2EVMOnRamp1_5 : l2.EVM2EVMOnRamp1_5); return keccak256(abi.encode(Internal.EVM_2_EVM_MESSAGE_HASH, sourceChainSelector, destChainSelector, onRamp)); } @@ -208,6 +204,8 @@ contract ForkBase is Test { vm.label(address(l1.proxyPool), "l1.proxyPool"); vm.label(address(l1.EVM2EVMOnRamp1_2), "l1.EVM2EVMOnRamp1_2"); vm.label(address(l1.EVM2EVMOnRamp1_5), "l1.EVM2EVMOnRamp1_5"); + vm.label(address(l1.EVM2EVMOffRamp1_2), "l1.EVM2EVMOffRamp1_2"); + vm.label(address(l1.EVM2EVMOffRamp1_5), "l1.EVM2EVMOffRamp1_5"); vm.label(address(l2.tokenPool), "l2.tokenPool"); vm.label(address(l2.token), "l2.token"); @@ -215,6 +213,8 @@ contract ForkBase is Test { vm.label(address(l2.proxyPool), "l2.proxyPool"); vm.label(address(l2.EVM2EVMOnRamp1_2), "l2.EVM2EVMOnRamp1_2"); vm.label(address(l2.EVM2EVMOnRamp1_5), "l2.EVM2EVMOnRamp1_5"); + vm.label(address(l2.EVM2EVMOffRamp1_2), "l2.EVM2EVMOffRamp1_2"); + vm.label(address(l2.EVM2EVMOffRamp1_5), "l2.EVM2EVMOffRamp1_5"); } } diff --git a/contracts/src/v0.8/ccip/test/pools/GHO/fork/GhoTokenPoolMigrate1_4To1_5/TokenPoolsUpgrade.t.sol b/contracts/src/v0.8/ccip/test/pools/GHO/fork/GhoTokenPoolMigrate1_4To1_5/TokenPoolsUpgrade.t.sol index 56c8e237be..11e2f12403 100644 --- a/contracts/src/v0.8/ccip/test/pools/GHO/fork/GhoTokenPoolMigrate1_4To1_5/TokenPoolsUpgrade.t.sol +++ b/contracts/src/v0.8/ccip/test/pools/GHO/fork/GhoTokenPoolMigrate1_4To1_5/TokenPoolsUpgrade.t.sol @@ -30,19 +30,53 @@ contract ForkPoolUpgradeAfterMigration is ForkBase { l2.tokenPool.setProxyPool(l2.proxyPool); } - function testLockOrBurnViaLegacyRouterL1() public { - vm.selectFork(l1.forkId); - + function testLockOrBurnViaRouter() public { uint256 amount = 10e18; Client.EVM2AnyMessage memory message = _generateMessage(alice, 1); - message.tokenAmounts[0] = Client.EVMTokenAmount({token: address(l1.token), amount: amount}); - uint256 feeTokenAmount = l1.router.getFee(l2.chainSelector, message); + { + vm.selectFork(l1.forkId); + + message.tokenAmounts[0] = Client.EVMTokenAmount({token: address(l1.token), amount: amount}); + uint256 feeTokenAmount = l1.router.getFee(l2.chainSelector, message); + + // router uses 1_5 onRamp + assertEq(l1.router.getOnRamp(l2.chainSelector), address(l1.EVM2EVMOnRamp1_5)); + vm.expectEmit(); + emit CCIPSendRequested(_messageToEvent(message, l1.EVM2EVMOnRamp1_5, feeTokenAmount, alice, true)); + vm.prank(alice); + l1.router.ccipSend{value: feeTokenAmount}(l2.chainSelector, message); + } + + { + vm.selectFork(l2.forkId); + + message.tokenAmounts[0] = Client.EVMTokenAmount({token: address(l2.token), amount: amount}); + uint256 feeTokenAmount = l2.router.getFee(l1.chainSelector, message); - vm.expectEmit(); - emit CCIPSendRequested(_messageToEvent(message, 220, 1, feeTokenAmount, alice, l1.metadataHash, uint32(90000))); - vm.prank(alice); - l1.router.ccipSend{value: feeTokenAmount}(l2.chainSelector, message); + // router uses 1_5 onRamp + assertEq(l2.router.getOnRamp(l1.chainSelector), address(l2.EVM2EVMOnRamp1_5)); + vm.expectEmit(); + emit CCIPSendRequested(_messageToEvent(message, l2.EVM2EVMOnRamp1_5, feeTokenAmount, alice, false)); + vm.prank(alice); + l2.router.ccipSend{value: feeTokenAmount}(l1.chainSelector, message); + } + } + + function testRevertLockOrBurnVia1_2OnRamp() public { + uint256 amount = 10e18; + { + vm.selectFork(l1.forkId); + vm.expectRevert(abi.encodeWithSelector(CallerIsNotARampOnRouter.selector, address(l1.EVM2EVMOnRamp1_2))); + vm.prank(address(l1.EVM2EVMOnRamp1_2)); + l1.tokenPool.lockOrBurn(alice, abi.encode(alice), amount, l2.chainSelector, ""); + } + { + vm.selectFork(l2.forkId); + vm.expectRevert(abi.encodeWithSelector(CallerIsNotARampOnRouter.selector, address(l2.EVM2EVMOnRamp1_2))); + vm.prank(address(l2.EVM2EVMOnRamp1_2)); + l2.tokenPool.lockOrBurn(alice, abi.encode(alice), amount, l1.chainSelector, ""); + } } function testReleaseOrMintVia1_2OffRamp() public { @@ -51,7 +85,7 @@ contract ForkPoolUpgradeAfterMigration is ForkBase { vm.selectFork(l1.forkId); uint256 balanceBefore = l1.token.balanceOf(alice); // mock release on legacy offramp - vm.prank(l1.EVM2EVMOffRamp1_2); + vm.prank(address(l1.EVM2EVMOffRamp1_2)); l1.tokenPool.releaseOrMint(abi.encode(alice), alice, amount, l2.chainSelector, ""); assertEq(l1.token.balanceOf(alice), balanceBefore + amount); } @@ -59,7 +93,7 @@ contract ForkPoolUpgradeAfterMigration is ForkBase { vm.selectFork(l2.forkId); uint256 balanceBefore = l2.token.balanceOf(alice); // mock release on legacy offramp - vm.prank(l2.EVM2EVMOffRamp1_2); + vm.prank(address(l2.EVM2EVMOffRamp1_2)); l2.tokenPool.releaseOrMint(abi.encode(alice), alice, amount, l1.chainSelector, ""); assertEq(l2.token.balanceOf(alice), balanceBefore + amount); } @@ -71,7 +105,7 @@ contract ForkPoolUpgradeAfterMigration is ForkBase { vm.selectFork(l1.forkId); uint256 balanceBefore = l1.token.balanceOf(alice); // mock release on legacy offramp - vm.prank(l1.EVM2EVMOffRamp1_5); + vm.prank(address(l1.EVM2EVMOffRamp1_5)); l1.tokenPool.releaseOrMint(abi.encode(alice), alice, amount, l2.chainSelector, ""); assertEq(l1.token.balanceOf(alice), balanceBefore + amount); } @@ -79,7 +113,7 @@ contract ForkPoolUpgradeAfterMigration is ForkBase { vm.selectFork(l2.forkId); uint256 balanceBefore = l2.token.balanceOf(alice); // mock release on legacy offramp - vm.prank(l2.EVM2EVMOffRamp1_5); + vm.prank(address(l2.EVM2EVMOffRamp1_5)); l2.tokenPool.releaseOrMint(abi.encode(alice), alice, amount, l1.chainSelector, ""); assertEq(l2.token.balanceOf(alice), balanceBefore + amount); }