Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
251// were just evicted in the previous block additions.252for (unsigned int i{num_from_peer1}; i < num_from_peer1 + 10; ++i) {253// Tx has already been sent by peer_dosy254BOOST_CHECK(orphanage->HaveTxFromPeer(txns.at(i)->GetWitnessHash(), peer_dosy));255orphanage->AddTx(txns.at(i), peer2);257// peer_dosy is still the only one getting evicted258BOOST_CHECK_EQUAL(orphanage->AnnouncementsFromPeer(peer_dosy), max_announcements - i - 1);259BOOST_CHECK_EQUAL(orphanage->AnnouncementsFromPeer(peer1), num_from_peer1);260BOOST_CHECK_EQUAL(orphanage->AnnouncementsFromPeer(peer2), i + 1 - num_from_peer1);262// Evictions are FIFO within a peer, so the ith transaction sent by peer_dosy is the one that was evicted.263BOOST_CHECK(!orphanage->HaveTxFromPeer(txns.at(i)->GetWitnessHash(), peer_dosy));264BOOST_CHECK(orphanage->HaveTx(txns.at(i)->GetWitnessHash()));265}267// With 6 peers, each can add 10, and still only peer_dosy's orphans are evicted.268const unsigned int max_per_peer{max_announcements / 6};269const unsigned int num_announcements{orphanage->CountAnnouncements()};270for (NodeId peer{3}; peer < 6; ++peer) {271for (unsigned int i{0}; i < max_per_peer; ++i) {272// Each addition causes 1 eviction.273orphanage->AddTx(txns.at(peer * max_per_peer + i), peer);274BOOST_CHECK_EQUAL(orphanage->CountAnnouncements(), num_announcements);275}276}277for (NodeId peer{0}; peer < 6; ++peer) {278BOOST_CHECK_EQUAL(orphanage->AnnouncementsFromPeer(peer), max_per_peer);279}280orphanage->SanityCheck();281}283// Limits change as more peers are added.284{285auto orphanage{node::MakeTxOrphanage()};286// These stay the same regardless of number of peers287BOOST_CHECK_EQUAL(orphanage->MaxGlobalLatencyScore(), node::DEFAULT_MAX_ORPHANAGE_LATENCY_SCORE);288BOOST_CHECK_EQUAL(orphanage->ReservedPeerUsage(), node::DEFAULT_RESERVED_ORPHAN_WEIGHT_PER_PEER);290// These change with number of peers291BOOST_CHECK_EQUAL(orphanage->MaxGlobalUsage(), node::DEFAULT_RESERVED_ORPHAN_WEIGHT_PER_PEER);292BOOST_CHECK_EQUAL(orphanage->MaxPeerLatencyScore(), node::DEFAULT_MAX_ORPHANAGE_LATENCY_SCORE);294// Number of peers = 1295orphanage->AddTx(txns.at(0), 0);296BOOST_CHECK_EQUAL(orphanage->MaxGlobalLatencyScore(), node::DEFAULT_MAX_ORPHANAGE_LATENCY_SCORE);297BOOST_CHECK_EQUAL(orphanage->ReservedPeerUsage(), node::DEFAULT_RESERVED_ORPHAN_WEIGHT_PER_PEER);298BOOST_CHECK_EQUAL(orphanage->MaxGlobalUsage(), node::DEFAULT_RESERVED_ORPHAN_WEIGHT_PER_PEER);299BOOST_CHECK_EQUAL(orphanage->MaxPeerLatencyScore(), node::DEFAULT_MAX_ORPHANAGE_LATENCY_SCORE);301// Number of peers = 2302orphanage->AddTx(txns.at(1), 1);303BOOST_CHECK_EQUAL(orphanage->MaxGlobalLatencyScore(), node::DEFAULT_MAX_ORPHANAGE_LATENCY_SCORE);304BOOST_CHECK_EQUAL(orphanage->ReservedPeerUsage(), node::DEFAULT_RESERVED_ORPHAN_WEIGHT_PER_PEER);305BOOST_CHECK_EQUAL(orphanage->MaxGlobalUsage(), node::DEFAULT_RESERVED_ORPHAN_WEIGHT_PER_PEER * 2);306BOOST_CHECK_EQUAL(orphanage->MaxPeerLatencyScore(), node::DEFAULT_MAX_ORPHANAGE_LATENCY_SCORE / 2);308// Number of peers = 3309orphanage->AddTx(txns.at(2), 2);310BOOST_CHECK_EQUAL(orphanage->MaxGlobalLatencyScore(), node::DEFAULT_MAX_ORPHANAGE_LATENCY_SCORE);311BOOST_CHECK_EQUAL(orphanage->ReservedPeerUsage(), node::DEFAULT_RESERVED_ORPHAN_WEIGHT_PER_PEER);312BOOST_CHECK_EQUAL(orphanage->MaxGlobalUsage(), node::DEFAULT_RESERVED_ORPHAN_WEIGHT_PER_PEER * 3);313BOOST_CHECK_EQUAL(orphanage->MaxPeerLatencyScore(), node::DEFAULT_MAX_ORPHANAGE_LATENCY_SCORE / 3);315// Number of peers didn't change.316orphanage->AddTx(txns.at(3), 2);317BOOST_CHECK_EQUAL(orphanage->MaxGlobalLatencyScore(), node::DEFAULT_MAX_ORPHANAGE_LATENCY_SCORE);318BOOST_CHECK_EQUAL(orphanage->ReservedPeerUsage(), node::DEFAULT_RESERVED_ORPHAN_WEIGHT_PER_PEER);319BOOST_CHECK_EQUAL(orphanage->MaxGlobalUsage(), node::DEFAULT_RESERVED_ORPHAN_WEIGHT_PER_PEER * 3);320BOOST_CHECK_EQUAL(orphanage->MaxPeerLatencyScore(), node::DEFAULT_MAX_ORPHANAGE_LATENCY_SCORE / 3);322// Once a peer has no orphans, it is not considered in the limits.323// Number of peers = 2324orphanage->EraseForPeer(2);325BOOST_CHECK_EQUAL(orphanage->MaxGlobalLatencyScore(), node::DEFAULT_MAX_ORPHANAGE_LATENCY_SCORE);326BOOST_CHECK_EQUAL(orphanage->ReservedPeerUsage(), node::DEFAULT_RESERVED_ORPHAN_WEIGHT_PER_PEER);327BOOST_CHECK_EQUAL(orphanage->MaxGlobalUsage(), node::DEFAULT_RESERVED_ORPHAN_WEIGHT_PER_PEER * 2);328BOOST_CHECK_EQUAL(orphanage->MaxPeerLatencyScore(), node::DEFAULT_MAX_ORPHANAGE_LATENCY_SCORE / 2);330// Number of peers = 1331orphanage->EraseTx(txns.at(0)->GetWitnessHash());332BOOST_CHECK_EQUAL(orphanage->MaxGlobalLatencyScore(), node::DEFAULT_MAX_ORPHANAGE_LATENCY_SCORE);333BOOST_CHECK_EQUAL(orphanage->ReservedPeerUsage(), node::DEFAULT_RESERVED_ORPHAN_WEIGHT_PER_PEER);334BOOST_CHECK_EQUAL(orphanage->MaxGlobalUsage(), node::DEFAULT_RESERVED_ORPHAN_WEIGHT_PER_PEER);335BOOST_CHECK_EQUAL(orphanage->MaxPeerLatencyScore(), node::DEFAULT_MAX_ORPHANAGE_LATENCY_SCORE);337orphanage->SanityCheck();338}340// Test eviction of multiple transactions at a time341{342// Create a large transaction that is 10 times larger than the normal size transaction.343CMutableTransaction tx_large;344tx_large.vin.resize(1);345BulkTransaction(tx_large, 10 * TX_SIZE);346auto ptx_large = MakeTransactionRef(tx_large);348const auto large_tx_size = GetTransactionWeight(*ptx_large);349BOOST_CHECK(large_tx_size > 10 * TX_SIZE);350BOOST_CHECK(large_tx_size < 11 * TX_SIZE);352auto orphanage = node::MakeTxOrphanage(20, large_tx_size);353// One peer sends 10 normal size transactions. The other peer sends 10 normal transactions and 1 very large one354NodeId peer_normal{0};355NodeId peer_large{1};356for (unsigned int i = 0; i < 20; i++) {357orphanage->AddTx(txns.at(i), i < 10 ? peer_normal : peer_large);358}359BOOST_CHECK(orphanage->TotalLatencyScore() <= orphanage->MaxGlobalLatencyScore());360BOOST_CHECK(orphanage->TotalOrphanUsage() <= orphanage->MaxGlobalUsage());362// Add the large transaction. This should cause evictions of all the previous 10 transactions from that peer.363orphanage->AddTx(ptx_large, peer_large);365// peer_normal should still have 10 transactions, and peer_large should have 1.366BOOST_CHECK_EQUAL(orphanage->AnnouncementsFromPeer(peer_normal), 10);367BOOST_CHECK_EQUAL(orphanage->AnnouncementsFromPeer(peer_large), 1);368BOOST_CHECK(orphanage->HaveTxFromPeer(ptx_large->GetWitnessHash(), peer_large));369BOOST_CHECK_EQUAL(orphanage->CountAnnouncements(), 11);371orphanage->SanityCheck();372}374// Test that latency score includes number of inputs.375{376auto orphanage = node::MakeTxOrphanage();378// Add 10 transactions with 9 inputs each.379std::vector<COutPoint> outpoints_9;380for (unsigned int j{0}; j < 9; ++j) {381outpoints_9.emplace_back(Txid::FromUint256(m_rng.rand256()), j);382}383for (unsigned int i{0}; i < 10; ++i) {384auto ptx = MakeTransactionSpending(outpoints_9, m_rng);385orphanage->AddTx(ptx, 0);386}387BOOST_CHECK_EQUAL(orphanage->CountAnnouncements(), 10);388BOOST_CHECK_EQUAL(orphanage->TotalLatencyScore(), 10);390// Add 10 transactions with 50 inputs each.391std::vector<COutPoint> outpoints_50;392for (unsigned int j{0}; j < 50; ++j) {393outpoints_50.emplace_back(Txid::FromUint256(m_rng.rand256()), j);394}396for (unsigned int i{0}; i < 10; ++i) {397CMutableTransaction tx;398std::shuffle(outpoints_50.begin(), outpoints_50.end(), m_rng);399auto ptx = MakeTransactionSpending(outpoints_50, m_rng);400BOOST_CHECK(orphanage->AddTx(ptx, 0));401if (i < 5) BOOST_CHECK(!orphanage->AddTx(ptx, 1));402}403// 10 of the 9-input transactions + 10 of the 50-input transactions + 5 more announcements of the 50-input transactions404BOOST_CHECK_EQUAL(orphanage->CountAnnouncements(), 25);405// Base of 25 announcements, plus 10 * 5 for the 50-input transactions (counted just once)406BOOST_CHECK_EQUAL(orphanage->TotalLatencyScore(), 25 + 50);408// Peer 0 sent all 20 transactions409BOOST_CHECK_EQUAL(orphanage->AnnouncementsFromPeer(0), 20);410BOOST_CHECK_EQUAL(orphanage->LatencyScoreFromPeer(0), 20 + 10 * 5);412// Peer 1 sent 5 of the 10 transactions with many inputs413BOOST_CHECK_EQUAL(orphanage->AnnouncementsFromPeer(1), 5);414BOOST_CHECK_EQUAL(orphanage->LatencyScoreFromPeer(1), 5 + 5 * 5);416orphanage->SanityCheck();417}418}419BOOST_AUTO_TEST_CASE(DoS_mapOrphans)420{421// This test had non-deterministic coverage due to422// randomly selected seeds.423// This seed is chosen so that all branches of the function424// ecdsa_signature_parse_der_lax are executed during this test.425// Specifically branches that run only when an ECDSA426// signature's R and S values have leading zeros.427m_rng.Reseed(uint256{33});429std::unique_ptr<node::TxOrphanage> orphanage{node::MakeTxOrphanage()};430CKey key;431MakeNewKeyWithFastRandomContext(key, m_rng);432FillableSigningProvider keystore;433BOOST_CHECK(keystore.AddKey(key));435FakeNodeClock clock{};437std::vector<CTransactionRef> orphans_added;439// 50 orphan transactions:440for (int i = 0; i < 50; i++)441{442CMutableTransaction tx;443tx.vin.resize(1);444tx.vin[0].prevout.n = 0;445tx.vin[0].prevout.hash = Txid::FromUint256(m_rng.rand256());446tx.vin[0].scriptSig << OP_1;447tx.vout.resize(1);448tx.vout[0].nValue = i*CENT;449tx.vout[0].scriptPubKey = GetScriptForDestination(PKHash(key.GetPubKey()));451auto ptx = MakeTransactionRef(tx);452orphanage->AddTx(ptx, i);453orphans_added.emplace_back(ptx);454}456// ... and 50 that depend on other orphans:457for (int i = 0; i < 50; i++)458{459const auto& txPrev = orphans_added[m_rng.randrange(orphans_added.size())];461CMutableTransaction tx;462tx.vin.resize(1);463tx.vin[0].prevout.n = 0;464tx.vin[0].prevout.hash = txPrev->GetHash();465tx.vout.resize(1);466tx.vout[0].nValue = i*CENT;467tx.vout[0].scriptPubKey = GetScriptForDestination(PKHash(key.GetPubKey()));468SignatureData empty;469BOOST_CHECK(SignSignature(keystore, *txPrev, tx, 0, SIGHASH_ALL, empty));471auto ptx = MakeTransactionRef(tx);472orphanage->AddTx(ptx, i);473orphans_added.emplace_back(ptx);474}476// This really-big orphan should be ignored:477for (int i = 0; i < 10; i++)478{479const auto& txPrev = orphans_added[m_rng.randrange(orphans_added.size())];481CMutableTransaction tx;482tx.vout.resize(1);483tx.vout[0].nValue = 1*CENT;484tx.vout[0].scriptPubKey = GetScriptForDestination(PKHash(key.GetPubKey()));485tx.vin.resize(2777);486for (unsigned int j = 0; j < tx.vin.size(); j++)487{488tx.vin[j].prevout.n = j;489tx.vin[j].prevout.hash = txPrev->GetHash();490}491SignatureData empty;492BOOST_CHECK(SignSignature(keystore, *txPrev, tx, 0, SIGHASH_ALL, empty));493// Reuse same signature for other inputs494// (they don't have to be valid for this test)495for (unsigned int j = 1; j < tx.vin.size(); j++)496tx.vin[j].scriptSig = tx.vin[0].scriptSig;498BOOST_CHECK(!orphanage->AddTx(MakeTransactionRef(tx), i));499}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.