Skip to content

Commit

Permalink
Problem: failed tx are ignored in json-rpc apis (backport crypto-org-…
Browse files Browse the repository at this point in the history
…chain#502)

Solution:
- fix the issue in ethermint by emitting extra events in ante handler.
- add integration test to test it.
- update cosmos-sdk dependency
- fix getTransactionReceiptsByBlock

fix integration test

move to crypto-org-chain repo

Update CHANGELOG.md

update cosmos-sdk to most recent

cosmos-sdk v0.45.5

Problem: gravity bridge integration test fails occasionally (crypto-org-chain#538)

* Problem: gravity bridge integration test fails occasionally

WIP: crypto-org-chain#48

- Add traceback env var to debug

* run ibc test together

* avoid port conflict

* Update scripts/run-integration-tests
  • Loading branch information
yihuang committed Jun 13, 2022
1 parent cdfe53c commit b90b527
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 122 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Bug Fixes

- [#454](https://github.com/crypto-org-chain/cronos/pull/454) Add back the latest testnet upgrade handler.
- [cronos#503](https://github.com/crypto-org-chain/cronos/pull/503) Fix failed tx are ignored in json-rpc apis (backport #502).

### Improvements

Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.17

require (
github.com/armon/go-metrics v0.3.10
github.com/cosmos/cosmos-sdk v0.45.4
github.com/cosmos/cosmos-sdk v0.45.5
github.com/cosmos/ibc-go/v2 v2.2.0
github.com/ethereum/go-ethereum v1.10.15
github.com/gogo/protobuf v1.3.3
Expand All @@ -13,7 +13,7 @@ require (
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/peggyjv/gravity-bridge/module v0.3.9
github.com/rakyll/statik v0.1.7
github.com/spf13/cast v1.4.1
github.com/spf13/cast v1.5.0
github.com/spf13/cobra v1.4.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.1
Expand Down Expand Up @@ -153,14 +153,14 @@ require (
replace (
// TODO: fix keyring upstream
github.com/99designs/keyring => github.com/crypto-org-chain/keyring v1.1.6-fixes
github.com/cosmos/cosmos-sdk => github.com/cosmos/cosmos-sdk v0.45.4
github.com/cosmos/cosmos-sdk => github.com/cosmos/cosmos-sdk v0.45.5

// TODO: remove when middleware will be implemented
github.com/cosmos/ibc-go/v2 => github.com/crypto-org-chain/ibc-go/v2 v2.2.0-hooks2

github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1

github.com/tharsis/ethermint => github.com/crypto-org-chain/ethermint v0.10.0-alpha1-cronos-9
github.com/tharsis/ethermint => github.com/crypto-org-chain/ethermint v0.10.0-alpha1-cronos-10

google.golang.org/grpc => google.golang.org/grpc v1.33.2
)
18 changes: 12 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,8 @@ github.com/coreos/pkg v0.0.0-20160727233714-3ac0863d7acf/go.mod h1:E3G3o1h8I7cfc
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/cosmos/btcutil v1.0.4 h1:n7C2ngKXo7UC9gNyMNLbzqz7Asuf+7Qv4gnX/rOdQ44=
github.com/cosmos/btcutil v1.0.4/go.mod h1:Ffqc8Hn6TJUdDgHBwIZLtrLQC1KdJ9jGJl/TvgUaxbU=
github.com/cosmos/cosmos-sdk v0.45.4 h1:eStDAhJdMY8n5arbBRe+OwpNeBSunxSBHp1g55ulfdA=
github.com/cosmos/cosmos-sdk v0.45.4/go.mod h1:WOqtDxN3eCCmnYLVla10xG7lEXkFjpTaqm2a2WasgCc=
github.com/cosmos/cosmos-sdk v0.45.5 h1:GVrZM+lss6y626Pq6loxh/3KLRgK/J6/alTkcKkYmGU=
github.com/cosmos/cosmos-sdk v0.45.5/go.mod h1:WOqtDxN3eCCmnYLVla10xG7lEXkFjpTaqm2a2WasgCc=
github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d/go.mod h1:tSxLoYXyBmiFeKpvmq4dzayMdCjCnu8uqmCysIGBT2Y=
github.com/cosmos/go-bip39 v1.0.0 h1:pcomnQdrdH22njcAatO0yWojsUnCO3y2tNoV1cb6hHY=
github.com/cosmos/go-bip39 v1.0.0/go.mod h1:RNJv0H/pOIVgxw6KS7QeX2a0Uo0aKUlfhZ4xuwvCdJw=
Expand All @@ -271,8 +271,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsr
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/crypto-org-chain/ethermint v0.10.0-alpha1-cronos-9 h1:szlis/QH5l7ETOsB3Ot/aItDywBDlV4i7ZjRROdIBFI=
github.com/crypto-org-chain/ethermint v0.10.0-alpha1-cronos-9/go.mod h1:FLjeCtSiiWzDhAKO6usihVrnJwGw88I+KGYfr/geQSs=
github.com/crypto-org-chain/ethermint v0.10.0-alpha1-cronos-10 h1:QLICrljqG8eJ4X+JCqvWf1Lbg/TXS+lxoFbcezBpyjc=
github.com/crypto-org-chain/ethermint v0.10.0-alpha1-cronos-10/go.mod h1:T4f1p3L5kccbjBZMtJLKfOLiUXTO1OuS1I7FzQuJD+w=
github.com/crypto-org-chain/ibc-go/v2 v2.2.0-hooks2 h1:elj+Tb/3O9GA3pv62zkc1B0P8hl1WHmF6vF8PInEJm4=
github.com/crypto-org-chain/ibc-go/v2 v2.2.0-hooks2/go.mod h1:rAHRlBcRiHPP/JszN+08SJx3pegww9bcVncIb9QLx7I=
github.com/crypto-org-chain/keyring v1.1.6-fixes h1:AUFSu56NY6XobY6XfRoDx6v3loiOrHK5MNUm32GEjwA=
Expand Down Expand Up @@ -364,6 +364,8 @@ github.com/franela/goblin v0.0.0-20200105215937-c9ffbefa60db/go.mod h1:7dvUGVsVB
github.com/franela/goblin v0.0.0-20210519012713-85d372ac71e2/go.mod h1:VzmDKDJVZI3aJmnRI9VjAn9nJ8qPPsN1fqzr9dqInIo=
github.com/franela/goreq v0.0.0-20171204163338-bcd34c9993f8/go.mod h1:ZhphrRTfi2rbfLwlschooIH4+wKKDR4Pdxhh+TRoA20=
github.com/frankban/quicktest v1.11.3/go.mod h1:wRf/ReqHper53s+kmmSZizM8NamnL3IM0I9ntUbOk+k=
github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE=
github.com/frankban/quicktest v1.14.3/go.mod h1:mgiwOwqx65TmIk1wJ6Q7wvnVMocbUorkibMOrVTHZps=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
github.com/fsnotify/fsnotify v1.5.1 h1:mZcQUHVQUQWoPXXtuf9yuEXKudkV2sx1E06UadKWpgI=
Expand Down Expand Up @@ -716,8 +718,9 @@ github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand Down Expand Up @@ -998,6 +1001,8 @@ github.com/rjeczalik/notify v0.9.1/go.mod h1:rKwnCoCGeuQnwBtTSPL9Dad03Vh2n40ePRr
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rs/cors v0.0.0-20160617231935-a62a804a8a00/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/rs/cors v1.8.2 h1:KCooALfAYGs415Cwu5ABvv9n9509fSiG5SQJn/AQo4U=
Expand Down Expand Up @@ -1049,8 +1054,9 @@ github.com/spf13/afero v1.8.2 h1:xehSyVa0YnHWsJ49JFljMpg1HX19V6NDZ1fkm1Xznbo=
github.com/spf13/afero v1.8.2/go.mod h1:CtAatgMJh6bJEIs48Ay/FOnkljP3WeGUG0MC1RfAqwo=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cast v1.3.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cast v1.4.1 h1:s0hze+J0196ZfEMTs80N7UlFt0BDuQ7Q+JDnHiMWKdA=
github.com/spf13/cast v1.4.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cast v1.5.0 h1:rj3WzYc11XZaIZMPKmwP96zkFEnnAmV8s6XbB2aY32w=
github.com/spf13/cast v1.5.0/go.mod h1:SpXXQ5YoyJw6s3/6cMTQuxvgRl3PCJiyaX9p6b155UU=
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
github.com/spf13/cobra v0.0.5/go.mod h1:3K3wKZymM7VvHMDS9+Akkh4K60UwM26emMESw8tLCHU=
github.com/spf13/cobra v1.0.0/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE=
Expand Down
24 changes: 12 additions & 12 deletions gomod2nix.toml
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,8 @@ schema = 1
version = "v1.0.4"
hash = "sha256-JvcBXBdjdmnaW/nyf/tw/uaOAGn1b78yxrtl2/Rs3kA="
[mod."github.com/cosmos/cosmos-sdk"]
version = "v0.45.4"
hash = "sha256-ll4HgVrDhwN/2vWP5fVraZT4MbrCsokwJLvcA0qP3fE="
version = "v0.45.5"
hash = "sha256-AW9ogzt//VLXKRwKsa7AgmBAfnVqm8MLl2PGa2zUvSI="
replaced = "github.com/cosmos/cosmos-sdk"
[mod."github.com/cosmos/go-bip39"]
version = "v1.0.0"
Expand Down Expand Up @@ -536,8 +536,8 @@ schema = 1
version = "v0.0.0-20171204163338-bcd34c9993f8"
hash = "sha256-Sx911OBB67XYUSbjoGtJ5I/WL+aXR67FpCu4wiwQqhQ="
[mod."github.com/frankban/quicktest"]
version = "v1.11.3"
hash = "sha256-Gg+S16VMcweQybw9RhiwdJockd/U1kh3AkhgN3mVtjQ="
version = "v1.14.3"
hash = "sha256-WCIb0lIRAaluIhKSUJNKpnNt4/mOII8/SwVXUeY9NWQ="
[mod."github.com/fsnotify/fsnotify"]
version = "v1.5.1"
hash = "sha256-B8kZ8yiWgallT7R2j1kSRJcJkSGFVf9ise+TpXa+7XY="
Expand Down Expand Up @@ -969,8 +969,8 @@ schema = 1
version = "v0.0.0-20140226030751-b84e30acd515"
hash = "sha256-CePQbqWGtS8qP/Av9pkLiqZwH6RaZQff/s1l+1//jQo="
[mod."github.com/kr/pretty"]
version = "v0.2.1"
hash = "sha256-HhaxBizBlvhiQ+tuByY69++QCtB+gt6t0A/h5A347m8="
version = "v0.3.0"
hash = "sha256-5KfVas96NAU7pmN2ZOwQFFpUeagiJTqYKBLxq5aM5W4="
[mod."github.com/kr/pty"]
version = "v1.1.1"
hash = "sha256-AVeS+ivwNzIrgWQaLtsmO2f2MYGpxIVqdac/EzaYI1Q="
Expand Down Expand Up @@ -1293,8 +1293,8 @@ schema = 1
version = "v1.2.0"
hash = "sha256-yDnHsLw8KF4zfbJska1igIo4IbVpGDq+vH3+YV5jCgk="
[mod."github.com/rogpeppe/go-internal"]
version = "v1.3.0"
hash = "sha256-JgiasZeXDy10syy7wmXtqRffDY7CJ1o5VNY+FmmAjVU="
version = "v1.6.1"
hash = "sha256-1IBwoA4d15vIBLjkGGVuBoNXh/jTRh74bu/pFe7dQgI="
[mod."github.com/rs/cors"]
version = "v1.8.2"
hash = "sha256-FxBbQVb8zIoDGGaAJKfsYV0+POkh1tyX7P6A/Lk5Vsc="
Expand Down Expand Up @@ -1371,8 +1371,8 @@ schema = 1
version = "v1.8.2"
hash = "sha256-ERuG4GK1LOn72DRbT9URpo1QoVIm2A3mPtlf+xSPgx8="
[mod."github.com/spf13/cast"]
version = "v1.4.1"
hash = "sha256-jaY+/RKUviKnE2h8Ly5cdZYinSE0uc32FW6+xfZ1Ghs="
version = "v1.5.0"
hash = "sha256-Pdp+wC5FWqyJKzyYHb7JCcV9BoJk/sxQw6nLyuLJvuQ="
[mod."github.com/spf13/cobra"]
version = "v1.4.0"
hash = "sha256-I6j9sD61Ztcc2W/WGeWo3ggYtnGTxNxZ2EFPdtO0UEY="
Expand Down Expand Up @@ -1434,8 +1434,8 @@ schema = 1
version = "v0.6.7"
hash = "sha256-hl/3RrBrpkk2zA6dmrNlIYKs1/GfqegSscDSkA5Pjlo="
[mod."github.com/tharsis/ethermint"]
version = "v0.10.0-alpha1-cronos-9"
hash = "sha256-0UYsRcen9DBGo1ykI3pUg/6Tnw2vBYtuSEJ33/jn9yE="
version = "v0.10.0-alpha1-cronos-10"
hash = "sha256-EfYOvGreGToosDcqg1MBCHwSRSFdBIVvMlXIlYFFH0A="
replaced = "github.com/crypto-org-chain/ethermint"
[mod."github.com/tidwall/gjson"]
version = "v1.6.7"
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/cosmoscli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1068,4 +1068,4 @@ def fix_unlucky_tx(self, start_block, end_block):
min_block_height=1,
home=self.data_dir,
).decode()
return [tuple(line.split()[1:]) for line in output.split("\n")]
return [tuple(line.split()[1:]) for line in output.split("\n") if line]
3 changes: 2 additions & 1 deletion integration_tests/test_gravity.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def cronos(request, tmp_path_factory):
params: enable_auto_deployment
"""
yield from setup_cronos_experimental(
tmp_path_factory.mktemp("cronos_experimental"), 26700, request.param
tmp_path_factory.mktemp("cronos_experimental"), 26600, request.param
)


Expand Down Expand Up @@ -171,6 +171,7 @@ def gravity(cronos, geth):
f'gorc -c "{gorc_config_path}" orchestrator start '
"--cosmos-key cronos --ethereum-key eth"
),
"environment": "RUST_BACKTRACE=full",
"autostart": "true",
"autorestart": "true",
"startsecs": "3",
Expand Down
54 changes: 34 additions & 20 deletions integration_tests/test_replay_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import pytest
import web3
from pystarport import ports
from web3._utils.method_formatters import receipt_formatter
from web3.datastructures import AttributeDict

Expand All @@ -14,7 +13,6 @@
deploy_contract,
sign_transaction,
supervisorctl,
wait_for_port,
)


Expand All @@ -37,19 +35,23 @@ def test_replay_block(custom_cronos):
)
iterations = 400
gas_limit = 800000
gas_price = 100000000000
for i in range(10):
nonce = w3.eth.get_transaction_count(ADDRS["validator"])
begin_balance = w3.eth.get_balance(ADDRS["validator"])
txs = [
contract.functions.test(iterations).buildTransaction(
{
"nonce": nonce,
"gas": gas_limit,
"gasPrice": gas_price,
}
),
contract.functions.test(iterations).buildTransaction(
{
"nonce": nonce + 1,
"gas": gas_limit,
"gasPrice": gas_price,
}
),
]
Expand All @@ -58,12 +60,8 @@ def test_replay_block(custom_cronos):
for tx in txs
]
receipt1 = w3.eth.wait_for_transaction_receipt(txhashes[0])
try:
receipt2 = w3.eth.wait_for_transaction_receipt(txhashes[1], timeout=10)
except web3.exceptions.TimeExhausted:
# expected exception, tx2 is included but failed.
receipt2 = None
break
# the tx2 should be included in json-rpc response too.
receipt2 = w3.eth.wait_for_transaction_receipt(txhashes[1], timeout=10)
if receipt1.blockNumber == receipt2.blockNumber:
break
print(
Expand All @@ -73,9 +71,34 @@ def test_replay_block(custom_cronos):
)
else:
assert False, "timeout"
assert not receipt2

# the first tx succeds.
assert receipt1.status == 1
assert receipt1.gasUsed < gas_limit
assert receipt1.cumulativeGasUsed == receipt1.gasUsed

# the second tx should fail and cost the whole gasLimit
assert receipt2.status == 0
assert receipt2.gasUsed == gas_limit
assert receipt2.cumulativeGasUsed == receipt1.cumulativeGasUsed + gas_limit

# check get block apis
assert w3.eth.get_block(receipt1.blockNumber).transactions == [
receipt1.transactionHash,
receipt2.transactionHash,
]
assert (
w3.eth.get_transaction_by_block(receipt1.blockNumber, 1).hash
== receipt2.transactionHash
)

# check sender's nonce is increased twice, which means both txs are executed.
assert nonce + 2 == w3.eth.get_transaction_count(ADDRS["validator"])
# check sender's balance is deducted as expected
assert receipt2.cumulativeGasUsed * gas_price == begin_balance - w3.eth.get_balance(
ADDRS["validator"]
)

rsp = w3.provider.make_request(
"cronos_replayBlock", [hex(receipt1.blockNumber), False]
)
Expand Down Expand Up @@ -111,19 +134,10 @@ def test_replay_block(custom_cronos):
assert replay_receipts[1].status == 0
assert replay_receipts[1].gasUsed == gas_limit

# patch the unlucky tx with the new cli command
# try to patch the unlucky tx, should have nothing to patch.
# stop the node0
end_height = cli.block_height()
supervisorctl(custom_cronos.base_dir / "../tasks.ini", "stop", "cronos_777-1-node0")
cli = custom_cronos.cosmos_cli()
results = cli.fix_unlucky_tx(begin_height, end_height)
# the second tx is patched
assert results[0][1] == txhashes[1].hex()
# start the node0 again
supervisorctl(
custom_cronos.base_dir / "../tasks.ini", "start", "cronos_777-1-node0"
)
# wait for tm-rpc port
wait_for_port(ports.rpc_port(custom_cronos.base_port(0)))
# check the tx indexer
assert len(cli.txs(f"ethereum_tx.ethereumTxHash={txhashes[1].hex()}")["txs"]) == 1
assert not results
49 changes: 30 additions & 19 deletions x/cronos/rpc/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,20 @@ func (api *CronosAPI) GetTransactionReceiptsByBlock(blockNrOrHash rpctypes.Block
cumulativeGasUsed := uint64(0)
for i, tx := range resBlock.Block.Txs {
txResult := blockRes.TxsResults[i]
if txResult.Code != 0 && txResult.Log != "" {
// skip failed transaction

// don't ignore the txs which exceed block gas limit.
if !backend.TxSuccessOrExceedsBlockGasLimit(txResult) {
continue
}

parsedTxs, err := rpctypes.ParseTxResult(txResult)
if err != nil {
return nil, fmt.Errorf("failed to parse tx events: %d:%d, %v", resBlock.Block.Height, i, err)
}

if len(parsedTxs.Txs) == 0 {
// not an evm tx
cumulativeGasUsed += uint64(txResult.GasUsed)
continue
}

Expand All @@ -139,14 +151,16 @@ func (api *CronosAPI) GetTransactionReceiptsByBlock(blockNrOrHash rpctypes.Block
return nil, fmt.Errorf("failed to decode tx: %w", err)
}

msgEvents, err := ParseEthTxEvents(txResult.Events)
if err != nil {
api.logger.Debug("parse tx events failed", "txIndex", txIndex, "error", err.Error())
return nil, fmt.Errorf("failed to parse tx events: %d %w", txIndex, err)
if len(parsedTxs.Txs) != len(tx.GetMsgs()) {
return nil, fmt.Errorf("wrong number of tx events: %d", txIndex)
}

if len(msgEvents) != len(tx.GetMsgs()) {
return nil, fmt.Errorf("wrong number of tx events: %d", txIndex)
if txResult.Code != 0 {
// tx failed, we should return gas limit as gas used, because that's how the fee get deducted.
for j := 0; j < len(parsedTxs.Txs); j++ {
gasLimit := tx.GetMsgs()[j].(*evmtypes.MsgEthereumTx).GetGas()
parsedTxs.Txs[j].GasUsed = gasLimit
}
}

msgCumulativeGasUsed := uint64(0)
Expand All @@ -163,17 +177,11 @@ func (api *CronosAPI) GetTransactionReceiptsByBlock(blockNrOrHash rpctypes.Block
return nil, err
}

var gasUsed uint64
if len(tx.GetMsgs()) == 1 {
// backward compatibility
gasUsed = uint64(txResult.GasUsed)
} else {
gasUsed = msgEvents[msgIndex].GasUsed
}
parsedTx := parsedTxs.GetTxByMsgIndex(msgIndex)

// Get the transaction result from the log
var status hexutil.Uint
if msgEvents[msgIndex].Failed {
if txResult.Code != 0 || parsedTx.Failed {
status = hexutil.Uint(ethtypes.ReceiptStatusFailed)
} else {
status = hexutil.Uint(ethtypes.ReceiptStatusSuccessful)
Expand All @@ -184,12 +192,15 @@ func (api *CronosAPI) GetTransactionReceiptsByBlock(blockNrOrHash rpctypes.Block
return nil, err
}

logs := msgEvents[msgIndex].Logs
logs, err := parsedTx.ParseTxLogs()
if err != nil {
api.logger.Debug("failed to parse logs", "block", resBlock.Block.Height, "txIndex", txIndex, "msgIndex", msgIndex, "error", err.Error())
}
if logs == nil {
logs = []*ethtypes.Log{}
}
// msgCumulativeGasUsed includes gas used by the current tx
msgCumulativeGasUsed += gasUsed
msgCumulativeGasUsed += parsedTx.GasUsed
receipt := map[string]interface{}{
// Consensus fields: These fields are defined by the Yellow Paper
"status": status,
Expand All @@ -201,7 +212,7 @@ func (api *CronosAPI) GetTransactionReceiptsByBlock(blockNrOrHash rpctypes.Block
// They are stored in the chain database.
"transactionHash": ethMsg.Hash,
"contractAddress": nil,
"gasUsed": hexutil.Uint64(gasUsed),
"gasUsed": hexutil.Uint64(parsedTx.GasUsed),
"type": hexutil.Uint(txData.TxType()),

// Inclusion information: These fields provide information about the inclusion of the
Expand Down
Loading

0 comments on commit b90b527

Please sign in to comment.