From 3f0b77785adfae9b2ff83847c52f17bcdf3a0582 Mon Sep 17 00:00:00 2001 From: huangyi Date: Mon, 2 Sep 2024 11:39:35 +0800 Subject: [PATCH 1/3] Problem: tx is not replaced by priority Closes: #1557 Solution: - add TxReplacement callback, only replace if priority is higher --- CHANGELOG.md | 1 + app/app.go | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3ce0b33b7d..83f1bd39d4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ * (store) [#1526](https://github.com/crypto-org-chain/cronos/pull/1526) Cache index/filters in rocksdb application.db to reduce ram usage. * (store)[#1529](https://github.com/crypto-org-chain/cronos/pull/1529) Enable pinL0FilterAndIndexBlocksInCache. * (store)[#1547](https://github.com/crypto-org-chain/cronos/pull/1547) Disable memiavl cache if block-stm is enabled. +* (app) [#]() Only replace tx if new priority is larger. ### Bug Fixes diff --git a/app/app.go b/app/app.go index 7da2128af5..7e503881e2 100644 --- a/app/app.go +++ b/app/app.go @@ -375,6 +375,10 @@ func New( TxPriority: mempool.NewDefaultTxPriority(), SignerExtractor: evmapp.NewEthSignerExtractionAdapter(mempool.NewDefaultSignerExtractionAdapter()), MaxTx: maxTxs, + TxReplacement: func(op, np int64, oTx, nTx sdk.Tx) bool { + // tx is only replaced if new priority is higher than old priority + return np > op + }, }) handler := baseapp.NewDefaultProposalHandler(mempool, app) From 0ab3868eab764acf32ddd67f752e8561da1dbf38 Mon Sep 17 00:00:00 2001 From: yihuang Date: Mon, 2 Sep 2024 11:41:00 +0800 Subject: [PATCH 2/3] Update CHANGELOG.md Signed-off-by: yihuang --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 83f1bd39d4..a8280aa2f1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,7 +31,7 @@ * (store) [#1526](https://github.com/crypto-org-chain/cronos/pull/1526) Cache index/filters in rocksdb application.db to reduce ram usage. * (store)[#1529](https://github.com/crypto-org-chain/cronos/pull/1529) Enable pinL0FilterAndIndexBlocksInCache. * (store)[#1547](https://github.com/crypto-org-chain/cronos/pull/1547) Disable memiavl cache if block-stm is enabled. -* (app) [#]() Only replace tx if new priority is larger. +* (app) [#1563](https://github.com/crypto-org-chain/cronos/pull/1563) Only replace tx if new priority is larger. ### Bug Fixes From 14b147f2d431fa8640d2a6eb4bbdf248f574cdf3 Mon Sep 17 00:00:00 2001 From: huangyi Date: Mon, 2 Sep 2024 11:52:19 +0800 Subject: [PATCH 3/3] test tx replacement --- integration_tests/test_mempool.py | 34 +++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/integration_tests/test_mempool.py b/integration_tests/test_mempool.py index bdd921cab1..f70c663e49 100644 --- a/integration_tests/test_mempool.py +++ b/integration_tests/test_mempool.py @@ -80,3 +80,37 @@ def test_blocked_address(cronos_mempool): rsp = cli.transfer("signer1", cli.address("validator"), "1basecro") assert rsp["code"] != 0 assert "signer is blocked" in rsp["raw_log"] + + +@pytest.mark.flaky(max_runs=5) +def test_tx_replacement(cronos_mempool): + w3: Web3 = cronos_mempool.w3 + account = "community" + nonce = w3.eth.get_transaction_count(ADDRS[account]) + gas_price = w3.eth.gas_price + reduction = 1000000 + # the second tx should replace the first tx with higher priority, + # but the third one shouldn't replace the second one. + prices = [ + gas_price, + gas_price + 2 * reduction, + gas_price + reduction, + ] + txs = [ + sign_transaction( + w3, + { + "to": ADDRS[account], + "value": 1, + "gas": 21000, + "gasPrice": price, + "nonce": nonce, + }, + KEYS[account], + ) + for price in prices + ] + + txhashes = [w3.eth.send_raw_transaction(tx.rawTransaction) for tx in txs] + receipt = w3.eth.wait_for_transaction_receipt(txhashes[1]) + assert receipt.status == 1