From 0688f98dcade97d0d0c9b47185052a9e4daac442 Mon Sep 17 00:00:00 2001 From: Josh Date: Wed, 25 Sep 2024 15:28:45 -0400 Subject: [PATCH] formatting --- .../tokenAdminRegistry/TokenPoolFactory.t.sol | 179 +++++------------- .../tokenAdminRegistry/TokenPoolFactory.sol | 6 +- 2 files changed, 49 insertions(+), 136 deletions(-) diff --git a/contracts/src/v0.8/ccip/test/tokenAdminRegistry/TokenPoolFactory.t.sol b/contracts/src/v0.8/ccip/test/tokenAdminRegistry/TokenPoolFactory.t.sol index 999feeb2ab..5580ee7226 100644 --- a/contracts/src/v0.8/ccip/test/tokenAdminRegistry/TokenPoolFactory.t.sol +++ b/contracts/src/v0.8/ccip/test/tokenAdminRegistry/TokenPoolFactory.t.sol @@ -40,12 +40,8 @@ contract TokenPoolFactorySetup is TokenAdminRegistrySetup { s_registryModuleOwnerCustom = new RegistryModuleOwnerCustom(address(s_tokenAdminRegistry)); s_tokenAdminRegistry.addRegistryModule(address(s_registryModuleOwnerCustom)); - s_tokenPoolFactory = new TokenPoolFactory( - s_tokenAdminRegistry, - s_registryModuleOwnerCustom, - s_rmnProxy, - address(s_sourceRouter) - ); + s_tokenPoolFactory = + new TokenPoolFactory(s_tokenAdminRegistry, s_registryModuleOwnerCustom, s_rmnProxy, address(s_sourceRouter)); // Create Init Code for BurnMintERC20 TestToken with 18 decimals and supply cap of max uint256 value s_tokenCreationParams = abi.encode("TestToken", "TT", 18, type(uint256).max, PREMINT_AMOUNT, OWNER); @@ -67,12 +63,7 @@ contract TokenPoolFactoryTests is TokenPoolFactorySetup { function test_TokenPoolFactory_Constructor_Revert() public { // Revert cause the tokenAdminRegistry is address(0) vm.expectRevert(TokenPoolFactory.InvalidZeroAddress.selector); - new TokenPoolFactory( - ITokenAdminRegistry(address(0)), - RegistryModuleOwnerCustom(address(0)), - address(0), - address(0) - ); + new TokenPoolFactory(ITokenAdminRegistry(address(0)), RegistryModuleOwnerCustom(address(0)), address(0), address(0)); new TokenPoolFactory( ITokenAdminRegistry(address(0xdeadbeef)), @@ -87,11 +78,8 @@ contract TokenPoolFactoryTests is TokenPoolFactorySetup { bytes32 dynamicSalt = keccak256(abi.encodePacked(FAKE_SALT, OWNER)); - address predictedTokenAddress = Create2.computeAddress( - dynamicSalt, - keccak256(s_tokenInitCode), - address(s_tokenPoolFactory) - ); + address predictedTokenAddress = + Create2.computeAddress(dynamicSalt, keccak256(s_tokenInitCode), address(s_tokenPoolFactory)); // Create the constructor params for the predicted pool bytes memory poolCreationParams = abi.encode(predictedTokenAddress, new address[](0), s_rmnProxy, s_sourceRouter); @@ -99,17 +87,11 @@ contract TokenPoolFactoryTests is TokenPoolFactorySetup { // Predict the address of the pool before we make the tx by using the init code and the params bytes memory predictedPoolInitCode = abi.encodePacked(s_poolInitCode, poolCreationParams); - address predictedPoolAddress = dynamicSalt.computeAddress( - keccak256(predictedPoolInitCode), - address(s_tokenPoolFactory) - ); + address predictedPoolAddress = + dynamicSalt.computeAddress(keccak256(predictedPoolInitCode), address(s_tokenPoolFactory)); (address tokenAddress, address poolAddress) = s_tokenPoolFactory.deployTokenAndTokenPool( - new TokenPoolFactory.RemoteTokenPoolInfo[](0), - s_tokenInitCode, - s_poolInitCode, - poolCreationParams, - FAKE_SALT + new TokenPoolFactory.RemoteTokenPoolInfo[](0), s_tokenInitCode, s_poolInitCode, poolCreationParams, FAKE_SALT ); assertNotEq(address(0), tokenAddress, "Token Address should not be 0"); @@ -136,27 +118,20 @@ contract TokenPoolFactoryTests is TokenPoolFactorySetup { RegistryModuleOwnerCustom newRegistryModule = new RegistryModuleOwnerCustom(address(newTokenAdminRegistry)); // We want to deploy a new factory and Owner Module. - TokenPoolFactory newTokenPoolFactory = new TokenPoolFactory( - newTokenAdminRegistry, - newRegistryModule, - s_rmnProxy, - address(s_destRouter) - ); + TokenPoolFactory newTokenPoolFactory = + new TokenPoolFactory(newTokenAdminRegistry, newRegistryModule, s_rmnProxy, address(s_destRouter)); newTokenAdminRegistry.addRegistryModule(address(newRegistryModule)); - TokenPoolFactory.RemoteChainConfig memory remoteChainConfig = TokenPoolFactory.RemoteChainConfig( - address(newTokenPoolFactory), - address(s_destRouter), - address(s_rmnProxy) - ); + TokenPoolFactory.RemoteChainConfig memory remoteChainConfig = + TokenPoolFactory.RemoteChainConfig(address(newTokenPoolFactory), address(s_destRouter), address(s_rmnProxy)); { TokenPoolFactory.RemoteChainConfig[] memory remoteChainConfigs = new TokenPoolFactory.RemoteChainConfig[](1); remoteChainConfigs[0] = remoteChainConfig; - TokenPoolFactory.RemoteChainConfigUpdate[] - memory remoteChainConfigUpdates = new TokenPoolFactory.RemoteChainConfigUpdate[](1); + TokenPoolFactory.RemoteChainConfigUpdate[] memory remoteChainConfigUpdates = + new TokenPoolFactory.RemoteChainConfigUpdate[](1); remoteChainConfigUpdates[0] = TokenPoolFactory.RemoteChainConfigUpdate(DEST_CHAIN_SELECTOR, remoteChainConfig); // Add the new token Factory to the remote chain config and set it for the simulated destination chain @@ -170,28 +145,16 @@ contract TokenPoolFactoryTests is TokenPoolFactorySetup { // The only field that matters is DEST_CHAIN_SELECTOR because we dont want any existing token pool or token // on the remote chain remoteTokenPools[0] = TokenPoolFactory.RemoteTokenPoolInfo( - DEST_CHAIN_SELECTOR, - "", - "", - s_tokenInitCode, - RateLimiter.Config(false, 0, 0) + DEST_CHAIN_SELECTOR, "", "", s_tokenInitCode, RateLimiter.Config(false, 0, 0) ); // Predict the address of the token and pool on the DESTINATION chain - address predictedTokenAddress = dynamicSalt.computeAddress( - keccak256(s_tokenInitCode), - address(newTokenPoolFactory) - ); + address predictedTokenAddress = dynamicSalt.computeAddress(keccak256(s_tokenInitCode), address(newTokenPoolFactory)); // Since the remote chain information was provided, we should be able to get the information from the newly // deployed token pool using the available getter functions - (, address poolAddress) = s_tokenPoolFactory.deployTokenAndTokenPool( - remoteTokenPools, - s_tokenInitCode, - s_poolInitCode, - "", - FAKE_SALT - ); + (, address poolAddress) = + s_tokenPoolFactory.deployTokenAndTokenPool(remoteTokenPools, s_tokenInitCode, s_poolInitCode, "", FAKE_SALT); // Ensure that the remote Token was set to the one we predicted assertEq( @@ -204,21 +167,15 @@ contract TokenPoolFactoryTests is TokenPoolFactorySetup { // Create the constructor params for the predicted pool // The predictedTokenAddress is NOT abi-encoded since the raw evm-address // is used in the constructor params - bytes memory predictedPoolCreationParams = abi.encode( - predictedTokenAddress, - new address[](0), - s_rmnProxy, - address(s_destRouter) - ); + bytes memory predictedPoolCreationParams = + abi.encode(predictedTokenAddress, new address[](0), s_rmnProxy, address(s_destRouter)); // Take the init code and concat the destination params to it, the initCode shouldn't change bytes memory predictedPoolInitCode = abi.encodePacked(s_poolInitCode, predictedPoolCreationParams); // Predict the address of the pool on the DESTINATION chain - address predictedPoolAddress = dynamicSalt.computeAddress( - keccak256(predictedPoolInitCode), - address(newTokenPoolFactory) - ); + address predictedPoolAddress = + dynamicSalt.computeAddress(keccak256(predictedPoolInitCode), address(newTokenPoolFactory)); // Assert that the address set for the remote pool is the same as the predicted address assertEq( @@ -231,11 +188,7 @@ contract TokenPoolFactoryTests is TokenPoolFactorySetup { // On the new token pool factory, representing a destination chain, // deploy a new token and a new pool (address newTokenAddress, address newPoolAddress) = newTokenPoolFactory.deployTokenAndTokenPool( - new TokenPoolFactory.RemoteTokenPoolInfo[](0), - s_tokenInitCode, - s_poolInitCode, - "", - FAKE_SALT + new TokenPoolFactory.RemoteTokenPoolInfo[](0), s_tokenInitCode, s_poolInitCode, "", FAKE_SALT ); assertEq( @@ -255,42 +208,28 @@ contract TokenPoolFactoryTests is TokenPoolFactorySetup { vm.startPrank(OWNER); bytes32 dynamicSalt = keccak256(abi.encodePacked(FAKE_SALT, OWNER)); - FactoryBurnMintERC20 newRemoteToken = new FactoryBurnMintERC20( - "TestToken", - "TT", - 18, - type(uint256).max, - PREMINT_AMOUNT, - OWNER - ); + FactoryBurnMintERC20 newRemoteToken = + new FactoryBurnMintERC20("TestToken", "TT", 18, type(uint256).max, PREMINT_AMOUNT, OWNER); // We have to create a new factory, registry module, and token admin registry to simulate the other chain - TokenAdminRegistry newTokenAdminRegistry = new TokenAdminRegistry(); RegistryModuleOwnerCustom newRegistryModule = new RegistryModuleOwnerCustom(address(newTokenAdminRegistry)); // We want to deploy a new factory and Owner Module. - TokenPoolFactory newTokenPoolFactory = new TokenPoolFactory( - newTokenAdminRegistry, - newRegistryModule, - s_rmnProxy, - address(s_destRouter) - ); + TokenPoolFactory newTokenPoolFactory = + new TokenPoolFactory(newTokenAdminRegistry, newRegistryModule, s_rmnProxy, address(s_destRouter)); newTokenAdminRegistry.addRegistryModule(address(newRegistryModule)); { - TokenPoolFactory.RemoteChainConfig memory remoteChainConfig = TokenPoolFactory.RemoteChainConfig( - address(newTokenPoolFactory), - address(s_destRouter), - address(s_rmnProxy) - ); + TokenPoolFactory.RemoteChainConfig memory remoteChainConfig = + TokenPoolFactory.RemoteChainConfig(address(newTokenPoolFactory), address(s_destRouter), address(s_rmnProxy)); TokenPoolFactory.RemoteChainConfig[] memory remoteChainConfigs = new TokenPoolFactory.RemoteChainConfig[](1); remoteChainConfigs[0] = remoteChainConfig; - TokenPoolFactory.RemoteChainConfigUpdate[] - memory remoteChainConfigUpdates = new TokenPoolFactory.RemoteChainConfigUpdate[](1); + TokenPoolFactory.RemoteChainConfigUpdate[] memory remoteChainConfigUpdates = + new TokenPoolFactory.RemoteChainConfigUpdate[](1); remoteChainConfigUpdates[0] = TokenPoolFactory.RemoteChainConfigUpdate(DEST_CHAIN_SELECTOR, remoteChainConfig); // Add the new token Factory to the remote chain config and set it for the simulated destination chain @@ -304,22 +243,13 @@ contract TokenPoolFactoryTests is TokenPoolFactorySetup { // The only field that matters is DEST_CHAIN_SELECTOR because we dont want any existing token pool or token // on the remote chain remoteTokenPools[0] = TokenPoolFactory.RemoteTokenPoolInfo( - DEST_CHAIN_SELECTOR, - "", - abi.encode(address(newRemoteToken)), - s_tokenInitCode, - RateLimiter.Config(false, 0, 0) + DEST_CHAIN_SELECTOR, "", abi.encode(address(newRemoteToken)), s_tokenInitCode, RateLimiter.Config(false, 0, 0) ); // Since the remote chain information was provided, we should be able to get the information from the newly // deployed token pool using the available getter functions - (address tokenAddress, address poolAddress) = s_tokenPoolFactory.deployTokenAndTokenPool( - remoteTokenPools, - s_tokenInitCode, - s_poolInitCode, - "", - FAKE_SALT - ); + (address tokenAddress, address poolAddress) = + s_tokenPoolFactory.deployTokenAndTokenPool(remoteTokenPools, s_tokenInitCode, s_poolInitCode, "", FAKE_SALT); assertEq(address(TokenPool(poolAddress).getToken()), tokenAddress, "Token Address should have been set locally"); @@ -333,21 +263,15 @@ contract TokenPoolFactoryTests is TokenPoolFactorySetup { // Create the constructor params for the predicted pool // The predictedTokenAddress is NOT abi-encoded since the raw evm-address // is used in the constructor params - bytes memory predictedPoolCreationParams = abi.encode( - address(newRemoteToken), - new address[](0), - s_rmnProxy, - address(s_destRouter) - ); + bytes memory predictedPoolCreationParams = + abi.encode(address(newRemoteToken), new address[](0), s_rmnProxy, address(s_destRouter)); // Take the init code and concat the destination params to it, the initCode shouldn't change bytes memory predictedPoolInitCode = abi.encodePacked(s_poolInitCode, predictedPoolCreationParams); // Predict the address of the pool on the DESTINATION chain - address predictedPoolAddress = dynamicSalt.computeAddress( - keccak256(predictedPoolInitCode), - address(newTokenPoolFactory) - ); + address predictedPoolAddress = + dynamicSalt.computeAddress(keccak256(predictedPoolInitCode), address(newTokenPoolFactory)); // Assert that the address set for the remote pool is the same as the predicted address assertEq( @@ -359,11 +283,7 @@ contract TokenPoolFactoryTests is TokenPoolFactorySetup { // On the new token pool factory, representing a destination chain, // deploy a new token and a new pool address newPoolAddress = newTokenPoolFactory.deployTokenPoolWithExistingToken( - address(newRemoteToken), - new TokenPoolFactory.RemoteTokenPoolInfo[](0), - s_poolInitCode, - "", - FAKE_SALT + address(newRemoteToken), new TokenPoolFactory.RemoteTokenPoolInfo[](0), s_poolInitCode, "", FAKE_SALT ); assertEq( @@ -396,19 +316,11 @@ contract TokenPoolFactoryTests is TokenPoolFactorySetup { TokenPoolFactory.RemoteTokenPoolInfo[] memory remoteTokenPools = new TokenPoolFactory.RemoteTokenPoolInfo[](1); remoteTokenPools[0] = TokenPoolFactory.RemoteTokenPoolInfo( - DEST_CHAIN_SELECTOR, - RANDOM_POOL_ADDRESS, - RANDOM_TOKEN_ADDRESS, - "", - RateLimiter.Config(false, 0, 0) + DEST_CHAIN_SELECTOR, RANDOM_POOL_ADDRESS, RANDOM_TOKEN_ADDRESS, "", RateLimiter.Config(false, 0, 0) ); (address tokenAddress, address poolAddress) = s_tokenPoolFactory.deployTokenAndTokenPool( - remoteTokenPools, - s_tokenInitCode, - s_poolInitCode, - poolCreationParams, - FAKE_SALT + remoteTokenPools, s_tokenInitCode, s_poolInitCode, poolCreationParams, FAKE_SALT ); assertNotEq(address(0), tokenAddress, "Token Address should not be 0"); @@ -448,15 +360,14 @@ contract TokenPoolFactoryTests is TokenPoolFactorySetup { remoteChainConfigs[0] = remoteChainConfig; - TokenPoolFactory.RemoteChainConfigUpdate[] - memory remoteChainConfigUpdates = new TokenPoolFactory.RemoteChainConfigUpdate[](1); + TokenPoolFactory.RemoteChainConfigUpdate[] memory remoteChainConfigUpdates = + new TokenPoolFactory.RemoteChainConfigUpdate[](1); remoteChainConfigUpdates[0] = TokenPoolFactory.RemoteChainConfigUpdate(DEST_CHAIN_SELECTOR, remoteChainConfig); s_tokenPoolFactory.updateRemoteChainConfig(remoteChainConfigUpdates); - TokenPoolFactory.RemoteChainConfig memory updatedRemoteChainConfig = s_tokenPoolFactory.getRemoteChainConfig( - DEST_CHAIN_SELECTOR - ); + TokenPoolFactory.RemoteChainConfig memory updatedRemoteChainConfig = + s_tokenPoolFactory.getRemoteChainConfig(DEST_CHAIN_SELECTOR); assertEq( remoteChainConfig.remotePoolFactory, diff --git a/contracts/src/v0.8/ccip/tokenAdminRegistry/TokenPoolFactory.sol b/contracts/src/v0.8/ccip/tokenAdminRegistry/TokenPoolFactory.sol index 5055a1065f..64634bff3c 100644 --- a/contracts/src/v0.8/ccip/tokenAdminRegistry/TokenPoolFactory.sol +++ b/contracts/src/v0.8/ccip/tokenAdminRegistry/TokenPoolFactory.sol @@ -88,7 +88,8 @@ contract TokenPoolFactory is OwnerIsCreator, ITypeAndVersion { bytes memory tokenPoolInitArgs, bytes32 salt ) external returns (address, address) { - // Ensure a unique deployment between senders even if the same input parameter is used + // Ensure a unique deployment between senders even if the same input parameter is used to prevent + // DOS/Frontrunning attacks salt = keccak256(abi.encodePacked(salt, msg.sender)); // Deploy the token. The constructor parameters are already provided in the tokenInitCode @@ -124,7 +125,8 @@ contract TokenPoolFactory is OwnerIsCreator, ITypeAndVersion { bytes memory tokenPoolInitArgs, bytes32 salt ) external returns (address poolAddress) { - // Ensure a unique deployment between senders even if the same input parameter is used + // Ensure a unique deployment between senders even if the same input parameter is used to prevent + // DOS/Frontrunning attacks salt = keccak256(abi.encodePacked(salt, msg.sender)); // create the token pool and return the address