@hyperbridge/core 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,228 @@
1
+ // Copyright (C) Polytope Labs Ltd.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ pragma solidity ^0.8.17;
15
+
16
+ import {PostRequest, FrozenStatus} from "../libraries/Message.sol";
17
+
18
+ /**
19
+ * @title DispatchPost
20
+ * @notice Parameters for dispatching a POST request through Hyperbridge
21
+ * @dev Used to send arbitrary data to applications on other chains
22
+ */
23
+ struct DispatchPost {
24
+ /// @notice Destination chain identifier (e.g., "POLKADOT-1000", "EVM-1")
25
+ /// @dev Must be a valid state machine identifier recognized by the protocol
26
+ bytes dest;
27
+ /// @notice Destination application address or identifier
28
+ /// @dev The receiving application on the destination chain
29
+ bytes to;
30
+ /// @notice The request payload
31
+ /// @dev Arbitrary bytes that will be delivered to the destination application
32
+ bytes body;
33
+ /// @notice Timeout duration in seconds from the current timestamp
34
+ /// @dev Request will be considered timed out after this duration
35
+ uint64 timeout;
36
+ /// @notice Fee paid to relayers for delivery & execution
37
+ /// @dev Paid in the fee token specified by IHost.feeToken()
38
+ uint256 fee;
39
+ /// @notice Account responsible for paying the fees
40
+ /// @dev If different from msg.sender, must have approved the Host contract
41
+ address payer;
42
+ }
43
+
44
+ /**
45
+ * @title DispatchGet
46
+ * @notice Parameters for dispatching a GET request to query state on another chain
47
+ * @dev Used to read storage values from other chains at a specific height
48
+ */
49
+ struct DispatchGet {
50
+ /// @notice Destination chain identifier
51
+ /// @dev Must be a valid state machine identifier
52
+ bytes dest;
53
+ /// @notice Block height at which to read the state
54
+ /// @dev Use 0 for latest available state
55
+ uint64 height;
56
+ /// @notice Storage keys to query
57
+ /// @dev Array of storage keys whose values will be retrieved
58
+ bytes[] keys;
59
+ /// @notice Timeout duration in seconds
60
+ /// @dev Query will be considered timed out after this duration
61
+ uint64 timeout;
62
+ /// @notice Fee amount for the query operation
63
+ /// @dev Covers both protocol fees and relayer incentives
64
+ uint256 fee;
65
+ /// @notice Application-specific metadata
66
+ /// @dev Can be used to track the query purpose or pass additional context
67
+ bytes context;
68
+ }
69
+
70
+ /**
71
+ * @title DispatchPostResponse
72
+ * @notice Parameters for dispatching a response to a previously received POST request
73
+ * @dev Used by applications to respond to cross-chain requests
74
+ */
75
+ struct DispatchPostResponse {
76
+ /// @notice The original request being responded to
77
+ /// @dev Must be a valid request that was previously received
78
+ PostRequest request;
79
+ /// @notice Response payload
80
+ /// @dev Data to send back to the requesting application
81
+ bytes response;
82
+ /// @notice Timeout duration in seconds for the response
83
+ /// @dev Response will be considered timed out after this duration
84
+ uint64 timeout;
85
+ /// @notice Fee paid to relayers for delivery & execution
86
+ /// @dev Paid in the fee token specified by IHost.feeToken()
87
+ uint256 fee;
88
+ /// @notice Account responsible for paying the fees
89
+ /// @dev If different from msg.sender, must have approved the Host contract
90
+ address payer;
91
+ }
92
+
93
+ /**
94
+ * @title IDispatcher
95
+ * @author Polytope Labs (hello@polytope.technology)
96
+ * @notice Interface for dispatching cross-chain messages through Hyperbridge
97
+ * @dev This interface provides methods for sending POST requests, GET queries, and responses.
98
+ * All dispatch methods support both native token and fee token payments for relayer fees.
99
+ * The dispatcher handles message routing, fee collection, and timeout management.
100
+ */
101
+ interface IDispatcher {
102
+ /**
103
+ * @return the host state machine id
104
+ */
105
+ function host() external view returns (bytes memory);
106
+
107
+ /**
108
+ * @return the state machine identifier for the connected hyperbridge instance
109
+ */
110
+ function hyperbridge() external view returns (bytes memory);
111
+
112
+ /**
113
+ * @return the `frozen` status
114
+ */
115
+ function frozen() external view returns (FrozenStatus);
116
+
117
+ /**
118
+ * @dev Returns the address for the Uniswap V2 Router implementation used for swaps
119
+ * @return routerAddress - The address to the in-use RouterV02 implementation
120
+ */
121
+ function uniswapV2Router() external view returns (address);
122
+
123
+ /**
124
+ * @dev Returns the nonce immediately available for requests
125
+ * @return the `nonce`
126
+ */
127
+ function nonce() external view returns (uint256);
128
+
129
+ /**
130
+ * @dev Returns the address of the ERC-20 fee token contract configured for this state machine.
131
+ *
132
+ * @notice Hyperbridge collects it's dispatch fees in the provided token denomination. This will typically be in stablecoins.
133
+ *
134
+ * @return feeToken - The ERC20 contract address for fees.
135
+ */
136
+ function feeToken() external view returns (address);
137
+
138
+ /**
139
+ * @dev Returns the address of the per byte fee configured for the destination state machine.
140
+ *
141
+ * @notice Hyperbridge collects it's dispatch fees per every byte of the outgoing message.
142
+ *
143
+ * @param dest - The destination chain for the per byte fee.
144
+ * @return perByteFee - The per byte fee for outgoing messages.
145
+ */
146
+ function perByteFee(bytes memory dest) external view returns (uint256);
147
+
148
+ /**
149
+ * @dev Dispatch a POST request to Hyperbridge
150
+ *
151
+ * @notice Payment for the request can be made with either the native token or the IHost.feeToken.
152
+ * If native tokens are supplied, it will perform a swap under the hood using the local uniswap router.
153
+ * Will revert if enough native tokens are not provided.
154
+ *
155
+ * If no native tokens are provided then it will try to collect payment from the calling contract in
156
+ * the IHost.feeToken.
157
+ *
158
+ * @param request - post request
159
+ * @return commitment - the request commitment
160
+ */
161
+ function dispatch(DispatchPost memory request) external payable returns (bytes32 commitment);
162
+
163
+ /**
164
+ * @dev Dispatch a GET request to Hyperbridge
165
+ *
166
+ * @notice Payment for the request can be made with either the native token or the IHost.feeToken.
167
+ * If native tokens are supplied, it will perform a swap under the hood using the local uniswap router.
168
+ * Will revert if enough native tokens are not provided.
169
+ *
170
+ * If no native tokens are provided then it will try to collect payment from the calling contract in
171
+ * the IHost.feeToken.
172
+ *
173
+ * @param request - get request
174
+ * @return commitment - the request commitment
175
+ */
176
+ function dispatch(DispatchGet memory request) external payable returns (bytes32 commitment);
177
+
178
+ /**
179
+ * @dev Dispatch a POST response to Hyperbridge
180
+ *
181
+ * @notice Payment for the request can be made with either the native token or the IHost.feeToken.
182
+ * If native tokens are supplied, it will perform a swap under the hood using the local uniswap router.
183
+ * Will revert if enough native tokens are not provided.
184
+ *
185
+ * If no native tokens are provided then it will try to collect payment from the calling contract in
186
+ * the IHost.feeToken.
187
+ *
188
+ * @param response - post response
189
+ * @return commitment - the request commitment
190
+ */
191
+ function dispatch(DispatchPostResponse memory response) external payable returns (bytes32 commitment);
192
+
193
+ /**
194
+ * @dev Increase the relayer fee for a previously dispatched request.
195
+ * This is provided for use only on pending requests, such that when they timeout,
196
+ * the user can recover the entire relayer fee.
197
+ *
198
+ * @notice Payment can be made with either the native token or the IHost.feeToken.
199
+ * If native tokens are supplied, it will perform a swap under the hood using the local uniswap router.
200
+ * Will revert if enough native tokens are not provided.
201
+ *
202
+ * If no native tokens are provided then it will try to collect payment from the calling contract in
203
+ * the IHost.feeToken.
204
+ *
205
+ * If called on an already delivered request, these funds will be seen as a donation to the hyperbridge protocol.
206
+ * @param commitment - The request commitment
207
+ * @param amount - The amount provided in `IHost.feeToken()`
208
+ */
209
+ function fundRequest(bytes32 commitment, uint256 amount) external payable;
210
+
211
+ /**
212
+ * @dev Increase the relayer fee for a previously dispatched response.
213
+ * This is provided for use only on pending responses, such that when they timeout,
214
+ * the user can recover the entire relayer fee.
215
+ *
216
+ * @notice Payment can be made with either the native token or the IHost.feeToken.
217
+ * If native tokens are supplied, it will perform a swap under the hood using the local uniswap router.
218
+ * Will revert if enough native tokens are not provided.
219
+ *
220
+ * If no native tokens are provided then it will try to collect payment from the calling contract in
221
+ * the IHost.feeToken.
222
+ *
223
+ * If called on an already delivered response, these funds will be seen as a donation to the hyperbridge protocol.
224
+ * @param commitment - The response commitment
225
+ * @param amount - The amount to be provided in `IHost.feeToken()`
226
+ */
227
+ function fundResponse(bytes32 commitment, uint256 amount) external payable;
228
+ }
@@ -0,0 +1,97 @@
1
+ // Copyright (C) Polytope Labs Ltd.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ pragma solidity ^0.8.17;
15
+
16
+ import {IHost} from "./IHost.sol";
17
+ import {
18
+ PostRequestMessage,
19
+ PostResponseMessage,
20
+ GetResponseMessage,
21
+ PostRequestTimeoutMessage,
22
+ PostResponseTimeoutMessage,
23
+ GetTimeoutMessage
24
+ } from "../libraries/Message.sol";
25
+
26
+ /**
27
+ * @title IHandler
28
+ * @author Polytope Labs (hello@polytope.technology)
29
+ * @notice Interface for handling incoming cross-chain messages and consensus updates
30
+ * @dev The Handler is responsible for verifying and processing all incoming ISMP protocol messages.
31
+ * It validates consensus proofs, message proofs, and ensures messages are delivered to the correct applications.
32
+ * This interface serves as the entry point for all cross-chain communication through Hyperbridge.
33
+ */
34
+ interface IHandler {
35
+ /**
36
+ * @notice Process an incoming consensus update
37
+ * @dev Verifies the consensus proof using the registered IConsensus implementation and updates the trusted state.
38
+ * This enables the protocol to track state changes on remote chains.
39
+ * @param host The Host contract that stores protocol state
40
+ * @param proof Consensus proof data (format depends on the consensus mechanism)
41
+ */
42
+ function handleConsensus(IHost host, bytes memory proof) external;
43
+
44
+ /**
45
+ * @notice Process a batch of incoming POST requests
46
+ * @dev Verifies request proofs, checks for timeouts, validates message delays, and dispatches valid requests to destination apps.
47
+ * Ensures requests haven't expired and come from verified state commitments.
48
+ * @param host The Host contract that stores protocol state
49
+ * @param request Batch of POST requests with their merkle proofs
50
+ */
51
+ function handlePostRequests(IHost host, PostRequestMessage memory request) external;
52
+
53
+ /**
54
+ * @notice Process a batch of incoming POST responses
55
+ * @dev Verifies response proofs, checks for timeouts, validates message delays, and dispatches valid responses to requesting apps.
56
+ * Matches responses to their original requests and ensures they haven't expired.
57
+ * @param host The Host contract that stores protocol state
58
+ * @param response Batch of POST responses with their merkle proofs
59
+ */
60
+ function handlePostResponses(IHost host, PostResponseMessage memory response) external;
61
+
62
+ /**
63
+ * @notice Process a batch of GET responses (state queries)
64
+ * @dev Verifies state proofs, checks for timeouts, and delivers the queried state data to requesting apps.
65
+ * Ensures the state data comes from the requested height and hasn't expired.
66
+ * @param host The Host contract that stores protocol state
67
+ * @param message Batch of GET responses with their state proofs
68
+ */
69
+ function handleGetResponses(IHost host, GetResponseMessage memory message) external;
70
+
71
+ /**
72
+ * @notice Process POST request timeouts
73
+ * @dev Verifies non-membership proofs to confirm requests were not processed before timeout.
74
+ * Notifies source apps about timed-out requests and allows them to handle refunds or retries.
75
+ * @param host The Host contract that stores protocol state
76
+ * @param message Batch of timed-out POST requests with non-membership proofs
77
+ */
78
+ function handlePostRequestTimeouts(IHost host, PostRequestTimeoutMessage memory message) external;
79
+
80
+ /**
81
+ * @notice Process POST response timeouts
82
+ * @dev Verifies non-membership proofs to confirm responses were not processed before timeout.
83
+ * Notifies responding apps about timed-out responses.
84
+ * @param host The Host contract that stores protocol state
85
+ * @param message Batch of timed-out POST responses with non-membership proofs
86
+ */
87
+ function handlePostResponseTimeouts(IHost host, PostResponseTimeoutMessage memory message) external;
88
+
89
+ /**
90
+ * @notice Process GET request timeouts
91
+ * @dev Verifies non-membership proofs to confirm queries were not answered before timeout.
92
+ * Notifies requesting apps about timed-out state queries so they can implement fallback logic.
93
+ * @param host The Host contract that stores protocol state
94
+ * @param message Batch of timed-out GET requests with non-membership proofs
95
+ */
96
+ function handleGetRequestTimeouts(IHost host, GetTimeoutMessage memory message) external;
97
+ }
@@ -0,0 +1,209 @@
1
+ // Copyright (C) Polytope Labs Ltd.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+ pragma solidity ^0.8.17;
15
+
16
+ import {StateCommitment, StateMachineHeight} from "./IConsensus.sol";
17
+ import {IDispatcher} from "./IDispatcher.sol";
18
+ import {PostRequest, PostResponse, GetResponse, GetRequest, FrozenStatus} from "../libraries/Message.sol";
19
+
20
+ // Some metadata about the request fee
21
+ struct FeeMetadata {
22
+ // the relayer fee
23
+ uint256 fee;
24
+ // user who initiated the request
25
+ address sender;
26
+ }
27
+
28
+ struct ResponseReceipt {
29
+ // commitment of the response object
30
+ bytes32 responseCommitment;
31
+ // address of the relayer responsible for this response delivery
32
+ address relayer;
33
+ }
34
+
35
+ /**
36
+ * @title The Host Interface
37
+ * @author Polytope Labs (hello@polytope.technology)
38
+ *
39
+ * @notice The Host interface sits at the core of the interoperable state machine protocol,
40
+ * It which encapsulates the interfaces required for ISMP datagram handlers and modules.
41
+ *
42
+ * @dev The Host provides the necessary storage interface for the ISMP handlers to process
43
+ * ISMP messages, the Dispatcher provides the interfaces applications use for dispatching requests
44
+ * and responses. This host implementation delegates all verification logic to the IHandler contract.
45
+ * It is only responsible for dispatching incoming & outgoing messages as well as managing
46
+ * the state of the ISMP protocol.
47
+ */
48
+ interface IHost is IDispatcher {
49
+ /**
50
+ * @return the host admin
51
+ */
52
+ function admin() external returns (address);
53
+
54
+ /**
55
+ * @return the host timestamp
56
+ */
57
+ function timestamp() external view returns (uint256);
58
+
59
+ /**
60
+ * @dev Returns the fisherman responsible for vetoing the given state machine height.
61
+ * @return the `fisherman` address
62
+ */
63
+ function vetoes(uint256 paraId, uint256 height) external view returns (address);
64
+
65
+ /**
66
+ * @dev Returns the fee required for 3rd party applications to access hyperbridge state commitments.
67
+ * @return the `stateCommitmentFee`
68
+ */
69
+ function stateCommitmentFee() external view returns (uint256);
70
+
71
+ /**
72
+ * @notice Charges the stateCommitmentFee to 3rd party applications.
73
+ * If native tokens are provided, will attempt to swap them for the stateCommitmentFee.
74
+ * If not enough native tokens are supplied, will revert.
75
+ *
76
+ * If no native tokens are provided then it will try to collect payment from the calling contract in
77
+ * the IHost.feeToken.
78
+ *
79
+ * @param height - state machine height
80
+ * @return the state commitment at `height`
81
+ */
82
+ function stateMachineCommitment(StateMachineHeight memory height) external payable returns (StateCommitment memory);
83
+
84
+ /**
85
+ * @param height - state machine height
86
+ * @return the state machine commitment update time at `height`
87
+ */
88
+ function stateMachineCommitmentUpdateTime(StateMachineHeight memory height) external returns (uint256);
89
+
90
+ /**
91
+ * @return the consensus client contract
92
+ */
93
+ function consensusClient() external view returns (address);
94
+
95
+ /**
96
+ * @return the last updated time of the consensus client
97
+ */
98
+ function consensusUpdateTime() external view returns (uint256);
99
+
100
+ /**
101
+ * @return the latest state machine height for the given stateMachineId. If it returns 0, the state machine is unsupported.
102
+ */
103
+ function latestStateMachineHeight(uint256 stateMachineId) external view returns (uint256);
104
+
105
+ /**
106
+ * @return the state of the consensus client
107
+ */
108
+ function consensusState() external view returns (bytes memory);
109
+
110
+ /**
111
+ * @dev Check the response status for a given request.
112
+ * @return `response` status
113
+ */
114
+ function responded(bytes32 commitment) external view returns (bool);
115
+
116
+ /**
117
+ * @param commitment - commitment to the request
118
+ * @return relayer address
119
+ */
120
+ function requestReceipts(bytes32 commitment) external view returns (address);
121
+
122
+ /**
123
+ * @param commitment - commitment to the request of the response
124
+ * @return response receipt
125
+ */
126
+ function responseReceipts(bytes32 commitment) external view returns (ResponseReceipt memory);
127
+
128
+ /**
129
+ * @param commitment - commitment to the request
130
+ * @return existence status of an outgoing request commitment
131
+ */
132
+ function requestCommitments(bytes32 commitment) external view returns (FeeMetadata memory);
133
+
134
+ /**
135
+ * @param commitment - commitment to the response
136
+ * @return existence status of an outgoing response commitment
137
+ */
138
+ function responseCommitments(bytes32 commitment) external view returns (FeeMetadata memory);
139
+
140
+ /**
141
+ * @return the challenge period
142
+ */
143
+ function challengePeriod() external view returns (uint256);
144
+
145
+ /**
146
+ * @return the unstaking period
147
+ */
148
+ function unStakingPeriod() external view returns (uint256);
149
+
150
+ /**
151
+ * @dev set the new frozen state of the host, only the admin or handler can call this.
152
+ * @param newState - the new frozen state
153
+ */
154
+ function setFrozenState(FrozenStatus newState) external;
155
+
156
+ /**
157
+ * @dev Store an encoded consensus state
158
+ * @param state new consensus state
159
+ */
160
+ function storeConsensusState(bytes memory state) external;
161
+
162
+ /**
163
+ * @dev Store the commitment at `state height`
164
+ * @param height state machine height
165
+ * @param commitment state commitment
166
+ */
167
+ function storeStateMachineCommitment(StateMachineHeight memory height, StateCommitment memory commitment) external;
168
+
169
+ /**
170
+ * @dev Delete the state commitment at given state height.
171
+ */
172
+ function deleteStateMachineCommitment(StateMachineHeight memory height, address fisherman) external;
173
+
174
+ /**
175
+ * @dev Dispatch an incoming request to destination app
176
+ * @param request - post request
177
+ */
178
+ function dispatchIncoming(PostRequest memory request, address relayer) external;
179
+
180
+ /**
181
+ * @dev Dispatch an incoming post response to source app
182
+ * @param response - post response
183
+ */
184
+ function dispatchIncoming(PostResponse memory response, address relayer) external;
185
+
186
+ /**
187
+ * @dev Dispatch an incoming get response to source app
188
+ * @param response - get response
189
+ */
190
+ function dispatchIncoming(GetResponse memory response, address relayer) external;
191
+
192
+ /**
193
+ * @dev Dispatch an incoming get timeout to source app
194
+ * @param timeout - timed-out get request
195
+ */
196
+ function dispatchTimeOut(GetRequest memory timeout, FeeMetadata memory meta, bytes32 commitment) external;
197
+
198
+ /**
199
+ * @dev Dispatch an incoming post timeout to source app
200
+ * @param timeout - timed-out post request
201
+ */
202
+ function dispatchTimeOut(PostRequest memory timeout, FeeMetadata memory meta, bytes32 commitment) external;
203
+
204
+ /**
205
+ * @dev Dispatch an incoming post response timeout to source app
206
+ * @param timeout - timed-out post response
207
+ */
208
+ function dispatchTimeOut(PostResponse memory timeout, FeeMetadata memory meta, bytes32 commitment) external;
209
+ }