Skip to content

Commit

Permalink
Merge pull request #6776
Browse files Browse the repository at this point in the history
ab1f560 Support -checkmempool=N, which runs checks on average once every N transactions (Pieter Wuille)
  • Loading branch information
sipa committed Oct 28, 2015
2 parents 8756c98 + ab1f560 commit e06c14f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/init.cpp
Expand Up @@ -841,7 +841,10 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
InitWarning(_("Warning: Unsupported argument -benchmark ignored, use -debug=bench.")); InitWarning(_("Warning: Unsupported argument -benchmark ignored, use -debug=bench."));


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


Expand Down
9 changes: 6 additions & 3 deletions src/txmempool.cpp
Expand Up @@ -314,7 +314,7 @@ CTxMemPool::CTxMemPool(const CFeeRate& _minReasonableRelayFee) :
// Sanity checks off by default for performance, because otherwise // Sanity checks off by default for performance, because otherwise
// accepting transactions becomes O(N^2) where N is the number // accepting transactions becomes O(N^2) where N is the number
// of transactions in the pool // of transactions in the pool
fSanityCheck = false; nCheckFrequency = 0;


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


void CTxMemPool::check(const CCoinsViewCache *pcoins) const void CTxMemPool::check(const CCoinsViewCache *pcoins) const
{ {
if (!fSanityCheck) if (nCheckFrequency == 0)
return;

if (insecure_rand() >= nCheckFrequency)
return; return;


LogPrint("mempool", "Checking mempool with %u transactions and %u inputs\n", (unsigned int)mapTx.size(), (unsigned int)mapNextTx.size()); LogPrint("mempool", "Checking mempool with %u transactions and %u inputs\n", (unsigned int)mapTx.size(), (unsigned int)mapNextTx.size());
Expand Down
4 changes: 2 additions & 2 deletions src/txmempool.h
Expand Up @@ -278,7 +278,7 @@ class CInPoint
class CTxMemPool class CTxMemPool
{ {
private: private:
bool fSanityCheck; //! Normally false, true if -checkmempool or -regtest uint32_t nCheckFrequency; //! Value n means that n times in 2^32 we check.
unsigned int nTransactionsUpdated; unsigned int nTransactionsUpdated;
CBlockPolicyEstimator* minerPolicyEstimator; CBlockPolicyEstimator* minerPolicyEstimator;


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


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

0 comments on commit e06c14f

Please sign in to comment.