Skip to content

Commit 9b060e5

Browse files
TheBlueMattsdaftuar
authored andcommitted
Fix removal of time-locked transactions during reorg
1 parent 0c9959a commit 9b060e5

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2350,7 +2350,7 @@ bool static DisconnectTip(CValidationState& state, const Consensus::Params& cons
23502350
// UpdateTransactionsFromBlock finds descendants of any transactions in this
23512351
// block that were added back and cleans up the mempool state.
23522352
mempool.UpdateTransactionsFromBlock(vHashUpdate);
2353-
mempool.removeCoinbaseSpends(pcoinsTip, pindexDelete->nHeight);
2353+
mempool.removeForReorg(pcoinsTip, pindexDelete->nHeight);
23542354
mempool.check(pcoinsTip);
23552355
// Update chainActive and related variables.
23562356
UpdateTip(pindexDelete->pprev);

src/txmempool.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "main.h"
1212
#include "policy/fees.h"
1313
#include "streams.h"
14+
#include "timedata.h"
1415
#include "util.h"
1516
#include "utilmoneystr.h"
1617
#include "utiltime.h"
@@ -478,22 +479,26 @@ void CTxMemPool::remove(const CTransaction &origTx, std::list<CTransaction>& rem
478479
}
479480
}
480481

481-
void CTxMemPool::removeCoinbaseSpends(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight)
482+
void CTxMemPool::removeForReorg(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight)
482483
{
483484
// Remove transactions spending a coinbase which are now immature
484485
LOCK(cs);
485486
list<CTransaction> transactionsToRemove;
486487
for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
487488
const CTransaction& tx = it->GetTx();
488-
BOOST_FOREACH(const CTxIn& txin, tx.vin) {
489-
indexed_transaction_set::const_iterator it2 = mapTx.find(txin.prevout.hash);
490-
if (it2 != mapTx.end())
491-
continue;
492-
const CCoins *coins = pcoins->AccessCoins(txin.prevout.hash);
493-
if (nCheckFrequency != 0) assert(coins);
494-
if (!coins || (coins->IsCoinBase() && ((signed long)nMemPoolHeight) - coins->nHeight < COINBASE_MATURITY)) {
495-
transactionsToRemove.push_back(tx);
496-
break;
489+
if (!IsFinalTx(tx, nMemPoolHeight, GetAdjustedTime())) {
490+
transactionsToRemove.push_back(tx);
491+
} else {
492+
BOOST_FOREACH(const CTxIn& txin, tx.vin) {
493+
indexed_transaction_set::const_iterator it2 = mapTx.find(txin.prevout.hash);
494+
if (it2 != mapTx.end())
495+
continue;
496+
const CCoins *coins = pcoins->AccessCoins(txin.prevout.hash);
497+
if (nCheckFrequency != 0) assert(coins);
498+
if (!coins || (coins->IsCoinBase() && ((signed long)nMemPoolHeight) - coins->nHeight < COINBASE_MATURITY)) {
499+
transactionsToRemove.push_back(tx);
500+
break;
501+
}
497502
}
498503
}
499504
}

src/txmempool.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ class CTxMemPool
376376
bool addUnchecked(const uint256& hash, const CTxMemPoolEntry &entry, setEntries &setAncestors, bool fCurrentEstimate = true);
377377

378378
void remove(const CTransaction &tx, std::list<CTransaction>& removed, bool fRecursive = false);
379-
void removeCoinbaseSpends(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight);
379+
void removeForReorg(const CCoinsViewCache *pcoins, unsigned int nMemPoolHeight);
380380
void removeConflicts(const CTransaction &tx, std::list<CTransaction>& removed);
381381
void removeForBlock(const std::vector<CTransaction>& vtx, unsigned int nBlockHeight,
382382
std::list<CTransaction>& conflicts, bool fCurrentEstimate = true);

0 commit comments

Comments
 (0)