Skip to content

Commit 44ae785

Browse files
committed
emit events before callback module
Signed-off-by: Jun Kimura <jun.kimura@datachain.jp>
1 parent 5617641 commit 44ae785

File tree

4 files changed

+82
-81
lines changed

4 files changed

+82
-81
lines changed

.gas-snapshot

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
IBCMockAppTest:testHandshake() (gas: 4420488)
22
IBCMockAppTest:testHandshakeBetweenDifferentPorts() (gas: 3334373)
3-
IBCMockAppTest:testPacketRelay() (gas: 13931365)
3+
IBCMockAppTest:testPacketRelay() (gas: 13935239)
44
IBCMockAppTest:testPacketTimeout() (gas: 4279263)
55
IBCTest:testBenchmarkCreateMockClient() (gas: 233366)
66
IBCTest:testBenchmarkLCUpdateMockClient() (gas: 62005)
7-
IBCTest:testBenchmarkRecvPacket() (gas: 158899)
7+
IBCTest:testBenchmarkRecvPacket() (gas: 158870)
88
IBCTest:testBenchmarkSendPacket() (gas: 128432)
99
IBCTest:testBenchmarkUpdateMockClient() (gas: 160229)
1010
IBCTest:testToUint128((uint64,uint64)) (runs: 256, μ: 947, ~: 947)
11-
TestICS02:testCreateClient() (gas: 36498151)
12-
TestICS02:testInvalidCreateClient() (gas: 36395370)
13-
TestICS02:testInvalidUpdateClient() (gas: 36394278)
14-
TestICS02:testRegisterClient() (gas: 36049944)
15-
TestICS02:testRegisterClientDuplicatedClientType() (gas: 36035253)
16-
TestICS02:testRegisterClientInvalidClientType() (gas: 36064714)
17-
TestICS02:testUpdateClient() (gas: 36562478)
11+
TestICS02:testCreateClient() (gas: 36496843)
12+
TestICS02:testInvalidCreateClient() (gas: 36394062)
13+
TestICS02:testInvalidUpdateClient() (gas: 36392970)
14+
TestICS02:testRegisterClient() (gas: 36048636)
15+
TestICS02:testRegisterClientDuplicatedClientType() (gas: 36033945)
16+
TestICS02:testRegisterClientInvalidClientType() (gas: 36063406)
17+
TestICS02:testUpdateClient() (gas: 36561170)
1818
TestICS03Handshake:testConnOpenAck() (gas: 1858230)
1919
TestICS03Handshake:testConnOpenConfirm() (gas: 2054143)
2020
TestICS03Handshake:testConnOpenInit() (gas: 1429838)
@@ -37,32 +37,32 @@ TestICS04Handshake:testChanOpenInit() (gas: 2543524)
3737
TestICS04Handshake:testChanOpenTry() (gas: 3099898)
3838
TestICS04Handshake:testInvalidChanOpenAck() (gas: 2439749)
3939
TestICS04Handshake:testInvalidChanOpenConfirm() (gas: 2517338)
40-
TestICS04Handshake:testInvalidChanOpenInit() (gas: 1758704)
41-
TestICS04Handshake:testInvalidChanOpenTry() (gas: 1773674)
42-
TestICS04Packet:testAcknowledgementPacket() (gas: 3351152)
40+
TestICS04Handshake:testInvalidChanOpenInit() (gas: 1760558)
41+
TestICS04Handshake:testInvalidChanOpenTry() (gas: 1775528)
42+
TestICS04Packet:testAcknowledgementPacket() (gas: 3351116)
4343
TestICS04Packet:testInvalidSendPacket() (gas: 3551583)
44-
TestICS04Packet:testRecvPacket() (gas: 10995054)
44+
TestICS04Packet:testRecvPacket() (gas: 10996130)
4545
TestICS04Packet:testRecvPacketTimeoutHeight() (gas: 3259727)
4646
TestICS04Packet:testRecvPacketTimeoutTimestamp() (gas: 3278877)
4747
TestICS04Packet:testSendPacket() (gas: 6412442)
4848
TestICS04Packet:testTimeoutOnClose() (gas: 3553289)
49-
TestICS04Upgrade:testUpgradeAuthorityCancel() (gas: 46742362)
49+
TestICS04Upgrade:testUpgradeAuthorityCancel() (gas: 46742335)
5050
TestICS04Upgrade:testUpgradeCannotCancelWithOldErrorReceipt() (gas: 3456630)
51-
TestICS04Upgrade:testUpgradeCannotRecvNextUpgradePacket() (gas: 5266752)
52-
TestICS04Upgrade:testUpgradeCounterpartyAdvanceNextSequenceBeforeOpen() (gas: 5236270)
51+
TestICS04Upgrade:testUpgradeCannotRecvNextUpgradePacket() (gas: 5266005)
52+
TestICS04Upgrade:testUpgradeCounterpartyAdvanceNextSequenceBeforeOpen() (gas: 5236258)
5353
TestICS04Upgrade:testUpgradeCrossingHelloIncompatibleProposals() (gas: 4407259)
54-
TestICS04Upgrade:testUpgradeFull() (gas: 57784890)
54+
TestICS04Upgrade:testUpgradeFull() (gas: 57784728)
5555
TestICS04Upgrade:testUpgradeInit() (gas: 3069408)
5656
TestICS04Upgrade:testUpgradeNoChanges() (gas: 2471936)
5757
TestICS04Upgrade:testUpgradeOutOfSync() (gas: 3903220)
58-
TestICS04Upgrade:testUpgradeRelaySuccessAtCounterpartyFlushComplete() (gas: 5238516)
59-
TestICS04Upgrade:testUpgradeRelaySuccessAtFlushing() (gas: 5612449)
60-
TestICS04Upgrade:testUpgradeSendPacketFailAtFlushingOrFlushComplete() (gas: 4071036)
58+
TestICS04Upgrade:testUpgradeRelaySuccessAtCounterpartyFlushComplete() (gas: 5237770)
59+
TestICS04Upgrade:testUpgradeRelaySuccessAtFlushing() (gas: 5610957)
60+
TestICS04Upgrade:testUpgradeSendPacketFailAtFlushingOrFlushComplete() (gas: 4071025)
6161
TestICS04Upgrade:testUpgradeTimeoutAbortAck() (gas: 17681581)
6262
TestICS04Upgrade:testUpgradeTimeoutAbortConfirm() (gas: 21317741)
63-
TestICS04Upgrade:testUpgradeTimeoutUpgrade() (gas: 44264108)
64-
TestICS04Upgrade:testUpgradeToOrdered() (gas: 56493668)
65-
TestICS04Upgrade:testUpgradeToUnordered() (gas: 45102699)
63+
TestICS04Upgrade:testUpgradeTimeoutUpgrade() (gas: 44264077)
64+
TestICS04Upgrade:testUpgradeToOrdered() (gas: 56509529)
65+
TestICS04Upgrade:testUpgradeToUnordered() (gas: 45113829)
6666
TestICS04UpgradeApp:testUpgradeAuthorizationChanneNotFound() (gas: 61690)
6767
TestICS04UpgradeApp:testUpgradeAuthorizationRePropose() (gas: 2565532)
6868
TestICS04UpgradeApp:testUpgradeAuthorizationRemove() (gas: 2473992)

contracts/core/04-channel/IBCChannelHandshake.sol

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@ import {IBCChannelLib} from "./IBCChannelLib.sol";
2020
contract IBCChannelHandshake is IBCModuleManager, IIBCChannelHandshake, IIBCChannelErrors {
2121
using IBCHeight for Height.Data;
2222

23+
// ------------- IIBCChannelHandshake implementation ------------------- //
24+
2325
/**
2426
* @dev channelOpenInit is called by a module to initiate a channel opening handshake with a module on another chain.
2527
*/
2628
function channelOpenInit(IIBCChannelHandshake.MsgChannelOpenInit calldata msg_)
27-
external
29+
public
30+
override
2831
returns (string memory, string memory)
2932
{
3033
if (msg_.channel.connection_hops.length != 1) {
@@ -52,6 +55,7 @@ contract IBCChannelHandshake is IBCModuleManager, IIBCChannelHandshake, IIBCChan
5255

5356
string memory channelId = generateChannelIdentifier();
5457
initializeSequences(msg_.portId, channelId);
58+
emit GeneratedChannelIdentifier(channelId);
5559

5660
IIBCModule module = lookupModuleByPort(msg_.portId);
5761
string memory version = module.onChanOpenInit(
@@ -74,15 +78,15 @@ contract IBCChannelHandshake is IBCModuleManager, IIBCChannelHandshake, IIBCChan
7478
msg_.channel.connection_hops,
7579
version
7680
);
77-
emit GeneratedChannelIdentifier(channelId);
7881
return (channelId, version);
7982
}
8083

8184
/**
8285
* @dev channelOpenTry is called by a module to accept the first step of a channel opening handshake initiated by a module on another chain.
8386
*/
8487
function channelOpenTry(IIBCChannelHandshake.MsgChannelOpenTry calldata msg_)
85-
external
88+
public
89+
override
8690
returns (string memory, string memory)
8791
{
8892
if (msg_.channel.connection_hops.length != 1) {
@@ -127,6 +131,7 @@ contract IBCChannelHandshake is IBCModuleManager, IIBCChannelHandshake, IIBCChan
127131

128132
string memory channelId = generateChannelIdentifier();
129133
initializeSequences(msg_.portId, channelId);
134+
emit GeneratedChannelIdentifier(channelId);
130135

131136
IIBCModule module = lookupModuleByPort(msg_.portId);
132137
string memory version = module.onChanOpenTry(
@@ -149,14 +154,13 @@ contract IBCChannelHandshake is IBCModuleManager, IIBCChannelHandshake, IIBCChan
149154
msg_.channel.connection_hops,
150155
version
151156
);
152-
emit GeneratedChannelIdentifier(channelId);
153157
return (channelId, version);
154158
}
155159

156160
/**
157161
* @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.
158162
*/
159-
function channelOpenAck(IIBCChannelHandshake.MsgChannelOpenAck calldata msg_) external {
163+
function channelOpenAck(IIBCChannelHandshake.MsgChannelOpenAck calldata msg_) public override {
160164
Channel.Data storage channel = channels[msg_.portId][msg_.channelId];
161165
if (channel.state != Channel.State.STATE_INIT) {
162166
revert IBCChannelUnexpectedChannelState(channel.state);
@@ -195,7 +199,7 @@ contract IBCChannelHandshake is IBCModuleManager, IIBCChannelHandshake, IIBCChan
195199
/**
196200
* @dev channelOpenConfirm is called by the counterparty module to close their end of the channel, since the other end has been closed.
197201
*/
198-
function channelOpenConfirm(IIBCChannelHandshake.MsgChannelOpenConfirm calldata msg_) external {
202+
function channelOpenConfirm(IIBCChannelHandshake.MsgChannelOpenConfirm calldata msg_) public override {
199203
Channel.Data storage channel = channels[msg_.portId][msg_.channelId];
200204
if (channel.state != Channel.State.STATE_TRYOPEN) {
201205
revert IBCChannelUnexpectedChannelState(channel.state);
@@ -227,7 +231,7 @@ contract IBCChannelHandshake is IBCModuleManager, IIBCChannelHandshake, IIBCChan
227231
/**
228232
* @dev channelCloseInit is called by either module to close their end of the channel. Once closed, channels cannot be reopened.
229233
*/
230-
function channelCloseInit(IIBCChannelHandshake.MsgChannelCloseInit calldata msg_) external {
234+
function channelCloseInit(IIBCChannelHandshake.MsgChannelCloseInit calldata msg_) public override {
231235
Channel.Data storage channel = channels[msg_.portId][msg_.channelId];
232236
if (channel.state == Channel.State.STATE_UNINITIALIZED_UNSPECIFIED) {
233237
revert IBCChannelChannelNotFound(msg_.portId, msg_.channelId);
@@ -249,7 +253,7 @@ contract IBCChannelHandshake is IBCModuleManager, IIBCChannelHandshake, IIBCChan
249253
* @dev channelCloseConfirm is called by the counterparty module to close their end of the
250254
* channel, since the other end has been closed.
251255
*/
252-
function channelCloseConfirm(IIBCChannelHandshake.MsgChannelCloseConfirm calldata msg_) external {
256+
function channelCloseConfirm(IIBCChannelHandshake.MsgChannelCloseConfirm calldata msg_) public override {
253257
Channel.Data storage channel = channels[msg_.portId][msg_.channelId];
254258
if (channel.state == Channel.State.STATE_UNINITIALIZED_UNSPECIFIED) {
255259
revert IBCChannelChannelNotFound(msg_.portId, msg_.channelId);
@@ -285,6 +289,8 @@ contract IBCChannelHandshake is IBCModuleManager, IIBCChannelHandshake, IIBCChan
285289
);
286290
}
287291

292+
// ------------- Private functions ------------------- //
293+
288294
/**
289295
* @dev writeChannel writes a channel which has successfully passed the OpenInit or OpenTry handshake step.
290296
*/
@@ -296,7 +302,7 @@ contract IBCChannelHandshake is IBCModuleManager, IIBCChannelHandshake, IIBCChan
296302
ChannelCounterparty.Data calldata counterparty,
297303
string[] calldata connectionHops,
298304
string memory version
299-
) internal {
305+
) private {
300306
Channel.Data storage channel = channels[portId][channelId];
301307
channel.state = state;
302308
channel.ordering = order;
@@ -309,13 +315,20 @@ contract IBCChannelHandshake is IBCModuleManager, IIBCChannelHandshake, IIBCChan
309315
updateChannelCommitment(portId, channelId);
310316
}
311317

318+
function initializeSequences(string memory portId, string memory channelId) internal {
319+
nextSequenceSends[portId][channelId] = 1;
320+
nextSequenceRecvs[portId][channelId] = 1;
321+
nextSequenceAcks[portId][channelId] = 1;
322+
recvStartSequences[portId][channelId].sequence = 1;
323+
commitments[IBCCommitment.nextSequenceRecvCommitmentKey(portId, channelId)] =
324+
keccak256(abi.encodePacked((bytes8(uint64(1)))));
325+
}
326+
312327
function updateChannelCommitment(string memory portId, string memory channelId) private {
313328
commitments[IBCCommitment.channelCommitmentKey(portId, channelId)] =
314329
keccak256(Channel.encode(channels[portId][channelId]));
315330
}
316331

317-
/* Verification functions */
318-
319332
function verifyChannelState(
320333
ConnectionEnd.Data storage connection,
321334
Height.Data calldata height,
@@ -345,17 +358,6 @@ contract IBCChannelHandshake is IBCModuleManager, IIBCChannelHandshake, IIBCChan
345358
);
346359
}
347360

348-
/* Internal functions */
349-
350-
function initializeSequences(string memory portId, string memory channelId) internal {
351-
nextSequenceSends[portId][channelId] = 1;
352-
nextSequenceRecvs[portId][channelId] = 1;
353-
nextSequenceAcks[portId][channelId] = 1;
354-
recvStartSequences[portId][channelId].sequence = 1;
355-
commitments[IBCCommitment.nextSequenceRecvCommitmentKey(portId, channelId)] =
356-
keccak256(abi.encodePacked((bytes8(uint64(1)))));
357-
}
358-
359361
function getCounterpartyHops(string memory connectionId) internal view returns (string[] memory hops) {
360362
hops = new string[](1);
361363
hops[0] = connections[connectionId].counterparty.connection_id;

contracts/core/04-channel/IBCChannelPacketSendRecv.sol

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ contract IBCChannelPacketSendRecv is
2424
{
2525
using IBCHeight for Height.Data;
2626

27+
// --------- IIBCChannelPacketSendRecv Implementation --------- //
28+
2729
/**
2830
* @dev sendPacket is called by a module in order to send an IBC packet on a channel.
2931
* The packet sequence generated for the packet to be sent is returned. An error
@@ -35,7 +37,7 @@ contract IBCChannelPacketSendRecv is
3537
Height.Data calldata timeoutHeight,
3638
uint64 timeoutTimestamp,
3739
bytes calldata data
38-
) external returns (uint64) {
40+
) public override returns (uint64) {
3941
authenticateChannelCapability(sourcePort, sourceChannel);
4042

4143
Channel.Data storage channel = channels[sourcePort][sourceChannel];
@@ -86,7 +88,7 @@ contract IBCChannelPacketSendRecv is
8688
string calldata destinationChannel,
8789
uint64 sequence,
8890
bytes calldata acknowledgement
89-
) public {
91+
) public override {
9092
authenticateChannelCapability(destinationPortId, destinationChannel);
9193
Channel.Data storage channel = channels[destinationPortId][destinationChannel];
9294
if (channel.state != Channel.State.STATE_OPEN) {
@@ -98,26 +100,11 @@ contract IBCChannelPacketSendRecv is
98100
_writeAcknowledgement(destinationPortId, destinationChannel, sequence, acknowledgement);
99101
}
100102

101-
function _writeAcknowledgement(
102-
string calldata destinationPortId,
103-
string calldata destinationChannel,
104-
uint64 sequence,
105-
bytes memory acknowledgement
106-
) internal {
107-
bytes32 ackCommitmentKey =
108-
IBCCommitment.packetAcknowledgementCommitmentKeyCalldata(destinationPortId, destinationChannel, sequence);
109-
if (commitments[ackCommitmentKey] != bytes32(0)) {
110-
revert IBCChannelAcknowledgementAlreadyWritten(destinationPortId, destinationChannel, sequence);
111-
}
112-
commitments[ackCommitmentKey] = keccak256(abi.encodePacked(sha256(acknowledgement)));
113-
emit WriteAcknowledgement(destinationPortId, destinationChannel, sequence, acknowledgement);
114-
}
115-
116103
/**
117104
* @dev recvPacket is called by a module in order to receive & process an IBC packet
118105
* sent on the corresponding channel end on the counterparty chain.
119106
*/
120-
function recvPacket(MsgPacketRecv calldata msg_) external {
107+
function recvPacket(MsgPacketRecv calldata msg_) public override {
121108
Channel.Data storage channel = channels[msg_.packet.destinationPort][msg_.packet.destinationChannel];
122109
if (channel.state == Channel.State.STATE_OPEN) {} else if (
123110
channel.state == Channel.State.STATE_FLUSHING || channel.state == Channel.State.STATE_FLUSHCOMPLETE
@@ -204,6 +191,7 @@ contract IBCChannelPacketSendRecv is
204191
} else {
205192
revert IBCChannelUnknownChannelOrder(channel.ordering);
206193
}
194+
emit RecvPacket(msg_.packet);
207195
bytes memory acknowledgement = lookupModuleByChannel(
208196
msg_.packet.destinationPort, msg_.packet.destinationChannel
209197
).onRecvPacket(msg_.packet, _msgSender());
@@ -212,7 +200,6 @@ contract IBCChannelPacketSendRecv is
212200
msg_.packet.destinationPort, msg_.packet.destinationChannel, msg_.packet.sequence, acknowledgement
213201
);
214202
}
215-
emit RecvPacket(msg_.packet);
216203
}
217204

218205
/**
@@ -223,7 +210,7 @@ contract IBCChannelPacketSendRecv is
223210
* which is no longer necessary since the packet has been received and acted upon.
224211
* It will also increment NextSequenceAck in case of ORDERED channels.
225212
*/
226-
function acknowledgePacket(MsgPacketAcknowledgement calldata msg_) external {
213+
function acknowledgePacket(MsgPacketAcknowledgement calldata msg_) public override {
227214
Channel.Data storage channel = channels[msg_.packet.sourcePort][msg_.packet.sourceChannel];
228215
if (channel.state != Channel.State.STATE_OPEN) {
229216
if (channel.state != Channel.State.STATE_FLUSHING) {
@@ -295,13 +282,28 @@ contract IBCChannelPacketSendRecv is
295282
}
296283

297284
delete commitments[packetCommitmentKey];
285+
emit AcknowledgePacket(msg_.packet, msg_.acknowledgement);
298286
lookupModuleByChannel(msg_.packet.sourcePort, msg_.packet.sourceChannel).onAcknowledgementPacket(
299287
msg_.packet, msg_.acknowledgement, _msgSender()
300288
);
301-
emit AcknowledgePacket(msg_.packet, msg_.acknowledgement);
302289
}
303290

304-
/* Verification functions */
291+
// --------- Private Functions --------- //
292+
293+
function _writeAcknowledgement(
294+
string calldata destinationPortId,
295+
string calldata destinationChannel,
296+
uint64 sequence,
297+
bytes memory acknowledgement
298+
) private {
299+
bytes32 ackCommitmentKey =
300+
IBCCommitment.packetAcknowledgementCommitmentKeyCalldata(destinationPortId, destinationChannel, sequence);
301+
if (commitments[ackCommitmentKey] != bytes32(0)) {
302+
revert IBCChannelAcknowledgementAlreadyWritten(destinationPortId, destinationChannel, sequence);
303+
}
304+
commitments[ackCommitmentKey] = keccak256(abi.encodePacked(sha256(acknowledgement)));
305+
emit WriteAcknowledgement(destinationPortId, destinationChannel, sequence, acknowledgement);
306+
}
305307

306308
function verifyPacketCommitment(
307309
ConnectionEnd.Data storage connection,
@@ -357,8 +359,6 @@ contract IBCChannelPacketSendRecv is
357359
);
358360
}
359361

360-
// private functions
361-
362362
function calcBlockDelay(uint64 timeDelay) private view returns (uint64) {
363363
if (timeDelay == 0) {
364364
return 0;

0 commit comments

Comments
 (0)