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>
|
|
|
|
|
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
|
|
|
|
|
|
|
/** Check if we already have an orphan transaction (by txid or wtxid) */
|
2022-10-07 14:32:19 +10:00
|
|
|
bool HaveTx(const GenTxid& gtxid) 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
|
|
|
|
2021-01-31 23:42:00 +10:00
|
|
|
/** Erase an orphan by txid */
|
2022-10-07 14:32:19 +10:00
|
|
|
int EraseTx(const uint256& txid) 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 */
|
2022-10-07 14:32:19 +10:00
|
|
|
void LimitOrphans(unsigned int max_orphans) 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
|
|
|
|
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;
|
|
|
|
int64_t nTimeExpire;
|
|
|
|
size_t list_pos;
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Map from txid to orphan transaction record. Limited by
|
|
|
|
* -maxorphantx/DEFAULT_MAX_ORPHAN_TRANSACTIONS */
|
2022-10-07 14:32:19 +10:00
|
|
|
std::map<uint256, 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 */
|
2022-10-07 14:32:19 +10:00
|
|
|
std::map<NodeId, std::set<uint256>> 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
|
|
|
|
{
|
|
|
|
return &(*a) < &(*b);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2021-02-15 22:53:06 +10:00
|
|
|
/** Index from wtxid into the m_orphans to lookup orphan
|
2021-01-31 23:42:00 +10:00
|
|
|
* transactions using their witness ids. */
|
2022-10-07 14:32:19 +10:00
|
|
|
std::map<uint256, OrphanMap::iterator> m_wtxid_to_orphan_it GUARDED_BY(m_mutex);
|
2022-10-07 14:25:47 +10:00
|
|
|
|
|
|
|
/** Erase an orphan by txid */
|
2022-10-07 14:32:19 +10:00
|
|
|
int _EraseTx(const uint256& txid) EXCLUSIVE_LOCKS_REQUIRED(m_mutex);
|
2021-01-31 23:42:00 +10:00
|
|
|
};
|
2021-01-31 00:55:54 +10:00
|
|
|
|
|
|
|
#endif // BITCOIN_TXORPHANAGE_H
|