mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-01-26 11:13:23 -03:00
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.
This commit is contained in:
parent
88271184e8
commit
9d4b4b2c2c
3 changed files with 8 additions and 12 deletions
|
@ -1394,10 +1394,8 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
|
||||||
assert(!node.mempool);
|
assert(!node.mempool);
|
||||||
node.mempool = MakeUnique<CTxMemPool>(&::feeEstimator);
|
node.mempool = MakeUnique<CTxMemPool>(&::feeEstimator);
|
||||||
if (node.mempool) {
|
if (node.mempool) {
|
||||||
int ratio = std::min<int>(std::max<int>(args.GetArg("-checkmempool", chainparams.DefaultConsistencyChecks() ? 1 : 0), 0), 1000000);
|
int check_ratio = std::min<int>(std::max<int>(args.GetArg("-checkmempool", chainparams.DefaultConsistencyChecks() ? 1 : 0), 0), 1000000);
|
||||||
if (ratio != 0) {
|
node.mempool->setSanityCheck(check_ratio);
|
||||||
node.mempool->setSanityCheck(1.0 / ratio);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(!node.chainman);
|
assert(!node.chainman);
|
||||||
|
|
|
@ -339,7 +339,7 @@ CTxMemPool::CTxMemPool(CBlockPolicyEstimator* estimator)
|
||||||
// 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
|
||||||
nCheckFrequency = 0;
|
m_check_ratio = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CTxMemPool::isSpent(const COutPoint& outpoint) const
|
bool CTxMemPool::isSpent(const COutPoint& outpoint) const
|
||||||
|
@ -523,7 +523,7 @@ void CTxMemPool::removeForReorg(const CCoinsViewCache *pcoins, unsigned int nMem
|
||||||
if (it2 != mapTx.end())
|
if (it2 != mapTx.end())
|
||||||
continue;
|
continue;
|
||||||
const Coin &coin = pcoins->AccessCoin(txin.prevout);
|
const Coin &coin = pcoins->AccessCoin(txin.prevout);
|
||||||
if (nCheckFrequency != 0) assert(!coin.IsSpent());
|
if (m_check_ratio != 0) assert(!coin.IsSpent());
|
||||||
if (coin.IsSpent() || (coin.IsCoinBase() && ((signed long)nMemPoolHeight) - coin.nHeight < COINBASE_MATURITY)) {
|
if (coin.IsSpent() || (coin.IsCoinBase() && ((signed long)nMemPoolHeight) - coin.nHeight < COINBASE_MATURITY)) {
|
||||||
txToRemove.insert(it);
|
txToRemove.insert(it);
|
||||||
break;
|
break;
|
||||||
|
@ -620,11 +620,9 @@ static void CheckInputsAndUpdateCoins(const CTransaction& tx, CCoinsViewCache& m
|
||||||
void CTxMemPool::check(const CCoinsViewCache *pcoins) const
|
void CTxMemPool::check(const CCoinsViewCache *pcoins) const
|
||||||
{
|
{
|
||||||
LOCK(cs);
|
LOCK(cs);
|
||||||
if (nCheckFrequency == 0)
|
if (m_check_ratio == 0) return;
|
||||||
return;
|
|
||||||
|
|
||||||
if (GetRand(std::numeric_limits<uint32_t>::max()) >= nCheckFrequency)
|
if (GetRand(m_check_ratio) >= 1) return;
|
||||||
return;
|
|
||||||
|
|
||||||
LogPrint(BCLog::MEMPOOL, "Checking mempool with %u transactions and %u inputs\n", (unsigned int)mapTx.size(), (unsigned int)mapNextTx.size());
|
LogPrint(BCLog::MEMPOOL, "Checking mempool with %u transactions and %u inputs\n", (unsigned int)mapTx.size(), (unsigned int)mapNextTx.size());
|
||||||
|
|
||||||
|
|
|
@ -488,7 +488,7 @@ public:
|
||||||
class CTxMemPool
|
class CTxMemPool
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
uint32_t nCheckFrequency GUARDED_BY(cs); //!< Value n means that n times in 2^32 we check.
|
uint32_t m_check_ratio GUARDED_BY(cs); //!< Value n means that 1 times in n we check.
|
||||||
std::atomic<unsigned int> nTransactionsUpdated; //!< Used by getblocktemplate to trigger CreateNewBlock() invocation
|
std::atomic<unsigned int> nTransactionsUpdated; //!< Used by getblocktemplate to trigger CreateNewBlock() invocation
|
||||||
CBlockPolicyEstimator* minerPolicyEstimator;
|
CBlockPolicyEstimator* minerPolicyEstimator;
|
||||||
|
|
||||||
|
@ -611,7 +611,7 @@ public:
|
||||||
* check does nothing.
|
* check does nothing.
|
||||||
*/
|
*/
|
||||||
void check(const CCoinsViewCache *pcoins) const;
|
void check(const CCoinsViewCache *pcoins) const;
|
||||||
void setSanityCheck(double dFrequency = 1.0) { LOCK(cs); nCheckFrequency = static_cast<uint32_t>(dFrequency * 4294967295.0); }
|
void setSanityCheck(int check_ratio = 0) { LOCK(cs); m_check_ratio = check_ratio; }
|
||||||
|
|
||||||
// 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
|
||||||
|
|
Loading…
Add table
Reference in a new issue