Skip to content

Commit

Permalink
fix error handling for non-replace situation to avoid hang
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenlanders committed Jan 1, 2024
1 parent c7778bf commit d28a356
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
3 changes: 3 additions & 0 deletions internal/mempool/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ func (txmp *TxMempool) removeExistingTx(res *abci.ResponseCheckTxV2) (bool, erro

// try to replace existing transaction in mempool
if existingTx := txmp.txStore.GetTxBySender(res.Sender); existingTx != nil {
fmt.Printf("mempool: priority=%d, new priority=%d\n", existingTx.priority, res.Priority)
if existingTx.priority >= res.Priority {
return false, types.ErrEvmTxNotReplaced
}
Expand Down Expand Up @@ -322,9 +323,11 @@ func (txmp *TxMempool) CheckTx(
txmp.logger.Error(
err.Error(),
"tx", fmt.Sprintf("%X", wtx.tx.Hash()),
"priority", res.Priority,
"sender", res.Sender,
)
txmp.metrics.RejectedTxs.Add(1)
return types.ErrEvmTxNotReplaced
// not treated as an error, fallthrough to callback
} else {
// log the replacement
Expand Down
10 changes: 6 additions & 4 deletions internal/mempool/tx.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mempool

import (
"fmt"
"sort"
"sync"
"time"
Expand Down Expand Up @@ -311,18 +312,19 @@ func NewPendingTxs() *PendingTxs {
}
}

func (p *PendingTxs) RemoveSameEvmTxIfLowerPriority(resp *abci.ResponseCheckTxV2) (bool, error) {
func (p *PendingTxs) RemoveSameEvmTxIfLowerPriority(res *abci.ResponseCheckTxV2) (bool, error) {
// lock needed so that indexes are fixed between search and delete
p.mtx.Lock()
defer p.mtx.Unlock()
for idx, tx := range p.txs {
for idx, existingTx := range p.txs {
// find the tx with the same nonce/address
if tx.checkTxResponse.IsSameAddressAndNonce(resp) {
if existingTx.checkTxResponse.IsSameAddressAndNonce(res) {
// only if it's lower priority than the new tx, replace it
if tx.checkTxResponse.IsLowerPriority(resp) {
if existingTx.checkTxResponse.IsLowerPriority(res) {
p.popTxsAtIndices([]int{idx})
return true, nil
}
fmt.Printf("pending: priority=%d, new priority=%d\n", existingTx.checkTxResponse.Priority, res.Priority)
// otherwise we must reject this transaction
return false, types.ErrEvmTxNotReplaced
}
Expand Down

0 comments on commit d28a356

Please sign in to comment.