Skip to content

Commit cd49bc0

Browse files
authored
Merge pull request #295 from hyperledger-labs/eip-7201
Utilize EIP-7201 as storage layout Signed-off-by: Jun Kimura <jun.kimura@datachain.jp>
2 parents 32c3918 + e483a95 commit cd49bc0

File tree

21 files changed

+514
-359
lines changed

21 files changed

+514
-359
lines changed

.gas-snapshot

Lines changed: 62 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,75 @@
1-
IBCBenchmarks:testAcknowledgePacket() (gas: 98678)
2-
IBCBenchmarks:testCreateMockClient() (gas: 217244)
3-
IBCBenchmarks:testRecvPacket() (gas: 155487)
4-
IBCBenchmarks:testSendPacket() (gas: 95329)
1+
IBCBenchmarks:testAcknowledgePacket() (gas: 98040)
2+
IBCBenchmarks:testCreateMockClient() (gas: 216997)
3+
IBCBenchmarks:testRecvPacket() (gas: 154889)
4+
IBCBenchmarks:testSendPacket() (gas: 94843)
55
IBCBenchmarks:testUpdateMockClientDirectly() (gas: 64879)
6-
IBCBenchmarks:testUpdateMockClientViaHandler() (gas: 148205)
7-
IBCMockAppTest:testHandshake() (gas: 4343205)
8-
IBCMockAppTest:testHandshakeBetweenDifferentPorts() (gas: 3286459)
9-
IBCMockAppTest:testPacketRelay() (gas: 11692905)
10-
IBCMockAppTest:testPacketTimeout() (gas: 4242242)
6+
IBCBenchmarks:testUpdateMockClientViaHandler() (gas: 148239)
7+
IBCMockAppTest:testHandshake() (gas: 4161353)
8+
IBCMockAppTest:testHandshakeBetweenDifferentPorts() (gas: 3104214)
9+
IBCMockAppTest:testPacketRelay() (gas: 11460317)
10+
IBCMockAppTest:testPacketTimeout() (gas: 4059417)
1111
ICS24HostTest:testValidatePortIdentifier() (gas: 44476)
12-
TestICS02:testCreateClient() (gas: 30153096)
12+
TestICS02:testCreateClient() (gas: 30572814)
1313
TestICS02:testHeightToUint128((uint64,uint64)) (runs: 256, μ: 907, ~: 907)
14-
TestICS02:testInvalidCreateClient() (gas: 30038762)
15-
TestICS02:testInvalidUpdateClient() (gas: 30042040)
16-
TestICS02:testRegisterClient() (gas: 29702122)
17-
TestICS02:testRegisterClientDuplicatedClientType() (gas: 29688342)
18-
TestICS02:testRegisterClientInvalidClientType() (gas: 29717304)
19-
TestICS02:testUpdateClient() (gas: 30208564)
20-
TestICS03Handshake:testConnOpenAck() (gas: 1811332)
21-
TestICS03Handshake:testConnOpenConfirm() (gas: 1989221)
22-
TestICS03Handshake:testConnOpenInit() (gas: 1423613)
23-
TestICS03Handshake:testConnOpenTry() (gas: 2348126)
24-
TestICS03Handshake:testInvalidConnOpenAck() (gas: 2323078)
25-
TestICS03Handshake:testInvalidConnOpenConfirm() (gas: 2421885)
26-
TestICS03Handshake:testInvalidConnOpenInit() (gas: 777600)
27-
TestICS03Handshake:testInvalidConnOpenTry() (gas: 2283933)
14+
TestICS02:testInvalidCreateClient() (gas: 30458130)
15+
TestICS02:testInvalidUpdateClient() (gas: 30461817)
16+
TestICS02:testRegisterClient() (gas: 30122086)
17+
TestICS02:testRegisterClientDuplicatedClientType() (gas: 30108306)
18+
TestICS02:testRegisterClientInvalidClientType() (gas: 30137268)
19+
TestICS02:testUpdateClient() (gas: 30628415)
20+
TestICS03Handshake:testConnOpenAck() (gas: 1810871)
21+
TestICS03Handshake:testConnOpenConfirm() (gas: 1988723)
22+
TestICS03Handshake:testConnOpenInit() (gas: 1422940)
23+
TestICS03Handshake:testConnOpenTry() (gas: 2347689)
24+
TestICS03Handshake:testInvalidConnOpenAck() (gas: 2322689)
25+
TestICS03Handshake:testInvalidConnOpenConfirm() (gas: 2421514)
26+
TestICS03Handshake:testInvalidConnOpenInit() (gas: 776893)
27+
TestICS03Handshake:testInvalidConnOpenTry() (gas: 2283346)
2828
TestICS03Version:testCopyVersions() (gas: 570207)
2929
TestICS03Version:testFindSupportedVersion() (gas: 34452)
3030
TestICS03Version:testIsSupportedVersion() (gas: 13568)
3131
TestICS03Version:testPickVersion() (gas: 37836)
3232
TestICS03Version:testVerifyProposedVersion() (gas: 21308)
3333
TestICS03Version:testVerifySupportedFeature() (gas: 10229)
34-
TestICS04Handshake:testBindPort() (gas: 456271)
35-
TestICS04Handshake:testChanClose() (gas: 12800640)
36-
TestICS04Handshake:testChanOpenAck() (gas: 3428597)
37-
TestICS04Handshake:testChanOpenConfirm() (gas: 3724273)
38-
TestICS04Handshake:testChanOpenInit() (gas: 2535741)
39-
TestICS04Handshake:testChanOpenTry() (gas: 3083807)
40-
TestICS04Handshake:testInvalidChanOpenAck() (gas: 2411670)
41-
TestICS04Handshake:testInvalidChanOpenConfirm() (gas: 2486327)
42-
TestICS04Handshake:testInvalidChanOpenInit() (gas: 1730517)
43-
TestICS04Handshake:testInvalidChanOpenTry() (gas: 1742638)
44-
TestICS04Packet:testAcknowledgementPacket() (gas: 3316365)
45-
TestICS04Packet:testInvalidSendPacket() (gas: 3476451)
46-
TestICS04Packet:testRecvPacket() (gas: 9965223)
47-
TestICS04Packet:testRecvPacketTimeoutHeight() (gas: 3236945)
48-
TestICS04Packet:testRecvPacketTimeoutTimestamp() (gas: 3261000)
49-
TestICS04Packet:testSendPacket() (gas: 4583983)
50-
TestICS04Packet:testTimeoutOnClose() (gas: 3488144)
51-
TestICS04Upgrade:testCrossingHelloInconsistentVersions() (gas: 9918550)
52-
TestICS04Upgrade:testUpgradeAuthorityCancel() (gas: 46157666)
53-
TestICS04Upgrade:testUpgradeCannotCancelWithOldErrorReceipt() (gas: 3392338)
54-
TestICS04Upgrade:testUpgradeCannotRecvNextUpgradePacket() (gas: 5230053)
55-
TestICS04Upgrade:testUpgradeCounterpartyAdvanceNextSequenceBeforeOpen() (gas: 5191254)
56-
TestICS04Upgrade:testUpgradeCrossingHelloIncompatibleProposals() (gas: 4944387)
57-
TestICS04Upgrade:testUpgradeFull() (gas: 55984957)
58-
TestICS04Upgrade:testUpgradeInit() (gas: 3018393)
59-
TestICS04Upgrade:testUpgradeNoChanges() (gas: 2433786)
60-
TestICS04Upgrade:testUpgradeNotUpgradableModule() (gas: 3533495)
61-
TestICS04Upgrade:testUpgradeOutOfSync() (gas: 3826003)
62-
TestICS04Upgrade:testUpgradeRelaySuccessAtCounterpartyFlushComplete() (gas: 5201982)
63-
TestICS04Upgrade:testUpgradeRelaySuccessAtFlushing() (gas: 5575834)
64-
TestICS04Upgrade:testUpgradeSendPacketFailAtFlushingOrFlushComplete() (gas: 4012928)
65-
TestICS04Upgrade:testUpgradeTimeoutAbortAck() (gas: 17445807)
66-
TestICS04Upgrade:testUpgradeTimeoutAbortConfirm() (gas: 21062598)
67-
TestICS04Upgrade:testUpgradeTimeoutUpgrade() (gas: 70448064)
68-
TestICS04Upgrade:testUpgradeToOrdered() (gas: 53937015)
69-
TestICS04Upgrade:testUpgradeToUnordered() (gas: 43053273)
34+
TestICS04Handshake:testBindPort() (gas: 456448)
35+
TestICS04Handshake:testChanClose() (gas: 12159113)
36+
TestICS04Handshake:testChanOpenAck() (gas: 3242588)
37+
TestICS04Handshake:testChanOpenConfirm() (gas: 3538396)
38+
TestICS04Handshake:testChanOpenInit() (gas: 2395187)
39+
TestICS04Handshake:testChanOpenTry() (gas: 2897666)
40+
TestICS04Handshake:testInvalidChanOpenAck() (gas: 2317011)
41+
TestICS04Handshake:testInvalidChanOpenConfirm() (gas: 2391668)
42+
TestICS04Handshake:testInvalidChanOpenInit() (gas: 1677250)
43+
TestICS04Handshake:testInvalidChanOpenTry() (gas: 1692203)
44+
TestICS04Packet:testAcknowledgementPacket() (gas: 3111231)
45+
TestICS04Packet:testInvalidSendPacket() (gas: 3294851)
46+
TestICS04Packet:testRecvPacket() (gas: 9485947)
47+
TestICS04Packet:testRecvPacketTimeoutHeight() (gas: 3053625)
48+
TestICS04Packet:testRecvPacketTimeoutTimestamp() (gas: 3077674)
49+
TestICS04Packet:testSendPacket() (gas: 4393823)
50+
TestICS04Packet:testTimeoutOnClose() (gas: 3304892)
51+
TestICS04Upgrade:testCrossingHelloInconsistentVersions() (gas: 9730721)
52+
TestICS04Upgrade:testUpgradeAuthorityCancel() (gas: 44972623)
53+
TestICS04Upgrade:testUpgradeCannotCancelWithOldErrorReceipt() (gas: 3296637)
54+
TestICS04Upgrade:testUpgradeCannotRecvNextUpgradePacket() (gas: 5124643)
55+
TestICS04Upgrade:testUpgradeCounterpartyAdvanceNextSequenceBeforeOpen() (gas: 5087713)
56+
TestICS04Upgrade:testUpgradeCrossingHelloIncompatibleProposals() (gas: 4843978)
57+
TestICS04Upgrade:testUpgradeFull() (gas: 55696695)
58+
TestICS04Upgrade:testUpgradeInit() (gas: 2925072)
59+
TestICS04Upgrade:testUpgradeNoChanges() (gas: 2342343)
60+
TestICS04Upgrade:testUpgradeNotUpgradableModule() (gas: 3442666)
61+
TestICS04Upgrade:testUpgradeOutOfSync() (gas: 3727867)
62+
TestICS04Upgrade:testUpgradeRelaySuccessAtCounterpartyFlushComplete() (gas: 5097250)
63+
TestICS04Upgrade:testUpgradeRelaySuccessAtFlushing() (gas: 5469407)
64+
TestICS04Upgrade:testUpgradeSendPacketFailAtFlushingOrFlushComplete() (gas: 3907425)
65+
TestICS04Upgrade:testUpgradeTimeoutAbortAck() (gas: 17323669)
66+
TestICS04Upgrade:testUpgradeTimeoutAbortConfirm() (gas: 20920588)
67+
TestICS04Upgrade:testUpgradeTimeoutUpgrade() (gas: 68891924)
68+
TestICS04Upgrade:testUpgradeToOrdered() (gas: 52805680)
69+
TestICS04Upgrade:testUpgradeToUnordered() (gas: 42151776)
7070
TestICS04UpgradeApp:testUpgradeAuthorizationChanneNotFound() (gas: 62062)
71-
TestICS04UpgradeApp:testUpgradeAuthorizationRePropose() (gas: 2456047)
72-
TestICS04UpgradeApp:testUpgradeAuthorizationRemove() (gas: 2436473)
71+
TestICS04UpgradeApp:testUpgradeAuthorizationRePropose() (gas: 2365121)
72+
TestICS04UpgradeApp:testUpgradeAuthorizationRemove() (gas: 2345567)
7373
TestICS20:testAddressToHex(address) (runs: 256, μ: 26850, ~: 27044)
7474
TestICS20:testHexToAddress(string) (runs: 256, μ: 4636, ~: 4595)
7575
TestICS20:testIsEscapedString() (gas: 62745)

contracts/clients/qbft/QBFTClient.sol

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ contract QBFTClient is ILightClient, ILightClientErrors {
7171
bytes32 internal constant CONSENSUS_STATE_TYPE_URL_HASH =
7272
keccak256(abi.encodePacked("/ibc.lightclients.qbft.v1.ConsensusState"));
7373

74-
uint256 internal constant COMMITMENT_SLOT = 0;
74+
// keccak256(abi.encode(uint256(keccak256("ibc.commitment")) - 1)) & ~bytes32(uint256(0xff))
75+
bytes32 internal constant COMMITMENT_SLOT = 0x1ee222554989dda120e26ecacf756fe1235cd8d726706b57517715dde4f0c900;
7576
uint8 internal constant ACCOUNT_STORAGE_ROOT_INDEX = 2;
7677

7778
address public immutable ibcHandler;

contracts/core/02-client/IBCClient.sol

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,18 @@ contract IBCClient is IBCHost, IIBCClient, IIBCClientErrors {
1717
* @dev createClient creates a new client state and populates it with a given consensus state
1818
*/
1919
function createClient(MsgCreateClient calldata msg_) external override returns (string memory clientId) {
20-
address clientImpl = clientRegistry[msg_.clientType];
20+
address clientImpl = getHostStorage().clientRegistry[msg_.clientType];
2121
if (clientImpl == address(0)) {
2222
revert IBCClientUnregisteredClientType(msg_.clientType);
2323
}
2424
clientId = generateClientIdentifier(msg_.clientType);
25-
clientTypes[clientId] = msg_.clientType;
26-
clientImpls[clientId] = clientImpl;
25+
ClientStorage storage client = getClientStorage()[clientId];
26+
client.clientType = msg_.clientType;
27+
client.clientImpl = clientImpl;
2728
Height.Data memory height =
2829
ILightClient(clientImpl).initializeClient(clientId, msg_.protoClientState, msg_.protoConsensusState);
2930
// update commitments
31+
mapping(bytes32 => bytes32) storage commitments = getCommitments();
3032
commitments[IBCCommitment.clientStateCommitmentKey(clientId)] = keccak256(msg_.protoClientState);
3133
commitments[IBCCommitment.consensusStateCommitmentKey(clientId, height.revision_number, height.revision_height)]
3234
= keccak256(msg_.protoConsensusState);
@@ -83,6 +85,7 @@ contract IBCClient is IBCHost, IIBCClient, IIBCClientErrors {
8385
if (!found) {
8486
revert IBCClientClientNotFound(clientId);
8587
}
88+
mapping(bytes32 => bytes32) storage commitments = getCommitments();
8689
commitments[IBCCommitment.clientStateCommitmentKey(clientId)] = keccak256(clientState);
8790
for (uint256 i = 0; i < heights.length; i++) {
8891
(consensusState, found) = lc.getConsensusState(clientId, heights[i]);
@@ -102,9 +105,11 @@ contract IBCClient is IBCHost, IIBCClient, IIBCClientErrors {
102105
/**
103106
* @dev generateClientIdentifier generates a new client identifier for a given client type
104107
*/
105-
function generateClientIdentifier(string calldata clientType) private returns (string memory) {
106-
string memory identifier = string(abi.encodePacked(clientType, "-", Strings.toString(nextClientSequence)));
107-
nextClientSequence++;
108+
function generateClientIdentifier(string calldata clientType) internal returns (string memory) {
109+
HostStorage storage hostStorage = getHostStorage();
110+
string memory identifier =
111+
string(abi.encodePacked(clientType, "-", Strings.toString(hostStorage.nextClientSequence)));
112+
hostStorage.nextClientSequence++;
108113
return identifier;
109114
}
110115
}

contracts/core/03-connection/IBCConnection.sol

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ abstract contract IBCConnection is IBCHost, IBCSelfStateValidator, IIBCConnectio
2626
returns (string memory)
2727
{
2828
string memory connectionId = generateConnectionIdentifier();
29-
ConnectionEnd.Data storage connection = connections[connectionId];
29+
ConnectionEnd.Data storage connection = getConnectionStorage()[connectionId].connection;
3030
if (connection.state != ConnectionEnd.State.STATE_UNINITIALIZED_UNSPECIFIED) {
3131
revert IBCConnectionAlreadyConnectionExists();
3232
}
@@ -71,7 +71,7 @@ abstract contract IBCConnection is IBCHost, IBCSelfStateValidator, IIBCConnectio
7171
bytes memory selfConsensusState = getSelfConsensusState(msg_.consensusHeight, msg_.hostConsensusStateProof);
7272

7373
string memory connectionId = generateConnectionIdentifier();
74-
ConnectionEnd.Data storage connection = connections[connectionId];
74+
ConnectionEnd.Data storage connection = getConnectionStorage()[connectionId].connection;
7575
if (connection.state != ConnectionEnd.State.STATE_UNINITIALIZED_UNSPECIFIED) {
7676
revert IBCConnectionAlreadyConnectionExists();
7777
}
@@ -120,7 +120,7 @@ abstract contract IBCConnection is IBCHost, IBCSelfStateValidator, IIBCConnectio
120120
* to chain A (this code is executed on chain A).
121121
*/
122122
function connectionOpenAck(IIBCConnection.MsgConnectionOpenAck calldata msg_) external override {
123-
ConnectionEnd.Data storage connection = connections[msg_.connectionId];
123+
ConnectionEnd.Data storage connection = getConnectionStorage()[msg_.connectionId].connection;
124124
if (connection.state != ConnectionEnd.State.STATE_INIT) {
125125
revert IBCConnectionUnexpectedConnectionState(connection.state);
126126
}
@@ -169,7 +169,7 @@ abstract contract IBCConnection is IBCHost, IBCSelfStateValidator, IIBCConnectio
169169
* which the connection is open on both chains (this code is executed on chain B).
170170
*/
171171
function connectionOpenConfirm(IIBCConnection.MsgConnectionOpenConfirm calldata msg_) external override {
172-
ConnectionEnd.Data storage connection = connections[msg_.connectionId];
172+
ConnectionEnd.Data storage connection = getConnectionStorage()[msg_.connectionId].connection;
173173
if (connection.state != ConnectionEnd.State.STATE_TRYOPEN) {
174174
revert IBCConnectionUnexpectedConnectionState(connection.state);
175175
}
@@ -194,12 +194,29 @@ abstract contract IBCConnection is IBCHost, IBCSelfStateValidator, IIBCConnectio
194194
updateConnectionCommitment(msg_.connectionId);
195195
}
196196

197-
function updateConnectionCommitment(string memory connectionId) private {
198-
commitments[IBCCommitment.connectionCommitmentKey(connectionId)] =
199-
keccak256(ConnectionEnd.encode(connections[connectionId]));
197+
/**
198+
* @dev getCompatibleVersions returns the supported versions of the host chain.
199+
*/
200+
function getCompatibleVersions() public pure virtual returns (Version.Data[] memory) {
201+
Version.Data[] memory versions = new Version.Data[](1);
202+
versions[0] = IBCConnectionLib.defaultIBCVersion();
203+
return versions;
200204
}
201205

202-
/* Verification functions */
206+
// --------- Private Functions --------- //
207+
208+
function generateConnectionIdentifier() private returns (string memory) {
209+
HostStorage storage hostStorage = getHostStorage();
210+
string memory identifier =
211+
string(abi.encodePacked("connection-", Strings.toString(hostStorage.nextConnectionSequence)));
212+
hostStorage.nextConnectionSequence++;
213+
return identifier;
214+
}
215+
216+
function updateConnectionCommitment(string memory connectionId) private {
217+
getCommitments()[IBCCommitment.connectionCommitmentKey(connectionId)] =
218+
keccak256(ConnectionEnd.encode(getConnectionStorage()[connectionId].connection));
219+
}
203220

204221
function verifyClientState(
205222
ConnectionEnd.Data storage connection,
@@ -294,21 +311,4 @@ abstract contract IBCConnection is IBCHost, IBCSelfStateValidator, IIBCConnectio
294311
height
295312
);
296313
}
297-
298-
/**
299-
* @dev getCompatibleVersions returns the supported versions of the host chain.
300-
*/
301-
function getCompatibleVersions() public pure virtual returns (Version.Data[] memory) {
302-
Version.Data[] memory versions = new Version.Data[](1);
303-
versions[0] = IBCConnectionLib.defaultIBCVersion();
304-
return versions;
305-
}
306-
307-
/* Internal functions */
308-
309-
function generateConnectionIdentifier() private returns (string memory) {
310-
string memory identifier = string(abi.encodePacked("connection-", Strings.toString(nextConnectionSequence)));
311-
nextConnectionSequence++;
312-
return identifier;
313-
}
314314
}

0 commit comments

Comments
 (0)