Skip to content

Commit ab1f560

Browse files
committed
Support -checkmempool=N, which runs checks on average once every N transactions
1 parent c6de5cc commit ab1f560

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

src/init.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,10 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
836836
InitWarning(_("Warning: Unsupported argument -benchmark ignored, use -debug=bench."));
837837

838838
// Checkmempool and checkblockindex default to true in regtest mode
839-
mempool.setSanityCheck(GetBoolArg("-checkmempool", chainparams.DefaultConsistencyChecks()));
839+
int ratio = std::min<int>(std::max<int>(GetArg("-checkmempool", chainparams.DefaultConsistencyChecks() ? 1 : 0), 0), 1000000);
840+
if (ratio != 0) {
841+
mempool.setSanityCheck(1.0 / ratio);
842+
}
840843
fCheckBlockIndex = GetBoolArg("-checkblockindex", chainparams.DefaultConsistencyChecks());
841844
fCheckpointsEnabled = GetBoolArg("-checkpoints", true);
842845

src/txmempool.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ CTxMemPool::CTxMemPool(const CFeeRate& _minRelayFee) :
311311
// Sanity checks off by default for performance, because otherwise
312312
// accepting transactions becomes O(N^2) where N is the number
313313
// of transactions in the pool
314-
fSanityCheck = false;
314+
nCheckFrequency = 0;
315315

316316
minerPolicyEstimator = new CBlockPolicyEstimator(_minRelayFee);
317317
}
@@ -483,7 +483,7 @@ void CTxMemPool::removeCoinbaseSpends(const CCoinsViewCache *pcoins, unsigned in
483483
if (it2 != mapTx.end())
484484
continue;
485485
const CCoins *coins = pcoins->AccessCoins(txin.prevout.hash);
486-
if (fSanityCheck) assert(coins);
486+
if (nCheckFrequency != 0) assert(coins);
487487
if (!coins || (coins->IsCoinBase() && ((signed long)nMemPoolHeight) - coins->nHeight < COINBASE_MATURITY)) {
488488
transactionsToRemove.push_back(tx);
489489
break;
@@ -553,7 +553,10 @@ void CTxMemPool::clear()
553553

554554
void CTxMemPool::check(const CCoinsViewCache *pcoins) const
555555
{
556-
if (!fSanityCheck)
556+
if (nCheckFrequency == 0)
557+
return;
558+
559+
if (insecure_rand() >= nCheckFrequency)
557560
return;
558561

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

src/txmempool.h

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

@@ -338,7 +338,7 @@ class CTxMemPool
338338
* check does nothing.
339339
*/
340340
void check(const CCoinsViewCache *pcoins) const;
341-
void setSanityCheck(bool _fSanityCheck) { fSanityCheck = _fSanityCheck; }
341+
void setSanityCheck(double dFrequency = 1.0) { nCheckFrequency = dFrequency * 4294967296.0; }
342342

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

0 commit comments

Comments
 (0)