Skip to content

Commit e06c14f

Browse files
committed
Merge pull request #6776
ab1f560 Support -checkmempool=N, which runs checks on average once every N transactions (Pieter Wuille)
2 parents 8756c98 + ab1f560 commit e06c14f

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

src/init.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,10 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
841841
InitWarning(_("Warning: Unsupported argument -benchmark ignored, use -debug=bench."));
842842

843843
// Checkmempool and checkblockindex default to true in regtest mode
844-
mempool.setSanityCheck(GetBoolArg("-checkmempool", chainparams.DefaultConsistencyChecks()));
844+
int ratio = std::min<int>(std::max<int>(GetArg("-checkmempool", chainparams.DefaultConsistencyChecks() ? 1 : 0), 0), 1000000);
845+
if (ratio != 0) {
846+
mempool.setSanityCheck(1.0 / ratio);
847+
}
845848
fCheckBlockIndex = GetBoolArg("-checkblockindex", chainparams.DefaultConsistencyChecks());
846849
fCheckpointsEnabled = GetBoolArg("-checkpoints", true);
847850

src/txmempool.cpp

+6-3
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ CTxMemPool::CTxMemPool(const CFeeRate& _minReasonableRelayFee) :
314314
// Sanity checks off by default for performance, because otherwise
315315
// accepting transactions becomes O(N^2) where N is the number
316316
// of transactions in the pool
317-
fSanityCheck = false;
317+
nCheckFrequency = 0;
318318

319319
minerPolicyEstimator = new CBlockPolicyEstimator(_minReasonableRelayFee);
320320
minReasonableRelayFee = _minReasonableRelayFee;
@@ -487,7 +487,7 @@ void CTxMemPool::removeCoinbaseSpends(const CCoinsViewCache *pcoins, unsigned in
487487
if (it2 != mapTx.end())
488488
continue;
489489
const CCoins *coins = pcoins->AccessCoins(txin.prevout.hash);
490-
if (fSanityCheck) assert(coins);
490+
if (nCheckFrequency != 0) assert(coins);
491491
if (!coins || (coins->IsCoinBase() && ((signed long)nMemPoolHeight) - coins->nHeight < COINBASE_MATURITY)) {
492492
transactionsToRemove.push_back(tx);
493493
break;
@@ -568,7 +568,10 @@ void CTxMemPool::clear()
568568

569569
void CTxMemPool::check(const CCoinsViewCache *pcoins) const
570570
{
571-
if (!fSanityCheck)
571+
if (nCheckFrequency == 0)
572+
return;
573+
574+
if (insecure_rand() >= nCheckFrequency)
572575
return;
573576

574577
LogPrint("mempool", "Checking mempool with %u transactions and %u inputs\n", (unsigned int)mapTx.size(), (unsigned int)mapNextTx.size());

src/txmempool.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ class CInPoint
278278
class CTxMemPool
279279
{
280280
private:
281-
bool fSanityCheck; //! Normally false, true if -checkmempool or -regtest
281+
uint32_t nCheckFrequency; //! Value n means that n times in 2^32 we check.
282282
unsigned int nTransactionsUpdated;
283283
CBlockPolicyEstimator* minerPolicyEstimator;
284284

@@ -360,7 +360,7 @@ class CTxMemPool
360360
* check does nothing.
361361
*/
362362
void check(const CCoinsViewCache *pcoins) const;
363-
void setSanityCheck(bool _fSanityCheck) { fSanityCheck = _fSanityCheck; }
363+
void setSanityCheck(double dFrequency = 1.0) { nCheckFrequency = dFrequency * 4294967296.0; }
364364

365365
// addUnchecked must updated state for all ancestors of a given transaction,
366366
// to track size/count of descendant transactions. First version of

0 commit comments

Comments
 (0)