Merge bitcoin/bitcoin#36277: net: always complete all initial private broadcast connections
What changed, and why it matters
This Bitcoin Core patch fixes a privacy weakness in how new transactions are first announced to the network. Previously, if the node saw its own transaction come back from another peer quickly, it would cancel the remaining private broadcast connections. An attacker who can race the transaction back to the node could reduce how many independent peers receive it, making it easier to guess which peer originally created the transaction. The patch now always completes all three initial private broadcast sends, even after the transaction is seen again, and only stops further retries. It also tracks disconnects more carefully so failed sends are replaced rather than silently dropped.
Reviewers and operators should treat this as a privacy-hardening fix for the private transaction broadcast feature. Nodes using -privatebroadcast should upgrade to ensure the intended 3-peer initial broadcast is enforced. No immediate emergency response is warranted because the attack is theoretical and requires winning a network race, but the fix should be included in the next maintenance release.
Security signals we found
Fixes a theoretical timing attack against transaction-origin privacy in private transaction broadcast
Ensures all 3 initial private broadcast connections complete before stopping, preventing premature cancellation
Replaces Remove() with MarkResolved() for mempool-conflict / received-back cases to avoid aborting in-flight sends
Adds explicit disconnect tracking and TryGrantRetry() to prevent stale retry loops and compensate for failed sends
Adds new functional test p2p_private_broadcast_completion.py demonstrating the fixed behavior
Evidence from the diff
The change refactors PrivateBroadcast so that ‘receiving the transaction back’ (MarkResolved) is decoupled from ‘stopping all sends’. A new resolved flag prevents stale retry attempts, but IsPending() still allows the initially planned three sends to complete. Remove() now returns remaining planned sends instead of confirmed count, and NumToOpenSub cancels only unstarted connections. NodeDisconnected() records disconnects, and TryGrantRetry() explicitly grants one extra attempt for unresolved transactions whose planned sends are exhausted but under the cap. net_processing uses fSuccessfullyConnected as a proxy for ‘did we send an INV’ to open a replacement connection when a private-broadcast peer disconnects before the handshake completes. Fuzz and unit tests are updated, and a new functional test verifies all three initial sends complete even when the transaction is returned early.
Changed components
src/net_processing.cppsrc/private_broadcast.cppsrc/private_broadcast.hsrc/test/fuzz/private_broadcast.cppsrc/test/private_broadcast_tests.cpptest/functional/p2p_private_broadcast_completion.pytest/functional/test_runner.pyInspect captured patch +303 / −99
### src/net_processing.cpp
@@ -203,8 +203,6 @@ static constexpr double MAX_ADDR_RATE_PER_SECOND{0.1};
* based increments won't go above this, but the MAX_ADDR_TO_SEND increment following GETADDR
* is exempt from this limit). */
static constexpr size_t MAX_ADDR_PROCESSING_TOKEN_BUCKET{MAX_ADDR_TO_SEND};
-/** For private broadcast, send a transaction to this many peers. */
-static constexpr size_t NUM_PRIVATE_BROADCAST_PER_TX{3};
/** Private broadcast connections must complete within this time. Disconnect the peer if it takes longer. */
static constexpr auto PRIVATE_BROADCAST_MAX_CONNECTION_LIFETIME{3min};
@@ -1746,7 +1744,7 @@ void PeerManagerImpl::ReattemptInitialBroadcast(CScheduler& scheduler)
void PeerManagerImpl::ReattemptPrivateBroadcast(CScheduler& scheduler)
{
- // Remove stale transactions that are no longer relevant (e.g. already in
+ // Resolve stale transactions that are no longer relevant (e.g. already in
// the mempool or mined) and count the remaining ones.
size_t num_for_rebroadcast{0};
const auto stale_txs = m_tx_for_private_broadcast.GetStale();
@@ -1756,6 +1754,7 @@ void PeerManagerImpl::ReattemptPrivateBroadcast(CScheduler& scheduler)
LOCK(cs_main);
auto mempool_acceptable = m_chainman.ProcessTransaction(stale_tx, /*test_accept=*/true);
if (mempool_acceptable.m_result_type == MempoolAcceptResult::ResultType::VALID) {
+ if (!m_tx_for_private_broadcast.TryGrantRetry(stale_tx)) continue;
LogDebug(BCLog::PRIVBROADCAST,
"Reattempting broadcast of stale txid=%s wtxid=%s",
stale_tx->GetHash().ToString(), stale_tx->GetWitnessHash().ToString());
@@ -1764,7 +1763,7 @@ void PeerManagerImpl::ReattemptPrivateBroadcast(CScheduler& scheduler)
LogDebug(BCLog::PRIVBROADCAST, "Giving up broadcast attempts for txid=%s wtxid=%s: %s",
stale_tx->GetHash().ToString(), stale_tx->GetWitnessHash().ToString(),
mempool_acceptable.m_state.ToString());
- m_tx_for_private_broadcast.Remove(stale_tx);
+ m_tx_for_private_broadcast.MarkResolved(stale_tx);
}
}
@@ -1844,11 +1843,18 @@ void PeerManagerImpl::FinalizeNode(const CNode& node)
LOCK(m_headers_presync_mutex);
m_headers_presync_stats.erase(nodeid);
}
- if (node.IsPrivateBroadcastConn() &&
- !m_tx_for_private_broadcast.DidNodeConfirmReception(nodeid) &&
- m_tx_for_private_broadcast.HavePendingTransactions()) {
-
- m_connman.m_private_broadcast.NumToOpenAdd(1);
+ if (node.IsPrivateBroadcastConn()) {
+ m_tx_for_private_broadcast.NodeDisconnected(nodeid);
+ // We consider a transaction sent to a peer if we sent them an INV.
+ // Normally they should request the transaction with GETDATA, but this may
+ // not happen if they are already aware of the transaction.
+ // If we didn't send them an INV, schedule a new connection to compensate
+ // for this send failure. Whether we sent them an INV is not readily
+ // available here, so use fSuccessfullyConnected because INV is sent right
+ // after a successful handshake when there is a pending transaction.
+ if (!node.fSuccessfullyConnected && m_tx_for_private_broadcast.HavePendingTransactions()) {
+ m_connman.m_private_broadcast.NumToOpenAdd(1);
+ }
}
LogDebug(BCLog::NET, "Cleared nodestate for peer=%d\n", nodeid);
}
@@ -1990,11 +1996,9 @@ std::vector<CTransactionRef> PeerManagerImpl::AbortPrivateBroadcast(const uint25
for (const auto& tx_info : snapshot) {
const CTransactionRef& tx{tx_info.tx};
if (tx->GetHash().ToUint256() != id && tx->GetWitnessHash().ToUint256() != id) continue;
- if (const auto peer_acks{m_tx_for_private_broadcast.Remove(tx)}) {
+ if (const auto remaining_sends{m_tx_for_private_broadcast.Remove(tx)}) {
removed_txs.push_back(tx);
- if (NUM_PRIVATE_BROADCAST_PER_TX > *peer_acks) {
- connections_cancelled += (NUM_PRIVATE_BROADCAST_PER_TX - *peer_acks);
- }
+ connections_cancelled += *remaining_sends;
}
}
m_connman.m_private_broadcast.NumToOpenSub(connections_cancelled);
@@ -2497,8 +2501,8 @@ node::TransactionError PeerManagerImpl::InitiateTxBroadcastPrivate(const CTransa
const auto txstr{strprintf("txid=%s, wtxid=%s", tx->GetHash().ToString(), tx->GetWitnessHash().ToString())};
switch (m_tx_for_private_broadcast.Add(tx)) {
case PrivateBroadcast::AddResult::Added:
- LogDebug(BCLog::PRIVBROADCAST, "Requesting %d new connections due to %s", NUM_PRIVATE_BROADCAST_PER_TX, txstr);
- m_connman.m_private_broadcast.NumToOpenAdd(NUM_PRIVATE_BROADCAST_PER_TX);
+ LogDebug(BCLog::PRIVBROADCAST, "Requesting %d new connections due to %s", PrivateBroadcast::INITIAL_CONNECTION_COUNT, txstr);
+ m_connman.m_private_broadcast.NumToOpenAdd(PrivateBroadcast::INITIAL_CONNECTION_COUNT);
return node::TransactionError::OK;
case PrivateBroadcast::AddResult::AlreadyPresent:
LogDebug(BCLog::PRIVBROADCAST, "Ignoring unnecessary request to schedule an already scheduled transaction: %s", txstr);
@@ -4723,15 +4727,10 @@ void PeerManagerImpl::ProcessMessage(Peer& peer, CNode& pfrom, const std::string
const uint256& hash = peer.m_wtxid_relay ? wtxid.ToUint256() : txid.ToUint256();
AddKnownTx(peer, hash);
- if (const auto num_broadcasted{m_tx_for_private_broadcast.Remove(ptx)}) {
+ if (m_tx_for_private_broadcast.MarkResolved(ptx)) {
LogDebug(BCLog::PRIVBROADCAST, "Received our privately broadcast transaction (txid=%s) from the "
- "network from %s; stopping private broadcast attempts",
+ "network from %s",
txid.ToString(), pfrom.LogPeer());
- if (NUM_PRIVATE_BROADCAST_PER_TX > num_broadcasted.value()) {
- // Not all of the initial NUM_PRIVATE_BROADCAST_PER_TX connections were needed.
- // Tell CConnman it does not need to start the remaining ones.
- m_connman.m_private_broadcast.NumToOpenSub(NUM_PRIVATE_BROADCAST_PER_TX - num_broadcasted.value());
- }
}
LOCK2(cs_main, m_tx_download_mutex);
### src/private_broadcast.cpp
@@ -14,12 +14,21 @@ PrivateBroadcast::AddResult PrivateBroadcast::Add(const CTransactionRef& tx)
EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
{
LOCK(m_mutex);
+ // Cleanup finished transactions
+ std::erase_if(m_transactions, [this](const auto& entry) {
+ const auto& state{entry.second};
+ return state.resolved && !IsPending(state) &&
+ std::ranges::all_of(state.send_statuses, [](const auto& status) { return status.disconnected; });
+ });
+
if (const auto it{m_transactions.find(tx)}; it != m_transactions.end()) {
- if (IsPending(it->second)) return AddResult::AlreadyPresent;
+ if (it->second.send_statuses.size() < m_max_send_attempts) return AddResult::AlreadyPresent;
- // An exhausted transaction can be explicitly retried by adding it again.
+ // A transaction that has reached m_max_send_attempts can be explicitly retried by adding it again.
it->second.time_added = NodeClock::now();
it->second.send_statuses.clear();
+ it->second.planned_sends = INITIAL_CONNECTION_COUNT;
+ it->second.resolved = false;
return AddResult::Added;
}
@@ -35,12 +44,43 @@ std::optional<size_t> PrivateBroadcast::Remove(const CTransactionRef& tx)
LOCK(m_mutex);
const auto handle{m_transactions.extract(tx)};
if (handle) {
- const auto p{DerivePriority(handle.mapped().send_statuses)};
- return p.num_confirmed;
+ const auto& state{handle.mapped()};
+ const size_t planned{std::min(state.planned_sends, m_max_send_attempts)};
+ const size_t actual{state.send_statuses.size()};
+ return planned > actual ? planned - actual : 0;
}
return std::nullopt;
}
+bool PrivateBroadcast::MarkResolved(const CTransactionRef& tx)
+{
+ LOCK(m_mutex);
+ const auto it{m_transactions.find(tx)};
+ if (it == m_transactions.end()) return false;
+ it->second.resolved = true;
+ return true;
+}
+
+void PrivateBroadcast::NodeDisconnected(NodeId nodeid)
+{
+ LOCK(m_mutex);
+ if (const auto entry{GetSendStatusByNode(nodeid)}) {
+ entry->send_status.disconnected = true;
+ }
+}
+
+bool PrivateBroadcast::TryGrantRetry(const CTransactionRef& tx)
+{
+ LOCK(m_mutex);
+ const auto it{m_transactions.find(tx)};
+ if (it == m_transactions.end() ||
+ it->second.resolved ||
+ IsPending(it->second) ||
+ it->second.planned_sends >= m_max_send_attempts) return false;
+ ++it->second.planned_sends;
+ return true;
+}
+
std::optional<CTransactionRef> PrivateBroadcast::PickTxForSend(const NodeId& will_send_to_nodeid, const CService& will_send_to_address)
EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
{
@@ -87,17 +127,6 @@ void PrivateBroadcast::NodeConfirmedReception(const NodeId& nodeid)
}
}
-bool PrivateBroadcast::DidNodeConfirmReception(const NodeId& nodeid)
- EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
-{
- LOCK(m_mutex);
- const auto tx_and_status{GetSendStatusByNode(nodeid)};
- if (tx_and_status.has_value()) {
- return tx_and_status.value().send_status.confirmed.has_value();
- }
- return false;
-}
-
bool PrivateBroadcast::HavePendingTransactions()
EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
{
@@ -112,7 +141,7 @@ std::vector<CTransactionRef> PrivateBroadcast::GetStale() const
const auto now{NodeClock::now()};
std::vector<CTransactionRef> stale;
for (const auto& [tx, state] : m_transactions) {
- if (!IsPending(state)) continue;
+ if (state.resolved || state.planned_sends >= m_max_send_attempts) continue;
const Priority p{DerivePriority(state.send_statuses)};
if (p.num_confirmed == 0) {
if (state.time_added < now - INITIAL_STALE_DURATION) stale.push_back(tx);
@@ -131,6 +160,7 @@ std::vector<PrivateBroadcast::TxBroadcastInfo> PrivateBroadcast::GetBroadcastInf
entries.reserve(m_transactions.size());
for (const auto& [tx, state] : m_transactions) {
+ if (state.resolved) continue;
std::vector<PeerSendInfo> peers;
peers.reserve(state.send_statuses.size());
for (const auto& status : state.send_statuses) {
@@ -145,7 +175,9 @@ std::vector<PrivateBroadcast::TxBroadcastInfo> PrivateBroadcast::GetBroadcastInf
bool PrivateBroadcast::IsPending(const TxSendStatus& status) const
{
- return status.send_statuses.size() < m_max_send_attempts;
+ // Deliberately ignore resolved so all initially scheduled connections can complete.
+ const size_t limit{std::min(status.planned_sends, m_max_send_attempts)};
+ return status.send_statuses.size() < limit;
}
PrivateBroadcast::Priority PrivateBroadcast::DerivePriority(const std::vector<SendStatus>& sent_to)
### src/private_broadcast.h
@@ -20,16 +20,21 @@
* Store a list of transactions to be broadcast privately. Supports the following operations:
* - Add a new transaction
* - Remove a transaction
+ * - Mark a transaction as resolved, so no more retry send attempts are granted
+ * - Mark a connection finished
+ * - Grant an additional send attempt for an unresolved transaction
* - Pick a transaction for sending to one recipient
* - Query which transaction has been picked for sending to a given recipient node
* - Mark that a given recipient node has confirmed receipt of a transaction
- * - Query whether a given recipient node has confirmed reception
* - Query whether any transactions that need sending are currently on the list
*/
class PrivateBroadcast
{
public:
+ /// Number of connections to make for initial broadcast.
+ static constexpr size_t INITIAL_CONNECTION_COUNT{3};
+
/// If a transaction is not sent to any peer for this duration,
/// then we consider it stale / for rebroadcasting.
static constexpr auto INITIAL_STALE_DURATION{5min};
@@ -93,13 +98,39 @@ class PrivateBroadcast
/**
* Forget a transaction.
* @param[in] tx Transaction to forget.
- * @retval !nullopt The number of times the transaction was sent and confirmed
- * by the recipient (if the transaction existed and was removed).
+ * @retval !nullopt The number of planned send attempts not yet picked
+ * (if the transaction existed and was removed).
* @retval nullopt The transaction was not in the storage.
*/
std::optional<size_t> Remove(const CTransactionRef& tx)
EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
+ /**
+ * Mark a transaction as resolved because it was received back from the network
+ * or is no longer acceptable to the mempool.
+ * @param[in] tx Transaction to resolve.
+ * @return Whether the transaction was found in the storage.
+ */
+ bool MarkResolved(const CTransactionRef& tx)
+ EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
+
+ /**
+ * Mark a connection finished.
+ * @param[in] nodeid Node whose connection has ended.
+ */
+ void NodeDisconnected(NodeId nodeid)
+ EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
+
+ /**
+ * Grant one additional send attempt if the transaction is unresolved, all
+ * planned sends have been picked, and the attempt limit has not been reached.
+ * The caller schedules the connection separately.
+ * @param[in] tx Transaction to retry.
+ * @return Whether an additional send attempt was granted.
+ */
+ bool TryGrantRetry(const CTransactionRef& tx)
+ EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
+
/**
* Pick the transaction with the fewest send attempts, and confirmations,
* and oldest send/confirm times.
@@ -131,14 +162,6 @@ class PrivateBroadcast
void NodeConfirmedReception(const NodeId& nodeid)
EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
- /**
- * Check if the node has confirmed reception of the transaction.
- * @retval true Node has confirmed, `NodeConfirmedReception()` has been called.
- * @retval false Node has not confirmed, `NodeConfirmedReception()` has not been called.
- */
- bool DidNodeConfirmReception(const NodeId& nodeid)
- EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
-
/**
* Check if there are transactions with send attempts remaining.
*/
@@ -169,6 +192,8 @@ class PrivateBroadcast
const NodeClock::time_point picked;
/// When was the transaction reception confirmed by the node (by PONG).
std::optional<NodeClock::time_point> confirmed;
+ /// Whether the node has disconnected.
+ bool disconnected{false};
SendStatus(const NodeId& nodeid, const CService& address, const NodeClock::time_point& picked) : nodeid{nodeid}, address{address}, picked{picked} {}
};
@@ -228,6 +253,10 @@ class PrivateBroadcast
struct TxSendStatus {
NodeClock::time_point time_added{NodeClock::now()};
std::vector<SendStatus> send_statuses;
+ /// Total number of sends granted, including initial count.
+ size_t planned_sends{INITIAL_CONNECTION_COUNT};
+ /// Whether the transaction no longer needs to be retried.
+ bool resolved{false};
};
bool IsPending(const TxSendStatus& status) const;
/// Cap on the number of simultaneously tracked transactions (see Add()).
### src/test/fuzz/private_broadcast.cpp
@@ -44,22 +44,22 @@ FUZZ_TARGET(private_broadcast)
const size_t max_send_attempts{fdp.ConsumeIntegralInRange<size_t>(1, 12)};
PrivateBroadcast pb{cap, max_send_attempts};
- // Random transaction that the test generated and passed to Add(). Trimmed when Remove() is called.
+ // Random transactions that the test generated and passed to Add(). Trimmed by Remove() and Add() cleanup.
// The values are the number of times a transaction was picked for sending.
std::unordered_map<CTransactionRef, size_t, CTransactionRefHash, CTransactionRefComp> transactions;
+ // The values are the number of times a transaction is planned to be sent.
+ std::unordered_map<CTransactionRef, size_t, CTransactionRefHash, CTransactionRefComp> planned_sends;
+ std::unordered_set<CTransactionRef, CTransactionRefHash, CTransactionRefComp> resolved_transactions;
// Transactions passed to PickTxForSend(), indexed by node id. Trimmed when
- // Remove() is called or a transaction is reset by Add().
+ // Remove() is called or a transaction is reset or cleaned up by Add().
std::unordered_map<NodeId, CTransactionRef> nodes_sent_to;
-
- // A subset of `nodes_sent_to`, node ids passed to NodeConfirmedReception().
- // Trimmed when Remove() is called or a transaction is reset by Add().
- std::unordered_set<NodeId> nodes_that_confirmed_reception;
+ std::unordered_set<NodeId> disconnected_nodes;
NodeId next_nodeid{0}; // Generate unique node ids.
- const auto is_pending{[max_send_attempts](const auto& entry) {
- return entry.second < max_send_attempts;
+ const auto is_pending{[&](const auto& entry) {
+ return entry.second < std::min(planned_sends.at(entry.first), max_send_attempts);
}};
const auto ExistentOrNewNodeId = [&next_nodeid, &fdp](){
@@ -80,19 +80,32 @@ FUZZ_TARGET(private_broadcast)
tx = PickIterator(fdp, transactions)->first;
}
+ auto cleanup_candidates{resolved_transactions};
+ for (const auto& [nodeid, sent_tx] : nodes_sent_to) {
+ if (!disconnected_nodes.contains(nodeid)) cleanup_candidates.erase(sent_tx);
+ }
+ std::erase_if(transactions, [&](const auto& entry) {
+ if (!cleanup_candidates.contains(entry.first) || is_pending(entry)) return false;
+ planned_sends.erase(entry.first);
+ resolved_transactions.erase(entry.first);
+ return true;
+ });
+ std::erase_if(nodes_sent_to, [&](const auto& entry) { return !transactions.contains(entry.second); });
+
const bool present_before{transactions.contains(tx)};
const auto res{pb.Add(tx)};
if (present_before) {
auto tx_it{transactions.find(tx)};
Assert(tx_it != transactions.end());
- if (is_pending(*tx_it)) {
+ if (tx_it->second < max_send_attempts) {
Assert(res == PrivateBroadcast::AddResult::AlreadyPresent);
} else {
Assert(res == PrivateBroadcast::AddResult::Added);
tx_it->second = 0;
+ planned_sends[tx] = PrivateBroadcast::INITIAL_CONNECTION_COUNT;
+ resolved_transactions.erase(tx);
for (auto it = nodes_sent_to.begin(); it != nodes_sent_to.end();) {
if (CTransactionRefComp{}(it->second, tx)) {
- nodes_that_confirmed_reception.erase(it->first);
it = nodes_sent_to.erase(it);
} else {
++it;
@@ -104,6 +117,7 @@ FUZZ_TARGET(private_broadcast)
} else {
Assert(res == PrivateBroadcast::AddResult::Added);
transactions.emplace(tx, 0);
+ planned_sends.emplace(tx, PrivateBroadcast::INITIAL_CONNECTION_COUNT);
}
},
[&] { // Remove()
@@ -113,28 +127,31 @@ FUZZ_TARGET(private_broadcast)
const auto transactions_it{PickIterator(fdp, transactions)};
const CTransactionRef& tx{transactions_it->first};
- size_t num_nodes_that_confirmed_tx{0};
-
- // Remove relevant entries from nodes_sent_to[] and nodes_that_confirmed_reception[] if any.
+ // Remove relevant entries from nodes_sent_to[] if any.
for (auto it = nodes_sent_to.begin(); it != nodes_sent_to.end();) {
- const NodeId nodeid{it->first};
if (CTransactionRefComp{}(it->second, tx)) {
it = nodes_sent_to.erase(it);
- if (nodes_that_confirmed_reception.erase(nodeid) > 0) {
- ++num_nodes_that_confirmed_tx;
- }
} else {
++it;
}
}
- const auto opt_num_confirmed{pb.Remove(tx)};
+ const auto remaining_sends{pb.Remove(tx)};
- Assert(opt_num_confirmed.has_value());
- Assert(opt_num_confirmed.value() == num_nodes_that_confirmed_tx);
+ Assert(remaining_sends.has_value());
+ Assert(remaining_sends.value() == std::min(planned_sends.at(tx), max_send_attempts) - transactions_it->second);
Assert(!pb.Remove(tx).has_value());
+ planned_sends.erase(tx);
+ resolved_transactions.erase(tx);
transactions.erase(transactions_it);
},
+ [&] { // MarkResolved()
+ const CTransactionRef tx{transactions.empty() || fdp.ConsumeBool() ?
+ MakeTransactionRef(ConsumeTransaction(fdp, std::nullopt)) :
+ PickIterator(fdp, transactions)->first};
+ Assert(pb.MarkResolved(tx) == transactions.contains(tx));
+ if (transactions.contains(tx)) resolved_transactions.insert(tx);
+ },
[&] { // PickTxForSend()
// Only give pristine node ids to PickTxForSend() as required.
const NodeId will_send_to_nodeid{next_nodeid++};
@@ -180,24 +197,11 @@ FUZZ_TARGET(private_broadcast)
const NodeId nodeid{ExistentOrNewNodeId()};
pb.NodeConfirmedReception(nodeid);
-
- if (nodes_sent_to.contains(nodeid)) {
- // nodeid was previously passed to PickTxForSend(), so NodeConfirmedReception()
- // must have changed the internal state. Remember this to later check that
- // DidNodeConfirmReception() works correctly.
- nodes_that_confirmed_reception.emplace(nodeid);
- }
},
- [&] { // DidNodeConfirmReception()
+ [&] { // NodeDisconnected()
const NodeId nodeid{ExistentOrNewNodeId()};
-
- const bool confirmed{pb.DidNodeConfirmReception(nodeid)};
-
- if (nodes_that_confirmed_reception.contains(nodeid)) {
- Assert(confirmed);
- } else {
- Assert(!confirmed);
- }
+ pb.NodeDisconnected(nodeid);
+ if (nodes_sent_to.contains(nodeid)) disconnected_nodes.insert(nodeid);
},
[&] { // HavePendingTransactions()
if (std::ranges::any_of(transactions, is_pending)) {
@@ -206,6 +210,14 @@ FUZZ_TARGET(private_broadcast)
Assert(!pb.HavePendingTransactions());
}
},
+ [&] { // TryGrantRetry()
+ if (transactions.empty()) return;
+ const auto tx_it{PickIterator(fdp, transactions)};
+ auto& count{planned_sends.at(tx_it->first)};
+ const bool can_retry{!resolved_transactions.contains(tx_it->first) && !is_pending(*tx_it) && count < max_send_attempts};
+ Assert(pb.TryGrantRetry(tx_it->first) == can_retry);
+ if (can_retry) ++count;
+ },
[&] { // GetStale()
const auto stale{pb.GetStale()};
@@ -214,17 +226,19 @@ FUZZ_TARGET(private_broadcast)
for (const auto& stale_tx : stale) {
const auto it{transactions.find(stale_tx)};
Assert(it != transactions.end());
- Assert(is_pending(*it));
+ Assert(!resolved_transactions.contains(stale_tx));
+ Assert(planned_sends.at(stale_tx) < max_send_attempts);
}
},
[&] { // GetBroadcastInfo()
const auto all_broadcast_info{pb.GetBroadcastInfo()};
- Assert(all_broadcast_info.size() == transactions.size());
+ Assert(all_broadcast_info.size() == transactions.size() - resolved_transactions.size());
for (const auto& info : all_broadcast_info) {
const auto it{transactions.find(info.tx)};
Assert(it != transactions.end());
+ Assert(!resolved_transactions.contains(info.tx));
Assert(info.peers.size() == it->second); // exactly the sends we recorded
Assert(info.attempts_remaining == max_send_attempts - it->second);
}
### src/test/private_broadcast_tests.cpp
@@ -98,9 +98,9 @@ BOOST_AUTO_TEST_CASE(basic)
BOOST_CHECK_EQUAL(pb.GetTxForNode(recipient2).value(), tx_for_recipient2);
// Confirm none of the transactions' reception have been confirmed.
- BOOST_CHECK(!pb.DidNodeConfirmReception(recipient1));
- BOOST_CHECK(!pb.DidNodeConfirmReception(recipient2));
- BOOST_CHECK(!pb.DidNodeConfirmReception(nonexistent_recipient));
+ for (const auto& info : pb.GetBroadcastInfo()) {
+ BOOST_CHECK(std::ranges::none_of(info.peers, [](const auto& peer) { return peer.received.has_value(); }));
+ }
// 1. Freshly added transactions should NOT be stale yet.
BOOST_CHECK_EQUAL(pb.GetStale().size(), 0);
@@ -115,9 +115,6 @@ BOOST_AUTO_TEST_CASE(basic)
pb.NodeConfirmedReception(nonexistent_recipient); // Dummy call.
pb.NodeConfirmedReception(recipient1);
- BOOST_CHECK(pb.DidNodeConfirmReception(recipient1));
- BOOST_CHECK(!pb.DidNodeConfirmReception(recipient2));
-
const auto infos{pb.GetBroadcastInfo()};
BOOST_CHECK_EQUAL(infos.size(), 2);
{
@@ -141,16 +138,43 @@ BOOST_AUTO_TEST_CASE(basic)
BOOST_CHECK_EQUAL(pb.GetStale().size(), 2);
- BOOST_CHECK_EQUAL(pb.Remove(tx_for_recipient1).value(), 1);
+ BOOST_CHECK_EQUAL(pb.Remove(tx_for_recipient1).value(), 2);
BOOST_CHECK(!pb.Remove(tx_for_recipient1).has_value());
- BOOST_CHECK_EQUAL(pb.Remove(tx_for_recipient2).value(), 0);
+ BOOST_CHECK_EQUAL(pb.Remove(tx_for_recipient2).value(), 2);
BOOST_CHECK(!pb.Remove(tx_for_recipient2).has_value());
BOOST_CHECK_EQUAL(pb.GetBroadcastInfo().size(), 0);
const CService addr_nonexistent{ipv4Addr, 3333};
BOOST_CHECK(!pb.PickTxForSend(/*will_send_to_nodeid=*/nonexistent_recipient, /*will_send_to_address=*/addr_nonexistent).has_value());
}
+BOOST_AUTO_TEST_CASE(complete_all_initial_sends)
+{
+ PrivateBroadcast pb;
+ const auto tx{MakeDummyTx(/*id=*/1, /*num_witness=*/0)};
+ BOOST_REQUIRE_EQUAL(pb.Add(tx), PrivateBroadcast::AddResult::Added);
+
+ in_addr ipv4_addr;
+ ipv4_addr.s_addr = 0xa0b0c001;
+ const CService address{ipv4_addr, 1111};
+
+ // Pick the first send, then receive the transaction back from the network.
+ BOOST_REQUIRE_EQUAL(pb.PickTxForSend(/*will_send_to_nodeid=*/1, address).value(), tx);
+ BOOST_CHECK_EQUAL(pb.Add(tx), PrivateBroadcast::AddResult::AlreadyPresent);
+ BOOST_REQUIRE(pb.MarkResolved(tx));
+ BOOST_CHECK_EQUAL(pb.Add(tx), PrivateBroadcast::AddResult::AlreadyPresent);
+ BOOST_REQUIRE_EQUAL(pb.GetTxForNode(1).value(), tx);
+ BOOST_CHECK(pb.GetBroadcastInfo().empty());
+
+ // Receipt must not prevent either of the two remaining initial sends.
+ BOOST_REQUIRE_EQUAL(pb.PickTxForSend(/*will_send_to_nodeid=*/2, address).value(), tx);
+ BOOST_REQUIRE_EQUAL(pb.PickTxForSend(/*will_send_to_nodeid=*/3, address).value(), tx);
+
+ BOOST_CHECK_EQUAL(pb.Add(tx), PrivateBroadcast::AddResult::AlreadyPresent);
+ pb.NodeDisconnected(/*nodeid=*/1);
+ BOOST_CHECK(!pb.PickTxForSend(/*will_send_to_nodeid=*/4, address).has_value());
+}
+
BOOST_AUTO_TEST_CASE(stale_unpicked_tx)
{
FakeNodeClock clock{};
@@ -184,13 +208,17 @@ BOOST_AUTO_TEST_CASE(send_attempt_limit)
NodeId node_id{0};
for (size_t attempt{0}; attempt < max_attempts; ++attempt) {
+ if (attempt >= PrivateBroadcast::INITIAL_CONNECTION_COUNT) {
+ BOOST_REQUIRE(pb.TryGrantRetry(tx));
+ }
BOOST_CHECK(pb.HavePendingTransactions());
BOOST_REQUIRE_EQUAL(pb.PickTxForSend(/*will_send_to_nodeid=*/node_id++, address).value(), tx);
}
// The transaction and its complete send history remain available, but no
// further connections should be opened for it.
BOOST_CHECK(!pb.HavePendingTransactions());
+ BOOST_CHECK(!pb.TryGrantRetry(tx));
BOOST_CHECK(!pb.PickTxForSend(/*will_send_to_nodeid=*/node_id++, address).has_value());
const auto info{pb.GetBroadcastInfo()};
BOOST_REQUIRE_EQUAL(info.size(), 1);
@@ -233,20 +261,22 @@ BOOST_AUTO_TEST_CASE(reset_with_equivalent_transaction_reference)
BOOST_REQUIRE_EQUAL(pb.Add(tx), PrivateBroadcast::AddResult::Added);
BOOST_REQUIRE_EQUAL(pb.PickTxForSend(/*will_send_to_nodeid=*/0, address).value(), tx);
pb.NodeConfirmedReception(/*nodeid=*/0);
- BOOST_CHECK(pb.DidNodeConfirmReception(/*nodeid=*/0));
+ const auto confirmed_info{pb.GetBroadcastInfo()};
+ BOOST_REQUIRE_EQUAL(confirmed_info.size(), 1);
+ BOOST_REQUIRE_EQUAL(confirmed_info[0].peers.size(), 1);
+ BOOST_CHECK(confirmed_info[0].peers[0].received.has_value());
BOOST_CHECK(!pb.HavePendingTransactions());
// A distinct CTransactionRef with the same WTXID must reset the exhausted
// transaction, including its send and confirmation history.
BOOST_REQUIRE_EQUAL(pb.Add(equivalent_tx), PrivateBroadcast::AddResult::Added);
BOOST_CHECK(pb.HavePendingTransactions());
BOOST_CHECK(!pb.GetTxForNode(/*nodeid=*/0).has_value());
- BOOST_CHECK(!pb.DidNodeConfirmReception(/*nodeid=*/0));
const auto info{pb.GetBroadcastInfo()};
BOOST_REQUIRE_EQUAL(info.size(), 1);
BOOST_CHECK(info[0].tx->GetWitnessHash() == tx->GetWitnessHash());
BOOST_CHECK(info[0].peers.empty());
- BOOST_CHECK_EQUAL(pb.Remove(equivalent_tx).value(), 0);
+ BOOST_CHECK_EQUAL(pb.Remove(equivalent_tx).value(), 1);
}
BOOST_AUTO_TEST_CASE(rejection_at_cap)
### test/functional/p2p_private_broadcast_completion.py
@@ -0,0 +1,99 @@
+#!/usr/bin/env python3
+# Copyright (c) The Bitcoin Core developers
+# Distributed under the MIT software license, see the accompanying
+# file COPYING or http://www.opensource.org/licenses/mit-license.php.
+"""Test that all three initial private sends complete even after receiving the transaction back."""
+
+from test_framework.messages import CAddress, msg_tx
+from test_framework.p2p import P2PInterface, P2P_SERVICES, p2p_lock, start_p2p_listener
+from test_framework.socks5 import Socks5Server, start_socks5_server
+from test_framework.test_framework import BitcoinTestFramework
+from test_framework.util import assert_equal
+from test_framework.wallet import MiniWallet
+
+
+class DelayedPrivateBroadcastPeer(P2PInterface):
+ def __init__(self, private_peers):
+ super().__init__()
+ self.private_peers = private_peers
+
+ def on_version(self, message):
+ # Private broadcast connections have zero services. Hold them at verack.
+ if message.nServices == 0:
+ self.private_peers.append(self)
+ else:
+ super().on_version(message)
+
+
+class PrivateBroadcastCompletionTest(BitcoinTestFramework):
+ def set_test_params(self):
+ self.num_nodes = 1
+ self.disable_autoconnect = False
+
+ def setup_nodes(self):
+ self.private_peers = [] # Guarded by p2p_lock
+
+ def destinations_factory(_requested_to_addr, _requested_to_port, _proxy_client):
+ peer = DelayedPrivateBroadcastPeer(self.private_peers)
+ peer.peer_connect_helper(dstaddr="0.0.0.0", dstport=0, net=self.chain, timeout_factor=self.options.timeout_factor)
+ peer.peer_connect_send_version(services=P2P_SERVICES)
+ addr, port = start_p2p_listener(self.network_thread, peer)
+ return {"actual_to_addr": addr, "actual_to_port": port}
+
+ self.proxy = start_socks5_server(destinations_factory)
+ self.extra_args = [[
+ "-privatebroadcast",
+ f"-proxy={self.proxy.conf.addr[0]}:{self.proxy.conf.addr[1]}",
+ ]]
+ super().setup_nodes()
+
+ def run_test(self):
+ node = self.nodes[0]
+ returner = node.add_p2p_connection(P2PInterface())
+ self.fill_node_addrman(node_index=0, address_types_to_add=[CAddress.NET_TORV3])
+ wallet = MiniWallet(node)
+ tx = wallet.create_self_transfer()
+ node.sendrawtransaction(tx["hex"])
+
+ def three_private_peers():
+ with p2p_lock:
+ return len(self.private_peers) == 3
+ self.wait_until(three_private_peers)
+ with p2p_lock:
+ peers = list(self.private_peers)
+ self.private_peers.clear()
+
+ self.log.info("Ensure each peer receives the transaction before disconnecting")
+ for i, peer in enumerate(peers):
+ P2PInterface.on_version(peer, peer.last_message["version"])
+ peer.wait_for_disconnect()
+ assert_equal(peer.last_message["tx"].tx.txid_hex, tx["txid"])
+ if i == 0:
+ self.log.info("Return the transaction while the other two peers still wait on handshake")
+ returner.send_and_ping(msg_tx(tx["tx"]))
+
+ self.log.info("Receive another transaction while the proxy is unavailable")
+ self.proxy.stop()
+ self.proxy.s.close()
+ next_tx = wallet.create_self_transfer()
+ with node.assert_debug_log(["will retry to a different address"], timeout=10):
+ node.sendrawtransaction(next_tx["hex"])
+ returner.send_and_ping(msg_tx(next_tx["tx"]))
+
+ self.log.info("Restoring the proxy opens all three connections for the received transaction")
+ self.proxy = Socks5Server(self.proxy.conf)
+ self.proxy.start()
+ self.wait_until(three_private_peers)
+ with p2p_lock:
+ peers = list(self.private_peers)
+ for peer in peers:
+ P2PInterface.on_version(peer, peer.last_message["version"])
+ peer.wait_for_disconnect()
+ assert_equal(peer.last_message["tx"].tx.txid_hex, next_tx["txid"])
+
+ self.stop_node(0)
+ self.proxy.stop()
+
+
+if __name__ == "__main__":
+ PrivateBroadcastCompletionTest(__file__).main()
### test/functional/test_runner.py
@@ -323,6 +323,7 @@
'feature_minchainwork.py',
'rpc_estimatefee.py',
'p2p_private_broadcast.py',
+ 'p2p_private_broadcast_completion.py',
'p2p_private_broadcast_cap.py',
'rpc_getblockstats.py',
'feature_port.py',Why this scored 39/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.