24 const Txid& hash = tx->GetHash();
25 const Wtxid& wtxid = tx->GetWitnessHash();
26 if (m_orphans.count(hash))
45 m_orphan_list.push_back(
ret.first);
47 m_wtxid_to_orphan_it.emplace(tx->GetWitnessHash(),
ret.first);
48 for (
const CTxIn& txin : tx->vin) {
49 m_outpoint_to_orphan_it[txin.
prevout].insert(
ret.first);
53 m_orphans.size(), m_outpoint_to_orphan_it.size());
66 std::map<Txid, OrphanTx>::iterator it = m_orphans.find(txid);
67 if (it == m_orphans.end())
69 for (
const CTxIn& txin : it->second.tx->vin)
71 auto itPrev = m_outpoint_to_orphan_it.find(txin.
prevout);
72 if (itPrev == m_outpoint_to_orphan_it.end())
74 itPrev->second.erase(it);
75 if (itPrev->second.empty())
76 m_outpoint_to_orphan_it.erase(itPrev);
79 size_t old_pos = it->second.list_pos;
80 assert(m_orphan_list[old_pos] == it);
81 if (old_pos + 1 != m_orphan_list.size()) {
84 auto it_last = m_orphan_list.back();
85 m_orphan_list[old_pos] = it_last;
86 it_last->second.list_pos = old_pos;
88 const auto& wtxid = it->second.tx->GetWitnessHash();
90 m_orphan_list.pop_back();
91 m_wtxid_to_orphan_it.erase(it->second.tx->GetWitnessHash());
101 m_peer_work_set.erase(peer);
104 std::map<Txid, OrphanTx>::iterator iter = m_orphans.begin();
105 while (iter != m_orphans.end())
107 std::map<Txid, OrphanTx>::iterator maybeErase = iter++;
108 if (maybeErase->second.fromPeer == peer)
120 unsigned int nEvicted = 0;
121 static int64_t nNextSweep;
123 if (nNextSweep <= nNow) {
127 std::map<Txid, OrphanTx>::iterator iter = m_orphans.begin();
128 while (iter != m_orphans.end())
130 std::map<Txid, OrphanTx>::iterator maybeErase = iter++;
131 if (maybeErase->second.nTimeExpire <= nNow) {
134 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<Txid>& orphan_work_set = m_peer_work_set.try_emplace(elem->second.fromPeer).first->second;
164 orphan_work_set.insert(elem->first);
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<Txid> 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) {
229 const auto& orphanHash = orphanTx.
GetHash();
230 vOrphanErase.push_back(orphanHash);
236 if (vOrphanErase.size()) {
238 for (
const auto& orphanHash : vOrphanErase) {
251 std::vector<OrphanMap::iterator> iters;
254 for (
unsigned int i = 0; i < parent->vout.size(); i++) {
255 const auto it_by_prev = m_outpoint_to_orphan_it.find(
COutPoint(parent->GetHash(), i));
256 if (it_by_prev != m_outpoint_to_orphan_it.end()) {
257 for (
const auto& elem : it_by_prev->second) {
258 if (elem->second.fromPeer == nodeid) {
259 iters.emplace_back(elem);
268 std::sort(iters.begin(), iters.end(), [](
const auto& lhs,
const auto& rhs) {
269 if (lhs->second.nTimeExpire == rhs->second.nTimeExpire) {
270 return &(*lhs) < &(*rhs);
272 return lhs->second.nTimeExpire > rhs->second.nTimeExpire;
276 iters.erase(std::unique(iters.begin(), iters.end()), iters.end());
279 std::vector<CTransactionRef> children_found;
280 children_found.reserve(iters.size());
281 for (
const auto& child_iter : iters) {
282 children_found.emplace_back(child_iter->second.tx);
284 return children_found;
292 std::vector<OrphanMap::iterator> iters;
295 for (
unsigned int i = 0; i < parent->vout.size(); i++) {
296 const auto it_by_prev = m_outpoint_to_orphan_it.find(
COutPoint(parent->GetHash(), i));
297 if (it_by_prev != m_outpoint_to_orphan_it.end()) {
298 for (
const auto& elem : it_by_prev->second) {
299 if (elem->second.fromPeer != nodeid) {
300 iters.emplace_back(elem);
308 iters.erase(std::unique(iters.begin(), iters.end()), iters.end());
311 std::vector<std::pair<CTransactionRef, NodeId>> children_found;
312 children_found.reserve(iters.size());
313 for (
const auto& child_iter : iters) {
314 children_found.emplace_back(child_iter->second.tx, child_iter->second.fromPeer);
316 return children_found;
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 Txid & GetHash() const LIFETIMEBOUND
const Wtxid & GetWitnessHash() const LIFETIMEBOUND
const std::vector< CTxOut > vout
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 LIFETIMEBOUND
bool HaveTxToReconsider(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Does this peer have any work to do?
int EraseTxNoLock(const Txid &txid) EXCLUSIVE_LOCKS_REQUIRED(m_mutex)
Erase an orphan by txid.
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 LimitOrphans(unsigned int max_orphans, FastRandomContext &rng) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Limit the orphanage to the given maximum.
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)
std::vector< std::pair< CTransactionRef, NodeId > > GetChildrenFromDifferentPeer(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.
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.
int EraseTx(const Txid &txid) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Erase an orphan by txid.
void EraseForPeer(NodeId peer) EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Erase all orphans announced by a peer (eg, after that peer disconnects)
std::vector< CTransactionRef > GetChildrenFromSamePeer(const CTransactionRef &parent, NodeId nodeid) const EXCLUSIVE_LOCKS_REQUIRED(!m_mutex)
Get all children that spend from this tx and were received from nodeid.
std::string ToString() const
constexpr const std::byte * begin() const
static transaction_identifier FromUint256(const uint256 &id)
static int32_t GetTransactionWeight(const CTransaction &tx)
#define LogPrint(category,...)
static constexpr int32_t 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.