40 std::vector<COutPoint> outpoints;
43 for (uint8_t i = 0; i < 4; i++) {
49 LIMITED_WHILE(outpoints.size() < 200
'000 && fuzzed_data_provider.ConsumeBool(), 10 * DEFAULT_MAX_ORPHAN_TRANSACTIONS) 51 // construct transaction 52 const CTransactionRef tx = [&] { 53 CMutableTransaction tx_mut; 54 const auto num_in = fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(1, outpoints.size()); 55 const auto num_out = fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(1, 256); 56 // pick outpoints from outpoints as input. We allow input duplicates on purpose, given we are not 57 // running any transaction validation logic before adding transactions to the orphanage 58 for (uint32_t i = 0; i < num_in; i++) { 59 auto& prevout = PickValue(fuzzed_data_provider, outpoints); 60 // try making transactions unique by setting a random nSequence, but allow duplicate transactions if they happen 61 tx_mut.vin.emplace_back(prevout, CScript{}, fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(0, CTxIn::SEQUENCE_FINAL)); 63 // output amount will not affect txorphanage 64 for (uint32_t i = 0; i < num_out; i++) { 65 tx_mut.vout.emplace_back(CAmount{0}, CScript{}); 67 auto new_tx = MakeTransactionRef(tx_mut); 68 // add newly constructed outpoints to the coin pool 69 for (uint32_t i = 0; i < num_out; i++) { 70 outpoints.emplace_back(new_tx->GetHash(), i); 75 // Trigger orphanage functions that are called using parents. ptx_potential_parent is a tx we constructed in a 76 // previous loop and potentially the parent of this tx. 77 if (ptx_potential_parent) { 78 // Set up future GetTxToReconsider call. 79 orphanage.AddChildrenToWorkSet(*ptx_potential_parent); 81 // Check that all txns returned from GetChildrenFrom* are indeed a direct child of this tx. 82 NodeId peer_id = fuzzed_data_provider.ConsumeIntegral<NodeId>(); 83 for (const auto& child : orphanage.GetChildrenFromSamePeer(ptx_potential_parent, peer_id)) { 84 assert(std::any_of(child->vin.cbegin(), child->vin.cend(), [&](const auto& input) { 85 return input.prevout.hash == ptx_potential_parent->GetHash(); 88 for (const auto& [child, peer] : orphanage.GetChildrenFromDifferentPeer(ptx_potential_parent, peer_id)) { 89 assert(std::any_of(child->vin.cbegin(), child->vin.cend(), [&](const auto& input) { 90 return input.prevout.hash == ptx_potential_parent->GetHash(); 92 assert(peer != peer_id); 96 // trigger orphanage functions 97 LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10 * DEFAULT_MAX_ORPHAN_TRANSACTIONS) 99 NodeId peer_id = fuzzed_data_provider.ConsumeIntegral<NodeId>(); 102 fuzzed_data_provider, 105 CTransactionRef ref = orphanage.GetTxToReconsider(peer_id); 107 Assert(orphanage.HaveTx(ref->GetWitnessHash())); 112 bool have_tx = orphanage.HaveTx(tx->GetWitnessHash()); 113 // AddTx should return false if tx is too big or already have it 114 // tx weight is unknown, we only check when tx is already in orphanage 116 bool add_tx = orphanage.AddTx(tx, peer_id); 117 // have_tx == true -> add_tx == false 118 Assert(!have_tx || !add_tx); 120 have_tx = orphanage.HaveTx(tx->GetWitnessHash()); 122 bool add_tx = orphanage.AddTx(tx, peer_id); 123 // if have_tx is still false, it must be too big 124 Assert(!have_tx == (GetTransactionWeight(*tx) > MAX_STANDARD_TX_WEIGHT)); 125 Assert(!have_tx || !add_tx); 129 bool have_tx = orphanage.HaveTx(tx->GetWitnessHash()); 130 // EraseTx should return 0 if m_orphans doesn't have the tx
134 have_tx = orphanage.
HaveTx(tx->GetWitnessHash());
137 Assert(!have_tx && !orphanage.
EraseTx(tx->GetWitnessHash()));
141 orphanage.EraseForPeer(peer_id);
146 auto limit = fuzzed_data_provider.ConsumeIntegral<
unsigned int>();
147 orphanage.LimitOrphans(limit, limit_orphans_rng);
148 Assert(orphanage.Size() <= limit);
153 if (!ptx_potential_parent || fuzzed_data_provider.ConsumeBool()) {
154 ptx_potential_parent = tx;
std::shared_ptr< const CTransaction > CTransactionRef
int EraseTx(const Wtxid &wtxid)
Erase an orphan by wtxid.
void initialize_orphanage()
A class to track orphan transactions (failed on TX_MISSING_INPUTS) Since we cannot distinguish orphan...
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
int64_t ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
static transaction_identifier FromUint256(const uint256 &id)
FUZZ_TARGET(txorphan,.init=initialize_orphanage)
bool HaveTx(const Wtxid &wtxid) const
Check if we already have an orphan transaction (by wtxid only)
#define Assert(val)
Identity function.
std::unique_ptr< T > MakeNoLogFileContext(const ChainType chain_type=ChainType::REGTEST, TestOpts opts={})
Make a test setup that has disk access to the debug.log file disabled.