2022-12-24 23:49:50 +00:00
|
|
|
// Copyright (c) 2021-2022 The Bitcoin Core developers
|
2021-01-31 00:55:54 +10:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#ifndef BITCOIN_TXORPHANAGE_H
|
|
|
|
#define BITCOIN_TXORPHANAGE_H
|
|
|
|
|
|
|
|
#include <net.h>
|
2021-01-31 23:37:41 +10:00
|
|
|
#include <primitives/block.h>
|
2021-01-31 00:55:54 +10:00
|
|
|
#include <primitives/transaction.h>
|
|
|
|
#include <sync.h>
|
2024-05-15 18:54:15 +02:00
|
|
|
#include <util/time.h>
|
2021-01-31 00:55:54 +10:00
|
|
|
|
2021-02-25 00:28:16 +10:00
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
|
2021-03-02 19:36:48 +10:00
|
|
|
/** A class to track orphan transactions (failed on TX_MISSING_INPUTS)
|
|
|
|
* Since we cannot distinguish orphans from bad transactions with
|
|
|
|
* non-existent inputs, we heavily limit the number of orphans
|
|
|
|
* we keep and the duration we keep them for.
|
2021-01-31 23:42:00 +10:00
|
|
|
*/
|
|
|
|
class TxOrphanage {
|
|
|
|
public:
|
|
|
|
/** Add a new orphan transaction */
|
2022-10-07 14:32:19 +10:00
|
|
|
bool AddTx(const CTransactionRef& tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
|
2021-01-31 23:42:00 +10:00
|
|
|
|
2024-05-10 10:55:39 +01:00
|
|
|
/** Check if we already have an orphan transaction (by wtxid only) */
|
|
|
|
bool HaveTx(const Wtxid& wtxid) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
|
2021-01-31 23:42:00 +10:00
|
|
|
|
2021-02-25 00:28:16 +10:00
|
|
|
/** Extract a transaction from a peer's work set
|
2023-01-25 18:13:00 +10:00
|
|
|
* Returns nullptr if there are no transactions to work on.
|
|
|
|
* Otherwise returns the transaction reference, and removes
|
|
|
|
* it from the work set.
|
2021-03-02 19:36:48 +10:00
|
|
|
*/
|
2023-01-25 18:13:00 +10:00
|
|
|
CTransactionRef GetTxToReconsider(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
|
2021-01-31 00:55:54 +10:00
|
|
|
|
2024-05-10 12:04:50 +01:00
|
|
|
/** Erase an orphan by wtxid */
|
|
|
|
int EraseTx(const Wtxid& wtxid) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
|
2021-01-31 00:55:54 +10:00
|
|
|
|
2021-01-31 23:42:00 +10:00
|
|
|
/** Erase all orphans announced by a peer (eg, after that peer disconnects) */
|
2022-10-07 14:32:19 +10:00
|
|
|
void EraseForPeer(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
|
2021-01-31 00:55:54 +10:00
|
|
|
|
2021-03-02 19:36:48 +10:00
|
|
|
/** Erase all orphans included in or invalidated by a new block */
|
2022-10-07 14:32:19 +10:00
|
|
|
void EraseForBlock(const CBlock& block) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
|
2021-01-31 23:42:00 +10:00
|
|
|
|
|
|
|
/** Limit the orphanage to the given maximum */
|
2023-12-08 13:14:46 +00:00
|
|
|
void LimitOrphans(unsigned int max_orphans, FastRandomContext& rng) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
|
2021-01-31 23:42:00 +10:00
|
|
|
|
2021-03-25 14:13:45 +10:00
|
|
|
/** Add any orphans that list a particular tx as a parent into the from peer's work set */
|
|
|
|
void AddChildrenToWorkSet(const CTransaction& tx) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);;
|
2021-01-31 23:42:00 +10:00
|
|
|
|
2022-12-23 00:21:10 +10:00
|
|
|
/** Does this peer have any work to do? */
|
|
|
|
bool HaveTxToReconsider(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);;
|
2021-01-31 23:42:00 +10:00
|
|
|
|
2024-03-28 17:14:38 +00:00
|
|
|
/** Get all children that spend from this tx and were received from nodeid. Sorted from most
|
|
|
|
* recent to least recent. */
|
|
|
|
std::vector<CTransactionRef> GetChildrenFromSamePeer(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. Also return
|
|
|
|
* which peer provided each tx. */
|
|
|
|
std::vector<std::pair<CTransactionRef, NodeId>> GetChildrenFromDifferentPeer(const CTransactionRef& parent, NodeId nodeid) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex);
|
|
|
|
|
2021-03-31 18:34:49 +01:00
|
|
|
/** Return how many entries exist in the orphange */
|
2022-10-07 14:32:19 +10:00
|
|
|
size_t Size() EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
|
2021-03-31 18:34:49 +01:00
|
|
|
{
|
2022-10-07 14:32:19 +10:00
|
|
|
LOCK(m_mutex);
|
2021-03-31 18:34:49 +01:00
|
|
|
return m_orphans.size();
|
|
|
|
}
|
|
|
|
|
2021-01-31 23:42:00 +10:00
|
|
|
protected:
|
2022-10-07 14:25:47 +10:00
|
|
|
/** Guards orphan transactions */
|
2022-10-07 14:32:19 +10:00
|
|
|
mutable Mutex m_mutex;
|
2022-10-07 14:25:47 +10:00
|
|
|
|
2021-02-15 22:53:06 +10:00
|
|
|
struct OrphanTx {
|
2021-01-31 23:42:00 +10:00
|
|
|
CTransactionRef tx;
|
|
|
|
NodeId fromPeer;
|
2024-05-15 18:54:15 +02:00
|
|
|
NodeSeconds nTimeExpire;
|
2021-01-31 23:42:00 +10:00
|
|
|
size_t list_pos;
|
|
|
|
};
|
|
|
|
|
2024-04-29 16:40:28 +01:00
|
|
|
/** Map from wtxid to orphan transaction record. Limited by
|
2021-01-31 23:42:00 +10:00
|
|
|
* -maxorphantx/DEFAULT_MAX_ORPHAN_TRANSACTIONS */
|
2024-04-29 16:40:28 +01:00
|
|
|
std::map<Wtxid, OrphanTx> m_orphans GUARDED_BY(m_mutex);
|
2021-01-31 23:42:00 +10:00
|
|
|
|
2021-03-25 14:13:45 +10:00
|
|
|
/** Which peer provided the orphans that need to be reconsidered */
|
2024-04-29 16:40:28 +01:00
|
|
|
std::map<NodeId, std::set<Wtxid>> m_peer_work_set GUARDED_BY(m_mutex);
|
2021-02-25 00:28:16 +10:00
|
|
|
|
2021-02-15 22:53:06 +10:00
|
|
|
using OrphanMap = decltype(m_orphans);
|
2021-01-31 00:55:54 +10:00
|
|
|
|
|
|
|
struct IteratorComparator
|
|
|
|
{
|
|
|
|
template<typename I>
|
|
|
|
bool operator()(const I& a, const I& b) const
|
|
|
|
{
|
2024-06-19 09:57:22 +01:00
|
|
|
return a->first < b->first;
|
2021-01-31 00:55:54 +10:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-02-15 22:53:06 +10:00
|
|
|
/** Index from the parents' COutPoint into the m_orphans. Used
|
|
|
|
* to remove orphan transactions from the m_orphans */
|
2022-10-07 14:32:19 +10:00
|
|
|
std::map<COutPoint, std::set<OrphanMap::iterator, IteratorComparator>> m_outpoint_to_orphan_it GUARDED_BY(m_mutex);
|
2021-01-31 00:55:54 +10:00
|
|
|
|
|
|
|
/** Orphan transactions in vector for quick random eviction */
|
2022-10-07 14:32:19 +10:00
|
|
|
std::vector<OrphanMap::iterator> m_orphan_list GUARDED_BY(m_mutex);
|
2021-01-31 23:42:00 +10:00
|
|
|
|
2024-05-10 12:04:50 +01:00
|
|
|
/** Erase an orphan by wtxid */
|
|
|
|
int EraseTxNoLock(const Wtxid& wtxid) EXCLUSIVE_LOCKS_REQUIRED(m_mutex);
|
2024-05-28 09:45:19 -07:00
|
|
|
|
|
|
|
/** Timestamp for the next scheduled sweep of expired orphans */
|
2024-05-29 08:53:50 -07:00
|
|
|
NodeSeconds m_next_sweep GUARDED_BY(m_mutex){0s};
|
2021-01-31 23:42:00 +10:00
|
|
|
};
|
2021-01-31 00:55:54 +10:00
|
|
|
|
|
|
|
#endif // BITCOIN_TXORPHANAGE_H
|