Skip to content

Commit

Permalink
Merge pull request #295 from hyperledger-labs/eip-7201
Browse files Browse the repository at this point in the history
Utilize EIP-7201 as storage layout

Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele authored Aug 23, 2024
2 parents 32c3918 + e483a95 commit cd49bc0
Show file tree
Hide file tree
Showing 21 changed files with 514 additions and 359 deletions.
124 changes: 62 additions & 62 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,75 +1,75 @@
IBCBenchmarks:testAcknowledgePacket() (gas: 98678)
IBCBenchmarks:testCreateMockClient() (gas: 217244)
IBCBenchmarks:testRecvPacket() (gas: 155487)
IBCBenchmarks:testSendPacket() (gas: 95329)
IBCBenchmarks:testAcknowledgePacket() (gas: 98040)
IBCBenchmarks:testCreateMockClient() (gas: 216997)
IBCBenchmarks:testRecvPacket() (gas: 154889)
IBCBenchmarks:testSendPacket() (gas: 94843)
IBCBenchmarks:testUpdateMockClientDirectly() (gas: 64879)
IBCBenchmarks:testUpdateMockClientViaHandler() (gas: 148205)
IBCMockAppTest:testHandshake() (gas: 4343205)
IBCMockAppTest:testHandshakeBetweenDifferentPorts() (gas: 3286459)
IBCMockAppTest:testPacketRelay() (gas: 11692905)
IBCMockAppTest:testPacketTimeout() (gas: 4242242)
IBCBenchmarks:testUpdateMockClientViaHandler() (gas: 148239)
IBCMockAppTest:testHandshake() (gas: 4161353)
IBCMockAppTest:testHandshakeBetweenDifferentPorts() (gas: 3104214)
IBCMockAppTest:testPacketRelay() (gas: 11460317)
IBCMockAppTest:testPacketTimeout() (gas: 4059417)
ICS24HostTest:testValidatePortIdentifier() (gas: 44476)
TestICS02:testCreateClient() (gas: 30153096)
TestICS02:testCreateClient() (gas: 30572814)
TestICS02:testHeightToUint128((uint64,uint64)) (runs: 256, μ: 907, ~: 907)
TestICS02:testInvalidCreateClient() (gas: 30038762)
TestICS02:testInvalidUpdateClient() (gas: 30042040)
TestICS02:testRegisterClient() (gas: 29702122)
TestICS02:testRegisterClientDuplicatedClientType() (gas: 29688342)
TestICS02:testRegisterClientInvalidClientType() (gas: 29717304)
TestICS02:testUpdateClient() (gas: 30208564)
TestICS03Handshake:testConnOpenAck() (gas: 1811332)
TestICS03Handshake:testConnOpenConfirm() (gas: 1989221)
TestICS03Handshake:testConnOpenInit() (gas: 1423613)
TestICS03Handshake:testConnOpenTry() (gas: 2348126)
TestICS03Handshake:testInvalidConnOpenAck() (gas: 2323078)
TestICS03Handshake:testInvalidConnOpenConfirm() (gas: 2421885)
TestICS03Handshake:testInvalidConnOpenInit() (gas: 777600)
TestICS03Handshake:testInvalidConnOpenTry() (gas: 2283933)
TestICS02:testInvalidCreateClient() (gas: 30458130)
TestICS02:testInvalidUpdateClient() (gas: 30461817)
TestICS02:testRegisterClient() (gas: 30122086)
TestICS02:testRegisterClientDuplicatedClientType() (gas: 30108306)
TestICS02:testRegisterClientInvalidClientType() (gas: 30137268)
TestICS02:testUpdateClient() (gas: 30628415)
TestICS03Handshake:testConnOpenAck() (gas: 1810871)
TestICS03Handshake:testConnOpenConfirm() (gas: 1988723)
TestICS03Handshake:testConnOpenInit() (gas: 1422940)
TestICS03Handshake:testConnOpenTry() (gas: 2347689)
TestICS03Handshake:testInvalidConnOpenAck() (gas: 2322689)
TestICS03Handshake:testInvalidConnOpenConfirm() (gas: 2421514)
TestICS03Handshake:testInvalidConnOpenInit() (gas: 776893)
TestICS03Handshake:testInvalidConnOpenTry() (gas: 2283346)
TestICS03Version:testCopyVersions() (gas: 570207)
TestICS03Version:testFindSupportedVersion() (gas: 34452)
TestICS03Version:testIsSupportedVersion() (gas: 13568)
TestICS03Version:testPickVersion() (gas: 37836)
TestICS03Version:testVerifyProposedVersion() (gas: 21308)
TestICS03Version:testVerifySupportedFeature() (gas: 10229)
TestICS04Handshake:testBindPort() (gas: 456271)
TestICS04Handshake:testChanClose() (gas: 12800640)
TestICS04Handshake:testChanOpenAck() (gas: 3428597)
TestICS04Handshake:testChanOpenConfirm() (gas: 3724273)
TestICS04Handshake:testChanOpenInit() (gas: 2535741)
TestICS04Handshake:testChanOpenTry() (gas: 3083807)
TestICS04Handshake:testInvalidChanOpenAck() (gas: 2411670)
TestICS04Handshake:testInvalidChanOpenConfirm() (gas: 2486327)
TestICS04Handshake:testInvalidChanOpenInit() (gas: 1730517)
TestICS04Handshake:testInvalidChanOpenTry() (gas: 1742638)
TestICS04Packet:testAcknowledgementPacket() (gas: 3316365)
TestICS04Packet:testInvalidSendPacket() (gas: 3476451)
TestICS04Packet:testRecvPacket() (gas: 9965223)
TestICS04Packet:testRecvPacketTimeoutHeight() (gas: 3236945)
TestICS04Packet:testRecvPacketTimeoutTimestamp() (gas: 3261000)
TestICS04Packet:testSendPacket() (gas: 4583983)
TestICS04Packet:testTimeoutOnClose() (gas: 3488144)
TestICS04Upgrade:testCrossingHelloInconsistentVersions() (gas: 9918550)
TestICS04Upgrade:testUpgradeAuthorityCancel() (gas: 46157666)
TestICS04Upgrade:testUpgradeCannotCancelWithOldErrorReceipt() (gas: 3392338)
TestICS04Upgrade:testUpgradeCannotRecvNextUpgradePacket() (gas: 5230053)
TestICS04Upgrade:testUpgradeCounterpartyAdvanceNextSequenceBeforeOpen() (gas: 5191254)
TestICS04Upgrade:testUpgradeCrossingHelloIncompatibleProposals() (gas: 4944387)
TestICS04Upgrade:testUpgradeFull() (gas: 55984957)
TestICS04Upgrade:testUpgradeInit() (gas: 3018393)
TestICS04Upgrade:testUpgradeNoChanges() (gas: 2433786)
TestICS04Upgrade:testUpgradeNotUpgradableModule() (gas: 3533495)
TestICS04Upgrade:testUpgradeOutOfSync() (gas: 3826003)
TestICS04Upgrade:testUpgradeRelaySuccessAtCounterpartyFlushComplete() (gas: 5201982)
TestICS04Upgrade:testUpgradeRelaySuccessAtFlushing() (gas: 5575834)
TestICS04Upgrade:testUpgradeSendPacketFailAtFlushingOrFlushComplete() (gas: 4012928)
TestICS04Upgrade:testUpgradeTimeoutAbortAck() (gas: 17445807)
TestICS04Upgrade:testUpgradeTimeoutAbortConfirm() (gas: 21062598)
TestICS04Upgrade:testUpgradeTimeoutUpgrade() (gas: 70448064)
TestICS04Upgrade:testUpgradeToOrdered() (gas: 53937015)
TestICS04Upgrade:testUpgradeToUnordered() (gas: 43053273)
TestICS04Handshake:testBindPort() (gas: 456448)
TestICS04Handshake:testChanClose() (gas: 12159113)
TestICS04Handshake:testChanOpenAck() (gas: 3242588)
TestICS04Handshake:testChanOpenConfirm() (gas: 3538396)
TestICS04Handshake:testChanOpenInit() (gas: 2395187)
TestICS04Handshake:testChanOpenTry() (gas: 2897666)
TestICS04Handshake:testInvalidChanOpenAck() (gas: 2317011)
TestICS04Handshake:testInvalidChanOpenConfirm() (gas: 2391668)
TestICS04Handshake:testInvalidChanOpenInit() (gas: 1677250)
TestICS04Handshake:testInvalidChanOpenTry() (gas: 1692203)
TestICS04Packet:testAcknowledgementPacket() (gas: 3111231)
TestICS04Packet:testInvalidSendPacket() (gas: 3294851)
TestICS04Packet:testRecvPacket() (gas: 9485947)
TestICS04Packet:testRecvPacketTimeoutHeight() (gas: 3053625)
TestICS04Packet:testRecvPacketTimeoutTimestamp() (gas: 3077674)
TestICS04Packet:testSendPacket() (gas: 4393823)
TestICS04Packet:testTimeoutOnClose() (gas: 3304892)
TestICS04Upgrade:testCrossingHelloInconsistentVersions() (gas: 9730721)
TestICS04Upgrade:testUpgradeAuthorityCancel() (gas: 44972623)
TestICS04Upgrade:testUpgradeCannotCancelWithOldErrorReceipt() (gas: 3296637)
TestICS04Upgrade:testUpgradeCannotRecvNextUpgradePacket() (gas: 5124643)
TestICS04Upgrade:testUpgradeCounterpartyAdvanceNextSequenceBeforeOpen() (gas: 5087713)
TestICS04Upgrade:testUpgradeCrossingHelloIncompatibleProposals() (gas: 4843978)
TestICS04Upgrade:testUpgradeFull() (gas: 55696695)
TestICS04Upgrade:testUpgradeInit() (gas: 2925072)
TestICS04Upgrade:testUpgradeNoChanges() (gas: 2342343)
TestICS04Upgrade:testUpgradeNotUpgradableModule() (gas: 3442666)
TestICS04Upgrade:testUpgradeOutOfSync() (gas: 3727867)
TestICS04Upgrade:testUpgradeRelaySuccessAtCounterpartyFlushComplete() (gas: 5097250)
TestICS04Upgrade:testUpgradeRelaySuccessAtFlushing() (gas: 5469407)
TestICS04Upgrade:testUpgradeSendPacketFailAtFlushingOrFlushComplete() (gas: 3907425)
TestICS04Upgrade:testUpgradeTimeoutAbortAck() (gas: 17323669)
TestICS04Upgrade:testUpgradeTimeoutAbortConfirm() (gas: 20920588)
TestICS04Upgrade:testUpgradeTimeoutUpgrade() (gas: 68891924)
TestICS04Upgrade:testUpgradeToOrdered() (gas: 52805680)
TestICS04Upgrade:testUpgradeToUnordered() (gas: 42151776)
TestICS04UpgradeApp:testUpgradeAuthorizationChanneNotFound() (gas: 62062)
TestICS04UpgradeApp:testUpgradeAuthorizationRePropose() (gas: 2456047)
TestICS04UpgradeApp:testUpgradeAuthorizationRemove() (gas: 2436473)
TestICS04UpgradeApp:testUpgradeAuthorizationRePropose() (gas: 2365121)
TestICS04UpgradeApp:testUpgradeAuthorizationRemove() (gas: 2345567)
TestICS20:testAddressToHex(address) (runs: 256, μ: 26850, ~: 27044)
TestICS20:testHexToAddress(string) (runs: 256, μ: 4636, ~: 4595)
TestICS20:testIsEscapedString() (gas: 62745)
Expand Down
3 changes: 2 additions & 1 deletion contracts/clients/qbft/QBFTClient.sol
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ contract QBFTClient is ILightClient, ILightClientErrors {
bytes32 internal constant CONSENSUS_STATE_TYPE_URL_HASH =
keccak256(abi.encodePacked("/ibc.lightclients.qbft.v1.ConsensusState"));

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

address public immutable ibcHandler;
Expand Down
17 changes: 11 additions & 6 deletions contracts/core/02-client/IBCClient.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@ contract IBCClient is IBCHost, IIBCClient, IIBCClientErrors {
* @dev createClient creates a new client state and populates it with a given consensus state
*/
function createClient(MsgCreateClient calldata msg_) external override returns (string memory clientId) {
address clientImpl = clientRegistry[msg_.clientType];
address clientImpl = getHostStorage().clientRegistry[msg_.clientType];
if (clientImpl == address(0)) {
revert IBCClientUnregisteredClientType(msg_.clientType);
}
clientId = generateClientIdentifier(msg_.clientType);
clientTypes[clientId] = msg_.clientType;
clientImpls[clientId] = clientImpl;
ClientStorage storage client = getClientStorage()[clientId];
client.clientType = msg_.clientType;
client.clientImpl = clientImpl;
Height.Data memory height =
ILightClient(clientImpl).initializeClient(clientId, msg_.protoClientState, msg_.protoConsensusState);
// update commitments
mapping(bytes32 => bytes32) storage commitments = getCommitments();
commitments[IBCCommitment.clientStateCommitmentKey(clientId)] = keccak256(msg_.protoClientState);
commitments[IBCCommitment.consensusStateCommitmentKey(clientId, height.revision_number, height.revision_height)]
= keccak256(msg_.protoConsensusState);
Expand Down Expand Up @@ -83,6 +85,7 @@ contract IBCClient is IBCHost, IIBCClient, IIBCClientErrors {
if (!found) {
revert IBCClientClientNotFound(clientId);
}
mapping(bytes32 => bytes32) storage commitments = getCommitments();
commitments[IBCCommitment.clientStateCommitmentKey(clientId)] = keccak256(clientState);
for (uint256 i = 0; i < heights.length; i++) {
(consensusState, found) = lc.getConsensusState(clientId, heights[i]);
Expand All @@ -102,9 +105,11 @@ contract IBCClient is IBCHost, IIBCClient, IIBCClientErrors {
/**
* @dev generateClientIdentifier generates a new client identifier for a given client type
*/
function generateClientIdentifier(string calldata clientType) private returns (string memory) {
string memory identifier = string(abi.encodePacked(clientType, "-", Strings.toString(nextClientSequence)));
nextClientSequence++;
function generateClientIdentifier(string calldata clientType) internal returns (string memory) {
HostStorage storage hostStorage = getHostStorage();
string memory identifier =
string(abi.encodePacked(clientType, "-", Strings.toString(hostStorage.nextClientSequence)));
hostStorage.nextClientSequence++;
return identifier;
}
}
50 changes: 25 additions & 25 deletions contracts/core/03-connection/IBCConnection.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ abstract contract IBCConnection is IBCHost, IBCSelfStateValidator, IIBCConnectio
returns (string memory)
{
string memory connectionId = generateConnectionIdentifier();
ConnectionEnd.Data storage connection = connections[connectionId];
ConnectionEnd.Data storage connection = getConnectionStorage()[connectionId].connection;
if (connection.state != ConnectionEnd.State.STATE_UNINITIALIZED_UNSPECIFIED) {
revert IBCConnectionAlreadyConnectionExists();
}
Expand Down Expand Up @@ -71,7 +71,7 @@ abstract contract IBCConnection is IBCHost, IBCSelfStateValidator, IIBCConnectio
bytes memory selfConsensusState = getSelfConsensusState(msg_.consensusHeight, msg_.hostConsensusStateProof);

string memory connectionId = generateConnectionIdentifier();
ConnectionEnd.Data storage connection = connections[connectionId];
ConnectionEnd.Data storage connection = getConnectionStorage()[connectionId].connection;
if (connection.state != ConnectionEnd.State.STATE_UNINITIALIZED_UNSPECIFIED) {
revert IBCConnectionAlreadyConnectionExists();
}
Expand Down Expand Up @@ -120,7 +120,7 @@ abstract contract IBCConnection is IBCHost, IBCSelfStateValidator, IIBCConnectio
* to chain A (this code is executed on chain A).
*/
function connectionOpenAck(IIBCConnection.MsgConnectionOpenAck calldata msg_) external override {
ConnectionEnd.Data storage connection = connections[msg_.connectionId];
ConnectionEnd.Data storage connection = getConnectionStorage()[msg_.connectionId].connection;
if (connection.state != ConnectionEnd.State.STATE_INIT) {
revert IBCConnectionUnexpectedConnectionState(connection.state);
}
Expand Down Expand Up @@ -169,7 +169,7 @@ abstract contract IBCConnection is IBCHost, IBCSelfStateValidator, IIBCConnectio
* which the connection is open on both chains (this code is executed on chain B).
*/
function connectionOpenConfirm(IIBCConnection.MsgConnectionOpenConfirm calldata msg_) external override {
ConnectionEnd.Data storage connection = connections[msg_.connectionId];
ConnectionEnd.Data storage connection = getConnectionStorage()[msg_.connectionId].connection;
if (connection.state != ConnectionEnd.State.STATE_TRYOPEN) {
revert IBCConnectionUnexpectedConnectionState(connection.state);
}
Expand All @@ -194,12 +194,29 @@ abstract contract IBCConnection is IBCHost, IBCSelfStateValidator, IIBCConnectio
updateConnectionCommitment(msg_.connectionId);
}

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

/* Verification functions */
// --------- Private Functions --------- //

function generateConnectionIdentifier() private returns (string memory) {
HostStorage storage hostStorage = getHostStorage();
string memory identifier =
string(abi.encodePacked("connection-", Strings.toString(hostStorage.nextConnectionSequence)));
hostStorage.nextConnectionSequence++;
return identifier;
}

function updateConnectionCommitment(string memory connectionId) private {
getCommitments()[IBCCommitment.connectionCommitmentKey(connectionId)] =
keccak256(ConnectionEnd.encode(getConnectionStorage()[connectionId].connection));
}

function verifyClientState(
ConnectionEnd.Data storage connection,
Expand Down Expand Up @@ -294,21 +311,4 @@ abstract contract IBCConnection is IBCHost, IBCSelfStateValidator, IIBCConnectio
height
);
}

/**
* @dev getCompatibleVersions returns the supported versions of the host chain.
*/
function getCompatibleVersions() public pure virtual returns (Version.Data[] memory) {
Version.Data[] memory versions = new Version.Data[](1);
versions[0] = IBCConnectionLib.defaultIBCVersion();
return versions;
}

/* Internal functions */

function generateConnectionIdentifier() private returns (string memory) {
string memory identifier = string(abi.encodePacked("connection-", Strings.toString(nextConnectionSequence)));
nextConnectionSequence++;
return identifier;
}
}
Loading

0 comments on commit cd49bc0

Please sign in to comment.