Skip to content

Commit ee66e23

Browse files
authored
Merge pull request #293 from hyperledger-labs/upgradable-module
Move `IBCChannelUpgradableModule` into `apps/commons` Signed-off-by: Jun Kimura <jun.kimura@datachain.jp>
2 parents 9b5036a + 6b6cf96 commit ee66e23

File tree

6 files changed

+127
-115
lines changed

6 files changed

+127
-115
lines changed

tests/foundry/src/helpers/IBCChannelUpgradableModule.sol renamed to contracts/apps/commons/IBCChannelUpgradableModule.sol

Lines changed: 7 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,11 @@
11
// SPDX-License-Identifier: Apache-2.0
22
pragma solidity ^0.8.20;
33

4-
import {Channel, UpgradeFields, Timeout} from "../../../../contracts/proto/Channel.sol";
5-
import {IIBCHandler} from "../../../../contracts/core/25-handler/IIBCHandler.sol";
6-
import {IIBCModuleUpgrade} from "../../../../contracts/core/26-router/IIBCModuleUpgrade.sol";
7-
import {AppBase} from "../../../../contracts/apps/commons/IBCAppBase.sol";
8-
9-
interface IIBCChannelUpgradableModuleErrors {
10-
// ------------------- Errors ------------------- //
11-
12-
error IBCChannelUpgradableModuleUnauthorizedUpgrader();
13-
error IBCChannelUpgradableModuleInvalidTimeout();
14-
error IBCChannelUpgradableModuleInvalidConnectionHops();
15-
error IBCChannelUpgradableModuleUpgradeAlreadyExists();
16-
error IBCChannelUpgradableModuleUpgradeNotFound();
17-
error IBCChannelUpgradableModuleInvalidUpgrade();
18-
19-
error IBCChannelUpgradableModuleCannotRemoveInProgressUpgrade();
20-
/// @param state The current state of the channel
21-
error IBCChannelUpgradableModuleChannelNotFlushingState(Channel.State state);
22-
/// @param actual The actual upgrade sequence
23-
error IBCChannelUpgradableModuleSequenceMismatch(uint64 actual);
24-
25-
error IBCChannelUpgradableModuleChannelNotFound();
26-
error IBCChannelUpgradableModuleCannotOverwriteUpgrade();
27-
}
28-
29-
interface IIBCChannelUpgradableModule {
30-
// ------------------- Data Structures ------------------- //
31-
32-
/**
33-
* @dev Proposed upgrade fields
34-
* @param fields Upgrade fields
35-
* @param timeout Absolute timeout for the upgrade
36-
*/
37-
struct UpgradeProposal {
38-
UpgradeFields.Data fields;
39-
Timeout.Data timeout;
40-
}
41-
42-
/**
43-
* @dev Allowed transition for the channel upgrade
44-
* @param flushComplete Whether the upgrade is allowed to transition to the flush complete state
45-
*/
46-
struct AllowedTransition {
47-
bool flushComplete;
48-
}
49-
50-
// ------------------- Functions ------------------- //
51-
52-
/**
53-
* @dev Returns the proposed upgrade for the given port, channel, and sequence
54-
*/
55-
function getUpgradeProposal(string calldata portId, string calldata channelId)
56-
external
57-
view
58-
returns (UpgradeProposal memory);
59-
60-
/**
61-
* @dev Propose an upgrade for the given port, channel, and sequence
62-
* @notice This function is only callable by an authorized upgrader
63-
* The upgrader must call this function before calling `channelUpgradeInit` or `channelUpgradeTry` of the IBC handler
64-
*/
65-
function proposeUpgrade(
66-
string calldata portId,
67-
string calldata channelId,
68-
UpgradeFields.Data calldata upgradeFields,
69-
Timeout.Data calldata timeout
70-
) external;
71-
72-
/**
73-
* @dev Removes the proposed upgrade for the given port and channel
74-
* @notice This function is only callable by an authorized upgrader
75-
* @param portId Port identifier
76-
* @param channelId Channel identifier
77-
*/
78-
function removeUpgradeProposal(string calldata portId, string calldata channelId) external;
79-
80-
/**
81-
* @dev Allow the upgrade to transition to the flush complete state
82-
* @notice This function is only callable by an authorized upgrader
83-
* WARNING: Before calling this function, the upgrader must ensure that all inflight packets have been received on the receiving chain,
84-
* and all acknowledgements written have been acknowledged on the sending chain
85-
*/
86-
function allowTransitionToFlushComplete(string calldata portId, string calldata channelId, uint64 upgradeSequence)
87-
external;
88-
}
4+
import {Channel, UpgradeFields, Timeout} from "../../proto/Channel.sol";
5+
import {IIBCHandler} from "../../core/25-handler/IIBCHandler.sol";
6+
import {IIBCModuleUpgrade} from "../../core/26-router/IIBCModuleUpgrade.sol";
7+
import {AppBase} from "./IBCAppBase.sol";
8+
import {IIBCChannelUpgradableModule, IIBCChannelUpgradableModuleErrors} from "./IIBCChannelUpgradableModule.sol";
899

9010
abstract contract IBCChannelUpgradableModuleBase is
9111
AppBase,
@@ -312,10 +232,8 @@ abstract contract IBCChannelUpgradableModuleBase is
312232
* @dev See {IERC165-supportsInterface}
313233
*/
314234
function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
315-
return
316-
super.supportsInterface(interfaceId) ||
317-
interfaceId == type(IIBCModuleUpgrade).interfaceId ||
318-
interfaceId == type(IIBCChannelUpgradableModule).interfaceId;
235+
return super.supportsInterface(interfaceId) || interfaceId == type(IIBCModuleUpgrade).interfaceId
236+
|| interfaceId == type(IIBCChannelUpgradableModule).interfaceId;
319237
}
320238

321239
// ------------------- Internal Functions ------------------- //
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
pragma solidity ^0.8.20;
3+
4+
import {Channel, UpgradeFields, Timeout} from "../../proto/Channel.sol";
5+
6+
interface IIBCChannelUpgradableModuleErrors {
7+
// ------------------- Errors ------------------- //
8+
9+
error IBCChannelUpgradableModuleUnauthorizedUpgrader();
10+
error IBCChannelUpgradableModuleInvalidTimeout();
11+
error IBCChannelUpgradableModuleInvalidConnectionHops();
12+
error IBCChannelUpgradableModuleUpgradeAlreadyExists();
13+
error IBCChannelUpgradableModuleUpgradeNotFound();
14+
error IBCChannelUpgradableModuleInvalidUpgrade();
15+
16+
error IBCChannelUpgradableModuleCannotRemoveInProgressUpgrade();
17+
/// @param state The current state of the channel
18+
error IBCChannelUpgradableModuleChannelNotFlushingState(Channel.State state);
19+
/// @param actual The actual upgrade sequence
20+
error IBCChannelUpgradableModuleSequenceMismatch(uint64 actual);
21+
22+
error IBCChannelUpgradableModuleChannelNotFound();
23+
error IBCChannelUpgradableModuleCannotOverwriteUpgrade();
24+
}
25+
26+
interface IIBCChannelUpgradableModule {
27+
// ------------------- Data Structures ------------------- //
28+
29+
/**
30+
* @dev Proposed upgrade fields
31+
* @param fields Upgrade fields
32+
* @param timeout Absolute timeout for the upgrade
33+
*/
34+
struct UpgradeProposal {
35+
UpgradeFields.Data fields;
36+
Timeout.Data timeout;
37+
}
38+
39+
/**
40+
* @dev Allowed transition for the channel upgrade
41+
* @param flushComplete Whether the upgrade is allowed to transition to the flush complete state
42+
*/
43+
struct AllowedTransition {
44+
bool flushComplete;
45+
}
46+
47+
// ------------------- Functions ------------------- //
48+
49+
/**
50+
* @dev Returns the proposed upgrade for the given port, channel, and sequence
51+
*/
52+
function getUpgradeProposal(string calldata portId, string calldata channelId)
53+
external
54+
view
55+
returns (UpgradeProposal memory);
56+
57+
/**
58+
* @dev Propose an upgrade for the given port, channel, and sequence
59+
* @notice This function is only callable by an authorized upgrader
60+
* The upgrader must call this function before calling `channelUpgradeInit` or `channelUpgradeTry` of the IBC handler
61+
*/
62+
function proposeUpgrade(
63+
string calldata portId,
64+
string calldata channelId,
65+
UpgradeFields.Data calldata upgradeFields,
66+
Timeout.Data calldata timeout
67+
) external;
68+
69+
/**
70+
* @dev Removes the proposed upgrade for the given port and channel
71+
* @notice This function is only callable by an authorized upgrader
72+
* @param portId Port identifier
73+
* @param channelId Channel identifier
74+
*/
75+
function removeUpgradeProposal(string calldata portId, string calldata channelId) external;
76+
77+
/**
78+
* @dev Allow the upgrade to transition to the flush complete state
79+
* @notice This function is only callable by an authorized upgrader
80+
* WARNING: Before calling this function, the upgrader must ensure that all inflight packets have been received on the receiving chain,
81+
* and all acknowledgements written have been acknowledged on the sending chain
82+
*/
83+
function allowTransitionToFlushComplete(string calldata portId, string calldata channelId, uint64 upgradeSequence)
84+
external;
85+
}

tests/foundry/src/helpers/TestIBCChannelUpgradableMockApp.t.sol renamed to contracts/apps/mock/IBCChannelUpgradableMockApp.sol

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
11
// SPDX-License-Identifier: Apache-2.0
22
pragma solidity ^0.8.20;
33

4-
import {Upgrade, UpgradeFields, Timeout} from "../../../../contracts/proto/Channel.sol";
5-
import {
6-
IIBCChannelUpgrade, IIBCChannelUpgradeBase
7-
} from "../../../../contracts/core/04-channel/IIBCChannelUpgrade.sol";
8-
import {IIBCHandler} from "../../../../contracts/core/25-handler/IIBCHandler.sol";
9-
import {IBCMockApp} from "../../../../contracts/apps/mock/IBCMockApp.sol";
10-
import {IBCChannelUpgradableModuleBase} from "./IBCChannelUpgradableModule.sol";
11-
import {IBCAppBase} from "../../../../contracts/apps/commons/IBCAppBase.sol";
4+
import {UpgradeFields, Timeout} from "../../proto/Channel.sol";
5+
import {IIBCChannelUpgradeBase} from "../../core/04-channel/IIBCChannelUpgrade.sol";
6+
import {IIBCHandler} from "../../core/25-handler/IIBCHandler.sol";
7+
import {IBCMockApp} from "./IBCMockApp.sol";
8+
import {IBCChannelUpgradableModuleBase} from "../commons/IBCChannelUpgradableModule.sol";
9+
import {IBCAppBase} from "../commons/IBCAppBase.sol";
1210

13-
contract TestIBCChannelUpgradableMockApp is IBCMockApp, IBCChannelUpgradableModuleBase {
11+
contract IBCChannelUpgradableMockApp is IBCMockApp, IBCChannelUpgradableModuleBase {
1412
constructor(IIBCHandler ibcHandler_) IBCMockApp(ibcHandler_) {}
1513

16-
function supportsInterface(bytes4 interfaceId) public view virtual override(IBCChannelUpgradableModuleBase, IBCAppBase) returns (bool) {
17-
return
18-
super.supportsInterface(interfaceId) || interfaceId == this.proposeAndInitUpgrade.selector;
14+
/**
15+
* @dev See {IERC165-supportsInterface}.
16+
*/
17+
function supportsInterface(bytes4 interfaceId)
18+
public
19+
view
20+
virtual
21+
override(IBCChannelUpgradableModuleBase, IBCAppBase)
22+
returns (bool)
23+
{
24+
return super.supportsInterface(interfaceId) || interfaceId == this.proposeAndInitUpgrade.selector;
1925
}
2026

27+
/**
28+
* @dev Propose upgrade and perform chanUpgradeInit.
29+
*/
2130
function proposeAndInitUpgrade(
2231
string calldata portId,
2332
string calldata channelId,

tests/foundry/src/ICS04Upgrade.t.sol

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ import {LocalhostClientLib} from "../../../contracts/clients/09-localhost/Localh
99
import {LocalhostHelper} from "../../../contracts/clients/09-localhost/LocalhostHelper.sol";
1010
import {IIBCChannelRecvPacket, IIBCChannelAcknowledgePacket} from "../../../contracts/core/04-channel/IIBCChannel.sol";
1111
import {IIBCChannelUpgradeBase} from "../../../contracts/core/04-channel/IIBCChannelUpgrade.sol";
12+
import {IBCChannelUpgradableMockApp} from "../../../contracts/apps/mock/IBCChannelUpgradableMockApp.sol";
1213
import {IIBCHostErrors} from "../../../contracts/core/24-host/IIBCHostErrors.sol";
13-
import {TestIBCChannelUpgradableMockApp} from "./helpers/TestIBCChannelUpgradableMockApp.t.sol";
1414
import {TestIBCChannelUpgradableMockAppInconsistentVersions} from "./helpers/TestIBCChannelUpgradableMockAppInconsistentVersions.t.sol";
1515
import {ICS04UpgradeTestHelper} from "./helpers/ICS04UpgradeTestHelper.t.sol";
1616
import {ICS04PacketEventTestHelper} from "./helpers/ICS04PacketTestHelper.t.sol";
17-
import {IIBCChannelUpgradableModule} from "./helpers/IBCChannelUpgradableModule.sol";
17+
import {
18+
IIBCChannelUpgradableModule
19+
} from "../../../contracts/apps/commons/IBCChannelUpgradableModule.sol";
1820
import {IBCMockLib} from "../../../contracts/apps/mock/IBCMockLib.sol";
1921
import {IBCMockApp} from "../../../contracts/apps/mock/IBCMockApp.sol";
2022

@@ -26,7 +28,7 @@ contract TestICS04Upgrade is ICS04UpgradeTestHelper, ICS04PacketEventTestHelper
2628
string internal constant MOCK_APP_VERSION_2 = "mockapp-2";
2729

2830
TestableIBCHandler ibcHandler;
29-
TestIBCChannelUpgradableMockApp mockApp;
31+
IBCChannelUpgradableMockApp mockApp;
3032
TestIBCChannelUpgradableMockAppInconsistentVersions maliciousMockApp;
3133

3234
struct ChannelInfo {
@@ -37,7 +39,7 @@ contract TestICS04Upgrade is ICS04UpgradeTestHelper, ICS04PacketEventTestHelper
3739

3840
function setUp() public {
3941
ibcHandler = defaultIBCHandler();
40-
mockApp = new TestIBCChannelUpgradableMockApp(ibcHandler);
42+
mockApp = new IBCChannelUpgradableMockApp(ibcHandler);
4143
maliciousMockApp = new TestIBCChannelUpgradableMockAppInconsistentVersions(ibcHandler);
4244
ibcHandler.bindPort(MOCK_APP_PORT, mockApp);
4345
ibcHandler.registerLocalhostClient();

tests/foundry/src/ICS04UpgradeApp.t.sol

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@ pragma solidity ^0.8.20;
33

44
import "./helpers/IBCTestHelper.t.sol";
55
import {Vm} from "forge-std/Test.sol";
6-
import {Upgrade, UpgradeFields, Timeout} from "../../../contracts/proto/Channel.sol";
7-
import {LocalhostClientLib} from "../../../contracts/clients/09-localhost/LocalhostClient.sol";
6+
import {UpgradeFields, Timeout} from "../../../contracts/proto/Channel.sol";
87
import {LocalhostHelper} from "../../../contracts/clients/09-localhost/LocalhostHelper.sol";
9-
import {IIBCChannelUpgrade} from "../../../contracts/core/04-channel/IIBCChannelUpgrade.sol";
10-
import {TestIBCChannelUpgradableMockApp} from "./helpers/TestIBCChannelUpgradableMockApp.t.sol";
118
import {
129
IIBCChannelUpgradableModule, IIBCChannelUpgradableModuleErrors
13-
} from "./helpers/IBCChannelUpgradableModule.sol";
10+
} from "../../../contracts/apps/commons/IBCChannelUpgradableModule.sol";
11+
import {IBCChannelUpgradableMockApp} from "../../../contracts/apps/mock/IBCChannelUpgradableMockApp.sol";
1412
import {ICS04UpgradeTestHelper} from "./helpers/ICS04UpgradeTestHelper.t.sol";
1513

1614
contract TestICS04UpgradeApp is ICS04UpgradeTestHelper {
@@ -21,7 +19,7 @@ contract TestICS04UpgradeApp is ICS04UpgradeTestHelper {
2119
string internal constant MOCK_APP_VERSION_2 = "mockapp-2";
2220

2321
TestableIBCHandler ibcHandler;
24-
TestIBCChannelUpgradableMockApp mockApp;
22+
IBCChannelUpgradableMockApp mockApp;
2523

2624
struct ChannelInfo {
2725
string connectionId;
@@ -31,7 +29,7 @@ contract TestICS04UpgradeApp is ICS04UpgradeTestHelper {
3129

3230
function setUp() public {
3331
ibcHandler = defaultIBCHandler();
34-
mockApp = new TestIBCChannelUpgradableMockApp(ibcHandler);
32+
mockApp = new IBCChannelUpgradableMockApp(ibcHandler);
3533
ibcHandler.bindPort(MOCK_APP_PORT, mockApp);
3634
ibcHandler.registerLocalhostClient();
3735
ibcHandler.createLocalhostClient();

tests/foundry/src/helpers/TestIBCChannelUpgradableMockAppInconsistentVersions.t.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ pragma solidity ^0.8.20;
33

44
import {UpgradeFields} from "../../../../contracts/proto/Channel.sol";
55
import {TestableIBCHandler} from "./TestableIBCHandler.t.sol";
6-
import {TestIBCChannelUpgradableMockApp} from "./TestIBCChannelUpgradableMockApp.t.sol";
6+
import {IBCChannelUpgradableMockApp} from "../../../../contracts/apps/mock/IBCChannelUpgradableMockApp.sol";
77

8-
contract TestIBCChannelUpgradableMockAppInconsistentVersions is TestIBCChannelUpgradableMockApp {
9-
constructor(TestableIBCHandler _ibcHandler) TestIBCChannelUpgradableMockApp(_ibcHandler) {}
8+
contract TestIBCChannelUpgradableMockAppInconsistentVersions is IBCChannelUpgradableMockApp {
9+
constructor(TestableIBCHandler _ibcHandler) IBCChannelUpgradableMockApp(_ibcHandler) {}
1010

1111
function onChanUpgradeInit(
1212
string calldata portId,

0 commit comments

Comments
 (0)