23 const uint256& hash = tx->GetHash();
24 if (m_orphans.count(hash))
43 m_orphan_list.push_back(
ret.first);
45 m_wtxid_to_orphan_it.emplace(tx->GetWitnessHash(),
ret.first);
46 for (
const CTxIn& txin : tx->vin) {
47 m_outpoint_to_orphan_it[txin.
prevout].insert(
ret.first);
51 m_orphans.size(), m_outpoint_to_orphan_it.size());
64 std::map<uint256, OrphanTx>::iterator it = m_orphans.find(txid);
65 if (it == m_orphans.end())
67 for (
const CTxIn& txin : it->second.tx->vin)
69 auto itPrev = m_outpoint_to_orphan_it.find(txin.
prevout);
70 if (itPrev == m_outpoint_to_orphan_it.end())
72 itPrev->second.erase(it);
73 if (itPrev->second.empty())
74 m_outpoint_to_orphan_it.erase(itPrev);
77 size_t old_pos = it->second.list_pos;
78 assert(m_orphan_list[old_pos] == it);
79 if (old_pos + 1 != m_orphan_list.size()) {
82 auto it_last = m_orphan_list.back();
83 m_orphan_list[old_pos] = it_last;
84 it_last->second.list_pos = old_pos;
86 m_orphan_list.pop_back();
87 m_wtxid_to_orphan_it.erase(it->second.tx->GetWitnessHash());
97 m_peer_work_set.erase(peer);
100 std::map<uint256, OrphanTx>::iterator iter = m_orphans.begin();
101 while (iter != m_orphans.end())
103 std::map<uint256, OrphanTx>::iterator maybeErase = iter++;
104 if (maybeErase->second.fromPeer == peer)
106 nErased +=
_EraseTx(maybeErase->second.tx->GetHash());
116 unsigned int nEvicted = 0;
117 static int64_t nNextSweep;
119 if (nNextSweep <= nNow) {
123 std::map<uint256, OrphanTx>::iterator iter = m_orphans.begin();
124 while (iter != m_orphans.end())
126 std::map<uint256, OrphanTx>::iterator maybeErase = iter++;
127 if (maybeErase->second.nTimeExpire <= nNow) {
128 nErased +=
_EraseTx(maybeErase->second.tx->GetHash());
130 nMinExpTime = std::min(maybeErase->second.nTimeExpire, nMinExpTime);
138 while (m_orphans.size() > max_orphans)
141 size_t randompos = rng.
randrange(m_orphan_list.size());
142 _EraseTx(m_orphan_list[randompos]->first);
153 for (
unsigned int i = 0; i < tx.
vout.size(); i++) {
154 const auto it_by_prev = m_outpoint_to_orphan_it.find(
COutPoint(tx.
GetHash(), i));
155 if (it_by_prev != m_outpoint_to_orphan_it.end()) {
156 for (
const auto& elem : it_by_prev->second) {
159 std::set<uint256>& orphan_work_set = m_peer_work_set.try_emplace(elem->second.fromPeer).first->second;
161 orphan_work_set.insert(elem->first);
171 return m_wtxid_to_orphan_it.count(gtxid.
GetHash());
173 return m_orphans.count(gtxid.
GetHash());
181 auto work_set_it = m_peer_work_set.find(peer);
182 if (work_set_it != m_peer_work_set.end()) {
183 auto& work_set = work_set_it->second;
184 while (!work_set.empty()) {
186 work_set.erase(work_set.begin());
188 const auto orphan_it = m_orphans.find(txid);
189 if (orphan_it != m_orphans.end()) {
190 return orphan_it->second.tx;
201 auto work_set_it = m_peer_work_set.find(peer);
202 if (work_set_it != m_peer_work_set.end()) {
203 auto& work_set = work_set_it->second;
204 return !work_set.empty();
213 std::vector<uint256> vOrphanErase;
219 for (
const auto& txin : tx.
vin) {
220 auto itByPrev = m_outpoint_to_orphan_it.find(txin.prevout);
221 if (itByPrev == m_outpoint_to_orphan_it.end())
continue;
222 for (
auto mi = itByPrev->second.begin(); mi != itByPrev->second.end(); ++mi) {
225 vOrphanErase.push_back(orphanHash);
231 if (vOrphanErase.size()) {
233 for (
const uint256& orphanHash : vOrphanErase) {
std::vector< CTransactionRef > vtx
An outpoint - a combination of a transaction hash and an index n into its vout.
The basic transaction that is broadcasted on the network and contained in blocks.
const std::vector< CTxOut > vout
const uint256 & GetHash() const
const std::vector< CTxIn > vin
An input of a transaction.
uint64_t randrange(uint64_t range) noexcept
Generate a random integer in the range [0..range).
A generic txid reference (txid or wtxid).
const uint256 & GetHash() const
bool HaveTxToReconsider(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Does this peer have any work to do?
void AddChildrenToWorkSet(const CTransaction &tx) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Add any orphans that list a particular tx as a parent into the from peer's work set.
void EraseForBlock(const CBlock &block) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Erase all orphans included in or invalidated by a new block.
Mutex m_mutex
Guards orphan transactions.
bool AddTx(const CTransactionRef &tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Add a new orphan transaction.
bool HaveTx(const GenTxid >xid) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Check if we already have an orphan transaction (by txid or wtxid)
CTransactionRef GetTxToReconsider(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Extract a transaction from a peer's work set Returns nullptr if there are no transactions to work on.
void EraseForPeer(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Erase all orphans announced by a peer (eg, after that peer disconnects)
int _EraseTx(const uint256 &txid) EXCLUSIVE_LOCKS_REQUIRED(m_mutex)
Erase an orphan by txid.
int EraseTx(const uint256 &txid) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Erase an orphan by txid.
void LimitOrphans(unsigned int max_orphans) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Limit the orphanage to the given maximum.
std::string ToString() const
constexpr unsigned char * begin()
static int64_t GetTransactionWeight(const CTransaction &tx)
#define LogPrint(category,...)
static constexpr unsigned int MAX_STANDARD_TX_WEIGHT
The maximum weight for transactions we're willing to relay/mine.
std::shared_ptr< const CTransaction > CTransactionRef
static constexpr int64_t ORPHAN_TX_EXPIRE_INTERVAL
Minimum time between orphan transactions expire time checks in seconds.
static constexpr int64_t ORPHAN_TX_EXPIRE_TIME
Expiration time for orphan transactions in seconds.