Skip to content

Commit 77ebe8f

Browse files
committed
[prep/test] have TxOrphanage remember its own limits in LimitOrphans
Move towards a model where TxOrphanage is initialized with limits that it remembers throughout its lifetime. Remove the param. Limiting by number of unique orphans will be removed in a later commit. Now that -maxorphantx is gone, this does not change the node behavior. The parameter is only used in tests.
1 parent d0af423 commit 77ebe8f

10 files changed

+19
-36
lines changed

src/net_processing.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1925,7 +1925,7 @@ PeerManagerImpl::PeerManagerImpl(CConnman& connman, AddrMan& addrman,
19251925
m_banman(banman),
19261926
m_chainman(chainman),
19271927
m_mempool(pool),
1928-
m_txdownloadman(node::TxDownloadOptions{pool, m_rng, opts.max_orphan_txs, opts.deterministic_rng}),
1928+
m_txdownloadman(node::TxDownloadOptions{pool, m_rng, opts.deterministic_rng}),
19291929
m_warnings{warnings},
19301930
m_opts{opts}
19311931
{

src/net_processing.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ class PeerManager : public CValidationInterface, public NetEventsInterface
7676
bool ignore_incoming_txs{DEFAULT_BLOCKSONLY};
7777
//! Whether transaction reconciliation protocol is enabled
7878
bool reconcile_txs{DEFAULT_TXRECONCILIATION_ENABLE};
79-
//! Maximum number of orphan transactions kept in memory
80-
uint32_t max_orphan_txs{node::DEFAULT_MAX_ORPHAN_TRANSACTIONS};
8179
//! Number of non-mempool transactions to keep around for block reconstruction. Includes
8280
//! orphan, replaced, and rejected transactions.
8381
uint32_t max_extra_txs{DEFAULT_BLOCK_RECONSTRUCTION_EXTRA_TXN};

src/node/txdownloadman.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ struct TxDownloadOptions {
4141
const CTxMemPool& m_mempool;
4242
/** RNG provided by caller. */
4343
FastRandomContext& m_rng;
44-
/** Maximum number of transactions allowed in orphanage. */
45-
const uint32_t m_max_orphan_txs;
4644
/** Instantiate TxRequestTracker as deterministic (used for tests). */
4745
bool m_deterministic_txrequest{false};
4846
};

src/node/txdownloadman_impl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ node::RejectedTxTodo TxDownloadManagerImpl::MempoolRejectedTx(const CTransaction
422422
// DoS prevention: do not allow m_orphanage to grow unbounded (see CVE-2012-3789)
423423
// Note that, if the orphanage reaches capacity, it's possible that we immediately evict
424424
// the transaction we just added.
425-
m_orphanage.LimitOrphans(m_opts.m_max_orphan_txs, m_opts.m_rng);
425+
m_orphanage.LimitOrphans(m_opts.m_rng);
426426
} else {
427427
unique_parents.clear();
428428
LogDebug(BCLog::MEMPOOL, "not keeping orphan with rejected parents %s (wtxid=%s)\n",

src/node/txorphanage.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ void TxOrphanage::EraseForPeer(NodeId peer)
141141
if (nErased > 0) LogDebug(BCLog::TXPACKAGES, "Erased %d orphan transaction(s) from peer=%d\n", nErased, peer);
142142
}
143143

144-
void TxOrphanage::LimitOrphans(unsigned int max_orphans, FastRandomContext& rng)
144+
void TxOrphanage::LimitOrphans(FastRandomContext& rng)
145145
{
146146
unsigned int nEvicted = 0;
147147
auto nNow{Now<NodeSeconds>()};
@@ -163,7 +163,7 @@ void TxOrphanage::LimitOrphans(unsigned int max_orphans, FastRandomContext& rng)
163163
m_next_sweep = nMinExpTime + ORPHAN_TX_EXPIRE_INTERVAL;
164164
if (nErased > 0) LogDebug(BCLog::TXPACKAGES, "Erased %d orphan tx due to expiration\n", nErased);
165165
}
166-
while (m_orphans.size() > max_orphans)
166+
while (m_orphans.size() > DEFAULT_MAX_ORPHAN_TRANSACTIONS)
167167
{
168168
// Evict a random orphan:
169169
size_t randompos = rng.randrange(m_orphan_list.size());

src/node/txorphanage.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ class TxOrphanage {
6262
/** Erase all orphans included in or invalidated by a new block */
6363
void EraseForBlock(const CBlock& block);
6464

65-
/** Limit the orphanage to the given maximum */
66-
void LimitOrphans(unsigned int max_orphans, FastRandomContext& rng);
65+
/** Limit the orphanage to DEFAULT_MAX_ORPHAN_TRANSACTIONS. */
66+
void LimitOrphans(FastRandomContext& rng);
6767

6868
/** Add any orphans that list a particular tx as a parent into the from peer's work set */
6969
void AddChildrenToWorkSet(const CTransaction& tx, FastRandomContext& rng);

src/test/fuzz/txdownloadman.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,8 @@ FUZZ_TARGET(txdownloadman, .init = initialize)
173173
// Initialize txdownloadman
174174
bilingual_str error;
175175
CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node), error};
176-
const auto max_orphan_count = fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, 300);
177176
FastRandomContext det_rand{true};
178-
node::TxDownloadManager txdownloadman{node::TxDownloadOptions{pool, det_rand, max_orphan_count, true}};
177+
node::TxDownloadManager txdownloadman{node::TxDownloadOptions{pool, det_rand, true}};
179178

180179
std::chrono::microseconds time{244466666};
181180

@@ -304,9 +303,9 @@ FUZZ_TARGET(txdownloadman_impl, .init = initialize)
304303
// Initialize a TxDownloadManagerImpl
305304
bilingual_str error;
306305
CTxMemPool pool{MemPoolOptionsForTest(g_setup->m_node), error};
307-
const auto max_orphan_count = fuzzed_data_provider.ConsumeIntegralInRange<unsigned int>(0, 300);
306+
const auto max_orphan_count = node::DEFAULT_MAX_ORPHAN_TRANSACTIONS;
308307
FastRandomContext det_rand{true};
309-
node::TxDownloadManagerImpl txdownload_impl{node::TxDownloadOptions{pool, det_rand, max_orphan_count, true}};
308+
node::TxDownloadManagerImpl txdownload_impl{node::TxDownloadOptions{pool, det_rand, true}};
310309

311310
std::chrono::microseconds time{244466666};
312311

src/test/fuzz/txorphan.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,8 @@ FUZZ_TARGET(txorphan, .init = initialize_orphanage)
217217
[&] {
218218
// test mocktime and expiry
219219
SetMockTime(ConsumeTime(fuzzed_data_provider));
220-
auto limit = fuzzed_data_provider.ConsumeIntegral<unsigned int>();
221-
orphanage.LimitOrphans(limit, orphanage_rng);
222-
Assert(orphanage.Size() <= limit);
220+
orphanage.LimitOrphans(orphanage_rng);
221+
Assert(orphanage.Size() <= node::DEFAULT_MAX_ORPHAN_TRANSACTIONS);
223222
});
224223

225224
}

src/test/orphanage_tests.cpp

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -181,34 +181,23 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
181181

182182
// Test LimitOrphanTxSize() function, nothing should timeout:
183183
FastRandomContext rng{/*fDeterministic=*/true};
184-
orphanage.LimitOrphans(/*max_orphans=*/expected_num_orphans, rng);
184+
orphanage.LimitOrphans(rng);
185185
BOOST_CHECK_EQUAL(orphanage.Size(), expected_num_orphans);
186-
expected_num_orphans -= 1;
187-
orphanage.LimitOrphans(/*max_orphans=*/expected_num_orphans, rng);
188-
BOOST_CHECK_EQUAL(orphanage.Size(), expected_num_orphans);
189-
assert(expected_num_orphans > 40);
190-
orphanage.LimitOrphans(40, rng);
191-
BOOST_CHECK_EQUAL(orphanage.Size(), 40);
192-
orphanage.LimitOrphans(10, rng);
193-
BOOST_CHECK_EQUAL(orphanage.Size(), 10);
194-
orphanage.LimitOrphans(0, rng);
195-
BOOST_CHECK_EQUAL(orphanage.Size(), 0);
196186

197187
// Add one more orphan, check timeout logic
198188
auto timeout_tx = MakeTransactionSpending(/*outpoints=*/{}, rng);
199189
orphanage.AddTx(timeout_tx, 0);
200-
orphanage.LimitOrphans(1, rng);
201-
BOOST_CHECK_EQUAL(orphanage.Size(), 1);
190+
expected_num_orphans += 1;
191+
BOOST_CHECK_EQUAL(orphanage.Size(), expected_num_orphans);
202192

203193
// One second shy of expiration
204194
SetMockTime(now + node::ORPHAN_TX_EXPIRE_TIME - 1s);
205-
orphanage.LimitOrphans(1, rng);
206-
BOOST_CHECK_EQUAL(orphanage.Size(), 1);
195+
orphanage.LimitOrphans(rng);
196+
BOOST_CHECK_EQUAL(orphanage.Size(), expected_num_orphans);
207197

208198
// Jump one more second, orphan should be timed out on limiting
209199
SetMockTime(now + node::ORPHAN_TX_EXPIRE_TIME);
210-
BOOST_CHECK_EQUAL(orphanage.Size(), 1);
211-
orphanage.LimitOrphans(1, rng);
200+
orphanage.LimitOrphans(rng);
212201
BOOST_CHECK_EQUAL(orphanage.Size(), 0);
213202
}
214203

src/test/txdownload_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ BOOST_FIXTURE_TEST_CASE(tx_rejection_types, TestChain100Setup)
114114
{
115115
CTxMemPool& pool = *Assert(m_node.mempool);
116116
FastRandomContext det_rand{true};
117-
node::TxDownloadOptions DEFAULT_OPTS{pool, det_rand, node::DEFAULT_MAX_ORPHAN_TRANSACTIONS, true};
117+
node::TxDownloadOptions DEFAULT_OPTS{pool, det_rand, true};
118118

119119
// A new TxDownloadManagerImpl is created for each tx so we can just reuse the same one.
120120
TxValidationState state;
@@ -172,7 +172,7 @@ BOOST_FIXTURE_TEST_CASE(handle_missing_inputs, TestChain100Setup)
172172
{
173173
CTxMemPool& pool = *Assert(m_node.mempool);
174174
FastRandomContext det_rand{true};
175-
node::TxDownloadOptions DEFAULT_OPTS{pool, det_rand, node::DEFAULT_MAX_ORPHAN_TRANSACTIONS, true};
175+
node::TxDownloadOptions DEFAULT_OPTS{pool, det_rand, true};
176176
NodeId nodeid{1};
177177
node::TxDownloadConnectionInfo DEFAULT_CONN{/*m_preferred=*/false, /*m_relay_permissions=*/false, /*m_wtxid_relay=*/true};
178178

0 commit comments

Comments
 (0)