Skip to content

Commit

Permalink
txorphanage: Extract GetOrphanTx
Browse files Browse the repository at this point in the history
Summary:
Extract orphan lookup code into GetOrphanTx function.

This is a backport of [[bitcoin/bitcoin#21148 | core#21148]] [6/14]
bitcoin/bitcoin@f294da7

Depends on D11485

Test Plan: `ninja all check-all`

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Subscribers: Fabien

Differential Revision: https://reviews.bitcoinabc.org/D11486
  • Loading branch information
ajtowns authored and PiRK committed May 19, 2022
1 parent 4c724a2 commit 718ec97
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
11 changes: 4 additions & 7 deletions src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2991,14 +2991,12 @@ void PeerManagerImpl::ProcessOrphanTx(const Config &config,
const TxId orphanTxId = *orphan_work_set.begin();
orphan_work_set.erase(orphan_work_set.begin());

auto orphan_it = mapOrphanTransactions.find(orphanTxId);
if (orphan_it == mapOrphanTransactions.end()) {
const auto [porphanTx, from_peer] = GetOrphanTx(orphanTxId);
if (porphanTx == nullptr) {
continue;
}

const CTransactionRef porphanTx = orphan_it->second.tx;
TxValidationState state;

if (AcceptToMemoryPool(::ChainstateActive(), config, m_mempool, state,
porphanTx, false /* bypass_limits */)) {
LogPrint(BCLog::MEMPOOL, " accepted orphan tx %s\n",
Expand All @@ -3011,10 +3009,9 @@ void PeerManagerImpl::ProcessOrphanTx(const Config &config,
if (state.IsInvalid()) {
LogPrint(BCLog::MEMPOOL,
" invalid orphan tx %s from peer=%d. %s\n",
orphanTxId.ToString(), orphan_it->second.fromPeer,
state.ToString());
orphanTxId.ToString(), from_peer, state.ToString());
// Punish peer that gave us an invalid orphan tx
MaybePunishNodeForTx(orphan_it->second.fromPeer, state);
MaybePunishNodeForTx(from_peer, state);
}
// Has inputs but not accepted to mempool
// Probably non-standard or insufficient fee
Expand Down
10 changes: 10 additions & 0 deletions src/txorphanage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,13 @@ bool HaveOrphanTx(const TxId &txid) {
LOCK(g_cs_orphans);
return mapOrphanTransactions.count(txid);
}

std::pair<CTransactionRef, NodeId> GetOrphanTx(const TxId &txid) {
AssertLockHeld(g_cs_orphans);

const auto it = mapOrphanTransactions.find(txid);
if (it == mapOrphanTransactions.end()) {
return {nullptr, -1};
}
return {it->second.tx, it->second.fromPeer};
}
2 changes: 2 additions & 0 deletions src/txorphanage.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ void AddChildrenToWorkSet(const CTransaction &tx,
std::set<TxId> &orphan_work_set)
EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans);
bool HaveOrphanTx(const TxId &txid) LOCKS_EXCLUDED(g_cs_orphans);
std::pair<CTransactionRef, NodeId> GetOrphanTx(const TxId &txid)
EXCLUSIVE_LOCKS_REQUIRED(g_cs_orphans);

/**
* Map from txid to orphan transaction record. Limited by
Expand Down

0 comments on commit 718ec97

Please sign in to comment.