16 const TxId &txid = tx->GetId();
26 unsigned int sz = tx->GetTotalSize();
29 "ignoring large %s tx (size: %u, hash: %s)\n",
txKind, sz,
39 for (
const CTxIn &txin : tx->vin) {
44 "stored %s tx %s, size: %u (mapsz %u outsz %u)\n",
txKind,
53 Assume(!it->second.announcers.empty());
54 const auto ret = it->second.announcers.insert(peer);
57 "added peer=%d as announcer of %s tx %s\n", peer,
txKind,
66 std::map<TxId, PoolTx>::iterator it =
m_pool_txs.find(txid);
70 for (
const CTxIn &txin : it->second.tx->vin) {
75 itPrev->second.erase(it);
76 if (itPrev->second.empty()) {
81 size_t old_pos = it->second.list_pos;
88 it_last->second.list_pos = old_pos;
96 it->second.nTimeExpire));
107 std::map<TxId, PoolTx>::iterator iter =
m_pool_txs.begin();
110 auto &[txid, pooltx] = *iter++;
111 auto pooltx_it = pooltx.announcers.find(peer);
112 if (pooltx_it != pooltx.announcers.end()) {
113 pooltx.announcers.erase(peer);
116 if (pooltx.announcers.empty()) {
117 nErased +=
EraseTx(pooltx.tx->GetId());
123 "Erased %d %s transaction(s) from peer=%d\n", nErased,
txKind,
129 unsigned int nEvicted = 0;
130 auto nNow{Now<NodeSeconds>()};
135 std::map<TxId, PoolTx>::iterator iter =
m_pool_txs.begin();
137 std::map<TxId, PoolTx>::iterator maybeErase = iter++;
138 if (maybeErase->second.nTimeExpire <= nNow) {
139 nErased +=
EraseTx(maybeErase->first);
142 std::min(maybeErase->second.nTimeExpire, nMinExpTime);
164 for (
size_t i = 0; i < tx.vout.size(); i++) {
165 const auto it_by_prev =
168 for (
const auto &elem : it_by_prev->second) {
171 if (!
Assume(!elem->second.announcers.empty())) {
180 auto announcer_iter = std::begin(elem->second.announcers);
181 std::advance(announcer_iter,
182 rng.
randrange(elem->second.announcers.size()));
183 auto announcer = *(announcer_iter);
187 std::set<TxId> &work_set =
190 work_set.insert(elem->first);
192 "added %s tx %s to peer %d workset\n",
txKind,
193 tx.GetId().ToString(), announcer);
205 return (it !=
m_pool_txs.end() && it->second.announcers.contains(peer));
211 return tx_it->second.tx;
217std::vector<CTransactionRef>
219 std::vector<CTransactionRef> conflictingTxs;
220 for (
const auto &txin : tx->vin) {
226 for (
auto mi = itByPrev->second.begin(); mi != itByPrev->second.end();
228 conflictingTxs.push_back((*mi)->second.tx);
231 return conflictingTxs;
237 auto &work_set = work_set_it->second;
238 while (!work_set.empty()) {
240 work_set.erase(work_set.begin());
244 return tx_it->second.tx;
254 auto &work_set = work_set_it->second;
255 return !work_set.empty();
265 std::vector<TxId> vTxErase;
268 const CTransaction &tx = *ptx;
271 for (
const auto &txin : tx.vin) {
277 for (
auto mi = itByPrev->second.begin();
278 mi != itByPrev->second.end(); ++mi) {
279 const CTransaction &orphanTx = *(*mi)->second.tx;
280 const TxId &txid = orphanTx.GetId();
281 vTxErase.push_back(txid);
287 if (vTxErase.size()) {
289 for (
const auto &txid : vTxErase) {
294 "Erased %d %s transaction(s) included or conflicted by block\n",
299std::vector<CTransactionRef>
304 std::vector<PoolTxMap::iterator> iters;
308 for (
unsigned int i = 0; i < parent->vout.size(); i++) {
309 const auto it_by_prev =
312 for (
const auto &elem : it_by_prev->second) {
313 if (elem->second.announcers.contains(nodeid)) {
314 iters.emplace_back(elem);
324 std::sort(iters.begin(), iters.end(), [](
const auto &lhs,
const auto &rhs) {
325 if (lhs->second.nTimeExpire == rhs->second.nTimeExpire) {
326 return &(*lhs) < &(*rhs);
328 return lhs->second.nTimeExpire > rhs->second.nTimeExpire;
331 iters.erase(std::unique(iters.begin(), iters.end()), iters.end());
334 std::vector<CTransactionRef> children_found;
335 children_found.reserve(iters.size());
336 for (
const auto &child_iter : iters) {
337 children_found.emplace_back(child_iter->second.tx);
339 return children_found;
343 std::vector<PoolTx> ret;
346 ret.push_back({o.second.tx, o.second.announcers, o.second.nTimeExpire,
#define Assume(val)
Assume is the identity function.
std::vector< CTransactionRef > vtx
I randrange(I range) noexcept
Generate a random integer in the range [0..range), with range > 0.
void EraseForPeer(NodeId peer)
Maybe erase all txs announced by a peer (eg, after that peer disconnects).
void EraseForBlock(const CBlock &block)
Erase all txs included in or invalidated by a new block.
CTransactionRef GetTx(const TxId &txid) const
const std::chrono::seconds expireInterval
Minimum time between transactions expire time checks.
std::map< COutPoint, std::set< PoolTxMap ::iterator, IteratorComparator > > m_outpoint_to_tx_it
Index from the parents' COutPoint into the m_pool_txs.
CTransactionRef GetTxToReconsider(NodeId peer)
Extract a transaction from a peer's work set.
bool AddAnnouncer(const TxId &txid, NodeId peer)
Add an additional announcer to a tx if it exists.
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.
const std::chrono::seconds expireTime
Expiration time for transactions.
bool AddTx(const CTransactionRef &tx, NodeId peer)
Add a new transaction to the pool.
NodeSeconds m_next_sweep
Timestamp for the next scheduled sweep of expired transactions.
const std::string txKind
The transaction kind as string, used for logging.
std::vector< CTransactionRef > GetConflictTxs(const CTransactionRef &tx) const
std::map< NodeId, std::set< TxId > > m_peer_work_set
Which peer provided the transactions that need to be reconsidered.
std::vector< PoolTxMap::iterator > m_txs_list
Pool transactions in vector for quick random eviction.
unsigned int LimitTxs(unsigned int max_txs, FastRandomContext &rng)
Limit the txs to the given maximum.
std::vector< CTransactionRef > GetChildrenFromSamePeer(const CTransactionRef &parent, NodeId nodeid) const
Get all children that spend from this tx and were received from nodeid.
bool HaveTx(const TxId &txid) const
Check if we already have an the transaction.
bool HaveTxToReconsider(NodeId peer)
Does this peer have any work to do?
int EraseTx(const TxId &txid)
Erase a tx by txid.
bool HaveTxFromPeer(const TxId &txid, NodeId peer) const
Check if a {tx, peer} exists in the pool.
std::vector< PoolTx > GetPoolTransactions() const
std::map< TxId, PoolTx > m_pool_txs
Map from txid to pool transaction record.
constexpr uint8_t * begin()
std::string ToString() const
#define LogDebug(category,...)
static constexpr unsigned int MAX_STANDARD_TX_SIZE
The maximum size for transactions we're willing to relay/mine.
std::shared_ptr< const CTransaction > CTransactionRef
static time_point now() noexcept
Return current system time or mocked time, if set.
A TxId is the identifier of a transaction.
Allows providing pool transaction information externally.