Bitcoin ABC 0.34.5
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#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);
33
37 bool AddAnnouncer(const TxId &txid, NodeId peer);
38
40 bool HaveTx(const TxId &txid) const;
41
43 bool HaveTxFromPeer(const TxId &txid, NodeId peer) const;
44
45 CTransactionRef GetTx(const TxId &txid) const;
46 std::vector<CTransactionRef>
47 GetConflictTxs(const CTransactionRef &tx) const;
48
57
59 int EraseTx(const TxId &txid);
60
66 void EraseForPeer(NodeId peer);
67
69 void EraseForBlock(const CBlock &block);
70
72 unsigned int LimitTxs(unsigned int max_txs, FastRandomContext &rng);
73
79
81 bool HaveTxToReconsider(NodeId peer);
82
87 std::vector<CTransactionRef>
88 GetChildrenFromSamePeer(const CTransactionRef &parent, NodeId nodeid) const;
89
90
92 size_t Size() const { return m_pool_txs.size(); }
93
102
103 std::vector<PoolTx> GetPoolTransactions() const;
104
105protected:
107 const std::string txKind;
109 const std::chrono::seconds expireTime;
111 const std::chrono::seconds expireInterval;
112
117 std::map<TxId, PoolTx> m_pool_txs;
118
120 std::map<NodeId, std::set<TxId>> m_peer_work_set;
121
122 using PoolTxMap = decltype(m_pool_txs);
123
125 template <typename I> bool operator()(const I &a, const I &b) const {
126 return a->first < b->first;
127 }
128 };
129
134 std::map<COutPoint, std::set<PoolTxMap ::iterator, IteratorComparator>>
136
138 std::vector<PoolTxMap::iterator> m_txs_list;
139
142};
143
144#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:411
A class to store and track transactions by peers.
Definition txpool.h:24
void EraseForPeer(NodeId peer)
Maybe erase all txs announced by a peer (eg, after that peer disconnects).
Definition txpool.cpp:103
TxPool(const std::string &txKindIn, std::chrono::seconds expireTimeIn, std::chrono::seconds expireIntervalIn)
Definition txpool.h:26
void EraseForBlock(const CBlock &block)
Erase all txs included in or invalidated by a new block.
Definition txpool.cpp:260
CTransactionRef GetTx(const TxId &txid) const
Definition txpool.cpp:208
const std::chrono::seconds expireInterval
Minimum time between transactions expire time checks.
Definition txpool.h:111
std::map< COutPoint, std::set< PoolTxMap ::iterator, IteratorComparator > > m_outpoint_to_tx_it
Index from the parents' COutPoint into the m_pool_txs.
Definition txpool.h:135
CTransactionRef GetTxToReconsider(NodeId peer)
Extract a transaction from a peer's work set.
Definition txpool.cpp:234
bool AddAnnouncer(const TxId &txid, NodeId peer)
Add an additional announcer to a tx if it exists.
Definition txpool.cpp:50
void AddChildrenToWorkSet(const CTransaction &tx, FastRandomContext &rng)
Add any tx that list a particular tx as a parent into the from peer's work set.
Definition txpool.cpp:162
const std::chrono::seconds expireTime
Expiration time for transactions.
Definition txpool.h:109
bool AddTx(const CTransactionRef &tx, NodeId peer)
Add a new transaction to the pool.
Definition txpool.cpp:15
NodeSeconds m_next_sweep
Timestamp for the next scheduled sweep of expired transactions.
Definition txpool.h:141
const std::string txKind
The transaction kind as string, used for logging.
Definition txpool.h:107
std::vector< CTransactionRef > GetConflictTxs(const CTransactionRef &tx) const
Definition txpool.cpp:218
size_t Size() const
Return how many entries exist in the pool.
Definition txpool.h:92
std::map< NodeId, std::set< TxId > > m_peer_work_set
Which peer provided the transactions that need to be reconsidered.
Definition txpool.h:120
std::vector< PoolTxMap::iterator > m_txs_list
Pool transactions in vector for quick random eviction.
Definition txpool.h:138
unsigned int LimitTxs(unsigned int max_txs, FastRandomContext &rng)
Limit the txs to the given maximum.
Definition txpool.cpp:128
std::vector< CTransactionRef > GetChildrenFromSamePeer(const CTransactionRef &parent, NodeId nodeid) const
Get all children that spend from this tx and were received from nodeid.
Definition txpool.cpp:300
bool HaveTx(const TxId &txid) const
Check if we already have an the transaction.
Definition txpool.cpp:199
bool HaveTxToReconsider(NodeId peer)
Does this peer have any work to do?
Definition txpool.cpp:251
int EraseTx(const TxId &txid)
Erase a tx by txid.
Definition txpool.cpp:65
bool HaveTxFromPeer(const TxId &txid, NodeId peer) const
Check if a {tx, peer} exists in the pool.
Definition txpool.cpp:203
std::vector< PoolTx > GetPoolTransactions() const
Definition txpool.cpp:342
decltype(m_pool_txs) PoolTxMap
Definition txpool.h:122
std::map< TxId, PoolTx > m_pool_txs
Map from txid to pool transaction record.
Definition txpool.h:117
int64_t NodeId
Definition eviction.h:16
std::shared_ptr< const CTransaction > CTransactionRef
A TxId is the identifier of a transaction.
Definition txid.h:14
bool operator()(const I &a, const I &b) const
Definition txpool.h:125
Allows providing pool transaction information externally.
Definition txpool.h:95
size_t list_pos
Definition txpool.h:100
NodeSeconds nTimeExpire
Definition txpool.h:99
CTransactionRef tx
Definition txpool.h:96
std::set< NodeId > announcers
Peers added with AddTx or AddAnnouncer.
Definition txpool.h:98
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Definition time.h:27