Skip to content

Commit dd7e12a

Browse files
committed
Merge bitcoin#30082: test: expand LimitOrphan and EraseForPeer coverage
172c1ad test: expand LimitOrphan and EraseForPeer coverage (Greg Sanders) 28dbe21 refactor: move orphanage constants to header file (Greg Sanders) Pull request description: Inspired by refactorings in bitcoin#30000 as the coverage appeared a bit sparse. Added some minimal border value testing, timeouts, and tightened existing assertions. ACKs for top commit: achow101: ACK 172c1ad rkrux: reACK [172c1ad](bitcoin@172c1ad) glozow: reACK 172c1ad Tree-SHA512: e8fa9b1de6a8617612bbe9b132c9c0c9b5a651ec94fd8c91042a34a8c91c5f9fa7ec4175b47e2b97d1320d452c23775be671a9970613533e68e81937539a7d70
2 parents 902dd14 + 172c1ad commit dd7e12a

File tree

3 files changed

+46
-13
lines changed

3 files changed

+46
-13
lines changed

src/test/orphanage_tests.cpp

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
112112
FillableSigningProvider keystore;
113113
BOOST_CHECK(keystore.AddKey(key));
114114

115+
// Freeze time for length of test
116+
auto now{GetTime<std::chrono::seconds>()};
117+
SetMockTime(now);
118+
115119
// 50 orphan transactions:
116120
for (int i = 0; i < 50; i++)
117121
{
@@ -170,22 +174,52 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
170174
BOOST_CHECK(!orphanage.AddTx(MakeTransactionRef(tx), i));
171175
}
172176

173-
// Test EraseOrphansFor:
177+
size_t expected_num_orphans = orphanage.CountOrphans();
178+
179+
// Non-existent peer; nothing should be deleted
180+
orphanage.EraseForPeer(/*peer=*/-1);
181+
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), expected_num_orphans);
182+
183+
// Each of first three peers stored
184+
// two transactions each.
174185
for (NodeId i = 0; i < 3; i++)
175186
{
176-
size_t sizeBefore = orphanage.CountOrphans();
177187
orphanage.EraseForPeer(i);
178-
BOOST_CHECK(orphanage.CountOrphans() < sizeBefore);
188+
expected_num_orphans -= 2;
189+
BOOST_CHECK(orphanage.CountOrphans() == expected_num_orphans);
179190
}
180191

181-
// Test LimitOrphanTxSize() function:
192+
// Test LimitOrphanTxSize() function, nothing should timeout:
182193
FastRandomContext rng{/*fDeterministic=*/true};
194+
orphanage.LimitOrphans(/*max_orphans=*/expected_num_orphans, rng);
195+
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), expected_num_orphans);
196+
expected_num_orphans -= 1;
197+
orphanage.LimitOrphans(/*max_orphans=*/expected_num_orphans, rng);
198+
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), expected_num_orphans);
199+
assert(expected_num_orphans > 40);
183200
orphanage.LimitOrphans(40, rng);
184-
BOOST_CHECK(orphanage.CountOrphans() <= 40);
201+
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), 40);
185202
orphanage.LimitOrphans(10, rng);
186-
BOOST_CHECK(orphanage.CountOrphans() <= 10);
203+
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), 10);
187204
orphanage.LimitOrphans(0, rng);
188-
BOOST_CHECK(orphanage.CountOrphans() == 0);
205+
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), 0);
206+
207+
// Add one more orphan, check timeout logic
208+
auto timeout_tx = MakeTransactionSpending(/*outpoints=*/{}, rng);
209+
orphanage.AddTx(timeout_tx, 0);
210+
orphanage.LimitOrphans(1, rng);
211+
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), 1);
212+
213+
// One second shy of expiration
214+
SetMockTime(now + ORPHAN_TX_EXPIRE_TIME - 1s);
215+
orphanage.LimitOrphans(1, rng);
216+
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), 1);
217+
218+
// Jump one more second, orphan should be timed out on limiting
219+
SetMockTime(now + ORPHAN_TX_EXPIRE_TIME);
220+
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), 1);
221+
orphanage.LimitOrphans(1, rng);
222+
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), 0);
189223
}
190224

191225
BOOST_AUTO_TEST_CASE(same_txid_diff_witness)

src/txorphanage.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@
1212

1313
#include <cassert>
1414

15-
/** Expiration time for orphan transactions */
16-
static constexpr auto ORPHAN_TX_EXPIRE_TIME{20min};
17-
/** Minimum time between orphan transactions expire time checks */
18-
static constexpr auto ORPHAN_TX_EXPIRE_INTERVAL{5min};
19-
20-
2115
bool TxOrphanage::AddTx(const CTransactionRef& tx, NodeId peer)
2216
{
2317
const Txid& hash = tx->GetHash();

src/txorphanage.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
#include <map>
1515
#include <set>
1616

17+
/** Expiration time for orphan transactions */
18+
static constexpr auto ORPHAN_TX_EXPIRE_TIME{20min};
19+
/** Minimum time between orphan transactions expire time checks */
20+
static constexpr auto ORPHAN_TX_EXPIRE_INTERVAL{5min};
21+
1722
/** A class to track orphan transactions (failed on TX_MISSING_INPUTS)
1823
* Since we cannot distinguish orphans from bad transactions with
1924
* non-existent inputs, we heavily limit the number of orphans

0 commit comments

Comments
 (0)