20#include <boost/test/unit_test.hpp>
31 return m_orphans.size();
36 std::map<Wtxid, OrphanTx>::iterator it;
38 if (it == m_orphans.end())
39 it = m_orphans.begin();
48 std::vector<unsigned char> keydata;
50 key.
Set(keydata.data(), keydata.data() + keydata.size(),
true);
61 if (outpoints.empty()) {
64 for (
const auto& outpoint : outpoints) {
65 tx.
vin.emplace_back(outpoint);
69 tx.
vin[0].scriptWitness.stack.push_back({1});
82 tx.
vin[0].scriptWitness.stack.push_back({5});
84 assert(ptx->GetHash() == mutated_tx->GetHash());
88static bool EqualTxns(
const std::set<CTransactionRef>& set_txns,
const std::vector<CTransactionRef>& vec_txns)
90 if (vec_txns.size() != set_txns.size())
return false;
91 for (
const auto& tx : vec_txns) {
92 if (!set_txns.contains(tx))
return false;
96static bool EqualTxns(
const std::set<CTransactionRef>& set_txns,
97 const std::vector<std::pair<CTransactionRef, NodeId>>& vec_txns)
99 if (vec_txns.size() != set_txns.size())
return false;
100 for (
const auto& [tx, nodeid] : vec_txns) {
101 if (!set_txns.contains(tx))
return false;
123 auto now{GetTime<std::chrono::seconds>()};
127 for (
int i = 0; i < 50; i++)
131 tx.
vin[0].prevout.n = 0;
142 for (
int i = 0; i < 50; i++)
148 tx.
vin[0].prevout.n = 0;
149 tx.
vin[0].prevout.hash = txPrev->GetHash();
160 for (
int i = 0; i < 10; i++)
169 for (
unsigned int j = 0; j < tx.
vin.size(); j++)
171 tx.
vin[j].prevout.n = j;
172 tx.
vin[j].prevout.hash = txPrev->GetHash();
178 for (
unsigned int j = 1; j < tx.
vin.size(); j++)
179 tx.
vin[j].scriptSig = tx.
vin[0].scriptSig;
184 size_t expected_num_orphans = orphanage.CountOrphans();
187 orphanage.EraseForPeer(-1);
192 for (
NodeId i = 0; i < 3; i++)
194 orphanage.EraseForPeer(i);
195 expected_num_orphans -= 2;
196 BOOST_CHECK(orphanage.CountOrphans() == expected_num_orphans);
201 orphanage.LimitOrphans(expected_num_orphans, rng);
203 expected_num_orphans -= 1;
204 orphanage.LimitOrphans(expected_num_orphans, rng);
206 assert(expected_num_orphans > 40);
207 orphanage.LimitOrphans(40, rng);
209 orphanage.LimitOrphans(10, rng);
211 orphanage.LimitOrphans(0, rng);
216 orphanage.AddTx(timeout_tx, 0);
217 orphanage.LimitOrphans(1, rng);
222 orphanage.LimitOrphans(1, rng);
228 orphanage.LimitOrphans(1, rng);
238 std::vector<COutPoint> empty_outpoints;
245 const auto& normal_wtxid = child_normal->GetWitnessHash();
246 const auto& mutated_wtxid = child_mutated->GetWitnessHash();
261 std::set<CTransactionRef> expected_children{child_normal, child_mutated};
278 std::vector<COutPoint> empty_outpoints;
284 while (parent1->GetHash() == parent2->GetHash()) {
307 std::set<CTransactionRef> expected_parent1_children{child_p1n0, child_p1n0_p2n0, child_p1n0_p1n1};
308 std::set<CTransactionRef> expected_parent2_children{child_p2n1, child_p1n0_p2n0};
344 std::set<CTransactionRef> expected_parent1_node1{child_p1n0};
352 std::set<CTransactionRef> expected_parent2_node1{child_p2n1};
360 std::set<CTransactionRef> expected_parent1_node2{child_p1n0_p1n1, child_p1n0_p2n0};
368 std::set<CTransactionRef> expected_parent2_node2{child_p1n0_p2n0};
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
An encapsulated private key.
bool IsValid() const
Check whether this private key is valid.
CPubKey GetPubKey() const
Compute the public key from a private key.
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
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.
std::vector< B > randbytes(size_t len) noexcept
Generate random bytes.
A class to track orphan transactions (failed on TX_MISSING_INPUTS) Since we cannot distinguish orphan...
bool AddTx(const CTransactionRef &tx, NodeId peer)
Add a new orphan transaction.
int EraseTx(const Wtxid &wtxid)
Erase an orphan by wtxid.
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.
std::vector< CTransactionRef > GetChildrenFromSamePeer(const CTransactionRef &parent, NodeId nodeid) const
Get all children that spend from this tx and were received from nodeid.
bool HaveTx(const Wtxid &wtxid) const
Check if we already have an orphan transaction (by wtxid only)
FastRandomContext & m_rng
size_t CountOrphans() const
CTransactionRef RandomOrphan()
TxOrphanageTest(FastRandomContext &rng)
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()
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
static CTransactionRef MakeMutation(const CTransactionRef &ptx)
static void MakeNewKeyWithFastRandomContext(CKey &key, FastRandomContext &rand_ctx)
BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
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 SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
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).
static constexpr auto ORPHAN_TX_EXPIRE_TIME
Expiration time for orphan transactions.