Skip to content

Commit bde953e

Browse files
committed
Uncache input txn in utxo cache if a tx is not accepted to mempool
1 parent 97bf377 commit bde953e

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

src/main.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -835,8 +835,9 @@ std::string FormatStateMessage(const CValidationState &state)
835835
state.GetRejectCode());
836836
}
837837

838-
bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransaction &tx, bool fLimitFree,
839-
bool* pfMissingInputs, bool fOverrideMempoolLimit, bool fRejectAbsurdFee)
838+
bool AcceptToMemoryPoolWorker(CTxMemPool& pool, CValidationState &state, const CTransaction &tx, bool fLimitFree,
839+
bool* pfMissingInputs, bool fOverrideMempoolLimit, bool fRejectAbsurdFee,
840+
std::vector<uint256>& vHashTxnToUncache)
840841
{
841842
AssertLockHeld(cs_main);
842843
if (pfMissingInputs)
@@ -917,13 +918,19 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa
917918
view.SetBackend(viewMemPool);
918919

919920
// do we already have it?
920-
if (view.HaveCoins(hash))
921+
bool fHadTxInCache = pcoinsTip->HaveCoinsInCache(hash);
922+
if (view.HaveCoins(hash)) {
923+
if (!fHadTxInCache)
924+
vHashTxnToUncache.push_back(hash);
921925
return state.Invalid(false, REJECT_ALREADY_KNOWN, "txn-already-known");
926+
}
922927

923928
// do all inputs exist?
924929
// Note that this does not check for the presence of actual outputs (see the next check for that),
925930
// and only helps with filling in pfMissingInputs (to determine missing vs spent).
926931
BOOST_FOREACH(const CTxIn txin, tx.vin) {
932+
if (!pcoinsTip->HaveCoinsInCache(txin.prevout.hash))
933+
vHashTxnToUncache.push_back(txin.prevout.hash);
927934
if (!view.HaveCoins(txin.prevout.hash)) {
928935
if (pfMissingInputs)
929936
*pfMissingInputs = true;
@@ -1232,6 +1239,18 @@ bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransa
12321239
return true;
12331240
}
12341241

1242+
bool AcceptToMemoryPool(CTxMemPool& pool, CValidationState &state, const CTransaction &tx, bool fLimitFree,
1243+
bool* pfMissingInputs, bool fOverrideMempoolLimit, bool fRejectAbsurdFee)
1244+
{
1245+
std::vector<uint256> vHashTxToUncache;
1246+
bool res = AcceptToMemoryPoolWorker(pool, state, tx, fLimitFree, pfMissingInputs, fOverrideMempoolLimit, fRejectAbsurdFee, vHashTxToUncache);
1247+
if (!res) {
1248+
BOOST_FOREACH(const uint256& hashTx, vHashTxToUncache)
1249+
pcoinsTip->Uncache(hashTx);
1250+
}
1251+
return res;
1252+
}
1253+
12351254
/** Return transaction in tx, and if it was found inside a block, its hash is placed in hashBlock */
12361255
bool GetTransaction(const uint256 &hash, CTransaction &txOut, const Consensus::Params& consensusParams, uint256 &hashBlock, bool fAllowSlow)
12371256
{

0 commit comments

Comments
 (0)