Skip to content

Commit

Permalink
GetResponseLeaf
Browse files Browse the repository at this point in the history
  • Loading branch information
seunlanlege committed Aug 6, 2024
1 parent a9a8290 commit a8c4783
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
39 changes: 38 additions & 1 deletion interfaces/Message.sol
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ struct PostResponseLeaf {
uint256 kIndex;
}

// A get response as a leaf in a merkle mountain range tree
struct GetResponseLeaf {
// The response
GetResponse response;
// It's index in the mmr leaves
uint256 index;
// it's k-index
uint256 kIndex;
}

// A merkle mountain range proof.
struct Proof {
// height of the state machine
Expand All @@ -97,7 +107,7 @@ struct GetResponseMessage {
// proof for the responses
Proof proof;
// The responses, contained in the merkle mountain range tree
GetResponse[] responses;
GetResponseLeaf[] responses;
}

struct GetTimeoutMessage {
Expand Down Expand Up @@ -136,6 +146,9 @@ struct PostResponseMessage {
}

library Message {
/**
* @dev Calculates the absolute timeout value for a PostRequest
*/
function timeout(PostRequest memory req) internal pure returns (uint64) {
if (req.timeoutTimestamp == 0) {
return type(uint64).max;
Expand All @@ -144,6 +157,9 @@ library Message {
}
}

/**
* @dev Calculates the absolute timeout value for a GetRequest
*/
function timeout(GetRequest memory req) internal pure returns (uint64) {
if (req.timeoutTimestamp == 0) {
return type(uint64).max;
Expand All @@ -152,6 +168,9 @@ library Message {
}
}

/**
* @dev Calculates the absolute timeout value for a PostResponse
*/
function timeout(PostResponse memory res) internal pure returns (uint64) {
if (res.timeoutTimestamp == 0) {
return type(uint64).max;
Expand All @@ -160,10 +179,16 @@ library Message {
}
}

/**
* @dev Encode the given post request for commitment
*/
function encode(PostRequest memory req) internal pure returns (bytes memory) {
return abi.encodePacked(req.source, req.dest, req.nonce, req.timeoutTimestamp, req.from, req.to, req.body);
}

/**
* @dev Encode the given get request for commitment
*/
function encode(GetRequest memory req) internal pure returns (bytes memory) {
bytes memory keysEncoding = bytes("");
uint256 len = req.keys.length;
Expand All @@ -183,18 +208,30 @@ library Message {
);
}

/**
* @dev Returns the commitment for the given post response
*/
function hash(PostResponse memory res) internal pure returns (bytes32) {
return keccak256(bytes.concat(encode(res.request), abi.encodePacked(res.response, res.timeoutTimestamp)));
}

/**
* @dev Returns the commitment for the given post request
*/
function hash(PostRequest memory req) internal pure returns (bytes32) {
return keccak256(encode(req));
}

/**
* @dev Returns the commitment for the given get request
*/
function hash(GetRequest memory req) internal pure returns (bytes32) {
return keccak256(encode(req));
}

/**
* @dev Returns the commitment for the given get response
*/
function hash(GetResponse memory res) internal pure returns (bytes32) {
bytes memory response = bytes("");
uint256 len = res.values.length;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@polytope-labs/ismp-solidity",
"version": "0.3.7",
"version": "0.3.8",
"description": "Hyperbridge Solidity SDK for the Interoperable state machine protocol",
"author": "Polytope Labs <[email protected]>",
"license": "Apache-2.0",
Expand Down

0 comments on commit a8c4783

Please sign in to comment.