Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem: ica packet callback doesn't contain channel detail #1235

Merged
merged 13 commits into from
Nov 29, 2023
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
### State Machine Breaking

- [#1232](https://github.com/crypto-org-chain/cronos/pull/1232) Adjust require gas in relayer precompile to be closed with actual consumed.
- [#1235](https://github.com/crypto-org-chain/cronos/pull/1235) Add channel detail in ica packet callback.


*October 17, 2023*
Expand Down
14 changes: 9 additions & 5 deletions integration_tests/contracts/contracts/TestICA.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ contract TestICA {
SUCCESS,
FAIL
}
mapping (uint64 => Status) public statusMap;
event OnPacketResult(uint64 seq, Status status);
mapping (string => mapping (uint64 => Status)) public statusMap;
event OnPacketResult(string indexed packetSrcChannel, uint64 seq, Status status);

function encodeRegister(string memory connectionID, string memory version) internal view returns (bytes memory) {
return abi.encodeWithSignature(
Expand Down Expand Up @@ -102,15 +102,19 @@ contract TestICA {
return lastSeq;
}

function onPacketResultCallback(uint64 seq, bool ack) external payable returns (bool) {
function getStatus(string calldata packetSrcChannel, uint64 seq) public view returns (Status) {
return statusMap[packetSrcChannel][seq];
}

function onPacketResultCallback(string calldata packetSrcChannel, uint64 seq, bool ack) external payable returns (bool) {
// To prevent called by arbitrary user
require(msg.sender == module_address);
Status status = Status.FAIL;
if (ack) {
status = Status.SUCCESS;
}
statusMap[seq] = status;
emit OnPacketResult(seq, status);
statusMap[packetSrcChannel][seq] = status;
emit OnPacketResult(packetSrcChannel, seq, status);
return true;
}
}
35 changes: 18 additions & 17 deletions integration_tests/test_ica_precompile.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,21 +168,21 @@ def test_call(ibc):
assert cli_host.balance(ica_address, denom=denom) == balance


def wait_for_status_change(tcontract, seq):
def wait_for_status_change(tcontract, channel_id, seq):
print(f"wait for status change for {seq}")

def check_status():
status = tcontract.caller.statusMap(seq)
status = tcontract.caller.getStatus(channel_id, seq)
print("current", status)
return status

wait_for_fn("current status", check_status)


def assert_packet_result(event, seq, status):
def assert_packet_result(event, channel_id, seq, status):
(logs) = event.getLogs()
assert len(logs) > 0
return logs[0].args == AttributeDict({"seq": seq, "status": status})
return logs[0].args == AttributeDict({"channel_id": channel_id, "seq": seq, "status": status})


def test_sc_call(ibc):
Expand All @@ -198,14 +198,15 @@ def test_sc_call(ibc):
signer = ADDRS[name]
keys = KEYS[name]
data = {"from": signer, "gas": 400000}
channel_id = get_next_channel(cli_controller, connid)
ica_address = register_acc(
cli_controller,
w3,
tcontract.functions.callRegister,
contract.functions.queryAccount,
data,
addr,
get_next_channel(cli_controller, connid),
channel_id,
)
balance = funds_ica(cli_host, ica_address)
assert tcontract.caller.getAccount() == signer
Expand Down Expand Up @@ -249,11 +250,11 @@ def submit_msgs_ro(func, str):
submit_msgs_ro(tcontract.functions.delegateSubmitMsgs, str)
submit_msgs_ro(tcontract.functions.staticSubmitMsgs, str)
last_seq = tcontract.caller.getLastSeq()
wait_for_status_change(tcontract, last_seq)
status = tcontract.caller.statusMap(last_seq)
wait_for_status_change(tcontract, channel_id, last_seq)
status = tcontract.caller.getStatus(channel_id, last_seq)
assert expected_seq == last_seq
assert status == Status.SUCCESS
assert_packet_result(tcontract.events.OnPacketResult, last_seq, status)
assert_packet_result(tcontract.events.OnPacketResult, channel_id, last_seq, status)
balance -= diff
assert cli_host.balance(ica_address, denom=denom) == balance

Expand All @@ -270,11 +271,11 @@ def submit_msgs_ro(func, str):
submit_msgs_ro(tcontract.functions.delegateSubmitMsgs, str)
submit_msgs_ro(tcontract.functions.staticSubmitMsgs, str)
last_seq = tcontract.caller.getLastSeq()
wait_for_status_change(tcontract, last_seq)
status = tcontract.caller.statusMap(last_seq)
wait_for_status_change(tcontract, channel_id, last_seq)
status = tcontract.caller.getStatus(channel_id, last_seq)
assert expected_seq == last_seq
assert status == Status.SUCCESS
assert_packet_result(tcontract.events.OnPacketResult, last_seq, status)
assert_packet_result(tcontract.events.OnPacketResult, channel_id, last_seq, status)
balance -= diff
assert cli_host.balance(ica_address, denom=denom) == balance

Expand All @@ -292,11 +293,11 @@ def submit_msgs_ro(func, str):
need_wait=False,
)
last_seq = tcontract.caller.getLastSeq()
wait_for_status_change(tcontract, last_seq)
status = tcontract.caller.statusMap(last_seq)
wait_for_status_change(tcontract, channel_id, last_seq)
status = tcontract.caller.getStatus(channel_id, last_seq)
assert expected_seq == last_seq
assert status == Status.FAIL
assert_packet_result(tcontract.events.OnPacketResult, last_seq, status)
assert_packet_result(tcontract.events.OnPacketResult, channel_id, last_seq, status)
assert cli_host.balance(ica_address, denom=denom) == balance

# balance should not change on timeout
Expand All @@ -313,9 +314,9 @@ def submit_msgs_ro(func, str):
timeout,
)
last_seq = tcontract.caller.getLastSeq()
wait_for_status_change(tcontract, last_seq)
status = tcontract.caller.statusMap(last_seq)
wait_for_status_change(tcontract, channel_id, last_seq)
status = tcontract.caller.getStatus(channel_id, last_seq)
assert expected_seq == last_seq
assert status == Status.FAIL
assert_packet_result(tcontract.events.OnPacketResult, last_seq, status)
assert_packet_result(tcontract.events.OnPacketResult, channel_id, last_seq, status)
assert cli_host.balance(ica_address, denom=denom) == balance

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion x/cronos/events/bindings/src/ICACallback.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
pragma solidity ^0.8.4;

interface IICACallback {
function onPacketResultCallback(uint64 seq, bool ack) external payable returns (bool);
function onPacketResultCallback(string calldata packetSrcChannel, uint64 seq, bool ack) external payable returns (bool);
}
3 changes: 1 addition & 2 deletions x/cronos/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,7 @@ func (k Keeper) onPacketResult(
packetSenderAddress string,
) error {
contractAddr := common.HexToAddress(contractAddress)

data, err := cronosprecompiles.OnPacketResultCallback(packet.Sequence, acknowledgement)
data, err := cronosprecompiles.OnPacketResultCallback(packet.SourceChannel, packet.Sequence, acknowledgement)
if err != nil {
return err
}
Expand Down