Skip to content
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
IBC-2: fix to update channel commitment in timeoutPacket()
Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele committed Nov 23, 2024
commit 7e58f5b388fbdfe42c5e04f6c82401ee960ff126
9 changes: 9 additions & 0 deletions contracts/core/04-channel/IBCChannelPacketTimeout.sol
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ contract IBCChannelPacketTimeout is IBCModuleManager, IIBCChannelPacketTimeout,
);
}
channel.state = Channel.State.STATE_CLOSED;
updateChannelCommitment(msg_.packet.sourcePort, msg_.packet.sourceChannel);
} else if (channel.ordering == Channel.Order.ORDER_UNORDERED) {
bytes memory path = IBCCommitment.packetReceiptCommitmentPathCalldata(
msg_.packet.destinationPort, msg_.packet.destinationChannel, msg_.packet.sequence
Expand Down Expand Up @@ -284,4 +285,12 @@ contract IBCChannelPacketTimeout is IBCModuleManager, IIBCChannelPacketTimeout,
return (timeDelay + hostStorage.expectedTimePerBlock - 1) / hostStorage.expectedTimePerBlock;
}
}

/**
* @dev updateChannelCommitment updates the channel commitment for the given port and channel
*/
function updateChannelCommitment(string memory portId, string memory channelId) private {
getCommitments()[IBCCommitment.channelCommitmentKey(portId, channelId)] =
keccak256(Channel.encode(getChannelStorage()[portId][channelId].channel));
}
}
8 changes: 8 additions & 0 deletions tests/foundry/src/ICS04Packet.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -439,11 +439,15 @@ contract TestICS04Packet is
counterpartyHandler.recvPacket(msg_);
client.updateClient(clientId, mockClientHeader(uint64(getBlockNumber())));
// timeout on source chain
(Channel.Data memory c,) = handler.getChannel(channelInfo.portId, channelInfo.channelId);
handler.timeoutPacket(msgTimeoutPacket(channelInfo.ordering, p0, H(getBlockNumber())));
if (orders[i] == Channel.Order.ORDER_ORDERED) {
ensureChannelState(handler, channelInfo, Channel.State.STATE_CLOSED);
c.state = Channel.State.STATE_CLOSED;
ensureChannelCommitment(handler, channelInfo, c);
} else if (orders[i] == Channel.Order.ORDER_UNORDERED) {
ensureChannelState(handler, channelInfo, Channel.State.STATE_OPEN);
ensureChannelCommitment(handler, channelInfo, c);
}
assertEq(handler.getPacketCommitment(channelInfo.portId, channelInfo.channelId, p0.sequence), bytes32(0));
}
Expand Down Expand Up @@ -491,11 +495,15 @@ contract TestICS04Packet is
counterpartyHandler.recvPacket(msg_);
client.updateClient(clientId, mockClientHeader(uint64(getBlockNumber())));
// timeout on source chain
(Channel.Data memory c,) = handler.getChannel(channelInfo.portId, channelInfo.channelId);
handler.timeoutPacket(msgTimeoutPacket(channelInfo.ordering, p0, H(getBlockNumber())));
if (orders[i] == Channel.Order.ORDER_ORDERED) {
ensureChannelState(handler, channelInfo, Channel.State.STATE_CLOSED);
c.state = Channel.State.STATE_CLOSED;
ensureChannelCommitment(handler, channelInfo, c);
} else if (orders[i] == Channel.Order.ORDER_UNORDERED) {
ensureChannelState(handler, channelInfo, Channel.State.STATE_OPEN);
ensureChannelCommitment(handler, channelInfo, c);
}
assertEq(handler.getPacketCommitment(channelInfo.portId, channelInfo.channelId, p0.sequence), bytes32(0));
}
Expand Down
5 changes: 5 additions & 0 deletions tests/foundry/src/helpers/ICS04HandshakeTestHelper.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ abstract contract ICS04TestHelper is IBCTestHelper {
assertTrue(ok);
assertTrue(channel_.state == state);
}

function ensureChannelCommitment(IIBCHandler handler, ChannelInfo memory channelInfo, Channel.Data memory channel) internal {
bytes32 commitment = handler.getCommitment(IBCCommitment.channelCommitmentKey(channelInfo.portId, channelInfo.channelId));
assertEq(commitment, keccak256(Channel.encode(channel)));
}
}

abstract contract ICS04HandshakeTestHelper is ICS04TestHelper {
Expand Down