Skip to content

Commit

Permalink
make recv payable
Browse files Browse the repository at this point in the history
  • Loading branch information
hujw77 committed Oct 31, 2023
1 parent 6a066ab commit cd32a23
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ sizes = false
via_ir = false
no_storage_caching = false
no_rpc_rate_limit = false
bytecode_hash = "none"
bytecode_hash = "ipfs"
cbor_metadata = true

fs_permissions = [
Expand Down
6 changes: 5 additions & 1 deletion src/ORMP.sol
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ contract ORMP is ReentrancyGuard, Channel {
/// @return dispatchResult Result of the message dispatch.
function recv(Message calldata message, bytes calldata proof)
external
payable
recvNonReentrant
returns (bool dispatchResult)
{
Expand All @@ -140,7 +141,10 @@ contract ORMP is ReentrancyGuard, Channel {
function _dispatch(Message memory message, bytes32 msgHash) private returns (bool dispatchResult) {
// Deliver the message to user application contract address.
(dispatchResult,) = message.to.excessivelySafeCall(
message.gasLimit, 0, abi.encodePacked(message.encoded, msgHash, message.fromChainId, message.from)
message.gasLimit,
msg.value,
0,
abi.encodePacked(message.encoded, msgHash, message.fromChainId, message.from)
);
}
}
5 changes: 3 additions & 2 deletions src/security/ExcessivelySafeCall.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@ library ExcessivelySafeCall {
/// to memory.
/// @param _target The address to call
/// @param _gas The amount of gas to forward to the remote contract
/// @param _value Value in wei to send to the account
/// @param _maxCopy The maximum number of bytes of returndata to copy
/// to memory.
/// @param _calldata The data to send to the remote contract
/// @return success and returndata, as `.call()`. Returndata is capped to
/// `_maxCopy` bytes.
function excessivelySafeCall(address _target, uint256 _gas, uint16 _maxCopy, bytes memory _calldata)
function excessivelySafeCall(address _target, uint256 _gas, uint256 _value, uint16 _maxCopy, bytes memory _calldata)
internal
returns (bool, bytes memory)
{
Expand All @@ -38,7 +39,7 @@ library ExcessivelySafeCall {
call(
_gas, // gas
_target, // recipient
0, // ether value
_value, // ether value
add(_calldata, 0x20), // inloc
mload(_calldata), // inlen
0, // outloc
Expand Down
12 changes: 7 additions & 5 deletions test/security/ExcessivelySafeCall.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ contract ContractTest is DSTest {
bool _success;
bytes memory _ret;

(_success, _ret) = target.excessivelySafeCall(100_000, 0, abi.encodeWithSelector(CallTarget.one.selector));
(_success, _ret) = target.excessivelySafeCall(100_000, 0, 0, abi.encodeWithSelector(CallTarget.one.selector));
assertEq(t.called(), 1);
}

Expand All @@ -55,13 +55,13 @@ contract ContractTest is DSTest {
bytes memory _ret;

(_success, _ret) = target.excessivelySafeCall(
100_000, _maxCopy, abi.encodeWithSelector(CallTarget.retBytes.selector, uint256(_requested))
100_000, 0, _maxCopy, abi.encodeWithSelector(CallTarget.retBytes.selector, uint256(_requested))
);
assertTrue(_success);
assertEq(_ret.length, _toCopy, "return copied wrong amount");

(_success, _ret) = target.excessivelySafeCall(
100_000, _maxCopy, abi.encodeWithSelector(CallTarget.revBytes.selector, uint256(_requested))
100_000, 0, _maxCopy, abi.encodeWithSelector(CallTarget.revBytes.selector, uint256(_requested))
);
assertTrue(!_success);
assertEq(_ret.length, _toCopy, "revert copied wrong amount");
Expand Down Expand Up @@ -90,12 +90,14 @@ contract ContractTest is DSTest {
bool _success;
bytes memory _ret;

(_success, _ret) = target.excessivelySafeCall(3_000_000, 32, abi.encodeWithSelector(CallTarget.badRet.selector));
(_success, _ret) =
target.excessivelySafeCall(3_000_000, 0, 32, abi.encodeWithSelector(CallTarget.badRet.selector));
assertTrue(_success);
assertEq(returnSize(), 1_000_000, "didn't return all");
assertEq(_ret.length, 32, "revert didn't truncate");

(_success, _ret) = target.excessivelySafeCall(3_000_000, 32, abi.encodeWithSelector(CallTarget.badRev.selector));
(_success, _ret) =
target.excessivelySafeCall(3_000_000, 0, 32, abi.encodeWithSelector(CallTarget.badRev.selector));
assertTrue(!_success);
assertEq(returnSize(), 1_000_000, "didn't return all");
assertEq(_ret.length, 32, "revert didn't truncate");
Expand Down
4 changes: 2 additions & 2 deletions test/user/Application.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ contract ApplicationTest is Test {

function test_recv() public {
(bool dispatchResult,) = address(ua).excessivelySafeCall(
gasleft(), 0, abi.encodePacked(ua.recv.selector, bytes32(uint256(1)), uint256(1), self)
gasleft(), 0, 0, abi.encodePacked(ua.recv.selector, bytes32(uint256(1)), uint256(1), self)
);
assertEq(dispatchResult, true);
}

function testFail_recv() public {
(bool dispatchResult,) = address(ua).excessivelySafeCall(
gasleft(), 0, abi.encodePacked(ua.recv.selector, bytes32(uint256(1)), uint256(1), address(1))
gasleft(), 0, 0, abi.encodePacked(ua.recv.selector, bytes32(uint256(1)), uint256(1), address(1))
);
assertEq(dispatchResult, true);
}
Expand Down

0 comments on commit cd32a23

Please sign in to comment.