Skip to content

Commit

Permalink
split capabilities into portCapabilities and channelCapabilities
Browse files Browse the repository at this point in the history
Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele committed Feb 26, 2024
1 parent eccbf75 commit b4b9745
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 92 deletions.
72 changes: 36 additions & 36 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
IBCMockAppTest:testHandshake() (gas: 3435974)
IBCMockAppTest:testHandshakeBetweenDifferentPorts() (gas: 2542936)
IBCMockAppTest:testPacketRelay() (gas: 9534826)
IBCMockAppTest:testPacketTimeout() (gas: 2914422)
IBCMockAppTest:testHandshake() (gas: 3433206)
IBCMockAppTest:testHandshakeBetweenDifferentPorts() (gas: 2539275)
IBCMockAppTest:testPacketRelay() (gas: 9485102)
IBCMockAppTest:testPacketTimeout() (gas: 2906505)
IBCTest:testBenchmarkCreateMockClient() (gas: 209365)
IBCTest:testBenchmarkLCUpdateMockClient() (gas: 39945)
IBCTest:testBenchmarkRecvPacket() (gas: 130774)
IBCTest:testBenchmarkSendPacket() (gas: 84557)
IBCTest:testBenchmarkRecvPacket() (gas: 129941)
IBCTest:testBenchmarkSendPacket() (gas: 84051)
IBCTest:testBenchmarkUpdateMockClient() (gas: 137373)
IBCTest:testToUint128((uint64,uint64)) (runs: 256, μ: 947, ~: 947)
TestICS02:testCreateClient() (gas: 23300679)
TestICS02:testInvalidCreateClient() (gas: 23150346)
TestICS02:testInvalidUpdateClient() (gas: 23152169)
TestICS02:testRegisterClient() (gas: 22895198)
TestICS02:testRegisterClientDuplicatedClientType() (gas: 22878423)
TestICS02:testRegisterClientInvalidClientType() (gas: 22862552)
TestICS02:testUpdateClient() (gas: 23317721)
TestICS03Handshake:testConnOpenAck() (gas: 1631316)
TestICS03Handshake:testConnOpenConfirm() (gas: 1765734)
TestICS02:testCreateClient() (gas: 23157317)
TestICS02:testInvalidCreateClient() (gas: 23006914)
TestICS02:testInvalidUpdateClient() (gas: 23008761)
TestICS02:testRegisterClient() (gas: 22751790)
TestICS02:testRegisterClientDuplicatedClientType() (gas: 22735015)
TestICS02:testRegisterClientInvalidClientType() (gas: 22719144)
TestICS02:testUpdateClient() (gas: 23174313)
TestICS03Handshake:testConnOpenAck() (gas: 1631720)
TestICS03Handshake:testConnOpenConfirm() (gas: 1766228)
TestICS03Handshake:testConnOpenInit() (gas: 1279055)
TestICS03Handshake:testConnOpenTry() (gas: 2160463)
TestICS03Handshake:testInvalidConnOpenAck() (gas: 2031008)
TestICS03Handshake:testInvalidConnOpenConfirm() (gas: 2095567)
TestICS03Handshake:testConnOpenTry() (gas: 2160823)
TestICS03Handshake:testInvalidConnOpenAck() (gas: 2031544)
TestICS03Handshake:testInvalidConnOpenConfirm() (gas: 2096106)
TestICS03Handshake:testInvalidConnOpenInit() (gas: 666816)
TestICS03Handshake:testInvalidConnOpenTry() (gas: 2101014)
TestICS03Handshake:testInvalidConnOpenTry() (gas: 2101149)
TestICS03Version:testCopyVersions() (gas: 558658)
TestICS03Version:testFindSupportedVersion() (gas: 19400)
TestICS03Version:testIsSupportedVersion() (gas: 7864)
TestICS03Version:testPickVersion() (gas: 25327)
TestICS03Version:testVerifyProposedVersion() (gas: 11777)
TestICS03Version:testVerifySupportedFeature() (gas: 4153)
TestICS04Handshake:testBindPort() (gas: 38248)
TestICS04Handshake:testChanClose() (gas: 8477568)
TestICS04Handshake:testChanOpenAck() (gas: 2882343)
TestICS04Handshake:testChanOpenConfirm() (gas: 3062123)
TestICS04Handshake:testChanOpenInit() (gas: 2197889)
TestICS04Handshake:testChanOpenTry() (gas: 2656386)
TestICS04Handshake:testInvalidChanOpenAck() (gas: 2062174)
TestICS04Handshake:testInvalidChanOpenConfirm() (gas: 2118130)
TestICS04Handshake:testInvalidChanOpenInit() (gas: 1276375)
TestICS04Handshake:testInvalidChanOpenTry() (gas: 1352538)
TestICS04Packet:testAcknowledgementPacket() (gas: 2284000)
TestICS04Packet:testInvalidSendPacket() (gas: 2288945)
TestICS04Packet:testRecvPacket() (gas: 7669339)
TestICS04Packet:testRecvPacketTimeoutHeight() (gas: 2327617)
TestICS04Packet:testRecvPacketTimeoutTimestamp() (gas: 2326594)
TestICS04Packet:testSendPacket() (gas: 5082327)
TestICS04Packet:testTimeoutOnClose() (gas: 2546246)
TestICS04Handshake:testBindPort() (gas: 37414)
TestICS04Handshake:testChanClose() (gas: 8457966)
TestICS04Handshake:testChanOpenAck() (gas: 2879243)
TestICS04Handshake:testChanOpenConfirm() (gas: 3058457)
TestICS04Handshake:testChanOpenInit() (gas: 2195918)
TestICS04Handshake:testChanOpenTry() (gas: 2653852)
TestICS04Handshake:testInvalidChanOpenAck() (gas: 2060952)
TestICS04Handshake:testInvalidChanOpenConfirm() (gas: 2116580)
TestICS04Handshake:testInvalidChanOpenInit() (gas: 1275939)
TestICS04Handshake:testInvalidChanOpenTry() (gas: 1352198)
TestICS04Packet:testAcknowledgementPacket() (gas: 2276942)
TestICS04Packet:testInvalidSendPacket() (gas: 2274601)
TestICS04Packet:testRecvPacket() (gas: 7645640)
TestICS04Packet:testRecvPacketTimeoutHeight() (gas: 2320869)
TestICS04Packet:testRecvPacketTimeoutTimestamp() (gas: 2319828)
TestICS04Packet:testSendPacket() (gas: 5067297)
TestICS04Packet:testTimeoutOnClose() (gas: 2539008)
TestICS20:testAddressToHex(address) (runs: 256, μ: 22676, ~: 22804)
TestICS20:testHexToAddress(string) (runs: 256, μ: 4776, ~: 4734)
TestICS20:testIsEscapedString() (gas: 48979)
Expand Down
4 changes: 2 additions & 2 deletions contracts/core/04-channel/IBCChannelHandshake.sol
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ contract IBCChannelHandshake is IBCModuleManager, IIBCChannelHandshake, IIBCChan
version: msg_.channel.version
})
);
claimCapability(channelCapabilityPath(msg_.portId, channelId), address(module));
claimChannelCapability(msg_.portId, channelId, address(module));
writeChannel(
msg_.portId,
channelId,
Expand Down Expand Up @@ -138,7 +138,7 @@ contract IBCChannelHandshake is IBCModuleManager, IIBCChannelHandshake, IIBCChan
counterpartyVersion: msg_.counterpartyVersion
})
);
claimCapability(channelCapabilityPath(msg_.portId, channelId), address(module));
claimChannelCapability(msg_.portId, channelId, address(module));
writeChannel(
msg_.portId,
channelId,
Expand Down
4 changes: 2 additions & 2 deletions contracts/core/04-channel/IBCChannelPacketSendRecv.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ contract IBCChannelPacketSendRecv is
uint64 timeoutTimestamp,
bytes calldata data
) external returns (uint64) {
authenticateCapability(channelCapabilityPath(sourcePort, sourceChannel));
authenticateChannelCapability(sourcePort, sourceChannel);

Channel.Data storage channel = channels[sourcePort][sourceChannel];
if (channel.state != Channel.State.STATE_OPEN) {
Expand Down Expand Up @@ -92,7 +92,7 @@ contract IBCChannelPacketSendRecv is
uint64 sequence,
bytes calldata acknowledgement
) public {
authenticateCapability(channelCapabilityPath(destinationPortId, destinationChannel));
authenticateChannelCapability(destinationPortId, destinationChannel);
Channel.Data storage channel = channels[destinationPortId][destinationChannel];
if (channel.state != Channel.State.STATE_OPEN) {
revert IBCChannelUnexpectedChannelState(channel.state);
Expand Down
2 changes: 1 addition & 1 deletion contracts/core/24-host/IBCHostConfigurator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ abstract contract IBCHostConfigurator is IIBCHostConfigurator, IBCModuleManager
if (address(moduleAddress) == address(0) || address(moduleAddress) == address(this)) {
revert IBCHostInvalidModuleAddress(address(moduleAddress));
}
claimCapability(portCapabilityPath(portId), address(moduleAddress));
claimPortCapability(portId, address(moduleAddress));
}
}
3 changes: 2 additions & 1 deletion contracts/core/24-host/IBCStore.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ abstract contract IBCStore {
mapping(string => mapping(string => uint64)) internal nextSequenceSends;
mapping(string => mapping(string => uint64)) internal nextSequenceRecvs;
mapping(string => mapping(string => uint64)) internal nextSequenceAcks;
mapping(string => address) internal capabilities;
mapping(string => address) internal portCapabilities;
mapping(string => mapping(string => address)) internal channelCapabilities;

// Host parameters
uint64 internal expectedTimePerBlock;
Expand Down
21 changes: 15 additions & 6 deletions contracts/core/24-host/IIBCHostErrors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,22 @@ interface IIBCHostErrors {
/// @param moduleAddress module contract address
error IBCHostInvalidModuleAddress(address moduleAddress);

/// @param name module name
error IBCHostModuleNotFound(string name);
/// @param portId port identifier
error IBCHostModulePortNotFound(string portId);

/// @param portId port identifier
/// @param channelId channel identifier
error IBCHostModuleChannelNotFound(string portId, string channelId);

/// @param name capability name
error IBCHostCapabilityAlreadyClaimed(string name);
/// @param portId port identifier
error IBCHostPortCapabilityAlreadyClaimed(string portId);

/// @param name capability name
/// @param portId port identifier
/// @param channelId channel identifier
error IBCHostChannelCapabilityAlreadyClaimed(string portId, string channelId);

/// @param portId port identifier
/// @param channelId channel identifier
/// @param caller caller address
error IBCHostFailedAuthenticateCapability(string name, address caller);
error IBCHostFailedAuthenticateChannelCapability(string portId, string channelId, address caller);
}
60 changes: 21 additions & 39 deletions contracts/core/26-router/IBCModuleManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,73 +12,55 @@ contract IBCModuleManager is IBCHost, Context {
/**
* @dev lookupModuleByPort will return the IBCModule along with the capability associated with a given portID
*/
function lookupModuleByPort(string memory portId) internal view virtual returns (IIBCModule) {
address module = lookupModule(portCapabilityPath(portId));
function lookupModuleByPort(string calldata portId) internal view virtual returns (IIBCModule) {
address module = portCapabilities[portId];
if (module == address(0)) {
revert IBCHostModuleNotFound(portCapabilityPath(portId));
revert IBCHostModulePortNotFound(portId);
}
return IIBCModule(module);
}

/**
* @dev lookupModuleByChannel will return the IBCModule along with the capability associated with a given channel defined by its portID and channelID
*/
function lookupModuleByChannel(string memory portId, string memory channelId)
function lookupModuleByChannel(string calldata portId, string calldata channelId)
internal
view
virtual
returns (IIBCModule)
{
address module = lookupModule(channelCapabilityPath(portId, channelId));
address module = channelCapabilities[portId][channelId];
if (module == address(0)) {
revert IBCHostModuleNotFound(channelCapabilityPath(portId, channelId));
revert IBCHostModuleChannelNotFound(portId, channelId);
}
return IIBCModule(module);
}

/**
* @dev claimCapability allows the IBC app module to claim a capability that core IBC passes to it
*/
function claimCapability(string memory name, address addr) internal {
if (capabilities[name] != address(0)) {
revert IBCHostCapabilityAlreadyClaimed(name);
function claimPortCapability(string calldata portId, address addr) internal {
if (portCapabilities[portId] != address(0)) {
revert IBCHostPortCapabilityAlreadyClaimed(portId);
}
capabilities[name] = addr;
portCapabilities[portId] = addr;
}

/**
* @dev authenticateCapability attempts to authenticate a given name from a caller.
* It allows for a caller to check that a capability does in fact correspond to a particular name.
* @dev claimCapability allows the IBC app module to claim a capability that core IBC passes to it
*/
function authenticateCapability(string memory name) internal view {
if (capabilities[name] != _msgSender()) {
revert IBCHostFailedAuthenticateCapability(name, _msgSender());
function claimChannelCapability(string calldata portId, string memory channelId, address addr) internal {
if (channelCapabilities[portId][channelId] != address(0)) {
revert IBCHostChannelCapabilityAlreadyClaimed(portId, channelId);
}
channelCapabilities[portId][channelId] = addr;
}

/**
* @dev lookupModule will return the IBCModule addresses bound to a given name.
*/
function lookupModule(string memory name) internal view returns (address) {
return capabilities[name];
}

/**
* @dev portCapabilityPath returns the path under which owner module address associated with a port should be stored.
*/
function portCapabilityPath(string memory portId) internal pure returns (string memory) {
return portId;
}

/**
* @dev channelCapabilityPath returns the path under which module address associated with a port and channel should be stored.
* @dev authenticateChannelCapability attempts to authenticate a given name from a caller.
* It allows for a caller to check that a capability does in fact correspond to a particular name.
*/
function channelCapabilityPath(string memory portId, string memory channelId)
internal
pure
returns (string memory)
{
return string.concat(portId, "/", channelId);
function authenticateChannelCapability(string calldata portId, string calldata channelId) internal view {
if (channelCapabilities[portId][channelId] != _msgSender()) {
revert IBCHostFailedAuthenticateChannelCapability(portId, channelId, _msgSender());
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion pkg/contract/ibchandler/ibchandler.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions tests/foundry/src/IBC.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ contract IBCTest is Test {
function setUpMockApp() internal {
mockApp = new IBCMockApp(handler);
handler.bindPort(MOCK_PORT_ID, mockApp);
handler.setCapability(string.concat(MOCK_PORT_ID, "/channel-0"), address(mockApp));
handler.setChannelCapability(MOCK_PORT_ID, "channel-0", address(mockApp));
}

/* test cases */
Expand All @@ -116,7 +116,7 @@ contract IBCTest is Test {
}

function testBenchmarkSendPacket() public {
handler.setCapability(string.concat(MOCK_PORT_ID, "/channel-0"), address(this));
handler.setChannelCapability(MOCK_PORT_ID, "channel-0", address(this));
Packet memory packet = createPacket(0, 100);
handler.sendPacket(
packet.sourcePort, packet.sourceChannel, packet.timeoutHeight, packet.timeoutTimestamp, packet.data
Expand Down
4 changes: 2 additions & 2 deletions tests/foundry/src/helpers/TestableIBCHandler.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ contract TestableIBCHandler is OwnableIBCHandler {
);
}

function setCapability(string calldata name, address addr) external {
capabilities[name] = addr;
function setChannelCapability(string calldata portId, string calldata channelId, address addr) external {
channelCapabilities[portId][channelId] = addr;
}

function getPacketCommitment(string calldata portId, string calldata channelId, uint64 sequence)
Expand Down

0 comments on commit b4b9745

Please sign in to comment.