forked from hyperledger-labs/yui-ibc-solidity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIBCChannelHandshake.sol
283 lines (255 loc) · 11.9 KB
/
IBCChannelHandshake.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.9;
import "@openzeppelin/contracts/utils/Strings.sol";
import "../../proto/Channel.sol";
import "../02-client/IBCHeight.sol";
import "../03-connection/IBCConnection.sol";
import "../04-channel/IIBCChannel.sol";
import "../24-host/IBCStore.sol";
import "../24-host/IBCCommitment.sol";
import "../25-handler/IBCMsgs.sol";
/**
* @dev IBCChannelHandshake is a contract that implements [ICS-4](https://github.com/cosmos/ibc/tree/main/spec/core/ics-004-channel-and-packet-semantics).
*/
contract IBCChannelHandshake is IBCStore, IIBCChannelHandshake {
using IBCHeight for Height.Data;
/**
* @dev channelOpenInit is called by a module to initiate a channel opening handshake with a module on another chain.
*/
function channelOpenInit(IBCMsgs.MsgChannelOpenInit calldata msg_) external returns (string memory) {
require(msg_.channel.connection_hops.length == 1, "connection_hops length must be 1");
ConnectionEnd.Data storage connection = connections[msg_.channel.connection_hops[0]];
require(
connection.versions.length == 1, "single version must be negotiated on connection before opening channel"
);
require(
IBCConnectionLib.verifySupportedFeature(
connection.versions[0], IBCChannelLib.toString(msg_.channel.ordering)
),
"feature not supported"
);
require(msg_.channel.state == Channel.State.STATE_INIT, "channel state must STATE_INIT");
require(bytes(msg_.channel.counterparty.channel_id).length == 0, "counterparty channel_id must be empty");
string memory channelId = generateChannelIdentifier();
initializeSequences(msg_.portId, channelId);
return channelId;
}
/**
* @dev channelOpenTry is called by a module to accept the first step of a channel opening handshake initiated by a module on another chain.
*/
function channelOpenTry(IBCMsgs.MsgChannelOpenTry calldata msg_) external returns (string memory) {
require(msg_.channel.connection_hops.length == 1, "connection_hops length must be 1");
ConnectionEnd.Data storage connection = connections[msg_.channel.connection_hops[0]];
require(connection.state == ConnectionEnd.State.STATE_OPEN, "connection state must be STATE_OPEN");
require(
connection.versions.length == 1, "single version must be negotiated on connection before opening channel"
);
require(
IBCConnectionLib.verifySupportedFeature(
connection.versions[0], IBCChannelLib.toString(msg_.channel.ordering)
),
"feature not supported"
);
require(msg_.channel.state == Channel.State.STATE_TRYOPEN, "channel state must be STATE_TRYOPEN");
Channel.Data memory expectedChannel = Channel.Data({
state: Channel.State.STATE_INIT,
ordering: msg_.channel.ordering,
counterparty: ChannelCounterparty.Data({port_id: msg_.portId, channel_id: ""}),
connection_hops: getCounterpartyHops(msg_.channel.connection_hops[0]),
version: msg_.counterpartyVersion
});
require(
verifyChannelState(
connection,
msg_.proofHeight,
msg_.proofInit,
msg_.channel.counterparty.port_id,
msg_.channel.counterparty.channel_id,
Channel.encode(expectedChannel)
),
"failed to verify channel state"
);
string memory channelId = generateChannelIdentifier();
initializeSequences(msg_.portId, channelId);
return channelId;
}
/**
* @dev channelOpenAck is called by the handshake-originating module to acknowledge the acceptance of the initial request by the counterparty module on the other chain.
*/
function channelOpenAck(IBCMsgs.MsgChannelOpenAck calldata msg_) external {
Channel.Data storage channel = channels[msg_.portId][msg_.channelId];
require(channel.state == Channel.State.STATE_INIT, "channel state should be INIT");
ConnectionEnd.Data storage connection = connections[channel.connection_hops[0]];
require(connection.state == ConnectionEnd.State.STATE_OPEN, "connection state is not OPEN");
require(channel.connection_hops.length == 1);
Channel.Data memory expectedChannel = Channel.Data({
state: Channel.State.STATE_TRYOPEN,
ordering: channel.ordering,
counterparty: ChannelCounterparty.Data({port_id: msg_.portId, channel_id: msg_.channelId}),
connection_hops: getCounterpartyHops(channel.connection_hops[0]),
version: msg_.counterpartyVersion
});
require(
verifyChannelState(
connection,
msg_.proofHeight,
msg_.proofTry,
channel.counterparty.port_id,
msg_.counterpartyChannelId,
Channel.encode(expectedChannel)
),
"failed to verify channel state"
);
channel.state = Channel.State.STATE_OPEN;
channel.version = msg_.counterpartyVersion;
channel.counterparty.channel_id = msg_.counterpartyChannelId;
updateChannelCommitment(msg_.portId, msg_.channelId);
}
/**
* @dev channelOpenConfirm is called by the counterparty module to close their end of the channel, since the other end has been closed.
*/
function channelOpenConfirm(IBCMsgs.MsgChannelOpenConfirm calldata msg_) external {
Channel.Data storage channel = channels[msg_.portId][msg_.channelId];
require(channel.state == Channel.State.STATE_TRYOPEN, "channel state is not TRYOPEN");
ConnectionEnd.Data storage connection = connections[channel.connection_hops[0]];
require(connection.state == ConnectionEnd.State.STATE_OPEN, "connection state is not OPEN");
require(channel.connection_hops.length == 1);
Channel.Data memory expectedChannel = Channel.Data({
state: Channel.State.STATE_OPEN,
ordering: channel.ordering,
counterparty: ChannelCounterparty.Data({port_id: msg_.portId, channel_id: msg_.channelId}),
connection_hops: getCounterpartyHops(channel.connection_hops[0]),
version: channel.version
});
require(
verifyChannelState(
connection,
msg_.proofHeight,
msg_.proofAck,
channel.counterparty.port_id,
channel.counterparty.channel_id,
Channel.encode(expectedChannel)
),
"failed to verify channel state"
);
channel.state = Channel.State.STATE_OPEN;
updateChannelCommitment(msg_.portId, msg_.channelId);
}
/**
* @dev channelCloseInit is called by either module to close their end of the channel. Once closed, channels cannot be reopened.
*/
function channelCloseInit(IBCMsgs.MsgChannelCloseInit calldata msg_) external {
Channel.Data storage channel = channels[msg_.portId][msg_.channelId];
require(channel.state != Channel.State.STATE_CLOSED, "channel state is already CLOSED");
ConnectionEnd.Data storage connection = connections[channel.connection_hops[0]];
require(connection.state == ConnectionEnd.State.STATE_OPEN, "connection state is not OPEN");
channel.state = Channel.State.STATE_CLOSED;
updateChannelCommitment(msg_.portId, msg_.channelId);
}
/**
* @dev channelCloseConfirm is called by the counterparty module to close their end of the
* channel, since the other end has been closed.
*/
function channelCloseConfirm(IBCMsgs.MsgChannelCloseConfirm calldata msg_) external {
Channel.Data storage channel = channels[msg_.portId][msg_.channelId];
require(channel.state != Channel.State.STATE_CLOSED, "channel state is already CLOSED");
require(channel.connection_hops.length == 1);
ConnectionEnd.Data storage connection = connections[channel.connection_hops[0]];
require(connection.state == ConnectionEnd.State.STATE_OPEN, "connection state is not OPEN");
Channel.Data memory expectedChannel = Channel.Data({
state: Channel.State.STATE_CLOSED,
ordering: channel.ordering,
counterparty: ChannelCounterparty.Data({port_id: msg_.portId, channel_id: msg_.channelId}),
connection_hops: getCounterpartyHops(channel.connection_hops[0]),
version: channel.version
});
require(
verifyChannelState(
connection,
msg_.proofHeight,
msg_.proofInit,
channel.counterparty.port_id,
channel.counterparty.channel_id,
Channel.encode(expectedChannel)
),
"failed to verify channel state"
);
channel.state = Channel.State.STATE_CLOSED;
updateChannelCommitment(msg_.portId, msg_.channelId);
}
/**
* @dev writeChannel writes a channel which has successfully passed the OpenInit or OpenTry handshake step.
*/
function writeChannel(
string calldata portId,
string calldata channelId,
Channel.State state,
Channel.Order order,
ChannelCounterparty.Data calldata counterparty,
string[] calldata connectionHops,
string calldata version
) external {
Channel.Data storage channel = channels[portId][channelId];
channel.state = state;
channel.ordering = order;
channel.counterparty = counterparty;
for (uint256 i = 0; i < connectionHops.length; i++) {
channel.connection_hops.push(connectionHops[i]);
}
channel.version = version;
updateChannelCommitment(portId, channelId);
}
function updateChannelCommitment(string memory portId, string memory channelId) private {
commitments[IBCCommitment.channelCommitmentKey(portId, channelId)] =
keccak256(Channel.encode(channels[portId][channelId]));
}
/* Verification functions */
function verifyChannelState(
ConnectionEnd.Data storage connection,
Height.Data calldata height,
bytes calldata proof,
string memory portId,
string memory channelId,
bytes memory channelBytes
) private returns (bool) {
return checkAndGetClient(connection.client_id).verifyMembership(
connection.client_id,
height,
0,
0,
proof,
connection.counterparty.prefix.key_prefix,
IBCCommitment.channelPath(portId, channelId),
channelBytes
);
}
/* Internal functions */
function initializeSequences(string memory portId, string memory channelId) internal {
nextSequenceSends[portId][channelId] = 1;
nextSequenceRecvs[portId][channelId] = 1;
nextSequenceAcks[portId][channelId] = 1;
commitments[IBCCommitment.nextSequenceRecvCommitmentKey(portId, channelId)] =
keccak256(abi.encodePacked((bytes8(uint64(1)))));
}
function getCounterpartyHops(string memory connectionId) internal view returns (string[] memory hops) {
hops = new string[](1);
hops[0] = connections[connectionId].counterparty.connection_id;
return hops;
}
function generateChannelIdentifier() private returns (string memory) {
string memory identifier = string(abi.encodePacked("channel-", Strings.toString(nextChannelSequence)));
nextChannelSequence++;
return identifier;
}
}
library IBCChannelLib {
function toString(Channel.Order order) internal pure returns (string memory) {
if (order == Channel.Order.ORDER_UNORDERED) {
return "ORDER_UNORDERED";
} else if (order == Channel.Order.ORDER_ORDERED) {
return "ORDER_ORDERED";
} else {
revert("unknown order");
}
}
}