mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-01-19 06:43:45 +01:00
Merge bitcoin/bitcoin#23157: txmempool -/-> validation 1/2: improve performance of check() and remove dependency on validation
082c5bf099[refactor] pass coinsview and height to check() (glozow)ed6115f1ea[mempool] simplify some check() logic (glozow)9e8d7ad5d9[validation/mempool] use Spend/AddCoin instead of UpdateCoins (glozow)09d18916afMOVEONLY: remove single-use helper func CheckInputsAndUpdateCoins (glozow)e8639ec26a[mempool] remove now-unnecessary code (glozow)54c6f3c1da[mempool] speed up check() by using coins cache and iterating in topo order (glozow)30e240f65e[bench] Benchmark CTxMemPool::check() (glozow)cb1407196f[refactor/bench] make mempool_stress bench reusable and parameterizable (glozow) Pull request description: Remove the txmempool <-> validation circular dependency by removing txmempool's dependency on validation. There are two functions in txmempool that need validation right now: `check()` and `removeForReorg()`. This PR removes the dependencies in `check()`. This PR also improves the performance of `CTxMemPool::check()` by walking through the entries exactly once, in ascending ancestorcount order, which guarantees that we see parents before children. ACKs for top commit: jnewbery: reACK082c5bf099GeneFerneau: tACK [082c5bf](082c5bf099) rajarshimaitra: tACK082c5bf099theStack: Code-review ACK082c5bf099Tree-SHA512: 40ac622af1627b5c3e6abb4f0f035d833265a8c5e8dc88faf5354875dfb5137f137825e54bbd2a2668ed37b145c5d02285f776402629f58596e51853a9a79d29
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
|
||||
#include <txmempool.h>
|
||||
|
||||
#include <coins.h>
|
||||
#include <consensus/consensus.h>
|
||||
#include <consensus/tx_verify.h>
|
||||
#include <consensus/validation.h>
|
||||
@@ -671,16 +672,7 @@ void CTxMemPool::clear()
|
||||
_clear();
|
||||
}
|
||||
|
||||
static void CheckInputsAndUpdateCoins(const CTransaction& tx, CCoinsViewCache& mempoolDuplicate, const int64_t spendheight)
|
||||
{
|
||||
TxValidationState dummy_state; // Not used. CheckTxInputs() should always pass
|
||||
CAmount txfee = 0;
|
||||
bool fCheckResult = tx.IsCoinBase() || Consensus::CheckTxInputs(tx, dummy_state, mempoolDuplicate, spendheight, txfee);
|
||||
assert(fCheckResult);
|
||||
UpdateCoins(tx, mempoolDuplicate, std::numeric_limits<int>::max());
|
||||
}
|
||||
|
||||
void CTxMemPool::check(CChainState& active_chainstate) const
|
||||
void CTxMemPool::check(const CCoinsViewCache& active_coins_tip, int64_t spendheight) const
|
||||
{
|
||||
if (m_check_ratio == 0) return;
|
||||
|
||||
@@ -693,20 +685,16 @@ void CTxMemPool::check(CChainState& active_chainstate) const
|
||||
uint64_t checkTotal = 0;
|
||||
CAmount check_total_fee{0};
|
||||
uint64_t innerUsage = 0;
|
||||
uint64_t prev_ancestor_count{0};
|
||||
|
||||
CCoinsViewCache& active_coins_tip = active_chainstate.CoinsTip();
|
||||
CCoinsViewCache mempoolDuplicate(const_cast<CCoinsViewCache*>(&active_coins_tip));
|
||||
const int64_t spendheight = active_chainstate.m_chain.Height() + 1;
|
||||
|
||||
std::list<const CTxMemPoolEntry*> waitingOnDependants;
|
||||
for (indexed_transaction_set::const_iterator it = mapTx.begin(); it != mapTx.end(); it++) {
|
||||
unsigned int i = 0;
|
||||
for (const auto& it : GetSortedDepthAndScore()) {
|
||||
checkTotal += it->GetTxSize();
|
||||
check_total_fee += it->GetFee();
|
||||
innerUsage += it->DynamicMemoryUsage();
|
||||
const CTransaction& tx = it->GetTx();
|
||||
innerUsage += memusage::DynamicUsage(it->GetMemPoolParentsConst()) + memusage::DynamicUsage(it->GetMemPoolChildrenConst());
|
||||
bool fDependsWait = false;
|
||||
CTxMemPoolEntry::Parents setParentCheck;
|
||||
for (const CTxIn &txin : tx.vin) {
|
||||
// Check that every mempool transaction's inputs refer to available coins, or other mempool tx's.
|
||||
@@ -714,17 +702,17 @@ void CTxMemPool::check(CChainState& active_chainstate) const
|
||||
if (it2 != mapTx.end()) {
|
||||
const CTransaction& tx2 = it2->GetTx();
|
||||
assert(tx2.vout.size() > txin.prevout.n && !tx2.vout[txin.prevout.n].IsNull());
|
||||
fDependsWait = true;
|
||||
setParentCheck.insert(*it2);
|
||||
} else {
|
||||
assert(active_coins_tip.HaveCoin(txin.prevout));
|
||||
}
|
||||
// We are iterating through the mempool entries sorted in order by ancestor count.
|
||||
// All parents must have been checked before their children and their coins added to
|
||||
// the mempoolDuplicate coins cache.
|
||||
assert(mempoolDuplicate.HaveCoin(txin.prevout));
|
||||
// Check whether its inputs are marked in mapNextTx.
|
||||
auto it3 = mapNextTx.find(txin.prevout);
|
||||
assert(it3 != mapNextTx.end());
|
||||
assert(it3->first == &txin.prevout);
|
||||
assert(it3->second == &tx);
|
||||
i++;
|
||||
}
|
||||
auto comp = [](const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) -> bool {
|
||||
return a.GetTx().GetHash() == b.GetTx().GetHash();
|
||||
@@ -751,6 +739,9 @@ void CTxMemPool::check(CChainState& active_chainstate) const
|
||||
assert(it->GetSizeWithAncestors() == nSizeCheck);
|
||||
assert(it->GetSigOpCostWithAncestors() == nSigOpCheck);
|
||||
assert(it->GetModFeesWithAncestors() == nFeesCheck);
|
||||
// Sanity check: we are walking in ascending ancestor count order.
|
||||
assert(prev_ancestor_count <= it->GetCountWithAncestors());
|
||||
prev_ancestor_count = it->GetCountWithAncestors();
|
||||
|
||||
// Check children against mapNextTx
|
||||
CTxMemPoolEntry::Children setChildrenCheck;
|
||||
@@ -769,24 +760,12 @@ void CTxMemPool::check(CChainState& active_chainstate) const
|
||||
// just a sanity check, not definitive that this calc is correct...
|
||||
assert(it->GetSizeWithDescendants() >= child_sizes + it->GetTxSize());
|
||||
|
||||
if (fDependsWait)
|
||||
waitingOnDependants.push_back(&(*it));
|
||||
else {
|
||||
CheckInputsAndUpdateCoins(tx, mempoolDuplicate, spendheight);
|
||||
}
|
||||
}
|
||||
unsigned int stepsSinceLastRemove = 0;
|
||||
while (!waitingOnDependants.empty()) {
|
||||
const CTxMemPoolEntry* entry = waitingOnDependants.front();
|
||||
waitingOnDependants.pop_front();
|
||||
if (!mempoolDuplicate.HaveInputs(entry->GetTx())) {
|
||||
waitingOnDependants.push_back(entry);
|
||||
stepsSinceLastRemove++;
|
||||
assert(stepsSinceLastRemove < waitingOnDependants.size());
|
||||
} else {
|
||||
CheckInputsAndUpdateCoins(entry->GetTx(), mempoolDuplicate, spendheight);
|
||||
stepsSinceLastRemove = 0;
|
||||
}
|
||||
TxValidationState dummy_state; // Not used. CheckTxInputs() should always pass
|
||||
CAmount txfee = 0;
|
||||
assert(!tx.IsCoinBase());
|
||||
assert(Consensus::CheckTxInputs(tx, dummy_state, mempoolDuplicate, spendheight, txfee));
|
||||
for (const auto& input: tx.vin) mempoolDuplicate.SpendCoin(input.prevout);
|
||||
AddCoins(mempoolDuplicate, tx, std::numeric_limits<int>::max());
|
||||
}
|
||||
for (auto it = mapNextTx.cbegin(); it != mapNextTx.cend(); it++) {
|
||||
uint256 hash = it->second->GetHash();
|
||||
|
||||
Reference in New Issue
Block a user