Skip to content

Commit 9d4b4b2

Browse files
committed
refactor: Avoid double to int cast for nCheckFrequency
Use a ratio instead of a frequency that requires a double to int cast for determining how often a mempool sanity check should run.
1 parent 8827118 commit 9d4b4b2

File tree

3 files changed

+8
-12
lines changed

3 files changed

+8
-12
lines changed

src/init.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,10 +1394,8 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
13941394
assert(!node.mempool);
13951395
node.mempool = MakeUnique<CTxMemPool>(&::feeEstimator);
13961396
if (node.mempool) {
1397-
int ratio = std::min<int>(std::max<int>(args.GetArg("-checkmempool", chainparams.DefaultConsistencyChecks() ? 1 : 0), 0), 1000000);
1398-
if (ratio != 0) {
1399-
node.mempool->setSanityCheck(1.0 / ratio);
1400-
}
1397+
int check_ratio = std::min<int>(std::max<int>(args.GetArg("-checkmempool", chainparams.DefaultConsistencyChecks() ? 1 : 0), 0), 1000000);
1398+
node.mempool->setSanityCheck(check_ratio);
14011399
}
14021400

14031401
assert(!node.chainman);

src/txmempool.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ CTxMemPool::CTxMemPool(CBlockPolicyEstimator* estimator)
339339
// Sanity checks off by default for performance, because otherwise
340340
// accepting transactions becomes O(N^2) where N is the number
341341
// of transactions in the pool
342-
nCheckFrequency = 0;
342+
m_check_ratio = 0;
343343
}
344344

345345
bool CTxMemPool::isSpent(const COutPoint& outpoint) const
@@ -523,7 +523,7 @@ void CTxMemPool::removeForReorg(const CCoinsViewCache *pcoins, unsigned int nMem
523523
if (it2 != mapTx.end())
524524
continue;
525525
const Coin &coin = pcoins->AccessCoin(txin.prevout);
526-
if (nCheckFrequency != 0) assert(!coin.IsSpent());
526+
if (m_check_ratio != 0) assert(!coin.IsSpent());
527527
if (coin.IsSpent() || (coin.IsCoinBase() && ((signed long)nMemPoolHeight) - coin.nHeight < COINBASE_MATURITY)) {
528528
txToRemove.insert(it);
529529
break;
@@ -620,11 +620,9 @@ static void CheckInputsAndUpdateCoins(const CTransaction& tx, CCoinsViewCache& m
620620
void CTxMemPool::check(const CCoinsViewCache *pcoins) const
621621
{
622622
LOCK(cs);
623-
if (nCheckFrequency == 0)
624-
return;
623+
if (m_check_ratio == 0) return;
625624

626-
if (GetRand(std::numeric_limits<uint32_t>::max()) >= nCheckFrequency)
627-
return;
625+
if (GetRand(m_check_ratio) >= 1) return;
628626

629627
LogPrint(BCLog::MEMPOOL, "Checking mempool with %u transactions and %u inputs\n", (unsigned int)mapTx.size(), (unsigned int)mapNextTx.size());
630628

src/txmempool.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,7 @@ class SaltedTxidHasher
488488
class CTxMemPool
489489
{
490490
private:
491-
uint32_t nCheckFrequency GUARDED_BY(cs); //!< Value n means that n times in 2^32 we check.
491+
uint32_t m_check_ratio GUARDED_BY(cs); //!< Value n means that 1 times in n we check.
492492
std::atomic<unsigned int> nTransactionsUpdated; //!< Used by getblocktemplate to trigger CreateNewBlock() invocation
493493
CBlockPolicyEstimator* minerPolicyEstimator;
494494

@@ -611,7 +611,7 @@ class CTxMemPool
611611
* check does nothing.
612612
*/
613613
void check(const CCoinsViewCache *pcoins) const;
614-
void setSanityCheck(double dFrequency = 1.0) { LOCK(cs); nCheckFrequency = static_cast<uint32_t>(dFrequency * 4294967295.0); }
614+
void setSanityCheck(int check_ratio = 0) { LOCK(cs); m_check_ratio = check_ratio; }
615615

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

0 commit comments

Comments
 (0)