59 std::vector<CTxMemPoolEntry::CTxMemPoolEntryRef>
ret;
64 for (; iter !=
mapNextTx.end() && iter->first->hash == hash; ++iter) {
65 ret.emplace_back(*(iter->second));
69 auto removed = std::ranges::unique(
ret, [](
auto&
a,
auto& b)
noexcept {
return &
a.get() == &b.get(); });
70 ret.erase(removed.begin(), removed.end());
77 std::vector<CTxMemPoolEntry::CTxMemPoolEntryRef>
ret;
78 std::set<Txid> inputs;
80 inputs.insert(
txin.prevout.hash);
82 for (
const auto& hash : inputs) {
100 if (it ==
mapTx.end()) {
105 for (; iter !=
mapNextTx.end() && iter->first->hash == hash; ++iter) {
126 if (!entry)
return false;
134 if (ancestors.size() > 0) {
149 for (
unsigned int i = 0; i < tx.
vin.size(); i++) {
168 opts.
check_ratio = std::clamp<int>(opts.check_ratio, 0, 1'000'000);
170 if (opts.max_size_bytes < 0 || (opts.max_size_bytes > 0 && opts.max_size_bytes <
cluster_limit_bytes)) {
173 return std::move(opts);
184 const Txid& txid_a = static_cast<const CTxMemPoolEntry&>(a).GetTx().GetHash();
185 const Txid& txid_b = static_cast<const CTxMemPoolEntry&>(b).GetTx().GetHash();
186 return txid_a <=> txid_b;
213 for (
size_t i=0; i<
changeset->m_entry_vec.size(); ++i) {
220 txiter it = result.position;
239 for (
unsigned int i = 0; i < tx.
vin.size(); i++) {
277 it->GetTx().GetHash().data(),
281 std::chrono::duration_cast<std::chrono::duration<std::uint64_t>>(it->GetTime()).count()
284 for (
const CTxIn&
txin : it->GetTx().vin)
292 txns_randomized[it->idx_randomized].second->idx_randomized = it->idx_randomized;
303 cachedInnerUsage -= it->DynamicMemoryUsage();
320 return mapTx.iterator_to(entry);
328 for (
auto tx: descendants) {
347 std::vector<const TxGraph::Ref*>
to_remove;
348 while (iter !=
mapNextTx.end() && iter->first->hash ==
origTx.GetHash()) {
349 to_remove.emplace_back(&*(iter->second));
367 std::vector<const TxGraph::Ref*>
to_remove;
380 for (indexed_transaction_set::const_iterator it =
mapTx.begin(); it !=
mapTx.end(); it++) {
413 for (
const auto& tx : vtx) {
415 if (it !=
mapTx.end()) {
461 std::optional<Wtxid>
last_wtxid = std::nullopt;
489 std::set<CTxMemPoolEntry::CTxMemPoolEntryRef, CompareIteratorByHash>
setParentCheck;
490 std::set<CTxMemPoolEntry::CTxMemPoolEntryRef, CompareIteratorByHash>
setParentsStored;
493 indexed_transaction_set::const_iterator
it2 =
mapTx.find(
txin.prevout.hash);
511 return a.GetTx().GetHash() == b.GetTx().GetHash();
520 std::set<CTxMemPoolEntry::CTxMemPoolEntryRef, CompareIteratorByHash>
setChildrenCheck;
521 std::set<CTxMemPoolEntry::CTxMemPoolEntryRef, CompareIteratorByHash>
setChildrenStored;
523 for (; iter !=
mapNextTx.end() && iter->first->hash == it->GetTx().GetHash(); ++iter) {
542 indexed_transaction_set::const_iterator
it2 = it->second;
565 if (!
j.has_value())
return false;
567 if (!i.has_value())
return true;
569 return m_txgraph->CompareMainOrder(*i.value(), *
j.value()) < 0;
574 std::vector<indexed_transaction_set::const_iterator>
iters;
579 for (indexed_transaction_set::iterator
mi =
mapTx.begin();
mi !=
mapTx.end(); ++
mi) {
583 return m_txgraph->CompareMainOrder(*
a, *b) < 0;
592 std::vector<CTxMemPoolEntryRef>
ret;
595 ret.emplace_back(*it);
605 std::vector<TxMempoolInfo>
ret;
607 for (
auto it :
iters) {
617 const auto i =
mapTx.find(txid);
624 indexed_transaction_set::const_iterator i =
mapTx.find(hash);
625 if (i ==
mapTx.end())
627 return i->GetSharedTx();
637 if (it !=
mapTx.end()) {
640 it->UpdateModifiedFee(nFeeDelta);
641 m_txgraph->SetTransactionFee(*it, it->GetModifiedFee());
646 LogInfo(
"PrioritiseTransaction: %s (%sin mempool) delta cleared\n", hash.
ToString(), it ==
mapTx.end() ?
"not " :
"");
648 LogInfo(
"PrioritiseTransaction: %s (%sin mempool) fee += %s, new delta=%s\n",
650 it ==
mapTx.end() ?
"not " :
"",
660 std::map<Txid, CAmount>::const_iterator pos =
mapDeltas.find(hash);
663 const CAmount &delta = pos->second;
677 std::vector<delta_info> result;
679 for (
const auto& [txid, delta] :
mapDeltas) {
680 const auto iter{
mapTx.find(txid)};
681 const bool in_mempool{iter !=
mapTx.end()};
682 std::optional<CAmount> modified_fee;
683 if (in_mempool) modified_fee = iter->GetModifiedFee();
684 result.emplace_back(
delta_info{in_mempool, delta, modified_fee, txid});
698 auto it =
mapTx.find(txid);
699 return it !=
mapTx.end() ? std::make_optional(it) : std::nullopt;
706 return it !=
mapTx.end() ? std::make_optional(it) : std::nullopt;
712 for (
const auto& h :
hashes) {
722 std::vector<txiter>
ret;
723 ret.reserve(txids.size());
724 for (
const auto& txid : txids) {
734 for (
unsigned int i = 0; i < tx.
vin.size(); i++)
755 if (outpoint.
n <
ptx->vout.size()) {
767 for (
unsigned int n = 0; n < tx->vout.size(); ++n) {
808 return changeset->CheckMemPoolPolicyLimits();
815 indexed_transaction_set::index<entry_time>::type::iterator it =
mapTx.get<
entry_time>().begin();
883 std::vector<CTransaction> txn;
917 size_t ancestor_count = ancestors.size();
920 for (
auto tx: ancestors) {
931 size_t descendant_count = descendants.size();
935 for (
auto tx: descendants) {
945 auto it =
mapTx.find(txid);
946 ancestors = cluster_count = 0;
947 if (it !=
mapTx.end()) {
949 ancestors = ancestor_count;
972 std::vector<CTxMemPool::txiter>
ret;
974 for (
auto txid : txids) {
975 auto it =
mapTx.find(txid);
976 if (it !=
mapTx.end()) {
982 for (
auto tx : cluster) {
988 if (
ret.size() > 500) {
1002 return m_pool->m_txgraph->GetMainStagingDiagrams();
1008 Assume(m_to_add.find(tx->GetHash()) == m_to_add.end());
1009 Assume(!m_dependencies_processed);
1012 m_dependencies_processed =
false;
1015 m_pool->ApplyDelta(tx->GetHash(), delta);
1021 newit->UpdateModifiedFee(delta);
1022 m_pool->m_txgraph->SetTransactionFee(*
newit,
newit->GetModifiedFee());
1025 m_entry_vec.push_back(
newit);
1033 m_pool->m_txgraph->RemoveTransaction(*it);
1034 m_to_remove.insert(it);
1040 if (!m_dependencies_processed) {
1041 ProcessDependencies();
1043 m_pool->Apply(
this);
1045 m_to_remove.clear();
1046 m_entry_vec.clear();
1047 m_ancestors.clear();
1053 Assume(!m_dependencies_processed);
1054 for (
const auto&
entryptr : m_entry_vec) {
1055 for (
const auto &
txin :
entryptr->GetSharedTx()->vin) {
1056 std::optional<txiter>
piter = m_pool->GetIter(
txin.prevout.hash);
1058 auto it = m_to_add.find(
txin.prevout.hash);
1059 if (it != m_to_add.end()) {
1060 piter = std::make_optional(it);
1068 m_dependencies_processed =
true;
1075 if (!m_dependencies_processed) {
1076 ProcessDependencies();
1085 std::vector<FeePerWeight>
ret;
1091 std::vector<CTxMemPoolEntry::CTxMemPoolEntryRef> dummy;
int64_t CAmount
Amount in satoshis (Can be negative)
#define Assume(val)
Assume is the identity function.
An in-memory indexed chain of blocks.
CCoinsView backed by another CCoinsView.
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Abstract view on the open txout dataset.
virtual std::optional< Coin > GetCoin(const COutPoint &outpoint) const
Retrieve the Coin (unspent transaction output) for a given outpoint.
std::optional< Coin > GetCoin(const COutPoint &outpoint) const override
GetCoin, returning whether it exists and is not spent.
void Reset()
Clear m_temp_added and m_non_base_coins.
std::unordered_map< COutPoint, Coin, SaltedOutpointHasher > m_temp_added
Coins made available by transactions being validated.
CCoinsViewMemPool(CCoinsView *baseIn, const CTxMemPool &mempoolIn)
std::unordered_set< COutPoint, SaltedOutpointHasher > m_non_base_coins
Set of all coins that have been fetched from mempool or created using PackageAddTransaction (not base...
void PackageAddTransaction(const CTransactionRef &tx)
Add the coins created by this transaction.
const CTxMemPool & mempool
Fee rate in satoshis per virtualbyte: CAmount / vB the feerate is represented internally as FeeFrac.
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.
The basic transaction that is broadcasted on the network and contained in blocks.
const Wtxid & GetWitnessHash() const LIFETIMEBOUND
const Txid & GetHash() const LIFETIMEBOUND
const std::vector< CTxIn > vin
An input of a transaction.
void Apply() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
void StageRemoval(CTxMemPool::txiter it)
util::Result< std::pair< std::vector< FeeFrac >, std::vector< FeeFrac > > > CalculateChunksForRBF()
Calculate the sorted chunks for the old and new mempool relating to the clusters that would be affect...
CTxMemPool::txiter TxHandle
TxHandle StageAddition(const CTransactionRef &tx, CAmount fee, int64_t time, unsigned int entry_height, uint64_t entry_sequence, bool spends_coinbase, int64_t sigops_cost, LockPoints lp)
bool CheckMemPoolPolicyLimits()
Check if any cluster limits are exceeded.
void ProcessDependencies()
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
const CTransaction & GetTx() const
size_t DynamicMemoryUsage() const
int32_t GetTxSize() const
const CAmount & GetFee() const
CAmount GetModifiedFee() const
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
void removeConflicts(const CTransaction &tx) EXCLUSIVE_LOCKS_REQUIRED(cs)
std::atomic< unsigned int > nTransactionsUpdated
Used by getblocktemplate to trigger CreateNewBlock() invocation.
void Apply(CTxMemPool::ChangeSet *changeset) EXCLUSIVE_LOCKS_REQUIRED(cs)
void PrioritiseTransaction(const Txid &hash, const CAmount &nFeeDelta)
Affect CreateNewBlock prioritisation of transactions.
std::unique_ptr< ChangeSet > GetChangeSet() EXCLUSIVE_LOCKS_REQUIRED(cs)
bool CompareMiningScoreWithTopology(const Wtxid &hasha, const Wtxid &hashb) const
static TxMempoolInfo GetInfo(CTxMemPool::indexed_transaction_set::const_iterator it)
bool HasNoInputsOf(const CTransaction &tx) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Check that none of this transactions inputs are in the mempool, and thus the tx is not dependent on o...
setEntries GetIterSet(const std::set< Txid > &hashes) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Translate a set of hashes into a set of pool iterators to avoid repeated lookups.
void ClearPrioritisation(const Txid &hash) EXCLUSIVE_LOCKS_REQUIRED(cs)
std::optional< txiter > GetIter(const Txid &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Returns an iterator to the given hash, if found.
bool GetLoadTried() const
void StopBlockBuilding() const EXCLUSIVE_LOCKS_REQUIRED(cs)
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.
void trackPackageRemoved(const CFeeRate &rate) EXCLUSIVE_LOCKS_REQUIRED(cs)
void TrimToSize(size_t sizelimit, std::vector< COutPoint > *pvNoSpendsRemaining=nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs)
Remove transactions from the mempool until its dynamic size is <= sizelimit.
void GetTransactionAncestry(const Txid &txid, size_t &ancestors, size_t &cluster_count, size_t *ancestorsize=nullptr, CAmount *ancestorfees=nullptr) const
Calculate the ancestor and cluster count for the given transaction.
void UpdateTransactionsFromBlock(const std::vector< Txid > &vHashesToUpdate) EXCLUSIVE_LOCKS_REQUIRED(cs
UpdateTransactionsFromBlock is called when adding transactions from a disconnected block back to the ...
void AddTransactionsUpdated(unsigned int n)
bool HasDescendants(const Txid &txid) const
std::vector< indexed_transaction_set::const_iterator > GetSortedScoreWithTopology() const EXCLUSIVE_LOCKS_REQUIRED(cs)
void StartBlockBuilding() const EXCLUSIVE_LOCKS_REQUIRED(cs)
CTransactionRef get(const Txid &hash) const
size_t DynamicMemoryUsage() const
std::vector< TxMempoolInfo > infoAll() const
CTxMemPool(Options opts, bilingual_str &error)
Create a new CTxMemPool.
void addNewTransaction(CTxMemPool::txiter it) EXCLUSIVE_LOCKS_REQUIRED(cs)
void removeUnchecked(txiter entry, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
int Expire(std::chrono::seconds time) EXCLUSIVE_LOCKS_REQUIRED(cs)
Expire all transaction (and their dependencies) in the mempool older than time.
void IncludeBuilderChunk() const EXCLUSIVE_LOCKS_REQUIRED(cs)
void removeForReorg(CChain &chain, std::function< bool(txiter)> filter_final_and_mature) EXCLUSIVE_LOCKS_REQUIRED(cs
After reorg, filter the entries that would no longer be valid in the next block, and update the entri...
std::vector< FeePerWeight > GetFeerateDiagram() const EXCLUSIVE_LOCKS_REQUIRED(cs)
std::tuple< size_t, size_t, CAmount > CalculateDescendantData(const CTxMemPoolEntry &entry) const EXCLUSIVE_LOCKS_REQUIRED(cs)
bool exists(const Txid &txid) const
std::vector< txiter > GetIterVec(const std::vector< Txid > &txids) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Translate a list of hashes into a list of mempool iterators to avoid repeated lookups.
static const int ROLLING_FEE_HALFLIFE
std::set< txiter, CompareIteratorByHash > setEntries
std::vector< CTxMemPoolEntry::CTxMemPoolEntryRef > GetParents(const CTxMemPoolEntry &entry) const
void ApplyDelta(const Txid &hash, CAmount &nFeeDelta) const EXCLUSIVE_LOCKS_REQUIRED(cs)
void removeForBlock(const std::vector< CTransactionRef > &vtx, unsigned int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(cs)
std::vector< delta_info > GetPrioritisedTransactions() const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Return a vector of all entries in mapDeltas with their corresponding delta_info.
std::vector< txiter > GatherClusters(const std::vector< Txid > &txids) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Collect the entire cluster of connected transactions for each transaction in txids.
void check(const CCoinsViewCache &active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(voi removeRecursive)(const CTransaction &tx, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
If sanity-checking is turned on, check makes sure the pool is consistent (does not contain two transa...
indexed_transaction_set::nth_index< 0 >::type::const_iterator txiter
uint64_t GetAndIncrementSequence() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Guards this internal counter for external reporting.
bool CheckPolicyLimits(const CTransactionRef &tx)
const CTransaction * GetConflictTx(const COutPoint &prevout) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Get the transaction in the pool that spends the same prevout.
void CalculateDescendants(txiter it, setEntries &setDescendants) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Populate setDescendants with all in-mempool descendants of given transaction.
std::tuple< size_t, size_t, CAmount > CalculateAncestorData(const CTxMemPoolEntry &entry) const EXCLUSIVE_LOCKS_REQUIRED(cs)
std::vector< CTxMemPoolEntry::CTxMemPoolEntryRef > GetChildren(const CTxMemPoolEntry &entry) const
setEntries CalculateMemPoolAncestors(const CTxMemPoolEntry &entry) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Calculate all in-mempool ancestors of entry (not including the tx itself)
void RemoveUnbroadcastTx(const Txid &txid, bool unchecked=false)
Removes a transaction from the unbroadcast set.
void SetLoadTried(bool load_tried)
Set whether or not an initial attempt to load the persisted mempool was made (regardless of whether t...
void RemoveStaged(setEntries &stage, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
Remove a set of transactions from the mempool.
std::vector< CTxMemPoolEntryRef > entryAll() const EXCLUSIVE_LOCKS_REQUIRED(cs)
bool isSpent(const COutPoint &outpoint) const
FeePerWeight GetBlockBuilderChunk(std::vector< CTxMemPoolEntry::CTxMemPoolEntryRef > &entries) const EXCLUSIVE_LOCKS_REQUIRED(cs)
const CTxMemPoolEntry * GetEntry(const Txid &txid) const LIFETIMEBOUND EXCLUSIVE_LOCKS_REQUIRED(cs)
unsigned int GetTransactionsUpdated() const
@ MAIN
Always refers to the main graph, whether staging is present or not.
@ TOP
Refers to staging if it exists, main otherwise.
void MempoolTransactionsRemovedForBlock(const std::vector< RemovedMempoolTransactionInfo > &, unsigned int nBlockHeight)
void TransactionRemovedFromMempool(const CTransactionRef &, MemPoolRemovalReason, uint64_t mempool_sequence)
std::string ToString() const
std::string GetHex() const
constexpr const std::byte * data() const
void AddCoins(CCoinsViewCache &cache, const CTransaction &tx, int nHeight, bool check_for_overwrite)
Utility function to add all of a transaction's outputs to a cache.
static int32_t GetTransactionWeight(const CTransaction &tx)
static const int WITNESS_SCALE_FACTOR
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
#define LogDebug(category,...)
std::string RemovalReasonToString(const MemPoolRemovalReason &r) noexcept
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal.
@ SIZELIMIT
Removed in size limiting.
@ BLOCK
Removed for block.
@ EXPIRY
Expired from mempool.
@ REPLACED
Removed for replacement.
@ CONFLICT
Removed for conflict with in-block transaction.
@ REORG
Removed for reorganization.
std::string FormatMoney(const CAmount n)
Money parsing/formatting utilities.
bool CheckTxInputs(const CTransaction &tx, TxValidationState &state, const CCoinsViewCache &inputs, int nSpendHeight, CAmount &txfee)
Check whether all inputs of this transaction are valid (no double spends and amounts) This does not m...
static size_t DynamicUsage(const int8_t &v)
Dynamic memory usage for built-in types is zero.
static size_t MallocUsage(size_t alloc)
Compute the total memory used by allocating alloc bytes.
T SaturatingAdd(const T i, const T j) noexcept
unsigned int nBytesPerSigOp
int64_t GetSigOpsAdjustedWeight(int64_t weight, int64_t sigop_cost, unsigned int bytes_per_sigop)
static FeePerVSize ToFeePerVSize(FeePerWeight feerate)
std::shared_ptr< const CTransaction > CTransactionRef
CBlockIndex * maxInputBlock
unsigned cluster_count
The maximum number of transactions in a cluster.
int64_t cluster_size_vbytes
The maximum allowed size in virtual bytes of a cluster.
Options struct containing options for constructing a CTxMemPool.
ValidationSignals * signals
CFeeRate incremental_relay_feerate
#define AssertLockNotHeld(cs)
#define AssertLockHeld(cs)
#define EXCLUSIVE_LOCKS_REQUIRED(...)
#define TRACEPOINT(context,...)
#define TRACEPOINT_SEMAPHORE(context, event)
consteval auto _(util::TranslatedLiteral str)
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
std::unique_ptr< TxGraph > MakeTxGraph(unsigned max_cluster_count, uint64_t max_cluster_size, uint64_t acceptable_cost, const std::function< std::strong_ordering(const TxGraph::Ref &, const TxGraph::Ref &)> &fallback_order) noexcept
Construct a new TxGraph with the specified limit on the number of transactions within a cluster,...
static CTxMemPool::Options && Flatten(CTxMemPool::Options &&opts, bilingual_str &error)
bool TestLockPointValidity(CChain &active_chain, const LockPoints &lp)
Test whether the LockPoints height and time are still valid on the current chain.
static constexpr uint64_t ACCEPTABLE_COST
How much linearization cost required for TxGraph clusters to have "acceptable" quality,...
static constexpr uint64_t POST_CHANGE_COST
How much work we ask TxGraph to do after a mempool change occurs (either due to a changeset being app...
static const uint32_t MEMPOOL_HEIGHT
Fake height value used in Coin to signify they are only in the memory pool (since 0....
bool TestLockPointValidity(CChain &active_chain, const LockPoints &lp) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Test whether the LockPoints height and time are still valid on the current chain.
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.