Skip to content

Commit

Permalink
Merge branch 'main' into echo/ir
Browse files Browse the repository at this point in the history
  • Loading branch information
hujw77 committed Mar 13, 2024
2 parents 7697f08 + e0aeb25 commit b1b6a58
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 36 deletions.
5 changes: 5 additions & 0 deletions src/interfaces/IORMP.sol
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,9 @@ interface IORMP {
function setAppConfig(address oracle, address relayer) external;

function defaultUC() external view returns (UC memory);

/// @dev Check the msg if it is dispatched.
/// @param msgHash Hash of the checked message.
/// @return Return the dispatched result of the checked message.
function dones(bytes32 msgHash) external view returns (bool);
}
55 changes: 55 additions & 0 deletions src/user/AppBase.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// This file is part of Darwinia.
// Copyright (C) 2018-2023 Darwinia Network
// SPDX-License-Identifier: GPL-3.0
//
// Darwinia is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Darwinia is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Darwinia. If not, see <https://www.gnu.org/licenses/>.

pragma solidity ^0.8.17;

import "../interfaces/IORMP.sol";

// https://eips.ethereum.org/EIPS/eip-5164
abstract contract AppBase {
function protocol() public view virtual returns (address);

function _setAppConfig(address oracle, address relayer) internal virtual {
IORMP(protocol()).setAppConfig(oracle, relayer);
}

modifier onlyORMP() {
require(protocol() == msg.sender, "!ormp-recver");
_;
}

function _messageId() internal pure returns (bytes32 _msgDataMessageId) {
require(msg.data.length >= 84, "!messageId");
assembly {
_msgDataMessageId := calldataload(sub(calldatasize(), 84))
}
}

function _fromChainId() internal pure returns (uint256 _msgDataFromChainId) {
require(msg.data.length >= 52, "!fromChainId");
assembly {
_msgDataFromChainId := calldataload(sub(calldatasize(), 52))
}
}

function _xmsgSender() internal pure returns (address payable _from) {
require(msg.data.length >= 20, "!xmsgSender");
assembly {
_from := shr(96, calldataload(sub(calldatasize(), 20)))
}
}
}
42 changes: 7 additions & 35 deletions src/user/Application.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,18 @@
// You should have received a copy of the GNU General Public License
// along with Darwinia. If not, see <https://www.gnu.org/licenses/>.

pragma solidity 0.8.17;
pragma solidity ^0.8.17;

import "../Common.sol";
import "../interfaces/IORMP.sol";
import "./AppBase.sol";

// https://eips.ethereum.org/EIPS/eip-5164
abstract contract Application {
address public immutable TRUSTED_ORMP;
abstract contract Application is AppBase {
address private immutable _ORMP;

constructor(address ormp) {
TRUSTED_ORMP = ormp;
_ORMP = ormp;
}

function _setAppConfig(address oracle, address relayer) internal virtual {
IORMP(TRUSTED_ORMP).setAppConfig(oracle, relayer);
}

modifier onlyORMP() {
require(TRUSTED_ORMP == msg.sender, "!ormp");
_;
}

function _messageId() internal pure returns (bytes32 _msgDataMessageId) {
require(msg.data.length >= 84, "!messageId");
assembly {
_msgDataMessageId := calldataload(sub(calldatasize(), 84))
}
}

function _fromChainId() internal pure returns (uint256 _msgDataFromChainId) {
require(msg.data.length >= 52, "!fromChainId");
assembly {
_msgDataFromChainId := calldataload(sub(calldatasize(), 52))
}
}

function _xmsgSender() internal pure returns (address payable _from) {
require(msg.data.length >= 20, "!xmsgSender");
assembly {
_from := shr(96, calldataload(sub(calldatasize(), 20)))
}
function protocol() public view virtual override returns (address) {
return _ORMP;
}
}
39 changes: 39 additions & 0 deletions src/user/UpgradeableApplication.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// This file is part of Darwinia.
// Copyright (C) 2018-2023 Darwinia Network
// SPDX-License-Identifier: GPL-3.0
//
// Darwinia is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Darwinia is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Darwinia. If not, see <https://www.gnu.org/licenses/>.

pragma solidity ^0.8.17;

import "./AppBase.sol";

abstract contract UpgradeableApplication is AppBase {
address private _ormp;

event SetORMP(address ormp);

constructor(address ormp) {
_ormp = ormp;
}

function protocol() public view virtual override returns (address) {
return _ormp;
}

function _setORMP(address ormp) internal virtual {
_ormp = ormp;
emit SetORMP(ormp);
}
}
44 changes: 44 additions & 0 deletions test/ORMP.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,56 @@ contract ORMPTest is Test, Verifier {
vm.chainId(2);
}

function test_Refunds() public {
uint256 f = ormp.fee(2, self, 0, "", "");
ormp.send{value: f + 5}(2, self, 0, "", address(5), "");
assertEq(address(5).balance, 5);
}

function testFail_sendSameMsg() public {
uint256 f1 = ormp.fee(2, self, 0, "", "");
bytes32 msgHash1 = ormp.send{value: f1}(2, self, 0, "", self, "");

uint256 f2 = ormp.fee(2, self, 0, "", "");
bytes32 msgHash2 = ormp.send{value: f2}(2, self, 0, "", self, "");
vm.chainId(2);

assertEq(msgHash1, msgHash2);
}

function testFail_SendWithZeroNativeFee() public {
ormp.send{value: 0}(2, self, 0, "", address(5), "");
proof = Proof({blockNumber: block.number, messageIndex: ormp.messageCount() - 1, messageProof: ormp.prove()});
vm.chainId(2);
}

function test_recv() public {
perform_send();
bool r = ormp.recv(message, abi.encode(proof));
assertEq(r, false);
}

function testFail_recvTwice() public {
perform_send();
bool r = ormp.recv(message, abi.encode(proof));
assertEq(r, false);
ormp.recv(message, abi.encode(proof));
}

function test_failedMsgDispactedSuccess_PoC() public {
uint256 f = ormp.fee(2, self, 0, "", "");
ormp.send{value: f}(2, self, 0, "", self, "");
proof = Proof({blockNumber: block.number, messageIndex: ormp.messageCount() - 1, messageProof: ormp.prove()});

vm.chainId(2);

bool returnValue = ormp.recv(message, abi.encode(proof));
/// msg delivery failed
assertEq(returnValue, false);
/// but marked dispatched
assertEq(ormp.dones(hash(message)), true);
}

function fee(uint256, address) external pure returns (uint256) {
return 2;
}
Expand Down
2 changes: 1 addition & 1 deletion test/user/Application.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ contract UserApplication is Application {
address xmsgSender = _xmsgSender();
require(msgHash == bytes32(uint256(1)));
require(fromChainid == 1);
require(xmsgSender == TRUSTED_ORMP);
require(xmsgSender == protocol());
}
}

0 comments on commit b1b6a58

Please sign in to comment.