New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
mempool,rpc: add removetx rpc method #7047
Changes from 13 commits
8216e08
5eeecdf
c4f0ada
f3a4a4d
ca1b93b
d247479
79064eb
dd268de
024657b
ac324b4
9136410
95f70d6
199e9b3
6a8b88d
6b8810a
b046103
fc7c986
e4b1ccc
2270efd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package v1 | |
import ( | ||
"bytes" | ||
"context" | ||
"errors" | ||
"fmt" | ||
"sync/atomic" | ||
"time" | ||
|
@@ -256,7 +257,7 @@ func (txmp *TxMempool) CheckTx( | |
return err | ||
} | ||
|
||
txHash := mempool.TxKey(tx) | ||
txHash := tx.Key() | ||
|
||
// We add the transaction to the mempool's cache and if the transaction already | ||
// exists, i.e. false is returned, then we check if we've seen this transaction | ||
|
@@ -304,6 +305,19 @@ func (txmp *TxMempool) CheckTx( | |
return nil | ||
} | ||
|
||
func (txmp *TxMempool) RemoveTxByKey(txKey types.TxKey) error { | ||
txmp.Lock() | ||
defer txmp.Unlock() | ||
|
||
// remove the committed transaction from the transaction store and indexes | ||
if wtx := txmp.txStore.GetTxByHash(txKey); wtx != nil { | ||
txmp.removeTx(wtx, false) | ||
return nil | ||
} | ||
|
||
return errors.New("transaction not found") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this get a variable like the already-in-cache case? It seem like the kind of thing the caller might care to distinguish. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess I don't much see the point in distinguishing the error: there's no circumstance where removing a transaction would be retriable (perhaps eventually via the RPC, you might get a networking error that would make it worth retrying, but you'd want to be able to identify that that error is retriable? which seems/feels like an RPC feature.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see your point, but is the answer different for the "already in cache" case? (Separately, and not related to this change, it seems like a mistake that we don't use the error code of the JSON-RPC response to communicate errors responsibly to the client) |
||
} | ||
|
||
// Flush flushes out the mempool. It acquires a read-lock, fetches all the | ||
// transactions currently in the transaction store and removes each transaction | ||
// from the store and all indexes and finally resets the cache. | ||
|
@@ -451,7 +465,7 @@ func (txmp *TxMempool) Update( | |
} | ||
|
||
// remove the committed transaction from the transaction store and indexes | ||
if wtx := txmp.txStore.GetTxByHash(mempool.TxKey(tx)); wtx != nil { | ||
if wtx := txmp.txStore.GetTxByHash(tx.Key()); wtx != nil { | ||
txmp.removeTx(wtx, false) | ||
} | ||
} | ||
|
@@ -629,7 +643,7 @@ func (txmp *TxMempool) defaultTxCallback(req *abci.Request, res *abci.Response) | |
tx := req.GetCheckTx().Tx | ||
wtx := txmp.recheckCursor.Value.(*WrappedTx) | ||
if !bytes.Equal(tx, wtx.tx) { | ||
panic(fmt.Sprintf("re-CheckTx transaction mismatch; got: %X, expected: %X", wtx.tx.Hash(), mempool.TxKey(tx))) | ||
panic(fmt.Sprintf("re-CheckTx transaction mismatch; got: %X, expected: %X", wtx.tx.Hash(), types.Tx(tx).Key())) | ||
} | ||
|
||
// Only evaluate transactions that have not been removed. This can happen | ||
|
@@ -647,7 +661,7 @@ func (txmp *TxMempool) defaultTxCallback(req *abci.Request, res *abci.Response) | |
txmp.logger.Debug( | ||
"existing transaction no longer valid; failed re-CheckTx callback", | ||
"priority", wtx.priority, | ||
"tx", fmt.Sprintf("%X", mempool.TxHashFromBytes(wtx.tx)), | ||
"tx", fmt.Sprintf("%X", wtx.tx.Hash()), | ||
"err", err, | ||
"code", checkTxRes.CheckTx.Code, | ||
) | ||
|
@@ -784,13 +798,13 @@ func (txmp *TxMempool) removeTx(wtx *WrappedTx, removeFromCache bool) { | |
// the height and time based indexes. | ||
func (txmp *TxMempool) purgeExpiredTxs(blockHeight int64) { | ||
now := time.Now() | ||
expiredTxs := make(map[[mempool.TxKeySize]byte]*WrappedTx) | ||
expiredTxs := make(map[types.TxKey]*WrappedTx) | ||
|
||
if txmp.config.TTLNumBlocks > 0 { | ||
purgeIdx := -1 | ||
for i, wtx := range txmp.heightIndex.txs { | ||
if (blockHeight - wtx.height) > txmp.config.TTLNumBlocks { | ||
expiredTxs[mempool.TxKey(wtx.tx)] = wtx | ||
expiredTxs[wtx.tx.Key()] = wtx | ||
purgeIdx = i | ||
} else { | ||
// since the index is sorted, we know no other txs can be be purged | ||
|
@@ -807,7 +821,7 @@ func (txmp *TxMempool) purgeExpiredTxs(blockHeight int64) { | |
purgeIdx := -1 | ||
for i, wtx := range txmp.timestampIndex.txs { | ||
if now.Sub(wtx.timestamp) > txmp.config.TTLDuration { | ||
expiredTxs[mempool.TxKey(wtx.tx)] = wtx | ||
expiredTxs[wtx.tx.Key()] = wtx | ||
purgeIdx = i | ||
} else { | ||
// since the index is sorted, we know no other txs can be be purged | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is a TxKey exactly, the same as hash? Ideally if we could remove the tx by hash that could be simpler.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup it's the sha256 hash of the transaction