diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index bbddd251d4..47e5f94eab 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -79,6 +79,7 @@ jobs: make test # run versiondb tests + nix registry add nixpkgs github:NixOS/nixpkgs/23.05 nix profile install nixpkgs#rocksdb export PKG_CONFIG_PATH=$HOME/.nix-profile/lib/pkgconfig export CGO_CFLAGS="$(pkg-config --cflags rocksdb)" CGO_LDFLAGS="$(pkg-config --libs rocksdb)" diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index e718d73a32..5d45f84a24 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -41,6 +41,7 @@ jobs: *.sum - name: run golangci-lint run: | + nix registry add nixpkgs github:NixOS/nixpkgs/23.05 nix profile install nixpkgs#rocksdb nixpkgs#golangci-lint export PKG_CONFIG_PATH=$HOME/.nix-profile/lib/pkgconfig export CGO_CFLAGS="$(pkg-config --cflags rocksdb)" CGO_LDFLAGS="$(pkg-config --libs rocksdb)" diff --git a/CHANGELOG.md b/CHANGELOG.md index 337a17ec86..ef9697674f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ - [#1073](https://github.com/crypto-org-chain/cronos/pull/1073) memiavl automatically truncate corrupted wal tail. - [#1087](https://github.com/crypto-org-chain/cronos/pull/1087) memiavl fix LastCommitID when memiavl db not loaded. - [#1088](https://github.com/crypto-org-chain/cronos/pull/1088) memiavl fix empty value in write-ahead-log replaying. +- [#1102](https://github.com/crypto-org-chain/cronos/pull/1102) avoid duplicate cache events emitted from ibc and gravity hook. ### Features diff --git a/integration_tests/test_gravity.py b/integration_tests/test_gravity.py index 59f2262307..faf832b030 100644 --- a/integration_tests/test_gravity.py +++ b/integration_tests/test_gravity.py @@ -1,9 +1,11 @@ import json import pytest +import requests from eth_account.account import Account from eth_utils import abi from hexbytes import HexBytes +from pystarport import ports from web3.exceptions import BadFunctionCallOutput from .gravity_utils import prepare_gravity, setup_cosmos_erc20_contract @@ -295,6 +297,17 @@ def check(): wait_for_fn("check balance on cronos", check) + # check duplicate end_block_events + height = cli.block_height() + port = ports.rpc_port(gravity.cronos.base_port(0)) + url = f"http://127.0.0.1:{port}/block_results?height={height}" + res = requests.get(url).json().get("result") + if res: + events = res["end_block_events"] + target = "ethereum_send_to_cosmos_handled" + count = sum(1 for evt in events if evt["type"] == target) + assert count <= 2, f"duplicate {target}" + def test_direct_token_mapping(gravity): """ diff --git a/integration_tests/test_ibc.py b/integration_tests/test_ibc.py index 30ebe17bf3..ff5a4dceea 100644 --- a/integration_tests/test_ibc.py +++ b/integration_tests/test_ibc.py @@ -37,6 +37,21 @@ def get_balances(chain, addr): return chain.cosmos_cli().balances(addr) +def find_duplicate(attributes): + res = set() + key = attributes[0]["key"] + for attribute in attributes: + if attribute["key"] == key: + value0 = attribute["value"] + elif attribute["key"] == "amount": + amount = attribute["value"] + value_pair = f"{value0}:{amount}" + if value_pair in res: + return value_pair + res.add(value_pair) + return None + + def test_ibc_transfer_with_hermes(ibc): """ test ibc transfer tokens with hermes cli @@ -68,6 +83,14 @@ def check_balance_change(): # rather than the normal gas price assert fee == gas * 1000000 + # check duplicate OnRecvPacket events + criteria = "message.action=/ibc.core.channel.v1.MsgRecvPacket" + tx = cli.tx_search(criteria)["txs"][0] + events = tx["logs"][1]["events"] + for event in events: + dup = find_duplicate(event["attributes"]) + assert not dup, f"duplicate {dup} in {event['type']}" + def test_ibc_incentivized_transfer(ibc): if not ibc.incentivized: diff --git a/x/cronos/keeper/gravity_hooks.go b/x/cronos/keeper/gravity_hooks.go index 139430554d..610d347f28 100644 --- a/x/cronos/keeper/gravity_hooks.go +++ b/x/cronos/keeper/gravity_hooks.go @@ -44,7 +44,6 @@ func (k Keeper) AfterSendToCosmosEvent(ctx sdk.Context, event gravitytypes.SendT err := k.doAfterSendToCosmosEvent(cacheCtx, event) if err == nil { commit() - ctx.EventManager().EmitEvents(cacheCtx.EventManager().Events()) } else { k.Logger(ctx).Error("AfterSendToCosmosEvent hook failed", "error", err) } diff --git a/x/cronos/keeper/keeper.go b/x/cronos/keeper/keeper.go index ab3767031c..c7541eae0c 100644 --- a/x/cronos/keeper/keeper.go +++ b/x/cronos/keeper/keeper.go @@ -202,7 +202,6 @@ func (k Keeper) OnRecvVouchers( err := k.ConvertVouchersToEvmCoins(cacheCtx, receiver, tokens) if err == nil { commit() - ctx.EventManager().EmitEvents(cacheCtx.EventManager().Events()) } else { k.Logger(ctx).Error( fmt.Sprintf("Failed to convert vouchers to evm tokens for receiver %s, coins %s. Receive error %s",