diff --git a/src/IConsensusClient.sol b/src/IConsensusClient.sol index f316cf5..1d1ebfe 100644 --- a/src/IConsensusClient.sol +++ b/src/IConsensusClient.sol @@ -1,8 +1,6 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity 0.8.17; -import "solidity-merkle-trees/MerklePatricia.sol"; - // The state commiment identifies a commiment to some intermediate state in the state machine. // This contains some metadata about the state machine like it's own timestamp at the time of this commitment. struct StateCommitment { diff --git a/src/IHandler.sol b/src/IHandler.sol index 8356dac..76f0fdf 100644 --- a/src/IHandler.sol +++ b/src/IHandler.sol @@ -1,8 +1,15 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity 0.8.17; -import "./IIsmpHost.sol"; -import "./IIsmp.sol"; +import {IIsmpHost} from "./IIsmpHost.sol"; +import { + PostRequestMessage, + PostResponseMessage, + GetResponseMessage, + PostRequestTimeoutMessage, + PostResponseTimeoutMessage, + GetTimeoutMessage +} from "./IIsmp.sol"; /* The IHandler interface serves as the entry point for ISMP datagrams, i.e consensus, requests & response messages. @@ -43,12 +50,19 @@ interface IHandler { * @param host - Ismp host * @param message - batch post request timeouts */ - function handlePostTimeouts(IIsmpHost host, PostTimeoutMessage memory message) external; + function handlePostRequestTimeouts(IIsmpHost host, PostRequestTimeoutMessage memory message) external; + + /** + * @dev check timeout proofs then dispatch to modules + * @param host - Ismp host + * @param message - batch post response timeouts + */ + function handlePostResponseTimeouts(IIsmpHost host, PostResponseTimeoutMessage memory message) external; /** * @dev dispatch to modules * @param host - Ismp host * @param message - batch get request timeouts */ - function handleGetTimeouts(IIsmpHost host, GetTimeoutMessage memory message) external; + function handleGetRequestTimeouts(IIsmpHost host, GetTimeoutMessage memory message) external; } diff --git a/src/IIsmp.sol b/src/IIsmp.sol index 0581e39..97ebf91 100644 --- a/src/IIsmp.sol +++ b/src/IIsmp.sol @@ -1,8 +1,7 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity 0.8.17; -import "solidity-merkle-trees/MerklePatricia.sol"; - +import {StorageValue} from "solidity-merkle-trees/MerklePatricia.sol"; import {StateMachineHeight} from "./IConsensusClient.sol"; struct PostRequest { @@ -39,7 +38,7 @@ struct GetRequest { bytes[] keys; // height at which to read destination state machine uint64 height; - // gas limit for executing this request on destination & its response (if any) on the source. + // gas limit for executing this request on destination uint64 gaslimit; } @@ -55,6 +54,10 @@ struct PostResponse { PostRequest request; // bytes for post response bytes response; + // timestamp by which this response times out. + uint64 timeoutTimestamp; + // gas limit for executing this response on destination which is the source of the request. + uint64 gaslimit; } // A post request as a leaf in a merkle tree @@ -114,7 +117,7 @@ struct PostTimeout { PostRequest request; } -struct PostTimeoutMessage { +struct PostRequestTimeoutMessage { // requests which have timed-out PostRequest[] timeouts; // the height of the state machine proof @@ -123,6 +126,15 @@ struct PostTimeoutMessage { bytes[] proof; } +struct PostResponseTimeoutMessage { + // responses which have timed-out + PostResponse[] timeouts; + // the height of the state machine proof + StateMachineHeight height; + // non-membership proof of the requests + bytes[] proof; +} + // A message for handling incoming responses struct PostResponseMessage { // proof for the responses @@ -143,6 +155,8 @@ struct DispatchPost { 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 amount; } // An object for dispatching get requests to the IsmpDispatcher @@ -157,6 +171,21 @@ struct DispatchGet { 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 amount; +} + +struct DispatchPostResponse { + // The request that initiated this response + PostRequest request; + // bytes for post response + bytes response; + // timestamp by which this response times out. + uint64 timeoutTimestamp; + // 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 amount; } // The core ISMP API, IIsmpModules use this interface to send outgoing get/post requests & responses @@ -167,38 +196,17 @@ interface IIsmp { */ function dispatch(DispatchPost memory request) external; - /** - * @dev Dispatch a POST request to the ISMP router and pay for a relayer. - * The amount provided is charged to tx.origin in $DAI. - * @param request - post request - */ - function dispatch(DispatchPost memory request, uint256 amount) external; - /** * @dev Dispatch a GET request to the ISMP router. * @param request - get request */ function dispatch(DispatchGet memory request) external; - /** - * @dev Dispatch a GET request to the ISMP router and pay for a relayer. - * The amount provided is charged to tx.origin in $DAI. - * @param request - get request - */ - function dispatch(DispatchGet memory request, uint256 amount) external; - /** * @dev Provide a response to a previously received request. * @param response - post response */ function dispatch(PostResponse memory response) external; - - /** - * @dev Provide a response to a previously received request. - * The amount provided is charged to tx.origin in $DAI. - * @param response - post response - */ - function dispatch(PostResponse memory response, uint256 amount) external; } library Message { @@ -209,10 +217,13 @@ library Message { res.request.dest, res.request.nonce, res.request.timeoutTimestamp, - res.request.body, res.request.from, res.request.to, - res.response + res.request.body, + res.request.gaslimit, + res.response, + res.timeoutTimestamp, + res.gaslimit ) ); } @@ -240,23 +251,6 @@ library Message { } function hash(GetResponse memory res) internal pure returns (bytes32) { - bytes memory keysEncoding = abi.encode(res.request.keys); - bytes memory preimage = abi.encodePacked( - res.request.source, - res.request.dest, - res.request.nonce, - res.request.height, - res.request.timeoutTimestamp, - res.request.from, - keysEncoding, - res.request.gaslimit - ); - uint256 len = res.values.length; - for (uint256 i = 0; i < len; i++) { - StorageValue memory entry = res.values[i]; - preimage = bytes.concat(preimage, abi.encodePacked(entry.key, entry.value)); - } - - return keccak256(preimage); + return hash(res.request); } } diff --git a/src/IIsmpHost.sol b/src/IIsmpHost.sol index f6d24be..081c97c 100644 --- a/src/IIsmpHost.sol +++ b/src/IIsmpHost.sol @@ -158,13 +158,19 @@ interface IIsmpHost is IIsmp { /** * @dev Dispatch an incoming get timeout to source module - * @param timeout - get timeout + * @param timeout - timed-out get request */ function dispatchIncoming(GetRequest memory timeout, RequestMetadata memory meta, bytes32 commitment) external; /** * @dev Dispatch an incoming post timeout to source module - * @param timeout - post timeout + * @param timeout - timed-out post request */ - function dispatchIncoming(PostTimeout memory timeout, RequestMetadata memory meta, bytes32 commitment) external; + function dispatchIncoming(PostRequest memory timeout, RequestMetadata memory meta, bytes32 commitment) external; + + /** + * @dev Dispatch an incoming post response timeout to source module + * @param timeout - timed-out post response + */ + function dispatchIncoming(PostResponse memory timeout, RequestMetadata memory meta, bytes32 commitment) external; } diff --git a/src/IIsmpModule.sol b/src/IIsmpModule.sol index d1d8383..5c231b0 100644 --- a/src/IIsmpModule.sol +++ b/src/IIsmpModule.sol @@ -1,35 +1,41 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity 0.8.17; -import "./IIsmp.sol"; +import {PostRequest, PostResponse, GetResponse, GetRequest} from "./IIsmp.sol"; interface IIsmpModule { /** - * @dev Called by the local ISMP router on a module, to notify module of a new request the module may choose to respond immediately, or in a later block + * @dev Called by the IsmpHost to notify a module of a new request the module may choose to respond immediately, or in a later block * @param request post request */ function onAccept(PostRequest memory request) external; /** - * @dev Called by the router on a module, to notify module of a post response to a previously sent out request + * @dev Called by the IsmpHost to notify a module of a post response to a previously sent out request * @param response post response */ function onPostResponse(PostResponse memory response) external; /** - * @dev Called by the router on a module, to notify module of a get response to a previously sent out request + * @dev Called by the IsmpHost to notify a module of a get response to a previously sent out request * @param response get response */ function onGetResponse(GetResponse memory response) external; /** - * @dev Called by the router on a module, to notify module of post requests that were previously sent but have now timed-out + * @dev Called by the IsmpHost to notify a module of post requests that were previously sent but have now timed-out * @param request post request */ - function onPostTimeout(PostRequest memory request) external; + function onPostRequestTimeout(PostRequest memory request) external; /** - * @dev Called by the router on a module, to notify module of get requests that were previously sent but have now timed-out + * @dev Called by the IsmpHost to notify a module of post requests that were previously sent but have now timed-out + * @param request post request + */ + function onPostResponseTimeout(PostResponse memory request) external; + + /** + * @dev Called by the IsmpHost to notify a module of get requests that were previously sent but have now timed-out * @param request get request */ function onGetTimeout(GetRequest memory request) external; diff --git a/src/StateMachine.sol b/src/StateMachine.sol index 47d9ad1..4755725 100644 --- a/src/StateMachine.sol +++ b/src/StateMachine.sol @@ -1,7 +1,7 @@ // SPDX-License-Identifier: UNLICENSED pragma solidity 0.8.17; -import "openzeppelin/utils/Strings.sol"; +import {Strings} from "openzeppelin/utils/Strings.sol"; library StateMachine { /// The identifier for the relay chain.