Skip to content

Commit

Permalink
Merge pull request #301 from hyperledger-labs/fix-ics20
Browse files Browse the repository at this point in the history
Refactor ics20

Signed-off-by: Jun Kimura <[email protected]>
  • Loading branch information
bluele authored Oct 11, 2024
2 parents ca7a56c + 98b049c commit 0e83dc7
Show file tree
Hide file tree
Showing 18 changed files with 1,493 additions and 1,642 deletions.
68 changes: 36 additions & 32 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -44,38 +44,42 @@ TestICS04Handshake:testInvalidChanOpenAck() (gas: 2327618)
TestICS04Handshake:testInvalidChanOpenConfirm() (gas: 2402634)
TestICS04Handshake:testInvalidChanOpenInit() (gas: 1677184)
TestICS04Handshake:testInvalidChanOpenTry() (gas: 1692266)
TestICS04Packet:testAcknowledgementPacket() (gas: 3133839)
TestICS04Packet:testInvalidSendPacket() (gas: 3317459)
TestICS04Packet:testRecvPacket() (gas: 9561916)
TestICS04Packet:testRecvPacketTimeoutHeight() (gas: 3083947)
TestICS04Packet:testRecvPacketTimeoutTimestamp() (gas: 3107997)
TestICS04Packet:testSendPacket() (gas: 4425319)
TestICS04Packet:testTimeoutOnClose() (gas: 3335905)
TestICS04Upgrade:testCrossingHelloInconsistentVersions() (gas: 9761065)
TestICS04Upgrade:testUpgradeAuthorityCancel() (gas: 45179742)
TestICS04Upgrade:testUpgradeCannotCancelWithOldErrorReceipt() (gas: 3309916)
TestICS04Upgrade:testUpgradeCannotRecvNextUpgradePacket() (gas: 5142516)
TestICS04Upgrade:testUpgradeCounterpartyAdvanceNextSequenceBeforeOpen() (gas: 5101888)
TestICS04Upgrade:testUpgradeCrossingHelloIncompatibleProposals() (gas: 4859793)
TestICS04Upgrade:testUpgradeFull() (gas: 55736320)
TestICS04Upgrade:testUpgradeInit() (gas: 2937759)
TestICS04Upgrade:testUpgradeNoChanges() (gas: 2354426)
TestICS04Upgrade:testUpgradeNotUpgradableModule() (gas: 3470809)
TestICS04Upgrade:testUpgradeOutOfSync() (gas: 3741133)
TestICS04Upgrade:testUpgradeRelaySuccessAtCounterpartyFlushComplete() (gas: 5115114)
TestICS04Upgrade:testUpgradeRelaySuccessAtFlushing() (gas: 5487347)
TestICS04Upgrade:testUpgradeSendPacketFailAtFlushingOrFlushComplete() (gas: 3920258)
TestICS04Upgrade:testUpgradeTimeoutAbortAck() (gas: 17345060)
TestICS04Upgrade:testUpgradeTimeoutAbortConfirm() (gas: 20945649)
TestICS04Upgrade:testUpgradeTimeoutUpgrade() (gas: 69105350)
TestICS04Upgrade:testUpgradeToOrdered() (gas: 53030759)
TestICS04Upgrade:testUpgradeToUnordered() (gas: 42323192)
TestICS04Packet:testAcknowledgementPacket() (gas: 3133817)
TestICS04Packet:testInvalidSendPacket() (gas: 3317437)
TestICS04Packet:testRecvPacket() (gas: 9561861)
TestICS04Packet:testRecvPacketTimeoutHeight() (gas: 3083925)
TestICS04Packet:testRecvPacketTimeoutTimestamp() (gas: 3107975)
TestICS04Packet:testSendPacket() (gas: 4425297)
TestICS04Packet:testTimeoutOnClose() (gas: 3335859)
TestICS04Upgrade:testCrossingHelloInconsistentVersions() (gas: 9761029)
TestICS04Upgrade:testUpgradeAuthorityCancel() (gas: 45179694)
TestICS04Upgrade:testUpgradeCannotCancelWithOldErrorReceipt() (gas: 3309912)
TestICS04Upgrade:testUpgradeCannotRecvNextUpgradePacket() (gas: 5142512)
TestICS04Upgrade:testUpgradeCounterpartyAdvanceNextSequenceBeforeOpen() (gas: 5101884)
TestICS04Upgrade:testUpgradeCrossingHelloIncompatibleProposals() (gas: 4859789)
TestICS04Upgrade:testUpgradeFull() (gas: 55736252)
TestICS04Upgrade:testUpgradeInit() (gas: 2937755)
TestICS04Upgrade:testUpgradeNoChanges() (gas: 2354422)
TestICS04Upgrade:testUpgradeNotUpgradableModule() (gas: 3470805)
TestICS04Upgrade:testUpgradeOutOfSync() (gas: 3741129)
TestICS04Upgrade:testUpgradeRelaySuccessAtCounterpartyFlushComplete() (gas: 5115110)
TestICS04Upgrade:testUpgradeRelaySuccessAtFlushing() (gas: 5487343)
TestICS04Upgrade:testUpgradeSendPacketFailAtFlushingOrFlushComplete() (gas: 3920254)
TestICS04Upgrade:testUpgradeTimeoutAbortAck() (gas: 17345056)
TestICS04Upgrade:testUpgradeTimeoutAbortConfirm() (gas: 20945645)
TestICS04Upgrade:testUpgradeTimeoutUpgrade() (gas: 69105286)
TestICS04Upgrade:testUpgradeToOrdered() (gas: 53030719)
TestICS04Upgrade:testUpgradeToUnordered() (gas: 42323160)
TestICS04UpgradeApp:testUpgradeAuthorizationChanneNotFound() (gas: 62062)
TestICS04UpgradeApp:testUpgradeAuthorizationRePropose() (gas: 2376902)
TestICS04UpgradeApp:testUpgradeAuthorizationRemove() (gas: 2357348)
TestICS20:testAddressToHex(address) (runs: 256, μ: 26910, ~: 27088)
TestICS20:testHexToAddress(string) (runs: 256, μ: 4658, ~: 4617)
TestICS20:testIsEscapedString() (gas: 64753)
TestICS20:testMarshaling() (gas: 180017)
TestICS20:testParseAmount(uint256) (runs: 256, μ: 31599, ~: 27438)
TestICS20:testParseUint256String() (gas: 26745)
TestICS20Lib:testAddressToHex(address) (runs: 256, μ: 26916, ~: 27088)
TestICS20Lib:testHexToAddress(string) (runs: 256, μ: 4658, ~: 4617)
TestICS20Lib:testIsEscapedString() (gas: 64753)
TestICS20Lib:testMarshaling() (gas: 180017)
TestICS20Lib:testParseAmount(uint256) (runs: 256, μ: 31619, ~: 27869)
TestICS20Lib:testParseUint256String() (gas: 26745)
TestICS20Transfer:testDeposit() (gas: 404927)
TestICS20Transfer:testDepositTransferWithdraw() (gas: 321969)
TestICS20Transfer:testRelay() (gas: 7588605)
TestICS20Transfer:testWithdraw() (gas: 416732)
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ lint:

.PHONY: test
test:
TEST_UPGRADEABLE=$(TEST_UPGRADEABLE) $(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)

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

.PHONY: coverage
coverage:
Expand Down
102 changes: 0 additions & 102 deletions contracts/apps/20-transfer/ICS20Bank.sol

This file was deleted.

77 changes: 66 additions & 11 deletions contracts/apps/20-transfer/ICS20Lib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,10 @@
pragma solidity ^0.8.20;

import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";
import {Height} from "../../proto/Client.sol";
import {IICS20Errors} from "./IICS20Errors.sol";

library ICS20Lib {
/**
* @dev PacketData is defined in [ICS-20](https://github.com/cosmos/ibc/tree/main/spec/app/ics-020-fungible-token-transfer).
*/
struct PacketData {
string denom;
string sender;
string receiver;
uint256 amount;
string memo;
}

bytes internal constant SUCCESSFUL_ACKNOWLEDGEMENT_JSON = bytes('{"result":"AQ=="}');
bytes internal constant FAILED_ACKNOWLEDGEMENT_JSON = bytes('{"error":"failed"}');
bytes32 internal constant KECCAK256_SUCCESSFUL_ACKNOWLEDGEMENT_JSON = keccak256(SUCCESSFUL_ACKNOWLEDGEMENT_JSON);
Expand All @@ -33,6 +23,25 @@ library ICS20Lib {

bytes16 private constant HEX_DIGITS = "0123456789abcdef";

/**
* @dev PacketData is defined in [ICS-20](https://github.com/cosmos/ibc/tree/main/spec/app/ics-020-fungible-token-transfer).
*/
struct PacketData {
string denom;
string sender;
string receiver;
uint256 amount;
string memo;
}

/**
* @dev Either `height` or `timestampNanos` must be set.
*/
struct Timeout {
Height.Data height;
uint64 timestampNanos;
}

/**
* @dev marshalUnsafeJSON marshals PacketData into JSON bytes without escaping.
* `memo` field is omitted if it is empty.
Expand Down Expand Up @@ -147,6 +156,23 @@ library ICS20Lib {
return pd;
}

/**
* @dev timeout returns a Timeout struct with the given height.
*/
function timeout(uint64 revisionNumber, uint64 revisionHeight) internal pure returns (Timeout memory) {
return Timeout({
height: Height.Data({revision_number: revisionNumber, revision_height: revisionHeight}),
timestampNanos: 0
});
}

/**
* @dev timeout returns a Timeout struct with the given timestamp.
*/
function timeout(uint64 timestampNanos) internal pure returns (Timeout memory) {
return Timeout({height: Height.Data({revision_number: 0, revision_height: 0}), timestampNanos: timestampNanos});
}

/**
* @dev parseUint256String parses `bz` from a position `pos` to produce a uint256 value.
* The parse will stop parsing when it encounters a non-digit character.
Expand Down Expand Up @@ -372,4 +398,33 @@ library ICS20Lib {
function equal(bytes memory a, bytes memory b) internal pure returns (bool) {
return keccak256(a) == keccak256(b);
}

/**
* @dev denomPrefix returns the prefix of the denomination.
*/
function denomPrefix(string memory port, string memory channel) internal pure returns (bytes memory) {
return abi.encodePacked(port, "/", channel, "/");
}

/**
* @dev denom returns the denomination string.
*/
function denom(string memory port, string memory channel, address tokenContract)
internal
pure
returns (string memory)
{
return denom(port, channel, addressToHexString(tokenContract));
}

/**
* @dev denom returns the denomination string.
*/
function denom(string memory port, string memory channel, string memory baseDenom)
internal
pure
returns (string memory)
{
return string(abi.encodePacked(denomPrefix(port, channel), baseDenom));
}
}
Loading

0 comments on commit 0e83dc7

Please sign in to comment.