20#include <boost/test/unit_test.hpp>
26 std::vector<unsigned char> keydata;
27 keydata = rand_ctx.randbytes(32);
28 key.Set(keydata.data(), keydata.data() + keydata.size(),
true);
39 if (outpoints.empty()) {
42 for (
const auto& outpoint : outpoints) {
43 tx.
vin.emplace_back(outpoint);
47 tx.
vin[0].scriptWitness.stack.push_back({1});
60 tx.
vin[0].scriptWitness.stack.push_back({5});
62 assert(ptx->GetHash() == mutated_tx->GetHash());
66static bool EqualTxns(
const std::set<CTransactionRef>& set_txns,
const std::vector<CTransactionRef>& vec_txns)
68 if (vec_txns.size() != set_txns.size())
return false;
69 for (
const auto& tx : vec_txns) {
70 if (!set_txns.contains(tx))
return false;
80 static constexpr unsigned int NUM_TXNS_CREATED = 100;
81 static constexpr int64_t TX_SIZE{469};
82 static constexpr int64_t TOTAL_SIZE = NUM_TXNS_CREATED * TX_SIZE;
84 std::vector<CTransactionRef> txns;
85 txns.reserve(NUM_TXNS_CREATED);
87 for (
unsigned int i{0}; i < NUM_TXNS_CREATED; ++i) {
89 txns.emplace_back(ptx);
101 orphanage_low_ann->AddTx(txns.at(0), peer);
102 orphanage_low_mem->AddTx(txns.at(0), peer);
105 orphanage_low_ann->AddTx(txns.at(1), peer);
106 orphanage_low_mem->AddTx(txns.at(1), peer);
109 BOOST_CHECK(!orphanage_low_ann->HaveTx(txns.at(0)->GetWitnessHash()));
110 BOOST_CHECK(!orphanage_low_mem->HaveTx(txns.at(0)->GetWitnessHash()));
111 BOOST_CHECK(orphanage_low_ann->HaveTx(txns.at(1)->GetWitnessHash()));
112 BOOST_CHECK(orphanage_low_mem->HaveTx(txns.at(1)->GetWitnessHash()));
114 orphanage_low_ann->SanityCheck();
115 orphanage_low_mem->SanityCheck();
125 orphanage_low_ann->AddTx(txns.at(0), peer);
128 std::vector<COutPoint> outpoints_45;
129 for (
unsigned int j{0}; j < 45; ++j) {
133 orphanage_low_ann->AddTx(ptx, peer);
136 BOOST_CHECK(!orphanage_low_ann->HaveTx(txns.at(0)->GetWitnessHash()));
137 BOOST_CHECK(orphanage_low_ann->HaveTx(ptx->GetWitnessHash()));
139 orphanage_low_ann->SanityCheck();
145 std::vector<CTransactionRef> parents;
146 std::vector<CTransactionRef> children;
147 for (
unsigned int i{0}; i < 10; ++i) {
150 parents.emplace_back(parent);
151 children.emplace_back(child);
159 orphanage->AddTx(children.at(0), peer);
162 orphanage->AddTx(children.at(1), peer);
163 orphanage->AddTx(children.at(2), peer);
166 const std::vector<std::pair<Wtxid, NodeId>> expected_set_c0{std::make_pair(children.at(0)->GetWitnessHash(), peer)};
167 BOOST_CHECK(orphanage->AddChildrenToWorkSet(*parents.at(0), det_rand) == expected_set_c0);
171 orphanage->AddTx(children.at(3), peer);
172 BOOST_CHECK(orphanage->HaveTx(children.at(0)->GetWitnessHash()));
173 BOOST_CHECK(!orphanage->HaveTx(children.at(1)->GetWitnessHash()));
174 BOOST_CHECK(orphanage->HaveTx(children.at(2)->GetWitnessHash()));
175 BOOST_CHECK(orphanage->HaveTx(children.at(3)->GetWitnessHash()));
176 orphanage->SanityCheck();
179 orphanage->AddTx(children.at(4), peer);
180 BOOST_CHECK(orphanage->HaveTx(children.at(0)->GetWitnessHash()));
181 BOOST_CHECK(!orphanage->HaveTx(children.at(2)->GetWitnessHash()));
182 BOOST_CHECK(orphanage->HaveTx(children.at(3)->GetWitnessHash()));
183 BOOST_CHECK(orphanage->HaveTx(children.at(4)->GetWitnessHash()));
186 const std::vector<std::pair<Wtxid, NodeId>> expected_set_c4{std::make_pair(children.at(4)->GetWitnessHash(), peer)};
187 BOOST_CHECK(orphanage->AddChildrenToWorkSet(*parents.at(4), det_rand) == expected_set_c4);
188 const std::vector<std::pair<Wtxid, NodeId>> expected_set_c3{std::make_pair(children.at(3)->GetWitnessHash(), peer)};
189 BOOST_CHECK(orphanage->AddChildrenToWorkSet(*parents.at(3), det_rand) == expected_set_c3);
192 orphanage->AddTx(children.at(5), peer);
193 BOOST_CHECK(orphanage->HaveTx(children.at(0)->GetWitnessHash()));
194 BOOST_CHECK(orphanage->HaveTx(children.at(3)->GetWitnessHash()));
195 BOOST_CHECK(orphanage->HaveTx(children.at(4)->GetWitnessHash()));
196 BOOST_CHECK(!orphanage->HaveTx(children.at(5)->GetWitnessHash()));
200 orphanage->AddTx(children.at(6), peer);
201 BOOST_CHECK(!orphanage->HaveTx(children.at(0)->GetWitnessHash()));
202 BOOST_CHECK(orphanage->HaveTx(children.at(3)->GetWitnessHash()));
203 BOOST_CHECK(orphanage->HaveTx(children.at(4)->GetWitnessHash()));
204 BOOST_CHECK(orphanage->HaveTx(children.at(6)->GetWitnessHash()));
211 orphanage->SanityCheck();
220 unsigned int max_announcements = 60;
225 for (
unsigned int i{0}; i < max_announcements; ++i) {
226 orphanage->AddTx(txns.at(i), peer_dosy);
228 orphanage->SanityCheck();
229 BOOST_CHECK_EQUAL(orphanage->AnnouncementsFromPeer(peer_dosy), max_announcements);
235 unsigned int num_from_peer1 = 10;
236 for (
unsigned int i{0}; i < num_from_peer1; ++i) {
237 orphanage->AddTx(txns.at(max_announcements + i), peer1);
240 BOOST_CHECK(orphanage->AnnouncementsFromPeer(peer_dosy) > orphanage->MaxPeerLatencyScore());
243 BOOST_CHECK_EQUAL(orphanage->AnnouncementsFromPeer(peer_dosy), max_announcements - i - 1);
246 BOOST_CHECK(!orphanage->HaveTx(txns.at(i)->GetWitnessHash()));
250 for (
unsigned int i{num_from_peer1}; i < num_from_peer1 + 10; ++i) {
252 BOOST_CHECK(orphanage->HaveTxFromPeer(txns.at(i)->GetWitnessHash(), peer_dosy));
253 orphanage->AddTx(txns.at(i), peer2);
256 BOOST_CHECK_EQUAL(orphanage->AnnouncementsFromPeer(peer_dosy), max_announcements - i - 1);
258 BOOST_CHECK_EQUAL(orphanage->AnnouncementsFromPeer(peer2), i + 1 - num_from_peer1);
261 BOOST_CHECK(!orphanage->HaveTxFromPeer(txns.at(i)->GetWitnessHash(), peer_dosy));
262 BOOST_CHECK(orphanage->HaveTx(txns.at(i)->GetWitnessHash()));
266 const unsigned int max_per_peer{max_announcements / 6};
267 const unsigned int num_announcements{orphanage->CountAnnouncements()};
268 for (
NodeId peer{3}; peer < 6; ++peer) {
269 for (
unsigned int i{0}; i < max_per_peer; ++i) {
271 orphanage->AddTx(txns.at(peer * max_per_peer + i), peer);
275 for (
NodeId peer{0}; peer < 6; ++peer) {
278 orphanage->SanityCheck();
293 orphanage->AddTx(txns.at(0), 0);
300 orphanage->AddTx(txns.at(1), 1);
307 orphanage->AddTx(txns.at(2), 2);
314 orphanage->AddTx(txns.at(3), 2);
322 orphanage->EraseForPeer(2);
329 orphanage->EraseTx(txns.at(0)->GetWitnessHash());
335 orphanage->SanityCheck();
342 tx_large.
vin.resize(1);
354 for (
unsigned int i = 0; i < 20; i++) {
355 orphanage->AddTx(txns.at(i), i < 10 ? peer_normal : peer_large);
357 BOOST_CHECK(orphanage->TotalLatencyScore() <= orphanage->MaxGlobalLatencyScore());
358 BOOST_CHECK(orphanage->TotalOrphanUsage() <= orphanage->MaxGlobalUsage());
361 orphanage->AddTx(ptx_large, peer_large);
366 BOOST_CHECK(orphanage->HaveTxFromPeer(ptx_large->GetWitnessHash(), peer_large));
369 orphanage->SanityCheck();
377 std::vector<COutPoint> outpoints_9;
378 for (
unsigned int j{0}; j < 9; ++j) {
381 for (
unsigned int i{0}; i < 10; ++i) {
383 orphanage->AddTx(ptx, 0);
389 std::vector<COutPoint> outpoints_50;
390 for (
unsigned int j{0}; j < 50; ++j) {
394 for (
unsigned int i{0}; i < 10; ++i) {
396 std::shuffle(outpoints_50.begin(), outpoints_50.end(), m_rng);
414 orphanage->SanityCheck();
434 auto now{GetTime<std::chrono::seconds>()};
437 std::vector<CTransactionRef> orphans_added;
440 for (
int i = 0; i < 50; i++)
444 tx.
vin[0].prevout.n = 0;
452 orphanage->AddTx(ptx, i);
453 orphans_added.emplace_back(ptx);
457 for (
int i = 0; i < 50; i++)
459 const auto& txPrev = orphans_added[m_rng.randrange(orphans_added.size())];
463 tx.
vin[0].prevout.n = 0;
464 tx.
vin[0].prevout.hash = txPrev->GetHash();
472 orphanage->AddTx(ptx, i);
473 orphans_added.emplace_back(ptx);
477 for (
int i = 0; i < 10; i++)
479 const auto& txPrev = orphans_added[m_rng.randrange(orphans_added.size())];
486 for (
unsigned int j = 0; j < tx.
vin.size(); j++)
488 tx.
vin[j].prevout.n = j;
489 tx.
vin[j].prevout.hash = txPrev->GetHash();
495 for (
unsigned int j = 1; j < tx.
vin.size(); j++)
496 tx.
vin[j].scriptSig = tx.
vin[0].scriptSig;
501 size_t expected_num_orphans = orphanage->CountUniqueOrphans();
504 orphanage->EraseForPeer(-1);
509 for (
NodeId i = 0; i < 3; i++)
511 orphanage->EraseForPeer(i);
512 expected_num_orphans -= 2;
523 std::vector<COutPoint> empty_outpoints;
530 const auto& normal_wtxid = child_normal->GetWitnessHash();
531 const auto& mutated_wtxid = child_mutated->GetWitnessHash();
538 BOOST_CHECK(orphanage->GetTx(normal_wtxid) == child_normal);
540 BOOST_CHECK(orphanage->GetTx(mutated_wtxid) ==
nullptr);
543 BOOST_CHECK(orphanage->AddTx(child_mutated, peer));
548 std::set<CTransactionRef> expected_children{child_normal, child_mutated};
565 std::vector<COutPoint> empty_outpoints;
571 while (parent1->GetHash() == parent2->GetHash()) {
593 BOOST_CHECK(orphanage->AddTx(child_p1n0_p1n1, node1));
594 BOOST_CHECK(orphanage->AddTx(child_p1n0_p2n0, node1));
597 BOOST_CHECK(!orphanage->AddTx(child_p1n0_p1n1, node0));
602 std::vector<CTransactionRef> expected_parent1_children{child_p1n0_p2n0, child_p1n0_p1n1, child_p1n0};
603 std::vector<CTransactionRef> expected_parent2_children{child_p1n0_p2n0, child_p2n1};
605 BOOST_CHECK(expected_parent1_children == orphanage->GetChildrenFromSamePeer(parent1, node1));
606 BOOST_CHECK(expected_parent2_children == orphanage->GetChildrenFromSamePeer(parent2, node1));
609 BOOST_CHECK(orphanage->GetChildrenFromSamePeer(parent1, node2).empty());
610 BOOST_CHECK(orphanage->GetChildrenFromSamePeer(parent2, node2).empty());
613 BOOST_CHECK(orphanage->GetChildrenFromSamePeer(child_p1n0_p2n0, node1).empty());
614 BOOST_CHECK(orphanage->GetChildrenFromSamePeer(child_p1n0_p2n0, node2).empty());
622 BOOST_CHECK(orphanage->AddTx(child_p1n0_p1n1, node2));
623 BOOST_CHECK(orphanage->AddTx(child_p1n0_p2n0, node2));
634 std::set<CTransactionRef> expected_parent1_node1{child_p1n0};
637 BOOST_CHECK(orphanage->HaveTxFromPeer(child_p1n0->GetWitnessHash(), node1));
638 BOOST_CHECK(
EqualTxns(expected_parent1_node1, orphanage->GetChildrenFromSamePeer(parent1, node1)));
643 std::set<CTransactionRef> expected_parent2_node1{child_p2n1};
645 BOOST_CHECK(
EqualTxns(expected_parent2_node1, orphanage->GetChildrenFromSamePeer(parent2, node1)));
650 std::vector<CTransactionRef> expected_parent1_node2{child_p1n0_p2n0, child_p1n0_p1n1};
651 BOOST_CHECK(orphanage->HaveTxFromPeer(child_p1n0_p1n1->GetWitnessHash(), node2));
652 BOOST_CHECK(orphanage->HaveTxFromPeer(child_p1n0_p2n0->GetWitnessHash(), node2));
653 BOOST_CHECK(expected_parent1_node2 == orphanage->GetChildrenFromSamePeer(parent1, node2));
658 std::set<CTransactionRef> expected_parent2_node2{child_p1n0_p2n0};
661 BOOST_CHECK(orphanage->HaveTxFromPeer(child_p1n0_p2n0->GetWitnessHash(), node2));
662 BOOST_CHECK(
EqualTxns(expected_parent2_node2, orphanage->GetChildrenFromSamePeer(parent2, node2)));
690 std::vector<COutPoint> outpoints;
691 const uint32_t num_outpoints{6};
692 outpoints.reserve(num_outpoints);
693 for (uint32_t i{0}; i < num_outpoints; ++i) {
706 block.
vtx.emplace_back(bo_tx_same_txid);
710 block.
vtx.emplace_back(b_tx_same_txid_diff_witness);
712 auto o_tx_same_txid_diff_witness =
MakeMutation(b_tx_same_txid_diff_witness);
717 block.
vtx.emplace_back(b_tx_conflict);
724 block.
vtx.emplace_back(b_tx_conflict_partial);
729 orphanage->EraseForBlock(block);
730 for (
const auto& expected_removed : {bo_tx_same_txid, o_tx_same_txid_diff_witness, o_tx_conflict, o_tx_conflict_partial_2}) {
731 const auto& expected_removed_wtxid = expected_removed->GetWitnessHash();
732 BOOST_CHECK(!orphanage->HaveTx(expected_removed_wtxid));
736 BOOST_CHECK(orphanage->HaveTx(control_tx->GetWitnessHash()));
744 size_t expected_total_count{0};
752 const auto& wtxid = ptx->GetWitnessHash();
755 expected_total_count += 1;
765 BOOST_CHECK(orphanage->HaveTx(ptx_mutated->GetWitnessHash()));
766 expected_total_count += 1;
771 BOOST_CHECK(orphanage->AddAnnouncer(ptx->GetWitnessHash(), node2));
775 BOOST_CHECK(orphanage->HaveTxFromPeer(ptx->GetWitnessHash(), node2));
776 BOOST_CHECK(!orphanage->AddAnnouncer(ptx->GetWitnessHash(), node2));
784 orphanage->EraseForPeer(node0);
785 BOOST_CHECK(orphanage->HaveTx(ptx->GetWitnessHash()));
786 BOOST_CHECK(!orphanage->HaveTxFromPeer(ptx->GetWitnessHash(), node0));
788 BOOST_CHECK(!orphanage->HaveTx(ptx_mutated->GetWitnessHash()));
789 expected_total_count -= 1;
793 orphanage->EraseForPeer(node1);
795 BOOST_CHECK(orphanage->HaveTx(ptx->GetWitnessHash()));
796 orphanage->EraseForPeer(node2);
797 expected_total_count -= 1;
799 BOOST_CHECK(!orphanage->HaveTx(ptx->GetWitnessHash()));
806 block.
vtx.emplace_back(tx_block);
810 expected_total_count += 1;
814 orphanage->EraseForBlock(block);
816 expected_total_count -= 1;
832 const auto& orphan_wtxid = tx_orphan->GetWitnessHash();
837 BOOST_CHECK(orphanage->AddAnnouncer(orphan_wtxid, node2));
843 auto newly_reconsiderable = orphanage->AddChildrenToWorkSet(*tx_missing_parent, det_rand);
845 int node0_reconsider = orphanage->HaveTxToReconsider(node0);
846 int node1_reconsider = orphanage->HaveTxToReconsider(node1);
847 int node2_reconsider = orphanage->HaveTxToReconsider(node2);
851 if (node0_reconsider) {
852 assigned_peer = node0;
853 }
else if (node1_reconsider) {
854 assigned_peer = node1;
857 assigned_peer = node2;
861 orphanage->EraseForPeer(assigned_peer);
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
std::vector< CTransactionRef > vtx
An encapsulated private key.
CPubKey GetPubKey() const
Compute the public key from a private key.
An outpoint - a combination of a transaction hash and an index n into its vout.
The basic transaction that is broadcasted on the network and contained in blocks.
Fillable signing provider that keeps keys in an address->secret map.
virtual bool AddKey(const CKey &key)
uint256 rand256() noexcept
generate a random uint256.
static transaction_identifier FromUint256(const uint256 &id)
static int32_t GetTransactionWeight(const CTransaction &tx)
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
std::unique_ptr< TxOrphanage > MakeTxOrphanage() noexcept
Create a new TxOrphanage instance.
static constexpr unsigned int DEFAULT_MAX_ORPHANAGE_LATENCY_SCORE
Default value for TxOrphanage::m_max_global_latency_score.
static constexpr int64_t DEFAULT_RESERVED_ORPHAN_WEIGHT_PER_PEER
Default value for TxOrphanage::m_reserved_usage_per_peer.
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
BOOST_AUTO_TEST_CASE(peer_dos_limits)
static CTransactionRef MakeMutation(const CTransactionRef &ptx)
static void MakeNewKeyWithFastRandomContext(CKey &key, FastRandomContext &rand_ctx)
static CTransactionRef MakeTransactionSpending(const std::vector< COutPoint > &outpoints, FastRandomContext &det_rand)
static bool EqualTxns(const std::set< CTransactionRef > &set_txns, const std::vector< CTransactionRef > &vec_txns)
static constexpr int32_t MAX_STANDARD_TX_WEIGHT
The maximum weight for transactions we're willing to relay/mine.
static CTransactionRef MakeTransactionRef(Tx &&txIn)
std::shared_ptr< const CTransaction > CTransactionRef
static constexpr CAmount CENT
A mutable version of CTransaction.
std::vector< CTxOut > vout
Testing setup that configures a complete environment.
void BulkTransaction(CMutableTransaction &tx, int32_t target_weight)
bool SignSignature(const SigningProvider &provider, const CScript &fromPubKey, CMutableTransaction &txTo, unsigned int nIn, const CAmount &amount, int nHashType, SignatureData &sig_data)
Produce a satisfying script (scriptSig or witness).
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.