23 const uint256& hash = tx->GetHash();
24 const uint256& wtxid = tx->GetWitnessHash();
25 if (m_orphans.count(hash))
44 m_orphan_list.push_back(
ret.first);
46 m_wtxid_to_orphan_it.emplace(tx->GetWitnessHash(),
ret.first);
47 for (
const CTxIn& txin : tx->vin) {
48 m_outpoint_to_orphan_it[txin.
prevout].insert(
ret.first);
52 m_orphans.size(), m_outpoint_to_orphan_it.size());
65 std::map<uint256, OrphanTx>::iterator it = m_orphans.find(txid);
66 if (it == m_orphans.end())
68 for (
const CTxIn& txin : it->second.tx->vin)
70 auto itPrev = m_outpoint_to_orphan_it.find(txin.
prevout);
71 if (itPrev == m_outpoint_to_orphan_it.end())
73 itPrev->second.erase(it);
74 if (itPrev->second.empty())
75 m_outpoint_to_orphan_it.erase(itPrev);
78 size_t old_pos = it->second.list_pos;
79 assert(m_orphan_list[old_pos] == it);
80 if (old_pos + 1 != m_orphan_list.size()) {
83 auto it_last = m_orphan_list.back();
84 m_orphan_list[old_pos] = it_last;
85 it_last->second.list_pos = old_pos;
87 const auto& wtxid = it->second.tx->GetWitnessHash();
89 m_orphan_list.pop_back();
90 m_wtxid_to_orphan_it.erase(it->second.tx->GetWitnessHash());
100 m_peer_work_set.erase(peer);
103 std::map<uint256, OrphanTx>::iterator iter = m_orphans.begin();
104 while (iter != m_orphans.end())
106 std::map<uint256, OrphanTx>::iterator maybeErase = iter++;
107 if (maybeErase->second.fromPeer == peer)
119 unsigned int nEvicted = 0;
120 static int64_t nNextSweep;
122 if (nNextSweep <= nNow) {
126 std::map<uint256, OrphanTx>::iterator iter = m_orphans.begin();
127 while (iter != m_orphans.end())
129 std::map<uint256, OrphanTx>::iterator maybeErase = iter++;
130 if (maybeErase->second.nTimeExpire <= nNow) {
133 nMinExpTime = std::min(maybeErase->second.nTimeExpire, nMinExpTime);
141 while (m_orphans.size() > max_orphans)
144 size_t randompos = rng.
randrange(m_orphan_list.size());
156 for (
unsigned int i = 0; i < tx.
vout.size(); i++) {
157 const auto it_by_prev = m_outpoint_to_orphan_it.find(
COutPoint(tx.
GetHash(), i));
158 if (it_by_prev != m_outpoint_to_orphan_it.end()) {
159 for (
const auto& elem : it_by_prev->second) {
162 std::set<uint256>& orphan_work_set = m_peer_work_set.try_emplace(elem->second.fromPeer).first->second;
164 orphan_work_set.insert(elem->first);
176 return m_wtxid_to_orphan_it.count(gtxid.
GetHash());
178 return m_orphans.count(gtxid.
GetHash());
186 auto work_set_it = m_peer_work_set.find(peer);
187 if (work_set_it != m_peer_work_set.end()) {
188 auto& work_set = work_set_it->second;
189 while (!work_set.empty()) {
191 work_set.erase(work_set.begin());
193 const auto orphan_it = m_orphans.find(txid);
194 if (orphan_it != m_orphans.end()) {
195 return orphan_it->second.tx;
206 auto work_set_it = m_peer_work_set.find(peer);
207 if (work_set_it != m_peer_work_set.end()) {
208 auto& work_set = work_set_it->second;
209 return !work_set.empty();
218 std::vector<uint256> vOrphanErase;
224 for (
const auto& txin : tx.
vin) {
225 auto itByPrev = m_outpoint_to_orphan_it.find(txin.prevout);
226 if (itByPrev == m_outpoint_to_orphan_it.end())
continue;
227 for (
auto mi = itByPrev->second.begin(); mi != itByPrev->second.end(); ++mi) {
230 vOrphanErase.push_back(orphanHash);
236 if (vOrphanErase.size()) {
238 for (
const uint256& orphanHash : vOrphanErase) {
std::shared_ptr< const CTransaction > CTransactionRef
#define LogPrint(category,...)
static constexpr int64_t ORPHAN_TX_EXPIRE_TIME
Expiration time for orphan transactions in seconds.
Mutex m_mutex
Guards orphan transactions.
int EraseTxNoLock(const uint256 &txid) EXCLUSIVE_LOCKS_REQUIRED(m_mutex)
Erase an orphan by txid.
const uint256 & GetHash() const
int EraseTx(const uint256 &txid) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Erase an orphan by txid.
static int32_t GetTransactionWeight(const CTransaction &tx)
const std::vector< CTxIn > vin
bool HaveTxToReconsider(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Does this peer have any work to do?
constexpr unsigned char * begin()
An input of a transaction.
const uint256 & GetWitnessHash() const
const uint256 & GetHash() const
void EraseForPeer(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Erase all orphans announced by a peer (eg, after that peer disconnects)
const std::vector< CTxOut > vout
std::string ToString() const
An outpoint - a combination of a transaction hash and an index n into its vout.
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.
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...
static constexpr int32_t MAX_STANDARD_TX_WEIGHT
The maximum weight for transactions we're willing to relay/mine.
std::vector< CTransactionRef > vtx
bool AddTx(const CTransactionRef &tx, NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Add a new orphan transaction.
void EraseForBlock(const CBlock &block) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Erase all orphans included in or invalidated by a new block.
The basic transaction that is broadcasted on the network and contained in blocks. ...
int64_t GetTime()
DEPRECATED, see GetTime.
bool HaveTx(const GenTxid >xid) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Check if we already have an orphan transaction (by txid or wtxid)
A generic txid reference (txid or wtxid).
uint64_t randrange(uint64_t range) noexcept
Generate a random integer in the range [0..range).
void LimitOrphans(unsigned int max_orphans) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Limit the orphanage to the given maximum.
static constexpr int64_t ORPHAN_TX_EXPIRE_INTERVAL
Minimum time between orphan transactions expire time checks in seconds.