Skip to content

Commit

Permalink
add upgradeable IBCHandler
Browse files Browse the repository at this point in the history
Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele committed Aug 28, 2024
1 parent cd49bc0 commit 250dd4e
Show file tree
Hide file tree
Showing 16 changed files with 2,018 additions and 41 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ jobs:
- name: Run tests
run: make test

- name: Run upgradeable tests
run: make TEST_UPGRADEABLE=true clean test

- name: Run coverage
run: make coverage

Expand Down Expand Up @@ -104,3 +107,6 @@ jobs:

- name: E2E test (QBFT)
run: make e2e-test network-down

- name: E2E test with upgredable IBCHandler (QBFT)
run: make TEST_UPGRADEABLE=true clean network-qbft e2e-test network-down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "lib/forge-std"]
path = tests/foundry/lib/forge-std
url = https://github.com/foundry-rs/forge-std
[submodule "tests/foundry/lib/openzeppelin-foundry-upgrades"]
path = tests/foundry/lib/openzeppelin-foundry-upgrades
url = https://github.com/OpenZeppelin/openzeppelin-foundry-upgrades
13 changes: 9 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ DOCKER_COMPOSE=$(DOCKER) compose
E2E_TEST_COMPOSE_FILE=./chains/compose.yml
TEST_BROADCAST_LOG_DIR=./broadcast/Deploy.s.sol
TEST_MNEMONIC="math razor capable expose worth grape metal sunset metal sudden usage scheme"
TEST_UPGRADEABLE=false

######## Development ########

.PHONY: build
build:
$(FORGE) build --sizes --skip test --use solc:$(SOLC_VERSION)

.PHONY: clean
clean:
$(FORGE) clean

.PHONY: fmt
fmt:
$(FORGE) fmt $(FORGE_FMT_OPTS)
Expand All @@ -27,7 +32,7 @@ lint:

.PHONY: test
test:
$(FORGE) test -vvvv --gas-report --isolate --use solc:$(SOLC_VERSION) $(FORGE_SNAPSHOT_OPTION)
TEST_UPGRADEABLE=$(TEST_UPGRADEABLE) $(FORGE) test -vvvv --gas-report --isolate --use solc:$(SOLC_VERSION) $(FORGE_SNAPSHOT_OPTION)

.PHONY: snapshot
snapshot:
Expand Down Expand Up @@ -88,9 +93,9 @@ network-qbft:

.PHONY: deploy
deploy:
TEST_MNEMONIC=$(TEST_MNEMONIC) $(FORGE) script --legacy --batch-size 5 --use solc:${SOLC_VERSION} --fork-url http://127.0.0.1:8645 --broadcast \
TEST_UPGRADEABLE=$(TEST_UPGRADEABLE) TEST_MNEMONIC=$(TEST_MNEMONIC) $(FORGE) script --legacy --batch-size 5 --use solc:${SOLC_VERSION} --fork-url http://127.0.0.1:8645 --broadcast \
./tests/foundry/src/Deploy.s.sol
TEST_MNEMONIC=$(TEST_MNEMONIC) $(FORGE) script --legacy --batch-size 5 --use solc:${SOLC_VERSION} --fork-url http://127.0.0.1:8745 --broadcast \
TEST_UPGRADEABLE=$(TEST_UPGRADEABLE) TEST_MNEMONIC=$(TEST_MNEMONIC) $(FORGE) script --legacy --batch-size 5 --use solc:${SOLC_VERSION} --fork-url http://127.0.0.1:8745 --broadcast \
./tests/foundry/src/Deploy.s.sol

.PHONY: network-down
Expand All @@ -99,4 +104,4 @@ network-down:

.PHONY: e2e-test
e2e-test:
TEST_MNEMONIC=$(TEST_MNEMONIC) TEST_BROADCAST_LOG_DIR=$(CURDIR)/$(TEST_BROADCAST_LOG_DIR) go test -v ./tests/e2e/... -count=1
TEST_UPGRADEABLE=$(TEST_UPGRADEABLE) TEST_MNEMONIC=$(TEST_MNEMONIC) TEST_BROADCAST_LOG_DIR=$(CURDIR)/$(TEST_BROADCAST_LOG_DIR) go test -v ./tests/e2e/... -count=1
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,19 @@ abstract contract IBCClientConnectionChannelHandler is
IIBCChannelPacketTimeout,
IIBCChannelUpgrade
{
/// @custom:oz-upgrades-unsafe-allow state-variable-immutable
address internal immutable ibcClient;
/// @custom:oz-upgrades-unsafe-allow state-variable-immutable
address internal immutable ibcConnection;
/// @custom:oz-upgrades-unsafe-allow state-variable-immutable
address internal immutable ibcChannelHandshake;
/// @custom:oz-upgrades-unsafe-allow state-variable-immutable
address internal immutable ibcChannelPacketSendRecv;
/// @custom:oz-upgrades-unsafe-allow state-variable-immutable
address internal immutable ibcChannelPacketTimeout;
/// @custom:oz-upgrades-unsafe-allow state-variable-immutable
address internal immutable ibcChannelUpgradeInitTryAck;
/// @custom:oz-upgrades-unsafe-allow state-variable-immutable
address internal immutable ibcChannelUpgradeConfirmOpenTimeoutCancel;

/**
Expand All @@ -48,6 +55,7 @@ abstract contract IBCClientConnectionChannelHandler is
* @param ibcChannelPacketTimeout_ is the address of a contract that implements `IIBCChannelPacketTimeout`.
* @param ibcChannelUpgradeInitTryAck_ is the address of a contract that implements `IIBCChannelUpgradeInitTryAck`.
* @param ibcChannelUpgradeConfirmOpenTimeoutCancel_ is the address of a contract that implements `IIBCChannelUpgradeConfirmOpenTimeoutCancel`.
* @custom:oz-upgrades-unsafe-allow constructor
*/
constructor(
IIBCClient ibcClient_,
Expand Down Expand Up @@ -84,6 +92,7 @@ abstract contract IBCClientConnectionChannelHandler is
}

function wrappedRouteUpdateClient(MsgUpdateClient calldata msg_) public returns (address, bytes4, bytes memory) {
/// @custom:oz-upgrades-unsafe-allow delegatecall
(bool success, bytes memory returndata) =
address(ibcClient).delegatecall(abi.encodeWithSelector(IIBCClient.routeUpdateClient.selector, msg_));
if (!success) {
Expand Down
1 change: 1 addition & 0 deletions contracts/core/25-handler/IBCHandler.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ abstract contract IBCHandler is IBCHostConfigurator, IBCClientConnectionChannelH
* @param ibcChannelPacketTimeout_ is the address of a contract that implements `IIBCChannelPacketTimeout`.
* @param ibcChannelUpgradeInitTryAck_ is the address of a contract that implements `IIBCChannelUpgrade`.
* @param ibcChannelUpgradeConfirmOpenTimeoutCancel_ is the address of a contract that implements `IIBCChannelUpgrade`.
* @custom:oz-upgrades-unsafe-allow constructor
*/
constructor(
IIBCClient ibcClient_,
Expand Down
83 changes: 83 additions & 0 deletions contracts/core/25-handler/OwnableUpgradeableIBCHandler.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.20;

import {ILightClient} from "../02-client/ILightClient.sol";
import {IIBCClient} from "../02-client/IIBCClient.sol";
import {IIBCConnection} from "../03-connection/IIBCConnection.sol";
import {
IIBCChannelHandshake, IIBCChannelPacketSendRecv, IIBCChannelPacketTimeout
} from "../04-channel/IIBCChannel.sol";
import {
IIBCChannelUpgradeInitTryAck,
IIBCChannelUpgradeConfirmOpenTimeoutCancel
} from "../04-channel/IIBCChannelUpgrade.sol";
import {IIBCModule} from "../26-router/IIBCModule.sol";
import {IBCHandler} from "./IBCHandler.sol";
import {Context} from "@openzeppelin/contracts/utils/Context.sol";
import {ContextUpgradeable} from "@openzeppelin/contracts-upgradeable/utils/ContextUpgradeable.sol";
import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import {OwnableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";

contract OwnableUpgradeableIBCHandler is IBCHandler, UUPSUpgradeable, OwnableUpgradeable {
/**
* @dev The arguments of constructor must satisfy the followings:
* @param ibcClient_ is the address of a contract that implements `IIBCClient`.
* @param ibcConnection_ is the address of a contract that implements `IIBCConnection`.
* @param ibcChannelHandshake_ is the address of a contract that implements `IIBCChannelHandshake`.
* @param ibcChannelPacketSendRecv_ is the address of a contract that implements `IIBCChannelPacketSendRecv`.
* @param ibcChannelPacketTimeout_ is the address of a contract that implements `IIBCChannelPacketTimeout`.
* @param ibcChannelUpgradeInitTryAck_ is the address of a contract that implements `IIBCChannelUpgrade`.
* @param ibcChannelUpgradeConfirmOpenTimeoutCancel_ is the address of a contract that implements `IIBCChannelUpgrade`.
* @custom:oz-upgrades-unsafe-allow constructor
*/
constructor(
IIBCClient ibcClient_,
IIBCConnection ibcConnection_,
IIBCChannelHandshake ibcChannelHandshake_,
IIBCChannelPacketSendRecv ibcChannelPacketSendRecv_,
IIBCChannelPacketTimeout ibcChannelPacketTimeout_,
IIBCChannelUpgradeInitTryAck ibcChannelUpgradeInitTryAck_,
IIBCChannelUpgradeConfirmOpenTimeoutCancel ibcChannelUpgradeConfirmOpenTimeoutCancel_
)
IBCHandler(
ibcClient_,
ibcConnection_,
ibcChannelHandshake_,
ibcChannelPacketSendRecv_,
ibcChannelPacketTimeout_,
ibcChannelUpgradeInitTryAck_,
ibcChannelUpgradeConfirmOpenTimeoutCancel_
)
{}

function initialize() public virtual initializer {
__UUPSUpgradeable_init();
__Ownable_init(msg.sender);
}

function registerClient(string calldata clientType, ILightClient client) public virtual onlyOwner {
super._registerClient(clientType, client);
}

function bindPort(string calldata portId, IIBCModule moduleAddress) public virtual onlyOwner {
super._bindPort(portId, moduleAddress);
}

function setExpectedTimePerBlock(uint64 expectedTimePerBlock_) public virtual onlyOwner {
super._setExpectedTimePerBlock(expectedTimePerBlock_);
}

function _msgSender() internal view virtual override(Context, ContextUpgradeable) returns (address) {
return ContextUpgradeable._msgSender();
}

function _msgData() internal view virtual override(Context, ContextUpgradeable) returns (bytes calldata) {
return ContextUpgradeable._msgData();
}

function _contextSuffixLength() internal view virtual override(Context, ContextUpgradeable) returns (uint256) {
return ContextUpgradeable._contextSuffixLength();
}

function _authorizeUpgrade(address newImplementation) internal virtual override onlyOwner {}
}
4 changes: 4 additions & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ gas_reports = ["*"]
optimizer = true
optimizer_runs = 9_999_999
via-ir = false
ffi = true
ast = true
build_info = true
extra_output = ["storageLayout"]

[fmt]
line_length = 120
Expand Down
Loading

0 comments on commit 250dd4e

Please sign in to comment.