merge bitcoin#20222: CTxMempool constructor clean up

This commit is contained in:
Kittywhiskers Van Gogh 2023-02-20 19:31:40 +00:00 committed by UdjinM6
parent 7ca8214a16
commit a5f20129fb
4 changed files with 21 additions and 31 deletions

View File

@ -1766,16 +1766,9 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc
assert(!node.connman); assert(!node.connman);
node.connman = std::make_unique<CConnman>(GetRand(std::numeric_limits<uint64_t>::max()), GetRand(std::numeric_limits<uint64_t>::max())); node.connman = std::make_unique<CConnman>(GetRand(std::numeric_limits<uint64_t>::max()), GetRand(std::numeric_limits<uint64_t>::max()));
// Make mempool generally available in the node context. For example the connection manager, wallet, or RPC threads,
// which are all started after this, may use it from the node context.
assert(!node.mempool); assert(!node.mempool);
node.mempool = std::make_unique<CTxMemPool>(&::feeEstimator); int check_ratio = std::min<int>(std::max<int>(args.GetArg("-checkmempool", chainparams.DefaultConsistencyChecks() ? 1 : 0), 0), 1000000);
if (node.mempool) { node.mempool = std::make_unique<CTxMemPool>(node.fee_estimator.get(), check_ratio);
int ratio = std::min<int>(std::max<int>(args.GetArg("-checkmempool", chainparams.DefaultConsistencyChecks() ? 1 : 0), 0), 1000000);
if (ratio != 0) {
node.mempool->setSanityCheck(1.0 / ratio);
}
}
assert(!node.chainman); assert(!node.chainman);
node.chainman = &g_chainman; node.chainman = &g_chainman;

View File

@ -169,8 +169,7 @@ ChainTestingSetup::ChainTestingSetup(const std::string& chainName, const std::ve
pblocktree.reset(new CBlockTreeDB(1 << 20, true)); pblocktree.reset(new CBlockTreeDB(1 << 20, true));
m_node.mempool = std::make_unique<CTxMemPool>(&::feeEstimator); m_node.mempool = std::make_unique<CTxMemPool>(&::feeEstimator, 1);
m_node.mempool->setSanityCheck(1.0);
m_node.chainman = &::g_chainman; m_node.chainman = &::g_chainman;

View File

@ -334,15 +334,10 @@ void CTxMemPoolEntry::UpdateAncestorState(int64_t modifySize, CAmount modifyFee,
assert(int(nSigOpCountWithAncestors) >= 0); assert(int(nSigOpCountWithAncestors) >= 0);
} }
CTxMemPool::CTxMemPool(CBlockPolicyEstimator* estimator) CTxMemPool::CTxMemPool(CBlockPolicyEstimator* estimator, int check_ratio)
: nTransactionsUpdated(0), minerPolicyEstimator(estimator), m_epoch(0), m_has_epoch_guard(false) : m_check_ratio(check_ratio), minerPolicyEstimator(estimator)
{ {
_clear(); //lock free clear _clear(); //lock free clear
// Sanity checks off by default for performance, because otherwise
// accepting transactions becomes O(N^2) where N is the number
// of transactions in the pool
nCheckFrequency = 0;
} }
bool CTxMemPool::isSpent(const COutPoint& outpoint) const bool CTxMemPool::isSpent(const COutPoint& outpoint) const
@ -776,7 +771,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;
@ -1035,13 +1030,11 @@ static void CheckInputsAndUpdateCoins(const CTransaction& tx, CCoinsViewCache& m
void CTxMemPool::check(const CCoinsViewCache *pcoins) const void CTxMemPool::check(const CCoinsViewCache *pcoins) const
{ {
if (m_check_ratio == 0) return;
if (GetRand(m_check_ratio) >= 1) return;
LOCK(cs); LOCK(cs);
if (nCheckFrequency == 0)
return;
if (GetRand(std::numeric_limits<uint32_t>::max()) >= nCheckFrequency)
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());
uint64_t checkTotal = 0; uint64_t checkTotal = 0;

View File

@ -449,8 +449,8 @@ public:
class CTxMemPool class CTxMemPool
{ {
private: private:
uint32_t nCheckFrequency GUARDED_BY(cs); //!< Value n means that n times in 2^32 we check. const int m_check_ratio; //!< 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{0}; //!< Used by getblocktemplate to trigger CreateNewBlock() invocation
CBlockPolicyEstimator* minerPolicyEstimator; CBlockPolicyEstimator* minerPolicyEstimator;
uint64_t totalTxSize; //!< sum of all mempool tx' byte sizes uint64_t totalTxSize; //!< sum of all mempool tx' byte sizes
@ -459,8 +459,8 @@ private:
mutable int64_t lastRollingFeeUpdate; mutable int64_t lastRollingFeeUpdate;
mutable bool blockSinceLastRollingFeeBump; mutable bool blockSinceLastRollingFeeBump;
mutable double rollingMinimumFeeRate; //!< minimum fee to get into the pool, decreases exponentially mutable double rollingMinimumFeeRate; //!< minimum fee to get into the pool, decreases exponentially
mutable uint64_t m_epoch; mutable uint64_t m_epoch{0};
mutable bool m_has_epoch_guard; mutable bool m_has_epoch_guard{false};
void trackPackageRemoved(const CFeeRate& rate) EXCLUSIVE_LOCKS_REQUIRED(cs); void trackPackageRemoved(const CFeeRate& rate) EXCLUSIVE_LOCKS_REQUIRED(cs);
@ -581,8 +581,14 @@ public:
std::map<uint256, CAmount> mapDeltas; std::map<uint256, CAmount> mapDeltas;
/** Create a new CTxMemPool. /** Create a new CTxMemPool.
* Sanity checks will be off by default for performance, because otherwise
* accepting transactions becomes O(N^2) where N is the number of transactions
* in the pool.
*
* @param[in] estimator is used to estimate appropriate transaction fees.
* @param[in] check_ratio is the ratio used to determine how often sanity checks will run.
*/ */
explicit CTxMemPool(CBlockPolicyEstimator* estimator = nullptr); explicit CTxMemPool(CBlockPolicyEstimator* estimator = nullptr, int check_ratio = 0);
/** /**
* If sanity-checking is turned on, check makes sure the pool is * If sanity-checking is turned on, check makes sure the pool is
@ -591,7 +597,6 @@ 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); }
// 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