Skip to content

Commit

Permalink
add gasleft check
Browse files Browse the repository at this point in the history
  • Loading branch information
lumtis committed Nov 15, 2024
1 parent 3ede272 commit 6a40f4e
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions v2/contracts/zevm/GatewayZEVM.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ contract GatewayZEVM is
/// @notice Error indicating a zero address was provided.
error ZeroAddress();

/// @notice Error indicating that the contract call is consuming too much gas to be processed.
error GasLimitExceeded();

/// @notice The constant address of the protocol
address public constant PROTOCOL_ADDRESS = 0x735b14BB79463307AAcBED86DAf3322B1e6226aB;
/// @notice The address of the Zeta token.
Expand All @@ -41,6 +44,13 @@ contract GatewayZEVM is
/// @notice Max size of message + revertOptions revert message.
uint256 public constant MAX_MESSAGE_SIZE = 1024;

/// @notice Max gas left for the contract call.
/// @dev This is a safety mechanism to prevent the contract call from consuming too much gas.
/// The gas limit is set to 1 million gas.
/// This value will aproximately allow the same function calls as once the gas limit is enforced in:
/// https://github.com/zeta-chain/node/pull/3106
uint256 public constant MAX_GAS_LIMIT = 1_000_000;

/// @dev Only protocol address allowed modifier.
modifier onlyProtocol() {
if (msg.sender != PROTOCOL_ADDRESS) {
Expand Down Expand Up @@ -344,6 +354,9 @@ contract GatewayZEVM is
{
if (zrc20 == address(0) || target == address(0)) revert ZeroAddress();

// TODO: remove after the protocol upgrad with this change: https://github.com/zeta-chain/node/pull/3106
if (gasleft() > MAX_GAS_LIMIT) revert GasLimitExceeded();

UniversalContract(target).onCall(context, zrc20, amount, message);
}

Expand All @@ -369,6 +382,9 @@ contract GatewayZEVM is
if (amount == 0) revert InsufficientZRC20Amount();
if (target == PROTOCOL_ADDRESS || target == address(this)) revert InvalidTarget();

// TODO: remove after the protocol upgrad with this change: https://github.com/zeta-chain/node/pull/3106
if (gasleft() > MAX_GAS_LIMIT) revert GasLimitExceeded();

if (!IZRC20(zrc20).deposit(target, amount)) revert ZRC20DepositFailed();
UniversalContract(target).onCall(context, zrc20, amount, message);
}
Expand All @@ -393,6 +409,9 @@ contract GatewayZEVM is
if (amount == 0) revert InsufficientZetaAmount();
if (target == PROTOCOL_ADDRESS || target == address(this)) revert InvalidTarget();

// TODO: remove after the protocol upgrad with this change: https://github.com/zeta-chain/node/pull/3106
if (gasleft() > MAX_GAS_LIMIT) revert GasLimitExceeded();

_transferZETA(amount, target);
UniversalContract(target).onCall(context, zetaToken, amount, message);
}
Expand All @@ -411,6 +430,9 @@ contract GatewayZEVM is
{
if (target == address(0)) revert ZeroAddress();

// TODO: remove after the protocol upgrad with this change: https://github.com/zeta-chain/node/pull/3106
if (gasleft() > MAX_GAS_LIMIT) revert GasLimitExceeded();

Revertable(target).onRevert(revertContext);
}

Expand All @@ -434,6 +456,9 @@ contract GatewayZEVM is
if (amount == 0) revert InsufficientZRC20Amount();
if (target == PROTOCOL_ADDRESS || target == address(this)) revert InvalidTarget();

// TODO: remove after the protocol upgrad with this change: https://github.com/zeta-chain/node/pull/3106
if (gasleft() > MAX_GAS_LIMIT) revert GasLimitExceeded();

if (!IZRC20(zrc20).deposit(target, amount)) revert ZRC20DepositFailed();
Revertable(target).onRevert(revertContext);
}
Expand Down

0 comments on commit 6a40f4e

Please sign in to comment.