Skip to content

Commit

Permalink
call callback from mempool (#200)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenlanders authored and udpatil committed Apr 16, 2024
1 parent a5ac3ff commit 0552641
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions internal/mempool/mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,27 +286,32 @@ func (txmp *TxMempool) CheckTx(
}

res, err := txmp.proxyAppConn.CheckTx(ctx, &abci.RequestCheckTx{Tx: tx})

// when a transaction is removed/expired/rejected, this should be called
// The expire tx handler unreserves the pending nonce
removeHandler := func(removeFromCache bool) {
if removeFromCache {
txmp.cache.Remove(tx)
}
if res.ExpireTxHandler != nil {
res.ExpireTxHandler()
}
}

if err != nil {
txmp.cache.Remove(tx)
removeHandler(true)
res.Log = txmp.AppendCheckTxErr(res.Log, err.Error())
}

wtx := &WrappedTx{
tx: tx,
hash: txHash,
timestamp: time.Now().UTC(),
height: txmp.height,
evmNonce: res.EVMNonce,
evmAddress: res.EVMSenderAddress,
isEVM: res.IsEVM,
removeHandler: func(removeFromCache bool) {
if removeFromCache {
txmp.cache.Remove(tx)
}
if res.ExpireTxHandler != nil {
res.ExpireTxHandler()
}
},
tx: tx,
hash: txHash,
timestamp: time.Now().UTC(),
height: txmp.height,
evmNonce: res.EVMNonce,
evmAddress: res.EVMSenderAddress,
isEVM: res.IsEVM,
removeHandler: removeHandler,
}

if err == nil {
Expand Down Expand Up @@ -561,9 +566,7 @@ func (txmp *TxMempool) addNewTransaction(wtx *WrappedTx, res *abci.ResponseCheck

txmp.metrics.FailedTxs.Add(1)

if !txmp.config.KeepInvalidTxsInCache {
txmp.cache.Remove(wtx.tx)
}
wtx.removeHandler(!txmp.config.KeepInvalidTxsInCache)
if res.Code != abci.CodeTypeOK {
txmp.mtxFailedCheckTxCounts.Lock()
defer txmp.mtxFailedCheckTxCounts.Unlock()
Expand Down Expand Up @@ -601,7 +604,7 @@ func (txmp *TxMempool) addNewTransaction(wtx *WrappedTx, res *abci.ResponseCheck
if len(evictTxs) == 0 {
// No room for the new incoming transaction so we just remove it from
// the cache.
txmp.cache.Remove(wtx.tx)
wtx.removeHandler(true)
txmp.logger.Error(
"rejected incoming good transaction; mempool full",
"tx", fmt.Sprintf("%X", wtx.tx.Hash()),
Expand Down Expand Up @@ -982,7 +985,7 @@ func (txmp *TxMempool) handlePendingTransactions() {
}
if !txmp.config.KeepInvalidTxsInCache {
for _, tx := range rejected {
txmp.cache.Remove(tx.tx.tx)
tx.tx.removeHandler(true)
}
}
}

0 comments on commit 0552641

Please sign in to comment.