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

Fix callbacks of IBC Module #222

Merged
merged 3 commits into from
Oct 12, 2023
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
12 changes: 6 additions & 6 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
IBCTest:testBenchmarkCreateMockClient() (gas: 214252)
IBCTest:testBenchmarkRecvPacket() (gas: 157441)
IBCTest:testBenchmarkSendPacket() (gas: 85940)
IBCTest:testBenchmarkUpdateMockClient() (gas: 129807)
IBCTest:testConnectionOpenInit() (gas: 496548)
IBCTest:testSendPacketInvalidTimestamp() (gas: 39167)
IBCTest:testBenchmarkCreateMockClient() (gas: 214230)
IBCTest:testBenchmarkRecvPacket() (gas: 157447)
IBCTest:testBenchmarkSendPacket() (gas: 86050)
IBCTest:testBenchmarkUpdateMockClient() (gas: 129918)
IBCTest:testConnectionOpenInit() (gas: 496526)
IBCTest:testSendPacketInvalidTimestamp() (gas: 39277)
IBCTest:testToUint128((uint64,uint64)) (runs: 256, μ: 1047, ~: 1047)
TestICS20:testAddressToHex(address) (runs: 256, μ: 23720, ~: 23889)
TestICS20:testHexToAddress(string) (runs: 256, μ: 4817, ~: 4829)
Expand Down
35 changes: 28 additions & 7 deletions contracts/apps/20-transfer/ICS20Transfer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import "solidity-bytes-utils/contracts/BytesLib.sol";
abstract contract ICS20Transfer is IBCAppBase {
using BytesLib for bytes;

string public constant ICS20_VERSION = "ics20-1";

mapping(string => address) channelEscrowAddresses;

function onRecvPacket(Packet.Data calldata packet, address)
Expand Down Expand Up @@ -71,26 +73,45 @@ abstract contract ICS20Transfer is IBCAppBase {
}

function onChanOpenInit(
Channel.Order,
Channel.Order order,
string[] calldata,
string calldata,
string calldata channelId,
ChannelCounterparty.Data calldata,
string calldata
) external virtual override onlyIBC {
string calldata version
) external virtual override onlyIBC returns (string memory) {
require(order == Channel.Order.ORDER_UNORDERED, "must be unordered");
bytes memory versionBytes = bytes(version);
require(versionBytes.length == 0 || keccak256(versionBytes) == keccak256(bytes(ICS20_VERSION)));
channelEscrowAddresses[channelId] = address(this);
return ICS20_VERSION;
}

function onChanOpenTry(
Channel.Order,
Channel.Order order,
string[] calldata,
string calldata,
string calldata channelId,
ChannelCounterparty.Data calldata,
string calldata,
string calldata
) external virtual override onlyIBC {
string calldata counterpartyVersion
) external virtual override onlyIBC returns (string memory) {
require(order == Channel.Order.ORDER_UNORDERED, "must be unordered");
require(keccak256(bytes(counterpartyVersion)) == keccak256(bytes(ICS20_VERSION)));
channelEscrowAddresses[channelId] = address(this);
return ICS20_VERSION;
}

function onChanOpenAck(string calldata, string calldata, string calldata counterpartyVersion)
external
virtual
override
onlyIBC
{
require(keccak256(bytes(counterpartyVersion)) == keccak256(bytes(ICS20_VERSION)));
}

function onChanCloseInit(string calldata, string calldata) external virtual override onlyIBC {
revert("not allowed");
}

function onTimeoutPacket(Packet.Data calldata packet, address) external virtual override onlyIBC {
Expand Down
5 changes: 2 additions & 3 deletions contracts/apps/commons/IBCAppBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ abstract contract IBCAppBase is Context, IIBCModule {
string calldata channelId,
ChannelCounterparty.Data calldata,
string calldata
) external virtual override onlyIBC {}
) external virtual override onlyIBC returns (string memory) {}

/**
* @dev See IIBCModule-onChanOpenTry
Expand All @@ -53,9 +53,8 @@ abstract contract IBCAppBase is Context, IIBCModule {
string calldata,
string calldata channelId,
ChannelCounterparty.Data calldata,
string calldata,
string calldata
) external virtual override onlyIBC {}
) external virtual override onlyIBC returns (string memory) {}

/**
* @dev See IIBCModule-onChanOpenAck
Expand Down
5 changes: 2 additions & 3 deletions contracts/core/05-port/IIBCModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface IIBCModule {
string calldata channelId,
ChannelCounterparty.Data calldata counterparty,
string calldata version
) external;
) external returns (string memory);

// OnChanOpenTry will verify the relayer-chosen parameters along with the
// counterparty-chosen version string and perform custom TRY logic.
Expand All @@ -39,9 +39,8 @@ interface IIBCModule {
string calldata portId,
string calldata channelId,
ChannelCounterparty.Data calldata counterparty,
string calldata version,
string calldata counterpartyVersion
) external;
) external returns (string memory);

// OnChanOpenAck will error if the counterparty selected version string
// is invalid to abort the handshake. It may also perform custom ACK logic.
Expand Down
29 changes: 15 additions & 14 deletions contracts/core/25-handler/IBCChannelHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@ abstract contract IBCChannelHandler is ModuleManager {
ibcChannel = _ibcChannel;
}

function channelOpenInit(IBCMsgs.MsgChannelOpenInit calldata msg_) external returns (string memory channelId) {
function channelOpenInit(IBCMsgs.MsgChannelOpenInit calldata msg_)
external
returns (string memory channelId, string memory version)
{
bytes memory res =
ibcChannel.functionDelegateCall(abi.encodeWithSelector(IIBCChannelHandshake.channelOpenInit.selector, msg_));
channelId = abi.decode(res, (string));

IIBCModule module = lookupModuleByPort(msg_.portId);
module.onChanOpenInit(
version = module.onChanOpenInit(
msg_.channel.ordering,
msg_.channel.connection_hops,
msg_.portId,
Expand All @@ -40,30 +43,28 @@ abstract contract IBCChannelHandler is ModuleManager {
);
claimCapability(channelCapabilityPath(msg_.portId, channelId), address(module));
emit GeneratedChannelIdentifier(channelId);
return channelId;
return (channelId, version);
}

function channelOpenTry(IBCMsgs.MsgChannelOpenTry calldata msg_) external returns (string memory channelId) {
{
// avoid "Stack too deep" error
bytes memory res = ibcChannel.functionDelegateCall(
abi.encodeWithSelector(IIBCChannelHandshake.channelOpenTry.selector, msg_)
);
channelId = abi.decode(res, (string));
}
function channelOpenTry(IBCMsgs.MsgChannelOpenTry calldata msg_)
external
returns (string memory channelId, string memory version)
{
bytes memory res =
ibcChannel.functionDelegateCall(abi.encodeWithSelector(IIBCChannelHandshake.channelOpenTry.selector, msg_));
channelId = abi.decode(res, (string));
IIBCModule module = lookupModuleByPort(msg_.portId);
module.onChanOpenTry(
version = module.onChanOpenTry(
msg_.channel.ordering,
msg_.channel.connection_hops,
msg_.portId,
channelId,
msg_.channel.counterparty,
msg_.channel.version,
msg_.counterpartyVersion
);
claimCapability(channelCapabilityPath(msg_.portId, channelId), address(module));
emit GeneratedChannelIdentifier(channelId);
return channelId;
return (channelId, version);
}

function channelOpenAck(IBCMsgs.MsgChannelOpenAck calldata msg_) external {
Expand Down
Loading