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-present 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/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
55static constexpr uint64_t ACCEPTABLE_ITERS = 1'700;
56
59static constexpr uint64_t POST_CHANGE_WORK = 5 * ACCEPTABLE_ITERS;
60
65
66// extracts a transaction hash from CTxMemPoolEntry or CTransactionRef
68{
71 {
72 return entry.GetTx().GetHash();
73 }
74
76 {
77 return tx->GetHash();
78 }
79};
80
81// extracts a transaction witness-hash from CTxMemPoolEntry or CTransactionRef
83{
86 {
87 return entry.GetTx().GetWitnessHash();
88 }
89
91 {
92 return tx->GetWitnessHash();
93 }
94};
95
97{
98public:
99 bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
100 {
101 return a.GetTime() < b.GetTime();
102 }
103};
104
105// Multi_index tag names
106struct entry_time {};
108
113{
116
118 std::chrono::seconds m_time;
119
122
124 int32_t vsize;
125
127 int64_t nFeeDelta;
128};
129
188{
189protected:
190 std::atomic<unsigned int> nTransactionsUpdated{0};
191
192 uint64_t totalTxSize GUARDED_BY(cs){0};
193 CAmount m_total_fee GUARDED_BY(cs){0};
194 uint64_t cachedInnerUsage GUARDED_BY(cs){0};
195
196 mutable int64_t lastRollingFeeUpdate GUARDED_BY(cs){GetTime()};
197 mutable bool blockSinceLastRollingFeeBump GUARDED_BY(cs){false};
198 mutable double rollingMinimumFeeRate GUARDED_BY(cs){0};
199
200 // In-memory counter for external mempool tracking purposes.
201 // This number is incremented once every time a transaction
202 // is added or removed from the mempool for any reason.
203 mutable uint64_t m_sequence_number GUARDED_BY(cs){1};
204
206
207 bool m_load_tried GUARDED_BY(cs){false};
208
209 CFeeRate GetMinFee(size_t sizelimit) const;
210
211public:
212
213 static const int ROLLING_FEE_HALFLIFE = 60 * 60 * 12; // public only for testing
214
215 struct CTxMemPoolEntry_Indices final : boost::multi_index::indexed_by<
216 // sorted by txid
217 boost::multi_index::hashed_unique<mempoolentry_txid, SaltedTxidHasher>,
218 // sorted by wtxid
219 boost::multi_index::hashed_unique<
220 boost::multi_index::tag<index_by_wtxid>,
221 mempoolentry_wtxid,
222 SaltedWtxidHasher
223 >,
224 // sorted by entry time
225 boost::multi_index::ordered_non_unique<
226 boost::multi_index::tag<entry_time>,
227 boost::multi_index::identity<CTxMemPoolEntry>,
228 CompareTxMemPoolEntryByEntryTime
229 >
230 >
231 {};
232 typedef boost::multi_index_container<
236
262 std::unique_ptr<TxGraph> m_txgraph GUARDED_BY(cs);
263 mutable std::unique_ptr<TxGraph::BlockBuilder> m_builder GUARDED_BY(cs);
265
266 using txiter = indexed_transaction_set::nth_index<0>::type::const_iterator;
267 std::vector<std::pair<Wtxid, txiter>> txns_randomized GUARDED_BY(cs);
268
269 typedef std::set<txiter, CompareIteratorByHash> setEntries;
270
272
273 std::tuple<size_t, size_t, CAmount> CalculateAncestorData(const CTxMemPoolEntry& entry) const EXCLUSIVE_LOCKS_REQUIRED(cs);
274 std::tuple<size_t, size_t, CAmount> CalculateDescendantData(const CTxMemPoolEntry& entry) const EXCLUSIVE_LOCKS_REQUIRED(cs);
275 int64_t GetDescendantCount(txiter it) const { LOCK(cs); return m_txgraph->GetDescendants(*it, TxGraph::Level::MAIN).size(); }
276 int64_t GetDescendantCount(const CTxMemPoolEntry &e) const { LOCK(cs); return m_txgraph->GetDescendants(e, TxGraph::Level::MAIN).size(); }
277 int64_t GetAncestorCount(const CTxMemPoolEntry &e) const { LOCK(cs); return m_txgraph->GetAncestors(e, TxGraph::Level::MAIN).size(); }
278 std::vector<CTxMemPoolEntry::CTxMemPoolEntryRef> GetChildren(const CTxMemPoolEntry &entry) const;
279 std::vector<CTxMemPoolEntry::CTxMemPoolEntryRef> GetParents(const CTxMemPoolEntry &entry) const;
280
281private:
282 std::vector<indexed_transaction_set::const_iterator> GetSortedScoreWithTopology() const EXCLUSIVE_LOCKS_REQUIRED(cs);
283
287 std::set<Txid> m_unbroadcast_txids GUARDED_BY(cs);
288
290 {
291 return TxMempoolInfo{it->GetSharedTx(), it->GetTime(), it->GetFee(), it->GetTxSize(), it->GetModifiedFee() - it->GetFee()};
292 }
293
294 // Helper to remove all transactions that conflict with a given
295 // transaction (used for transactions appearing in a block).
297
298public:
300 std::map<Txid, CAmount> mapDeltas GUARDED_BY(cs);
301
303
305
311 explicit CTxMemPool(Options opts, bilingual_str& error);
312
319 void check(const CCoinsViewCache& active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
320
334 void removeForReorg(CChain& chain, std::function<bool(txiter)> filter_final_and_mature) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main);
335 void removeForBlock(const std::vector<CTransactionRef>& vtx, unsigned int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(cs);
336
337 bool CompareMiningScoreWithTopology(const Wtxid& hasha, const Wtxid& hashb) const;
338 bool isSpent(const COutPoint& outpoint) const;
339 unsigned int GetTransactionsUpdated() const;
340 void AddTransactionsUpdated(unsigned int n);
346
348 void PrioritiseTransaction(const Txid& hash, const CAmount& nFeeDelta);
349 void ApplyDelta(const Txid& hash, CAmount &nFeeDelta) const EXCLUSIVE_LOCKS_REQUIRED(cs);
351
352 struct delta_info {
354 const bool in_mempool;
358 std::optional<CAmount> modified_fee;
360 const Txid txid;
361 };
363 std::vector<delta_info> GetPrioritisedTransactions() const EXCLUSIVE_LOCKS_REQUIRED(!cs);
364
366 const CTransaction* GetConflictTx(const COutPoint& prevout) const EXCLUSIVE_LOCKS_REQUIRED(cs);
367
369 std::optional<txiter> GetIter(const Txid& txid) const EXCLUSIVE_LOCKS_REQUIRED(cs);
370 std::optional<txiter> GetIter(const Wtxid& wtxid) const EXCLUSIVE_LOCKS_REQUIRED(cs);
371
375 setEntries GetIterSet(const std::set<Txid>& hashes) const EXCLUSIVE_LOCKS_REQUIRED(cs);
376
380 std::vector<txiter> GetIterVec(const std::vector<Txid>& txids) const EXCLUSIVE_LOCKS_REQUIRED(cs);
381
395 void UpdateTransactionsFromBlock(const std::vector<Txid>& vHashesToUpdate) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main);
396
399 return m_txgraph->GetMainChunkFeerate(tx);
400 }
401 std::vector<const CTxMemPoolEntry*> GetCluster(Txid txid) const EXCLUSIVE_LOCKS_REQUIRED(cs) {
402 auto tx = GetIter(txid);
403 if (!tx) return {};
404 auto cluster = m_txgraph->GetCluster(**tx, TxGraph::Level::MAIN);
405 std::vector<const CTxMemPoolEntry*> ret;
406 ret.reserve(cluster.size());
407 for (const auto& tx : cluster) {
408 ret.emplace_back(static_cast<const CTxMemPoolEntry*>(tx));
409 }
410 return ret;
411 }
412
413
414 size_t GetUniqueClusterCount(const setEntries& iters_conflicting) const EXCLUSIVE_LOCKS_REQUIRED(cs) {
415 std::vector<const TxGraph::Ref *> entries;
416 entries.reserve(iters_conflicting.size());
417 for (auto it : iters_conflicting) {
418 entries.emplace_back(&*it);
419 }
420 Assume(!m_txgraph->IsOversized(TxGraph::Level::MAIN));
421 return m_txgraph->CountDistinctClusters(entries, TxGraph::Level::MAIN);
422 }
423
432
433 bool HasDescendants(const Txid& txid) const;
434
439 std::vector<txiter> GatherClusters(const std::vector<Txid>& txids) const EXCLUSIVE_LOCKS_REQUIRED(cs);
440
444 void CalculateDescendants(txiter it, setEntries& setDescendants) const EXCLUSIVE_LOCKS_REQUIRED(cs);
446
455 }
456
461 void TrimToSize(size_t sizelimit, std::vector<COutPoint>* pvNoSpendsRemaining = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
462
464 int Expire(std::chrono::seconds time) EXCLUSIVE_LOCKS_REQUIRED(cs);
465
472 void GetTransactionAncestry(const Txid& txid, size_t& ancestors, size_t& cluster_count, size_t* ancestorsize = nullptr, CAmount* ancestorfees = nullptr) const;
473
478 bool GetLoadTried() const;
479
484 void SetLoadTried(bool load_tried);
485
486 unsigned long size() const
487 {
488 LOCK(cs);
489 return mapTx.size();
490 }
491
493 {
495 return totalTxSize;
496 }
497
499 {
501 return m_total_fee;
502 }
503
504 bool exists(const Txid& txid) const
505 {
506 LOCK(cs);
507 return (mapTx.count(txid) != 0);
508 }
509
510 bool exists(const Wtxid& wtxid) const
511 {
512 LOCK(cs);
513 return (mapTx.get<index_by_wtxid>().count(wtxid) != 0);
514 }
515
517
518 CTransactionRef get(const Txid& hash) const;
519
520 template <TxidOrWtxid T>
521 TxMempoolInfo info(const T& id) const
522 {
523 LOCK(cs);
524 auto i{GetIter(id)};
525 return i.has_value() ? GetInfo(*i) : TxMempoolInfo{};
526 }
527
529 template <TxidOrWtxid T>
530 TxMempoolInfo info_for_relay(const T& id, uint64_t last_sequence) const
531 {
532 LOCK(cs);
533 auto i{GetIter(id)};
534 return (i.has_value() && i.value()->GetSequence() < last_sequence) ? GetInfo(*i) : TxMempoolInfo{};
535 }
536
537 std::vector<CTxMemPoolEntryRef> entryAll() const EXCLUSIVE_LOCKS_REQUIRED(cs);
538 std::vector<TxMempoolInfo> infoAll() const;
539
540 size_t DynamicMemoryUsage() const;
541
543 void AddUnbroadcastTx(const Txid& txid)
544 {
545 LOCK(cs);
546 // Sanity check the transaction is in the mempool & insert into
547 // unbroadcast set.
548 if (exists(txid)) m_unbroadcast_txids.insert(txid);
549 };
550
551 bool CheckPolicyLimits(const CTransactionRef& tx);
552
554 void RemoveUnbroadcastTx(const Txid& txid, bool unchecked = false);
555
557 std::set<Txid> GetUnbroadcastTxs() const
558 {
559 LOCK(cs);
560 return m_unbroadcast_txids;
561 }
562
565 {
567 return m_unbroadcast_txids.contains(txid);
568 }
569
572 return m_sequence_number++;
573 }
574
576 return m_sequence_number;
577 }
578
579private:
586
587 /* Helper for the public removeRecursive() */
589
590 /* Removal from the mempool also triggers removal of the entry's Ref from txgraph. */
592public:
593 /*
594 * CTxMemPool::ChangeSet:
595 *
596 * This class is used for all mempool additions and associated removals (eg
597 * due to rbf). Removals that don't need to be evaluated for acceptance,
598 * such as removing transactions that appear in a block, or due to reorg,
599 * or removals related to mempool limiting or expiry do not need to use
600 * this.
601 *
602 * Callers can interleave calls to StageAddition()/StageRemoval(), and
603 * removals may be invoked in any order, but additions must be done in a
604 * topological order in the case of transaction packages (ie, parents must
605 * be added before children).
606 *
607 * CalculateChunksForRBF() can be used to calculate the feerate diagram of
608 * the proposed set of new transactions and compare with the existing
609 * mempool.
610 *
611 * CalculateMemPoolAncestors() calculates the in-mempool (not including
612 * what is in the change set itself) ancestors of a given transaction.
613 *
614 * Apply() will apply the removals and additions that are staged into the
615 * mempool.
616 *
617 * Only one changeset may exist at a time. While a changeset is
618 * outstanding, no removals or additions may be made directly to the
619 * mempool.
620 */
621 class ChangeSet {
622 public:
623 explicit ChangeSet(CTxMemPool* pool) : m_pool(pool) { m_pool->m_txgraph->StartStaging(); }
626 if (m_pool->m_txgraph->HaveStaging()) {
627 m_pool->m_txgraph->AbortStaging();
628 }
629 m_pool->m_have_changeset = false;
630 }
631
632 ChangeSet(const ChangeSet&) = delete;
633 ChangeSet& operator=(const ChangeSet&) = delete;
634
636
637 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);
638
640
642
645
647 {
648 // Look up transaction in our cache first
649 auto it = m_ancestors.find(tx);
650 if (it != m_ancestors.end()) return it->second;
651
652 // If not found, try to have the mempool calculate it, and cache
653 // for later.
654 LOCK(m_pool->cs);
656 m_ancestors.try_emplace(tx, ret);
657 return ret;
658 }
659
660 std::vector<CTransactionRef> GetAddedTxns() const {
661 std::vector<CTransactionRef> ret;
662 ret.reserve(m_entry_vec.size());
663 for (const auto& entry : m_entry_vec) {
664 ret.emplace_back(entry->GetSharedTx());
665 }
666 return ret;
667 }
668
676
677 size_t GetTxCount() const { return m_entry_vec.size(); }
678 const CTransaction& GetAddedTxn(size_t index) const { return m_entry_vec.at(index)->GetTx(); }
679
681
682 private:
683 void ProcessDependencies();
684
687 std::vector<CTxMemPool::txiter> m_entry_vec; // track the added transactions' insertion order
688 // map from the m_to_add index to the ancestors for the transaction
692
693 friend class CTxMemPool;
694 };
695
696 std::unique_ptr<ChangeSet> GetChangeSet() EXCLUSIVE_LOCKS_REQUIRED(cs) {
697 Assume(!m_have_changeset);
698 m_have_changeset = true;
699 return std::make_unique<ChangeSet>(this);
700 }
701
702 bool m_have_changeset GUARDED_BY(cs){false};
703
705
706private:
707 // Apply the given changeset to the mempool, by removing transactions in
708 // the to_remove set and adding transactions in the to_add set.
710
711 // addNewTransaction must update state for all ancestors of a given transaction,
712 // to track size/count of descendant transactions. First version of
713 // addNewTransaction can be used to have it call CalculateMemPoolAncestors(), and
714 // then invoke the second version.
715 // Note that addNewTransaction is ONLY called (via Apply()) from ATMP
716 // outside of tests and any other callers may break wallet's in-mempool
717 // tracking (due to lack of CValidationInterface::TransactionAddedToMempool
718 // callbacks).
720public:
721 void StartBlockBuilding() const EXCLUSIVE_LOCKS_REQUIRED(cs) { assert(!m_builder); m_builder = m_txgraph->GetBlockBuilder(); }
722 FeePerWeight GetBlockBuilderChunk(std::vector<CTxMemPoolEntry::CTxMemPoolEntryRef>& entries) const EXCLUSIVE_LOCKS_REQUIRED(cs)
723 {
724 if (!m_builder) { return {}; }
725
726 auto res = m_builder->GetCurrentChunk();
727 if (!res) { return {}; }
728
729 auto [chunk_entries, chunk_feerate] = *res;
730 for (TxGraph::Ref* ref : chunk_entries) {
731 entries.emplace_back(static_cast<const CTxMemPoolEntry&>(*ref));
732 }
733 return chunk_feerate;
734 }
735 void IncludeBuilderChunk() const EXCLUSIVE_LOCKS_REQUIRED(cs) { m_builder->Include(); }
736 void SkipBuilderChunk() const EXCLUSIVE_LOCKS_REQUIRED(cs) { m_builder->Skip(); }
737 void StopBlockBuilding() const EXCLUSIVE_LOCKS_REQUIRED(cs) { m_builder.reset(); }
738};
739
754{
759 std::unordered_map<COutPoint, Coin, SaltedOutpointHasher> m_temp_added;
760
765 mutable std::unordered_set<COutPoint, SaltedOutpointHasher> m_non_base_coins;
766protected:
768
769public:
770 CCoinsViewMemPool(CCoinsView* baseIn, const CTxMemPool& mempoolIn);
773 std::optional<Coin> GetCoin(const COutPoint& outpoint) const override;
778 const std::unordered_set<COutPoint, SaltedOutpointHasher>& GetNonBaseCoins() const { return m_non_base_coins; }
780 void Reset();
781};
782#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:336
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:355
Abstract view on the open txout dataset.
Definition: coins.h:302
CCoinsView that brings transactions from a mempool into view.
Definition: txmempool.h:754
std::optional< Coin > GetCoin(const COutPoint &outpoint) const override
GetCoin, returning whether it exists and is not spent.
Definition: txmempool.cpp:728
void Reset()
Clear m_temp_added and m_non_base_coins.
Definition: txmempool.cpp:758
const std::unordered_set< COutPoint, SaltedOutpointHasher > & GetNonBaseCoins() const
Get all coins in m_non_base_coins.
Definition: txmempool.h:778
std::unordered_map< COutPoint, Coin, SaltedOutpointHasher > m_temp_added
Coins made available by transactions being validated.
Definition: txmempool.h:759
CCoinsViewMemPool(CCoinsView *baseIn, const CTxMemPool &mempoolIn)
Definition: txmempool.cpp:726
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:765
void PackageAddTransaction(const CTransactionRef &tx)
Add the coins created by this transaction.
Definition: txmempool.cpp:751
const CTxMemPool & mempool
Definition: txmempool.h:767
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:677
const CTransaction & GetAddedTxn(size_t index) const
Definition: txmempool.h:678
CTxMemPool::setEntries m_to_remove
Definition: txmempool.h:690
ChangeSet(CTxMemPool *pool)
Definition: txmempool.h:623
void Apply() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Definition: txmempool.cpp:1022
CTxMemPool * m_pool
Definition: txmempool.h:685
void StageRemoval(CTxMemPool::txiter it)
Definition: txmempool.cpp:1015
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:980
CTxMemPool::txiter TxHandle
Definition: txmempool.h:635
CTxMemPool::indexed_transaction_set m_to_add
Definition: txmempool.h:686
~ChangeSet() EXCLUSIVE_LOCKS_REQUIRED(m_pool -> cs)
Definition: txmempool.h:624
ChangeSet(const ChangeSet &)=delete
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)
Definition: txmempool.cpp:991
const CTxMemPool::setEntries & GetRemovals() const
Definition: txmempool.h:641
CTxMemPool::setEntries CalculateMemPoolAncestors(TxHandle tx)
Definition: txmempool.h:646
bool CheckMemPoolPolicyLimits()
Check if any cluster limits are exceeded.
Definition: txmempool.cpp:1057
std::map< CTxMemPool::txiter, CTxMemPool::setEntries, CompareIteratorByHash > m_ancestors
Definition: txmempool.h:689
std::vector< CTransactionRef > GetAddedTxns() const
Definition: txmempool.h:660
ChangeSet & operator=(const ChangeSet &)=delete
std::vector< CTxMemPool::txiter > m_entry_vec
Definition: txmempool.h:687
CTxMemPoolEntry stores data about the corresponding transaction, as well as data about all in-mempool...
Definition: mempool_entry.h:66
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:188
void removeConflicts(const CTransaction &tx) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:376
std::atomic< unsigned int > nTransactionsUpdated
Used by getblocktemplate to trigger CreateNewBlock() invocation.
Definition: txmempool.h:190
void Apply(CTxMemPool::ChangeSet *changeset) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:198
void PrioritiseTransaction(const Txid &hash, const CAmount &nFeeDelta)
Affect CreateNewBlock prioritisation of transactions.
Definition: txmempool.cpp:616
std::unique_ptr< ChangeSet > GetChangeSet() EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:696
bool CompareMiningScoreWithTopology(const Wtxid &hasha, const Wtxid &hashb) const
Definition: txmempool.cpp:542
std::map< Txid, CAmount > mapDeltas GUARDED_BY(cs)
static TxMempoolInfo GetInfo(CTxMemPool::indexed_transaction_set::const_iterator it)
Definition: txmempool.h:289
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:530
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:718
std::vector< const CTxMemPoolEntry * > GetCluster(Txid txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:401
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:695
void ClearPrioritisation(const Txid &hash) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:653
uint64_t cachedInnerUsage GUARDED_BY(cs)
sum of all mempool tx's fees (NOT modified fee)
Definition: txmempool.h:194
std::optional< txiter > GetIter(const Txid &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Returns an iterator to the given hash, if found.
Definition: txmempool.cpp:681
bool GetLoadTried() const
Definition: txmempool.cpp:942
void StopBlockBuilding() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:737
bool exists(const Wtxid &wtxid) const
Definition: txmempool.h:510
CFeeRate GetMinFee() const
The minimum fee to get into the mempool, which may itself not be enough for larger-sized transactions...
Definition: txmempool.h:453
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:261
void trackPackageRemoved(const CFeeRate &rate) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:839
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:326
bool blockSinceLastRollingFeeBump GUARDED_BY(cs)
Definition: txmempool.h:197
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:847
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:929
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:91
void AddTransactionsUpdated(unsigned int n)
Definition: txmempool.cpp:193
bool HasDescendants(const Txid &txid) const
Definition: txmempool.cpp:122
std::vector< indexed_transaction_set::const_iterator > GetSortedScoreWithTopology() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:558
void StartBlockBuilding() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:721
CTransactionRef get(const Txid &hash) const
Definition: txmempool.cpp:607
bool m_have_changeset GUARDED_BY(cs)
Definition: txmempool.h:702
size_t GetUniqueClusterCount(const setEntries &iters_conflicting) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:414
size_t DynamicMemoryUsage() const
Definition: txmempool.cpp:764
const Options m_opts
Definition: txmempool.h:304
std::vector< TxMempoolInfo > infoAll() const
Definition: txmempool.cpp:586
indexed_transaction_set mapTx GUARDED_BY(cs)
TxMempoolInfo info(const T &id) const
Definition: txmempool.h:521
CTxMemPool(Options opts, bilingual_str &error)
Create a new CTxMemPool.
Definition: txmempool.cpp:176
void addNewTransaction(CTxMemPool::txiter it) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:219
void removeUnchecked(txiter entry, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:253
uint64_t totalTxSize GUARDED_BY(cs)
Definition: txmempool.h:192
boost::multi_index_container< CTxMemPoolEntry, CTxMemPoolEntry_Indices > indexed_transaction_set
Definition: txmempool.h:235
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:797
int64_t GetDescendantCount(txiter it) const
Definition: txmempool.h:275
void IncludeBuilderChunk() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:735
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:350
std::vector< FeePerWeight > GetFeerateDiagram() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:1067
std::tuple< size_t, size_t, CAmount > CalculateDescendantData(const CTxMemPoolEntry &entry) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:914
bool exists(const Txid &txid) const
Definition: txmempool.h:504
int64_t lastRollingFeeUpdate GUARDED_BY(cs)
sum of dynamic memory usage of all the map elements (NOT the maps themselves)
Definition: txmempool.h:196
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:705
bool m_load_tried GUARDED_BY(cs)
Definition: txmempool.h:207
static const int ROLLING_FEE_HALFLIFE
Definition: txmempool.h:213
std::set< txiter, CompareIteratorByHash > setEntries
Definition: txmempool.h:269
std::vector< CTxMemPoolEntry::CTxMemPoolEntryRef > GetParents(const CTxMemPoolEntry &entry) const
Definition: txmempool.cpp:74
FeePerWeight GetMainChunkFeerate(const CTxMemPoolEntry &tx) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:398
void ApplyDelta(const Txid &hash, CAmount &nFeeDelta) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:643
void removeForBlock(const std::vector< CTransactionRef > &vtx, unsigned int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:393
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:659
uint64_t GetSequence() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:575
int64_t GetAncestorCount(const CTxMemPoolEntry &e) const
Definition: txmempool.h:277
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:954
indexed_transaction_set::nth_index< 0 >::type::const_iterator txiter
Definition: txmempool.h:266
uint64_t GetAndIncrementSequence() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Guards this internal counter for external reporting.
Definition: txmempool.h:571
bool CheckPolicyLimits(const CTransactionRef &tx)
Definition: txmempool.cpp:786
double rollingMinimumFeeRate GUARDED_BY(cs)
Definition: txmempool.h:198
int64_t GetDescendantCount(const CTxMemPoolEntry &e) const
Definition: txmempool.h:276
void SkipBuilderChunk() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:736
void AddUnbroadcastTx(const Txid &txid)
Adds a transaction to the unbroadcast set.
Definition: txmempool.h:543
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:675
bool IsUnbroadcastTx(const Txid &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Returns whether a txid is in the unbroadcast set.
Definition: txmempool.h:564
std::unique_ptr< TxGraph > m_txgraph GUARDED_BY(cs)
std::set< Txid > GetUnbroadcastTxs() const
Returns transactions in unbroadcast set.
Definition: txmempool.h:557
void CalculateDescendants(txiter it, setEntries &setDescendants) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Populate setDescendants with all in-mempool descendants of given transaction.
Definition: txmempool.cpp:299
std::tuple< size_t, size_t, CAmount > CalculateAncestorData(const CTxMemPoolEntry &entry) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:899
unsigned long size() const
Definition: txmempool.h:486
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:130
void RemoveUnbroadcastTx(const Txid &txid, bool unchecked=false)
Removes a transaction from the unbroadcast set.
Definition: txmempool.cpp:770
void cs_main
Definition: txmempool.h:334
uint64_t m_sequence_number GUARDED_BY(cs)
minimum fee to get into the pool, decreases exponentially
Definition: txmempool.h:203
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:948
void RemoveStaged(setEntries &stage, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
Remove a set of transactions from the mempool.
Definition: txmempool.cpp:779
std::unique_ptr< TxGraph::BlockBuilder > m_builder GUARDED_BY(cs)
std::vector< CTxMemPoolEntryRef > entryAll() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:574
CAmount GetTotalFee() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:498
indirectmap< COutPoint, txiter > mapNextTx GUARDED_BY(cs)
uint64_t GetTotalTxSize() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:492
bool isSpent(const COutPoint &outpoint) const
Definition: txmempool.cpp:182
FeePerWeight GetBlockBuilderChunk(std::vector< CTxMemPoolEntry::CTxMemPoolEntryRef > &entries) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:722
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:600
unsigned int GetTransactionsUpdated() const
Definition: txmempool.cpp:188
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:193
Definition: txmempool.h:97
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
Definition: txmempool.h:99
@ 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:231
const CAmount delta
The fee delta added using PrioritiseTransaction().
Definition: txmempool.h:356
const bool in_mempool
Whether this transaction is in the mempool.
Definition: txmempool.h:354
std::optional< CAmount > modified_fee
The modified fee (base fee + delta) of this entry.
Definition: txmempool.h:358
const Txid txid
The prioritised transaction's txid.
Definition: txmempool.h:360
Tagged wrapper around FeeFrac to avoid unit confusion.
Definition: feefrac.h:239
Information about a mempool transaction.
Definition: txmempool.h:113
int64_t nFeeDelta
The fee delta.
Definition: txmempool.h:127
int32_t vsize
Virtual size of the transaction.
Definition: txmempool.h:124
CAmount fee
Fee of the transaction.
Definition: txmempool.h:121
CTransactionRef tx
The transaction itself.
Definition: txmempool.h:115
std::chrono::seconds m_time
Time the transaction entered the mempool.
Definition: txmempool.h:118
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:70
result_type operator()(const CTxMemPoolEntry &entry) const
Definition: txmempool.h:85
#define LOCK(cs)
Definition: sync.h:258
#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:55
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:59
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:81
AssertLockHeld(pool.cs)
assert(!tx.IsCoinBase())