46#include <unordered_map>
54std::vector<COutPoint> g_outpoints_coinbase_init_mature;
60 lastRollingFeeUpdate =
GetTime();
61 blockSinceLastRollingFeeBump =
true;
65void initialize_tx_pool()
67 static const auto testing_setup = MakeNoLogFileContext<const TestingSetup>();
73 .coinbase_output_script = P2WSH_EMPTY,
77 g_outpoints_coinbase_init_mature.push_back(prevout);
84 std::set<COutPoint>& m_mempool_outpoints;
86 explicit OutpointsUpdater(std::set<COutPoint>& r)
87 : m_mempool_outpoints{r} {}
94 for (uint32_t index{0}; index < tx.
info.
m_tx->vout.size(); ++index) {
102 for (
const auto& input : tx->vin) {
104 m_mempool_outpoints.insert(input.prevout);
107 for (uint32_t index{0}; index < tx->vout.size(); ++index) {
108 m_mempool_outpoints.erase(
COutPoint{tx->GetHash(), index});
114 std::set<CTransactionRef>& m_added;
116 explicit TransactionsDelta(std::set<CTransactionRef>& a)
136 std::numeric_limits<
decltype(chainstate.
m_chain.
Tip()->
nTime)>::max());
154 mempool_opts.check_ratio = 1;
159 auto mempool{std::make_unique<CTxMemPool>(std::move(mempool_opts), error)};
166std::unique_ptr<CTxMemPool> MakeEphemeralMempool(
const NodeContext&
node)
174 mempool_opts.require_standard =
true;
177 mempool_opts.min_relay_feerate =
CFeeRate(0);
181 auto mempool{std::make_unique<CTxMemPool>(std::move(mempool_opts), error)};
191std::optional<COutPoint> GetChildEvictingPrevout(
const CTxMemPool& tx_pool)
194 for (
const auto& tx_info : tx_pool.
infoAll()) {
195 const auto& entry = *
Assert(tx_pool.
GetEntry(tx_info.tx->GetHash()));
197 if (!dust_indexes.empty()) {
199 if (!children.empty()) {
200 Assert(children.size() == 1);
202 const auto& only_child = children.begin()->get().GetTx();
203 for (
const auto& tx_input : only_child.vin) {
204 if (tx_input.prevout.hash != tx_info.tx->GetHash()) {
205 return tx_input.prevout;
225 std::set<COutPoint> mempool_outpoints;
226 std::unordered_map<COutPoint, CAmount, SaltedOutpointHasher> outpoints_value;
227 for (
const auto& outpoint : g_outpoints_coinbase_init_mature) {
228 Assert(mempool_outpoints.insert(outpoint).second);
229 outpoints_value[outpoint] = 50 *
COIN;
232 auto outpoints_updater = std::make_shared<OutpointsUpdater>(mempool_outpoints);
233 node.validation_signals->RegisterSharedValidationInterface(outpoints_updater);
235 auto tx_pool_{MakeEphemeralMempool(
node)};
236 MockedTxPool& tx_pool = *
static_cast<MockedTxPool*
>(tx_pool_.get());
238 chainstate.SetMempool(&tx_pool);
242 Assert(!mempool_outpoints.empty());
244 std::vector<CTransactionRef> txs;
252 std::set<COutPoint> package_outpoints;
253 while (txs.size() < num_txs) {
255 txs.emplace_back([&] {
262 bool last_tx = num_txs > 1 && txs.size() == num_txs - 1;
263 const auto num_in = outpoint_to_rbf ? 2 :
268 auto& outpoints = last_tx ? package_outpoints : mempool_outpoints;
270 Assert((
int)outpoints.size() >= num_in && num_in > 0);
273 for (
int i = 0; i < num_in; ++i) {
276 auto pop = outpoints.begin();
278 auto outpoint = *pop;
280 if (i == 0 && outpoint_to_rbf) {
281 outpoint = *outpoint_to_rbf;
282 outpoints.erase(outpoint);
284 outpoints.erase(pop);
287 amount_in += outpoints_value.at(outpoint);
294 tx_mut.
vin.push_back(in);
298 const auto amount_out = (amount_in - amount_fee) / num_out;
299 for (
int i = 0; i < num_out; ++i) {
312 for (
const auto& in : tx->vin) {
316 for (
size_t i = 0; i < tx->vout.size(); ++i) {
317 package_outpoints.emplace(tx->GetHash(), i);
321 for (
size_t i = 0; i < tx->vout.size(); ++i) {
322 outpoints_value[
COutPoint(tx->GetHash(), i)] = tx->vout[i].nValue;
330 txs.back()->GetHash() :
335 if (tx_pool.exists(txid)) {
336 const auto tx_info{tx_pool.info(txid)};
337 if (
GetDust(*tx_info.tx, tx_pool.m_opts.dust_relay_feerate).empty()) {
338 tx_pool.PrioritiseTransaction(txid, delta);
343 auto single_submit = txs.size() == 1;
349 false, !single_submit));
355 const bool expect_valid{result_package.m_state.IsValid()};
359 node.validation_signals->SyncWithValidationInterfaceQueue();
364 node.validation_signals->UnregisterSharedValidationInterface(outpoints_updater);
366 WITH_LOCK(
::cs_main, tx_pool.check(chainstate.CoinsTip(), chainstate.m_chain.Height() + 1));
380 std::set<COutPoint> mempool_outpoints;
381 std::unordered_map<COutPoint, CAmount, SaltedOutpointHasher> outpoints_value;
382 for (
const auto& outpoint : g_outpoints_coinbase_init_mature) {
383 Assert(mempool_outpoints.insert(outpoint).second);
384 outpoints_value[outpoint] = 50 *
COIN;
387 auto outpoints_updater = std::make_shared<OutpointsUpdater>(mempool_outpoints);
388 node.validation_signals->RegisterSharedValidationInterface(outpoints_updater);
391 MockedTxPool& tx_pool = *
static_cast<MockedTxPool*
>(tx_pool_.get());
393 chainstate.SetMempool(&tx_pool);
397 Assert(!mempool_outpoints.empty());
399 std::vector<CTransactionRef> txs;
403 std::set<COutPoint> package_outpoints;
404 while (txs.size() < num_txs) {
406 txs.emplace_back([&] {
413 bool last_tx = num_txs > 1 && txs.size() == num_txs - 1;
417 auto& outpoints = last_tx ? package_outpoints : mempool_outpoints;
419 Assert(!outpoints.empty());
422 for (
size_t i = 0; i < num_in; ++i) {
425 auto pop = outpoints.begin();
427 const auto outpoint = *pop;
428 outpoints.erase(pop);
430 amount_in += outpoints_value.at(outpoint);
434 const auto script_sig =
CScript{};
443 tx_mut.
vin.push_back(in);
449 tx_mut.
vin.push_back(tx_mut.
vin.back());
454 tx_mut.
vin.emplace_back();
466 const auto amount_out = (amount_in - amount_fee) / num_out;
467 for (
int i = 0; i < num_out; ++i) {
473 for (
const auto& in : tx->vin) {
478 for (
size_t i = 0; i < tx->vout.size(); ++i) {
479 package_outpoints.emplace(tx->GetHash(), i);
483 for (
size_t i = 0; i < tx->vout.size(); ++i) {
484 outpoints_value[
COutPoint(tx->GetHash(), i)] = tx->vout[i].nValue;
494 tx_pool.RollingFeeUpdate();
498 txs.back()->GetHash() :
501 tx_pool.PrioritiseTransaction(txid, delta);
505 std::set<CTransactionRef> added;
506 auto txr = std::make_shared<TransactionsDelta>(added);
507 node.validation_signals->RegisterSharedValidationInterface(txr);
515 std::optional<CFeeRate> client_maxfeerate{};
521 return ProcessNewPackage(chainstate, tx_pool, txs, single_submit, client_maxfeerate));
526 false, !single_submit));
529 node.validation_signals->SyncWithValidationInterfaceQueue();
530 node.validation_signals->UnregisterSharedValidationInterface(txr);
534 Assert(passed != added.empty());
535 Assert(passed == res.m_state.IsValid());
537 Assert(added.size() == 1);
538 Assert(txs.back() == *added.begin());
544 const bool expect_valid{result_package.m_state.IsValid()};
548 Assert(result_package.m_tx_results.size() == txs.size() || result_package.m_tx_results.empty());
554 if (tx_pool.m_opts.require_standard) {
559 node.validation_signals->UnregisterSharedValidationInterface(outpoints_updater);
561 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.
const TestingSetup * g_setup
#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 ...
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)
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.
is used externally by mining IPC clients, so it should only declare simple data definitions.
@ 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.
NodeSeconds ConsumeTime(FuzzedDataProvider &fuzzed_data_provider, const std::optional< int64_t > &min, const std::optional< int64_t > &max) noexcept
uint32_t ConsumeSequence(FuzzedDataProvider &fuzzed_data_provider) noexcept
auto & PickValue(FuzzedDataProvider &fuzzed_data_provider, Collection &col)
COutPoint MineBlock(const NodeContext &node, const node::BlockCreateOptions &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