From cc50f2f0df6e6e2cc9b9aeb3c3c8e1c78fa5be1d Mon Sep 17 00:00:00 2001 From: glozow Date: Thu, 5 Jun 2025 14:13:28 -0400 Subject: [PATCH] [cleanup] replace TxOrphanage::Size() with CountUniqueOrphans --- src/node/txdownloadman_impl.cpp | 2 +- src/node/txorphanage.cpp | 1 - src/node/txorphanage.h | 3 --- src/test/orphanage_tests.cpp | 28 ++++++++++++++-------------- 4 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/node/txdownloadman_impl.cpp b/src/node/txdownloadman_impl.cpp index 99fa036bda3..38b949c654a 100644 --- a/src/node/txdownloadman_impl.cpp +++ b/src/node/txdownloadman_impl.cpp @@ -572,7 +572,7 @@ void TxDownloadManagerImpl::CheckIsEmpty(NodeId nodeid) void TxDownloadManagerImpl::CheckIsEmpty() { assert(m_orphanage->TotalOrphanUsage() == 0); - assert(m_orphanage->Size() == 0); + assert(m_orphanage->CountUniqueOrphans() == 0); assert(m_txrequest.Size() == 0); assert(m_num_wtxid_peers == 0); } diff --git a/src/node/txorphanage.cpp b/src/node/txorphanage.cpp index 33d2da32427..9d386b9dc0b 100644 --- a/src/node/txorphanage.cpp +++ b/src/node/txorphanage.cpp @@ -229,7 +229,6 @@ public: std::vector> AddChildrenToWorkSet(const CTransaction& tx, FastRandomContext& rng) override; bool HaveTxToReconsider(NodeId peer) override; std::vector GetChildrenFromSamePeer(const CTransactionRef& parent, NodeId nodeid) const override; - size_t Size() const override { return m_unique_orphans; } std::vector GetOrphanTransactions() const override; TxOrphanage::Usage TotalOrphanUsage() const override; void SanityCheck() const override; diff --git a/src/node/txorphanage.h b/src/node/txorphanage.h index 584fb390d34..24fa20bfe78 100644 --- a/src/node/txorphanage.h +++ b/src/node/txorphanage.h @@ -98,9 +98,6 @@ public: * recent to least recent. */ virtual std::vector GetChildrenFromSamePeer(const CTransactionRef& parent, NodeId nodeid) const = 0; - /** Return how many entries exist in the orphange */ - virtual size_t Size() const = 0; - /** Get all orphan transactions */ virtual std::vector GetOrphanTransactions() const = 0; diff --git a/src/test/orphanage_tests.cpp b/src/test/orphanage_tests.cpp index 905185a78ed..2de8693a5df 100644 --- a/src/test/orphanage_tests.cpp +++ b/src/test/orphanage_tests.cpp @@ -498,11 +498,11 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans) BOOST_CHECK(!orphanage->AddTx(MakeTransactionRef(tx), i)); } - size_t expected_num_orphans = orphanage->Size(); + size_t expected_num_orphans = orphanage->CountUniqueOrphans(); // Non-existent peer; nothing should be deleted orphanage->EraseForPeer(/*peer=*/-1); - BOOST_CHECK_EQUAL(orphanage->Size(), expected_num_orphans); + BOOST_CHECK_EQUAL(orphanage->CountUniqueOrphans(), expected_num_orphans); // Each of first three peers stored // two transactions each. @@ -510,7 +510,7 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans) { orphanage->EraseForPeer(i); expected_num_orphans -= 2; - BOOST_CHECK(orphanage->Size() == expected_num_orphans); + BOOST_CHECK_EQUAL(orphanage->CountUniqueOrphans(), expected_num_orphans); } } @@ -732,7 +732,7 @@ BOOST_AUTO_TEST_CASE(process_block) BOOST_CHECK(!orphanage->HaveTx(expected_removed_wtxid)); } // Only remaining tx is control_tx - BOOST_CHECK_EQUAL(orphanage->Size(), 1); + BOOST_CHECK_EQUAL(orphanage->CountUniqueOrphans(), 1); BOOST_CHECK(orphanage->HaveTx(control_tx->GetWitnessHash())); } @@ -753,11 +753,11 @@ BOOST_AUTO_TEST_CASE(multiple_announcers) BOOST_CHECK(orphanage->AddTx(ptx, node0)); BOOST_CHECK(orphanage->HaveTx(wtxid)); expected_total_count += 1; - BOOST_CHECK_EQUAL(orphanage->Size(), expected_total_count); + BOOST_CHECK_EQUAL(orphanage->CountUniqueOrphans(), expected_total_count); // Adding again should do nothing. BOOST_CHECK(!orphanage->AddTx(ptx, node0)); - BOOST_CHECK_EQUAL(orphanage->Size(), expected_total_count); + BOOST_CHECK_EQUAL(orphanage->CountUniqueOrphans(), expected_total_count); // We can add another tx with the same txid but different witness. auto ptx_mutated{MakeMutation(ptx)}; @@ -769,7 +769,7 @@ BOOST_AUTO_TEST_CASE(multiple_announcers) // Adding a new announcer should not change overall accounting. BOOST_CHECK(orphanage->AddAnnouncer(ptx->GetWitnessHash(), node2)); - BOOST_CHECK_EQUAL(orphanage->Size(), expected_total_count); + BOOST_CHECK_EQUAL(orphanage->CountUniqueOrphans(), expected_total_count); // If we already have this announcer, AddAnnouncer returns false. BOOST_CHECK(orphanage->HaveTxFromPeer(ptx->GetWitnessHash(), node2)); @@ -777,7 +777,7 @@ BOOST_AUTO_TEST_CASE(multiple_announcers) // Same with using AddTx for an existing tx, which is equivalent to using AddAnnouncer BOOST_CHECK(!orphanage->AddTx(ptx, node1)); - BOOST_CHECK_EQUAL(orphanage->Size(), expected_total_count); + BOOST_CHECK_EQUAL(orphanage->CountUniqueOrphans(), expected_total_count); // if EraseForPeer is called for an orphan with multiple announcers, the orphanage should only // erase that peer from the announcers set. @@ -787,15 +787,15 @@ BOOST_AUTO_TEST_CASE(multiple_announcers) // node0 is the only one that announced ptx_mutated BOOST_CHECK(!orphanage->HaveTx(ptx_mutated->GetWitnessHash())); expected_total_count -= 1; - BOOST_CHECK_EQUAL(orphanage->Size(), expected_total_count); + BOOST_CHECK_EQUAL(orphanage->CountUniqueOrphans(), expected_total_count); // EraseForPeer should delete the orphan if it's the only announcer left. orphanage->EraseForPeer(node1); - BOOST_CHECK_EQUAL(orphanage->Size(), expected_total_count); + BOOST_CHECK_EQUAL(orphanage->CountUniqueOrphans(), expected_total_count); BOOST_CHECK(orphanage->HaveTx(ptx->GetWitnessHash())); orphanage->EraseForPeer(node2); expected_total_count -= 1; - BOOST_CHECK_EQUAL(orphanage->Size(), expected_total_count); + BOOST_CHECK_EQUAL(orphanage->CountUniqueOrphans(), expected_total_count); BOOST_CHECK(!orphanage->HaveTx(ptx->GetWitnessHash())); } @@ -809,13 +809,13 @@ BOOST_AUTO_TEST_CASE(multiple_announcers) expected_total_count += 1; - BOOST_CHECK_EQUAL(orphanage->Size(), expected_total_count); + BOOST_CHECK_EQUAL(orphanage->CountUniqueOrphans(), expected_total_count); orphanage->EraseForBlock(block); expected_total_count -= 1; - BOOST_CHECK_EQUAL(orphanage->Size(), expected_total_count); + BOOST_CHECK_EQUAL(orphanage->CountUniqueOrphans(), expected_total_count); } } BOOST_AUTO_TEST_CASE(peer_worksets) @@ -863,7 +863,7 @@ BOOST_AUTO_TEST_CASE(peer_worksets) // Delete this tx, clearing the orphanage. BOOST_CHECK_EQUAL(orphanage->EraseTx(orphan_wtxid), 1); - BOOST_CHECK_EQUAL(orphanage->Size(), 0); + BOOST_CHECK_EQUAL(orphanage->CountUniqueOrphans(), 0); for (NodeId node = node0; node <= node2; ++node) { BOOST_CHECK_EQUAL(orphanage->GetTxToReconsider(node), nullptr); BOOST_CHECK(!orphanage->HaveTxFromPeer(orphan_wtxid, node));