Bitcoin Core 30.99.0
P2P Digital Currency
txmempool.h
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2022 The Bitcoin Core developers
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6#ifndef BITCOIN_TXMEMPOOL_H
7#define BITCOIN_TXMEMPOOL_H
8
9#include <coins.h>
10#include <consensus/amount.h>
11#include <indirectmap.h>
12#include <kernel/cs_main.h>
13#include <kernel/mempool_entry.h> // IWYU pragma: export
14#include <kernel/mempool_limits.h> // IWYU pragma: export
15#include <kernel/mempool_options.h> // IWYU pragma: export
16#include <kernel/mempool_removal_reason.h> // IWYU pragma: export
17#include <policy/feerate.h>
18#include <policy/packages.h>
21#include <sync.h>
22#include <txgraph.h>
23#include <util/epochguard.h>
24#include <util/feefrac.h>
25#include <util/hasher.h>
26#include <util/result.h>
27
28#include <boost/multi_index/hashed_index.hpp>
29#include <boost/multi_index/identity.hpp>
30#include <boost/multi_index/indexed_by.hpp>
31#include <boost/multi_index/ordered_index.hpp>
32#include <boost/multi_index/sequenced_index.hpp>
33#include <boost/multi_index/tag.hpp>
34#include <boost/multi_index_container.hpp>
35
36#include <atomic>
37#include <map>
38#include <optional>
39#include <set>
40#include <string>
41#include <string_view>
42#include <utility>
43#include <vector>
44
45class CChain;
47
48struct bilingual_str;
49
51static const uint32_t MEMPOOL_HEIGHT = 0x7FFFFFFF;
52
56static constexpr uint64_t ACCEPTABLE_ITERS = 1'700;
57
60static constexpr uint64_t POST_CHANGE_WORK = 5 * ACCEPTABLE_ITERS;
61
66
67// extracts a transaction hash from CTxMemPoolEntry or CTransactionRef
69{
72 {
73 return entry.GetTx().GetHash();
74 }
75
77 {
78 return tx->GetHash();
79 }
80};
81
82// extracts a transaction witness-hash from CTxMemPoolEntry or CTransactionRef
84{
87 {
88 return entry.GetTx().GetWitnessHash();
89 }
90
92 {
93 return tx->GetWitnessHash();
94 }
95};
96
98{
99public:
100 bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
101 {
102 return a.GetTime() < b.GetTime();
103 }
104};
105
106// Multi_index tag names
107struct entry_time {};
109
114{
117
119 std::chrono::seconds m_time;
120
123
125 int32_t vsize;
126
128 int64_t nFeeDelta;
129};
130
189{
190protected:
191 std::atomic<unsigned int> nTransactionsUpdated{0};
192
193 uint64_t totalTxSize GUARDED_BY(cs){0};
194 CAmount m_total_fee GUARDED_BY(cs){0};
195 uint64_t cachedInnerUsage GUARDED_BY(cs){0};
196
197 mutable int64_t lastRollingFeeUpdate GUARDED_BY(cs){GetTime()};
198 mutable bool blockSinceLastRollingFeeBump GUARDED_BY(cs){false};
199 mutable double rollingMinimumFeeRate GUARDED_BY(cs){0};
201
202 // In-memory counter for external mempool tracking purposes.
203 // This number is incremented once every time a transaction
204 // is added or removed from the mempool for any reason.
205 mutable uint64_t m_sequence_number GUARDED_BY(cs){1};
206
208
209 bool m_load_tried GUARDED_BY(cs){false};
210
211 CFeeRate GetMinFee(size_t sizelimit) const;
212
213public:
214
215 static const int ROLLING_FEE_HALFLIFE = 60 * 60 * 12; // public only for testing
216
217 struct CTxMemPoolEntry_Indices final : boost::multi_index::indexed_by<
218 // sorted by txid
219 boost::multi_index::hashed_unique<mempoolentry_txid, SaltedTxidHasher>,
220 // sorted by wtxid
221 boost::multi_index::hashed_unique<
222 boost::multi_index::tag<index_by_wtxid>,
223 mempoolentry_wtxid,
224 SaltedWtxidHasher
225 >,
226 // sorted by entry time
227 boost::multi_index::ordered_non_unique<
228 boost::multi_index::tag<entry_time>,
229 boost::multi_index::identity<CTxMemPoolEntry>,
230 CompareTxMemPoolEntryByEntryTime
231 >
232 >
233 {};
234 typedef boost::multi_index_container<
238
264 std::unique_ptr<TxGraph> m_txgraph GUARDED_BY(cs);
265 mutable std::unique_ptr<TxGraph::BlockBuilder> m_builder GUARDED_BY(cs);
267
268 using txiter = indexed_transaction_set::nth_index<0>::type::const_iterator;
269 std::vector<std::pair<Wtxid, txiter>> txns_randomized GUARDED_BY(cs);
270
271 typedef std::set<txiter, CompareIteratorByHash> setEntries;
272
274
275 std::tuple<size_t, size_t, CAmount> CalculateAncestorData(const CTxMemPoolEntry& entry) const EXCLUSIVE_LOCKS_REQUIRED(cs);
276 std::tuple<size_t, size_t, CAmount> CalculateDescendantData(const CTxMemPoolEntry& entry) const EXCLUSIVE_LOCKS_REQUIRED(cs);
277 int64_t GetDescendantCount(txiter it) const { LOCK(cs); return m_txgraph->GetDescendants(*it, TxGraph::Level::MAIN).size(); }
278 int64_t GetDescendantCount(const CTxMemPoolEntry &e) const { LOCK(cs); return m_txgraph->GetDescendants(e, TxGraph::Level::MAIN).size(); }
279 int64_t GetAncestorCount(const CTxMemPoolEntry &e) const { LOCK(cs); return m_txgraph->GetAncestors(e, TxGraph::Level::MAIN).size(); }
280 std::vector<CTxMemPoolEntry::CTxMemPoolEntryRef> GetChildren(const CTxMemPoolEntry &entry) const;
281 std::vector<CTxMemPoolEntry::CTxMemPoolEntryRef> GetParents(const CTxMemPoolEntry &entry) const;
282
283private:
284 std::vector<indexed_transaction_set::const_iterator> GetSortedScoreWithTopology() const EXCLUSIVE_LOCKS_REQUIRED(cs);
285
289 std::set<Txid> m_unbroadcast_txids GUARDED_BY(cs);
290
292 {
293 return TxMempoolInfo{it->GetSharedTx(), it->GetTime(), it->GetFee(), it->GetTxSize(), it->GetModifiedFee() - it->GetFee()};
294 }
295
296 // Helper to remove all transactions that conflict with a given
297 // transaction (used for transactions appearing in a block).
299
300public:
302 std::map<Txid, CAmount> mapDeltas GUARDED_BY(cs);
303
305
307
313 explicit CTxMemPool(Options opts, bilingual_str& error);
314
321 void check(const CCoinsViewCache& active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
322
336 void removeForReorg(CChain& chain, std::function<bool(txiter)> filter_final_and_mature) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main);
337 void removeForBlock(const std::vector<CTransactionRef>& vtx, unsigned int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(cs);
338
339 bool CompareMiningScoreWithTopology(const Wtxid& hasha, const Wtxid& hashb) const;
340 bool isSpent(const COutPoint& outpoint) const;
341 unsigned int GetTransactionsUpdated() const;
342 void AddTransactionsUpdated(unsigned int n);
348
350 void PrioritiseTransaction(const Txid& hash, const CAmount& nFeeDelta);
351 void ApplyDelta(const Txid& hash, CAmount &nFeeDelta) const EXCLUSIVE_LOCKS_REQUIRED(cs);
353
354 struct delta_info {
356 const bool in_mempool;
360 std::optional<CAmount> modified_fee;
362 const Txid txid;
363 };
365 std::vector<delta_info> GetPrioritisedTransactions() const EXCLUSIVE_LOCKS_REQUIRED(!cs);
366
368 const CTransaction* GetConflictTx(const COutPoint& prevout) const EXCLUSIVE_LOCKS_REQUIRED(cs);
369
371 std::optional<txiter> GetIter(const Txid& txid) const EXCLUSIVE_LOCKS_REQUIRED(cs);
372 std::optional<txiter> GetIter(const Wtxid& wtxid) const EXCLUSIVE_LOCKS_REQUIRED(cs);
373
377 setEntries GetIterSet(const std::set<Txid>& hashes) const EXCLUSIVE_LOCKS_REQUIRED(cs);
378
382 std::vector<txiter> GetIterVec(const std::vector<Txid>& txids) const EXCLUSIVE_LOCKS_REQUIRED(cs);
383
398
401 return m_txgraph->GetMainChunkFeerate(tx);
402 }
403 std::vector<const CTxMemPoolEntry*> GetCluster(Txid txid) const EXCLUSIVE_LOCKS_REQUIRED(cs) {
404 auto tx = GetIter(txid);
405 if (!tx) return {};
406 auto cluster = m_txgraph->GetCluster(**tx, TxGraph::Level::MAIN);
407 std::vector<const CTxMemPoolEntry*> ret;
408 ret.reserve(cluster.size());
409 for (const auto& tx : cluster) {
410 ret.emplace_back(static_cast<const CTxMemPoolEntry*>(tx));
411 }
412 return ret;
413 }
414
415
416 size_t GetUniqueClusterCount(const setEntries& iters_conflicting) const EXCLUSIVE_LOCKS_REQUIRED(cs) {
417 std::vector<const TxGraph::Ref *> entries;
418 entries.reserve(iters_conflicting.size());
419 for (auto it : iters_conflicting) {
420 entries.emplace_back(&*it);
421 }
422 Assume(!m_txgraph->IsOversized(TxGraph::Level::MAIN));
423 return m_txgraph->CountDistinctClusters(entries, TxGraph::Level::MAIN);
424 }
425
434
435 bool HasDescendants(const Txid& txid) const;
436
441 std::vector<txiter> GatherClusters(const std::vector<Txid>& txids) const EXCLUSIVE_LOCKS_REQUIRED(cs);
442
448
457 }
458
463 void TrimToSize(size_t sizelimit, std::vector<COutPoint>* pvNoSpendsRemaining = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
464
466 int Expire(std::chrono::seconds time) EXCLUSIVE_LOCKS_REQUIRED(cs);
467
474 void GetTransactionAncestry(const Txid& txid, size_t& ancestors, size_t& cluster_count, size_t* ancestorsize = nullptr, CAmount* ancestorfees = nullptr) const;
475
480 bool GetLoadTried() const;
481
486 void SetLoadTried(bool load_tried);
487
488 unsigned long size() const
489 {
490 LOCK(cs);
491 return mapTx.size();
492 }
493
495 {
497 return totalTxSize;
498 }
499
501 {
503 return m_total_fee;
504 }
505
506 bool exists(const Txid& txid) const
507 {
508 LOCK(cs);
509 return (mapTx.count(txid) != 0);
510 }
511
512 bool exists(const Wtxid& wtxid) const
513 {
514 LOCK(cs);
515 return (mapTx.get<index_by_wtxid>().count(wtxid) != 0);
516 }
517
519
520 CTransactionRef get(const Txid& hash) const;
521
522 template <TxidOrWtxid T>
523 TxMempoolInfo info(const T& id) const
524 {
525 LOCK(cs);
526 auto i{GetIter(id)};
527 return i.has_value() ? GetInfo(*i) : TxMempoolInfo{};
528 }
529
531 template <TxidOrWtxid T>
532 TxMempoolInfo info_for_relay(const T& id, uint64_t last_sequence) const
533 {
534 LOCK(cs);
535 auto i{GetIter(id)};
536 return (i.has_value() && i.value()->GetSequence() < last_sequence) ? GetInfo(*i) : TxMempoolInfo{};
537 }
538
539 std::vector<CTxMemPoolEntryRef> entryAll() const EXCLUSIVE_LOCKS_REQUIRED(cs);
540 std::vector<TxMempoolInfo> infoAll() const;
541
542 size_t DynamicMemoryUsage() const;
543
545 void AddUnbroadcastTx(const Txid& txid)
546 {
547 LOCK(cs);
548 // Sanity check the transaction is in the mempool & insert into
549 // unbroadcast set.
550 if (exists(txid)) m_unbroadcast_txids.insert(txid);
551 };
552
553 bool CheckPolicyLimits(const CTransactionRef& tx);
554
556 void RemoveUnbroadcastTx(const Txid& txid, const bool unchecked = false);
557
559 std::set<Txid> GetUnbroadcastTxs() const
560 {
561 LOCK(cs);
562 return m_unbroadcast_txids;
563 }
564
567 {
569 return m_unbroadcast_txids.count(txid) != 0;
570 }
571
574 return m_sequence_number++;
575 }
576
578 return m_sequence_number;
579 }
580
581private:
588
589 /* Helper for the public removeRecursive() */
591
601public:
611 {
612 return m_epoch.visited(it->m_epoch_marker);
613 }
614
615 bool visited(std::optional<txiter> it) const EXCLUSIVE_LOCKS_REQUIRED(cs, m_epoch)
616 {
617 assert(m_epoch.guarded()); // verify guard even when it==nullopt
618 return !it || visited(*it);
619 }
620
621 /*
622 * CTxMemPool::ChangeSet:
623 *
624 * This class is used for all mempool additions and associated removals (eg
625 * due to rbf). Removals that don't need to be evaluated for acceptance,
626 * such as removing transactions that appear in a block, or due to reorg,
627 * or removals related to mempool limiting or expiry do not need to use
628 * this.
629 *
630 * Callers can interleave calls to StageAddition()/StageRemoval(), and
631 * removals may be invoked in any order, but additions must be done in a
632 * topological order in the case of transaction packages (ie, parents must
633 * be added before children).
634 *
635 * CalculateChunksForRBF() can be used to calculate the feerate diagram of
636 * the proposed set of new transactions and compare with the existing
637 * mempool.
638 *
639 * CalculateMemPoolAncestors() calculates the in-mempool (not including
640 * what is in the change set itself) ancestors of a given transaction.
641 *
642 * Apply() will apply the removals and additions that are staged into the
643 * mempool.
644 *
645 * Only one changeset may exist at a time. While a changeset is
646 * outstanding, no removals or additions may be made directly to the
647 * mempool.
648 */
649 class ChangeSet {
650 public:
651 explicit ChangeSet(CTxMemPool* pool) : m_pool(pool) { m_pool->m_txgraph->StartStaging(); }
654 if (m_pool->m_txgraph->HaveStaging()) {
655 m_pool->m_txgraph->AbortStaging();
656 }
657 m_pool->m_have_changeset = false;
658 }
659
660 ChangeSet(const ChangeSet&) = delete;
661 ChangeSet& operator=(const ChangeSet&) = delete;
662
664
665 TxHandle StageAddition(const CTransactionRef& tx, const CAmount fee, int64_t time, unsigned int entry_height, uint64_t entry_sequence, bool spends_coinbase, int64_t sigops_cost, LockPoints lp);
666
668
670
673
675 {
676 // Look up transaction in our cache first
677 auto it = m_ancestors.find(tx);
678 if (it != m_ancestors.end()) return it->second;
679
680 // If not found, try to have the mempool calculate it, and cache
681 // for later.
682 LOCK(m_pool->cs);
684 m_ancestors.try_emplace(tx, ret);
685 return ret;
686 }
687
688 std::vector<CTransactionRef> GetAddedTxns() const {
689 std::vector<CTransactionRef> ret;
690 ret.reserve(m_entry_vec.size());
691 for (const auto& entry : m_entry_vec) {
692 ret.emplace_back(entry->GetSharedTx());
693 }
694 return ret;
695 }
696
704
705 size_t GetTxCount() const { return m_entry_vec.size(); }
706 const CTransaction& GetAddedTxn(size_t index) const { return m_entry_vec.at(index)->GetTx(); }
707
709
710 private:
711 void ProcessDependencies();
712
715 std::vector<CTxMemPool::txiter> m_entry_vec; // track the added transactions' insertion order
716 // map from the m_to_add index to the ancestors for the transaction
720
721 friend class CTxMemPool;
722 };
723
724 std::unique_ptr<ChangeSet> GetChangeSet() EXCLUSIVE_LOCKS_REQUIRED(cs) {
725 Assume(!m_have_changeset);
726 m_have_changeset = true;
727 return std::make_unique<ChangeSet>(this);
728 }
729
730 bool m_have_changeset GUARDED_BY(cs){false};
731
733
734private:
735 // Apply the given changeset to the mempool, by removing transactions in
736 // the to_remove set and adding transactions in the to_add set.
738
739 // addNewTransaction must update state for all ancestors of a given transaction,
740 // to track size/count of descendant transactions. First version of
741 // addNewTransaction can be used to have it call CalculateMemPoolAncestors(), and
742 // then invoke the second version.
743 // Note that addNewTransaction is ONLY called (via Apply()) from ATMP
744 // outside of tests and any other callers may break wallet's in-mempool
745 // tracking (due to lack of CValidationInterface::TransactionAddedToMempool
746 // callbacks).
748public:
749 void StartBlockBuilding() const EXCLUSIVE_LOCKS_REQUIRED(cs) { assert(!m_builder); m_builder = m_txgraph->GetBlockBuilder(); }
750 FeePerWeight GetBlockBuilderChunk(std::vector<CTxMemPoolEntry::CTxMemPoolEntryRef>& entries) const EXCLUSIVE_LOCKS_REQUIRED(cs)
751 {
752 if (!m_builder) { return {}; }
753
754 auto res = m_builder->GetCurrentChunk();
755 if (!res) { return {}; }
756
757 auto [chunk_entries, chunk_feerate] = *res;
758 for (TxGraph::Ref* ref : chunk_entries) {
759 entries.emplace_back(static_cast<const CTxMemPoolEntry&>(*ref));
760 }
761 return chunk_feerate;
762 }
763 void IncludeBuilderChunk() const EXCLUSIVE_LOCKS_REQUIRED(cs) { m_builder->Include(); }
764 void SkipBuilderChunk() const EXCLUSIVE_LOCKS_REQUIRED(cs) { m_builder->Skip(); }
765 void StopBlockBuilding() const EXCLUSIVE_LOCKS_REQUIRED(cs) { m_builder.reset(); }
766};
767
782{
787 std::unordered_map<COutPoint, Coin, SaltedOutpointHasher> m_temp_added;
788
793 mutable std::unordered_set<COutPoint, SaltedOutpointHasher> m_non_base_coins;
794protected:
796
797public:
798 CCoinsViewMemPool(CCoinsView* baseIn, const CTxMemPool& mempoolIn);
801 std::optional<Coin> GetCoin(const COutPoint& outpoint) const override;
806 const std::unordered_set<COutPoint, SaltedOutpointHasher>& GetNonBaseCoins() const { return m_non_base_coins; }
808 void Reset();
809};
810#endif // BITCOIN_TXMEMPOOL_H
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
#define LIFETIMEBOUND
Definition: attributes.h:16
int ret
#define Assume(val)
Assume is the identity function.
Definition: check.h:125
An in-memory indexed chain of blocks.
Definition: chain.h:381
CCoinsView backed by another CCoinsView.
Definition: coins.h:342
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:361
Abstract view on the open txout dataset.
Definition: coins.h:308
CCoinsView that brings transactions from a mempool into view.
Definition: txmempool.h:782
std::optional< Coin > GetCoin(const COutPoint &outpoint) const override
GetCoin, returning whether it exists and is not spent.
Definition: txmempool.cpp:725
void Reset()
Clear m_temp_added and m_non_base_coins.
Definition: txmempool.cpp:755
const std::unordered_set< COutPoint, SaltedOutpointHasher > & GetNonBaseCoins() const
Get all coins in m_non_base_coins.
Definition: txmempool.h:806
std::unordered_map< COutPoint, Coin, SaltedOutpointHasher > m_temp_added
Coins made available by transactions being validated.
Definition: txmempool.h:787
CCoinsViewMemPool(CCoinsView *baseIn, const CTxMemPool &mempoolIn)
Definition: txmempool.cpp:723
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...
Definition: txmempool.h:793
void PackageAddTransaction(const CTransactionRef &tx)
Add the coins created by this transaction.
Definition: txmempool.cpp:748
const CTxMemPool & mempool
Definition: txmempool.h:795
Fee rate in satoshis per virtualbyte: CAmount / vB the feerate is represented internally as FeeFrac.
Definition: feerate.h:35
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:29
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:281
const Wtxid & GetWitnessHash() const LIFETIMEBOUND
Definition: transaction.h:329
const Txid & GetHash() const LIFETIMEBOUND
Definition: transaction.h:328
size_t GetTxCount() const
Definition: txmempool.h:705
const CTransaction & GetAddedTxn(size_t index) const
Definition: txmempool.h:706
CTxMemPool::setEntries m_to_remove
Definition: txmempool.h:718
ChangeSet(CTxMemPool *pool)
Definition: txmempool.h:651
void Apply() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Definition: txmempool.cpp:1019
CTxMemPool * m_pool
Definition: txmempool.h:713
void StageRemoval(CTxMemPool::txiter it)
Definition: txmempool.cpp:1012
TxHandle StageAddition(const CTransactionRef &tx, const CAmount fee, int64_t time, unsigned int entry_height, uint64_t entry_sequence, bool spends_coinbase, int64_t sigops_cost, LockPoints lp)
Definition: txmempool.cpp:988
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...
Definition: txmempool.cpp:977
CTxMemPool::txiter TxHandle
Definition: txmempool.h:663
CTxMemPool::indexed_transaction_set m_to_add
Definition: txmempool.h:714
~ChangeSet() EXCLUSIVE_LOCKS_REQUIRED(m_pool -> cs)
Definition: txmempool.h:652
ChangeSet(const ChangeSet &)=delete
const CTxMemPool::setEntries & GetRemovals() const
Definition: txmempool.h:669
CTxMemPool::setEntries CalculateMemPoolAncestors(TxHandle tx)
Definition: txmempool.h:674
bool CheckMemPoolPolicyLimits()
Check if any cluster limits are exceeded.
Definition: txmempool.cpp:1054
std::map< CTxMemPool::txiter, CTxMemPool::setEntries, CompareIteratorByHash > m_ancestors
Definition: txmempool.h:717
std::vector< CTransactionRef > GetAddedTxns() const
Definition: txmempool.h:688
ChangeSet & operator=(const ChangeSet &)=delete
std::vector< CTxMemPool::txiter > m_entry_vec
Definition: txmempool.h:715
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: mempool_entry.h:67
std::chrono::seconds GetTime() const
const CTransaction & GetTx() const
CTxMemPool stores valid-according-to-the-current-best-chain transactions that may be included in the ...
Definition: txmempool.h:189
void removeConflicts(const CTransaction &tx) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:373
std::atomic< unsigned int > nTransactionsUpdated
Used by getblocktemplate to trigger CreateNewBlock() invocation.
Definition: txmempool.h:191
void Apply(CTxMemPool::ChangeSet *changeset) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:195
void PrioritiseTransaction(const Txid &hash, const CAmount &nFeeDelta)
Affect CreateNewBlock prioritisation of transactions.
Definition: txmempool.cpp:613
std::unique_ptr< ChangeSet > GetChangeSet() EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:724
bool CompareMiningScoreWithTopology(const Wtxid &hasha, const Wtxid &hashb) const
Definition: txmempool.cpp:539
std::map< Txid, CAmount > mapDeltas GUARDED_BY(cs)
static TxMempoolInfo GetInfo(CTxMemPool::indexed_transaction_set::const_iterator it)
Definition: txmempool.h:291
TxMempoolInfo info_for_relay(const T &id, uint64_t last_sequence) const
Returns info for a transaction if its entry_sequence < last_sequence.
Definition: txmempool.h:532
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...
Definition: txmempool.cpp:715
std::vector< const CTxMemPoolEntry * > GetCluster(Txid txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:403
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.
Definition: txmempool.cpp:692
void ClearPrioritisation(const Txid &hash) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:650
uint64_t cachedInnerUsage GUARDED_BY(cs)
sum of all mempool tx's fees (NOT modified fee)
Definition: txmempool.h:195
std::optional< txiter > GetIter(const Txid &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Returns an iterator to the given hash, if found.
Definition: txmempool.cpp:678
bool GetLoadTried() const
Definition: txmempool.cpp:939
bool visited(const txiter it) const EXCLUSIVE_LOCKS_REQUIRED(cs
visited marks a CTxMemPoolEntry as having been traversed during the lifetime of the most recently cre...
void StopBlockBuilding() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:765
bool exists(const Wtxid &wtxid) const
Definition: txmempool.h:512
bool visited(std::optional< txiter > it) 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...
Definition: txmempool.h:455
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:263
void trackPackageRemoved(const CFeeRate &rate) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:836
std::vector< std::pair< Wtxid, txiter > > txns_randomized GUARDED_BY(cs)
All transactions in mapTx with their wtxids, in arbitrary order.
void check(const CCoinsViewCache &active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(void 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...
Definition: txmempool.h:328
bool blockSinceLastRollingFeeBump GUARDED_BY(cs)
Definition: txmempool.h:198
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.
Definition: txmempool.cpp:844
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.
Definition: txmempool.cpp:926
void UpdateTransactionsFromBlock(const std::vector< Txid > &vHashesToUpdate) EXCLUSIVE_LOCKS_REQUIRED(cs
UpdateTransactionsFromBlock is called when adding transactions from a disconnected block back to the ...
Definition: txmempool.cpp:88
return !it visited * it
Definition: txmempool.h:618
void AddTransactionsUpdated(unsigned int n)
Definition: txmempool.cpp:190
bool HasDescendants(const Txid &txid) const
Definition: txmempool.cpp:119
std::vector< indexed_transaction_set::const_iterator > GetSortedScoreWithTopology() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:555
void StartBlockBuilding() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:749
CTransactionRef get(const Txid &hash) const
Definition: txmempool.cpp:604
bool m_have_changeset GUARDED_BY(cs)
Definition: txmempool.h:730
void cs_main LOCKS_EXCLUDED(m_epoch)
size_t GetUniqueClusterCount(const setEntries &iters_conflicting) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:416
size_t DynamicMemoryUsage() const
Definition: txmempool.cpp:761
const Options m_opts
Definition: txmempool.h:306
Epoch m_epoch GUARDED_BY(cs)
minimum fee to get into the pool, decreases exponentially
Definition: txmempool.h:200
std::vector< TxMempoolInfo > infoAll() const
Definition: txmempool.cpp:583
indexed_transaction_set mapTx GUARDED_BY(cs)
TxMempoolInfo info(const T &id) const
Definition: txmempool.h:523
CTxMemPool(Options opts, bilingual_str &error)
Create a new CTxMemPool.
Definition: txmempool.cpp:173
void addNewTransaction(CTxMemPool::txiter it) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:216
void removeUnchecked(txiter entry, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
Before calling removeUnchecked for a given transaction, UpdateForRemoveFromMempool must be called on ...
Definition: txmempool.cpp:250
uint64_t totalTxSize GUARDED_BY(cs)
Definition: txmempool.h:193
boost::multi_index_container< CTxMemPoolEntry, CTxMemPoolEntry_Indices > indexed_transaction_set
Definition: txmempool.h:237
void RemoveUnbroadcastTx(const Txid &txid, const bool unchecked=false)
Removes a transaction from the unbroadcast set.
Definition: txmempool.cpp:767
int Expire(std::chrono::seconds time) EXCLUSIVE_LOCKS_REQUIRED(cs)
Expire all transaction (and their dependencies) in the mempool older than time.
Definition: txmempool.cpp:794
int64_t GetDescendantCount(txiter it) const
Definition: txmempool.h:277
void IncludeBuilderChunk() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:763
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...
Definition: txmempool.cpp:347
std::vector< FeePerWeight > GetFeerateDiagram() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:1064
std::tuple< size_t, size_t, CAmount > CalculateDescendantData(const CTxMemPoolEntry &entry) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:911
bool exists(const Txid &txid) const
Definition: txmempool.h:506
int64_t lastRollingFeeUpdate GUARDED_BY(cs)
sum of dynamic memory usage of all the map elements (NOT the maps themselves)
Definition: txmempool.h:197
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.
Definition: txmempool.cpp:702
bool m_load_tried GUARDED_BY(cs)
Definition: txmempool.h:209
static const int ROLLING_FEE_HALFLIFE
Definition: txmempool.h:215
std::set< txiter, CompareIteratorByHash > setEntries
Definition: txmempool.h:271
std::vector< CTxMemPoolEntry::CTxMemPoolEntryRef > GetParents(const CTxMemPoolEntry &entry) const
Definition: txmempool.cpp:71
FeePerWeight GetMainChunkFeerate(const CTxMemPoolEntry &tx) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:400
void ApplyDelta(const Txid &hash, CAmount &nFeeDelta) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:640
void removeForBlock(const std::vector< CTransactionRef > &vtx, unsigned int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:390
std::vector< delta_info > GetPrioritisedTransactions() const EXCLUSIVE_LOCKS_REQUIRED(!cs)
Return a vector of all entries in mapDeltas with their corresponding delta_info.
Definition: txmempool.cpp:656
uint64_t GetSequence() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:577
int64_t GetAncestorCount(const CTxMemPoolEntry &e) const
Definition: txmempool.h:279
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.
Definition: txmempool.cpp:951
indexed_transaction_set::nth_index< 0 >::type::const_iterator txiter
Definition: txmempool.h:268
uint64_t GetAndIncrementSequence() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Guards this internal counter for external reporting.
Definition: txmempool.h:573
bool CheckPolicyLimits(const CTransactionRef &tx)
Definition: txmempool.cpp:783
double rollingMinimumFeeRate GUARDED_BY(cs)
Definition: txmempool.h:199
bool m_epoch
Definition: txmempool.h:611
int64_t GetDescendantCount(const CTxMemPoolEntry &e) const
Definition: txmempool.h:278
void SkipBuilderChunk() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:764
void AddUnbroadcastTx(const Txid &txid)
Adds a transaction to the unbroadcast set.
Definition: txmempool.h:545
const CTransaction * GetConflictTx(const COutPoint &prevout) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Get the transaction in the pool that spends the same prevout.
Definition: txmempool.cpp:672
bool IsUnbroadcastTx(const Txid &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Returns whether a txid is in the unbroadcast set.
Definition: txmempool.h:566
std::unique_ptr< TxGraph > m_txgraph GUARDED_BY(cs)
std::set< Txid > GetUnbroadcastTxs() const
Returns transactions in unbroadcast set.
Definition: txmempool.h:559
void CalculateDescendants(txiter it, setEntries &setDescendants) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Populate setDescendants with all in-mempool descendants of given transaction.
Definition: txmempool.cpp:296
std::tuple< size_t, size_t, CAmount > CalculateAncestorData(const CTxMemPoolEntry &entry) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:896
unsigned long size() const
Definition: txmempool.h:488
std::vector< CTxMemPoolEntry::CTxMemPoolEntryRef > GetChildren(const CTxMemPoolEntry &entry) const
Definition: txmempool.cpp:57
setEntries CalculateMemPoolAncestors(const CTxMemPoolEntry &entry) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Calculate all in-mempool ancestors of entry (not including the tx itself)
Definition: txmempool.cpp:127
void cs_main
Definition: txmempool.h:336
uint64_t m_sequence_number GUARDED_BY(cs)
Definition: txmempool.h:205
void SetLoadTried(bool load_tried)
Set whether or not an initial attempt to load the persisted mempool was made (regardless of whether t...
Definition: txmempool.cpp:945
void RemoveStaged(setEntries &stage, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
Remove a set of transactions from the mempool.
Definition: txmempool.cpp:776
std::unique_ptr< TxGraph::BlockBuilder > m_builder GUARDED_BY(cs)
std::vector< CTxMemPoolEntryRef > entryAll() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:571
CAmount GetTotalFee() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:500
indirectmap< COutPoint, txiter > mapNextTx GUARDED_BY(cs)
uint64_t GetTotalTxSize() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:494
bool isSpent(const COutPoint &outpoint) const
Definition: txmempool.cpp:179
FeePerWeight GetBlockBuilderChunk(std::vector< CTxMemPoolEntry::CTxMemPoolEntryRef > &entries) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:750
void removeRecursive(txiter to_remove, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
const CTxMemPoolEntry * GetEntry(const Txid &txid) const LIFETIMEBOUND EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:597
unsigned int GetTransactionsUpdated() const
Definition: txmempool.cpp:185
CAmount m_total_fee GUARDED_BY(cs)
sum of all mempool tx's virtual sizes. Differs from serialized tx size since witness data is discount...
Definition: txmempool.h:194
Definition: txmempool.h:98
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
Definition: txmempool.h:100
Epoch: RAII-style guard for using epoch-based graph traversal algorithms.
Definition: epochguard.h:35
@ MAIN
Always refers to the main graph, whether staging is present or not.
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: cs_main.cpp:8
uint64_t fee
LockPoints lp
MemPoolRemovalReason
Reason why a transaction was removed from the mempool, this is passed to the notification signal.
T check(T ptr)
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:403
Definition: txmempool.h:233
const CAmount delta
The fee delta added using PrioritiseTransaction().
Definition: txmempool.h:358
const bool in_mempool
Whether this transaction is in the mempool.
Definition: txmempool.h:356
std::optional< CAmount > modified_fee
The modified fee (base fee + delta) of this entry.
Definition: txmempool.h:360
const Txid txid
The prioritised transaction's txid.
Definition: txmempool.h:362
Tagged wrapper around FeeFrac to avoid unit confusion.
Definition: feefrac.h:239
Information about a mempool transaction.
Definition: txmempool.h:114
int64_t nFeeDelta
The fee delta.
Definition: txmempool.h:128
int32_t vsize
Virtual size of the transaction.
Definition: txmempool.h:125
CAmount fee
Fee of the transaction.
Definition: txmempool.h:122
CTransactionRef tx
The transaction itself.
Definition: txmempool.h:116
std::chrono::seconds m_time
Time the transaction entered the mempool.
Definition: txmempool.h:119
Bilingual messages:
Definition: translation.h:24
Options struct containing limit options for a CTxMemPool.
Options struct containing options for constructing a CTxMemPool.
result_type operator()(const CTxMemPoolEntry &entry) const
Definition: txmempool.h:71
result_type operator()(const CTxMemPoolEntry &entry) const
Definition: txmempool.h:86
#define LOCK(cs)
Definition: sync.h:259
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:51
static constexpr uint64_t ACCEPTABLE_ITERS
How many linearization iterations required for TxGraph clusters to have "acceptable" quality,...
Definition: txmempool.h:56
static constexpr uint64_t POST_CHANGE_WORK
How much work we ask TxGraph to do after a mempool change occurs (either due to a changeset being app...
Definition: txmempool.h:60
static const uint32_t MEMPOOL_HEIGHT
Fake height value used in Coin to signify they are only in the memory pool (since 0....
Definition: txmempool.h:51
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.
Definition: txmempool.cpp:40
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
Definition: time.cpp:77
AssertLockHeld(pool.cs)
assert(!tx.IsCoinBase())