Skip to content

Commit 03aaaed

Browse files
sipaglozow
authored andcommitted
[prep] Return the made-reconsiderable announcements in AddChildrenToWorkSet
This is preparation for the simulation fuzz test added in a later commit. Since AddChildrenToWorkSet consumes randomness, there is no way for the simulator to exactly predict its behavior. By returning the set of made-reconsiderable announcements instead, the simulator can instead test that it is *a* valid choice, and then apply it to its own data structures.
1 parent ea29c43 commit 03aaaed

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

src/node/txorphanage.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ class TxOrphanageImpl final : public TxOrphanage {
217217
void EraseForPeer(NodeId peer) override;
218218
void EraseForBlock(const CBlock& block) override;
219219
void LimitOrphans() override;
220-
void AddChildrenToWorkSet(const CTransaction& tx, FastRandomContext& rng) override;
220+
std::vector<std::pair<Wtxid, NodeId>> AddChildrenToWorkSet(const CTransaction& tx, FastRandomContext& rng) override;
221221
bool HaveTxToReconsider(NodeId peer) override;
222222
std::vector<CTransactionRef> GetChildrenFromSamePeer(const CTransactionRef& parent, NodeId nodeid) const override;
223223
size_t Size() const override { return m_unique_orphans; }
@@ -489,8 +489,9 @@ void TxOrphanageImpl::LimitOrphans()
489489
LogDebug(BCLog::TXPACKAGES, "orphanage overflow, removed %u tx (%u announcements)\n", original_unique_txns - remaining_unique_orphans, num_erased);
490490
}
491491

492-
void TxOrphanageImpl::AddChildrenToWorkSet(const CTransaction& tx, FastRandomContext& rng)
492+
std::vector<std::pair<Wtxid, NodeId>> TxOrphanageImpl::AddChildrenToWorkSet(const CTransaction& tx, FastRandomContext& rng)
493493
{
494+
std::vector<std::pair<Wtxid, NodeId>> ret;
494495
auto& index_by_wtxid = m_orphans.get<ByWtxid>();
495496
for (unsigned int i = 0; i < tx.vout.size(); i++) {
496497
const auto it_by_prev = m_outpoint_to_orphan_it.find(COutPoint(tx.GetHash(), i));
@@ -512,13 +513,17 @@ void TxOrphanageImpl::AddChildrenToWorkSet(const CTransaction& tx, FastRandomCon
512513

513514
// Mark this orphan as ready to be reconsidered.
514515
static constexpr auto mark_reconsidered_modifier = [](auto& ann) { ann.m_reconsider = true; };
515-
index_by_wtxid.modify(it, mark_reconsidered_modifier);
516+
if (!it->m_reconsider) {
517+
index_by_wtxid.modify(it, mark_reconsidered_modifier);
518+
ret.emplace_back(wtxid, it->m_announcer);
519+
}
516520

517521
LogDebug(BCLog::TXPACKAGES, "added %s (wtxid=%s) to peer %d workset\n",
518522
it->m_tx->GetHash().ToString(), it->m_tx->GetWitnessHash().ToString(), it->m_announcer);
519523
}
520524
}
521525
}
526+
return ret;
522527
}
523528

524529
bool TxOrphanageImpl::HaveTx(const Wtxid& wtxid) const

src/node/txorphanage.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ class TxOrphanage {
9292
virtual void LimitOrphans() = 0;
9393

9494
/** Add any orphans that list a particular tx as a parent into the from peer's work set */
95-
virtual void AddChildrenToWorkSet(const CTransaction& tx, FastRandomContext& rng) = 0;
95+
virtual std::vector<std::pair<Wtxid, NodeId>> AddChildrenToWorkSet(const CTransaction& tx, FastRandomContext& rng) = 0;
9696

9797
/** Does this peer have any work to do? */
9898
virtual bool HaveTxToReconsider(NodeId peer) = 0;

src/test/orphanage_tests.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ BOOST_AUTO_TEST_CASE(peer_dos_limits)
185185
orphanage->AddTx(children.at(2), peer);
186186

187187
// Make child0 ready to reconsider
188-
orphanage->AddChildrenToWorkSet(*parents.at(0), det_rand);
188+
const std::vector<std::pair<Wtxid, NodeId>> expected_set_c0{std::make_pair(children.at(0)->GetWitnessHash(), peer)};
189+
BOOST_CHECK(orphanage->AddChildrenToWorkSet(*parents.at(0), det_rand) == expected_set_c0);
189190
BOOST_CHECK(orphanage->HaveTxToReconsider(peer));
190191

191192
// Add 1 more orphan, causing the orphanage to be oversize. child1 is evicted.
@@ -204,9 +205,11 @@ BOOST_AUTO_TEST_CASE(peer_dos_limits)
204205
BOOST_CHECK(orphanage->HaveTx(children.at(3)->GetWitnessHash()));
205206
BOOST_CHECK(orphanage->HaveTx(children.at(4)->GetWitnessHash()));
206207

207-
// Eviction order is FIFO within the orphans that are ready to be reconsidered.
208-
orphanage->AddChildrenToWorkSet(*parents.at(4), det_rand);
209-
orphanage->AddChildrenToWorkSet(*parents.at(3), det_rand);
208+
// Eviction order is FIFO within the orphans that are read
209+
const std::vector<std::pair<Wtxid, NodeId>> expected_set_c4{std::make_pair(children.at(4)->GetWitnessHash(), peer)};
210+
BOOST_CHECK(orphanage->AddChildrenToWorkSet(*parents.at(4), det_rand) == expected_set_c4);
211+
const std::vector<std::pair<Wtxid, NodeId>> expected_set_c3{std::make_pair(children.at(3)->GetWitnessHash(), peer)};
212+
BOOST_CHECK(orphanage->AddChildrenToWorkSet(*parents.at(3), det_rand) == expected_set_c3);
210213

211214
// child5 is evicted immediately because it is the only non-reconsiderable orphan.
212215
orphanage->AddTx(children.at(5), peer);
@@ -857,7 +860,8 @@ BOOST_AUTO_TEST_CASE(peer_worksets)
857860
}
858861

859862
// Parent accepted: child is added to 1 of 3 worksets.
860-
orphanage->AddChildrenToWorkSet(*tx_missing_parent, det_rand);
863+
auto newly_reconsiderable = orphanage->AddChildrenToWorkSet(*tx_missing_parent, det_rand);
864+
BOOST_CHECK_EQUAL(newly_reconsiderable.size(), 1);
861865
int node0_reconsider = orphanage->HaveTxToReconsider(node0);
862866
int node1_reconsider = orphanage->HaveTxToReconsider(node1);
863867
int node2_reconsider = orphanage->HaveTxToReconsider(node2);

0 commit comments

Comments
 (0)