Skip to content

Commit

Permalink
Problem: duplicate cache events are emitted (crypto-org-chain#1102)
Browse files Browse the repository at this point in the history
* avoid emit duplicate events

for more info, see https://github.com/cosmos/cosmos-sdk/pull/13063/files

* test dup in ibc

* add change doc

* test dup in gravity

* pin nixpkgs flake

* pin to local nixpkgs

* pin to 23.05

* fix unit test

---------

Co-authored-by: HuangYi <huang@crypto.com>
  • Loading branch information
mmsqe and yihuang committed Jul 18, 2023
1 parent 0a4a09f commit bae1630
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 3 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,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)"
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,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)"
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

- [#1058](https://github.com/crypto-org-chain/cronos/pull/1058) Fix decode log for multi topics in websocket subscribe ([ethermint commit](https://github.com/crypto-org-chain/ethermint/commit/2136ad029860c819942ad1836dd3f42585002233)).
- [#1062](https://github.com/crypto-org-chain/cronos/pull/1062) Update cometbft `v0.34.29` with several minor bug fixes and low-severity security-fixes.
- [#1102](https://github.com/crypto-org-chain/cronos/pull/1102) avoid duplicate cache events emitted from ibc and gravity hook.

*Jun 9, 2023*

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ require (
github.com/tendermint/tendermint v0.34.28
github.com/tendermint/tm-db v0.6.7
golang.org/x/exp v0.0.0-20230310171629-522b1b587ee0
golang.org/x/sys v0.8.0
google.golang.org/genproto v0.0.0-20230323212658-478b75c54725
google.golang.org/grpc v1.54.0
google.golang.org/protobuf v1.30.0
Expand Down Expand Up @@ -208,6 +207,7 @@ require (
golang.org/x/net v0.10.0 // indirect
golang.org/x/oauth2 v0.6.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/term v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
Expand Down
12 changes: 12 additions & 0 deletions integration_tests/test_gravity.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json

import pytest
import requests
import sha3
import toml
from dateutil.parser import isoparse
Expand Down Expand Up @@ -382,6 +383,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}"


@pytest.mark.skip(reason="gravity-bridge not supported in v1.0.x")
def test_direct_token_mapping(gravity):
Expand Down
23 changes: 23 additions & 0 deletions integration_tests/test_ibc.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,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
Expand Down Expand Up @@ -65,6 +80,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:
Expand Down
1 change: 0 additions & 1 deletion x/cronos/keeper/gravity_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
1 change: 0 additions & 1 deletion x/cronos/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,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",
Expand Down

0 comments on commit bae1630

Please sign in to comment.