Skip to content

Commit

Permalink
feat: add non-zero/non-empty check for function params
Browse files Browse the repository at this point in the history
  • Loading branch information
adu-web3 committed Jul 3, 2024
1 parent 2ca8cb5 commit 1f65b36
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
17 changes: 11 additions & 6 deletions src/core/ExocoreGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions test/foundry/ExocoreDeployer.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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 --

Expand Down Expand Up @@ -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();
}
Expand Down
20 changes: 14 additions & 6 deletions test/foundry/unit/ExocoreGateway.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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");
Expand All @@ -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;
Expand All @@ -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");
Expand All @@ -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);
Expand Down

0 comments on commit 1f65b36

Please sign in to comment.