Skip to content

Commit

Permalink
Check relayer provide correct gas (#169)
Browse files Browse the repository at this point in the history
* fix tests

* fmt

* fix #166

* test

* unlink
  • Loading branch information
hujw77 authored May 22, 2024
1 parent 802853f commit 34f2c61
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/ORMP.sol
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ contract ORMP is ReentrancyGuard, Channel {

/// @dev Dispatch the cross chain message.
function _dispatch(Message memory message, bytes32 msgHash) private returns (bool dispatchResult) {
// where 5000 is the gas required for the operation between the call to gasleft()
uint256 gasAvailable = gasleft() - 5000;
require(gasAvailable - gasAvailable / 64 > message.gasLimit, "!gas");
// Deliver the message to user application contract address.
(dispatchResult,) = message.to.excessivelySafeCall(
message.gasLimit,
Expand Down
93 changes: 93 additions & 0 deletions test/EIP150.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;

import "forge-std/Test.sol";
import "../src/ORMP.sol";
import "../src/Verifier.sol";

contract EIP150Test is Test, Verifier {
ORMP ormp;
Message m;
address immutable self = address(this);

receive() external payable {}

function setUp() public {
vm.chainId(1);
ormp = new ORMP(self);
ormp.setDefaultConfig(self, self);
}

function loop() public pure {
while (true) {}
}

function test_couldNotCallContractFallbackFunc() public {
m = Message({
channel: address(ormp),
index: 0,
fromChainId: 1,
from: self,
toChainId: 2,
to: self,
gasLimit: 0,
encoded: ""
});
uint256 f = ormp.fee(m.toChainId, m.from, m.gasLimit, m.encoded, "");
ormp.send{value: f}(m.toChainId, m.to, m.gasLimit, m.encoded, self, "");
vm.chainId(m.toChainId);

bool r = ormp.recv{gas: 50000}(m, "");
assertEq(r, false);
}

function testFail_eip150() public {
m = Message({
channel: address(ormp),
index: 0,
fromChainId: 1,
from: self,
toChainId: 2,
to: self,
gasLimit: 300000,
encoded: abi.encodeWithSelector(this.loop.selector)
});
uint256 f = ormp.fee(m.toChainId, m.from, m.gasLimit, m.encoded, "");
ormp.send{value: f}(m.toChainId, m.to, m.gasLimit, m.encoded, self, "");
vm.chainId(m.toChainId);

bool r = ormp.recv{gas: 290000}(m, "");
assertEq(r, false);
}

function test_eip150() public {
m = Message({
channel: address(ormp),
index: 0,
fromChainId: 1,
from: self,
toChainId: 2,
to: self,
gasLimit: 100,
encoded: abi.encodeWithSelector(this.loop.selector)
});
uint256 f = ormp.fee(m.toChainId, m.from, m.gasLimit, m.encoded, "");
ormp.send{value: f}(m.toChainId, m.to, m.gasLimit, m.encoded, self, "");
vm.chainId(m.toChainId);

bool r = ormp.recv{gas: 50000}(m, "");
assertEq(r, false);
}

function fee(uint256, address) external pure returns (uint256) {
return 2;
}

function fee(uint256, address, uint256, bytes calldata, bytes calldata) external pure returns (uint256) {
return 1;
}

function hashOf(uint256, address, uint256) public view override returns (bytes32) {
return hash(m);
}
}

0 comments on commit 34f2c61

Please sign in to comment.