From 1f65b36052f45415b3cb676a10f1e7fe9c9db755 Mon Sep 17 00:00:00 2001 From: adu Date: Wed, 3 Jul 2024 20:45:11 +0800 Subject: [PATCH] feat: add non-zero/non-empty check for function params --- src/core/ExocoreGateway.sol | 17 +++++++++++------ test/foundry/ExocoreDeployer.t.sol | 6 +++--- test/foundry/unit/ExocoreGateway.t.sol | 20 ++++++++++++++------ 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/src/core/ExocoreGateway.sol b/src/core/ExocoreGateway.sol index 519016ee..2f8d5ac7 100644 --- a/src/core/ExocoreGateway.sol +++ b/src/core/ExocoreGateway.sol @@ -134,7 +134,13 @@ contract ExocoreGateway is string calldata metaInfo, string calldata signatureType ) public onlyOwner whenNotPaused { - _validatePeer(clientChainId, clientChainGateway); + require(clientChainId != uint32(0), "ExocoreGateway: endpoint id cannot be zero"); + require(clientChainGateway != bytes32(0), "ExocoreGateway: client chain gateway cannot be empty"); + require(addressLength != 0, "ExocoreGateway: address length cannot be zero"); + require(bytes(name).length != 0, "ExocoreGateway: name cannot be empty"); + require(bytes(metaInfo).length != 0, "ExocoreGateway: meta data cannot be empty"); + // signature type could be left as empty for current implementation + _registerClientChain(clientChainId, addressLength, name, metaInfo, signatureType); super.setPeer(clientChainId, clientChainGateway); @@ -174,6 +180,8 @@ contract ExocoreGateway is require(tokens[i] != bytes32(0), "ExocoreGateway: token cannot be zero address"); require(!isWhitelistedToken[tokens[i]], "ExocoreGateway: token has already been added to whitelist before"); require(tvlLimits[i] > 0, "ExocoreGateway: tvl limit should not be zero"); + require(bytes(names[i]).length != 0, "ExocoreGateway: name cannot be empty"); + require(bytes(metaData[i]).length != 0, "ExocoreGateway: meta data cannot be empty"); bool success = ASSETS_CONTRACT.registerToken( clientChainId, abi.encodePacked(tokens[i]), decimals[i], tvlLimits[i], names[i], metaData[i] @@ -207,6 +215,8 @@ contract ExocoreGateway is require(tokens[i] != bytes32(0), "ExocoreGateway: token cannot be zero address"); require(isWhitelistedToken[tokens[i]], "ExocoreGateway: token has not been added to whitelist before"); require(tvlLimits[i] > 0, "ExocoreGateway: tvl limit should not be zero"); + require(bytes(names[i]).length != 0, "ExocoreGateway: name cannot be empty"); + require(bytes(metaData[i]).length != 0, "ExocoreGateway: meta data cannot be empty"); bool success = ASSETS_CONTRACT.registerToken( clientChainId, abi.encodePacked(tokens[i]), decimals[i], tvlLimits[i], names[i], metaData[i] @@ -245,11 +255,6 @@ contract ExocoreGateway is } } - function _validatePeer(uint32 clientChainId, bytes32 clientChainGateway) internal pure { - require(clientChainId != uint32(0), "ExocoreGateway: zero value is not invalid endpoint id"); - require(clientChainGateway != bytes32(0), "ExocoreGateway: client chain gateway cannot be empty"); - } - function _registerClientChain( uint32 clientChainId, uint8 addressLength, diff --git a/test/foundry/ExocoreDeployer.t.sol b/test/foundry/ExocoreDeployer.t.sol index ada6d332..99ad969f 100644 --- a/test/foundry/ExocoreDeployer.t.sol +++ b/test/foundry/ExocoreDeployer.t.sol @@ -130,13 +130,13 @@ contract ExocoreDeployer is Test { decimals[0] = 18; tvlLimits[0] = 1e8 ether; names[0] = "RestakeToken"; - metaData[0] = ""; + metaData[0] = "ERC20 LST token"; whitelistTokens.push(bytes32(bytes20(VIRTUAL_STAKED_ETH_ADDRESS))); decimals[1] = 18; tvlLimits[1] = 1e8 ether; names[1] = "NativeStakedETH"; - metaData[1] = ""; + metaData[1] = "natively staked ETH on Ethereum"; // -- add whitelist tokens workflow test -- @@ -312,7 +312,7 @@ contract ExocoreDeployer is Test { // messages. On Exocore side, this is done by calling registerClientChain clientGateway.setPeer(exocoreChainId, address(exocoreGateway).toBytes32()); exocoreGateway.registerOrUpdateClientChain( - clientChainId, address(clientGateway).toBytes32(), 20, "clientChain", "", "secp256k1" + clientChainId, address(clientGateway).toBytes32(), 20, "clientChain", "EVM compatible client chain", "secp256k1" ); vm.stopPrank(); } diff --git a/test/foundry/unit/ExocoreGateway.t.sol b/test/foundry/unit/ExocoreGateway.t.sol index 203b80e0..a4caf040 100644 --- a/test/foundry/unit/ExocoreGateway.t.sol +++ b/test/foundry/unit/ExocoreGateway.t.sol @@ -97,7 +97,7 @@ contract SetUp is Test { vm.startPrank(exocoreValidatorSet.addr); exocoreLzEndpoint.setDestLzEndpoint(address(clientGateway), address(clientLzEndpoint)); exocoreGateway.registerOrUpdateClientChain( - clientChainId, address(clientGateway).toBytes32(), 20, "clientChain", "", "secp256k1" + clientChainId, address(clientGateway).toBytes32(), 20, "clientChain", "EVM compatible client chain", "secp256k1" ); vm.stopPrank(); @@ -276,6 +276,10 @@ contract AddWhitelistTokens is SetUp { whitelistTokens[0] = bytes32(bytes20(address(restakeToken))); tvlLimits[0] = 1e8 ether; tvlLimits[1] = 1e8 ether; + names[0] = "LST-1"; + names[1] = "LST-2"; + metaData[0] = "LST token"; + metaData[1] = "LST token"; uint256 messageLength = TOKEN_ADDRESS_BYTES_LENGTH * whitelistTokens.length + 2; uint256 nativeFee = exocoreGateway.quote(clientChainId, new bytes(messageLength)); @@ -307,7 +311,7 @@ contract AddWhitelistTokens is SetUp { decimals[0] = 18; tvlLimits[0] = 1e8 ether; names[0] = "RestakeToken"; - metaData[0] = ""; + metaData[0] = "ERC20 LST token"; uint256 messageLength = TOKEN_ADDRESS_BYTES_LENGTH * whitelistTokens.length + 2; uint256 nativeFee = exocoreGateway.quote(clientChainId, new bytes(messageLength)); @@ -411,13 +415,17 @@ contract UpdateWhitelistTokens is SetUp { decimals[0] = 18; tvlLimits[0] = 1e8 ether; names[0] = "RestakeToken"; - metaData[0] = ""; + metaData[0] = "ERC20 LST token"; _addWhitelistTokens(clientChainId, whitelistTokens, decimals, tvlLimits, names, metaData); _prepareInputs(2); whitelistTokens[0] = bytes32(bytes20(address(restakeToken))); tvlLimits[0] = 1e8 ether; tvlLimits[1] = 1e8 ether; + names[0] = "LST-1"; + names[1] = "LST-2"; + metaData[0] = "LST token"; + metaData[1] = "LST token"; vm.startPrank(exocoreValidatorSet.addr); vm.expectRevert("ExocoreGateway: token cannot be zero address"); @@ -430,7 +438,7 @@ contract UpdateWhitelistTokens is SetUp { decimals[0] = 18; tvlLimits[0] = 1e8 ether; names[0] = "RestakeToken"; - metaData[0] = ""; + metaData[0] = "ERC20 LST token"; _addWhitelistTokens(clientChainId, whitelistTokens, decimals, tvlLimits, names, metaData); tvlLimits[0] = 0; @@ -446,7 +454,7 @@ contract UpdateWhitelistTokens is SetUp { decimals[0] = 18; tvlLimits[0] = 1e10 ether; names[0] = "RestakeToken"; - metaData[0] = ""; + metaData[0] = "ERC20 LST token"; vm.startPrank(exocoreValidatorSet.addr); vm.expectRevert("ExocoreGateway: token has not been added to whitelist before"); @@ -459,7 +467,7 @@ contract UpdateWhitelistTokens is SetUp { decimals[0] = 18; tvlLimits[0] = 1e8 ether; names[0] = "RestakeToken"; - metaData[0] = ""; + metaData[0] = "ERC20 LST token"; // add token to whitelist first _addWhitelistTokens(clientChainId, whitelistTokens, decimals, tvlLimits, names, metaData);