13 #include <boost/test/unit_test.hpp>
34 txParent.
vin.resize(1);
36 txParent.
vout.resize(3);
37 for (
int i = 0; i < 3; i++)
40 txParent.
vout[i].nValue = 33000LL;
43 for (
int i = 0; i < 3; i++)
45 txChild[i].
vin.resize(1);
47 txChild[i].
vin[0].prevout.hash = txParent.
GetHash();
48 txChild[i].
vin[0].prevout.n = i;
49 txChild[i].
vout.resize(1);
51 txChild[i].
vout[0].nValue = 11000LL;
54 for (
int i = 0; i < 3; i++)
56 txGrandChild[i].
vin.resize(1);
58 txGrandChild[i].
vin[0].prevout.hash = txChild[i].
GetHash();
59 txGrandChild[i].
vin[0].prevout.n = 0;
60 txGrandChild[i].
vout.resize(1);
62 txGrandChild[i].
vout[0].nValue = 11000LL;
70 unsigned int poolSize = testPool.
size();
76 poolSize = testPool.
size();
82 for (
int i = 0; i < 3; i++)
88 poolSize = testPool.
size();
92 poolSize = testPool.
size();
95 poolSize = testPool.
size();
99 poolSize = testPool.
size();
105 for (
int i = 0; i < 3; i++)
112 poolSize = testPool.
size();
118 template <
typename name>
122 typename CTxMemPool::indexed_transaction_set::index<name>::type::iterator it = pool.mapTx.get<
name>().begin();
124 for (; it != pool.mapTx.get<
name>().end(); ++it, ++
count) {
172 std::vector<std::string> sortedOrder;
173 sortedOrder.resize(5);
179 CheckSort<descendant_score>(pool, sortedOrder);
191 CheckSort<descendant_score>(pool, sortedOrder);
194 setAncestors.insert(pool.mapTx.find(tx6.
GetHash()));
206 BOOST_REQUIRE(ancestors_calculated.has_value());
207 BOOST_CHECK(*ancestors_calculated == setAncestors);
213 sortedOrder.erase(sortedOrder.begin());
216 CheckSort<descendant_score>(pool, sortedOrder);
226 setAncestors.insert(pool.mapTx.find(tx7.
GetHash()));
231 CheckSort<descendant_score>(pool, sortedOrder);
246 CheckSort<descendant_score>(pool, sortedOrder);
248 std::vector<std::string> snapshotOrder = sortedOrder;
250 setAncestors.insert(pool.mapTx.find(tx8.
GetHash()));
251 setAncestors.insert(pool.mapTx.find(tx9.
GetHash()));
264 BOOST_REQUIRE(ancestors_calculated);
265 BOOST_CHECK(*ancestors_calculated == setAncestors);
284 sortedOrder.erase(sortedOrder.begin(), sortedOrder.begin()+2);
288 CheckSort<descendant_score>(pool, sortedOrder);
295 CheckSort<descendant_score>(pool, snapshotOrder);
344 std::vector<std::string> sortedOrder;
345 sortedOrder.resize(5);
360 CheckSort<ancestor_score>(pool, sortedOrder);
378 CheckSort<ancestor_score>(pool, sortedOrder);
390 CAmount fee = (20000/tx2Size)*(tx7Size + tx6Size) - 1;
395 CheckSort<ancestor_score>(pool, sortedOrder);
398 std::vector<CTransactionRef> vtx;
402 sortedOrder.erase(sortedOrder.begin()+1);
405 sortedOrder.pop_back();
407 sortedOrder.erase(sortedOrder.end()-2);
409 CheckSort<ancestor_score>(pool, sortedOrder);
426 CheckSort<ancestor_score>(pool, sortedOrder);
442 pool.addUnchecked(entry.
Fee(10000LL).
FromTx(tx1));
450 pool.addUnchecked(entry.
Fee(5000LL).
FromTx(tx2));
452 pool.TrimToSize(pool.DynamicMemoryUsage());
456 pool.TrimToSize(pool.DynamicMemoryUsage() * 3 / 4);
460 pool.addUnchecked(entry.
FromTx(tx2));
468 pool.addUnchecked(entry.
Fee(20000LL).
FromTx(tx3));
470 pool.TrimToSize(pool.DynamicMemoryUsage() * 3 / 4);
485 tx4.
vin[0].prevout.SetNull();
487 tx4.
vin[1].prevout.SetNull();
499 tx5.
vin[1].prevout.SetNull();
511 tx6.
vin[1].prevout.SetNull();
531 pool.addUnchecked(entry.
Fee(7000LL).
FromTx(tx4));
532 pool.addUnchecked(entry.
Fee(1000LL).
FromTx(tx5));
533 pool.addUnchecked(entry.
Fee(1100LL).
FromTx(tx6));
534 pool.addUnchecked(entry.
Fee(9000LL).
FromTx(tx7));
537 pool.TrimToSize(pool.DynamicMemoryUsage() - 1);
543 pool.addUnchecked(entry.
Fee(1000LL).
FromTx(tx5));
544 pool.addUnchecked(entry.
Fee(9000LL).
FromTx(tx7));
546 pool.TrimToSize(pool.DynamicMemoryUsage() / 2);
552 pool.addUnchecked(entry.
Fee(1000LL).
FromTx(tx5));
553 pool.addUnchecked(entry.
Fee(9000LL).
FromTx(tx7));
555 std::vector<CTransactionRef> vtx;
560 pool.removeForBlock(vtx, 1);
566 BOOST_CHECK_EQUAL(pool.GetMinFee(pool.DynamicMemoryUsage() * 5 / 2).GetFeePerK(), llround((maxFeeRateRemoved.
GetFeePerK() + 1000)/4.0));
570 BOOST_CHECK_EQUAL(pool.GetMinFee(pool.DynamicMemoryUsage() * 9 / 2).GetFeePerK(), llround((maxFeeRateRemoved.
GetFeePerK() + 1000)/8.0));
582 inline CTransactionRef make_tx(std::vector<CAmount>&& output_values, std::vector<CTransactionRef>&& inputs=std::vector<CTransactionRef>(), std::vector<uint32_t>&& input_indices=std::vector<uint32_t>())
585 tx.
vin.resize(inputs.size());
586 tx.
vout.resize(output_values.size());
587 for (
size_t i = 0; i < inputs.size(); ++i) {
588 tx.
vin[i].prevout.hash = inputs[i]->GetHash();
589 tx.
vin[i].prevout.n = input_indices.size() > i ? input_indices[i] : 0;
591 for (
size_t i = 0; i < output_values.size(); ++i) {
593 tx.
vout[i].nValue = output_values[i];
601 size_t ancestors, descendants;
701 for (uint64_t i = 0; i < 5; i++) {
703 tyi =
make_tx({v}, i > 0 ? std::vector<CTransactionRef>{*ty[i - 1]} : std::vector<CTransactionRef>{});
760 size_t ancestors, descendants;
int64_t CAmount
Amount in satoshis (Can be negative)
static constexpr CAmount COIN
The amount of satoshis in one BTC.
#define Assert(val)
Identity function.
Fee rate in satoshis per kilovirtualbyte: CAmount / kvB.
CAmount GetFeePerK() const
Return the fee in satoshis for a vsize of 1000 vbytes.
An outpoint - a combination of a transaction hash and an index n into its vout.
Serialized script, used inside transaction inputs and outputs.
The basic transaction that is broadcasted on the network and contained in blocks.
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
CFeeRate GetMinFee() const
The minimum fee to get into the mempool, which may itself not be enough for larger-sized transactions...
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
util::Result< setEntries > CalculateMemPoolAncestors(const CTxMemPoolEntry &entry, const Limits &limits, bool fSearchForParents=true) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Try to calculate all in-mempool ancestors of entry.
void removeRecursive(const CTransaction &tx, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
void check(const CCoinsViewCache &active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(void addUnchecked(const CTxMemPoolEntry &entry, bool validFeeEstimate=true) EXCLUSIVE_LOCKS_REQUIRED(cs
If sanity-checking is turned on, check makes sure the pool is consistent (does not contain two transa...
void GetTransactionAncestry(const uint256 &txid, size_t &ancestors, size_t &descendants, size_t *ancestorsize=nullptr, CAmount *ancestorfees=nullptr) const
Calculate the ancestor and descendant count for the given transaction.
static const int ROLLING_FEE_HALFLIFE
std::set< txiter, CompareIteratorByHash > setEntries
void removeForBlock(const std::vector< CTransactionRef > &vtx, unsigned int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(cs)
Called when a block is connected.
unsigned long size() const
static GenTxid Txid(const uint256 &hash)
std::string ToString() const
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
BOOST_AUTO_TEST_SUITE_END()
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal.
CTransactionRef make_tx(std::vector< CAmount > &&output_values, std::vector< CTransactionRef > &&inputs=std::vector< CTransactionRef >(), std::vector< uint32_t > &&input_indices=std::vector< uint32_t >())
static void CheckSort(CTxMemPool &pool, std::vector< std::string > &sortedOrder) EXCLUSIVE_LOCKS_REQUIRED(pool.cs)
static constexpr auto REMOVAL_REASON_DUMMY
BOOST_AUTO_TEST_CASE(MempoolRemoveTest)
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
int64_t GetVirtualTransactionSize(int64_t nWeight, int64_t nSigOpCost, unsigned int bytes_per_sigop)
Compute the virtual transaction size (weight reinterpreted as bytes).
static CTransactionRef MakeTransactionRef(Tx &&txIn)
std::shared_ptr< const CTransaction > CTransactionRef
static constexpr CAmount CENT
A mutable version of CTransaction.
uint256 GetHash() const
Compute the hash of this CMutableTransaction.
std::vector< CTxOut > vout
CTxMemPoolEntry FromTx(const CMutableTransaction &tx) const
TestMemPoolEntryHelper & Time(NodeSeconds tp)
TestMemPoolEntryHelper & Fee(CAmount _fee)
Testing setup that configures a complete environment.
static constexpr MemPoolLimits NoLimits()
std::unique_ptr< CTxMemPool > mempool
#define EXCLUSIVE_LOCKS_REQUIRED(...)
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds