mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-29 14:59:39 -04:00
test: expand LimitOrphan and EraseForPeer coverage
This commit is contained in:
parent
28dbe218fe
commit
172c1ad026
1 changed files with 41 additions and 7 deletions
|
@ -114,6 +114,10 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
|
||||||
FillableSigningProvider keystore;
|
FillableSigningProvider keystore;
|
||||||
BOOST_CHECK(keystore.AddKey(key));
|
BOOST_CHECK(keystore.AddKey(key));
|
||||||
|
|
||||||
|
// Freeze time for length of test
|
||||||
|
auto now{GetTime<std::chrono::seconds>()};
|
||||||
|
SetMockTime(now);
|
||||||
|
|
||||||
// 50 orphan transactions:
|
// 50 orphan transactions:
|
||||||
for (int i = 0; i < 50; i++)
|
for (int i = 0; i < 50; i++)
|
||||||
{
|
{
|
||||||
|
@ -172,22 +176,52 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
|
||||||
BOOST_CHECK(!orphanage.AddTx(MakeTransactionRef(tx), i));
|
BOOST_CHECK(!orphanage.AddTx(MakeTransactionRef(tx), i));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test EraseOrphansFor:
|
size_t expected_num_orphans = orphanage.CountOrphans();
|
||||||
|
|
||||||
|
// Non-existent peer; nothing should be deleted
|
||||||
|
orphanage.EraseForPeer(/*peer=*/-1);
|
||||||
|
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), expected_num_orphans);
|
||||||
|
|
||||||
|
// Each of first three peers stored
|
||||||
|
// two transactions each.
|
||||||
for (NodeId i = 0; i < 3; i++)
|
for (NodeId i = 0; i < 3; i++)
|
||||||
{
|
{
|
||||||
size_t sizeBefore = orphanage.CountOrphans();
|
|
||||||
orphanage.EraseForPeer(i);
|
orphanage.EraseForPeer(i);
|
||||||
BOOST_CHECK(orphanage.CountOrphans() < sizeBefore);
|
expected_num_orphans -= 2;
|
||||||
|
BOOST_CHECK(orphanage.CountOrphans() == expected_num_orphans);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test LimitOrphanTxSize() function:
|
// Test LimitOrphanTxSize() function, nothing should timeout:
|
||||||
FastRandomContext rng{/*fDeterministic=*/true};
|
FastRandomContext rng{/*fDeterministic=*/true};
|
||||||
|
orphanage.LimitOrphans(/*max_orphans=*/expected_num_orphans, rng);
|
||||||
|
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), expected_num_orphans);
|
||||||
|
expected_num_orphans -= 1;
|
||||||
|
orphanage.LimitOrphans(/*max_orphans=*/expected_num_orphans, rng);
|
||||||
|
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), expected_num_orphans);
|
||||||
|
assert(expected_num_orphans > 40);
|
||||||
orphanage.LimitOrphans(40, rng);
|
orphanage.LimitOrphans(40, rng);
|
||||||
BOOST_CHECK(orphanage.CountOrphans() <= 40);
|
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), 40);
|
||||||
orphanage.LimitOrphans(10, rng);
|
orphanage.LimitOrphans(10, rng);
|
||||||
BOOST_CHECK(orphanage.CountOrphans() <= 10);
|
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), 10);
|
||||||
orphanage.LimitOrphans(0, rng);
|
orphanage.LimitOrphans(0, rng);
|
||||||
BOOST_CHECK(orphanage.CountOrphans() == 0);
|
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), 0);
|
||||||
|
|
||||||
|
// Add one more orphan, check timeout logic
|
||||||
|
auto timeout_tx = MakeTransactionSpending(/*outpoints=*/{}, rng);
|
||||||
|
orphanage.AddTx(timeout_tx, 0);
|
||||||
|
orphanage.LimitOrphans(1, rng);
|
||||||
|
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), 1);
|
||||||
|
|
||||||
|
// One second shy of expiration
|
||||||
|
SetMockTime(now + ORPHAN_TX_EXPIRE_TIME - 1s);
|
||||||
|
orphanage.LimitOrphans(1, rng);
|
||||||
|
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), 1);
|
||||||
|
|
||||||
|
// Jump one more second, orphan should be timed out on limiting
|
||||||
|
SetMockTime(now + ORPHAN_TX_EXPIRE_TIME);
|
||||||
|
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), 1);
|
||||||
|
orphanage.LimitOrphans(1, rng);
|
||||||
|
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(same_txid_diff_witness)
|
BOOST_AUTO_TEST_CASE(same_txid_diff_witness)
|
||||||
|
|
Loading…
Add table
Reference in a new issue