Skip to content

Commit

Permalink
Merge pull request #221 from hyperledger-labs/mock-app
Browse files Browse the repository at this point in the history
Introduce Mock app and fix commitment of `nextSequenceRecv`

Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele authored Oct 10, 2023
2 parents 321c4d8 + 035d8c8 commit ffe0d7d
Show file tree
Hide file tree
Showing 14 changed files with 873 additions and 132 deletions.
59 changes: 59 additions & 0 deletions contracts/apps/mock/IBCMockApp.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.9;

import "../commons/IBCAppBase.sol";
import "../../core/05-port/IIBCModule.sol";
import "../../core/25-handler/IBCHandler.sol";
import "./IBCMockLib.sol";

contract IBCMockApp is IBCAppBase {
IBCHandler ibcHandler;

constructor(IBCHandler ibcHandler_) {
ibcHandler = ibcHandler_;
}

function ibcAddress() public view virtual override returns (address) {
return address(ibcHandler);
}

function sendPacket(
string calldata message,
string calldata sourcePort,
string calldata sourceChannel,
Height.Data calldata timeoutHeight,
uint64 timeoutTimestamp
) external {
ibcHandler.sendPacket(sourcePort, sourceChannel, timeoutHeight, timeoutTimestamp, bytes(message));
}

function onRecvPacket(Packet.Data calldata packet, address)
external
override
onlyIBC
returns (bytes memory acknowledgement)
{
if (keccak256(packet.data) == keccak256(IBCMockLib.MOCK_PACKET_DATA)) {
return IBCMockLib.SUCCESSFUL_ACKNOWLEDGEMENT_JSON;
} else if (keccak256(packet.data) == keccak256(IBCMockLib.MOCK_ASYNC_PACKET_DATA)) {
return bytes("");
} else {
return IBCMockLib.FAILED_ACKNOWLEDGEMENT_JSON;
}
}

function onAcknowledgementPacket(Packet.Data calldata packet, bytes calldata acknowledgement, address)
external
virtual
override
onlyIBC
{
if (keccak256(packet.data) == keccak256(IBCMockLib.MOCK_PACKET_DATA)) {
require(keccak256(acknowledgement) == keccak256(IBCMockLib.SUCCESSFUL_ACKNOWLEDGEMENT_JSON));
} else if (keccak256(packet.data) == keccak256(IBCMockLib.MOCK_ASYNC_PACKET_DATA)) {
require(acknowledgement.length == 0);
} else {
require(keccak256(acknowledgement) == keccak256(IBCMockLib.FAILED_ACKNOWLEDGEMENT_JSON));
}
}
}
11 changes: 11 additions & 0 deletions contracts/apps/mock/IBCMockLib.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.9;

library IBCMockLib {
bytes public constant MOCK_PACKET_DATA = bytes("mock packet data");
bytes public constant MOCK_FAIL_PACKET_DATA = bytes("mock failed packet data");
bytes public constant MOCK_ASYNC_PACKET_DATA = bytes("mock async packet data");

bytes public constant SUCCESSFUL_ACKNOWLEDGEMENT_JSON = bytes('{"result":"bW9jayBhY2tub3dsZWRnZW1lbnQ="}');
bytes public constant FAILED_ACKNOWLEDGEMENT_JSON = bytes('{"error":"mock failed acknowledgement"}');
}
4 changes: 4 additions & 0 deletions contracts/core/04-channel/IBCChannelHandshake.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ contract IBCChannelHandshake is IBCStore, IIBCChannelHandshake {
nextSequenceRecvs[msg_.portId][channelId] = 1;
nextSequenceAcks[msg_.portId][channelId] = 1;
updateChannelCommitment(msg_.portId, channelId);
commitments[IBCCommitment.nextSequenceRecvCommitmentKey(msg_.portId, channelId)] =
keccak256(abi.encodePacked((bytes8(uint64(1)))));
return channelId;
}

Expand Down Expand Up @@ -80,6 +82,8 @@ contract IBCChannelHandshake is IBCStore, IIBCChannelHandshake {
nextSequenceRecvs[msg_.portId][channelId] = 1;
nextSequenceAcks[msg_.portId][channelId] = 1;
updateChannelCommitment(msg_.portId, channelId);
commitments[IBCCommitment.nextSequenceRecvCommitmentKey(msg_.portId, channelId)] =
keccak256(abi.encodePacked((bytes8(uint64(1)))));
return channelId;
}

Expand Down
4 changes: 4 additions & 0 deletions contracts/core/25-handler/IBCQuerier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ abstract contract IBCQuerier is IBCStore {
return nextSequenceSends[portId][channelId];
}

function getNextSequenceRecv(string calldata portId, string calldata channelId) external view returns (uint64) {
return nextSequenceRecvs[portId][channelId];
}

function getExpectedTimePerBlock() external view returns (uint64) {
return expectedTimePerBlock;
}
Expand Down
33 changes: 32 additions & 1 deletion pkg/contract/ibchandler/ibchandler.go

Large diffs are not rendered by default.

446 changes: 446 additions & 0 deletions pkg/contract/ibcmockapp/ibcmockapp.go

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions pkg/ibc/core/commitment/commitment.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ func PacketReceiptCommitmentSlot(portID, channelID string, sequence uint64) stri
return CalculateCommitmentSlot(host.PacketReceiptKey(portID, channelID, sequence))
}

func NextSequenceRecvCommitmentSlot(portID, channelID string) string {
return CalculateCommitmentSlot(host.NextSequenceRecvKey(portID, channelID))
}

func CalculateCommitmentSlot(path []byte) string {
return crypto.Keccak256Hash(crypto.Keccak256Hash(path).Bytes(), ibcHostCommitmentSlot[:]).Hex()
}
Loading

0 comments on commit ffe0d7d

Please sign in to comment.