Skip to content

Commit a957bdb

Browse files
dergoeggedenavila
authored andcommitted
[net processing] Lazily initialize m_recent_rejects
1 parent b00f644 commit a957bdb

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

src/net_processing.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,18 @@ class PeerManagerImpl final : public PeerManager
897897
*
898898
* Memory used: 1.3 MB
899899
*/
900-
CRollingBloomFilter m_recent_rejects GUARDED_BY(m_tx_download_mutex){120'000, 0.000'001};
900+
std::unique_ptr<CRollingBloomFilter> m_recent_rejects GUARDED_BY(m_tx_download_mutex){nullptr};
901+
902+
CRollingBloomFilter& RecentRejectsFilter() EXCLUSIVE_LOCKS_REQUIRED(m_tx_download_mutex)
903+
{
904+
AssertLockHeld(m_tx_download_mutex);
905+
906+
if (!m_recent_rejects) {
907+
m_recent_rejects = std::make_unique<CRollingBloomFilter>(120'000, 0.000'001);
908+
}
909+
910+
return *m_recent_rejects;
911+
}
901912

902913
/**
903914
* Filter for:
@@ -2080,7 +2091,7 @@ void PeerManagerImpl::ActiveTipChange(const CBlockIndex& new_tip, bool is_ibd)
20802091
// If the chain tip has changed, previously rejected transactions might now be valid, e.g. due
20812092
// to a timelock. Reset the rejection filters to give those transactions another chance if we
20822093
// see them again.
2083-
m_recent_rejects.reset();
2094+
RecentRejectsFilter().reset();
20842095
m_recent_rejects_reconsiderable.reset();
20852096
}
20862097
}
@@ -2304,7 +2315,7 @@ bool PeerManagerImpl::AlreadyHaveTx(const GenTxid& gtxid, bool include_reconside
23042315

23052316
if (m_recent_confirmed_transactions.contains(hash)) return true;
23062317

2307-
return m_recent_rejects.contains(hash) || m_mempool.exists(gtxid);
2318+
return RecentRejectsFilter().contains(hash) || m_mempool.exists(gtxid);
23082319
}
23092320

23102321
bool PeerManagerImpl::AlreadyHaveBlock(const uint256& block_hash)
@@ -3197,7 +3208,7 @@ void PeerManagerImpl::ProcessInvalidTx(NodeId nodeid, const CTransactionRef& ptx
31973208
// submit it as part of a package later.
31983209
m_recent_rejects_reconsiderable.insert(ptx->GetWitnessHash().ToUint256());
31993210
} else {
3200-
m_recent_rejects.insert(ptx->GetWitnessHash().ToUint256());
3211+
RecentRejectsFilter().insert(ptx->GetWitnessHash().ToUint256());
32013212
}
32023213
m_txrequest.ForgetTxHash(ptx->GetWitnessHash());
32033214
// If the transaction failed for TX_INPUTS_NOT_STANDARD,
@@ -3211,7 +3222,7 @@ void PeerManagerImpl::ProcessInvalidTx(NodeId nodeid, const CTransactionRef& ptx
32113222
// We only add the txid if it differs from the wtxid, to avoid wasting entries in the
32123223
// rolling bloom filter.
32133224
if (state.GetResult() == TxValidationResult::TX_INPUTS_NOT_STANDARD && ptx->HasWitness()) {
3214-
m_recent_rejects.insert(ptx->GetHash().ToUint256());
3225+
RecentRejectsFilter().insert(ptx->GetHash().ToUint256());
32153226
m_txrequest.ForgetTxHash(ptx->GetHash());
32163227
}
32173228
if (maybe_add_extra_compact_tx && RecursiveDynamicUsage(*ptx) < 100000) {
@@ -4609,7 +4620,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
46094620
// submit 1p1c packages. However, fail immediately if any are in m_recent_rejects.
46104621
std::optional<uint256> rejected_parent_reconsiderable;
46114622
for (const uint256& parent_txid : unique_parents) {
4612-
if (m_recent_rejects.contains(parent_txid)) {
4623+
if (RecentRejectsFilter().contains(parent_txid)) {
46134624
fRejectedParents = true;
46144625
break;
46154626
} else if (m_recent_rejects_reconsiderable.contains(parent_txid) && !m_mempool.exists(GenTxid::Txid(parent_txid))) {
@@ -4658,8 +4669,8 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
46584669
// regardless of what witness is provided, we will not accept
46594670
// this, so we don't need to allow for redownload of this txid
46604671
// from any of our non-wtxidrelay peers.
4661-
m_recent_rejects.insert(tx.GetHash().ToUint256());
4662-
m_recent_rejects.insert(tx.GetWitnessHash().ToUint256());
4672+
RecentRejectsFilter().insert(tx.GetHash().ToUint256());
4673+
RecentRejectsFilter().insert(tx.GetWitnessHash().ToUint256());
46634674
m_txrequest.ForgetTxHash(tx.GetHash());
46644675
m_txrequest.ForgetTxHash(tx.GetWitnessHash());
46654676
}

0 commit comments

Comments
 (0)