From d1718efdc21e9e347d14bfb5aa4e1f3b9c70045f Mon Sep 17 00:00:00 2001 From: Carlos Rodriguez Date: Mon, 19 Aug 2024 13:52:57 +0200 Subject: [PATCH 1/3] imp: relocate timeout checks and use get app function --- src/ICS26Router.sol | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/ICS26Router.sol b/src/ICS26Router.sol index 7a469e3..a6ff343 100644 --- a/src/ICS26Router.sol +++ b/src/ICS26Router.sol @@ -59,9 +59,9 @@ contract ICS26Router is IICS26Router, IBCStore, Ownable, IICS26RouterErrors, Ree revert IBCInvalidPortIdentifier(newPortId); } - emit IBCAppAdded(newPortId, app); - apps[newPortId] = IIBCApp(app); + + emit IBCAppAdded(newPortId, app); } /// @notice Sends a packet @@ -69,6 +69,8 @@ contract ICS26Router is IICS26Router, IBCStore, Ownable, IICS26RouterErrors, Ree /// @return The sequence number of the packet /// @inheritdoc IICS26Router function sendPacket(MsgSendPacket calldata msg_) external nonReentrant returns (uint32) { + IIBCApp app = getIBCApp(msg_.packet.destPort); + string memory counterpartyId = ics02Client.getCounterparty(msg_.sourceChannel).clientId; // TODO: validate all identifiers @@ -91,11 +93,6 @@ contract ICS26Router is IICS26Router, IBCStore, Ownable, IICS26RouterErrors, Ree IIBCAppCallbacks.OnSendPacketCallback memory sendPacketCallback = IIBCAppCallbacks.OnSendPacketCallback({ packet: packet, sender: msg.sender }); - - IIBCApp app = apps[msg_.sourcePort]; - if (app == IIBCApp(address(0))) { - revert IBCAppNotFound(msg_.sourcePort); - } app.onSendPacket(sendPacketCallback); IBCStore.commitPacket(packet); @@ -115,6 +112,10 @@ contract ICS26Router is IICS26Router, IBCStore, Ownable, IICS26RouterErrors, Ree revert IBCInvalidCounterparty(counterpartyId, msg_.packet.sourceChannel); } + if (msg_.packet.timeoutTimestamp <= block.timestamp) { + revert IBCInvalidTimeoutTimestamp(msg_.packet.timeoutTimestamp, block.timestamp); + } + bytes memory commitmentPath = ICS24Host.packetCommitmentPathCalldata( msg_.packet.sourcePort, msg_.packet.sourceChannel, msg_.packet.sequence ); @@ -129,10 +130,6 @@ contract ICS26Router is IICS26Router, IBCStore, Ownable, IICS26RouterErrors, Ree ics02Client.getClient(msg_.packet.destChannel).membership(membershipMsg); - if (msg_.packet.timeoutTimestamp <= block.timestamp) { - revert IBCInvalidTimeoutTimestamp(msg_.packet.timeoutTimestamp, block.timestamp); - } - bytes memory ack = app.onRecvPacket(IIBCAppCallbacks.OnRecvPacketCallback({ packet: msg_.packet, relayer: msg.sender })); if (ack.length == 0) { @@ -200,6 +197,11 @@ contract ICS26Router is IICS26Router, IBCStore, Ownable, IICS26RouterErrors, Ree revert IBCInvalidCounterparty(counterpartyId, msg_.packet.destChannel); } + uint256 counterpartyTimestamp = ics02Client.getClient(msg_.packet.sourceChannel).membership(nonMembershipMsg); + if (counterpartyTimestamp < msg_.packet.timeoutTimestamp) { + revert IBCInvalidTimeoutTimestamp(msg_.packet.timeoutTimestamp, counterpartyTimestamp); + } + // this will revert if the packet commitment does not exist bytes32 storedCommitment = IBCStore.deletePacketCommitment(msg_.packet); if (storedCommitment != ICS24Host.packetCommitmentBytes32(msg_.packet)) { @@ -216,11 +218,6 @@ contract ICS26Router is IICS26Router, IBCStore, Ownable, IICS26RouterErrors, Ree value: bytes("") }); - uint256 counterpartyTimestamp = ics02Client.getClient(msg_.packet.sourceChannel).membership(nonMembershipMsg); - if (counterpartyTimestamp < msg_.packet.timeoutTimestamp) { - revert IBCInvalidTimeoutTimestamp(msg_.packet.timeoutTimestamp, counterpartyTimestamp); - } - app.onTimeoutPacket(IIBCAppCallbacks.OnTimeoutPacketCallback({ packet: msg_.packet, relayer: msg.sender })); emit TimeoutPacket(msg_.packet); From 0637a578d450d62129ef276bb7dcaa1a029a8d8d Mon Sep 17 00:00:00 2001 From: Carlos Rodriguez Date: Mon, 19 Aug 2024 14:05:33 +0200 Subject: [PATCH 2/3] fix build issue --- src/ICS26Router.sol | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ICS26Router.sol b/src/ICS26Router.sol index a6ff343..a1e6fc3 100644 --- a/src/ICS26Router.sol +++ b/src/ICS26Router.sol @@ -197,11 +197,6 @@ contract ICS26Router is IICS26Router, IBCStore, Ownable, IICS26RouterErrors, Ree revert IBCInvalidCounterparty(counterpartyId, msg_.packet.destChannel); } - uint256 counterpartyTimestamp = ics02Client.getClient(msg_.packet.sourceChannel).membership(nonMembershipMsg); - if (counterpartyTimestamp < msg_.packet.timeoutTimestamp) { - revert IBCInvalidTimeoutTimestamp(msg_.packet.timeoutTimestamp, counterpartyTimestamp); - } - // this will revert if the packet commitment does not exist bytes32 storedCommitment = IBCStore.deletePacketCommitment(msg_.packet); if (storedCommitment != ICS24Host.packetCommitmentBytes32(msg_.packet)) { @@ -218,6 +213,11 @@ contract ICS26Router is IICS26Router, IBCStore, Ownable, IICS26RouterErrors, Ree value: bytes("") }); + uint256 counterpartyTimestamp = ics02Client.getClient(msg_.packet.sourceChannel).membership(nonMembershipMsg); + if (counterpartyTimestamp < msg_.packet.timeoutTimestamp) { + revert IBCInvalidTimeoutTimestamp(msg_.packet.timeoutTimestamp, counterpartyTimestamp); + } + app.onTimeoutPacket(IIBCAppCallbacks.OnTimeoutPacketCallback({ packet: msg_.packet, relayer: msg.sender })); emit TimeoutPacket(msg_.packet); From c32912b07c67825d7902b3cd984bd34e625e64bd Mon Sep 17 00:00:00 2001 From: Carlos Rodriguez Date: Mon, 19 Aug 2024 14:13:28 +0200 Subject: [PATCH 3/3] use the right port --- src/ICS26Router.sol | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ICS26Router.sol b/src/ICS26Router.sol index a1e6fc3..192fc01 100644 --- a/src/ICS26Router.sol +++ b/src/ICS26Router.sol @@ -69,7 +69,7 @@ contract ICS26Router is IICS26Router, IBCStore, Ownable, IICS26RouterErrors, Ree /// @return The sequence number of the packet /// @inheritdoc IICS26Router function sendPacket(MsgSendPacket calldata msg_) external nonReentrant returns (uint32) { - IIBCApp app = getIBCApp(msg_.packet.destPort); + IIBCApp app = getIBCApp(msg_.sourcePort); string memory counterpartyId = ics02Client.getCounterparty(msg_.sourceChannel).clientId; @@ -217,7 +217,7 @@ contract ICS26Router is IICS26Router, IBCStore, Ownable, IICS26RouterErrors, Ree if (counterpartyTimestamp < msg_.packet.timeoutTimestamp) { revert IBCInvalidTimeoutTimestamp(msg_.packet.timeoutTimestamp, counterpartyTimestamp); } - + app.onTimeoutPacket(IIBCAppCallbacks.OnTimeoutPacketCallback({ packet: msg_.packet, relayer: msg.sender })); emit TimeoutPacket(msg_.packet);