Bitcoin ABC 0.30.5
P2P Digital Currency
txpool.h
Go to the documentation of this file.
1// Copyright (c) 2021 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#ifndef BITCOIN_TXPOOL_H
6#define BITCOIN_TXPOOL_H
7
8#include <nodeid.h>
9#include <primitives/block.h>
11#include <sync.h>
12#include <util/time.h>
13
14#include <chrono>
15#include <map>
16#include <set>
17#include <vector>
18
20
24class TxPool {
25public:
26 TxPool(const std::string &txKindIn, std::chrono::seconds expireTimeIn,
27 std::chrono::seconds expireIntervalIn)
28 : txKind(txKindIn), expireTime(expireTimeIn),
29 expireInterval(expireIntervalIn) {}
30
32 bool AddTx(const CTransactionRef &tx, NodeId peer)
34
36 bool HaveTx(const TxId &txid) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
37
38 CTransactionRef GetTx(const TxId &txid) const
40 std::vector<CTransactionRef> GetConflictTxs(const CTransactionRef &tx) const
42
52
55
60
63
65 unsigned int LimitTxs(unsigned int max_txs, FastRandomContext &rng)
67
72 void AddChildrenToWorkSet(const CTransaction &tx)
74
77
82 std::vector<CTransactionRef>
83 GetChildrenFromSamePeer(const CTransactionRef &parent, NodeId nodeid) const
85
90 std::vector<std::pair<CTransactionRef, NodeId>>
92 NodeId nodeid) const
94
98 return m_pool_txs.size();
99 }
100
101protected:
103 const std::string txKind;
105 const std::chrono::seconds expireTime;
107 const std::chrono::seconds expireInterval;
108
110 mutable Mutex m_mutex;
111
112 struct PoolTx {
116 size_t list_pos;
117 };
118
123 std::map<TxId, PoolTx> m_pool_txs GUARDED_BY(m_mutex);
124
126 std::map<NodeId, std::set<TxId>> m_peer_work_set GUARDED_BY(m_mutex);
127
128 using PoolTxMap = decltype(m_pool_txs);
129
131 template <typename I> bool operator()(const I &a, const I &b) const {
132 return a->first < b->first;
133 }
134 };
135
140 std::map<COutPoint, std::set<PoolTxMap ::iterator, IteratorComparator>>
141 m_outpoint_to_tx_it GUARDED_BY(m_mutex);
142
144 std::vector<PoolTxMap::iterator> m_txs_list GUARDED_BY(m_mutex);
145
148
150 NodeSeconds m_next_sweep GUARDED_BY(m_mutex){0s};
151};
152
153#endif // BITCOIN_TXPOOL_H
Definition: block.h:60
Fast randomness source.
Definition: random.h:156
A class to store and track transactions by peers.
Definition: txpool.h:24
CTransactionRef GetTx(const TxId &txid) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Definition: txpool.cpp:179
int EraseTx(const TxId &txid) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Erase a tx by txid.
Definition: txpool.cpp:50
TxPool(const std::string &txKindIn, std::chrono::seconds expireTimeIn, std::chrono::seconds expireIntervalIn)
Definition: txpool.h:26
const std::chrono::seconds expireInterval
Minimum time between transactions expire time checks.
Definition: txpool.h:107
void EraseForPeer(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Erase all txs announced by a peer (eg, after that peer disconnects)
Definition: txpool.cpp:94
std::vector< CTransactionRef > GetChildrenFromSamePeer(const CTransactionRef &parent, NodeId nodeid) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Get all children that spend from this tx and were received from nodeid.
Definition: txpool.cpp:277
size_t Size() const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Return how many entries exist in the pool.
Definition: txpool.h:96
bool AddTx(const CTransactionRef &tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Add a new transaction to the pool.
Definition: txpool.cpp:15
int EraseTxNoLock(const TxId &txid) EXCLUSIVE_LOCKS_REQUIRED(m_mutex)
Erase a transaction by txid.
Definition: txpool.cpp:55
unsigned int LimitTxs(unsigned int max_txs, FastRandomContext &rng) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Limit the txs to the given maximum.
Definition: txpool.cpp:115
NodeSeconds m_next_sweep GUARDED_BY(m_mutex)
Timestamp for the next scheduled sweep of expired transactions.
Definition: txpool.h:150
void EraseForBlock(const CBlock &block) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Erase all txs included in or invalidated by a new block.
Definition: txpool.cpp:239
const std::chrono::seconds expireTime
Expiration time for transactions.
Definition: txpool.h:105
std::vector< CTransactionRef > GetConflictTxs(const CTransactionRef &tx) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Definition: txpool.cpp:191
void AddChildrenToWorkSet(const CTransaction &tx) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Add any tx that list a particular tx as a parent into the from peer's work set.
Definition: txpool.cpp:151
const std::string txKind
The transaction kind as string, used for logging.
Definition: txpool.h:103
std::map< COutPoint, std::set< PoolTxMap ::iterator, IteratorComparator > > m_outpoint_to_tx_it GUARDED_BY(m_mutex)
Index from the parents' COutPoint into the m_pool_txs.
std::map< TxId, PoolTx > m_pool_txs GUARDED_BY(m_mutex)
Map from txid to pool transaction record.
Mutex m_mutex
Guards transactions.
Definition: txpool.h:110
std::vector< PoolTxMap::iterator > m_txs_list GUARDED_BY(m_mutex)
Pool transactions in vector for quick random eviction.
bool HaveTx(const TxId &txid) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Check if we already have an the transaction.
Definition: txpool.cpp:174
CTransactionRef GetTxToReconsider(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Extract a transaction from a peer's work set.
Definition: txpool.cpp:209
std::vector< std::pair< CTransactionRef, NodeId > > GetChildrenFromDifferentPeer(const CTransactionRef &parent, NodeId nodeid) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Get all children that spend from this tx but were not received from nodeid.
Definition: txpool.cpp:322
bool HaveTxToReconsider(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Does this peer have any work to do?
Definition: txpool.cpp:228
std::map< NodeId, std::set< TxId > > m_peer_work_set GUARDED_BY(m_mutex)
Which peer provided the transactions that need to be reconsidered.
decltype(m_pool_txs) PoolTxMap
Definition: txpool.h:128
int64_t NodeId
Definition: nodeid.h:10
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:315
A TxId is the identifier of a transaction.
Definition: txid.h:14
bool operator()(const I &a, const I &b) const
Definition: txpool.h:131
NodeId fromPeer
Definition: txpool.h:114
size_t list_pos
Definition: txpool.h:116
NodeSeconds nTimeExpire
Definition: txpool.h:115
CTransactionRef tx
Definition: txpool.h:113
#define LOCK(cs)
Definition: sync.h:306
#define EXCLUSIVE_LOCKS_REQUIRED(...)
Definition: threadsafety.h:56
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Definition: time.h:25