Skip to content

Commit

Permalink
Merge pull request #256 from hyperledger-labs/improve-gas-cost-effici…
Browse files Browse the repository at this point in the history
…ency

Improve gas cost efficiency

Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele authored Feb 26, 2024
2 parents 8f9b8cc + b4b9745 commit 86fa06e
Show file tree
Hide file tree
Showing 20 changed files with 195 additions and 624 deletions.
74 changes: 37 additions & 37 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
IBCMockAppTest:testHandshake() (gas: 3435974)
IBCMockAppTest:testHandshakeBetweenDifferentPorts() (gas: 2542936)
IBCMockAppTest:testPacketRelay() (gas: 9616141)
IBCMockAppTest:testPacketTimeout() (gas: 2924777)
IBCTest:testBenchmarkCreateMockClient() (gas: 209339)
IBCMockAppTest:testHandshake() (gas: 3433206)
IBCMockAppTest:testHandshakeBetweenDifferentPorts() (gas: 2539275)
IBCMockAppTest:testPacketRelay() (gas: 9485102)
IBCMockAppTest:testPacketTimeout() (gas: 2906505)
IBCTest:testBenchmarkCreateMockClient() (gas: 209365)
IBCTest:testBenchmarkLCUpdateMockClient() (gas: 39945)
IBCTest:testBenchmarkRecvPacket() (gas: 134536)
IBCTest:testBenchmarkSendPacket() (gas: 85126)
IBCTest:testBenchmarkRecvPacket() (gas: 129941)
IBCTest:testBenchmarkSendPacket() (gas: 84051)
IBCTest:testBenchmarkUpdateMockClient() (gas: 137373)
IBCTest:testToUint128((uint64,uint64)) (runs: 256, μ: 947, ~: 947)
TestICS02:testCreateClient() (gas: 23647978)
TestICS02:testInvalidCreateClient() (gas: 23497549)
TestICS02:testInvalidUpdateClient() (gas: 23499468)
TestICS02:testRegisterClient() (gas: 23242497)
TestICS02:testRegisterClientDuplicatedClientType() (gas: 23225722)
TestICS02:testRegisterClientInvalidClientType() (gas: 23209851)
TestICS02:testUpdateClient() (gas: 23665020)
TestICS03Handshake:testConnOpenAck() (gas: 1631316)
TestICS03Handshake:testConnOpenConfirm() (gas: 1765734)
TestICS02:testCreateClient() (gas: 23157317)
TestICS02:testInvalidCreateClient() (gas: 23006914)
TestICS02:testInvalidUpdateClient() (gas: 23008761)
TestICS02:testRegisterClient() (gas: 22751790)
TestICS02:testRegisterClientDuplicatedClientType() (gas: 22735015)
TestICS02:testRegisterClientInvalidClientType() (gas: 22719144)
TestICS02:testUpdateClient() (gas: 23174313)
TestICS03Handshake:testConnOpenAck() (gas: 1631720)
TestICS03Handshake:testConnOpenConfirm() (gas: 1766228)
TestICS03Handshake:testConnOpenInit() (gas: 1279055)
TestICS03Handshake:testConnOpenTry() (gas: 2160463)
TestICS03Handshake:testInvalidConnOpenAck() (gas: 2031008)
TestICS03Handshake:testInvalidConnOpenConfirm() (gas: 2095567)
TestICS03Handshake:testConnOpenTry() (gas: 2160823)
TestICS03Handshake:testInvalidConnOpenAck() (gas: 2031544)
TestICS03Handshake:testInvalidConnOpenConfirm() (gas: 2096106)
TestICS03Handshake:testInvalidConnOpenInit() (gas: 666816)
TestICS03Handshake:testInvalidConnOpenTry() (gas: 2101014)
TestICS03Handshake:testInvalidConnOpenTry() (gas: 2101149)
TestICS03Version:testCopyVersions() (gas: 558658)
TestICS03Version:testFindSupportedVersion() (gas: 19400)
TestICS03Version:testIsSupportedVersion() (gas: 7864)
TestICS03Version:testPickVersion() (gas: 25327)
TestICS03Version:testVerifyProposedVersion() (gas: 11777)
TestICS03Version:testVerifySupportedFeature() (gas: 4153)
TestICS04Handshake:testBindPort() (gas: 38248)
TestICS04Handshake:testChanClose() (gas: 8477010)
TestICS04Handshake:testChanOpenAck() (gas: 2881785)
TestICS04Handshake:testChanOpenConfirm() (gas: 3061565)
TestICS04Handshake:testChanOpenInit() (gas: 2197331)
TestICS04Handshake:testChanOpenTry() (gas: 2655828)
TestICS04Handshake:testInvalidChanOpenAck() (gas: 2061616)
TestICS04Handshake:testInvalidChanOpenConfirm() (gas: 2117572)
TestICS04Handshake:testInvalidChanOpenInit() (gas: 1275175)
TestICS04Handshake:testInvalidChanOpenTry() (gas: 1351818)
TestICS04Packet:testAcknowledgementPacket() (gas: 2283754)
TestICS04Packet:testInvalidSendPacket() (gas: 2288863)
TestICS04Packet:testRecvPacket() (gas: 7612057)
TestICS04Packet:testRecvPacketTimeoutHeight() (gas: 2316335)
TestICS04Packet:testRecvPacketTimeoutTimestamp() (gas: 2315328)
TestICS04Packet:testSendPacket() (gas: 5091415)
TestICS04Packet:testTimeoutOnClose() (gas: 2539103)
TestICS04Handshake:testBindPort() (gas: 37414)
TestICS04Handshake:testChanClose() (gas: 8457966)
TestICS04Handshake:testChanOpenAck() (gas: 2879243)
TestICS04Handshake:testChanOpenConfirm() (gas: 3058457)
TestICS04Handshake:testChanOpenInit() (gas: 2195918)
TestICS04Handshake:testChanOpenTry() (gas: 2653852)
TestICS04Handshake:testInvalidChanOpenAck() (gas: 2060952)
TestICS04Handshake:testInvalidChanOpenConfirm() (gas: 2116580)
TestICS04Handshake:testInvalidChanOpenInit() (gas: 1275939)
TestICS04Handshake:testInvalidChanOpenTry() (gas: 1352198)
TestICS04Packet:testAcknowledgementPacket() (gas: 2276942)
TestICS04Packet:testInvalidSendPacket() (gas: 2274601)
TestICS04Packet:testRecvPacket() (gas: 7645640)
TestICS04Packet:testRecvPacketTimeoutHeight() (gas: 2320869)
TestICS04Packet:testRecvPacketTimeoutTimestamp() (gas: 2319828)
TestICS04Packet:testSendPacket() (gas: 5067297)
TestICS04Packet:testTimeoutOnClose() (gas: 2539008)
TestICS20:testAddressToHex(address) (runs: 256, μ: 22676, ~: 22804)
TestICS20:testHexToAddress(string) (runs: 256, μ: 4776, ~: 4734)
TestICS20:testIsEscapedString() (gas: 48979)
Expand Down
4 changes: 2 additions & 2 deletions contracts/core/04-channel/IBCChannelHandshake.sol
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ contract IBCChannelHandshake is IBCModuleManager, IIBCChannelHandshake, IIBCChan
version: msg_.channel.version
})
);
claimCapability(channelCapabilityPath(msg_.portId, channelId), address(module));
claimChannelCapability(msg_.portId, channelId, address(module));
writeChannel(
msg_.portId,
channelId,
Expand Down Expand Up @@ -138,7 +138,7 @@ contract IBCChannelHandshake is IBCModuleManager, IIBCChannelHandshake, IIBCChan
counterpartyVersion: msg_.counterpartyVersion
})
);
claimCapability(channelCapabilityPath(msg_.portId, channelId), address(module));
claimChannelCapability(msg_.portId, channelId, address(module));
writeChannel(
msg_.portId,
channelId,
Expand Down
33 changes: 19 additions & 14 deletions contracts/core/04-channel/IBCChannelPacketSendRecv.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ contract IBCChannelPacketSendRecv is
uint64 timeoutTimestamp,
bytes calldata data
) external returns (uint64) {
authenticateCapability(channelCapabilityPath(sourcePort, sourceChannel));
authenticateChannelCapability(sourcePort, sourceChannel);

Channel.Data storage channel = channels[sourcePort][sourceChannel];
if (channel.state != Channel.State.STATE_OPEN) {
Expand Down Expand Up @@ -69,7 +69,7 @@ contract IBCChannelPacketSendRecv is

uint64 packetSequence = nextSequenceSends[sourcePort][sourceChannel];
nextSequenceSends[sourcePort][sourceChannel] = packetSequence + 1;
commitments[IBCCommitment.packetCommitmentKey(sourcePort, sourceChannel, packetSequence)] = keccak256(
commitments[IBCCommitment.packetCommitmentKeyCalldata(sourcePort, sourceChannel, packetSequence)] = keccak256(
abi.encodePacked(
sha256(
abi.encodePacked(
Expand All @@ -92,7 +92,7 @@ contract IBCChannelPacketSendRecv is
uint64 sequence,
bytes calldata acknowledgement
) public {
authenticateCapability(channelCapabilityPath(destinationPortId, destinationChannel));
authenticateChannelCapability(destinationPortId, destinationChannel);
Channel.Data storage channel = channels[destinationPortId][destinationChannel];
if (channel.state != Channel.State.STATE_OPEN) {
revert IBCChannelUnexpectedChannelState(channel.state);
Expand All @@ -110,7 +110,7 @@ contract IBCChannelPacketSendRecv is
bytes memory acknowledgement
) internal {
bytes32 ackCommitmentKey =
IBCCommitment.packetAcknowledgementCommitmentKey(destinationPortId, destinationChannel, sequence);
IBCCommitment.packetAcknowledgementCommitmentKeyCalldata(destinationPortId, destinationChannel, sequence);
if (commitments[ackCommitmentKey] != bytes32(0)) {
revert IBCChannelAcknowledgementAlreadyWritten(destinationPortId, destinationChannel, sequence);
}
Expand Down Expand Up @@ -146,7 +146,9 @@ contract IBCChannelPacketSendRecv is
connections[channel.connection_hops[0]],
msg_.proofHeight,
msg_.proof,
IBCCommitment.packetCommitmentPath(msg_.packet.sourcePort, msg_.packet.sourceChannel, msg_.packet.sequence),
IBCCommitment.packetCommitmentPathCalldata(
msg_.packet.sourcePort, msg_.packet.sourceChannel, msg_.packet.sequence
),
sha256(
abi.encodePacked(
msg_.packet.timeoutTimestamp,
Expand All @@ -158,7 +160,7 @@ contract IBCChannelPacketSendRecv is
);

if (channel.ordering == Channel.Order.ORDER_UNORDERED) {
bytes32 commitmentKey = IBCCommitment.packetReceiptCommitmentKey(
bytes32 commitmentKey = IBCCommitment.packetReceiptCommitmentKeyCalldata(
msg_.packet.destinationPort, msg_.packet.destinationChannel, msg_.packet.sequence
);
if (commitments[commitmentKey] != bytes32(0)) {
Expand All @@ -175,7 +177,7 @@ contract IBCChannelPacketSendRecv is
);
}
nextSequenceRecvs[msg_.packet.destinationPort][msg_.packet.destinationChannel]++;
commitments[IBCCommitment.nextSequenceRecvCommitmentKey(
commitments[IBCCommitment.nextSequenceRecvCommitmentKeyCalldata(
msg_.packet.destinationPort, msg_.packet.destinationChannel
)] = keccak256(
uint64ToBigEndianBytes(nextSequenceRecvs[msg_.packet.destinationPort][msg_.packet.destinationChannel])
Expand Down Expand Up @@ -219,8 +221,9 @@ contract IBCChannelPacketSendRecv is
// NOTE: We can assume here that the connection state is OPEN because the channel state is OPEN
ConnectionEnd.Data storage connection = connections[channel.connection_hops[0]];

bytes32 packetCommitmentKey =
IBCCommitment.packetCommitmentKey(msg_.packet.sourcePort, msg_.packet.sourceChannel, msg_.packet.sequence);
bytes32 packetCommitmentKey = IBCCommitment.packetCommitmentKeyCalldata(
msg_.packet.sourcePort, msg_.packet.sourceChannel, msg_.packet.sequence
);
bytes32 packetCommitment = commitments[packetCommitmentKey];
if (packetCommitment == bytes32(0)) {
revert IBCChannelPacketCommitmentNotFound(
Expand All @@ -247,7 +250,7 @@ contract IBCChannelPacketSendRecv is
connection,
msg_.proofHeight,
msg_.proof,
IBCCommitment.packetAcknowledgementCommitmentPath(
IBCCommitment.packetAcknowledgementCommitmentPathCalldata(
msg_.packet.destinationPort, msg_.packet.destinationChannel, msg_.packet.sequence
),
sha256(msg_.acknowledgement)
Expand Down Expand Up @@ -324,11 +327,13 @@ contract IBCChannelPacketSendRecv is
// private functions

function calcBlockDelay(uint64 timeDelay) private view returns (uint64) {
uint64 blockDelay = 0;
if (expectedTimePerBlock != 0) {
blockDelay = (timeDelay + expectedTimePerBlock - 1) / expectedTimePerBlock;
if (timeDelay == 0) {
return 0;
} else if (expectedTimePerBlock == 0) {
return 0;
} else {
return (timeDelay + expectedTimePerBlock - 1) / expectedTimePerBlock;
}
return blockDelay;
}

function buildConnectionHops(string memory connectionId) private pure returns (string[] memory hops) {
Expand Down
44 changes: 19 additions & 25 deletions contracts/core/04-channel/IBCChannelPacketTimeout.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ contract IBCChannelPacketTimeout is IBCModuleManager, IIBCChannelPacketTimeout,
}

{
bytes32 commitment = commitments[IBCCommitment.packetCommitmentKey(
bytes32 commitment = commitments[IBCCommitment.packetCommitmentKeyCalldata(
msg_.packet.sourcePort, msg_.packet.sourceChannel, msg_.packet.sequence
)];
// NOTE: if false, this indicates that the timeoutPacket already been executed
Expand Down Expand Up @@ -100,6 +100,9 @@ contract IBCChannelPacketTimeout is IBCModuleManager, IIBCChannelPacketTimeout,
}
channel.state = Channel.State.STATE_CLOSED;
} else if (channel.ordering == Channel.Order.ORDER_UNORDERED) {
bytes memory path = IBCCommitment.packetReceiptCommitmentPathCalldata(
msg_.packet.destinationPort, msg_.packet.destinationChannel, msg_.packet.sequence
);
if (
!client.verifyNonMembership(
connection.client_id,
Expand All @@ -108,25 +111,18 @@ contract IBCChannelPacketTimeout is IBCModuleManager, IIBCChannelPacketTimeout,
calcBlockDelay(connection.delay_period),
msg_.proof,
connection.counterparty.prefix.key_prefix,
IBCCommitment.packetReceiptCommitmentPath(
msg_.packet.destinationPort, msg_.packet.destinationChannel, msg_.packet.sequence
)
path
)
) {
revert IBCChannelFailedVerifyPacketReceiptAbsence(
connection.client_id,
IBCCommitment.packetReceiptCommitmentPath(
msg_.packet.destinationPort, msg_.packet.destinationChannel, msg_.packet.sequence
),
msg_.proof,
msg_.proofHeight
connection.client_id, path, msg_.proof, msg_.proofHeight
);
}
} else {
revert IBCChannelUnknownChannelOrder(channel.ordering);
}

delete commitments[IBCCommitment.packetCommitmentKey(
delete commitments[IBCCommitment.packetCommitmentKeyCalldata(
msg_.packet.sourcePort, msg_.packet.sourceChannel, msg_.packet.sequence
)];

Expand All @@ -153,7 +149,7 @@ contract IBCChannelPacketTimeout is IBCModuleManager, IIBCChannelPacketTimeout,
ConnectionEnd.Data storage connection = connections[channel.connection_hops[0]];
ILightClient client = ILightClient(clientImpls[connection.client_id]);
{
bytes32 commitment = commitments[IBCCommitment.packetCommitmentKey(
bytes32 commitment = commitments[IBCCommitment.packetCommitmentKeyCalldata(
msg_.packet.sourcePort, msg_.packet.sourceChannel, msg_.packet.sequence
)];
// NOTE: if false, this indicates that the timeoutPacket already been executed
Expand Down Expand Up @@ -242,6 +238,9 @@ contract IBCChannelPacketTimeout is IBCModuleManager, IIBCChannelPacketTimeout,
);
}
} else if (channel.ordering == Channel.Order.ORDER_UNORDERED) {
bytes memory path = IBCCommitment.packetReceiptCommitmentPathCalldata(
msg_.packet.destinationPort, msg_.packet.destinationChannel, msg_.packet.sequence
);
if (
!client.verifyNonMembership(
connection.client_id,
Expand All @@ -250,18 +249,11 @@ contract IBCChannelPacketTimeout is IBCModuleManager, IIBCChannelPacketTimeout,
calcBlockDelay(connection.delay_period),
msg_.proofUnreceived,
connection.counterparty.prefix.key_prefix,
IBCCommitment.packetReceiptCommitmentPath(
msg_.packet.destinationPort, msg_.packet.destinationChannel, msg_.packet.sequence
)
path
)
) {
revert IBCChannelFailedVerifyPacketReceiptAbsence(
connection.client_id,
IBCCommitment.packetReceiptCommitmentPath(
msg_.packet.destinationPort, msg_.packet.destinationChannel, msg_.packet.sequence
),
msg_.proofUnreceived,
msg_.proofHeight
connection.client_id, path, msg_.proofUnreceived, msg_.proofHeight
);
}
} else {
Expand All @@ -276,11 +268,13 @@ contract IBCChannelPacketTimeout is IBCModuleManager, IIBCChannelPacketTimeout,
// private functions

function calcBlockDelay(uint64 timeDelay) private view returns (uint64) {
uint64 blockDelay = 0;
if (expectedTimePerBlock != 0) {
blockDelay = (timeDelay + expectedTimePerBlock - 1) / expectedTimePerBlock;
if (timeDelay == 0) {
return 0;
} else if (expectedTimePerBlock == 0) {
return 0;
} else {
return (timeDelay + expectedTimePerBlock - 1) / expectedTimePerBlock;
}
return blockDelay;
}

function buildConnectionHops(string memory connectionId) private pure returns (string[] memory hops) {
Expand Down
Loading

0 comments on commit 86fa06e

Please sign in to comment.