Skip to content

Commit f4ea109

Browse files
MarcoFalkePastaPastaPasta
authored andcommitted
(partial) Merge bitcoin#21562: [net processing] Various tidying up of PeerManagerImpl ctor
fde1bf4 [net processing] Default initialize m_recent_confirmed_transactions (John Newbery) 37dcd12 scripted-diff: Rename recentRejects (John Newbery) cd9902a [net processing] Default initialize recentRejects (John Newbery) a28bfd1 [net processing] Default initialize m_stale_tip_check_time (John Newbery) 9190b01 [net processing] Add Orphanage empty consistency check (John Newbery) Pull request description: - Use default initialization of PeerManagerImpl members where possible - Remove unique_ptr indirection where it's not needed ACKs for top commit: MarcoFalke: ACK fde1bf4 👞 theStack: re-ACK fde1bf4 Tree-SHA512: 7ddedcc972df8e933e1fbe5c88b8ea17df89e1e58fc769518512c5540e49dc8eddb3f47e78d1329a6fc5644d2c1d11c981f681fd633f5218bfa4b3e6a86f3d7b
1 parent 42aea04 commit f4ea109

File tree

2 files changed

+30
-36
lines changed

2 files changed

+30
-36
lines changed

src/net_processing.cpp

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,8 @@ class PeerManagerImpl final : public PeerManager
318318
/** The height of the best chain */
319319
std::atomic<int> m_best_height{-1};
320320

321-
int64_t m_stale_tip_check_time; //!< Next time to check for stale tip
321+
/** Next time to check for stale tip */
322+
int64_t m_stale_tip_check_time{0};
322323

323324
/** Whether this node is running in blocks only mode */
324325
const bool m_ignore_incoming_txs;
@@ -443,16 +444,26 @@ class PeerManagerImpl final : public PeerManager
443444
*
444445
* Memory used: 1.3MB
445446
*/
446-
std::unique_ptr<CRollingBloomFilter> recentRejects GUARDED_BY(cs_main);
447+
CRollingBloomFilter m_recent_rejects GUARDED_BY(::cs_main){120'000, 0.000'001};
447448
uint256 hashRecentRejectsChainTip GUARDED_BY(cs_main);
448449

449450
/*
450451
* Filter for transactions that have been recently confirmed.
451452
* We use this to avoid requesting transactions that have already been
452453
* confirnmed.
454+
*
455+
* Blocks don't typically have more than 4000 transactions, so this should
456+
* be at least six blocks (~1 hr) worth of transactions that we can store,
457+
* inserting both a txid and wtxid for every observed transaction.
458+
* If the number of transactions appearing in a block goes up, or if we are
459+
* seeing getdata requests more than an hour after initial announcement, we
460+
* can increase this number.
461+
* The false positive rate of 1/1M should come out to less than 1
462+
* transaction per day that would be inadvertently ignored (which is the
463+
* same probability that we have in the reject filter).
453464
*/
454465
Mutex m_recent_confirmed_transactions_mutex;
455-
std::unique_ptr<CRollingBloomFilter> m_recent_confirmed_transactions GUARDED_BY(m_recent_confirmed_transactions_mutex);
466+
CRollingBloomFilter m_recent_confirmed_transactions GUARDED_BY(m_recent_confirmed_transactions_mutex){48'000, 0.000'001};
456467

457468
/* Returns a bool indicating whether we requested this block.
458469
* Also used if a block was /not/ received and timed out or started with another peer
@@ -1567,23 +1578,9 @@ PeerManagerImpl::PeerManagerImpl(const CChainParams& chainparams, CConnman& conn
15671578
m_cj_ctx(cj_ctx),
15681579
m_llmq_ctx(llmq_ctx),
15691580
m_govman(govman),
1570-
m_stale_tip_check_time(0),
15711581
m_ignore_incoming_txs(ignore_incoming_txs)
15721582
{
15731583
assert(std::addressof(g_chainman) == std::addressof(m_chainman));
1574-
// Initialize global variables that cannot be constructed at startup.
1575-
recentRejects.reset(new CRollingBloomFilter(120000, 0.000001));
1576-
1577-
// Blocks don't typically have more than 4000 transactions, so this should
1578-
// be at least six blocks (~1 hr) worth of transactions that we can store.
1579-
// If the number of transactions appearing in a block goes up, or if we are
1580-
// seeing getdata requests more than an hour after initial announcement, we
1581-
// can increase this number.
1582-
// The false positive rate of 1/1M should come out to less than 1
1583-
// transaction per day that would be inadvertently ignored (which is the
1584-
// same probability that we have in the reject filter).
1585-
m_recent_confirmed_transactions.reset(new CRollingBloomFilter(24000, 0.000001));
1586-
15871584
const Consensus::Params& consensusParams = Params().GetConsensus();
15881585
// Stale tip checking and peer eviction are on two different timers, but we
15891586
// don't want them to get out of sync due to drift in the scheduler, so we
@@ -1652,7 +1649,7 @@ void PeerManagerImpl::BlockConnected(const std::shared_ptr<const CBlock>& pblock
16521649
{
16531650
LOCK(m_recent_confirmed_transactions_mutex);
16541651
for (const auto& ptx : pblock->vtx) {
1655-
m_recent_confirmed_transactions->insert(ptx->GetHash());
1652+
m_recent_confirmed_transactions.insert(ptx->GetHash());
16561653
}
16571654
}
16581655
}
@@ -1668,7 +1665,7 @@ void PeerManagerImpl::BlockDisconnected(const std::shared_ptr<const CBlock> &blo
16681665
// presumably the most common case of relaying a confirmed transaction
16691666
// should be just after a new block containing it is found.
16701667
LOCK(m_recent_confirmed_transactions_mutex);
1671-
m_recent_confirmed_transactions->reset();
1668+
m_recent_confirmed_transactions.reset();
16721669
}
16731670

16741671
// All of the following cache a recent block, and are protected by cs_most_recent_block
@@ -1805,15 +1802,14 @@ bool PeerManagerImpl::AlreadyHave(const CInv& inv)
18051802
case MSG_TX:
18061803
case MSG_DSTX:
18071804
{
1808-
assert(recentRejects);
18091805
if (m_chainman.ActiveChain().Tip()->GetBlockHash() != hashRecentRejectsChainTip)
18101806
{
18111807
// If the chain tip has changed previously rejected transactions
18121808
// might be now valid, e.g. due to a nLockTime'd tx becoming valid,
18131809
// or a double-spend. Reset the rejects filter and give those
18141810
// txs a second chance.
18151811
hashRecentRejectsChainTip = m_chainman.ActiveChain().Tip()->GetBlockHash();
1816-
recentRejects->reset();
1812+
m_recent_rejects.reset();
18171813
}
18181814

18191815
{
@@ -1823,15 +1819,15 @@ bool PeerManagerImpl::AlreadyHave(const CInv& inv)
18231819

18241820
{
18251821
LOCK(m_recent_confirmed_transactions_mutex);
1826-
if (m_recent_confirmed_transactions->contains(inv.hash)) return true;
1822+
if (m_recent_confirmed_transactions.contains(inv.hash)) return true;
18271823
}
18281824

18291825
// When we receive an islock for a previously rejected transaction, we have to
18301826
// drop the first-seen tx (which such a locked transaction was conflicting with)
18311827
// and re-request the locked transaction (which did not make it into the mempool
18321828
// previously due to txn-mempool-conflict rule). This means that we must ignore
1833-
// recentRejects filter for such locked txes here.
1834-
// We also ignore recentRejects filter for DSTX-es because a malicious peer might
1829+
// m_recent_rejects filter for such locked txes here.
1830+
// We also ignore m_recent_rejects filter for DSTX-es because a malicious peer might
18351831
// relay a valid DSTX as a regular TX first which would skip all the specific checks
18361832
// but would cause such tx to be rejected by ATMP due to 0 fee. Ignoring it here
18371833
// should let DSTX to be propagated by honest peer later. Note, that a malicious
@@ -1842,7 +1838,7 @@ bool PeerManagerImpl::AlreadyHave(const CInv& inv)
18421838
m_llmq_ctx->isman->IsWaitingForTx(inv.hash) ||
18431839
m_llmq_ctx->isman->IsLocked(inv.hash);
18441840

1845-
return (!fIgnoreRecentRejects && recentRejects->contains(inv.hash)) ||
1841+
return (!fIgnoreRecentRejects && m_recent_rejects.contains(inv.hash)) ||
18461842
(inv.type == MSG_DSTX && static_cast<bool>(CCoinJoin::GetDSTX(inv.hash))) ||
18471843
m_mempool.exists(inv.hash) ||
18481844
(g_txindex != nullptr && g_txindex->HasTx(inv.hash));
@@ -2553,8 +2549,7 @@ void PeerManagerImpl::ProcessOrphanTx(std::set<uint256>& orphan_work_set)
25532549
// Has inputs but not accepted to mempool
25542550
// Probably non-standard or insufficient fee
25552551
LogPrint(BCLog::MEMPOOL, " removed orphan tx %s\n", orphanHash.ToString());
2556-
assert(recentRejects);
2557-
recentRejects->insert(orphanHash);
2552+
m_recent_rejects.insert(orphanHash);
25582553
EraseOrphanTx(orphanHash);
25592554
done = true;
25602555
}
@@ -3608,7 +3603,7 @@ void PeerManagerImpl::ProcessMessage(
36083603
{
36093604
bool fRejectedParents = false; // It may be the case that the orphans parents have all been rejected
36103605
for (const CTxIn& txin : tx.vin) {
3611-
if (recentRejects->contains(txin.prevout.hash)) {
3606+
if (m_recent_rejects.contains(txin.prevout.hash)) {
36123607
fRejectedParents = true;
36133608
break;
36143609
}
@@ -3637,12 +3632,11 @@ void PeerManagerImpl::ProcessMessage(
36373632
LogPrint(BCLog::MEMPOOL, "not keeping orphan with rejected parents %s\n",tx.GetHash().ToString());
36383633
// We will continue to reject this tx since it has rejected
36393634
// parents so avoid re-requesting it from other peers.
3640-
recentRejects->insert(tx.GetHash());
3635+
m_recent_rejects.insert(tx.GetHash());
36413636
m_llmq_ctx->isman->TransactionRemovedFromMempool(ptx);
36423637
}
36433638
} else {
3644-
assert(recentRejects);
3645-
recentRejects->insert(tx.GetHash());
3639+
m_recent_rejects.insert(tx.GetHash());
36463640
if (RecursiveDynamicUsage(*ptx) < 100000) {
36473641
AddToCompactExtraTransactions(ptx);
36483642
}
@@ -3661,21 +3655,21 @@ void PeerManagerImpl::ProcessMessage(
36613655
}
36623656
}
36633657

3664-
// If a tx has been detected by recentRejects, we will have reached
3658+
// If a tx has been detected by m_recent_rejects, we will have reached
36653659
// this point and the tx will have been ignored. Because we haven't run
36663660
// the tx through AcceptToMemoryPool, we won't have computed a DoS
36673661
// score for it or determined exactly why we consider it invalid.
36683662
//
36693663
// This means we won't penalize any peer subsequently relaying a DoSy
36703664
// tx (even if we penalized the first peer who gave it to us) because
3671-
// we have to account for recentRejects showing false positives. In
3665+
// we have to account for m_recent_rejects showing false positives. In
36723666
// other words, we shouldn't penalize a peer if we aren't *sure* they
36733667
// submitted a DoSy tx.
36743668
//
3675-
// Note that recentRejects doesn't just record DoSy or invalid
3669+
// Note that m_recent_rejects doesn't just record DoSy or invalid
36763670
// transactions, but any tx not accepted by the m_mempool, which may be
36773671
// due to node policy (vs. consensus). So we can't blanket penalize a
3678-
// peer simply for relaying a tx that our recentRejects has caught,
3672+
// peer simply for relaying a tx that our m_recent_rejects has caught,
36793673
// regardless of false positives.
36803674

36813675
if (state.IsInvalid())

test/functional/mempool_reorg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def run_test(self):
8181
spend_103_1_id = self.nodes[0].sendrawtransaction(spend_103_1_raw)
8282
last_block = self.nodes[0].generate(1)
8383
# Sync blocks, so that peer 1 gets the block before timelock_tx
84-
# Otherwise, peer 1 would put the timelock_tx in recentRejects
84+
# Otherwise, peer 1 would put the timelock_tx in m_recent_rejects
8585
self.sync_all()
8686

8787
# Time-locked transaction can now be spent

0 commit comments

Comments
 (0)