29constexpr size_t NUM_COINS{50};
49std::vector<CTransactionRef> TRANSACTIONS;
52constexpr int NUM_PEERS = 16;
55std::chrono::microseconds TIME_SKIPS[128];
61 for (
const auto& outpoint : outpoints) {
62 tx.
vin.emplace_back(outpoint);
65 tx.
vin[0].scriptWitness.stack.push_back({1});
72 std::vector<COutPoint>
ret;
82 static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
83 g_setup = testing_setup.get();
84 for (uint32_t i = 0; i < uint32_t{NUM_COINS}; ++i) {
87 size_t outpoints_index = 0;
92 Assert(tx1->GetHash() == tx2->GetHash());
93 TRANSACTIONS.emplace_back(tx1);
94 TRANSACTIONS.emplace_back(tx2);
100 TRANSACTIONS.emplace_back(tx_parent_1);
102 TRANSACTIONS.emplace_back(tx_parent_2);
109 TRANSACTIONS.emplace_back(tx_parent);
117 COutPoint& last_outpoint = COINS[outpoints_index++];
118 for (
auto i{0}; i < 5; ++i) {
120 TRANSACTIONS.emplace_back(tx);
126 COutPoint& last_outpoint = COINS[outpoints_index++];
127 for (
auto i{0}; i < 5; ++i) {
129 TRANSACTIONS.emplace_back(tx);
135 for (
const auto& outpoint : COINS) {
142 for (; i < 16; ++i) {
143 TIME_SKIPS[i] = std::chrono::microseconds{i};
147 for (; i < 128; ++i) {
148 int diff_bits = ((i - 10) * 2) / 9;
150 TIME_SKIPS[i] = TIME_SKIPS[i - 1] + std::chrono::microseconds{diff};
161 const auto&
package = package_to_validate.m_txns;
163 Assert(package.size() == 2);
178 std::chrono::microseconds time{244466666};
185 auto rand_tx = fuzzed_data_provider.
ConsumeBool() ?
189 TRANSACTIONS.at(fuzzed_data_provider.ConsumeIntegralInRange<unsigned>(0, TRANSACTIONS.size() - 1));
192 fuzzed_data_provider,
196 .m_relay_permissions = fuzzed_data_provider.
ConsumeBool(),
197 .m_wtxid_relay = fuzzed_data_provider.
ConsumeBool()
199 txdownloadman.ConnectedPeer(rand_peer, info);
202 txdownloadman.DisconnectedPeer(rand_peer);
203 txdownloadman.CheckIsEmpty(rand_peer);
206 txdownloadman.ActiveTipChange();
210 block.
vtx.push_back(rand_tx);
211 txdownloadman.BlockConnected(std::make_shared<CBlock>(block));
214 txdownloadman.BlockDisconnected();
217 txdownloadman.MempoolAcceptedTx(rand_tx);
222 bool first_time_failure{fuzzed_data_provider.ConsumeBool()};
224 node::RejectedTxTodo todo = txdownloadman.MempoolRejectedTx(rand_tx, state, rand_peer, first_time_failure);
225 Assert(first_time_failure || !todo.m_should_add_extra_compact_tx);
231 txdownloadman.AddTxAnnouncement(rand_peer, gtxid, time, fuzzed_data_provider.
ConsumeBool());
234 txdownloadman.GetRequestsToSend(rand_peer, time);
237 txdownloadman.ReceivedTx(rand_peer, rand_tx);
238 const auto& [should_validate, maybe_package] = txdownloadman.ReceivedTx(rand_peer, rand_tx);
244 Assert(!(should_validate && maybe_package.has_value()));
245 if (maybe_package.has_value()) CheckPackageToValidate(*maybe_package, rand_peer);
248 txdownloadman.ReceivedNotFound(rand_peer, {rand_tx->GetWitnessHash()});
251 const bool expect_work{txdownloadman.HaveMoreWork(rand_peer)};
252 const auto ptx = txdownloadman.GetTxToReconsider(rand_peer);
265 if (fuzzed_data_provider.
ConsumeBool()) time_skip *= -1;
269 for (
NodeId nodeid = 0; nodeid < NUM_PEERS; ++nodeid) {
270 txdownloadman.DisconnectedPeer(nodeid);
271 txdownloadman.CheckIsEmpty(nodeid);
273 txdownloadman.CheckIsEmpty();
278static bool HasRelayPermissions(
NodeId peer) {
return peer == 0; }
288 for (
NodeId peer = 0; peer < NUM_PEERS; ++peer) {
289 if (!HasRelayPermissions(peer)) {
308 std::chrono::microseconds time{244466666};
315 auto rand_tx = fuzzed_data_provider.
ConsumeBool() ?
319 TRANSACTIONS.at(fuzzed_data_provider.ConsumeIntegralInRange<unsigned>(0, TRANSACTIONS.size() - 1));
322 fuzzed_data_provider,
326 .m_relay_permissions = HasRelayPermissions(rand_peer),
327 .m_wtxid_relay = fuzzed_data_provider.
ConsumeBool()
338 for (
const auto& tx : TRANSACTIONS) {
347 block.
vtx.push_back(rand_tx);
363 bool first_time_failure{fuzzed_data_provider.
ConsumeBool()};
378 const auto getdata_requests = txdownload_impl.
GetRequestsToSend(rand_peer, time);
381 for (
const auto& gtxid : getdata_requests) {
386 const auto& [should_validate, maybe_package] = txdownload_impl.
ReceivedTx(rand_peer, rand_tx);
392 Assert(!(should_validate && maybe_package.has_value()));
393 if (should_validate) {
396 if (maybe_package.has_value()) {
397 CheckPackageToValidate(*maybe_package, rand_peer);
399 const auto&
package = maybe_package->m_txns;
414 const bool expect_work{txdownload_impl.
HaveMoreWork(rand_peer)};
435 if (fuzzed_data_provider.
ConsumeBool()) time_skip *= -1;
437 CheckInvariants(txdownload_impl, max_orphan_count);
440 for (
NodeId nodeid = 0; nodeid < NUM_PEERS; ++nodeid) {
#define Assert(val)
Identity function.
std::vector< CTransactionRef > vtx
An outpoint - a combination of a transaction hash and an index n into its vout.
bool contains(Span< const unsigned char > vKey) const
uint64_t Finalize() const
Compute the 64-bit SipHash-2-4 of the data written so far.
CSipHasher & Write(uint64_t data)
Hash a 64-bit integer worth of data It is treated as if this was the little-endian interpretation of ...
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
T ConsumeIntegralInRange(T min, T max)
T PickValueInArray(const T(&array)[size])
A generic txid reference (txid or wtxid).
static GenTxid Wtxid(const uint256 &hash)
static GenTxid Txid(const uint256 &hash)
A writer stream (for serialization) that computes a 256-bit hash.
A class to track orphan transactions (failed on TX_MISSING_INPUTS) Since we cannot distinguish orphan...
size_t Size() const
Return how many entries exist in the orphange.
bool HaveTx(const Wtxid &wtxid) const
Check if we already have an orphan transaction (by wtxid only)
void SanityCheck() const
Run internal consistency check (testing only).
size_t Count(NodeId peer) const
Count how many announcements a peer has (REQUESTED, CANDIDATE, and COMPLETED combined).
bool Invalid(Result result, const std::string &reject_reason="", const std::string &debug_message="")
Class responsible for deciding what transactions to request and, once downloaded, whether and how to ...
CRollingBloomFilter & RecentConfirmedTransactionsFilter()
bool AddTxAnnouncement(NodeId peer, const GenTxid >xid, std::chrono::microseconds now, bool p2p_inv)
Consider adding this tx hash to txrequest.
void ReceivedNotFound(NodeId nodeid, const std::vector< uint256 > &txhashes)
Marks a tx as ReceivedResponse in txrequest.
void DisconnectedPeer(NodeId nodeid)
bool HaveMoreWork(NodeId nodeid)
TxRequestTracker m_txrequest
Tracks candidates for requesting and downloading transaction data.
std::pair< bool, std::optional< PackageToValidate > > ReceivedTx(NodeId nodeid, const CTransactionRef &ptx)
void MempoolAcceptedTx(const CTransactionRef &tx)
CRollingBloomFilter & RecentRejectsReconsiderableFilter()
RejectedTxTodo MempoolRejectedTx(const CTransactionRef &ptx, const TxValidationState &state, NodeId nodeid, bool first_time_failure)
void ConnectedPeer(NodeId nodeid, const TxDownloadConnectionInfo &info)
bool AlreadyHaveTx(const GenTxid >xid, bool include_reconsiderable)
Check whether we already have this gtxid in:
TxOrphanage m_orphanage
Manages unvalidated tx data (orphan transactions for which we are downloading ancestors).
std::vector< GenTxid > GetRequestsToSend(NodeId nodeid, std::chrono::microseconds current_time)
Get getdata requests to send.
void BlockConnected(const std::shared_ptr< const CBlock > &pblock)
CTransactionRef GetTxToReconsider(NodeId nodeid)
CRollingBloomFilter & RecentRejectsFilter()
transaction_identifier represents the two canonical transaction identifier types (txid,...
const uint256 & ToUint256() const LIFETIMEBOUND
static transaction_identifier FromUint256(const uint256 &id)
TxValidationResult
A "reason" why a transaction was invalid, suitable for determining whether the provider of the transa...
@ TX_MISSING_INPUTS
transaction was missing some of its inputs
@ TX_MEMPOOL_POLICY
violated mempool's fee/size/descendant/RBF/etc limits
@ TX_UNKNOWN
transaction was not validated because package failed
@ TX_PREMATURE_SPEND
transaction spends a coinbase too early, or violates locktime/sequence locks
@ TX_INPUTS_NOT_STANDARD
inputs (covered by txid) failed policy rules
@ TX_WITNESS_STRIPPED
Transaction is missing a witness.
@ TX_CONFLICT
Tx already in mempool or conflicts with a tx in the chain (if it conflicts with another tx in mempool...
@ TX_NOT_STANDARD
otherwise didn't meet our local policy rules
@ TX_WITNESS_MUTATED
Transaction might have a witness prior to SegWit activation, or witness may have been malleated (whic...
@ TX_CONSENSUS
invalid by consensus rules
@ TX_RECONSIDERABLE
fails some policy, but might be acceptable if submitted in a (different) package
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
static constexpr int32_t MAX_PEER_TX_ANNOUNCEMENTS
Maximum number of transactions to consider for requesting, per peer.
static CTransactionRef MakeTransactionSpending(const std::vector< COutPoint > &outpoints, FastRandomContext &det_rand)
bool IsChildWithParents(const Package &package)
Context-free check that a package is exactly one child and its parents; not all parents need to be pr...
uint256 GetPackageHash(const std::vector< CTransactionRef > &transactions)
Get the hash of these transactions' wtxids, concatenated in lexicographical order (treating the wtxid...
static CTransactionRef MakeTransactionRef(Tx &&txIn)
std::shared_ptr< const CTransaction > CTransactionRef
static constexpr CAmount CENT
A mutable version of CTransaction.
std::vector< CTxOut > vout
Txid GetHash() const
Compute the hash of this CMutableTransaction.
Testing setup that configures a complete environment.
std::vector< NodeId > m_senders
std::vector< uint256 > m_unique_parents
bool m_should_add_extra_compact_tx
const bool m_preferred
Whether this peer is preferred for transaction download.
size_t CallOneOf(FuzzedDataProvider &fuzzed_data_provider, Callables... callables)
void SeedRandomStateForTest(SeedRand seedtype)
Seed the global RNG state for testing and log the seed value.
@ ZEROS
Seed with a compile time constant of zeros.
static const CScript P2WSH_OP_TRUE
CTxMemPool::Options MemPoolOptionsForTest(const NodeContext &node)