Skip to content

Commit

Permalink
Merge pull request #249 from hyperledger-labs/improve-tests
Browse files Browse the repository at this point in the history
Bump minimal solidity version to v0.8.20 and improve tests

Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele authored Feb 14, 2024
2 parents 4d0d3e9 + fd56278 commit 72824ca
Show file tree
Hide file tree
Showing 67 changed files with 2,695 additions and 1,551 deletions.
80 changes: 43 additions & 37 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,43 +1,49 @@
IBCTest:testBenchmarkCreateMockClient() (gas: 209319)
IBCTest:testBenchmarkCreateMockClient() (gas: 209341)
IBCTest:testBenchmarkLCUpdateMockClient() (gas: 39935)
IBCTest:testBenchmarkRecvPacket() (gas: 136859)
IBCTest:testBenchmarkSendPacket() (gas: 85154)
IBCTest:testBenchmarkUpdateMockClient() (gas: 137315)
IBCTest:testBenchmarkRecvPacket() (gas: 137264)
IBCTest:testBenchmarkSendPacket() (gas: 85111)
IBCTest:testBenchmarkUpdateMockClient() (gas: 137380)
IBCTest:testToUint128((uint64,uint64)) (runs: 256, μ: 947, ~: 947)
TestICS02:testCreateClient() (gas: 24329382)
TestICS02:testInvalidCreateClient() (gas: 24177244)
TestICS02:testInvalidUpdateClient() (gas: 24179521)
TestICS02:testRegisterClient() (gas: 23924001)
TestICS02:testRegisterClientDuplicatedClientType() (gas: 23906051)
TestICS02:testRegisterClientInvalidClientType() (gas: 23890923)
TestICS02:testUpdateClient() (gas: 24345834)
TestICS03Handshake:testConnOpenAck() (gas: 1631428)
TestICS03Handshake:testConnOpenConfirm() (gas: 1764503)
TestICS03Handshake:testConnOpenInit() (gas: 1279747)
TestICS03Handshake:testConnOpenTry() (gas: 2124860)
TestICS03Handshake:testInvalidConnOpenAck() (gas: 2001640)
TestICS03Handshake:testInvalidConnOpenConfirm() (gas: 2007413)
TestICS03Handshake:testInvalidConnOpenInit() (gas: 666565)
TestICS03Handshake:testInvalidConnOpenTry() (gas: 2110651)
TestICS03Version:testCopyVersions() (gas: 558658)
TestICS03Version:testFindSupportedVersion() (gas: 19400)
TestICS03Version:testIsSupportedVersion() (gas: 7864)
TestICS03Version:testPickVersion() (gas: 25399)
TestICS03Version:testVerifyProposedVersion() (gas: 11777)
TestICS03Version:testVerifySupportedFeature() (gas: 4153)
TestICS02:testCreateClient() (gas: 24828609)
TestICS02:testInvalidCreateClient() (gas: 24676246)
TestICS02:testInvalidUpdateClient() (gas: 24679444)
TestICS02:testRegisterClient() (gas: 24423230)
TestICS02:testRegisterClientDuplicatedClientType() (gas: 24405280)
TestICS02:testRegisterClientInvalidClientType() (gas: 24390152)
TestICS02:testUpdateClient() (gas: 24845663)
TestICS03Handshake:testConnOpenAck() (gas: 1636277)
TestICS03Handshake:testConnOpenConfirm() (gas: 1772310)
TestICS03Handshake:testConnOpenInit() (gas: 1281190)
TestICS03Handshake:testConnOpenTry() (gas: 2166092)
TestICS03Handshake:testInvalidConnOpenAck() (gas: 1951642)
TestICS03Handshake:testInvalidConnOpenConfirm() (gas: 2017044)
TestICS03Handshake:testInvalidConnOpenInit() (gas: 666722)
TestICS03Handshake:testInvalidConnOpenTry() (gas: 2079624)
TestICS03Version:testCopyVersions() (gas: 560134)
TestICS03Version:testFindSupportedVersion() (gas: 19740)
TestICS03Version:testIsSupportedVersion() (gas: 8272)
TestICS03Version:testPickVersion() (gas: 26823)
TestICS03Version:testVerifyProposedVersion() (gas: 12185)
TestICS03Version:testVerifySupportedFeature() (gas: 4357)
TestICS04Handshake:testBindPort() (gas: 40333)
TestICS04Handshake:testChanOpenAck() (gas: 2881255)
TestICS04Handshake:testChanOpenConfirm() (gas: 3061061)
TestICS04Handshake:testChanOpenInit() (gas: 2197808)
TestICS04Handshake:testChanOpenTry() (gas: 2655241)
TestICS04Handshake:testInvalidChanOpenAck() (gas: 2059020)
TestICS04Handshake:testInvalidChanOpenConfirm() (gas: 2117109)
TestICS04Handshake:testInvalidChanOpenInit() (gas: 1273682)
TestICS04Handshake:testInvalidChanOpenTry() (gas: 1347330)
TestICS04Packet:testInvalidSendPacket() (gas: 2207885)
TestICS04Packet:testSendPacket() (gas: 2264031)
TestICS20:testAddressToHex(address) (runs: 256, μ: 22673, ~: 22824)
TestICS04Handshake:testChanClose() (gas: 8493283)
TestICS04Handshake:testChanOpenAck() (gas: 2886058)
TestICS04Handshake:testChanOpenConfirm() (gas: 3067098)
TestICS04Handshake:testChanOpenInit() (gas: 2199573)
TestICS04Handshake:testChanOpenTry() (gas: 2658841)
TestICS04Handshake:testInvalidChanOpenAck() (gas: 2062335)
TestICS04Handshake:testInvalidChanOpenConfirm() (gas: 2120446)
TestICS04Handshake:testInvalidChanOpenInit() (gas: 1275021)
TestICS04Handshake:testInvalidChanOpenTry() (gas: 1350509)
TestICS04Packet:testAcknowledgementPacket() (gas: 2286474)
TestICS04Packet:testInvalidSendPacket() (gas: 2297971)
TestICS04Packet:testRecvPacket() (gas: 7973268)
TestICS04Packet:testRecvPacketTimeoutHeight() (gas: 2323693)
TestICS04Packet:testRecvPacketTimeoutTimestamp() (gas: 2322328)
TestICS04Packet:testSendPacket() (gas: 5793531)
TestICS04Packet:testTimeoutOnClose() (gas: 2512437)
TestICS20:testAddressToHex(address) (runs: 256, μ: 22698, ~: 22824)
TestICS20:testHexToAddress(string) (runs: 256, μ: 4776, ~: 4734)
TestICS20:testIsEscapedString() (gas: 48979)
TestICS20:testMarshaling() (gas: 148145)
TestICS20:testParseAmount(uint256) (runs: 256, μ: 26683, ~: 21388)
TestICS20:testParseAmount(uint256) (runs: 256, μ: 27053, ~: 22172)
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
pull_request:

env:
MINIMAL_SOLC_VERSION: 0.8.12
MINIMAL_SOLC_VERSION: 0.8.20

jobs:
contract-test:
Expand Down
2 changes: 1 addition & 1 deletion contracts/apps/20-transfer/ERC20Token.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.12;
pragma solidity ^0.8.20;

import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";

Expand Down
2 changes: 1 addition & 1 deletion contracts/apps/20-transfer/ICS20Bank.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.12;
pragma solidity ^0.8.20;

import {Context} from "@openzeppelin/contracts/utils/Context.sol";
import {AccessControl} from "@openzeppelin/contracts/access/AccessControl.sol";
Expand Down
2 changes: 1 addition & 1 deletion contracts/apps/20-transfer/ICS20Lib.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.12;
pragma solidity ^0.8.20;

import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";

Expand Down
2 changes: 1 addition & 1 deletion contracts/apps/20-transfer/ICS20Transfer.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.12;
pragma solidity ^0.8.20;

import {IBCAppBase} from "../commons/IBCAppBase.sol";
import {IIBCModule} from "../../core/26-router/IIBCModule.sol";
Expand Down
2 changes: 1 addition & 1 deletion contracts/apps/20-transfer/ICS20TransferBank.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.12;
pragma solidity ^0.8.20;

import {Height} from "../../proto/Client.sol";
import {IIBCHandler} from "../../core/25-handler/IIBCHandler.sol";
Expand Down
2 changes: 1 addition & 1 deletion contracts/apps/20-transfer/IICS20Bank.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.12;
pragma solidity ^0.8.20;

interface IICS20Bank {
/**
Expand Down
2 changes: 1 addition & 1 deletion contracts/apps/commons/IBCAppBase.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.12;
pragma solidity ^0.8.20;

import {Context} from "@openzeppelin/contracts/utils/Context.sol";
import {Packet} from "../../proto/Channel.sol";
Expand Down
52 changes: 43 additions & 9 deletions contracts/apps/mock/IBCMockApp.sol
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.12;
pragma solidity ^0.8.20;

import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {Height} from "../../proto/Client.sol";
import {Packet} from "../../proto/Channel.sol";
import {IBCAppBase} from "../commons/IBCAppBase.sol";
import {IIBCModule} from "../../core/26-router/IIBCModule.sol";
import {IIBCHandler} from "../../core/25-handler/IIBCHandler.sol";
import {IBCMockLib} from "./IBCMockLib.sol";

contract IBCMockApp is IBCAppBase {
contract IBCMockApp is IBCAppBase, Ownable {
string public constant MOCKAPP_VERSION = "mockapp-1";

IIBCHandler immutable ibcHandler;

bool public closeChannelAllowed = true;

constructor(IIBCHandler ibcHandler_) {
ibcHandler = ibcHandler_;
}
Expand All @@ -31,15 +34,28 @@ contract IBCMockApp is IBCAppBase {
return ibcHandler.sendPacket(sourcePort, sourceChannel, timeoutHeight, timeoutTimestamp, message);
}

function writeAcknowledgement(string calldata destinationPort, string calldata destinationChannel, uint64 sequence)
external
{
ibcHandler.writeAcknowledgement(
destinationPort, destinationChannel, sequence, IBCMockLib.SUCCESSFUL_ASYNC_ACKNOWLEDGEMENT_JSON
);
}

function allowCloseChannel(bool allow) public onlyOwner {
closeChannelAllowed = allow;
}

function onRecvPacket(Packet.Data calldata packet, address)
external
view
override
onlyIBC
returns (bytes memory acknowledgement)
{
if (keccak256(packet.data) == keccak256(IBCMockLib.MOCK_PACKET_DATA)) {
if (equals(packet.data, IBCMockLib.MOCK_PACKET_DATA)) {
return IBCMockLib.SUCCESSFUL_ACKNOWLEDGEMENT_JSON;
} else if (keccak256(packet.data) == keccak256(IBCMockLib.MOCK_ASYNC_PACKET_DATA)) {
} else if (equals(packet.data, IBCMockLib.MOCK_ASYNC_PACKET_DATA)) {
return bytes("");
} else {
return IBCMockLib.FAILED_ACKNOWLEDGEMENT_JSON;
Expand All @@ -52,12 +68,14 @@ contract IBCMockApp is IBCAppBase {
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);
if (equals(packet.data, IBCMockLib.MOCK_PACKET_DATA)) {
require(equals(acknowledgement, IBCMockLib.SUCCESSFUL_ACKNOWLEDGEMENT_JSON), "invalid ack");
} else if (equals(packet.data, IBCMockLib.MOCK_ASYNC_PACKET_DATA)) {
require(equals(acknowledgement, IBCMockLib.SUCCESSFUL_ASYNC_ACKNOWLEDGEMENT_JSON), "invalid async ack");
} else if (equals(packet.data, IBCMockLib.MOCK_FAIL_PACKET_DATA)) {
require(equals(acknowledgement, IBCMockLib.FAILED_ACKNOWLEDGEMENT_JSON), "invalid failed ack");
} else {
require(keccak256(acknowledgement) == keccak256(IBCMockLib.FAILED_ACKNOWLEDGEMENT_JSON));
revert("invalid packet data");
}
}

Expand Down Expand Up @@ -85,4 +103,20 @@ contract IBCMockApp is IBCAppBase {
require(keccak256(bytes(msg_.counterpartyVersion)) == keccak256(bytes(MOCKAPP_VERSION)), "version mismatch");
return MOCKAPP_VERSION;
}

function onChanCloseInit(IIBCModule.MsgOnChanCloseInit calldata) external virtual override onlyIBC {
require(closeChannelAllowed, "close not allowed");
}

function onChanCloseConfirm(IIBCModule.MsgOnChanCloseConfirm calldata) external virtual override onlyIBC {
require(closeChannelAllowed, "close not allowed");
}

function onTimeoutPacket(Packet.Data calldata, address) external view override onlyIBC {
require(closeChannelAllowed, "timeout not allowed");
}

function equals(bytes calldata a, bytes memory b) private pure returns (bool) {
return keccak256(a) == keccak256(b);
}
}
4 changes: 3 additions & 1 deletion contracts/apps/mock/IBCMockLib.sol
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.12;
pragma solidity ^0.8.20;

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 SUCCESSFUL_ASYNC_ACKNOWLEDGEMENT_JSON =
bytes('{"result":"bW9jayBhc3luYyBhY2tub3dsZWRnZW1lbnQ="}');
bytes public constant FAILED_ACKNOWLEDGEMENT_JSON = bytes('{"error":"mock failed acknowledgement"}');
}
2 changes: 1 addition & 1 deletion contracts/clients/IBFT2Client.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.12;
pragma solidity ^0.8.20;

import {ILightClient} from "../core/02-client/ILightClient.sol";
import {IBCHeight} from "../core/02-client/IBCHeight.sol";
Expand Down
8 changes: 4 additions & 4 deletions contracts/clients/MockClient.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.12;
pragma solidity ^0.8.20;

import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {ILightClient} from "../core/02-client/ILightClient.sol";
Expand Down Expand Up @@ -168,12 +168,12 @@ contract MockClient is Ownable, ILightClient {
uint64,
uint64,
bytes calldata proof,
bytes calldata prefix,
bytes memory
bytes memory prefix,
bytes memory path
) external view virtual override returns (bool) {
require(consensusStates[clientId][height.toUint128()].timestamp != 0, "consensus state not found");
require(keccak256(IIBCHandler(ibcHandler).getCommitmentPrefix()) == keccak256(prefix), "invalid prefix");
return proof.length == 0;
return sha256(abi.encodePacked(height.toUint128(), sha256(prefix), sha256(path), sha256(""))) == bytes32(proof);
}

/* State accessors */
Expand Down
2 changes: 1 addition & 1 deletion contracts/core/02-client/IBCClient.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.12;
pragma solidity ^0.8.20;

import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";
import {ILightClient} from "./ILightClient.sol";
Expand Down
2 changes: 1 addition & 1 deletion contracts/core/02-client/IBCClientLib.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.12;
pragma solidity ^0.8.20;

library IBCClientLib {
/**
Expand Down
2 changes: 1 addition & 1 deletion contracts/core/02-client/IBCHeight.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.12;
pragma solidity ^0.8.20;

import {Height} from "../../proto/Client.sol";

Expand Down
2 changes: 1 addition & 1 deletion contracts/core/02-client/IIBCClient.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.12;
pragma solidity ^0.8.20;

import {Height} from "../../proto/Client.sol";

Expand Down
2 changes: 1 addition & 1 deletion contracts/core/02-client/ILightClient.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.12;
pragma solidity ^0.8.20;

import {Height} from "../../proto/Client.sol";

Expand Down
2 changes: 1 addition & 1 deletion contracts/core/03-connection/IBCConnection.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.12;
pragma solidity ^0.8.20;

import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";
import {Height} from "../../proto/Client.sol";
Expand Down
2 changes: 1 addition & 1 deletion contracts/core/03-connection/IBCConnectionLib.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.12;
pragma solidity ^0.8.20;

import {Version} from "../../proto/Connection.sol";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.12;
pragma solidity ^0.8.20;

import {Height} from "../../proto/Client.sol";
import {IBCConnection} from "./IBCConnection.sol";
Expand Down
2 changes: 1 addition & 1 deletion contracts/core/03-connection/IIBCConnection.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.12;
pragma solidity ^0.8.20;

import {Height} from "../../proto/Client.sol";
import {Version, Counterparty} from "../../proto/Connection.sol";
Expand Down
2 changes: 1 addition & 1 deletion contracts/core/04-channel/IBCChannelHandshake.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.12;
pragma solidity ^0.8.20;

import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";
import {Height} from "../../proto/Client.sol";
Expand Down
2 changes: 1 addition & 1 deletion contracts/core/04-channel/IBCChannelLib.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.12;
pragma solidity ^0.8.20;

import {Channel} from "../../proto/Channel.sol";

Expand Down
2 changes: 1 addition & 1 deletion contracts/core/04-channel/IBCChannelPacketSendRecv.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.12;
pragma solidity ^0.8.20;

import {Height} from "../../proto/Client.sol";
import {ConnectionEnd} from "../../proto/Connection.sol";
Expand Down
2 changes: 1 addition & 1 deletion contracts/core/04-channel/IBCChannelPacketTimeout.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.12;
pragma solidity ^0.8.20;

import {Height} from "../../proto/Client.sol";
import {ConnectionEnd} from "../../proto/Connection.sol";
Expand Down
2 changes: 1 addition & 1 deletion contracts/core/04-channel/IIBCChannel.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.12;
pragma solidity ^0.8.20;

import {Height} from "../../proto/Client.sol";
import {Channel, Packet} from "../../proto/Channel.sol";
Expand Down
2 changes: 1 addition & 1 deletion contracts/core/24-host/IBCCommitment.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.12;
pragma solidity ^0.8.20;

import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";

Expand Down
2 changes: 1 addition & 1 deletion contracts/core/24-host/IBCHost.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.12;
pragma solidity ^0.8.20;

import {ILightClient} from "../02-client/ILightClient.sol";
import {IBCStore} from "../24-host/IBCStore.sol";
Expand Down
Loading

0 comments on commit 72824ca

Please sign in to comment.