-
Notifications
You must be signed in to change notification settings - Fork 62
/
IBCChannelPacketSendRecv.sol
369 lines (345 loc) · 16.8 KB
/
IBCChannelPacketSendRecv.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.20;
import {Height} from "../../proto/Client.sol";
import {ConnectionEnd} from "../../proto/Connection.sol";
import {Channel} from "../../proto/Channel.sol";
import {ILightClient} from "../02-client/ILightClient.sol";
import {IIBCClientErrors} from "../02-client/IIBCClientErrors.sol";
import {IBCHeight} from "../02-client/IBCHeight.sol";
import {IBCCommitment} from "../24-host/IBCCommitment.sol";
import {IBCModuleManager} from "../26-router/IBCModuleManager.sol";
import {IBCChannelLib} from "./IBCChannelLib.sol";
import {IIBCChannelPacketSendRecv} from "./IIBCChannel.sol";
import {IIBCChannelErrors} from "./IIBCChannelErrors.sol";
/**
* @dev IBCChannelPacketSendRecv is a contract that implements [ICS-4](https://github.com/cosmos/ibc/tree/main/spec/core/ics-004-channel-and-packet-semantics).
*/
contract IBCChannelPacketSendRecv is
IBCModuleManager,
IIBCChannelPacketSendRecv,
IIBCChannelErrors,
IIBCClientErrors
{
using IBCHeight for Height.Data;
// --------- IIBCChannelPacketSendRecv Implementation --------- //
/**
* @dev sendPacket is called by a module in order to send an IBC packet on a channel.
* The packet sequence generated for the packet to be sent is returned. An error
* is returned if one occurs. Also, `timeoutTimestamp` is given in nanoseconds since unix epoch.
*/
function sendPacket(
string calldata sourcePort,
string calldata sourceChannel,
Height.Data calldata timeoutHeight,
uint64 timeoutTimestamp,
bytes calldata data
) public override returns (uint64) {
authenticateChannelCapability(sourcePort, sourceChannel);
ChannelStorage storage channelStorage = getChannelStorage()[sourcePort][sourceChannel];
Channel.Data storage channel = channelStorage.channel;
if (channel.state != Channel.State.STATE_OPEN) {
revert IBCChannelUnexpectedChannelState(channel.state);
}
if (timeoutHeight.isZero() && timeoutTimestamp == 0) {
revert IBCChannelZeroPacketTimeout();
}
{
// NOTE: We can assume here that the connection state is OPEN because the channel state is OPEN
ConnectionEnd.Data storage connection = getConnectionStorage()[channel.connection_hops[0]].connection;
(Height.Data memory latestHeight, uint64 latestTimestamp, ILightClient.ClientStatus status) =
ILightClient(getClientStorage()[connection.client_id].clientImpl).getLatestInfo(connection.client_id);
if (status != ILightClient.ClientStatus.Active) {
revert IBCClientNotActiveClient(connection.client_id);
}
if (!timeoutHeight.isZero() && latestHeight.gte(timeoutHeight)) {
revert IBCChannelPastPacketTimeoutHeight(timeoutHeight, latestHeight);
}
if (timeoutTimestamp != 0 && latestTimestamp >= timeoutTimestamp) {
revert IBCChannelPastPacketTimeoutTimestamp(timeoutTimestamp, latestTimestamp);
}
}
uint64 packetSequence = channelStorage.nextSequenceSend;
channelStorage.nextSequenceSend = packetSequence + 1;
getCommitments()[IBCCommitment.packetCommitmentKeyCalldata(sourcePort, sourceChannel, packetSequence)] =
keccak256(
abi.encodePacked(
sha256(
abi.encodePacked(
timeoutTimestamp, timeoutHeight.revision_number, timeoutHeight.revision_height, sha256(data)
)
)
)
);
emit SendPacket(packetSequence, sourcePort, sourceChannel, timeoutHeight, timeoutTimestamp, data);
return packetSequence;
}
/**
* @dev writeAcknowledgement writes the packet execution acknowledgement to the state,
* which will be verified by the counterparty chain using AcknowledgePacket.
*/
function writeAcknowledgement(
string calldata destinationPortId,
string calldata destinationChannel,
uint64 sequence,
bytes calldata acknowledgement
) public override {
authenticateChannelCapability(destinationPortId, destinationChannel);
Channel.Data storage channel = getChannelStorage()[destinationPortId][destinationChannel].channel;
if (channel.state != Channel.State.STATE_OPEN) {
revert IBCChannelUnexpectedChannelState(channel.state);
}
if (acknowledgement.length == 0) {
revert IBCChannelEmptyAcknowledgement();
}
_writeAcknowledgement(destinationPortId, destinationChannel, sequence, acknowledgement);
}
/**
* @dev recvPacket is called by a module in order to receive & process an IBC packet
* sent on the corresponding channel end on the counterparty chain.
*/
function recvPacket(MsgPacketRecv calldata msg_) public override {
ChannelStorage storage channelStorage =
getChannelStorage()[msg_.packet.destinationPort][msg_.packet.destinationChannel];
Channel.Data storage channel = channelStorage.channel;
if (channel.state == Channel.State.STATE_OPEN) {} else if (
channel.state == Channel.State.STATE_FLUSHING || channel.state == Channel.State.STATE_FLUSHCOMPLETE
) {
RecvStartSequence storage rseq = channelStorage.recvStartSequence;
// prevSequence=0 means the channel is not in the process of being upgraded or counterparty has not been upgraded yet
if (rseq.prevSequence != 0) {
if (msg_.packet.sequence >= rseq.sequence) {
revert IBCChannelCannotRecvNextUpgradePacket(msg_.packet.sequence, rseq.sequence);
} else if (msg_.packet.sequence < rseq.prevSequence) {
revert IBCChannelPacketAlreadyProcessInPreviousUpgrade(msg_.packet.sequence, rseq.prevSequence);
}
}
} else {
revert IBCChannelUnexpectedChannelState(channel.state);
}
if (keccak256(bytes(msg_.packet.sourcePort)) != keccak256(bytes(channel.counterparty.port_id))) {
revert IBCChannelUnexpectedPacketSource(msg_.packet.sourcePort, msg_.packet.sourceChannel);
} else if (keccak256(bytes(msg_.packet.sourceChannel)) != keccak256(bytes(channel.counterparty.channel_id))) {
revert IBCChannelUnexpectedPacketSource(msg_.packet.sourcePort, msg_.packet.sourceChannel);
}
if (msg_.packet.timeoutHeight.revision_height != 0 && block.number >= msg_.packet.timeoutHeight.revision_height)
{
revert IBCChannelTimeoutPacketHeight(block.number, msg_.packet.timeoutHeight.revision_height);
}
if (msg_.packet.timeoutTimestamp != 0 && hostTimestamp() >= msg_.packet.timeoutTimestamp) {
revert IBCChannelTimeoutPacketTimestamp(hostTimestamp(), msg_.packet.timeoutTimestamp);
}
verifyPacketCommitment(
getConnectionStorage()[channel.connection_hops[0]].connection,
msg_.proofHeight,
msg_.proof,
IBCCommitment.packetCommitmentPathCalldata(
msg_.packet.sourcePort, msg_.packet.sourceChannel, msg_.packet.sequence
),
sha256(
abi.encodePacked(
msg_.packet.timeoutTimestamp,
msg_.packet.timeoutHeight.revision_number,
msg_.packet.timeoutHeight.revision_height,
sha256(msg_.packet.data)
)
)
);
if (channel.ordering == Channel.Order.ORDER_UNORDERED) {
if (channel.state == Channel.State.STATE_OPEN) {
RecvStartSequence storage rseq = channelStorage.recvStartSequence;
if (msg_.packet.sequence < rseq.sequence) {
revert IBCChannelPacketAlreadyProcessInPreviousUpgrade(msg_.packet.sequence, rseq.sequence);
}
}
bytes32 commitmentKey = IBCCommitment.packetReceiptCommitmentKeyCalldata(
msg_.packet.destinationPort, msg_.packet.destinationChannel, msg_.packet.sequence
);
mapping(bytes32 => bytes32) storage commitments = getCommitments();
if (commitments[commitmentKey] != bytes32(0)) {
revert IBCChannelPacketReceiptAlreadyExists(
msg_.packet.destinationPort, msg_.packet.destinationChannel, msg_.packet.sequence
);
}
commitments[commitmentKey] = IBCChannelLib.PACKET_RECEIPT_SUCCESSFUL_KECCAK256;
} else if (channel.ordering == Channel.Order.ORDER_ORDERED) {
if (channelStorage.nextSequenceRecv != msg_.packet.sequence) {
revert IBCChannelUnexpectedNextSequenceRecv(channelStorage.nextSequenceRecv);
}
channelStorage.nextSequenceRecv++;
getCommitments()[IBCCommitment.nextSequenceRecvCommitmentKeyCalldata(
msg_.packet.destinationPort, msg_.packet.destinationChannel
)] = keccak256(IBCChannelLib.uint64ToBigEndianBytes(channelStorage.nextSequenceRecv));
} else {
revert IBCChannelUnknownChannelOrder(channel.ordering);
}
emit RecvPacket(msg_.packet);
bytes memory acknowledgement = lookupModuleByChannel(
msg_.packet.destinationPort, msg_.packet.destinationChannel
).onRecvPacket(msg_.packet, _msgSender());
if (acknowledgement.length > 0) {
_writeAcknowledgement(
msg_.packet.destinationPort, msg_.packet.destinationChannel, msg_.packet.sequence, acknowledgement
);
}
}
/**
* @dev AcknowledgePacket is called by a module to process the acknowledgement of a
* packet previously sent by the calling module on a channel to a counterparty
* module on the counterparty chain. Its intended usage is within the ante
* handler. AcknowledgePacket will clean up the packet commitment,
* which is no longer necessary since the packet has been received and acted upon.
* It will also increment NextSequenceAck in case of ORDERED channels.
*/
function acknowledgePacket(MsgPacketAcknowledgement calldata msg_) public override {
ChannelStorage storage channelStorage = getChannelStorage()[msg_.packet.sourcePort][msg_.packet.sourceChannel];
Channel.Data storage channel = channelStorage.channel;
if (channel.state != Channel.State.STATE_OPEN) {
if (channel.state != Channel.State.STATE_FLUSHING) {
revert IBCChannelUnexpectedChannelState(channel.state);
}
}
if (keccak256(bytes(msg_.packet.destinationPort)) != keccak256(bytes(channel.counterparty.port_id))) {
revert IBCChannelUnexpectedPacketDestination(msg_.packet.destinationPort, msg_.packet.destinationChannel);
} else if (
keccak256(bytes(msg_.packet.destinationChannel)) != keccak256(bytes(channel.counterparty.channel_id))
) {
revert IBCChannelUnexpectedPacketDestination(msg_.packet.destinationPort, msg_.packet.destinationChannel);
}
bytes32 packetCommitmentKey = IBCCommitment.packetCommitmentKeyCalldata(
msg_.packet.sourcePort, msg_.packet.sourceChannel, msg_.packet.sequence
);
mapping(bytes32 => bytes32) storage commitments = getCommitments();
bytes32 packetCommitment = commitments[packetCommitmentKey];
if (packetCommitment == bytes32(0)) {
revert IBCChannelPacketCommitmentNotFound(
msg_.packet.sourcePort, msg_.packet.sourceChannel, msg_.packet.sequence
);
}
{
bytes32 commitment = keccak256(
abi.encodePacked(
sha256(
abi.encodePacked(
msg_.packet.timeoutTimestamp,
msg_.packet.timeoutHeight.revision_number,
msg_.packet.timeoutHeight.revision_height,
sha256(msg_.packet.data)
)
)
)
);
if (packetCommitment != commitment) {
revert IBCChannelPacketCommitmentMismatch(packetCommitment, commitment);
}
}
verifyPacketAcknowledgement(
// NOTE: We can assume here that the connection state is OPEN because the channel state is OPEN
getConnectionStorage()[channel.connection_hops[0]].connection,
msg_.proofHeight,
msg_.proof,
IBCCommitment.packetAcknowledgementCommitmentPathCalldata(
msg_.packet.destinationPort, msg_.packet.destinationChannel, msg_.packet.sequence
),
sha256(msg_.acknowledgement)
);
if (channel.ordering == Channel.Order.ORDER_ORDERED) {
if (msg_.packet.sequence != channelStorage.nextSequenceAck) {
revert IBCChannelUnexpectedNextSequenceAck(channelStorage.nextSequenceAck);
}
channelStorage.nextSequenceAck++;
} else if (channel.ordering == Channel.Order.ORDER_UNORDERED) {
if (msg_.packet.sequence < channelStorage.ackStartSequence) {
revert IBCChannelAckAlreadyProcessedInPreviousUpgrade(
msg_.packet.sequence, channelStorage.ackStartSequence
);
}
} else {
revert IBCChannelUnknownChannelOrder(channel.ordering);
}
delete commitments[packetCommitmentKey];
emit AcknowledgePacket(msg_.packet, msg_.acknowledgement);
lookupModuleByChannel(msg_.packet.sourcePort, msg_.packet.sourceChannel).onAcknowledgementPacket(
msg_.packet, msg_.acknowledgement, _msgSender()
);
}
// --------- Private Functions --------- //
function _writeAcknowledgement(
string calldata destinationPortId,
string calldata destinationChannel,
uint64 sequence,
bytes memory acknowledgement
) private {
bytes32 ackCommitmentKey =
IBCCommitment.packetAcknowledgementCommitmentKeyCalldata(destinationPortId, destinationChannel, sequence);
if (getCommitments()[ackCommitmentKey] != bytes32(0)) {
revert IBCChannelAcknowledgementAlreadyWritten(destinationPortId, destinationChannel, sequence);
}
getCommitments()[ackCommitmentKey] = keccak256(abi.encodePacked(sha256(acknowledgement)));
emit WriteAcknowledgement(destinationPortId, destinationChannel, sequence, acknowledgement);
}
/**
* @dev calcBlockDelay calculates the block delay based on the expected time per block
*/
function calcBlockDelay(uint64 timeDelay) private view returns (uint64) {
HostStorage storage hostStorage = getHostStorage();
if (timeDelay == 0) {
return 0;
} else if (hostStorage.expectedTimePerBlock == 0) {
return 0;
} else {
return (timeDelay + hostStorage.expectedTimePerBlock - 1) / hostStorage.expectedTimePerBlock;
}
}
function verifyPacketCommitment(
ConnectionEnd.Data storage connection,
Height.Data calldata height,
bytes calldata proof,
bytes memory path,
bytes32 commitment
) private {
// slither-disable-start reentrancy-no-eth
if (
checkAndGetClient(connection.client_id).verifyMembership(
connection.client_id,
height,
connection.delay_period,
calcBlockDelay(connection.delay_period),
proof,
connection.counterparty.prefix.key_prefix,
path,
abi.encodePacked(commitment)
)
) {
// slither-disable-end reentrancy-no-eth
return;
}
revert IBCChannelFailedVerifyPacketCommitment(connection.client_id, path, commitment, proof, height);
}
function verifyPacketAcknowledgement(
ConnectionEnd.Data storage connection,
Height.Data calldata height,
bytes calldata proof,
bytes memory path,
bytes32 acknowledgementCommitment
) private {
// slither-disable-start reentrancy-no-eth
if (
checkAndGetClient(connection.client_id).verifyMembership(
connection.client_id,
height,
connection.delay_period,
calcBlockDelay(connection.delay_period),
proof,
connection.counterparty.prefix.key_prefix,
path,
abi.encodePacked(acknowledgementCommitment)
)
) {
// slither-disable-end reentrancy-no-eth
return;
}
revert IBCChannelFailedVerifyPacketAcknowledgement(
connection.client_id, path, acknowledgementCommitment, proof, height
);
}
}