Skip to content

Commit

Permalink
introduce support for PostResponse timeouts (#13)
Browse files Browse the repository at this point in the history
* introduce support for PostResponse timeouts

* clean up interfaces
  • Loading branch information
seunlanlege authored Jan 12, 2024
1 parent 034c940 commit 0eaa2c0
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 62 deletions.
2 changes: 0 additions & 2 deletions src/IConsensusClient.sol
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
22 changes: 18 additions & 4 deletions src/IHandler.sol
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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;
}
84 changes: 39 additions & 45 deletions src/IIsmp.sol
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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;
}

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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
)
);
}
Expand Down Expand Up @@ -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);
}
}
12 changes: 9 additions & 3 deletions src/IIsmpHost.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
20 changes: 13 additions & 7 deletions src/IIsmpModule.sol
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/StateMachine.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 "openzeppelin/utils/Strings.sol";
import {Strings} from "openzeppelin/utils/Strings.sol";

library StateMachine {
/// The identifier for the relay chain.
Expand Down

0 comments on commit 0eaa2c0

Please sign in to comment.