17 #include <boost/test/unit_test.hpp> 26 return m_orphans.size();
31 std::map<Wtxid, OrphanTx>::iterator it;
33 if (it == m_orphans.end())
34 it = m_orphans.begin();
41 std::vector<unsigned char> keydata;
42 keydata = rand_ctx.randbytes(32);
43 key.
Set(keydata.data(), keydata.data() + keydata.size(),
true);
54 if (outpoints.empty()) {
57 for (
const auto& outpoint : outpoints) {
58 tx.
vin.emplace_back(outpoint);
62 tx.
vin[0].scriptWitness.stack.push_back({1});
75 tx.
vin[0].scriptWitness.stack.push_back({5});
77 assert(ptx->GetHash() == mutated_tx->GetHash());
81 static bool EqualTxns(
const std::set<CTransactionRef>& set_txns,
const std::vector<CTransactionRef>& vec_txns)
83 if (vec_txns.size() != set_txns.size())
return false;
84 for (
const auto& tx : vec_txns) {
85 if (!set_txns.contains(tx))
return false;
89 static bool EqualTxns(
const std::set<CTransactionRef>& set_txns,
90 const std::vector<std::pair<CTransactionRef, NodeId>>& vec_txns)
92 if (vec_txns.size() != set_txns.size())
return false;
93 for (
const auto& [tx, nodeid] : vec_txns) {
94 if (!set_txns.contains(tx))
return false;
116 auto now{GetTime<std::chrono::seconds>()};
120 for (
int i = 0; i < 50; i++)
124 tx.
vin[0].prevout.n = 0;
135 for (
int i = 0; i < 50; i++)
141 tx.
vin[0].prevout.n = 0;
142 tx.
vin[0].prevout.hash = txPrev->GetHash();
153 for (
int i = 0; i < 10; i++)
162 for (
unsigned int j = 0; j < tx.
vin.size(); j++)
164 tx.
vin[j].prevout.n = j;
165 tx.
vin[j].prevout.hash = txPrev->GetHash();
171 for (
unsigned int j = 1; j < tx.
vin.size(); j++)
172 tx.
vin[j].scriptSig = tx.
vin[0].scriptSig;
185 for (
NodeId i = 0; i < 3; i++)
188 expected_num_orphans -= 2;
196 expected_num_orphans -= 1;
199 assert(expected_num_orphans > 40);
209 orphanage.
AddTx(timeout_tx, 0);
231 std::vector<COutPoint> empty_outpoints;
238 const auto& normal_wtxid = child_normal->GetWitnessHash();
239 const auto& mutated_wtxid = child_mutated->GetWitnessHash();
254 std::set<CTransactionRef> expected_children{child_normal, child_mutated};
271 std::vector<COutPoint> empty_outpoints;
277 while (parent1->GetHash() == parent2->GetHash()) {
300 std::set<CTransactionRef> expected_parent1_children{child_p1n0, child_p1n0_p2n0, child_p1n0_p1n1};
301 std::set<CTransactionRef> expected_parent2_children{child_p2n1, child_p1n0_p2n0};
337 std::set<CTransactionRef> expected_parent1_node1{child_p1n0};
345 std::set<CTransactionRef> expected_parent2_node1{child_p2n1};
353 std::set<CTransactionRef> expected_parent1_node2{child_p1n0_p1n1, child_p1n0_p2n0};
361 std::set<CTransactionRef> expected_parent2_node2{child_p1n0_p2n0};
std::shared_ptr< const CTransaction > CTransactionRef
int EraseTx(const Wtxid &wtxid)
Erase an orphan by wtxid.
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).
static constexpr auto ORPHAN_TX_EXPIRE_TIME
Expiration time for orphan transactions.
size_t CountOrphans() const
CPubKey GetPubKey() const
Compute the public key from a private key.
A class to track orphan transactions (failed on TX_MISSING_INPUTS) Since we cannot distinguish orphan...
std::vector< CTransactionRef > GetChildrenFromSamePeer(const CTransactionRef &parent, NodeId nodeid) const
Get all children that spend from this tx and were received from nodeid.
static void MakeNewKeyWithFastRandomContext(CKey &key, FastRandomContext &rand_ctx=g_insecure_rand_ctx)
BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
CTransactionRef RandomOrphan()
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
void LimitOrphans(unsigned int max_orphans, FastRandomContext &rng)
Limit the orphanage to the given maximum.
void EraseForPeer(NodeId peer)
Erase all orphans announced by a peer (eg, after that peer disconnects)
void Reseed(const uint256 &seed) noexcept
Reseed with explicit seed (only for testing).
static CTransactionRef MakeMutation(const CTransactionRef &ptx)
BOOST_AUTO_TEST_SUITE_END()
Fillable signing provider that keeps keys in an address->secret map.
std::vector< std::pair< CTransactionRef, NodeId > > GetChildrenFromDifferentPeer(const CTransactionRef &parent, NodeId nodeid) const
Get all children that spend from this tx but were not received from nodeid.
static CTransactionRef MakeTransactionSpending(const std::vector< COutPoint > &outpoints, FastRandomContext &det_rand)
static uint256 InsecureRand256()
std::vector< CTxOut > vout
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
bool AddTx(const CTransactionRef &tx, NodeId peer)
Add a new orphan transaction.
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
static CTransactionRef MakeTransactionRef(Tx &&txIn)
#define BOOST_CHECK_EQUAL(v1, v2)
static transaction_identifier FromUint256(const uint256 &id)
uint256 rand256() noexcept
generate a random uint256.
static bool EqualTxns(const std::set< CTransactionRef > &set_txns, const std::vector< CTransactionRef > &vec_txns)
FastRandomContext g_insecure_rand_ctx
This global and the helpers that use it are not thread-safe.
A mutable version of CTransaction.
static constexpr CAmount CENT
An encapsulated private key.
bool HaveTx(const Wtxid &wtxid) const
Check if we already have an orphan transaction (by wtxid only)
Testing setup that configures a complete environment.
virtual bool AddKey(const CKey &key)
bool IsValid() const
Check whether this private key is valid.
#define BOOST_CHECK(expr)