Skip to content

Commit

Permalink
API refactor (#16)
Browse files Browse the repository at this point in the history
* some api changes

* add missing files
  • Loading branch information
seunlanlege authored Feb 28, 2024
1 parent 54092bf commit 7ffc17d
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 93 deletions.
71 changes: 71 additions & 0 deletions src/IDispatcher.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.17;

import {StateMachineHeight} from "./IConsensusClient.sol";
import {PostRequest} from "./Message.sol";

// An object for dispatching post requests to the IsmpDispatcher
struct DispatchPost {
// bytes representation of the destination state machine
bytes dest;
// the destination module
bytes to;
// the request body
bytes body;
// timeout for this request in seconds
uint64 timeout;
// gas limit for executing this request on destination & its response (if any) on the source.
uint64 gaslimit;
// the amount put up to be paid to the relayer, this is in $DAI and charged to tx.origin
uint256 fee;
}

// An object for dispatching get requests to the IsmpDispatcher
struct DispatchGet {
// bytes representation of the destination state machine
bytes dest;
// height at which to read the state machine
uint64 height;
// Storage keys to read
bytes[] keys;
// timeout for this request in seconds
uint64 timeout;
// gas limit for executing this request on destination & its response (if any) on the source.
uint64 gaslimit;
// the amount put up to be paid to the relayer, this is in $DAI and charged to tx.origin
uint256 fee;
}

struct DispatchPostResponse {
// The request that initiated this response
PostRequest request;
// bytes for post response
bytes response;
// timeout for this response in seconds
uint64 timeout;
// gas limit for executing this response on destination which is the source of the request.
uint64 gaslimit;
// the amount put up to be paid to the relayer, this is in $DAI and charged to tx.origin
uint256 fee;
}

// The core ISMP API, IIsmpModules use this interface to send outgoing get/post requests & responses
interface IDispatcher {
/**
* @dev Dispatch a post request to the ISMP router.
* @param request - post request
*/
function dispatch(DispatchPost memory request) external;

/**
* @dev Dispatch a GET request to the ISMP router.
* @param request - get request
*/
function dispatch(DispatchGet memory request) external;

/**
* @dev Provide a response to a previously received request.
* @param response - post response
*/
function dispatch(DispatchPostResponse memory response) external;
}
2 changes: 1 addition & 1 deletion src/IHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
PostRequestTimeoutMessage,
PostResponseTimeoutMessage,
GetTimeoutMessage
} from "./IIsmp.sol";
} from "./Message.sol";

/*
The IHandler interface serves as the entry point for ISMP datagrams, i.e consensus, requests & response messages.
Expand Down
38 changes: 22 additions & 16 deletions src/IIsmpHost.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
pragma solidity 0.8.17;

import {StateCommitment, StateMachineHeight} from "./IConsensusClient.sol";
import {IIsmp, PostRequest, PostResponse, GetResponse, PostTimeout, GetRequest} from "./IIsmp.sol";
import {IDispatcher} from "./IDispatcher.sol";
import {PostRequest, PostResponse, GetResponse, PostTimeout, GetRequest} from "./Message.sol";

// Some metadata about the request
struct FeeMetadata {
Expand All @@ -19,7 +20,7 @@ struct ResponseReceipt {
address relayer;
}

interface IIsmpHost is IIsmp {
interface IIsmpHost is IDispatcher {
/**
* @return the host admin
*/
Expand All @@ -28,22 +29,27 @@ interface IIsmpHost is IIsmp {
/**
* @return the address of the DAI ERC-20 contract on this state machine
*/
function dai() external returns (address);
function dai() external view returns (address);

/**
* @return the per-byte fee for outgoing requests.
*/
function perByteFee() external view returns (uint256);

/**
* @return the host state machine id
*/
function host() external returns (bytes memory);
function host() external view returns (bytes memory);

/**
* @return the host timestamp
*/
function timestamp() external returns (uint256);
function timestamp() external view returns (uint256);

/**
* @return the `frozen` status
*/
function frozen() external returns (bool);
function frozen() external view returns (bool);

/**
* @param height - state machine height
Expand All @@ -61,56 +67,56 @@ interface IIsmpHost is IIsmp {
* @dev Should return a handle to the consensus client based on the id
* @return the consensus client contract
*/
function consensusClient() external returns (address);
function consensusClient() external view returns (address);

/**
* @return the last updated time of the consensus client
*/
function consensusUpdateTime() external returns (uint256);
function consensusUpdateTime() external view returns (uint256);

/**
* @return the latest state machine height
*/
function latestStateMachineHeight() external returns (uint256);
function latestStateMachineHeight() external view returns (uint256);

/**
* @return the state of the consensus client
*/
function consensusState() external returns (bytes memory);
function consensusState() external view returns (bytes memory);

/**
* @param commitment - commitment to the request
* @return relayer address
*/
function requestReceipts(bytes32 commitment) external returns (address);
function requestReceipts(bytes32 commitment) external view returns (address);

/**
* @param commitment - commitment to the request of the response
* @return response receipt
*/
function responseReceipts(bytes32 commitment) external returns (ResponseReceipt memory);
function responseReceipts(bytes32 commitment) external view returns (ResponseReceipt memory);

/**
* @param commitment - commitment to the request
* @return existence status of an outgoing request commitment
*/
function requestCommitments(bytes32 commitment) external returns (FeeMetadata memory);
function requestCommitments(bytes32 commitment) external view returns (FeeMetadata memory);

/**
* @param commitment - commitment to the response
* @return existence status of an outgoing response commitment
*/
function responseCommitments(bytes32 commitment) external returns (FeeMetadata memory);
function responseCommitments(bytes32 commitment) external view returns (FeeMetadata memory);

/**
* @return the challenge period
*/
function challengePeriod() external returns (uint256);
function challengePeriod() external view returns (uint256);

/**
* @return the unstaking period
*/
function unStakingPeriod() external returns (uint256);
function unStakingPeriod() external view returns (uint256);

/**
* @dev Store an encoded consensus state
Expand Down
29 changes: 28 additions & 1 deletion src/IIsmpModule.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.17;

import {PostRequest, PostResponse, GetResponse, GetRequest} from "./IIsmp.sol";
import {PostRequest, PostResponse, GetResponse, GetRequest} from "./Message.sol";

interface IIsmpModule {
/**
Expand Down Expand Up @@ -40,3 +40,30 @@ interface IIsmpModule {
*/
function onGetTimeout(GetRequest memory request) external;
}

/// Abstract contract to make implementing `IIsmpModule` easier.
abstract contract BaseIsmpModule is IIsmpModule {
function onAccept(PostRequest calldata) external virtual {
revert("IsmpModule doesn't expect Post requests");
}

function onPostRequestTimeout(PostRequest memory) external virtual {
revert("IsmpModule doesn't emit Post requests");
}

function onPostResponse(PostResponse memory) external virtual {
revert("IsmpModule doesn't emit Post responses");
}

function onPostResponseTimeout(PostResponse memory) external virtual {
revert("IsmpModule doesn't emit Post responses");
}

function onGetResponse(GetResponse memory) external virtual {
revert("IsmpModule doesn't emit Get requests");
}

function onGetTimeout(GetRequest memory) external virtual {
revert("IsmpModule doesn't emit Get requests");
}
}
68 changes: 1 addition & 67 deletions src/IIsmp.sol → src/Message.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.17;

import {StorageValue} from "solidity-merkle-trees/MerklePatricia.sol";
import {StateMachineHeight} from "./IConsensusClient.sol";
import {StorageValue} from "solidity-merkle-trees/MerklePatricia.sol";

struct PostRequest {
// the source state machine of this request
Expand Down Expand Up @@ -143,72 +143,6 @@ struct PostResponseMessage {
PostResponseLeaf[] responses;
}

// An object for dispatching post requests to the IsmpDispatcher
struct DispatchPost {
// bytes representation of the destination state machine
bytes dest;
// the destination module
bytes to;
// the request body
bytes body;
// timeout for this request in seconds
uint64 timeout;
// gas limit for executing this request on destination & its response (if any) on the source.
uint64 gaslimit;
// the amount put up to be paid to the relayer, this is in $DAI and charged to tx.origin
uint256 fee;
}

// An object for dispatching get requests to the IsmpDispatcher
struct DispatchGet {
// bytes representation of the destination state machine
bytes dest;
// height at which to read the state machine
uint64 height;
// Storage keys to read
bytes[] keys;
// timeout for this request in seconds
uint64 timeout;
// gas limit for executing this request on destination & its response (if any) on the source.
uint64 gaslimit;
// the amount put up to be paid to the relayer, this is in $DAI and charged to tx.origin
uint256 fee;
}

struct DispatchPostResponse {
// The request that initiated this response
PostRequest request;
// bytes for post response
bytes response;
// timeout for this response in seconds
uint64 timeout;
// gas limit for executing this response on destination which is the source of the request.
uint64 gaslimit;
// the amount put up to be paid to the relayer, this is in $DAI and charged to tx.origin
uint256 fee;
}

// The core ISMP API, IIsmpModules use this interface to send outgoing get/post requests & responses
interface IIsmp {
/**
* @dev Dispatch a post request to the ISMP router.
* @param request - post request
*/
function dispatch(DispatchPost memory request) external;

/**
* @dev Dispatch a GET request to the ISMP router.
* @param request - get request
*/
function dispatch(DispatchGet memory request) external;

/**
* @dev Provide a response to a previously received request.
* @param response - post response
*/
function dispatch(DispatchPostResponse memory response) external;
}

library Message {
function timeout(PostRequest memory req) internal pure returns (uint64) {
if (req.timeoutTimestamp == 0) {
Expand Down
16 changes: 8 additions & 8 deletions src/StateMachine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,42 @@ library StateMachine {
uint256 public constant RELAY_CHAIN = 0;

// Address a state machine on the polkadot relay chain
function polkadot(uint256 id) public pure returns (bytes memory) {
function polkadot(uint256 id) internal pure returns (bytes memory) {
return bytes(string.concat("POLKADOT-", Strings.toString(id)));
}

// Address a state machine on the kusama relay chain
function kusama(uint256 id) public pure returns (bytes memory) {
function kusama(uint256 id) internal pure returns (bytes memory) {
return bytes(string.concat("KUSAMA-", Strings.toString(id)));
}

// Address the ethereum "execution layer"
function ethereum() public pure returns (bytes memory) {
function ethereum() internal pure returns (bytes memory) {
return bytes("ETHE");
}

// Address the Arbitrum state machine
function arbitrum() public pure returns (bytes memory) {
function arbitrum() internal pure returns (bytes memory) {
return bytes("ARBI");
}

// Address the Optimism state machine
function optimism() public pure returns (bytes memory) {
function optimism() internal pure returns (bytes memory) {
return bytes("OPTI");
}

// Address the Base state machine
function base() public pure returns (bytes memory) {
function base() internal pure returns (bytes memory) {
return bytes("BASE");
}

// Address the Polygon POS state machine
function polygon() public pure returns (bytes memory) {
function polygon() internal pure returns (bytes memory) {
return bytes("POLY");
}

// Address the Binance smart chain state machine
function bsc() public pure returns (bytes memory) {
function bsc() internal pure returns (bytes memory) {
return bytes("BSC");
}
}

0 comments on commit 7ffc17d

Please sign in to comment.