Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ics26): implemented AckPacket #1

Merged
merged 4 commits into from
Jul 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions src/ICS26Router.sol
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,42 @@ contract ICS26Router is IICS26Router, IBCStore, Ownable, IICS26RouterErrors, Ree
/// @notice Acknowledges a packet
/// @param msg_ The message for acknowledging packets
function ackPacket(MsgAckPacket calldata msg_) external nonReentrant {
// TODO: implement
// IIBCApp app = IIBCApp(apps[msg.packet.sourcePort]);
IIBCApp app = IIBCApp(apps[msg_.packet.sourcePort]);

string memory counterpartyId = ics02Client.getCounterparty(msg_.packet.sourceChannel).clientId;
if (keccak256(bytes(counterpartyId)) != keccak256(bytes(msg_.packet.destChannel))) {
revert IBCInvalidCounterparty(counterpartyId, msg_.packet.destChannel);
}

// this will revert if the packet commitment does not exist
bytes32 storedCommitment = IBCStore.deletePacketCommitment(msg_.packet);
if (storedCommitment != ICS24Host.packetCommitmentBytes32(msg_.packet)) {
revert IBCPacketCommitmentMismatch(storedCommitment, ICS24Host.packetCommitmentBytes32(msg_.packet));
}

bytes memory commitmentPath = ICS24Host.packetAcknowledgementCommitmentPathCalldata(
msg_.packet.sourcePort, msg_.packet.sourceChannel, msg_.packet.sequence
);
bytes32 commitmentBz = ICS24Host.packetAcknowledgementCommitmentBytes32(msg_.acknowledgement);

// verify the packet acknowledgement
ILightClientMsgs.MsgMembership memory membershipMsg = ILightClientMsgs.MsgMembership({
proof: msg_.proofAcked,
proofHeight: msg_.proofHeight,
kvPair: ILightClientMsgs.KVPair({ path: commitmentPath, value: abi.encodePacked(commitmentBz) })
});

ics02Client.getClient(msg_.packet.sourceChannel).verifyMembership(membershipMsg);

app.onAcknowledgementPacket(
IIBCAppCallbacks.OnAcknowledgementPacketCallback({
packet: msg_.packet,
acknowledgement: msg_.acknowledgement,
relayer: msg.sender
})
);

emit AckPacket(msg_.packet, msg_.acknowledgement);
}

/// @notice Timeouts a packet
Expand Down
2 changes: 2 additions & 0 deletions src/errors/IICS26RouterErrors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ interface IICS26RouterErrors {
error IBCInvalidCounterparty(string expected, string actual);

error IBCAsyncAcknowledgementNotSupported();

error IBCPacketCommitmentMismatch(bytes32 expected, bytes32 actual);
}
4 changes: 3 additions & 1 deletion src/interfaces/IICS26Router.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ interface IICS26Router is IICS26RouterMsgs {
event SendPacket(Packet packet);
/// @notice Emitted when a packet is received
event RecvPacket(Packet packet);
/// @notice Emitted when a packet is acknowledged
/// @notice Emitted when a packet acknowledgement is written
event WriteAcknowledgement(Packet packet, bytes acknowledgement);
/// @notice Emitted when a packet is timed out
event TimeoutPacket(Packet packet);
/// @notice Emitted when a packet is acknowledged
event AckPacket(Packet packet, bytes acknowledgement);
}
3 changes: 0 additions & 3 deletions src/utils/IBCIdentifiers.sol
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.25;

import { Strings } from "@openzeppelin/contracts/utils/Strings.sol";
import { IICS26RouterMsgs } from "../msgs/IICS26RouterMsgs.sol";

/// @title IBC Identifiers
/// @notice Utilities for validating IBC identifiers
library IBCIdentifiers {
Expand Down
8 changes: 5 additions & 3 deletions src/utils/IBCStore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,19 @@ abstract contract IBCStore is IIBCStore, IICS24HostErrors {
commitments[path] = commitment;
}

/// @notice Deletes a packet commitment
/// @notice Deletes a packet commitment and reverts if it does not exist
/// @param packet The packet whose commitment to delete
function deletePacketCommitment(IICS26RouterMsgs.Packet memory packet) internal {
function deletePacketCommitment(IICS26RouterMsgs.Packet memory packet) internal returns (bytes32) {
bytes32 path = ICS24Host.packetCommitmentKeyCalldata(packet.sourcePort, packet.sourceChannel, packet.sequence);
if (commitments[path] == 0) {
bytes32 commitment = commitments[path];
if (commitment == 0) {
revert IBCPacketCommitmentNotFound(
ICS24Host.packetCommitmentPathCalldata(packet.sourcePort, packet.sourceChannel, packet.sequence)
);
}

delete commitments[path];
return commitment;
}

/// @notice Sets a packet receipt
Expand Down
2 changes: 2 additions & 0 deletions test/IbcIdentifiers.t.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.25 <0.9.0;

// solhint-disable custom-errors,max-line-length

import { Test } from "forge-std/src/Test.sol";
import { IBCIdentifiers } from "../src/utils/IBCIdentifiers.sol";

Expand Down