mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-08-25 20:41:21 +02:00
[prep/test] modify test to not access TxOrphanage internals
These internals should and will be private.
This commit is contained in:
@@ -26,20 +26,6 @@ class TxOrphanageTest : public node::TxOrphanage
|
|||||||
public:
|
public:
|
||||||
TxOrphanageTest(FastRandomContext& rng) : m_rng{rng} {}
|
TxOrphanageTest(FastRandomContext& rng) : m_rng{rng} {}
|
||||||
|
|
||||||
inline size_t CountOrphans() const
|
|
||||||
{
|
|
||||||
return m_orphans.size();
|
|
||||||
}
|
|
||||||
|
|
||||||
CTransactionRef RandomOrphan()
|
|
||||||
{
|
|
||||||
std::map<Wtxid, OrphanTx>::iterator it;
|
|
||||||
it = m_orphans.lower_bound(Wtxid::FromUint256(m_rng.rand256()));
|
|
||||||
if (it == m_orphans.end())
|
|
||||||
it = m_orphans.begin();
|
|
||||||
return it->second.tx;
|
|
||||||
}
|
|
||||||
|
|
||||||
FastRandomContext& m_rng;
|
FastRandomContext& m_rng;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -114,6 +100,8 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
|
|||||||
auto now{GetTime<std::chrono::seconds>()};
|
auto now{GetTime<std::chrono::seconds>()};
|
||||||
SetMockTime(now);
|
SetMockTime(now);
|
||||||
|
|
||||||
|
std::vector<CTransactionRef> orphans_added;
|
||||||
|
|
||||||
// 50 orphan transactions:
|
// 50 orphan transactions:
|
||||||
for (int i = 0; i < 50; i++)
|
for (int i = 0; i < 50; i++)
|
||||||
{
|
{
|
||||||
@@ -126,13 +114,15 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
|
|||||||
tx.vout[0].nValue = i*CENT;
|
tx.vout[0].nValue = i*CENT;
|
||||||
tx.vout[0].scriptPubKey = GetScriptForDestination(PKHash(key.GetPubKey()));
|
tx.vout[0].scriptPubKey = GetScriptForDestination(PKHash(key.GetPubKey()));
|
||||||
|
|
||||||
orphanage.AddTx(MakeTransactionRef(tx), i);
|
auto ptx = MakeTransactionRef(tx);
|
||||||
|
orphanage.AddTx(ptx, i);
|
||||||
|
orphans_added.emplace_back(ptx);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ... and 50 that depend on other orphans:
|
// ... and 50 that depend on other orphans:
|
||||||
for (int i = 0; i < 50; i++)
|
for (int i = 0; i < 50; i++)
|
||||||
{
|
{
|
||||||
CTransactionRef txPrev = orphanage.RandomOrphan();
|
const auto& txPrev = orphans_added[m_rng.randrange(orphans_added.size())];
|
||||||
|
|
||||||
CMutableTransaction tx;
|
CMutableTransaction tx;
|
||||||
tx.vin.resize(1);
|
tx.vin.resize(1);
|
||||||
@@ -144,13 +134,15 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
|
|||||||
SignatureData empty;
|
SignatureData empty;
|
||||||
BOOST_CHECK(SignSignature(keystore, *txPrev, tx, 0, SIGHASH_ALL, empty));
|
BOOST_CHECK(SignSignature(keystore, *txPrev, tx, 0, SIGHASH_ALL, empty));
|
||||||
|
|
||||||
orphanage.AddTx(MakeTransactionRef(tx), i);
|
auto ptx = MakeTransactionRef(tx);
|
||||||
|
orphanage.AddTx(ptx, i);
|
||||||
|
orphans_added.emplace_back(ptx);
|
||||||
}
|
}
|
||||||
|
|
||||||
// This really-big orphan should be ignored:
|
// This really-big orphan should be ignored:
|
||||||
for (int i = 0; i < 10; i++)
|
for (int i = 0; i < 10; i++)
|
||||||
{
|
{
|
||||||
CTransactionRef txPrev = orphanage.RandomOrphan();
|
const auto& txPrev = orphans_added[m_rng.randrange(orphans_added.size())];
|
||||||
|
|
||||||
CMutableTransaction tx;
|
CMutableTransaction tx;
|
||||||
tx.vout.resize(1);
|
tx.vout.resize(1);
|
||||||
@@ -172,11 +164,11 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
|
|||||||
BOOST_CHECK(!orphanage.AddTx(MakeTransactionRef(tx), i));
|
BOOST_CHECK(!orphanage.AddTx(MakeTransactionRef(tx), i));
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t expected_num_orphans = orphanage.CountOrphans();
|
size_t expected_num_orphans = orphanage.Size();
|
||||||
|
|
||||||
// Non-existent peer; nothing should be deleted
|
// Non-existent peer; nothing should be deleted
|
||||||
orphanage.EraseForPeer(/*peer=*/-1);
|
orphanage.EraseForPeer(/*peer=*/-1);
|
||||||
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), expected_num_orphans);
|
BOOST_CHECK_EQUAL(orphanage.Size(), expected_num_orphans);
|
||||||
|
|
||||||
// Each of first three peers stored
|
// Each of first three peers stored
|
||||||
// two transactions each.
|
// two transactions each.
|
||||||
@@ -184,40 +176,40 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
|
|||||||
{
|
{
|
||||||
orphanage.EraseForPeer(i);
|
orphanage.EraseForPeer(i);
|
||||||
expected_num_orphans -= 2;
|
expected_num_orphans -= 2;
|
||||||
BOOST_CHECK(orphanage.CountOrphans() == expected_num_orphans);
|
BOOST_CHECK(orphanage.Size() == expected_num_orphans);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Test LimitOrphanTxSize() function, nothing should timeout:
|
// Test LimitOrphanTxSize() function, nothing should timeout:
|
||||||
FastRandomContext rng{/*fDeterministic=*/true};
|
FastRandomContext rng{/*fDeterministic=*/true};
|
||||||
orphanage.LimitOrphans(/*max_orphans=*/expected_num_orphans, rng);
|
orphanage.LimitOrphans(/*max_orphans=*/expected_num_orphans, rng);
|
||||||
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), expected_num_orphans);
|
BOOST_CHECK_EQUAL(orphanage.Size(), expected_num_orphans);
|
||||||
expected_num_orphans -= 1;
|
expected_num_orphans -= 1;
|
||||||
orphanage.LimitOrphans(/*max_orphans=*/expected_num_orphans, rng);
|
orphanage.LimitOrphans(/*max_orphans=*/expected_num_orphans, rng);
|
||||||
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), expected_num_orphans);
|
BOOST_CHECK_EQUAL(orphanage.Size(), expected_num_orphans);
|
||||||
assert(expected_num_orphans > 40);
|
assert(expected_num_orphans > 40);
|
||||||
orphanage.LimitOrphans(40, rng);
|
orphanage.LimitOrphans(40, rng);
|
||||||
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), 40);
|
BOOST_CHECK_EQUAL(orphanage.Size(), 40);
|
||||||
orphanage.LimitOrphans(10, rng);
|
orphanage.LimitOrphans(10, rng);
|
||||||
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), 10);
|
BOOST_CHECK_EQUAL(orphanage.Size(), 10);
|
||||||
orphanage.LimitOrphans(0, rng);
|
orphanage.LimitOrphans(0, rng);
|
||||||
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), 0);
|
BOOST_CHECK_EQUAL(orphanage.Size(), 0);
|
||||||
|
|
||||||
// Add one more orphan, check timeout logic
|
// Add one more orphan, check timeout logic
|
||||||
auto timeout_tx = MakeTransactionSpending(/*outpoints=*/{}, rng);
|
auto timeout_tx = MakeTransactionSpending(/*outpoints=*/{}, rng);
|
||||||
orphanage.AddTx(timeout_tx, 0);
|
orphanage.AddTx(timeout_tx, 0);
|
||||||
orphanage.LimitOrphans(1, rng);
|
orphanage.LimitOrphans(1, rng);
|
||||||
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), 1);
|
BOOST_CHECK_EQUAL(orphanage.Size(), 1);
|
||||||
|
|
||||||
// One second shy of expiration
|
// One second shy of expiration
|
||||||
SetMockTime(now + node::ORPHAN_TX_EXPIRE_TIME - 1s);
|
SetMockTime(now + node::ORPHAN_TX_EXPIRE_TIME - 1s);
|
||||||
orphanage.LimitOrphans(1, rng);
|
orphanage.LimitOrphans(1, rng);
|
||||||
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), 1);
|
BOOST_CHECK_EQUAL(orphanage.Size(), 1);
|
||||||
|
|
||||||
// Jump one more second, orphan should be timed out on limiting
|
// Jump one more second, orphan should be timed out on limiting
|
||||||
SetMockTime(now + node::ORPHAN_TX_EXPIRE_TIME);
|
SetMockTime(now + node::ORPHAN_TX_EXPIRE_TIME);
|
||||||
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), 1);
|
BOOST_CHECK_EQUAL(orphanage.Size(), 1);
|
||||||
orphanage.LimitOrphans(1, rng);
|
orphanage.LimitOrphans(1, rng);
|
||||||
BOOST_CHECK_EQUAL(orphanage.CountOrphans(), 0);
|
BOOST_CHECK_EQUAL(orphanage.Size(), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(same_txid_diff_witness)
|
BOOST_AUTO_TEST_CASE(same_txid_diff_witness)
|
||||||
|
Reference in New Issue
Block a user