Skip to content
This repository was archived by the owner on Jan 20, 2026. It is now read-only.

EVM transaction replacement#206

Merged
codchen merged 1 commit intoevmfrom
replacement-evm
Mar 7, 2024
Merged

EVM transaction replacement#206
codchen merged 1 commit intoevmfrom
replacement-evm

Conversation

@codchen
Copy link
Collaborator

@codchen codchen commented Feb 28, 2024

Describe your changes and provide context

Added replacement logic for EVM transactions that works as follows:

  • if a tx with the same nonce exists in the priority index that has a lower priority, the existing tx is replaced by the incoming tx
  • if a tx with the same nonce exists in the priority index that has an equal or higher priority, the incoming tx is dropped
    After this change, there will not be any tx with duplicate nonce in the same queue inside the priority index, so the timestamp check in IsBefore is also removed.

As a requirement for keeping the invariant above, for any committed transaction, we will remove any tx in the mempool that has the same nonce regardless of whether it's the same tx or not. (For example, a node may have a tx of nonce-10 with content ABC in its local mempool, but received and executed a block proposal that contains a tx of nonce-10 with content DEF. In this case, we want to remove ABC from the local mempool after DEF's commit)

Testing performed to validate your change

unit test & local sei test

local sei test steps:

Block time -> 10s

block 1
seid tx evm send 0x9f76969569764cFc33D597D0eb00242cDB11EAE2 1000 --from=admin --nonce=0 --gas-fee-cap=1000000000000
seid tx evm send 0x9f76969569764cFc33D597D0eb00242cDB11EAE2 2000 --from=admin --nonce=0 --gas-fee-cap=1100000000000

block 1
seid tx evm send 0x9f76969569764cFc33D597D0eb00242cDB11EAE2 1000 --from=admin --nonce=0 --gas-fee-cap=1000000000000
seid tx evm send 0x9f76969569764cFc33D597D0eb00242cDB11EAE2 2000 --from=admin --nonce=0 --gas-fee-cap=900000000000

block 1
seid tx evm send 0x9f76969569764cFc33D597D0eb00242cDB11EAE2 1000 --from=admin --nonce=0 --gas-fee-cap=1000000000000
seid tx evm send 0x9f76969569764cFc33D597D0eb00242cDB11EAE2 2000 --from=admin --nonce=2 --gas-fee-cap=1000000000000
seid tx evm send 0x9f76969569764cFc33D597D0eb00242cDB11EAE2 3000 --from=admin --nonce=2 --gas-fee-cap=1100000000000
block 2
seid tx evm send 0x9f76969569764cFc33D597D0eb00242cDB11EAE2 10000 --from=admin --nonce=1 --gas-fee-cap=1000000000000
seid tx evm send 0x9f76969569764cFc33D597D0eb00242cDB11EAE2 100000 --from=admin --nonce=3 --gas-fee-cap=1000000000000

block 1
seid tx evm send 0x9f76969569764cFc33D597D0eb00242cDB11EAE2 1000 --from=admin --nonce=0 --gas-fee-cap=1000000000000
seid tx evm send 0x9f76969569764cFc33D597D0eb00242cDB11EAE2 2000 --from=admin --nonce=2 --gas-fee-cap=1000000000000
seid tx evm send 0x9f76969569764cFc33D597D0eb00242cDB11EAE2 3000 --from=admin --nonce=2 --gas-fee-cap=900000000000
block 2
seid tx evm send 0x9f76969569764cFc33D597D0eb00242cDB11EAE2 10000 --from=admin --nonce=1 --gas-fee-cap=1000000000000
seid tx evm send 0x9f76969569764cFc33D597D0eb00242cDB11EAE2 100000 --from=admin --nonce=3 --gas-fee-cap=1000000000000

Copy link
Collaborator

@philipsu522 philipsu522 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm but let's wait for @stevenlanders to review

Comment on lines +85 to +86
// should replace
// replace heap if applicable
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit comment fix

// remove the committed transaction from the transaction store and indexes
if wtx := txmp.txStore.GetTxByHash(txKey); wtx != nil {
txmp.removeTx(wtx, false, true)
txmp.removeTx(wtx, false, true, true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(once we have so many flags, it might make sense to split these out as named methods)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah makes sense. Or group them into a RemoveTxOptions struct that has named fields?

@codchen codchen merged commit f4e9c14 into evm Mar 7, 2024
codchen added a commit that referenced this pull request Mar 7, 2024
codchen added a commit that referenced this pull request Mar 7, 2024
codchen added a commit to sei-protocol/sei-cosmos that referenced this pull request Mar 7, 2024
## Describe your changes and provide context
See description in
sei-protocol/sei-tendermint#206

## Testing performed to validate your change

---------

Co-authored-by: Steven Landers <steven@seinetwork.io>
Co-authored-by: Philip Su <philip.su.522@gmail.com>
Co-authored-by: Yiming Zang <50607998+yzang2019@users.noreply.github.com>
Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: Uday Patil <udpatil@gmail.com>
udpatil added a commit to sei-protocol/sei-cosmos that referenced this pull request Mar 11, 2024
## Describe your changes and provide context
See description in
sei-protocol/sei-tendermint#206

## Testing performed to validate your change

---------

Co-authored-by: Steven Landers <steven@seinetwork.io>
Co-authored-by: Philip Su <philip.su.522@gmail.com>
Co-authored-by: Yiming Zang <50607998+yzang2019@users.noreply.github.com>
Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: Uday Patil <udpatil@gmail.com>
udpatil added a commit to sei-protocol/sei-cosmos that referenced this pull request Mar 14, 2024
## Describe your changes and provide context
See description in
sei-protocol/sei-tendermint#206

## Testing performed to validate your change

---------

Co-authored-by: Steven Landers <steven@seinetwork.io>
Co-authored-by: Philip Su <philip.su.522@gmail.com>
Co-authored-by: Yiming Zang <50607998+yzang2019@users.noreply.github.com>
Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: Uday Patil <udpatil@gmail.com>
udpatil added a commit to sei-protocol/sei-cosmos that referenced this pull request Mar 26, 2024
## Describe your changes and provide context
See description in
sei-protocol/sei-tendermint#206

## Testing performed to validate your change

---------

Co-authored-by: Steven Landers <steven@seinetwork.io>
Co-authored-by: Philip Su <philip.su.522@gmail.com>
Co-authored-by: Yiming Zang <50607998+yzang2019@users.noreply.github.com>
Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: Uday Patil <udpatil@gmail.com>
udpatil added a commit to sei-protocol/sei-cosmos that referenced this pull request Mar 26, 2024
## Describe your changes and provide context
See description in
sei-protocol/sei-tendermint#206

## Testing performed to validate your change

---------

Co-authored-by: Steven Landers <steven@seinetwork.io>
Co-authored-by: Philip Su <philip.su.522@gmail.com>
Co-authored-by: Yiming Zang <50607998+yzang2019@users.noreply.github.com>
Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: Uday Patil <udpatil@gmail.com>
udpatil pushed a commit that referenced this pull request Mar 26, 2024
udpatil added a commit to sei-protocol/sei-cosmos that referenced this pull request Mar 27, 2024
## Describe your changes and provide context
See description in
sei-protocol/sei-tendermint#206

## Testing performed to validate your change

---------

Co-authored-by: Steven Landers <steven@seinetwork.io>
Co-authored-by: Philip Su <philip.su.522@gmail.com>
Co-authored-by: Yiming Zang <50607998+yzang2019@users.noreply.github.com>
Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: Uday Patil <udpatil@gmail.com>
udpatil pushed a commit that referenced this pull request Apr 16, 2024
udpatil added a commit to sei-protocol/sei-cosmos that referenced this pull request Apr 16, 2024
## Describe your changes and provide context
See description in
sei-protocol/sei-tendermint#206

## Testing performed to validate your change

---------

Co-authored-by: Steven Landers <steven@seinetwork.io>
Co-authored-by: Philip Su <philip.su.522@gmail.com>
Co-authored-by: Yiming Zang <50607998+yzang2019@users.noreply.github.com>
Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: Uday Patil <udpatil@gmail.com>
udpatil pushed a commit that referenced this pull request Apr 16, 2024
udpatil added a commit that referenced this pull request Apr 19, 2024
* Make ReadMaxTxs atomic (#166)

* Support pending transaction in mempool (#169)

* fix unconfirmed tx to consider pending txs (#172)

* fix pending pop (#173)

* add TTL for pending txs (#174)

* [EVM] Fix evm pending nonce (#179)

* Perf: Increase buffer size for pubsub server to boost performance (#167)

* Increase buffer size for pubsub server

* Add more timeout for test failure

* Add more timeout

* Fix test split scripts

* Fix test split

* Fix unit test

* Unit test

* Unit test

* [P2P] Optimize block pool requester retry and peer pick up logic (#170)

* P2P Improvements: Fix block sync reactor and block pool retry logic

* Revert "Add event data to result event (#165)" (#176)

This reverts commit 72bb29c.

* Fix block sync auto restart not working as expected (#175)

* Fix edge case for blocksync (#178)

* fix evm pending nonce

* fix test

* deflake a test

* de-flake test

* Revert "merge main"

This reverts commit 58b9424, reversing
changes made to 02d1478.

* consider keep-in-cache logic when removing from cache

* undo test tweaks

---------

Co-authored-by: Yiming Zang <50607998+yzang2019@users.noreply.github.com>
Co-authored-by: Jeremy Wei <jeremy.t.wei@gmail.com>

* Fix bug when popping pending TXs (#188)

* Add mempool metrics for number of pending tx and expired txs (#189)

* Add metrics for mempool pending transaction size

* Add expired tx count metrics

* [EVM] Allow multiple txs from same account in a block (#190)

* add mempool prioritization with evm nonce

* fix priority stability

* index fixes

* replace with binary search insert

* impl binary search

* fix removeTx to push next queued evm tx (#191)

* fix expire metric (#193)

* [EVM] Fix duplicate evm txs from priority queue (#195)

* debug duplicate evm tx

* add more logs

* add some \ns

* more logs

* fix swap check

* add-lockable-reap-by-gas

* add invariant checks

* fix invariant parenthesis

* fix log

* remove invalid invariant

* fix nonce ordering pain

* handle ordering of insert

* fix remove

* cleanup

* fix imports

* cleanup

* avoid getTransactionByHash(hash) panic due to index

* use Key() to compare instead of pointer

* [EVM] prevent duplicate txs from getting inserted (#196)

* prevent duplicates in mempool

* use timestamp in priority queue

* [EVM] Add logging for expiration (#198)

* add logging for expired txs

* cleanup

* [EVM] Avoid returning nil transactions on ForEach (#197)

* remove heapIndex to avoid nil scenario

* avoid returning nil in loop (mimic Peek)

* call callback from mempool (#200)

* separate limit for pending tx (#202)

* Add EVM txs eviction logic (#204)

* Fix debug log (#205)

* EVM transaction replacement (#206) (#208)

* Add heapIndex with safety check (#213)

* add heapIndex with safety check

* cleanup

* comment out for perf test

* add back perf improvement

* fix nil test

* Use write-lock in (*TxPriorityQueue).ReapMax funcs (#209)

ReapMaxBytesMaxGas and ReapMaxTxs funcs in TxPriorityQueue claim
> Transactions returned are not removed from the mempool transaction
> store or indexes.

However, they use a priority queue to accomplish the claim
> Transaction are retrieved in priority order.

This is accomplished by popping all items out of the whole heap, and
then pushing then back in sequentially. A copy of the heap cannot be
obtained otherwise. Both of the mentioned functions use a read-lock
(RLock) when doing this. This results in a potential scenario where
multiple executions of the ReapMax can be started in parallel, and
both would be popping items out of the priority queue.

In practice, this can be abused by executing the `unconfirmed_txs` RPC
call repeatedly. Based on our observations, running it multiple times
per millisecond results in multiple threads picking it up at the same
time. Such a scenario can be obtained via the WebSocket interface, and
spamming `unconfirmed_txs` calls there. The behavior that happens is a
`Panic in WSJSONRPC handler` when a queue item unexpectedly disappears
for `mempool.(*TxPriorityQueue).Swap`.
(`runtime error: index out of range [0] with length 0`)

This can additionally lead to a `CONSENSUS FAILURE!!!` if the race
condition occurs for `internal/consensus.(*State).finalizeCommit`
when it tries to do `mempool.(*TxPriorityQueue).RemoveTx`, but
the ReapMax has already removed all elements from the underlying
heap. (`runtime error: index out of range [-1]`)

This commit switches the lock type to a write-lock (Lock) to ensure
no parallel modifications take place. This commit additionally updates
the tests to allow parallel execution of the func calls in testing,
as to prevent regressions (in case someone wants to downgrade the locks
without considering the implications from the underlying heap usage).

---------

Co-authored-by: Valters Jansons <sigv@users.noreply.github.com>

* Pending Txs Update Condition (#214)

* Add metrics for mempool size changes (#220)

* [EVM] Adjust locking for replacement (#224)

* Remove tx from cache when canAddPendingTx fails (#230)

* add tx hash to evm info proto (#231)

---------

Co-authored-by: codchen <codchen03@gmail.com>
Co-authored-by: Steven Landers <steven@seinetwork.io>
Co-authored-by: Yiming Zang <50607998+yzang2019@users.noreply.github.com>
Co-authored-by: Jeremy Wei <jeremy.t.wei@gmail.com>
Co-authored-by: Valters Jansons <sigv@users.noreply.github.com>
Co-authored-by: Kartik Bhat <kartikbhatri@gmail.com>
udpatil pushed a commit that referenced this pull request Apr 19, 2024
udpatil added a commit to sei-protocol/sei-cosmos that referenced this pull request Apr 19, 2024
## Describe your changes and provide context
See description in
sei-protocol/sei-tendermint#206

## Testing performed to validate your change

---------

Co-authored-by: Steven Landers <steven@seinetwork.io>
Co-authored-by: Philip Su <philip.su.522@gmail.com>
Co-authored-by: Yiming Zang <50607998+yzang2019@users.noreply.github.com>
Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: Uday Patil <udpatil@gmail.com>
udpatil added a commit to sei-protocol/sei-cosmos that referenced this pull request Apr 19, 2024
## Describe your changes and provide context
See description in
sei-protocol/sei-tendermint#206

## Testing performed to validate your change

---------

Co-authored-by: Steven Landers <steven@seinetwork.io>
Co-authored-by: Philip Su <philip.su.522@gmail.com>
Co-authored-by: Yiming Zang <50607998+yzang2019@users.noreply.github.com>
Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: Uday Patil <udpatil@gmail.com>
masih pushed a commit to sei-protocol/sei-chain that referenced this pull request Sep 29, 2025
## Describe your changes and provide context
See description in
sei-protocol/sei-tendermint#206

## Testing performed to validate your change

---------

Co-authored-by: Steven Landers <steven@seinetwork.io>
Co-authored-by: Philip Su <philip.su.522@gmail.com>
Co-authored-by: Yiming Zang <50607998+yzang2019@users.noreply.github.com>
Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: Uday Patil <udpatil@gmail.com>
masih pushed a commit to sei-protocol/sei-chain that referenced this pull request Sep 30, 2025
## Describe your changes and provide context
See description in
sei-protocol/sei-tendermint#206

## Testing performed to validate your change

---------

Co-authored-by: Steven Landers <steven@seinetwork.io>
Co-authored-by: Philip Su <philip.su.522@gmail.com>
Co-authored-by: Yiming Zang <50607998+yzang2019@users.noreply.github.com>
Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
Co-authored-by: Uday Patil <udpatil@gmail.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants