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: logs in callback contract are lost #1233

Merged
merged 11 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- [#1217](https://github.com/crypto-org-chain/cronos/pull/1217) Use the default chain-id behavour in sdk.
- [#1216](https://github.com/crypto-org-chain/cronos/pull/1216) Update ethermint to fix of avoid redundant parse chainID from gensis when start server.
- [#1230](https://github.com/crypto-org-chain/cronos/pull/1230) Fix mem store in versiondb multistore.
- [#1233](https://github.com/crypto-org-chain/cronos/pull/1233) Re-emit logs in callback contract.

*October 17, 2023*

Expand Down
91 changes: 89 additions & 2 deletions integration_tests/test_ica_precompile.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import base64
import itertools
import json
from enum import IntEnum

import pytest
from hexbytes import HexBytes
from pystarport import cluster
from web3.datastructures import AttributeDict

from .ibc_utils import (
Expand Down Expand Up @@ -45,9 +48,15 @@ class Status(IntEnum):
@pytest.fixture(scope="module")
def ibc(request, tmp_path_factory):
"prepare-network"
name = "ibc"
name = "ibc_rly"
path = tmp_path_factory.mktemp(name)
yield from prepare_network(path, name, incentivized=False, connection_only=True)
yield from prepare_network(
path,
name,
incentivized=False,
connection_only=True,
relayer=cluster.Relayer.RLY.value,
)


def register_acc(cli, w3, register, query, data, addr, channel_id):
Expand Down Expand Up @@ -181,6 +190,7 @@ def test_sc_call(ibc):
cli_host = ibc.chainmain.cosmos_cli()
cli_controller = ibc.cronos.cosmos_cli()
w3 = ibc.cronos.w3
begin_block = w3.eth.block_number
contract = w3.eth.contract(address=CONTRACT, abi=contract_info)
jsonfile = CONTRACTS["TestICA"]
tcontract = deploy_contract(w3, jsonfile)
Expand Down Expand Up @@ -302,3 +312,80 @@ def submit_msgs_ro(func, str):
assert expected_seq == last_seq
assert status == Status.FAIL
assert cli_host.balance(ica_address, denom=denom) == balance

# check logs emitted in callback
logs = get_logs_since(w3, addr, begin_block)
assert_logs_equal(
logs,
[
{
"address": addr,
"topics": [
HexBytes(
"0xb58d968e02022e42ce2b8f5c494b724806ff73755eb957ff2be7a62947"
"8ac9f9"
)
],
"data": HexBytes(
"0x00000000000000000000000000000000000000000000000000000000000000"
"010000000000000000000000000000000000000000000000000000000000000001"
),
"transactionIndex": 1,
"logIndex": 0,
"removed": False,
},
{
"address": addr,
"topics": [
HexBytes(
"0xb58d968e02022e42ce2b8f5c494b724806ff73755eb957ff2be7a62947"
"8ac9f9"
)
],
"data": HexBytes(
"0x00000000000000000000000000000000000000000000000000000000000000"
"020000000000000000000000000000000000000000000000000000000000000001"
),
"transactionIndex": 1,
"logIndex": 0,
"removed": False,
},
{
"address": addr,
"topics": [
HexBytes(
"0xb58d968e02022e42ce2b8f5c494b724806ff73755eb957ff2be7a62947"
"8ac9f9"
)
],
"data": HexBytes(
"0x00000000000000000000000000000000000000000000000000000000000000"
"030000000000000000000000000000000000000000000000000000000000000002"
),
"transactionIndex": 1,
"logIndex": 0,
"removed": False,
},
{
"address": addr,
"topics": [
HexBytes(
"0xb58d968e02022e42ce2b8f5c494b724806ff73755eb957ff2be7a62947"
"8ac9f9"
)
],
"data": HexBytes(
"0x00000000000000000000000000000000000000000000000000000000000000"
"040000000000000000000000000000000000000000000000000000000000000002"
),
"transactionIndex": 1,
"logIndex": 0,
"removed": False,
},
],
)


def assert_logs_equal(logs, exp_logs):
for exp_log, log in itertools.zip_longest(exp_logs, logs):
assert exp_log.items() <= log.items()
14 changes: 12 additions & 2 deletions x/cronos/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
cronosprecompiles "github.com/crypto-org-chain/cronos/v2/x/cronos/keeper/precompiles"
"github.com/crypto-org-chain/cronos/v2/x/cronos/types"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"
// this line is used by starport scaffolding # ibc/keeper/import
)

Expand Down Expand Up @@ -298,8 +299,17 @@ func (k Keeper) onPacketResult(
return err
}
gasLimit := k.GetParams(ctx).MaxCallbackGas
_, _, err = k.CallEVM(ctx, &contractAddr, data, big.NewInt(0), gasLimit)
return err
_, rsp, err := k.CallEVM(ctx, &contractAddr, data, big.NewInt(0), gasLimit)
if err != nil {
return err
}

if stateDB, ok := ctx.Value(types.StateDBContextKey).(vm.StateDB); ok {
for _, l := range rsp.Logs {
stateDB.AddLog(l.ToEthereum())
thomas-nguy marked this conversation as resolved.
Show resolved Hide resolved
}
}
return nil
}

func (k Keeper) IBCOnAcknowledgementPacketCallback(
Expand Down
2 changes: 2 additions & 0 deletions x/cronos/keeper/precompiles/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/crypto-org-chain/cronos/v2/x/cronos/types"
"github.com/ethereum/go-ethereum/common"
"github.com/evmos/ethermint/x/evm/statedb"
)
Expand Down Expand Up @@ -45,6 +46,7 @@ func exec[Req any, PReq interface {
var res Resp
if err := stateDB.ExecuteNativeAction(contract, converter, func(ctx sdk.Context) error {
var err error
ctx = ctx.WithValue(types.StateDBContextKey, stateDB)
yihuang marked this conversation as resolved.
Show resolved Hide resolved
res, err = action(ctx, msg)
return err
}); err != nil {
Expand Down
2 changes: 2 additions & 0 deletions x/cronos/types/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const (
prefixContractToDenom
paramsKey
prefixAdminToPermissions

StateDBContextKey = "statedb"
)

// KVStore key prefixes
Expand Down
Loading