Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
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
12#include <sync.h>
13#include <util/time.h>
14
15#include <chrono>
16#include <map>
17#include <set>
18#include <vector>
19
21
25class TxPool {
26public:
27 TxPool(const std::string &txKindIn, std::chrono::seconds expireTimeIn,
28 std::chrono::seconds expireIntervalIn)
31
33 bool AddTx(const CTransactionRef &tx, NodeId peer)
35
37 bool HaveTx(const TxId &txid) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
38
48
51
56
59
61 unsigned int LimitTxs(unsigned int max_txs, FastRandomContext &rng)
63
70
73
78 std::vector<CTransactionRef>
79 GetChildrenFromSamePeer(const CTransactionRef &parent, NodeId nodeid) const
81
86 std::vector<std::pair<CTransactionRef, NodeId>>
88 NodeId nodeid) const
90
94 return m_pool_txs.size();
95 }
96
97protected:
99 const std::string txKind;
101 const std::chrono::seconds expireTime;
103 const std::chrono::seconds expireInterval;
104
106 mutable Mutex m_mutex;
107
114
119 std::map<TxId, PoolTx> m_pool_txs GUARDED_BY(m_mutex);
120
122 std::map<NodeId, std::set<TxId>> m_peer_work_set GUARDED_BY(m_mutex);
123
124 using PoolTxMap = decltype(m_pool_txs);
125
127 template <typename I> bool operator()(const I &a, const I &b) const {
128 return a->first < b->first;
129 }
130 };
131
136 std::map<COutPoint, std::set<PoolTxMap ::iterator, IteratorComparator>>
138
140 std::vector<PoolTxMap::iterator> m_txs_list GUARDED_BY(m_mutex);
141
144
147};
148
149#endif // BITCOIN_TXPOOL_H
Definition block.h:60
The basic transaction that is broadcasted on the network and contained in blocks.
Fast randomness source.
Definition random.h:156
A class to store and track transactions by peers.
Definition txpool.h:25
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:27
const std::chrono::seconds expireInterval
Minimum time between transactions expire time checks.
Definition txpool.h:103
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:247
size_t Size() const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Return how many entries exist in the pool.
Definition txpool.h:92
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:146
void EraseForBlock(const CBlock &block) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Erase all txs included in or invalidated by a new block.
Definition txpool.cpp:209
const std::chrono::seconds expireTime
Expiration time for transactions.
Definition txpool.h:101
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:99
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:106
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:179
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:292
bool HaveTxToReconsider(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Does this peer have any work to do?
Definition txpool.cpp:198
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:124
int64_t NodeId
Definition nodeid.h:10
std::shared_ptr< const CTransaction > CTransactionRef
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
A TxId is the identifier of a transaction.
Definition txid.h:14
bool operator()(const I &a, const I &b) const
Definition txpool.h:127
NodeId fromPeer
Definition txpool.h:110
size_t list_pos
Definition txpool.h:112
NodeSeconds nTimeExpire
Definition txpool.h:111
CTransactionRef tx
Definition txpool.h:109
#define LOCK(cs)
Definition sync.h:306
#define EXCLUSIVE_LOCKS_REQUIRED(...)
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Definition time.h:25