Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OracleV2 #109

Merged
merged 9 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions bin/dao.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ c3=$PWD/script/input/c3.json

deployer=$(jq -r ".DEPLOYER" $c3)
dao=$(jq -r ".MSGDAO" $c3)
subapi_dao=$(jq -r ".SUBAPIDAO_ADDR" $c3)
subapi_exe=$(jq -r ".SUBAPIEXECDAO_ADDR" $c3)
ormp=$(jq -r ".ORMP_ADDR" $c3)
oracle=$(jq -r ".ORACLEV2_ADDR" $c3)
relayer=$(jq -r ".RELAYER_ADDR" $c3)
Expand All @@ -25,5 +25,6 @@ set -x
# seth send -F $deployer $relayer "changeOwner(address)" $dao --chain arbitrum
# seth send -F $deployer $relayer "changeOwner(address)" $dao --chain ethereum

seth send -F $deployer $oracle "changeOwner(address)" $subapi_dao --chain crab
seth send -F $deployer $oracle "changeOwner(address)" $subapi_dao --chain sepolia
seth send -F $deployer $oracle "changeOwner(address)" $subapi_exe --chain crab
seth send -F $deployer $oracle "changeOwner(address)" $subapi_exe --chain sepolia
seth send -F $deployer $oracle "changeOwner(address)" $subapi_exe --chain arbitrum-sepolia
2 changes: 1 addition & 1 deletion bin/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -eo pipefail

# forge script script/deploy/Deploy.s.sol:Deploy --chain-id 1 --legacy --broadcast --verify --slow
# forge script script/deploy/Deploy.s.sol:Deploy --chain-id 1 --broadcast --verify --slow --legacy
# forge script script/deploy/Deploy.s.sol:Deploy --chain-id 46 --broadcast --verify --slow
# forge script script/deploy/Deploy.s.sol:Deploy --chain-id 42161 --broadcast --verify --slow --legacy

Expand Down
14 changes: 7 additions & 7 deletions bin/fee.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ set -eo pipefail
set -x
# forge script script/fee/Fee.s.sol:Fee --sig "run(uint256)" 42161 --chain-id 46 --broadcast --slow
# forge script script/fee/Fee.s.sol:Fee --sig "run(uint256)" 1 --chain-id 46 --broadcast --slow
# forge script script/fee/Fee.s.sol:Fee --sig "run(uint256)" 46 --chain-id 42161 --broadcast --slow --legacy
# forge script script/fee/Fee.s.sol:Fee --sig "run(uint256)" 1 --chain-id 42161 --broadcast --slow --legacy
# forge script script/fee/Fee.s.sol:Fee --sig "run(uint256)" 46 --chain-id 1 --broadcast --slow --legacy
# forge script script/fee/Fee.s.sol:Fee --sig "run(uint256)" 42161 --chain-id 1 --broadcast --slow --legacy
# forge script script/fee/Fee.s.sol:Fee --sig "run(uint256)" 46 --chain-id 42161 --broadcast --slow --legacy --skip-simulation
# forge script script/fee/Fee.s.sol:Fee --sig "run(uint256)" 1 --chain-id 42161 --broadcast --slow --legacy --skip-simulation
# forge script script/fee/Fee.s.sol:Fee --sig "run(uint256)" 46 --chain-id 1 --broadcast --slow --legacy
# forge script script/fee/Fee.s.sol:Fee --sig "run(uint256)" 42161 --chain-id 1 --broadcast --slow --legacy
# forge script script/fee/Fee.s.sol:Fee --sig "run(uint256)" 46 --chain-id 44 --broadcast --slow --legacy
# forge script script/fee/Fee.s.sol:Fee --sig "run(uint256)" 44 --chain-id 46 --broadcast --slow --legacy

# forge script script/fee/Fee.s.sol:Fee --sig "run(uint256)" 11155111 --chain-id 44 --broadcast
forge script script/fee/Fee.s.sol:Fee --sig "run(uint256)" 11155111 --chain-id 44 --broadcast
# forge script script/fee/Fee.s.sol:Fee --sig "run(uint256)" 421614 --chain-id 44 --broadcast
# forge script script/fee/Fee.s.sol:Fee --sig "run(uint256)" 421614 --chain-id 11155111 --broadcast
# forge script script/fee/Fee.s.sol:Fee --sig "run(uint256)" 44 --chain-id 11155111 --broadcast
forge script script/fee/Fee.s.sol:Fee --sig "run(uint256)" 44 --chain-id 421614 --broadcast --skip-simulation --legacy
forge script script/fee/Fee.s.sol:Fee --sig "run(uint256)" 11155111 --chain-id 421614 --broadcast --skip-simulation --legacy
# forge script script/fee/Fee.s.sol:Fee --sig "run(uint256)" 44 --chain-id 421614 --broadcast --skip-simulation --legacy
# forge script script/fee/Fee.s.sol:Fee --sig "run(uint256)" 11155111 --chain-id 421614 --broadcast --skip-simulation --legacy
16 changes: 16 additions & 0 deletions bin/upgrade.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash

set -eo pipefail

c3=$PWD/script/input/c3.json

deployer=$(jq -r ".DEPLOYER" $c3)
ormp=$(jq -r ".ORMP_ADDR" $c3)
oracle=$(jq -r ".ORACLEV2_ADDR" $c3)
relayer=$(jq -r ".RELAYER_ADDR" $c3)

set -x

seth send -F $deployer $ormp "setDefaultConfig(address,address)" $oracle $relayer --chain crab
seth send -F $deployer $ormp "setDefaultConfig(address,address)" $oracle $relayer --chain sepolia
seth send -F $deployer $ormp "setDefaultConfig(address,address)" $oracle $relayer --chain arbitrum-sepolia
6 changes: 3 additions & 3 deletions bin/verify.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ verify() {
$path > script/output/$chain_id/$name.v.json)
}

verify $oracle 44 $(cast abi-encode "constructor(address,address)" $deployer $ormp) src/eco/OracleV2.sol:OracleV2
verify $oracle 11155111 $(cast abi-encode "constructor(address,address)" $deployer $ormp) src/eco/OracleV2.sol:OracleV2
verify $oracle 421614 $(cast abi-encode "constructor(address,address)" $deployer $ormp) src/eco/OracleV2.sol:OracleV2
verify $oracle 1 $(cast abi-encode "constructor(address,address)" $deployer $ormp) src/eco/OracleV2.sol:OracleV2
verify $oracle 46 $(cast abi-encode "constructor(address,address)" $deployer $ormp) src/eco/OracleV2.sol:OracleV2
verify $oracle 42161 $(cast abi-encode "constructor(address,address)" $deployer $ormp) src/eco/OracleV2.sol:OracleV2

# verify $ormp 42161 $(cast abi-encode "constructor(address)" $deployer) src/ORMP.sol:ORMP
# verify $ormp 46 $(cast abi-encode "constructor(address)" $deployer) src/ORMP.sol:ORMP
Expand Down
8 changes: 4 additions & 4 deletions script/deploy/Deploy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ contract Deploy is Common {

/// @notice Set the protocol config
function setConfig() public broadcast {
ORMP(ORMP_ADDR).setDefaultConfig(ORACLE_ADDR, RELAYER_ADDR);
(address o, address r) = ORMP(ORMP_ADDR).defaultUC();
require(o == ORACLE_ADDR, "!oracle");
require(r == RELAYER_ADDR, "!relayer");
// ORMP(ORMP_ADDR).setDefaultConfig(ORACLE_ADDR, RELAYER_ADDR);
// (address o, address r) = ORMP(ORMP_ADDR).defaultUC();
// require(o == ORACLE_ADDR, "!oracle");
// require(r == RELAYER_ADDR, "!relayer");

III(ORACLE_ADDR).setApproved(oracleOperator, true);
require(III(ORACLE_ADDR).isApproved(oracleOperator), "!o-operator");
Expand Down
4 changes: 2 additions & 2 deletions script/fee/Fee.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ contract Fee is Common {

function run(uint256 chainId) public {
require(dao == msg.sender, "!dao");
setOracleFee(chainId);
// setRelayerFee(chainId);
// setOracleFee(chainId);
setRelayerFee(chainId);
}

function setOracleFee(uint256 chainId) public broadcast {
Expand Down
2 changes: 1 addition & 1 deletion script/input/1/fee.c.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"fee": 20000000000000
},
"42161": {
"fee": 5000000000000000
"fee": 400000000000000
}
},
"RELAYER": {
Expand Down
2 changes: 1 addition & 1 deletion script/input/42161/fee.c.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"fee": 20000000000000
},
"1": {
"fee": 100000000000000000
"fee": 4000000000000000
}
},
"RELAYER": {
Expand Down
6 changes: 0 additions & 6 deletions script/input/421613/deploy.c.json

This file was deleted.

15 changes: 0 additions & 15 deletions script/input/421613/fee.c.json

This file was deleted.

3 changes: 0 additions & 3 deletions script/input/421613/oracle.c.json

This file was deleted.

6 changes: 0 additions & 6 deletions script/input/43/deploy.c.json

This file was deleted.

15 changes: 0 additions & 15 deletions script/input/43/fee.c.json

This file was deleted.

3 changes: 0 additions & 3 deletions script/input/43/oracle.c.json

This file was deleted.

2 changes: 1 addition & 1 deletion script/input/44/fee.c.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"11155111": {
"dstPriceRatio": 9000000000000000,
"dstGasPriceInWei": 1000000,
"baseGas": 200000,
"baseGas": 100000,
"gasPerByte": 16
}
}
Expand Down
8 changes: 4 additions & 4 deletions script/input/46/fee.c.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
{
"ORACLE": {
"1": {
"fee": 55000000000000000000000
"fee": 4000000000000000000000
},
"44": {
"fee": 1000000000000000000
},
"42161": {
"fee": 2500000000000000000000
"fee": 400000000000000000000
}
},
"RELAYER": {
"1": {
"dstPriceRatio": 9000000000000000,
"dstGasPriceInWei": 50000000000,
"baseGas": 200000,
"dstGasPriceInWei": 40000000000,
"baseGas": 100000,
"gasPerByte": 16
},
"44": {
Expand Down
1 change: 1 addition & 0 deletions script/input/c3.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"DEPLOYER": "0x0f14341A7f464320319025540E8Fe48Ad0fe5aec",
"MSGDAO": "0x000000000879926D12aF396788C0785B7e581e53",
"SUBAPIDAO_ADDR": "0x000000000a0D8ac9cc6CbD817fA77090322FF29d",
"SUBAPIEXECDAO_ADDR": "0x00000000079b20FEd6365B38f299D40236A90119",
"ORMP_ADDR": "0x00000000001523057a05d6293C1e5171eE33eE0A",
"ORMP_SALT": "0x4d629bbdb40d29206f12a51aa81faf14553b218f96845742a89e02e55ecfcef6",
"ORACLEV2_ADDR": "0x0000000005Be70A35b6534bFBd21eC0c98B27b1f",
Expand Down
1 change: 1 addition & 0 deletions script/output/1/OracleV2.v.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion script/output/1/deploy.a-latest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"DAO": "0x0f14341A7f464320319025540E8Fe48Ad0fe5aec",
"ORACLE": "0x00000000046bc530804d66B6b64f7aF69B4E4E81",
"ORACLE": "0x0000000005Be70A35b6534bFBd21eC0c98B27b1f",
"ORMP": "0x00000000001523057a05d6293C1e5171eE33eE0A",
"RELAYER": "0x0000000000808fE9bDCc1d180EfbF5C53552a6b1"
}
1 change: 1 addition & 0 deletions script/output/42161/OracleV2.v.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion script/output/42161/deploy.a-latest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"DAO": "0x0f14341A7f464320319025540E8Fe48Ad0fe5aec",
"ORACLE": "0x00000000046bc530804d66B6b64f7aF69B4E4E81",
"ORACLE": "0x0000000005Be70A35b6534bFBd21eC0c98B27b1f",
"ORMP": "0x00000000001523057a05d6293C1e5171eE33eE0A",
"RELAYER": "0x0000000000808fE9bDCc1d180EfbF5C53552a6b1"
}
1 change: 1 addition & 0 deletions script/output/46/OracleV2.v.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion script/output/46/deploy.a-latest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"DAO": "0x0f14341A7f464320319025540E8Fe48Ad0fe5aec",
"ORACLE": "0x00000000046bc530804d66B6b64f7aF69B4E4E81",
"ORACLE": "0x0000000005Be70A35b6534bFBd21eC0c98B27b1f",
"ORMP": "0x00000000001523057a05d6293C1e5171eE33eE0A",
"RELAYER": "0x0000000000808fE9bDCc1d180EfbF5C53552a6b1"
}
6 changes: 2 additions & 4 deletions src/Verifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,15 @@ import "./imt/IncrementalMerkleTree.sol";

abstract contract Verifier is IVerifier {
/// @notice Message proof.
/// @param blockNumber The block number corresponding to the proof.
/// @param messageIndex Leaf index of the message hash in incremental merkle tree.
/// @param messageProof Merkle proof of the message hash.
struct Proof {
uint256 blockNumber;
uint256 messageIndex;
bytes32[32] messageProof;
}

/// @inheritdoc IVerifier
function merkleRoot(uint256 chainId, uint256 blockNumber) public view virtual returns (bytes32);
function merkleRoot(uint256 chainId, uint256 messageIndex) public view virtual returns (bytes32);

/// @inheritdoc IVerifier
function verifyMessageProof(uint256 fromChainId, bytes32 msgHash, bytes calldata proof)
Expand All @@ -44,7 +42,7 @@ abstract contract Verifier is IVerifier {
Proof memory p = abi.decode(proof, (Proof));

// fetch message root in block number from chain
bytes32 imtRootOracle = merkleRoot(fromChainId, p.blockNumber);
bytes32 imtRootOracle = merkleRoot(fromChainId, p.messageIndex);
// calculate the expected root based on the proof
bytes32 imtRootProof = IncrementalMerkleTree.branchRoot(msgHash, p.messageProof, p.messageIndex);

Expand Down
24 changes: 14 additions & 10 deletions src/eco/OracleV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,21 @@
pragma solidity 0.8.17;

import "../Verifier.sol";
import "../interfaces/IFeedOracle.sol";

contract OracleV2 is Verifier {
event Assigned(bytes32 indexed msgHash, uint256 fee);
event SetFee(uint256 indexed chainId, uint256 fee);
event SetApproved(address operator, bool approve);
event ImportedMessageRoot(uint256 indexed chainId, uint256 indexed blockHeight, bytes32 messageRoot);
event Withdrawal(address indexed to, uint256 amt);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
event ImportedMessageRoot(uint256 indexed chainId, uint256 indexed messageIndex, bytes32 messageRoot);

address public immutable PROTOCOL;

address public owner;
// chainId => price
mapping(uint256 => uint256) public feeOf;
// chainId => blockNumber => messageRoot
// chainId => messageIndex => messageRoot
mapping(uint256 => mapping(uint256 => bytes32)) rootOf;
// operator => isApproved
mapping(address => bool) public approvedOf;
Expand All @@ -53,13 +54,15 @@ contract OracleV2 is Verifier {

receive() external payable {}

function importMessageRoot(uint256 chainId, uint256 blockNumber, bytes32 messageRoot) external onlyOwner {
rootOf[chainId][blockNumber] = messageRoot;
emit ImportedMessageRoot(chainId, blockNumber, messageRoot);
function importMessageRoot(uint256 chainId, uint256 messageIndex, bytes32 messageRoot) external onlyOwner {
rootOf[chainId][messageIndex] = messageRoot;
emit ImportedMessageRoot(chainId, messageIndex, messageRoot);
}

function changeOwner(address owner_) external onlyOwner {
owner = owner_;
function changeOwner(address newOwner) external onlyOwner {
address oldOwner = owner;
owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}

function setApproved(address operator, bool approve) external onlyOwner {
Expand All @@ -74,6 +77,7 @@ contract OracleV2 is Verifier {
function withdraw(address to, uint256 amount) external onlyApproved {
(bool success,) = to.call{value: amount}("");
require(success, "!withdraw");
emit Withdrawal(to, amount);
}

function setFee(uint256 chainId, uint256 fee_) external onlyApproved {
Expand All @@ -92,7 +96,7 @@ contract OracleV2 is Verifier {
emit Assigned(msgHash, msg.value);
}

function merkleRoot(uint256 chainId, uint256 blockNumber) public view override returns (bytes32) {
return rootOf[chainId][blockNumber];
function merkleRoot(uint256 chainId, uint256 messageIndex) public view override returns (bytes32) {
return rootOf[chainId][messageIndex];
}
}
4 changes: 2 additions & 2 deletions src/interfaces/IVerifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ pragma solidity 0.8.17;
interface IVerifier {
/// @notice Fetch message root oracle.
/// @param chainId The destination chain id.
/// @param blockNumber The block number where the message root is located.
/// @param messageIndex Leaf index of the message hash in incremental merkle tree.
/// @return Message root in destination chain.
function merkleRoot(uint256 chainId, uint256 blockNumber) external view returns (bytes32);
function merkleRoot(uint256 chainId, uint256 messageIndex) external view returns (bytes32);

/// @notice Verify message proof
/// @dev Message proof provided by relayer. Oracle should provide message root of
Expand Down
4 changes: 2 additions & 2 deletions test/Channel.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ contract ChannelTest is Test, Verifier {
encoded: ""
});
assertEq(msgHash, hash(message));
Proof memory proof = Proof({blockNumber: block.number, messageIndex: 0, messageProof: channel.prove()});
Proof memory proof = Proof({messageIndex: 0, messageProof: channel.prove()});
vm.chainId(2);
channel.recvMessage(message, abi.encode(proof));
}
Expand All @@ -89,7 +89,7 @@ contract ChannelTest is Test, Verifier {
encoded: ""
});
assertEq(msgHash, hash(message));
Proof memory proof = Proof({blockNumber: block.number, messageIndex: index, messageProof: channel.prove()});
Proof memory proof = Proof({messageIndex: index, messageProof: channel.prove()});
vm.chainId(2);
channel.recvMessage(message, abi.encode(proof));
}
Expand Down
2 changes: 1 addition & 1 deletion test/ORMP.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ contract ORMPTest is Test, Verifier {
function perform_send() 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()});
proof = Proof({messageIndex: ormp.messageCount() - 1, messageProof: ormp.prove()});
vm.chainId(2);
}

Expand Down
2 changes: 1 addition & 1 deletion test/Verifier.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ contract VerifierTest is Test, Verifier {
function test_verifyMessageProof() public {
bytes32 msgHash = bytes32(uint256(1));
imt.insert(msgHash);
Proof memory proof = Proof({blockNumber: block.number, messageIndex: 0, messageProof: zeroHashes});
Proof memory proof = Proof({messageIndex: 0, messageProof: zeroHashes});
bool r = this.verifyMessageProof(1, msgHash, abi.encode(proof));
assertEq(r, true);
}
Expand Down
3 changes: 1 addition & 2 deletions test/bench/ORMP.b.sol
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ contract ORMPBenchmarkTest is Test {

function perform_recv(Message memory message) public {
root = ormp.root();
Verifier.Proof memory proof =
Verifier.Proof({blockNumber: block.number, messageIndex: message.index, messageProof: ormp.prove()});
Verifier.Proof memory proof = Verifier.Proof({messageIndex: message.index, messageProof: ormp.prove()});

vm.createSelectFork(message.toChainId.toChainName());
// TODO: setDefaltOracle
Expand Down
Loading