30std::vector<COutPoint> g_outpoints_coinbase_init_mature;
36 lastRollingFeeUpdate =
GetTime();
37 blockSinceLastRollingFeeBump =
true;
41void initialize_tx_pool()
43 static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
44 g_setup = testing_setup.get();
47 BlockAssembler::Options options;
54 g_outpoints_coinbase_init_mature.push_back(prevout);
61 std::set<COutPoint>& m_mempool_outpoints;
63 explicit OutpointsUpdater(std::set<COutPoint>& r)
64 : m_mempool_outpoints{r} {}
71 for (uint32_t index{0}; index < tx.
info.
m_tx->vout.size(); ++index) {
79 for (
const auto& input : tx->vin) {
81 m_mempool_outpoints.insert(input.prevout);
84 for (uint32_t index{0}; index < tx->vout.size(); ++index) {
85 m_mempool_outpoints.erase(
COutPoint{tx->GetHash(), index});
91 std::set<CTransactionRef>& m_added;
93 explicit TransactionsDelta(std::set<CTransactionRef>& a)
113 std::numeric_limits<
decltype(chainstate.
m_chain.
Tip()->
nTime)>::max());
131 mempool_opts.check_ratio = 1;
136 auto mempool{std::make_unique<CTxMemPool>(std::move(mempool_opts), error)};
143std::unique_ptr<CTxMemPool> MakeEphemeralMempool(
const NodeContext&
node)
151 mempool_opts.require_standard =
true;
154 mempool_opts.min_relay_feerate =
CFeeRate(0);
158 auto mempool{std::make_unique<CTxMemPool>(std::move(mempool_opts), error)};
168std::optional<COutPoint> GetChildEvictingPrevout(
const CTxMemPool& tx_pool)
171 for (
const auto& tx_info : tx_pool.
infoAll()) {
172 const auto& entry = *
Assert(tx_pool.
GetEntry(tx_info.tx->GetHash()));
174 if (!dust_indexes.empty()) {
176 if (!children.empty()) {
177 Assert(children.size() == 1);
179 const auto& only_child = children.begin()->get().GetTx();
180 for (
const auto& tx_input : only_child.vin) {
181 if (tx_input.prevout.hash != tx_info.tx->GetHash()) {
182 return tx_input.prevout;
202 std::set<COutPoint> mempool_outpoints;
203 std::unordered_map<COutPoint, CAmount, SaltedOutpointHasher> outpoints_value;
204 for (
const auto& outpoint : g_outpoints_coinbase_init_mature) {
205 Assert(mempool_outpoints.insert(outpoint).second);
206 outpoints_value[outpoint] = 50 *
COIN;
209 auto outpoints_updater = std::make_shared<OutpointsUpdater>(mempool_outpoints);
210 node.validation_signals->RegisterSharedValidationInterface(outpoints_updater);
212 auto tx_pool_{MakeEphemeralMempool(
node)};
213 MockedTxPool& tx_pool = *
static_cast<MockedTxPool*
>(tx_pool_.get());
215 chainstate.SetMempool(&tx_pool);
219 Assert(!mempool_outpoints.empty());
221 std::vector<CTransactionRef> txs;
229 std::set<COutPoint> package_outpoints;
230 while (txs.size() < num_txs) {
232 txs.emplace_back([&] {
239 bool last_tx = num_txs > 1 && txs.size() == num_txs - 1;
240 const auto num_in = outpoint_to_rbf ? 2 :
245 auto& outpoints = last_tx ? package_outpoints : mempool_outpoints;
247 Assert((
int)outpoints.size() >= num_in && num_in > 0);
250 for (
int i = 0; i < num_in; ++i) {
253 auto pop = outpoints.begin();
255 auto outpoint = *pop;
257 if (i == 0 && outpoint_to_rbf) {
258 outpoint = *outpoint_to_rbf;
259 outpoints.erase(outpoint);
261 outpoints.erase(pop);
264 amount_in += outpoints_value.at(outpoint);
271 tx_mut.
vin.push_back(in);
275 const auto amount_out = (amount_in - amount_fee) / num_out;
276 for (
int i = 0; i < num_out; ++i) {
289 for (
const auto& in : tx->vin) {
293 for (
size_t i = 0; i < tx->vout.size(); ++i) {
294 package_outpoints.emplace(tx->GetHash(), i);
298 for (
size_t i = 0; i < tx->vout.size(); ++i) {
299 outpoints_value[
COutPoint(tx->GetHash(), i)] = tx->vout[i].nValue;
307 txs.back()->GetHash() :
312 if (tx_pool.exists(txid)) {
313 const auto tx_info{tx_pool.info(txid)};
314 if (
GetDust(*tx_info.tx, tx_pool.m_opts.dust_relay_feerate).empty()) {
315 tx_pool.PrioritiseTransaction(txid, delta);
320 auto single_submit = txs.size() == 1;
326 false, !single_submit));
332 const bool expect_valid{result_package.m_state.IsValid()};
336 node.validation_signals->SyncWithValidationInterfaceQueue();
341 node.validation_signals->UnregisterSharedValidationInterface(outpoints_updater);
343 WITH_LOCK(
::cs_main, tx_pool.check(chainstate.CoinsTip(), chainstate.m_chain.Height() + 1));
357 std::set<COutPoint> mempool_outpoints;
358 std::unordered_map<COutPoint, CAmount, SaltedOutpointHasher> outpoints_value;
359 for (
const auto& outpoint : g_outpoints_coinbase_init_mature) {
360 Assert(mempool_outpoints.insert(outpoint).second);
361 outpoints_value[outpoint] = 50 *
COIN;
364 auto outpoints_updater = std::make_shared<OutpointsUpdater>(mempool_outpoints);
365 node.validation_signals->RegisterSharedValidationInterface(outpoints_updater);
368 MockedTxPool& tx_pool = *
static_cast<MockedTxPool*
>(tx_pool_.get());
370 chainstate.SetMempool(&tx_pool);
374 Assert(!mempool_outpoints.empty());
376 std::vector<CTransactionRef> txs;
380 std::set<COutPoint> package_outpoints;
381 while (txs.size() < num_txs) {
383 txs.emplace_back([&] {
390 bool last_tx = num_txs > 1 && txs.size() == num_txs - 1;
394 auto& outpoints = last_tx ? package_outpoints : mempool_outpoints;
396 Assert(!outpoints.empty());
399 for (
size_t i = 0; i < num_in; ++i) {
402 auto pop = outpoints.begin();
404 const auto outpoint = *pop;
405 outpoints.erase(pop);
407 amount_in += outpoints_value.at(outpoint);
411 const auto script_sig =
CScript{};
420 tx_mut.
vin.push_back(in);
426 tx_mut.
vin.push_back(tx_mut.
vin.back());
431 tx_mut.
vin.emplace_back();
443 const auto amount_out = (amount_in - amount_fee) / num_out;
444 for (
int i = 0; i < num_out; ++i) {
450 for (
const auto& in : tx->vin) {
455 for (
size_t i = 0; i < tx->vout.size(); ++i) {
456 package_outpoints.emplace(tx->GetHash(), i);
460 for (
size_t i = 0; i < tx->vout.size(); ++i) {
461 outpoints_value[
COutPoint(tx->GetHash(), i)] = tx->vout[i].nValue;
471 tx_pool.RollingFeeUpdate();
475 txs.back()->GetHash() :
478 tx_pool.PrioritiseTransaction(txid, delta);
482 std::set<CTransactionRef> added;
483 auto txr = std::make_shared<TransactionsDelta>(added);
484 node.validation_signals->RegisterSharedValidationInterface(txr);
492 std::optional<CFeeRate> client_maxfeerate{};
498 return ProcessNewPackage(chainstate, tx_pool, txs, single_submit, client_maxfeerate));
503 false, !single_submit));
506 node.validation_signals->SyncWithValidationInterfaceQueue();
507 node.validation_signals->UnregisterSharedValidationInterface(txr);
511 Assert(passed != added.empty());
512 Assert(passed == res.m_state.IsValid());
514 Assert(added.size() == 1);
515 Assert(txs.back() == *added.begin());
521 const bool expect_valid{result_package.m_state.IsValid()};
525 Assert(result_package.m_tx_results.size() == txs.size() || result_package.m_tx_results.empty());
531 if (tx_pool.m_opts.require_standard) {
536 node.validation_signals->UnregisterSharedValidationInterface(outpoints_updater);
538 WITH_LOCK(
::cs_main, tx_pool.check(chainstate.CoinsTip(), chainstate.m_chain.Height() + 1));
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.
int64_t GetMedianTimePast() const
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Fee rate in satoshis per virtualbyte: CAmount / vB the feerate is represented internally as FeeFrac.
An outpoint - a combination of a transaction hash and an index n into its vout.
Serialized script, used inside transaction inputs and outputs.
static const uint32_t CURRENT_VERSION
An input of a transaction.
CScriptWitness scriptWitness
Only serialized through CTransaction.
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
std::vector< TxMempoolInfo > infoAll() const
std::vector< CTxMemPoolEntry::CTxMemPoolEntryRef > GetChildren(const CTxMemPoolEntry &entry) const
const CTxMemPoolEntry * GetEntry(const Txid &txid) const LIFETIMEBOUND EXCLUSIVE_LOCKS_REQUIRED(cs)
An output of a transaction.
Implement this to subscribe to events generated in validation and mempool.
virtual void TransactionRemovedFromMempool(const CTransactionRef &tx, MemPoolRemovalReason reason, uint64_t mempool_sequence)
Notifies listeners of a transaction leaving mempool.
virtual void TransactionAddedToMempool(const NewMempoolTransactionInfo &tx, uint64_t mempool_sequence)
Notifies listeners of a transaction having been added to mempool.
Chainstate stores and provides an API to update our local knowledge of the current best chain.
CChain m_chain
The current chain of blockheaders we consult and build on.
T ConsumeIntegralInRange(T min, T max)
Generate a new block, without valid proof-of-work.
static const int COINBASE_MATURITY
Coinbase transaction outputs can only be spent after this number of new blocks (network rule)
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
#define LIMITED_WHILE(condition, limit)
Can be used to limit a theoretically unbounded loop.
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal.
@ PCKG_POLICY
The package itself is invalid (e.g. too many transactions).
unsigned int nBytesPerSigOp
std::vector< uint32_t > GetDust(const CTransaction &tx, CFeeRate dust_relay_rate)
Get the vout index numbers of all dust outputs.
static CTransactionRef MakeTransactionRef(Tx &&txIn)
std::shared_ptr< const CTransaction > CTransactionRef
A mutable version of CTransaction.
std::vector< CTxOut > vout
std::vector< std::vector< unsigned char > > stack
Testing setup that configures a complete environment.
const CTransactionRef m_tx
int64_t ancestor_count
The maximum allowed number of transactions in a package including the entry and its ancestors.
Options struct containing options for constructing a CTxMemPool.
CFeeRate dust_relay_feerate
NodeContext struct containing references to chain state and connection state.
std::unique_ptr< ValidationSignals > validation_signals
Issues calls about blocks and transactions.
std::unique_ptr< ChainstateManager > chainman
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
uint32_t ConsumeSequence(FuzzedDataProvider &fuzzed_data_provider) noexcept
int64_t ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
auto & PickValue(FuzzedDataProvider &fuzzed_data_provider, Collection &col)
COutPoint MineBlock(const NodeContext &node, const node::BlockAssembler::Options &assembler_options)
Returns the generated coin.
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 std::vector< std::vector< uint8_t > > P2WSH_EMPTY_TRUE_STACK
static const std::vector< std::vector< uint8_t > > P2WSH_EMPTY_TWO_STACK
static const CScript P2WSH_EMPTY
void CheckMempoolTRUCInvariants(const CTxMemPool &tx_pool)
For every transaction in tx_pool, check TRUC invariants:
CTxMemPool::Options MemPoolOptionsForTest(const NodeContext &node)
std::optional< std::string > CheckPackageMempoolAcceptResult(const Package &txns, const PackageMempoolAcceptResult &result, bool expect_valid, const CTxMemPool *mempool)
Check expected properties for every PackageMempoolAcceptResult, regardless of value.
void CheckMempoolEphemeralInvariants(const CTxMemPool &tx_pool)
Check that we never get into a state where an ephemeral dust transaction would be mined without the s...
#define EXCLUSIVE_LOCKS_REQUIRED(...)
static constexpr decltype(CTransaction::version) TRUC_VERSION
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
PackageMempoolAcceptResult ProcessNewPackage(Chainstate &active_chainstate, CTxMemPool &pool, const Package &package, bool test_accept, const std::optional< CFeeRate > &client_maxfeerate)
Validate (and maybe submit) a package to the mempool.
MempoolAcceptResult AcceptToMemoryPool(Chainstate &active_chainstate, const CTransactionRef &tx, int64_t accept_time, bool bypass_limits, bool test_accept)
Try to add a transaction to the mempool.
FuzzedDataProvider & fuzzed_data_provider