Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
move validatePortIdentifier to IBCHostLib
* add port identifier validation test from ibc-go

Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele committed Jul 23, 2024
commit df4c49f84a9253e13806111abee8d2d9abee0c18
14 changes: 7 additions & 7 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ IBCTest:testBenchmarkRecvPacket() (gas: 158888)
IBCTest:testBenchmarkSendPacket() (gas: 128432)
IBCTest:testBenchmarkUpdateMockClient() (gas: 160229)
IBCTest:testToUint128((uint64,uint64)) (runs: 256, μ: 947, ~: 947)
TestICS02:testCreateClient() (gas: 36633805)
TestICS02:testInvalidCreateClient() (gas: 36530922)
TestICS02:testInvalidUpdateClient() (gas: 36529932)
TestICS02:testRegisterClient() (gas: 36185598)
TestICS02:testRegisterClientDuplicatedClientType() (gas: 36170907)
TestICS02:testRegisterClientInvalidClientType() (gas: 36200368)
TestICS02:testUpdateClient() (gas: 36698132)
TestICS02:testCreateClient() (gas: 36633817)
TestICS02:testInvalidCreateClient() (gas: 36530934)
TestICS02:testInvalidUpdateClient() (gas: 36529944)
TestICS02:testRegisterClient() (gas: 36185610)
TestICS02:testRegisterClientDuplicatedClientType() (gas: 36170919)
TestICS02:testRegisterClientInvalidClientType() (gas: 36200380)
TestICS02:testUpdateClient() (gas: 36698144)
TestICS03Handshake:testConnOpenAck() (gas: 1858230)
TestICS03Handshake:testConnOpenConfirm() (gas: 2054143)
TestICS03Handshake:testConnOpenInit() (gas: 1429838)
Expand Down
3 changes: 2 additions & 1 deletion contracts/core/24-host/IBCHostConfigurator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pragma solidity ^0.8.20;
import {ERC165Checker} from "@openzeppelin/contracts/utils/introspection/ERC165Checker.sol";
import {IBCClientLib} from "../02-client/IBCClientLib.sol";
import {ILightClient} from "../02-client/ILightClient.sol";
import {IBCHostLib} from "./IBCHostLib.sol";
import {IBCModuleManager} from "../26-router/IBCModuleManager.sol";
import {IIBCModule} from "../26-router/IIBCModule.sol";
import {IIBCHostConfigurator} from "./IIBCHostConfigurator.sol";
Expand All @@ -30,7 +31,7 @@ abstract contract IBCHostConfigurator is IIBCHostConfigurator, IBCModuleManager

function _bindPort(string calldata portId, IIBCModule module) internal virtual {
address moduleAddress = address(module);
if (!validatePortIdentifier(bytes(portId))) {
if (!IBCHostLib.validatePortIdentifier(bytes(portId))) {
revert IBCHostInvalidPortIdentifier(portId);
}
if (moduleAddress == address(0) || moduleAddress == address(this)) {
Expand Down
37 changes: 37 additions & 0 deletions contracts/core/24-host/IBCHostLib.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.20;

library IBCHostLib {
/**
* @dev validatePortIdentifier validates a port identifier string
* check if the string consist of characters in one of the following categories only:
* - Alphanumeric
* - `.`, `_`, `+`, `-`, `#`
* - `[`, `]`, `<`, `>`
*/
function validatePortIdentifier(bytes memory portId) internal pure returns (bool) {
if (portId.length < 2 || portId.length > 128) {
return false;
}
unchecked {
for (uint256 i = 0; i < portId.length; i++) {
uint256 c = uint256(uint8(portId[i]));
if (
// a-z
(c >= 0x61 && c <= 0x7A)
// 0-9
|| (c >= 0x30 && c <= 0x39)
// A-Z
|| (c >= 0x41 && c <= 0x5A)
// ".", "_", "+", "-"
|| (c == 0x2E || c == 0x5F || c == 0x2B || c == 0x2D)
// "#", "[", "]", "<", ">"
|| (c == 0x23 || c == 0x5B || c == 0x5D || c == 0x3C || c == 0x3E)
) {
continue;
}
}
}
return true;
}
}
33 changes: 0 additions & 33 deletions contracts/core/26-router/IBCModuleManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -112,37 +112,4 @@ contract IBCModuleManager is IBCHost, Context {
portId, channelId, upgradeSequence, _msgSender()
);
}

/**
* @dev validatePortIdentifier validates a port identifier string
* check if the string consist of characters in one of the following categories only:
* - Alphanumeric
* - `.`, `_`, `+`, `-`, `#`
* - `[`, `]`, `<`, `>`
*/
function validatePortIdentifier(bytes memory portId) internal pure returns (bool) {
if (portId.length < 2 || portId.length > 128) {
return false;
}
unchecked {
for (uint256 i = 0; i < portId.length; i++) {
uint256 c = uint256(uint8(portId[i]));
if (
// a-z
(c >= 0x61 && c <= 0x7A)
// 0-9
|| (c >= 0x30 && c <= 0x39)
// A-Z
|| (c >= 0x41 && c <= 0x5A)
// ".", "_", "+", "-"
|| (c == 0x2E || c == 0x5F || c == 0x2B || c == 0x2D)
// "#", "[", "]", "<", ">"
|| (c == 0x23 || c == 0x5B || c == 0x5D || c == 0x3C || c == 0x3E)
) {
continue;
}
}
}
return true;
}
}
89 changes: 89 additions & 0 deletions tests/foundry/src/ICS24Host.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.20;

import "./helpers/IBCTestHelper.t.sol";
import {IBCHostLib} from "../../../contracts/core/24-host/IBCHostLib.sol";

contract ICS24HostTest is IBCTestHelper {
struct ValidatePortIdentifierTestCase {
string m;
string id;
bool expPass;
}

function testValidatePortIdentifier() public {
// The following test cases are based on the test cases of ibc-go:
// https://github.com/cosmos/ibc-go/blob/e443a88e0f2c84c131c5a1de47945a5733ff9c91/modules/core/24-host/validate_test.go#L57
ValidatePortIdentifierTestCase[] memory testCases = new ValidatePortIdentifierTestCase[](12);
testCases[0] = ValidatePortIdentifierTestCase({
m: "valid lowercase",
id: "transfer",
expPass: true
});
testCases[1] = ValidatePortIdentifierTestCase({
m: "valid id special chars",
id: "._+-#[]<>._+-#[]<>",
expPass: true
});
testCases[2] = ValidatePortIdentifierTestCase({
m: "valid id lower and special chars",
id: "lower._+-#[]<>",
expPass: true
});
testCases[3] = ValidatePortIdentifierTestCase({
m: "numeric id",
id: "1234567890",
expPass: true
});
testCases[4] = ValidatePortIdentifierTestCase({
m: "uppercase id",
id: "NOTLOWERCASE",
expPass: true
});
testCases[5] = ValidatePortIdentifierTestCase({
m: "numeric id",
id: "1234567890",
expPass: true
});
testCases[6] = ValidatePortIdentifierTestCase({
m: "blank id",
id: " ",
expPass: false
});
testCases[7] = ValidatePortIdentifierTestCase({
m: "id length out of range",
id: "1",
expPass: false
});
testCases[8] = ValidatePortIdentifierTestCase({
m: "id is too long",
id: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis eros neque, ultricies vel ligula ac, convallis porttitor elit. Maecenas tincidunt turpis elit, vel faucibus nisl pellentesque sodales",
expPass: false
});
testCases[9] = ValidatePortIdentifierTestCase({
m: "path-like id",
id: "lower/case/id",
expPass: false
});
testCases[10] = ValidatePortIdentifierTestCase({
m: "invalid id",
id: "(clientid)",
expPass: false
});
testCases[11] = ValidatePortIdentifierTestCase({
m: "empty string",
id: "",
expPass: false
});

for (uint i = 0; i < testCases.length; i++) {
ValidatePortIdentifierTestCase memory tc = testCases[i];
bool res = IBCHostLib.validatePortIdentifier(bytes(tc.id));
if (tc.expPass) {
require(res, tc.m);
} else {
require(!res, tc.m);
}
}
}
}