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

Rename Config to UserConfig #76

Merged
merged 2 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions script/deploy/Deploy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {console2 as console} from "forge-std/console2.sol";
import {Common} from "create3-deploy/script/Common.s.sol";
import {ScriptTools} from "create3-deploy/script/ScriptTools.sol";

import {Config, ORMP} from "../../src/ORMP.sol";
import {ORMP} from "../../src/ORMP.sol";
import {Relayer} from "../../src/eco/Relayer.sol";
import {Oracle} from "../../src/eco/Oracle.sol";

Expand Down Expand Up @@ -121,7 +121,7 @@ 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).defaultConfig();
(address o, address r) = ORMP(ORMP_ADDR).defaultUC();
require(o == ORACLE_ADDR, "!oracle");
require(r == RELAYER_ADDR, "!relayer");

Expand Down
6 changes: 3 additions & 3 deletions src/Channel.sol
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,14 @@ contract Channel is UserConfig {
/// @param proof Message proof of this message.
function _recv(Message calldata message, bytes calldata proof) internal returns (bytes32) {
// get message.to user config.
Config memory uaConfig = getAppConfig(message.to);
UC memory uc = getAppConfig(message.to);
// only the config relayer could relay this message.
require(uaConfig.relayer == msg.sender, "!auth");
require(uc.relayer == msg.sender, "!auth");

// hash the message.
bytes32 msgHash = hash(message);
// verify message by the config oracle.
require(IVerifier(uaConfig.oracle).verifyMessageProof(message.fromChainId, msgHash, proof), "!proof");
require(IVerifier(uc.oracle).verifyMessageProof(message.fromChainId, msgHash, proof), "!proof");

// check destination chain id is correct.
require(LOCAL_CHAINID() == message.toChainId, "!toChainId");
Expand Down
2 changes: 1 addition & 1 deletion src/Common.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct Message {
/// @dev User application custom configuration.
/// @param oracle Oracle contract address.
/// @param relayer Relayer contract address.
struct Config {
struct UC {
address oracle;
address relayer;
}
Expand Down
12 changes: 6 additions & 6 deletions src/ORMP.sol
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ contract ORMP is ReentrancyGuard, Channel {
bytes calldata params
) internal {
// fetch user application's config.
Config memory uaConfig = getAppConfig(ua);
UC memory uc = getAppConfig(ua);
// handle relayer fee
uint256 relayerFee = _handleRelayer(uaConfig.relayer, msgHash, toChainId, ua, gasLimit, encoded, params);
uint256 relayerFee = _handleRelayer(uc.relayer, msgHash, toChainId, ua, gasLimit, encoded, params);
// handle oracle fee
uint256 oracleFee = _handleOracle(uaConfig.oracle, msgHash, toChainId, ua);
uint256 oracleFee = _handleOracle(uc.oracle, msgHash, toChainId, ua);

// refund
if (msg.value > relayerFee + oracleFee) {
Expand All @@ -94,9 +94,9 @@ contract ORMP is ReentrancyGuard, Channel {
view
returns (uint256)
{
Config memory uaConfig = getAppConfig(ua);
uint256 relayerFee = IRelayer(uaConfig.relayer).fee(toChainId, ua, gasLimit, encoded, params);
uint256 oracleFee = IOracle(uaConfig.oracle).fee(toChainId, ua);
UC memory uc = getAppConfig(ua);
uint256 relayerFee = IRelayer(uc.relayer).fee(toChainId, ua, gasLimit, encoded, params);
uint256 oracleFee = IOracle(uc.oracle).fee(toChainId, ua);
return relayerFee + oracleFee;
}

Expand Down
32 changes: 16 additions & 16 deletions src/UserConfig.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ import "./Common.sol";
/// @title UserConfig
/// @notice User config could select their own relayer and oracle.
/// The default configuration is used by default.
/// @dev Only setter could set default config.
/// @dev Only setter could set default user config.
contract UserConfig {
/// @dev Setter address.
address public setter;
/// @dev Default config.
Config public defaultConfig;
/// @dev ua => config.
mapping(address => Config) public appConfig;
/// @dev Default user config.
UC public defaultUC;
/// @dev ua => uc.
mapping(address => UC) public ucOf;

/// @dev Notifies an observer that the default config has updated.
/// @dev Notifies an observer that the default user config has updated.
/// @param oracle Default oracle.
/// @param relayer Default relayer.
event defaultConfigUpdated(address oracle, address relayer);
event DefaultConfigUpdated(address oracle, address relayer);
/// @dev Notifies an observer that the user application config has updated.
/// @param ua User application contract address.
/// @param oracle Oracle which the user application choose.
Expand All @@ -57,36 +57,36 @@ contract UserConfig {
setter = setter_;
}

/// @dev Set default config for all user application.
/// @dev Set default user config for all user application.
/// @notice Only setter could call.
/// @param oracle Default oracle.
/// @param relayer Default relayer.
function setDefaultConfig(address oracle, address relayer) external onlySetter {
defaultConfig = Config(oracle, relayer);
emit defaultConfigUpdated(oracle, relayer);
defaultUC = UC(oracle, relayer);
emit DefaultConfigUpdated(oracle, relayer);
}

/// @notice Set user application config.
/// @param oracle Oracle which user application.
/// @param relayer Relayer which user application choose.
function setAppConfig(address oracle, address relayer) external {
appConfig[msg.sender] = Config(oracle, relayer);
ucOf[msg.sender] = UC(oracle, relayer);
emit AppConfigUpdated(msg.sender, oracle, relayer);
}

/// @dev Fetch user application config.
/// @notice If user application has not configured, then the default config is used.
/// @notice If user application has not configured, then the default user config is used.
/// @param ua User application contract address.
/// @return user application config.
function getAppConfig(address ua) public view returns (Config memory) {
Config memory c = appConfig[ua];
function getAppConfig(address ua) public view returns (UC memory) {
UC memory c = ucOf[ua];

if (c.relayer == address(0x0)) {
c.relayer = defaultConfig.relayer;
c.relayer = defaultUC.relayer;
}

if (c.oracle == address(0x0)) {
c.oracle = defaultConfig.oracle;
c.oracle = defaultUC.oracle;
}

return c;
Expand Down
6 changes: 2 additions & 4 deletions src/interfaces/IORMP.sol
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,12 @@ interface IORMP {
/// @notice If user application has not configured, then the default config is used.
/// @param ua User application contract address.
/// @return user application config.
function getAppConfig(address ua) external view returns (Config memory);
function getAppConfig(address ua) external view returns (UC memory);

/// @notice Set user application config.
/// @param oracle Oracle which user application choose.
/// @param relayer Relayer which user application choose.
function setAppConfig(address oracle, address relayer) external;

function setDefaultConfig(address oracle, address relayer) external;
function defaultConfig() external view returns (Config memory);
function changeSetter(address setter_) external;
function defaultUC() external view returns (UC memory);
}
8 changes: 4 additions & 4 deletions test/UserConfig.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ contract UserConfigTest is Test {

function test_constructorArgs() public {
assertEq(config.setter(), self);
(address relayer, address oracle) = config.defaultConfig();
(address relayer, address oracle) = config.defaultUC();
assertEq(relayer, address(0));
assertEq(oracle, address(0));
}
Expand All @@ -48,7 +48,7 @@ contract UserConfigTest is Test {

function test_setDefaultConfig() public {
config.setDefaultConfig(address(1), address(2));
(address oracle, address relayer) = config.defaultConfig();
(address oracle, address relayer) = config.defaultUC();
assertEq(oracle, address(1));
assertEq(relayer, address(2));
}
Expand All @@ -59,7 +59,7 @@ contract UserConfigTest is Test {
}

function test_setAppConfig() public {
Config memory c = config.getAppConfig(self);
UC memory c = config.getAppConfig(self);
assertEq(c.oracle, address(0));
assertEq(c.relayer, address(0));

Expand All @@ -70,7 +70,7 @@ contract UserConfigTest is Test {
}

function test_getAppConfig() public {
Config memory c = config.getAppConfig(self);
UC memory c = config.getAppConfig(self);
assertEq(c.relayer, address(0));
assertEq(c.oracle, address(0));

Expand Down