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
54static constexpr uint64_t ACCEPTABLE_COST = 75'000;
55
58static constexpr uint64_t POST_CHANGE_COST = 5 * ACCEPTABLE_COST;
59
64
65// extracts a transaction hash from CTxMemPoolEntry or CTransactionRef
67{
70 {
71 return entry.GetTx().GetHash();
72 }
73
75 {
76 return tx->GetHash();
77 }
78};
79
80// extracts a transaction witness-hash from CTxMemPoolEntry or CTransactionRef
82{
85 {
86 return entry.GetTx().GetWitnessHash();
87 }
88
90 {
91 return tx->GetWitnessHash();
92 }
93};
94
96{
97public:
98 bool operator()(const CTxMemPoolEntry& a, const CTxMemPoolEntry& b) const
99 {
100 return a.GetTime() < b.GetTime();
101 }
102};
103
104// Multi_index tag names
105struct entry_time {};
107
112{
115
117 std::chrono::seconds m_time;
118
121
123 int32_t vsize;
124
126 int64_t nFeeDelta;
127};
128
187{
188protected:
189 std::atomic<unsigned int> nTransactionsUpdated{0};
190
191 uint64_t totalTxSize GUARDED_BY(cs){0};
192 CAmount m_total_fee GUARDED_BY(cs){0};
193 uint64_t cachedInnerUsage GUARDED_BY(cs){0};
194
195 mutable int64_t lastRollingFeeUpdate GUARDED_BY(cs){GetTime()};
196 mutable bool blockSinceLastRollingFeeBump GUARDED_BY(cs){false};
197 mutable double rollingMinimumFeeRate GUARDED_BY(cs){0};
198
199 // In-memory counter for external mempool tracking purposes.
200 // This number is incremented once every time a transaction
201 // is added or removed from the mempool for any reason.
202 mutable uint64_t m_sequence_number GUARDED_BY(cs){1};
203
205
206 bool m_load_tried GUARDED_BY(cs){false};
207
208 CFeeRate GetMinFee(size_t sizelimit) const;
209
210public:
211
212 static const int ROLLING_FEE_HALFLIFE = 60 * 60 * 12; // public only for testing
213
214 struct CTxMemPoolEntry_Indices final : boost::multi_index::indexed_by<
215 // sorted by txid
216 boost::multi_index::hashed_unique<mempoolentry_txid, SaltedTxidHasher>,
217 // sorted by wtxid
218 boost::multi_index::hashed_unique<
219 boost::multi_index::tag<index_by_wtxid>,
220 mempoolentry_wtxid,
221 SaltedWtxidHasher
222 >,
223 // sorted by entry time
224 boost::multi_index::ordered_non_unique<
225 boost::multi_index::tag<entry_time>,
226 boost::multi_index::identity<CTxMemPoolEntry>,
227 CompareTxMemPoolEntryByEntryTime
228 >
229 >
230 {};
231 typedef boost::multi_index_container<
235
261 std::unique_ptr<TxGraph> m_txgraph GUARDED_BY(cs);
262 mutable std::unique_ptr<TxGraph::BlockBuilder> m_builder GUARDED_BY(cs);
264
265 using txiter = indexed_transaction_set::nth_index<0>::type::const_iterator;
266 std::vector<std::pair<Wtxid, txiter>> txns_randomized GUARDED_BY(cs);
267
268 typedef std::set<txiter, CompareIteratorByHash> setEntries;
269
271
272 std::tuple<size_t, size_t, CAmount> CalculateAncestorData(const CTxMemPoolEntry& entry) const EXCLUSIVE_LOCKS_REQUIRED(cs);
273 std::tuple<size_t, size_t, CAmount> CalculateDescendantData(const CTxMemPoolEntry& entry) const EXCLUSIVE_LOCKS_REQUIRED(cs);
274 int64_t GetDescendantCount(txiter it) const { LOCK(cs); return m_txgraph->GetDescendants(*it, TxGraph::Level::MAIN).size(); }
275 int64_t GetDescendantCount(const CTxMemPoolEntry &e) const { LOCK(cs); return m_txgraph->GetDescendants(e, TxGraph::Level::MAIN).size(); }
276 int64_t GetAncestorCount(const CTxMemPoolEntry &e) const { LOCK(cs); return m_txgraph->GetAncestors(e, TxGraph::Level::MAIN).size(); }
277 std::vector<CTxMemPoolEntry::CTxMemPoolEntryRef> GetChildren(const CTxMemPoolEntry &entry) const;
278 std::vector<CTxMemPoolEntry::CTxMemPoolEntryRef> GetParents(const CTxMemPoolEntry &entry) const;
279
280private:
281 std::vector<indexed_transaction_set::const_iterator> GetSortedScoreWithTopology() const EXCLUSIVE_LOCKS_REQUIRED(cs);
282
286 std::set<Txid> m_unbroadcast_txids GUARDED_BY(cs);
287
289 {
290 return TxMempoolInfo{it->GetSharedTx(), it->GetTime(), it->GetFee(), it->GetTxSize(), it->GetModifiedFee() - it->GetFee()};
291 }
292
293 // Helper to remove all transactions that conflict with a given
294 // transaction (used for transactions appearing in a block).
296
297public:
299 std::map<Txid, CAmount> mapDeltas GUARDED_BY(cs);
300
302
304
310 explicit CTxMemPool(Options opts, bilingual_str& error);
311
318 void check(const CCoinsViewCache& active_coins_tip, int64_t spendheight) const EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
319
333 void removeForReorg(CChain& chain, std::function<bool(txiter)> filter_final_and_mature) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main);
334 void removeForBlock(const std::vector<CTransactionRef>& vtx, unsigned int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(cs);
335
336 bool CompareMiningScoreWithTopology(const Wtxid& hasha, const Wtxid& hashb) const;
337 bool isSpent(const COutPoint& outpoint) const;
338 unsigned int GetTransactionsUpdated() const;
339 void AddTransactionsUpdated(unsigned int n);
345
347 void PrioritiseTransaction(const Txid& hash, const CAmount& nFeeDelta);
348 void ApplyDelta(const Txid& hash, CAmount &nFeeDelta) const EXCLUSIVE_LOCKS_REQUIRED(cs);
350
351 struct delta_info {
353 const bool in_mempool;
357 std::optional<CAmount> modified_fee;
359 const Txid txid;
360 };
362 std::vector<delta_info> GetPrioritisedTransactions() const EXCLUSIVE_LOCKS_REQUIRED(!cs);
363
365 const CTransaction* GetConflictTx(const COutPoint& prevout) const EXCLUSIVE_LOCKS_REQUIRED(cs);
366
368 std::optional<txiter> GetIter(const Txid& txid) const EXCLUSIVE_LOCKS_REQUIRED(cs);
369 std::optional<txiter> GetIter(const Wtxid& wtxid) const EXCLUSIVE_LOCKS_REQUIRED(cs);
370
374 setEntries GetIterSet(const std::set<Txid>& hashes) const EXCLUSIVE_LOCKS_REQUIRED(cs);
375
379 std::vector<txiter> GetIterVec(const std::vector<Txid>& txids) const EXCLUSIVE_LOCKS_REQUIRED(cs);
380
394 void UpdateTransactionsFromBlock(const std::vector<Txid>& vHashesToUpdate) EXCLUSIVE_LOCKS_REQUIRED(cs, cs_main);
395
398 return m_txgraph->GetMainChunkFeerate(tx);
399 }
400 std::vector<const CTxMemPoolEntry*> GetCluster(Txid txid) const EXCLUSIVE_LOCKS_REQUIRED(cs) {
401 auto tx = GetIter(txid);
402 if (!tx) return {};
403 auto cluster = m_txgraph->GetCluster(**tx, TxGraph::Level::MAIN);
404 std::vector<const CTxMemPoolEntry*> ret;
405 ret.reserve(cluster.size());
406 for (const auto& tx : cluster) {
407 ret.emplace_back(static_cast<const CTxMemPoolEntry*>(tx));
408 }
409 return ret;
410 }
411
412
413 size_t GetUniqueClusterCount(const setEntries& iters_conflicting) const EXCLUSIVE_LOCKS_REQUIRED(cs) {
414 std::vector<const TxGraph::Ref *> entries;
415 entries.reserve(iters_conflicting.size());
416 for (auto it : iters_conflicting) {
417 entries.emplace_back(&*it);
418 }
419 Assume(!m_txgraph->IsOversized(TxGraph::Level::MAIN));
420 return m_txgraph->CountDistinctClusters(entries, TxGraph::Level::MAIN);
421 }
422
431
432 bool HasDescendants(const Txid& txid) const;
433
438 std::vector<txiter> GatherClusters(const std::vector<Txid>& txids) const EXCLUSIVE_LOCKS_REQUIRED(cs);
439
443 void CalculateDescendants(txiter it, setEntries& setDescendants) const EXCLUSIVE_LOCKS_REQUIRED(cs);
445
454 }
455
460 void TrimToSize(size_t sizelimit, std::vector<COutPoint>* pvNoSpendsRemaining = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs);
461
463 int Expire(std::chrono::seconds time) EXCLUSIVE_LOCKS_REQUIRED(cs);
464
471 void GetTransactionAncestry(const Txid& txid, size_t& ancestors, size_t& cluster_count, size_t* ancestorsize = nullptr, CAmount* ancestorfees = nullptr) const;
472
477 bool GetLoadTried() const;
478
483 void SetLoadTried(bool load_tried);
484
485 unsigned long size() const
486 {
487 LOCK(cs);
488 return mapTx.size();
489 }
490
492 {
494 return totalTxSize;
495 }
496
498 {
500 return m_total_fee;
501 }
502
503 bool exists(const Txid& txid) const
504 {
505 LOCK(cs);
506 return (mapTx.count(txid) != 0);
507 }
508
509 bool exists(const Wtxid& wtxid) const
510 {
511 LOCK(cs);
512 return (mapTx.get<index_by_wtxid>().count(wtxid) != 0);
513 }
514
516
517 CTransactionRef get(const Txid& hash) const;
518
519 template <TxidOrWtxid T>
520 TxMempoolInfo info(const T& id) const
521 {
522 LOCK(cs);
523 auto i{GetIter(id)};
524 return i.has_value() ? GetInfo(*i) : TxMempoolInfo{};
525 }
526
528 template <TxidOrWtxid T>
529 TxMempoolInfo info_for_relay(const T& id, uint64_t last_sequence) const
530 {
531 LOCK(cs);
532 auto i{GetIter(id)};
533 return (i.has_value() && i.value()->GetSequence() < last_sequence) ? GetInfo(*i) : TxMempoolInfo{};
534 }
535
536 std::vector<CTxMemPoolEntryRef> entryAll() const EXCLUSIVE_LOCKS_REQUIRED(cs);
537 std::vector<TxMempoolInfo> infoAll() const;
538
539 size_t DynamicMemoryUsage() const;
540
542 void AddUnbroadcastTx(const Txid& txid)
543 {
544 LOCK(cs);
545 // Sanity check the transaction is in the mempool & insert into
546 // unbroadcast set.
547 if (exists(txid)) m_unbroadcast_txids.insert(txid);
548 };
549
550 bool CheckPolicyLimits(const CTransactionRef& tx);
551
553 void RemoveUnbroadcastTx(const Txid& txid, bool unchecked = false);
554
556 std::set<Txid> GetUnbroadcastTxs() const
557 {
558 LOCK(cs);
559 return m_unbroadcast_txids;
560 }
561
564 {
566 return m_unbroadcast_txids.contains(txid);
567 }
568
571 return m_sequence_number++;
572 }
573
575 return m_sequence_number;
576 }
577
578private:
585
586 /* Helper for the public removeRecursive() */
588
589 /* Removal from the mempool also triggers removal of the entry's Ref from txgraph. */
591public:
592 /*
593 * CTxMemPool::ChangeSet:
594 *
595 * This class is used for all mempool additions and associated removals (eg
596 * due to rbf). Removals that don't need to be evaluated for acceptance,
597 * such as removing transactions that appear in a block, or due to reorg,
598 * or removals related to mempool limiting or expiry do not need to use
599 * this.
600 *
601 * Callers can interleave calls to StageAddition()/StageRemoval(), and
602 * removals may be invoked in any order, but additions must be done in a
603 * topological order in the case of transaction packages (ie, parents must
604 * be added before children).
605 *
606 * CalculateChunksForRBF() can be used to calculate the feerate diagram of
607 * the proposed set of new transactions and compare with the existing
608 * mempool.
609 *
610 * CalculateMemPoolAncestors() calculates the in-mempool (not including
611 * what is in the change set itself) ancestors of a given transaction.
612 *
613 * Apply() will apply the removals and additions that are staged into the
614 * mempool.
615 *
616 * Only one changeset may exist at a time. While a changeset is
617 * outstanding, no removals or additions may be made directly to the
618 * mempool.
619 */
620 class ChangeSet {
621 public:
622 explicit ChangeSet(CTxMemPool* pool) : m_pool(pool) { m_pool->m_txgraph->StartStaging(); }
625 if (m_pool->m_txgraph->HaveStaging()) {
626 m_pool->m_txgraph->AbortStaging();
627 }
628 m_pool->m_have_changeset = false;
629 }
630
631 ChangeSet(const ChangeSet&) = delete;
632 ChangeSet& operator=(const ChangeSet&) = delete;
633
635
636 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);
637
639
641
644
646 {
647 // Look up transaction in our cache first
648 auto it = m_ancestors.find(tx);
649 if (it != m_ancestors.end()) return it->second;
650
651 // If not found, try to have the mempool calculate it, and cache
652 // for later.
653 LOCK(m_pool->cs);
655 m_ancestors.try_emplace(tx, ret);
656 return ret;
657 }
658
659 std::vector<CTransactionRef> GetAddedTxns() const {
660 std::vector<CTransactionRef> ret;
661 ret.reserve(m_entry_vec.size());
662 for (const auto& entry : m_entry_vec) {
663 ret.emplace_back(entry->GetSharedTx());
664 }
665 return ret;
666 }
667
675
676 size_t GetTxCount() const { return m_entry_vec.size(); }
677 const CTransaction& GetAddedTxn(size_t index) const { return m_entry_vec.at(index)->GetTx(); }
678
680
681 private:
682 void ProcessDependencies();
683
686 std::vector<CTxMemPool::txiter> m_entry_vec; // track the added transactions' insertion order
687 // map from the m_to_add index to the ancestors for the transaction
691
692 friend class CTxMemPool;
693 };
694
695 std::unique_ptr<ChangeSet> GetChangeSet() EXCLUSIVE_LOCKS_REQUIRED(cs) {
696 Assume(!m_have_changeset);
697 m_have_changeset = true;
698 return std::make_unique<ChangeSet>(this);
699 }
700
701 bool m_have_changeset GUARDED_BY(cs){false};
702
704
705private:
706 // Apply the given changeset to the mempool, by removing transactions in
707 // the to_remove set and adding transactions in the to_add set.
709
710 // addNewTransaction must update state for all ancestors of a given transaction,
711 // to track size/count of descendant transactions. First version of
712 // addNewTransaction can be used to have it call CalculateMemPoolAncestors(), and
713 // then invoke the second version.
714 // Note that addNewTransaction is ONLY called (via Apply()) from ATMP
715 // outside of tests and any other callers may break wallet's in-mempool
716 // tracking (due to lack of CValidationInterface::TransactionAddedToMempool
717 // callbacks).
719public:
720 void StartBlockBuilding() const EXCLUSIVE_LOCKS_REQUIRED(cs) { assert(!m_builder); m_builder = m_txgraph->GetBlockBuilder(); }
721 FeePerWeight GetBlockBuilderChunk(std::vector<CTxMemPoolEntry::CTxMemPoolEntryRef>& entries) const EXCLUSIVE_LOCKS_REQUIRED(cs)
722 {
723 if (!m_builder) { return {}; }
724
725 auto res = m_builder->GetCurrentChunk();
726 if (!res) { return {}; }
727
728 auto [chunk_entries, chunk_feerate] = *res;
729 for (TxGraph::Ref* ref : chunk_entries) {
730 entries.emplace_back(static_cast<const CTxMemPoolEntry&>(*ref));
731 }
732 return chunk_feerate;
733 }
734 void IncludeBuilderChunk() const EXCLUSIVE_LOCKS_REQUIRED(cs) { m_builder->Include(); }
735 void SkipBuilderChunk() const EXCLUSIVE_LOCKS_REQUIRED(cs) { m_builder->Skip(); }
736 void StopBlockBuilding() const EXCLUSIVE_LOCKS_REQUIRED(cs) { m_builder.reset(); }
737};
738
753{
758 std::unordered_map<COutPoint, Coin, SaltedOutpointHasher> m_temp_added;
759
764 mutable std::unordered_set<COutPoint, SaltedOutpointHasher> m_non_base_coins;
765protected:
767
768public:
769 CCoinsViewMemPool(CCoinsView* baseIn, const CTxMemPool& mempoolIn);
772 std::optional<Coin> GetCoin(const COutPoint& outpoint) const override;
777 const std::unordered_set<COutPoint, SaltedOutpointHasher>& GetNonBaseCoins() const { return m_non_base_coins; }
779 void Reset();
780};
781#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:380
CCoinsView backed by another CCoinsView.
Definition: coins.h:348
CCoinsView that adds a memory cache for transactions to another CCoinsView.
Definition: coins.h:368
Abstract view on the open txout dataset.
Definition: coins.h:308
CCoinsView that brings transactions from a mempool into view.
Definition: txmempool.h:753
std::optional< Coin > GetCoin(const COutPoint &outpoint) const override
GetCoin, returning whether it exists and is not spent.
Definition: txmempool.cpp:742
void Reset()
Clear m_temp_added and m_non_base_coins.
Definition: txmempool.cpp:772
const std::unordered_set< COutPoint, SaltedOutpointHasher > & GetNonBaseCoins() const
Get all coins in m_non_base_coins.
Definition: txmempool.h:777
std::unordered_map< COutPoint, Coin, SaltedOutpointHasher > m_temp_added
Coins made available by transactions being validated.
Definition: txmempool.h:758
CCoinsViewMemPool(CCoinsView *baseIn, const CTxMemPool &mempoolIn)
Definition: txmempool.cpp:740
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:764
void PackageAddTransaction(const CTransactionRef &tx)
Add the coins created by this transaction.
Definition: txmempool.cpp:765
const CTxMemPool & mempool
Definition: txmempool.h:766
Fee rate in satoshis per virtualbyte: CAmount / vB the feerate is represented internally as FeeFrac.
Definition: feerate.h:32
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:676
const CTransaction & GetAddedTxn(size_t index) const
Definition: txmempool.h:677
CTxMemPool::setEntries m_to_remove
Definition: txmempool.h:689
ChangeSet(CTxMemPool *pool)
Definition: txmempool.h:622
void Apply() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
Definition: txmempool.cpp:1037
CTxMemPool * m_pool
Definition: txmempool.h:684
void StageRemoval(CTxMemPool::txiter it)
Definition: txmempool.cpp:1030
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:994
CTxMemPool::txiter TxHandle
Definition: txmempool.h:634
CTxMemPool::indexed_transaction_set m_to_add
Definition: txmempool.h:685
~ChangeSet() EXCLUSIVE_LOCKS_REQUIRED(m_pool -> cs)
Definition: txmempool.h:623
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:1005
const CTxMemPool::setEntries & GetRemovals() const
Definition: txmempool.h:640
CTxMemPool::setEntries CalculateMemPoolAncestors(TxHandle tx)
Definition: txmempool.h:645
bool CheckMemPoolPolicyLimits()
Check if any cluster limits are exceeded.
Definition: txmempool.cpp:1072
std::map< CTxMemPool::txiter, CTxMemPool::setEntries, CompareIteratorByHash > m_ancestors
Definition: txmempool.h:688
std::vector< CTransactionRef > GetAddedTxns() const
Definition: txmempool.h:659
ChangeSet & operator=(const ChangeSet &)=delete
std::vector< CTxMemPool::txiter > m_entry_vec
Definition: txmempool.h:686
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:187
void removeConflicts(const CTransaction &tx) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:388
std::atomic< unsigned int > nTransactionsUpdated
Used by getblocktemplate to trigger CreateNewBlock() invocation.
Definition: txmempool.h:189
void Apply(CTxMemPool::ChangeSet *changeset) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:206
void PrioritiseTransaction(const Txid &hash, const CAmount &nFeeDelta)
Affect CreateNewBlock prioritisation of transactions.
Definition: txmempool.cpp:630
std::unique_ptr< ChangeSet > GetChangeSet() EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:695
bool CompareMiningScoreWithTopology(const Wtxid &hasha, const Wtxid &hashb) const
Definition: txmempool.cpp:556
std::map< Txid, CAmount > mapDeltas GUARDED_BY(cs)
static TxMempoolInfo GetInfo(CTxMemPool::indexed_transaction_set::const_iterator it)
Definition: txmempool.h:288
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:529
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:732
std::vector< const CTxMemPoolEntry * > GetCluster(Txid txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:400
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:709
void ClearPrioritisation(const Txid &hash) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:667
uint64_t cachedInnerUsage GUARDED_BY(cs)
sum of all mempool tx's fees (NOT modified fee)
Definition: txmempool.h:193
std::optional< txiter > GetIter(const Txid &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Returns an iterator to the given hash, if found.
Definition: txmempool.cpp:695
bool GetLoadTried() const
Definition: txmempool.cpp:956
void StopBlockBuilding() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:736
bool exists(const Wtxid &wtxid) const
Definition: txmempool.h:509
CFeeRate GetMinFee() const
The minimum fee to get into the mempool, which may itself not be enough for larger-sized transactions...
Definition: txmempool.h:452
RecursiveMutex cs
This mutex needs to be locked when accessing mapTx or other members that are guarded by it.
Definition: txmempool.h:260
void trackPackageRemoved(const CFeeRate &rate) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:853
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:325
bool blockSinceLastRollingFeeBump GUARDED_BY(cs)
Definition: txmempool.h:196
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:861
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:943
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:201
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:572
void StartBlockBuilding() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:720
CTransactionRef get(const Txid &hash) const
Definition: txmempool.cpp:621
bool m_have_changeset GUARDED_BY(cs)
Definition: txmempool.h:701
size_t GetUniqueClusterCount(const setEntries &iters_conflicting) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:413
size_t DynamicMemoryUsage() const
Definition: txmempool.cpp:778
const Options m_opts
Definition: txmempool.h:303
std::vector< TxMempoolInfo > infoAll() const
Definition: txmempool.cpp:600
indexed_transaction_set mapTx GUARDED_BY(cs)
TxMempoolInfo info(const T &id) const
Definition: txmempool.h:520
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:229
void removeUnchecked(txiter entry, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:263
uint64_t totalTxSize GUARDED_BY(cs)
Definition: txmempool.h:191
boost::multi_index_container< CTxMemPoolEntry, CTxMemPoolEntry_Indices > indexed_transaction_set
Definition: txmempool.h:234
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:811
int64_t GetDescendantCount(txiter it) const
Definition: txmempool.h:274
void IncludeBuilderChunk() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:734
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:360
std::vector< FeePerWeight > GetFeerateDiagram() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:1082
std::tuple< size_t, size_t, CAmount > CalculateDescendantData(const CTxMemPoolEntry &entry) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:928
bool exists(const Txid &txid) const
Definition: txmempool.h:503
int64_t lastRollingFeeUpdate GUARDED_BY(cs)
sum of dynamic memory usage of all the map elements (NOT the maps themselves)
Definition: txmempool.h:195
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:719
bool m_load_tried GUARDED_BY(cs)
Definition: txmempool.h:206
static const int ROLLING_FEE_HALFLIFE
Definition: txmempool.h:212
std::set< txiter, CompareIteratorByHash > setEntries
Definition: txmempool.h:268
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:397
void ApplyDelta(const Txid &hash, CAmount &nFeeDelta) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:657
void removeForBlock(const std::vector< CTransactionRef > &vtx, unsigned int nBlockHeight) EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:405
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:673
uint64_t GetSequence() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:574
int64_t GetAncestorCount(const CTxMemPoolEntry &e) const
Definition: txmempool.h:276
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:968
indexed_transaction_set::nth_index< 0 >::type::const_iterator txiter
Definition: txmempool.h:265
uint64_t GetAndIncrementSequence() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Guards this internal counter for external reporting.
Definition: txmempool.h:570
bool CheckPolicyLimits(const CTransactionRef &tx)
Definition: txmempool.cpp:800
double rollingMinimumFeeRate GUARDED_BY(cs)
Definition: txmempool.h:197
int64_t GetDescendantCount(const CTxMemPoolEntry &e) const
Definition: txmempool.h:275
void SkipBuilderChunk() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:735
void AddUnbroadcastTx(const Txid &txid)
Adds a transaction to the unbroadcast set.
Definition: txmempool.h:542
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:689
bool IsUnbroadcastTx(const Txid &txid) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Returns whether a txid is in the unbroadcast set.
Definition: txmempool.h:563
std::unique_ptr< TxGraph > m_txgraph GUARDED_BY(cs)
std::set< Txid > GetUnbroadcastTxs() const
Returns transactions in unbroadcast set.
Definition: txmempool.h:556
void CalculateDescendants(txiter it, setEntries &setDescendants) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Populate setDescendants with all in-mempool descendants of given transaction.
Definition: txmempool.cpp:309
std::tuple< size_t, size_t, CAmount > CalculateAncestorData(const CTxMemPoolEntry &entry) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:913
unsigned long size() const
Definition: txmempool.h:485
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:784
void cs_main
Definition: txmempool.h:333
uint64_t m_sequence_number GUARDED_BY(cs)
minimum fee to get into the pool, decreases exponentially
Definition: txmempool.h:202
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:962
void RemoveStaged(setEntries &stage, MemPoolRemovalReason reason) EXCLUSIVE_LOCKS_REQUIRED(cs)
Remove a set of transactions from the mempool.
Definition: txmempool.cpp:793
std::unique_ptr< TxGraph::BlockBuilder > m_builder GUARDED_BY(cs)
std::vector< CTxMemPoolEntryRef > entryAll() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.cpp:588
CAmount GetTotalFee() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:497
indirectmap< COutPoint, txiter > mapNextTx GUARDED_BY(cs)
uint64_t GetTotalTxSize() const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:491
bool isSpent(const COutPoint &outpoint) const
Definition: txmempool.cpp:190
FeePerWeight GetBlockBuilderChunk(std::vector< CTxMemPoolEntry::CTxMemPoolEntryRef > &entries) const EXCLUSIVE_LOCKS_REQUIRED(cs)
Definition: txmempool.h:721
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:614
unsigned int GetTransactionsUpdated() const
Definition: txmempool.cpp:196
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:192
Definition: txmempool.h:96
bool operator()(const CTxMemPoolEntry &a, const CTxMemPoolEntry &b) const
Definition: txmempool.h:98
@ 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)
Definition: common.h:29
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:403
Definition: txmempool.h:230
const CAmount delta
The fee delta added using PrioritiseTransaction().
Definition: txmempool.h:355
const bool in_mempool
Whether this transaction is in the mempool.
Definition: txmempool.h:353
std::optional< CAmount > modified_fee
The modified fee (base fee + delta) of this entry.
Definition: txmempool.h:357
const Txid txid
The prioritised transaction's txid.
Definition: txmempool.h:359
Tagged wrapper around FeeFrac to avoid unit confusion.
Definition: feefrac.h:239
Information about a mempool transaction.
Definition: txmempool.h:112
int64_t nFeeDelta
The fee delta.
Definition: txmempool.h:126
int32_t vsize
Virtual size of the transaction.
Definition: txmempool.h:123
CAmount fee
Fee of the transaction.
Definition: txmempool.h:120
CTransactionRef tx
The transaction itself.
Definition: txmempool.h:114
std::chrono::seconds m_time
Time the transaction entered the mempool.
Definition: txmempool.h:117
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:69
result_type operator()(const CTxMemPoolEntry &entry) const
Definition: txmempool.h:84
#define LOCK(cs)
Definition: sync.h:258
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:51
static constexpr uint64_t ACCEPTABLE_COST
How much linearization cost required for TxGraph clusters to have "acceptable" quality,...
Definition: txmempool.h:54
static constexpr uint64_t POST_CHANGE_COST
How much work we ask TxGraph to do after a mempool change occurs (either due to a changeset being app...
Definition: txmempool.h:58
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())