Skip to content

Commit 0e83dc7

Browse files
authored
Merge pull request #301 from hyperledger-labs/fix-ics20
Refactor ics20 Signed-off-by: Jun Kimura <jun.kimura@datachain.jp>
2 parents ca7a56c + 98b049c commit 0e83dc7

File tree

18 files changed

+1493
-1642
lines changed

18 files changed

+1493
-1642
lines changed

.gas-snapshot

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -44,38 +44,42 @@ TestICS04Handshake:testInvalidChanOpenAck() (gas: 2327618)
4444
TestICS04Handshake:testInvalidChanOpenConfirm() (gas: 2402634)
4545
TestICS04Handshake:testInvalidChanOpenInit() (gas: 1677184)
4646
TestICS04Handshake:testInvalidChanOpenTry() (gas: 1692266)
47-
TestICS04Packet:testAcknowledgementPacket() (gas: 3133839)
48-
TestICS04Packet:testInvalidSendPacket() (gas: 3317459)
49-
TestICS04Packet:testRecvPacket() (gas: 9561916)
50-
TestICS04Packet:testRecvPacketTimeoutHeight() (gas: 3083947)
51-
TestICS04Packet:testRecvPacketTimeoutTimestamp() (gas: 3107997)
52-
TestICS04Packet:testSendPacket() (gas: 4425319)
53-
TestICS04Packet:testTimeoutOnClose() (gas: 3335905)
54-
TestICS04Upgrade:testCrossingHelloInconsistentVersions() (gas: 9761065)
55-
TestICS04Upgrade:testUpgradeAuthorityCancel() (gas: 45179742)
56-
TestICS04Upgrade:testUpgradeCannotCancelWithOldErrorReceipt() (gas: 3309916)
57-
TestICS04Upgrade:testUpgradeCannotRecvNextUpgradePacket() (gas: 5142516)
58-
TestICS04Upgrade:testUpgradeCounterpartyAdvanceNextSequenceBeforeOpen() (gas: 5101888)
59-
TestICS04Upgrade:testUpgradeCrossingHelloIncompatibleProposals() (gas: 4859793)
60-
TestICS04Upgrade:testUpgradeFull() (gas: 55736320)
61-
TestICS04Upgrade:testUpgradeInit() (gas: 2937759)
62-
TestICS04Upgrade:testUpgradeNoChanges() (gas: 2354426)
63-
TestICS04Upgrade:testUpgradeNotUpgradableModule() (gas: 3470809)
64-
TestICS04Upgrade:testUpgradeOutOfSync() (gas: 3741133)
65-
TestICS04Upgrade:testUpgradeRelaySuccessAtCounterpartyFlushComplete() (gas: 5115114)
66-
TestICS04Upgrade:testUpgradeRelaySuccessAtFlushing() (gas: 5487347)
67-
TestICS04Upgrade:testUpgradeSendPacketFailAtFlushingOrFlushComplete() (gas: 3920258)
68-
TestICS04Upgrade:testUpgradeTimeoutAbortAck() (gas: 17345060)
69-
TestICS04Upgrade:testUpgradeTimeoutAbortConfirm() (gas: 20945649)
70-
TestICS04Upgrade:testUpgradeTimeoutUpgrade() (gas: 69105350)
71-
TestICS04Upgrade:testUpgradeToOrdered() (gas: 53030759)
72-
TestICS04Upgrade:testUpgradeToUnordered() (gas: 42323192)
47+
TestICS04Packet:testAcknowledgementPacket() (gas: 3133817)
48+
TestICS04Packet:testInvalidSendPacket() (gas: 3317437)
49+
TestICS04Packet:testRecvPacket() (gas: 9561861)
50+
TestICS04Packet:testRecvPacketTimeoutHeight() (gas: 3083925)
51+
TestICS04Packet:testRecvPacketTimeoutTimestamp() (gas: 3107975)
52+
TestICS04Packet:testSendPacket() (gas: 4425297)
53+
TestICS04Packet:testTimeoutOnClose() (gas: 3335859)
54+
TestICS04Upgrade:testCrossingHelloInconsistentVersions() (gas: 9761029)
55+
TestICS04Upgrade:testUpgradeAuthorityCancel() (gas: 45179694)
56+
TestICS04Upgrade:testUpgradeCannotCancelWithOldErrorReceipt() (gas: 3309912)
57+
TestICS04Upgrade:testUpgradeCannotRecvNextUpgradePacket() (gas: 5142512)
58+
TestICS04Upgrade:testUpgradeCounterpartyAdvanceNextSequenceBeforeOpen() (gas: 5101884)
59+
TestICS04Upgrade:testUpgradeCrossingHelloIncompatibleProposals() (gas: 4859789)
60+
TestICS04Upgrade:testUpgradeFull() (gas: 55736252)
61+
TestICS04Upgrade:testUpgradeInit() (gas: 2937755)
62+
TestICS04Upgrade:testUpgradeNoChanges() (gas: 2354422)
63+
TestICS04Upgrade:testUpgradeNotUpgradableModule() (gas: 3470805)
64+
TestICS04Upgrade:testUpgradeOutOfSync() (gas: 3741129)
65+
TestICS04Upgrade:testUpgradeRelaySuccessAtCounterpartyFlushComplete() (gas: 5115110)
66+
TestICS04Upgrade:testUpgradeRelaySuccessAtFlushing() (gas: 5487343)
67+
TestICS04Upgrade:testUpgradeSendPacketFailAtFlushingOrFlushComplete() (gas: 3920254)
68+
TestICS04Upgrade:testUpgradeTimeoutAbortAck() (gas: 17345056)
69+
TestICS04Upgrade:testUpgradeTimeoutAbortConfirm() (gas: 20945645)
70+
TestICS04Upgrade:testUpgradeTimeoutUpgrade() (gas: 69105286)
71+
TestICS04Upgrade:testUpgradeToOrdered() (gas: 53030719)
72+
TestICS04Upgrade:testUpgradeToUnordered() (gas: 42323160)
7373
TestICS04UpgradeApp:testUpgradeAuthorizationChanneNotFound() (gas: 62062)
7474
TestICS04UpgradeApp:testUpgradeAuthorizationRePropose() (gas: 2376902)
7575
TestICS04UpgradeApp:testUpgradeAuthorizationRemove() (gas: 2357348)
76-
TestICS20:testAddressToHex(address) (runs: 256, μ: 26910, ~: 27088)
77-
TestICS20:testHexToAddress(string) (runs: 256, μ: 4658, ~: 4617)
78-
TestICS20:testIsEscapedString() (gas: 64753)
79-
TestICS20:testMarshaling() (gas: 180017)
80-
TestICS20:testParseAmount(uint256) (runs: 256, μ: 31599, ~: 27438)
81-
TestICS20:testParseUint256String() (gas: 26745)
76+
TestICS20Lib:testAddressToHex(address) (runs: 256, μ: 26916, ~: 27088)
77+
TestICS20Lib:testHexToAddress(string) (runs: 256, μ: 4658, ~: 4617)
78+
TestICS20Lib:testIsEscapedString() (gas: 64753)
79+
TestICS20Lib:testMarshaling() (gas: 180017)
80+
TestICS20Lib:testParseAmount(uint256) (runs: 256, μ: 31619, ~: 27869)
81+
TestICS20Lib:testParseUint256String() (gas: 26745)
82+
TestICS20Transfer:testDeposit() (gas: 404927)
83+
TestICS20Transfer:testDepositTransferWithdraw() (gas: 321969)
84+
TestICS20Transfer:testRelay() (gas: 7588605)
85+
TestICS20Transfer:testWithdraw() (gas: 416732)

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ lint:
3232

3333
.PHONY: test
3434
test:
35-
TEST_UPGRADEABLE=$(TEST_UPGRADEABLE) $(FORGE) test -vvvv --gas-report --isolate --use solc:$(SOLC_VERSION) $(FORGE_SNAPSHOT_OPTION)
35+
TEST_UPGRADEABLE=$(TEST_UPGRADEABLE) $(FORGE) test -vvvv --gas-report --isolate --use solc:$(SOLC_VERSION)
3636

3737
.PHONY: snapshot
3838
snapshot:
39-
$(FORGE) snapshot -vvvv --gas-report --isolate --use solc:$(SOLC_VERSION) $(FORGE_SNAPSHOT_OPTION)
39+
$(FORGE) snapshot -vvvv --gas-report --isolate --use solc:$(SOLC_VERSION)
4040

4141
.PHONY: coverage
4242
coverage:

contracts/apps/20-transfer/ICS20Bank.sol

Lines changed: 0 additions & 102 deletions
This file was deleted.

contracts/apps/20-transfer/ICS20Lib.sol

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,10 @@
22
pragma solidity ^0.8.20;
33

44
import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";
5+
import {Height} from "../../proto/Client.sol";
56
import {IICS20Errors} from "./IICS20Errors.sol";
67

78
library ICS20Lib {
8-
/**
9-
* @dev PacketData is defined in [ICS-20](https://github.com/cosmos/ibc/tree/main/spec/app/ics-020-fungible-token-transfer).
10-
*/
11-
struct PacketData {
12-
string denom;
13-
string sender;
14-
string receiver;
15-
uint256 amount;
16-
string memo;
17-
}
18-
199
bytes internal constant SUCCESSFUL_ACKNOWLEDGEMENT_JSON = bytes('{"result":"AQ=="}');
2010
bytes internal constant FAILED_ACKNOWLEDGEMENT_JSON = bytes('{"error":"failed"}');
2111
bytes32 internal constant KECCAK256_SUCCESSFUL_ACKNOWLEDGEMENT_JSON = keccak256(SUCCESSFUL_ACKNOWLEDGEMENT_JSON);
@@ -33,6 +23,25 @@ library ICS20Lib {
3323

3424
bytes16 private constant HEX_DIGITS = "0123456789abcdef";
3525

26+
/**
27+
* @dev PacketData is defined in [ICS-20](https://github.com/cosmos/ibc/tree/main/spec/app/ics-020-fungible-token-transfer).
28+
*/
29+
struct PacketData {
30+
string denom;
31+
string sender;
32+
string receiver;
33+
uint256 amount;
34+
string memo;
35+
}
36+
37+
/**
38+
* @dev Either `height` or `timestampNanos` must be set.
39+
*/
40+
struct Timeout {
41+
Height.Data height;
42+
uint64 timestampNanos;
43+
}
44+
3645
/**
3746
* @dev marshalUnsafeJSON marshals PacketData into JSON bytes without escaping.
3847
* `memo` field is omitted if it is empty.
@@ -147,6 +156,23 @@ library ICS20Lib {
147156
return pd;
148157
}
149158

159+
/**
160+
* @dev timeout returns a Timeout struct with the given height.
161+
*/
162+
function timeout(uint64 revisionNumber, uint64 revisionHeight) internal pure returns (Timeout memory) {
163+
return Timeout({
164+
height: Height.Data({revision_number: revisionNumber, revision_height: revisionHeight}),
165+
timestampNanos: 0
166+
});
167+
}
168+
169+
/**
170+
* @dev timeout returns a Timeout struct with the given timestamp.
171+
*/
172+
function timeout(uint64 timestampNanos) internal pure returns (Timeout memory) {
173+
return Timeout({height: Height.Data({revision_number: 0, revision_height: 0}), timestampNanos: timestampNanos});
174+
}
175+
150176
/**
151177
* @dev parseUint256String parses `bz` from a position `pos` to produce a uint256 value.
152178
* The parse will stop parsing when it encounters a non-digit character.
@@ -372,4 +398,33 @@ library ICS20Lib {
372398
function equal(bytes memory a, bytes memory b) internal pure returns (bool) {
373399
return keccak256(a) == keccak256(b);
374400
}
401+
402+
/**
403+
* @dev denomPrefix returns the prefix of the denomination.
404+
*/
405+
function denomPrefix(string memory port, string memory channel) internal pure returns (bytes memory) {
406+
return abi.encodePacked(port, "/", channel, "/");
407+
}
408+
409+
/**
410+
* @dev denom returns the denomination string.
411+
*/
412+
function denom(string memory port, string memory channel, address tokenContract)
413+
internal
414+
pure
415+
returns (string memory)
416+
{
417+
return denom(port, channel, addressToHexString(tokenContract));
418+
}
419+
420+
/**
421+
* @dev denom returns the denomination string.
422+
*/
423+
function denom(string memory port, string memory channel, string memory baseDenom)
424+
internal
425+
pure
426+
returns (string memory)
427+
{
428+
return string(abi.encodePacked(denomPrefix(port, channel), baseDenom));
429+
}
375430
}

0 commit comments

Comments
 (0)