Bitcoin Core 30.99.0
P2P Digital Currency
private_broadcast_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 2025-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
6#include <private_broadcast.h>
8#include <util/time.h>
9
10#include <boost/test/unit_test.hpp>
11
12BOOST_FIXTURE_TEST_SUITE(private_broadcast_tests, BasicTestingSetup)
13
14static CTransactionRef MakeDummyTx(uint32_t id, size_t num_witness)
15{
17 mtx.vin.resize(1);
18 mtx.vin[0].nSequence = id;
19 if (num_witness > 0) {
20 mtx.vin[0].scriptWitness = CScriptWitness{};
21 mtx.vin[0].scriptWitness.stack.resize(num_witness);
22 }
23 return MakeTransactionRef(mtx);
24}
25
27{
28 SetMockTime(Now<NodeSeconds>());
29
31 const NodeId recipient1{1};
32
33 // No transactions initially.
34 BOOST_CHECK(!pb.PickTxForSend(/*will_send_to_nodeid=*/recipient1).has_value());
35 BOOST_CHECK_EQUAL(pb.GetStale().size(), 0);
37
38 // Make a transaction and add it.
39 const auto tx1{MakeDummyTx(/*id=*/1, /*num_witness=*/0)};
40
41 BOOST_CHECK(pb.Add(tx1));
42 BOOST_CHECK(!pb.Add(tx1));
43
44 // Make another transaction with same txid, different wtxid and add it.
45 const auto tx2{MakeDummyTx(/*id=*/1, /*num_witness=*/1)};
46 BOOST_REQUIRE(tx1->GetHash() == tx2->GetHash());
47 BOOST_REQUIRE(tx1->GetWitnessHash() != tx2->GetWitnessHash());
48
49 BOOST_CHECK(pb.Add(tx2));
50
51 const auto tx_for_recipient1{pb.PickTxForSend(/*will_send_to_nodeid=*/recipient1).value()};
52 BOOST_CHECK(tx_for_recipient1 == tx1 || tx_for_recipient1 == tx2);
53
54 // A second pick must return the other transaction.
55 const NodeId recipient2{2};
56 const auto tx_for_recipient2{pb.PickTxForSend(/*will_send_to_nodeid=*/recipient2).value()};
57 BOOST_CHECK(tx_for_recipient2 == tx1 || tx_for_recipient2 == tx2);
58 BOOST_CHECK_NE(tx_for_recipient1, tx_for_recipient2);
59
60 const NodeId nonexistent_recipient{0};
61
62 // Confirm transactions <-> recipients mapping is correct.
63 BOOST_CHECK(!pb.GetTxForNode(nonexistent_recipient).has_value());
64 BOOST_CHECK_EQUAL(pb.GetTxForNode(recipient1).value(), tx_for_recipient1);
65 BOOST_CHECK_EQUAL(pb.GetTxForNode(recipient2).value(), tx_for_recipient2);
66
67 // Confirm none of the transactions' reception have been confirmed.
68 BOOST_CHECK(!pb.DidNodeConfirmReception(recipient1));
69 BOOST_CHECK(!pb.DidNodeConfirmReception(recipient2));
70 BOOST_CHECK(!pb.DidNodeConfirmReception(nonexistent_recipient));
71
72 BOOST_CHECK_EQUAL(pb.GetStale().size(), 2);
73
74 // Confirm reception by recipient1.
75 pb.NodeConfirmedReception(nonexistent_recipient); // Dummy call.
76 pb.NodeConfirmedReception(recipient1);
77
79 BOOST_CHECK(!pb.DidNodeConfirmReception(recipient2));
80
81 BOOST_CHECK_EQUAL(pb.GetStale().size(), 1);
82 BOOST_CHECK_EQUAL(pb.GetStale()[0], tx_for_recipient2);
83
84 SetMockTime(Now<NodeSeconds>() + 10h);
85
86 BOOST_CHECK_EQUAL(pb.GetStale().size(), 2);
87
88 BOOST_CHECK_EQUAL(pb.Remove(tx_for_recipient1).value(), 1);
89 BOOST_CHECK(!pb.Remove(tx_for_recipient1).has_value());
90 BOOST_CHECK_EQUAL(pb.Remove(tx_for_recipient2).value(), 0);
91 BOOST_CHECK(!pb.Remove(tx_for_recipient2).has_value());
92
93 BOOST_CHECK(!pb.PickTxForSend(/*will_send_to_nodeid=*/nonexistent_recipient).has_value());
94}
95
Store a list of transactions to be broadcast privately.
void NodeConfirmedReception(const NodeId &nodeid) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Mark that the node has confirmed reception of the transaction we sent it by responding with PONG to o...
bool HavePendingTransactions() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Check if there are transactions that need to be broadcast.
bool DidNodeConfirmReception(const NodeId &nodeid) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Check if the node has confirmed reception of the transaction.
std::optional< size_t > Remove(const CTransactionRef &tx) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Forget a transaction.
std::optional< CTransactionRef > PickTxForSend(const NodeId &will_send_to_nodeid) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Pick the transaction with the fewest send attempts, and confirmations, and oldest send/confirm times.
std::optional< CTransactionRef > GetTxForNode(const NodeId &nodeid) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Get the transaction that was picked for sending to a given node by PickTxForSend().
bool Add(const CTransactionRef &tx) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Add a transaction to the storage.
std::vector< CTransactionRef > GetStale() const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Get the transactions that have not been broadcast recently.
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
int64_t NodeId
Definition: net.h:103
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:17
#define BOOST_CHECK(expr)
Definition: object.cpp:16
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:404
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:403
static CTransactionRef MakeDummyTx(uint32_t id, size_t num_witness)
BOOST_AUTO_TEST_CASE(basic)
Basic testing setup.
Definition: setup_common.h:64
A mutable version of CTransaction.
Definition: transaction.h:358
std::vector< CTxIn > vin
Definition: transaction.h:359
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:44