Bitcoin Core 29.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 <util/epochguard.h>
23#include <util/feefrac.h>
24#include <util/hasher.h>
25#include <util/result.h>
26
27#include <boost/multi_index/hashed_index.hpp>
28#include <boost/multi_index/identity.hpp>
29#include <boost/multi_index/indexed_by.hpp>
30#include <boost/multi_index/ordered_index.hpp>
31#include <boost/multi_index/sequenced_index.hpp>
32#include <boost/multi_index/tag.hpp>
33#include <boost/multi_index_container.hpp>
34
35#include <atomic>
36#include <map>
37#include <optional>
38#include <set>
39#include <string>
40#include <string_view>
41#include <utility>
42#include <vector>
43
44class CChain;
46
47struct bilingual_str;
48
50static const uint32_t MEMPOOL_HEIGHT = 0x7FFFFFFF;
51
56
57// extracts a transaction hash from CTxMemPoolEntry or CTransactionRef
59{
62 {
63 return entry.GetTx().GetHash();
64 }
65
67 {
68 return tx->GetHash();
69 }
70};
71
72// extracts a transaction witness-hash from CTxMemPoolEntry or CTransactionRef
74{
77 {
78 return entry.GetTx().GetWitnessHash();
79 }
80
82 {
83 return tx->GetWitnessHash();
84 }
85};
86
87
93{
94public:
95 bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
96 {
99
100 if (FeeRateCompare(f1, f2) == 0) {
101 return a.GetTime() >= b.GetTime();
102 }
103 return f1 < f2;
104 }
105
106 // Return the fee/size we're using for sorting this entry.
108 {
109 // Compare feerate with descendants to feerate of the transaction, and
110 // return the fee/size for the max.
111 return std::max<FeeFrac>(
114 );
115 }
116};
117
126{
127public:
128 bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
129 {
130 FeeFrac f1(a.GetFee(), a.GetTxSize());
131 FeeFrac f2(b.GetFee(), b.GetTxSize());
132 if (FeeRateCompare(f1, f2) == 0) {
133 return b.GetTx().GetHash() < a.GetTx().GetHash();
134 }
135 return f1 > f2;
136 }
137};
138
140{
141public:
142 bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
143 {
144 return a.GetTime() < b.GetTime();
145 }
146};
147
153{
154public:
155 template<typename T>
156 bool operator()(const T& a, const T& b) const
157 {
160
161 if (FeeRateCompare(f1, f2) == 0) {
162 return a.GetTx().GetHash() < b.GetTx().GetHash();
163 }
164 return f1 > f2;
165 }
166
167 // Return the fee/size we're using for sorting this entry.
168 template <typename T>
169 FeeFrac GetModFeeAndSize(const T &a) const
170 {
171 // Compare feerate with ancestors to feerate of the transaction, and
172 // return the fee/size for the min.
173 return std::min<FeeFrac>(
174 FeeFrac(a.GetModFeesWithAncestors(), a.GetSizeWithAncestors()),
175 FeeFrac(a.GetModifiedFee(), a.GetTxSize())
176 );
177 }
178};
179
180// Multi_index tag names
182struct entry_time {};
185
190{
193
195 std::chrono::seconds m_time;
196
199
201 int32_t vsize;
202
204 int64_t nFeeDelta;
205};
206
281{
282protected:
283 std::atomic<unsigned int> nTransactionsUpdated{0};
284
285 uint64_t totalTxSize GUARDED_BY(cs){0};
286 CAmount m_total_fee GUARDED_BY(cs){0};
287 uint64_t cachedInnerUsage GUARDED_BY(cs){0};
288
289 mutable int64_t lastRollingFeeUpdate GUARDED_BY(cs){GetTime()};
290 mutable bool blockSinceLastRollingFeeBump GUARDED_BY(cs){false};
291 mutable double rollingMinimumFeeRate GUARDED_BY(cs){0};
293
294 // In-memory counter for external mempool tracking purposes.
295 // This number is incremented once every time a transaction
296 // is added or removed from the mempool for any reason.
297 mutable uint64_t m_sequence_number GUARDED_BY(cs){1};
298
300
301 bool m_load_tried GUARDED_BY(cs){false};
302
303 CFeeRate GetMinFee(size_t sizelimit) const;
304
305public:
306
307 static const int ROLLING_FEE_HALFLIFE = 60 * 60 * 12; // public only for testing
308
309 struct CTxMemPoolEntry_Indices final : boost::multi_index::indexed_by<
310 // sorted by txid
311 boost::multi_index::hashed_unique<mempoolentry_txid, SaltedTxidHasher>,
312 // sorted by wtxid
313 boost::multi_index::hashed_unique<
314 boost::multi_index::tag<index_by_wtxid>,
315 mempoolentry_wtxid,
316 SaltedWtxidHasher
317 >,
318 // sorted by fee rate
319 boost::multi_index::ordered_non_unique<
320 boost::multi_index::tag<descendant_score>,
321 boost::multi_index::identity<CTxMemPoolEntry>,
322 CompareTxMemPoolEntryByDescendantScore
323 >,
324 // sorted by entry time
325 boost::multi_index::ordered_non_unique<
326 boost::multi_index::tag<entry_time>,
327 boost::multi_index::identity<CTxMemPoolEntry>,
328 CompareTxMemPoolEntryByEntryTime
329 >,
330 // sorted by fee rate with ancestors
331 boost::multi_index::ordered_non_unique<
332 boost::multi_index::tag<ancestor_score>,
333 boost::multi_index::identity<CTxMemPoolEntry>,
334 CompareTxMemPoolEntryByAncestorFee
335 >
336 >
337 {};
338 typedef boost::multi_index_container<
342
369
370 using txiter = indexed_transaction_set::nth_index<0>::type::const_iterator;
371 std::vector<CTransactionRef> txns_randomized GUARDED_BY(cs);
372
373 typedef std::set<txiter, CompareIteratorByHash> setEntries;
374
376
378private:
379 typedef std::map<txiter, setEntries, CompareIteratorByHash> cacheMap;
380
381
382 void UpdateParent(txiter entry, txiter parent, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs);
383 void UpdateChild(txiter entry, txiter child, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs);
384
385 std::vector<indexed_transaction_set::const_iterator> GetSortedDepthAndScore() const EXCLUSIVE_LOCKS_REQUIRED(cs);
386
390 std::set<Txid> m_unbroadcast_txids GUARDED_BY(cs);
391
392
405 size_t entry_count,
406 CTxMemPoolEntry::Parents &staged_ancestors,
407 const Limits& limits
409
411 {
412 return TxMempoolInfo{it->GetSharedTx(), it->GetTime(), it->GetFee(), it->GetTxSize(), it->GetModifiedFee() - it->GetFee()};
413 }
414
415public:
417 std::map<Txid, CAmount> mapDeltas GUARDED_BY(cs);
418
420
422
428 explicit CTxMemPool(Options opts, bilingual_str& error);
429
436 void check(const CCoinsViewCache& active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
437
438
447 void removeForReorg(CChain& chain, std::function<bool(txiter)> filter_final_and_mature) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main);
449 void removeForBlock(const std::vector<CTransactionRef>& vtx, unsigned int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(cs);
450
451 bool CompareDepthAndScore(const Wtxid& hasha, const Wtxid& hashb) const;
452 bool isSpent(const COutPoint& outpoint) const;
453 unsigned int GetTransactionsUpdated() const;
454 void AddTransactionsUpdated(unsigned int n);
460
462 void PrioritiseTransaction(const Txid& hash, const CAmount& nFeeDelta);
463 void ApplyDelta(const Txid& hash, CAmount &nFeeDelta) const EXCLUSIVE_LOCKS_REQUIRED(cs);
465
466 struct delta_info {
468 const bool in_mempool;
472 std::optional<CAmount> modified_fee;
474 const Txid txid;
475 };
477 std::vector<delta_info> GetPrioritisedTransactions() const EXCLUSIVE_LOCKS_REQUIRED(!cs);
478
480 const CTransaction* GetConflictTx(const COutPoint& prevout) const EXCLUSIVE_LOCKS_REQUIRED(cs);
481
483 std::optional<txiter> GetIter(const Txid& txid) const EXCLUSIVE_LOCKS_REQUIRED(cs);
484 std::optional<txiter> GetIter(const Wtxid& wtxid) const EXCLUSIVE_LOCKS_REQUIRED(cs);
485
489 setEntries GetIterSet(const std::set<Txid>& hashes) const EXCLUSIVE_LOCKS_REQUIRED(cs);
490
494 std::vector<txiter> GetIterVec(const std::vector<Txid>& txids) const EXCLUSIVE_LOCKS_REQUIRED(cs);
495
510
524 const Limits& limits,
525 bool fSearchForParents = true) const EXCLUSIVE_LOCKS_REQUIRED(cs);
526
542 std::string_view calling_fn_name,
543 const CTxMemPoolEntry &entry,
544 const Limits& limits,
545 bool fSearchForParents = true) const EXCLUSIVE_LOCKS_REQUIRED(cs);
546
551 std::vector<txiter> GatherClusters(const std::vector<Txid>& txids) const EXCLUSIVE_LOCKS_REQUIRED(cs);
552
565 util::Result<void> CheckPackageLimits(const Package& package,
566 int64_t total_vsize) const EXCLUSIVE_LOCKS_REQUIRED(cs);
567
572
581 }
582
587 void TrimToSize(size_t sizelimit, std::vector<COutPoint>* pvNoSpendsRemaining = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
588
590 int Expire(std::chrono::seconds time) EXCLUSIVE_LOCKS_REQUIRED(cs);
591
598 void GetTransactionAncestry(const Txid& txid, size_t& ancestors, size_t& descendants, size_t* ancestorsize = nullptr, CAmount* ancestorfees = nullptr) const;
599
604 bool GetLoadTried() const;
605
610 void SetLoadTried(bool load_tried);
611
612 unsigned long size() const
613 {
614 LOCK(cs);
615 return mapTx.size();
616 }
617
619 {
621 return totalTxSize;
622 }
623
625 {
627 return m_total_fee;
628 }
629
630 bool exists(const Txid& txid) const
631 {
632 LOCK(cs);
633 return (mapTx.count(txid) != 0);
634 }
635
636 bool exists(const Wtxid& wtxid) const
637 {
638 LOCK(cs);
639 return (mapTx.get<index_by_wtxid>().count(wtxid) != 0);
640 }
641
643
644 CTransactionRef get(const Txid& hash) const;
645
646 template <TxidOrWtxid T>
647 TxMempoolInfo info(const T& id) const
648 {
649 LOCK(cs);
650 auto i{GetIter(id)};
651 return i.has_value() ? GetInfo(*i) : TxMempoolInfo{};
652 }
653
655 template <TxidOrWtxid T>
656 TxMempoolInfo info_for_relay(const T& id, uint64_t last_sequence) const
657 {
658 LOCK(cs);
659 auto i{GetIter(id)};
660 return (i.has_value() && i.value()->GetSequence() < last_sequence) ? GetInfo(*i) : TxMempoolInfo{};
661 }
662
663 std::vector<CTxMemPoolEntryRef> entryAll() const EXCLUSIVE_LOCKS_REQUIRED(cs);
664 std::vector<TxMempoolInfo> infoAll() const;
665
666 size_t DynamicMemoryUsage() const;
667
669 void AddUnbroadcastTx(const Txid& txid)
670 {
671 LOCK(cs);
672 // Sanity check the transaction is in the mempool & insert into
673 // unbroadcast set.
674 if (exists(txid)) m_unbroadcast_txids.insert(txid);
675 };
676
678 void RemoveUnbroadcastTx(const Txid& txid, const bool unchecked = false);
679
681 std::set<Txid> GetUnbroadcastTxs() const
682 {
683 LOCK(cs);
684 return m_unbroadcast_txids;
685 }
686
689 {
691 return m_unbroadcast_txids.count(txid) != 0;
692 }
693
696 return m_sequence_number++;
697 }
698
700 return m_sequence_number;
701 }
702
703 /* Check that all direct conflicts are in a cluster size of two or less. Each
704 * direct conflict may be in a separate cluster.
705 */
706 std::optional<std::string> CheckConflictTopology(const setEntries& direct_conflicts);
707
708private:
716 void RemoveStaged(setEntries& stage, bool updateDescendants, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs);
717
746 void UpdateForDescendants(txiter updateIt, cacheMap& cachedDescendants,
747 const std::set<Txid>& setExclude, std::set<Txid>& descendants_to_remove) EXCLUSIVE_LOCKS_REQUIRED(cs);
749 void UpdateAncestorsOf(bool add, txiter hash, setEntries &setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs);
755 void UpdateForRemoveFromMempool(const setEntries &entriesToRemove, bool updateDescendants) EXCLUSIVE_LOCKS_REQUIRED(cs);
758
768public:
778 {
779 return m_epoch.visited(it->m_epoch_marker);
780 }
781
782 bool visited(std::optional<txiter> it) const EXCLUSIVE_LOCKS_REQUIRED(cs, m_epoch)
783 {
784 assert(m_epoch.guarded()); // verify guard even when it==nullopt
785 return !it || visited(*it);
786 }
787
788 /*
789 * CTxMemPool::ChangeSet:
790 *
791 * This class is used for all mempool additions and associated removals (eg
792 * due to rbf). Removals that don't need to be evaluated for acceptance,
793 * such as removing transactions that appear in a block, or due to reorg,
794 * or removals related to mempool limiting or expiry do not need to use
795 * this.
796 *
797 * Callers can interleave calls to StageAddition()/StageRemoval(), and
798 * removals may be invoked in any order, but additions must be done in a
799 * topological order in the case of transaction packages (ie, parents must
800 * be added before children).
801 *
802 * CalculateChunksForRBF() can be used to calculate the feerate diagram of
803 * the proposed set of new transactions and compare with the existing
804 * mempool.
805 *
806 * CalculateMemPoolAncestors() calculates the in-mempool (not including
807 * what is in the change set itself) ancestors of a given transaction.
808 *
809 * Apply() will apply the removals and additions that are staged into the
810 * mempool.
811 *
812 * Only one changeset may exist at a time. While a changeset is
813 * outstanding, no removals or additions may be made directly to the
814 * mempool.
815 */
816 class ChangeSet {
817 public:
818 explicit ChangeSet(CTxMemPool* pool) : m_pool(pool) {}
819 ~ChangeSet() EXCLUSIVE_LOCKS_REQUIRED(m_pool->cs) { m_pool->m_have_changeset = false; }
820
821 ChangeSet(const ChangeSet&) = delete;
822 ChangeSet& operator=(const ChangeSet&) = delete;
823
825
826 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);
828
830
832 {
833 // Look up transaction in our cache first
834 auto it = m_ancestors.find(tx);
835 if (it != m_ancestors.end()) return it->second;
836
837 // If not found, try to have the mempool calculate it, and cache
838 // for later.
839 LOCK(m_pool->cs);
840 auto ret{m_pool->CalculateMemPoolAncestors(*tx, limits)};
841 if (ret) m_ancestors.try_emplace(tx, *ret);
842 return ret;
843 }
844
845 std::vector<CTransactionRef> GetAddedTxns() const {
846 std::vector<CTransactionRef> ret;
847 ret.reserve(m_entry_vec.size());
848 for (const auto& entry : m_entry_vec) {
849 ret.emplace_back(entry->GetSharedTx());
850 }
851 return ret;
852 }
853
861
862 size_t GetTxCount() const { return m_entry_vec.size(); }
863 const CTransaction& GetAddedTxn(size_t index) const { return m_entry_vec.at(index)->GetTx(); }
864
866
867 private:
870 std::vector<CTxMemPool::txiter> m_entry_vec; // track the added transactions' insertion order
871 // map from the m_to_add index to the ancestors for the transaction
874
875 friend class CTxMemPool;
876 };
877
879 Assume(!m_have_changeset);
880 m_have_changeset = true;
881 return std::make_unique<ChangeSet>(this);
882 }
883
884 bool m_have_changeset GUARDED_BY(cs){false};
885
887
888private:
889 // Apply the given changeset to the mempool, by removing transactions in
890 // the to_remove set and adding transactions in the to_add set.
892
893 // addNewTransaction must update state for all ancestors of a given transaction,
894 // to track size/count of descendant transactions. First version of
895 // addNewTransaction can be used to have it call CalculateMemPoolAncestors(), and
896 // then invoke the second version.
897 // Note that addNewTransaction is ONLY called (via Apply()) from ATMP
898 // outside of tests and any other callers may break wallet's in-mempool
899 // tracking (due to lack of CValidationInterface::TransactionAddedToMempool
900 // callbacks).
903};
904
919{
924 std::unordered_map<COutPoint, Coin, SaltedOutpointHasher> m_temp_added;
925
930 mutable std::unordered_set<COutPoint, SaltedOutpointHasher> m_non_base_coins;
931protected:
933
934public:
935 CCoinsViewMemPool(CCoinsView* baseIn, const CTxMemPool& mempoolIn);
938 std::optional<Coin> GetCoin(const COutPoint& outpoint) const override;
941 void PackageAddTransaction(const CTransactionRef& tx);
943 const std::unordered_set<COutPoint, SaltedOutpointHasher>& GetNonBaseCoins() const { return m_non_base_coins; }
945 void Reset();
946};
947#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:118
An in-memory indexed chain of blocks.
Definition: chain.h:417
CCoinsView backed by another CCoinsView.
Definition: coins.h:344
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:363
Abstract view on the open txout dataset.
Definition: coins.h:310
CCoinsView that brings transactions from a mempool into view.
Definition: txmempool.h:919
const std::unordered_set< COutPoint, SaltedOutpointHasher > & GetNonBaseCoins() const
Get all coins in m_non_base_coins.
Definition: txmempool.h:943
std::unordered_map< COutPoint, Coin, SaltedOutpointHasher > m_temp_added
Coins made available by transactions being validated.
Definition: txmempool.h:924
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:930
const CTxMemPool & mempool
Definition: txmempool.h:932
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:296
const Wtxid & GetWitnessHash() const LIFETIMEBOUND
Definition: transaction.h:344
const Txid & GetHash() const LIFETIMEBOUND
Definition: transaction.h:343
size_t GetTxCount() const
Definition: txmempool.h:862
const CTransaction & GetAddedTxn(size_t index) const
Definition: txmempool.h:863
CTxMemPool::setEntries m_to_remove
Definition: txmempool.h:873
ChangeSet(CTxMemPool *pool)
Definition: txmempool.h:818
void Apply() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Definition: txmempool.cpp:1400
CTxMemPool * m_pool
Definition: txmempool.h:868
void StageRemoval(CTxMemPool::txiter it)
Definition: txmempool.h:827
util::Result< CTxMemPool::setEntries > CalculateMemPoolAncestors(TxHandle tx, const Limits &limits)
Definition: txmempool.h:831
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:1387
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:1296
CTxMemPool::txiter TxHandle
Definition: txmempool.h:824
CTxMemPool::indexed_transaction_set m_to_add
Definition: txmempool.h:869
~ChangeSet() EXCLUSIVE_LOCKS_REQUIRED(m_pool -> cs)
Definition: txmempool.h:819
ChangeSet(const ChangeSet &)=delete
const CTxMemPool::setEntries & GetRemovals() const
Definition: txmempool.h:829
std::map< CTxMemPool::txiter, CTxMemPool::setEntries, CompareIteratorByHash > m_ancestors
Definition: txmempool.h:872
std::vector< CTransactionRef > GetAddedTxns() const
Definition: txmempool.h:845
ChangeSet & operator=(const ChangeSet &)=delete
std::vector< CTxMemPool::txiter > m_entry_vec
Definition: txmempool.h:870
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: mempool_entry.h:66
int64_t GetSizeWithDescendants() const
std::chrono::seconds GetTime() const
const CTransaction & GetTx() const
CAmount GetModFeesWithDescendants() 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 ...
Definition: txmempool.h:281
void removeConflicts(const CTransaction &tx) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:647
std::atomic< unsigned int > nTransactionsUpdated
Used by getblocktemplate to trigger CreateNewBlock() invocation.
Definition: txmempool.h:283
void PrioritiseTransaction(const Txid &hash, const CAmount &nFeeDelta)
Affect CreateNewBlock prioritisation of transactions.
Definition: txmempool.cpp:887
setEntries AssumeCalculateMemPoolAncestors(std::string_view calling_fn_name, const CTxMemPoolEntry &entry, const Limits &limits, bool fSearchForParents=true) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Same as CalculateMemPoolAncestors, but always returns a (non-optional) setEntries.
Definition: txmempool.cpp:275
std::unique_ptr< ChangeSet > GetChangeSet() EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:878
std::map< Txid, CAmount > mapDeltas GUARDED_BY(cs)
static TxMempoolInfo GetInfo(CTxMemPool::indexed_transaction_set::const_iterator it)
Definition: txmempool.h:410
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:656
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:998
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:975
void ClearPrioritisation(const Txid &hash) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:933
uint64_t cachedInnerUsage GUARDED_BY(cs)
sum of all mempool tx's fees (NOT modified fee)
Definition: txmempool.h:287
void UpdateEntryForAncestors(txiter it, const setEntries &setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs)
Set ancestor state for an entry.
Definition: txmempool.cpp:304
std::optional< txiter > GetIter(const Txid &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Returns an iterator to the given hash, if found.
Definition: txmempool.cpp:961
bool GetLoadTried() const
Definition: txmempool.cpp:1217
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...
bool exists(const Wtxid &wtxid) const
Definition: txmempool.h:636
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:579
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:367
void trackPackageRemoved(const CFeeRate &rate) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:1131
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.
Definition: txmempool.cpp:243
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:439
void UpdateForDescendants(txiter updateIt, cacheMap &cachedDescendants, const std::set< Txid > &setExclude, std::set< Txid > &descendants_to_remove) EXCLUSIVE_LOCKS_REQUIRED(cs)
UpdateForDescendants is used by UpdateTransactionsFromBlock to update the descendants for a single tr...
Definition: txmempool.cpp:57
bool blockSinceLastRollingFeeBump GUARDED_BY(cs)
Definition: txmempool.h:290
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:1139
std::vector< CTransactionRef > txns_randomized GUARDED_BY(cs)
All transactions in mapTx, in random order.
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:108
return !it visited * it
Definition: txmempool.h:785
void AddTransactionsUpdated(unsigned int n)
Definition: txmempool.cpp:430
void UpdateChildrenForRemoval(txiter entry) EXCLUSIVE_LOCKS_REQUIRED(cs)
Sever link between specified transaction and direct children.
Definition: txmempool.cpp:318
indirectmap< COutPoint, const CTransaction * > mapNextTx GUARDED_BY(cs)
util::Result< setEntries > CalculateAncestorsAndCheckLimits(int64_t entry_size, size_t entry_count, CTxMemPoolEntry::Parents &staged_ancestors, const Limits &limits) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Helper function to calculate all in-mempool ancestors of staged_ancestors and apply ancestor and desc...
Definition: txmempool.cpp:163
CTransactionRef get(const Txid &hash) const
Definition: txmempool.cpp:878
bool m_have_changeset GUARDED_BY(cs)
Definition: txmempool.h:884
void cs_main LOCKS_EXCLUDED(m_epoch)
size_t DynamicMemoryUsage() const
Definition: txmempool.cpp:1044
const Options m_opts
Definition: txmempool.h:421
Epoch m_epoch GUARDED_BY(cs)
minimum fee to get into the pool, decreases exponentially
Definition: txmempool.h:292
std::vector< TxMempoolInfo > infoAll() const
Definition: txmempool.cpp:857
indexed_transaction_set mapTx GUARDED_BY(cs)
TxMempoolInfo info(const T &id) const
Definition: txmempool.h:647
CTxMemPool(Options opts, bilingual_str &error)
Create a new CTxMemPool.
Definition: txmempool.cpp:414
void UpdateParent(txiter entry, txiter parent, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:1096
void addNewTransaction(CTxMemPool::txiter it) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:471
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:520
uint64_t totalTxSize GUARDED_BY(cs)
Definition: txmempool.h:285
boost::multi_index_container< CTxMemPoolEntry, CTxMemPoolEntry_Indices > indexed_transaction_set
Definition: txmempool.h:341
void RemoveUnbroadcastTx(const Txid &txid, const bool unchecked=false)
Removes a transaction from the unbroadcast set.
Definition: txmempool.cpp:1050
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:1067
bool CompareDepthAndScore(const Wtxid &hasha, const Wtxid &hashb) const
Definition: txmempool.cpp:795
void UpdateAncestorsOf(bool add, txiter hash, setEntries &setAncestors) EXCLUSIVE_LOCKS_REQUIRED(cs)
Update ancestors of hash to add/remove it as a descendant transaction.
Definition: txmempool.cpp:289
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:626
util::Result< void > CheckPackageLimits(const Package &package, int64_t total_vsize) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Calculate all in-mempool ancestors of a set of transactions not already in the mempool and check ance...
Definition: txmempool.cpp:205
bool exists(const Txid &txid) const
Definition: txmempool.h:630
int64_t lastRollingFeeUpdate GUARDED_BY(cs)
sum of dynamic memory usage of all the map elements (NOT the maps themselves)
Definition: txmempool.h:289
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:985
bool m_load_tried GUARDED_BY(cs)
Definition: txmempool.h:301
static const int ROLLING_FEE_HALFLIFE
Definition: txmempool.h:307
std::set< txiter, CompareIteratorByHash > setEntries
Definition: txmempool.h:373
std::vector< indexed_transaction_set::const_iterator > GetSortedDepthAndScore() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:831
void ApplyDelta(const Txid &hash, CAmount &nFeeDelta) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:923
void RemoveStaged(setEntries &stage, bool updateDescendants, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
Remove a set of transactions from the mempool.
Definition: txmempool.cpp:1059
void removeForBlock(const std::vector< CTransactionRef > &vtx, unsigned int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:664
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:939
uint64_t GetSequence() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:699
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:1229
indexed_transaction_set::nth_index< 0 >::type::const_iterator txiter
Definition: txmempool.h:370
uint64_t GetAndIncrementSequence() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Guards this internal counter for external reporting.
Definition: txmempool.h:695
void UpdateChild(txiter entry, txiter child, bool add) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:1085
double rollingMinimumFeeRate GUARDED_BY(cs)
Definition: txmempool.h:291
std::map< txiter, setEntries, CompareIteratorByHash > cacheMap
Definition: txmempool.h:379
bool m_epoch
Definition: txmempool.h:778
void GetTransactionAncestry(const Txid &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.
Definition: txmempool.cpp:1205
void AddUnbroadcastTx(const Txid &txid)
Adds a transaction to the unbroadcast set.
Definition: txmempool.h:669
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:955
bool IsUnbroadcastTx(const Txid &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Returns whether a txid is in the unbroadcast set.
Definition: txmempool.h:688
std::set< Txid > GetUnbroadcastTxs() const
Returns transactions in unbroadcast set.
Definition: txmempool.h:681
void CalculateDescendants(txiter it, setEntries &setDescendants) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Populate setDescendants with all in-mempool descendants of hash.
Definition: txmempool.cpp:571
unsigned long size() const
Definition: txmempool.h:612
std::optional< std::string > CheckConflictTopology(const setEntries &direct_conflicts)
Definition: txmempool.cpp:1256
void cs_main
Definition: txmempool.h:447
uint64_t m_sequence_number GUARDED_BY(cs)
Definition: txmempool.h:297
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:1223
void UpdateForRemoveFromMempool(const setEntries &entriesToRemove, bool updateDescendants) EXCLUSIVE_LOCKS_REQUIRED(cs)
For each transaction being removed, update ancestors and any direct children.
Definition: txmempool.cpp:326
std::vector< CTxMemPoolEntryRef > entryAll() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:845
CAmount GetTotalFee() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:624
uint64_t CalculateDescendantMaximum(txiter entry) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:1183
uint64_t GetTotalTxSize() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:618
bool isSpent(const COutPoint &outpoint) const
Definition: txmempool.cpp:419
const CTxMemPoolEntry * GetEntry(const Txid &txid) const LIFETIMEBOUND EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:871
unsigned int GetTransactionsUpdated() const
Definition: txmempool.cpp:425
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:286
Definition: txmempool.h:153
FeeFrac GetModFeeAndSize(const T &a) const
Definition: txmempool.h:169
bool operator()(const T &a, const T &b) const
Definition: txmempool.h:156
Sort an entry by max(score/size of entry's tx, score/size with all descendants).
Definition: txmempool.h:93
FeeFrac GetModFeeAndSize(const CTxMemPoolEntry &a) const
Definition: txmempool.h:107
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
Definition: txmempool.h:95
Definition: txmempool.h:140
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
Definition: txmempool.h:142
Sort by feerate of entry (fee/size) in descending order This is only used for transaction relay,...
Definition: txmempool.h:126
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
Definition: txmempool.h:128
Epoch: RAII-style guard for using epoch-based graph traversal algorithms.
Definition: epochguard.h:35
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.
std::vector< CTransactionRef > Package
A package is an ordered list of transactions.
Definition: packages.h:50
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:423
Definition: txmempool.h:337
const CAmount delta
The fee delta added using PrioritiseTransaction().
Definition: txmempool.h:470
const bool in_mempool
Whether this transaction is in the mempool.
Definition: txmempool.h:468
std::optional< CAmount > modified_fee
The modified fee (base fee + delta) of this entry.
Definition: txmempool.h:472
const Txid txid
The prioritised transaction's txid.
Definition: txmempool.h:474
Data structure storing a fee and size, ordered by increasing fee/size.
Definition: feefrac.h:40
Information about a mempool transaction.
Definition: txmempool.h:190
int64_t nFeeDelta
The fee delta.
Definition: txmempool.h:204
int32_t vsize
Virtual size of the transaction.
Definition: txmempool.h:201
CAmount fee
Fee of the transaction.
Definition: txmempool.h:198
CTransactionRef tx
The transaction itself.
Definition: txmempool.h:192
std::chrono::seconds m_time
Time the transaction entered the mempool.
Definition: txmempool.h:195
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:61
result_type operator()(const CTxMemPoolEntry &entry) const
Definition: txmempool.h:76
#define LOCK(cs)
Definition: sync.h:259
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:51
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:50
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())