Skip to content

Commit 9353aa4

Browse files
committed
[refactor] consolidate valid MempoolAcceptResult processing
Deduplicate code that exists in both tx processing and ProcessOrphanTx. Additionally, this can be reused in a function that handles multiple MempoolAcceptResults from package submission.
1 parent 59567d7 commit 9353aa4

File tree

1 file changed

+37
-28
lines changed

1 file changed

+37
-28
lines changed

src/net_processing.cpp

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,11 @@ class PeerManagerImpl final : public PeerManager
582582
*/
583583
bool MaybeDiscourageAndDisconnect(CNode& pnode, Peer& peer);
584584

585+
/** Handle a transaction whose result was MempoolAcceptResult::ResultType::VALID.
586+
* Updates m_txrequest, m_orphanage, and vExtraTxnForCompact. Also queues the tx for relay. */
587+
void ProcessValidTx(NodeId nodeid, const CTransactionRef& tx, const std::list<CTransactionRef>& replaced_transactions)
588+
EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex, g_msgproc_mutex, cs_main);
589+
585590
/**
586591
* Reconsider orphan transactions after a parent has been accepted to the mempool.
587592
*
@@ -3032,6 +3037,34 @@ void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, Peer& peer,
30323037
return;
30333038
}
30343039

3040+
void PeerManagerImpl::ProcessValidTx(NodeId nodeid, const CTransactionRef& tx, const std::list<CTransactionRef>& replaced_transactions)
3041+
{
3042+
AssertLockNotHeld(m_peer_mutex);
3043+
AssertLockHeld(g_msgproc_mutex);
3044+
AssertLockHeld(cs_main);
3045+
3046+
// As this version of the transaction was acceptable, we can forget about any requests for it.
3047+
// No-op if the tx is not in txrequest.
3048+
m_txrequest.ForgetTxHash(tx->GetHash());
3049+
m_txrequest.ForgetTxHash(tx->GetWitnessHash());
3050+
3051+
m_orphanage.AddChildrenToWorkSet(*tx);
3052+
// If it came from the orphanage, remove it. No-op if the tx is not in txorphanage.
3053+
m_orphanage.EraseTx(tx->GetHash());
3054+
3055+
LogDebug(BCLog::MEMPOOL, "AcceptToMemoryPool: peer=%d: accepted %s (wtxid=%s) (poolsz %u txn, %u kB)\n",
3056+
nodeid,
3057+
tx->GetHash().ToString(),
3058+
tx->GetWitnessHash().ToString(),
3059+
m_mempool.size(), m_mempool.DynamicMemoryUsage() / 1000);
3060+
3061+
RelayTransaction(tx->GetHash(), tx->GetWitnessHash());
3062+
3063+
for (const CTransactionRef& removedTx : replaced_transactions) {
3064+
AddToCompactExtraTransactions(removedTx);
3065+
}
3066+
}
3067+
30353068
bool PeerManagerImpl::ProcessOrphanTx(Peer& peer)
30363069
{
30373070
AssertLockHeld(g_msgproc_mutex);
@@ -3047,17 +3080,9 @@ bool PeerManagerImpl::ProcessOrphanTx(Peer& peer)
30473080

30483081
if (result.m_result_type == MempoolAcceptResult::ResultType::VALID) {
30493082
LogPrint(BCLog::TXPACKAGES, " accepted orphan tx %s (wtxid=%s)\n", orphanHash.ToString(), orphan_wtxid.ToString());
3050-
LogPrint(BCLog::MEMPOOL, "AcceptToMemoryPool: peer=%d: accepted %s (wtxid=%s) (poolsz %u txn, %u kB)\n",
3051-
peer.m_id,
3052-
orphanHash.ToString(),
3053-
orphan_wtxid.ToString(),
3054-
m_mempool.size(), m_mempool.DynamicMemoryUsage() / 1000);
3055-
RelayTransaction(orphanHash, porphanTx->GetWitnessHash());
3056-
m_orphanage.AddChildrenToWorkSet(*porphanTx);
3057-
m_orphanage.EraseTx(orphanHash);
3058-
for (const CTransactionRef& removedTx : result.m_replaced_transactions.value()) {
3059-
AddToCompactExtraTransactions(removedTx);
3060-
}
3083+
Assume(result.m_replaced_transactions.has_value());
3084+
std::list<CTransactionRef> empty_replacement_list;
3085+
ProcessValidTx(peer.m_id, porphanTx, result.m_replaced_transactions.value_or(empty_replacement_list));
30613086
return true;
30623087
} else if (state.GetResult() != TxValidationResult::TX_MISSING_INPUTS) {
30633088
if (state.IsInvalid()) {
@@ -4276,24 +4301,8 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
42764301
const TxValidationState& state = result.m_state;
42774302

42784303
if (result.m_result_type == MempoolAcceptResult::ResultType::VALID) {
4279-
// As this version of the transaction was acceptable, we can forget about any
4280-
// requests for it.
4281-
m_txrequest.ForgetTxHash(tx.GetHash());
4282-
m_txrequest.ForgetTxHash(tx.GetWitnessHash());
4283-
RelayTransaction(tx.GetHash(), tx.GetWitnessHash());
4284-
m_orphanage.AddChildrenToWorkSet(tx);
4285-
4304+
ProcessValidTx(pfrom.GetId(), ptx, result.m_replaced_transactions.value());
42864305
pfrom.m_last_tx_time = GetTime<std::chrono::seconds>();
4287-
4288-
LogPrint(BCLog::MEMPOOL, "AcceptToMemoryPool: peer=%d: accepted %s (wtxid=%s) (poolsz %u txn, %u kB)\n",
4289-
pfrom.GetId(),
4290-
tx.GetHash().ToString(),
4291-
tx.GetWitnessHash().ToString(),
4292-
m_mempool.size(), m_mempool.DynamicMemoryUsage() / 1000);
4293-
4294-
for (const CTransactionRef& removedTx : result.m_replaced_transactions.value()) {
4295-
AddToCompactExtraTransactions(removedTx);
4296-
}
42974306
}
42984307
else if (state.GetResult() == TxValidationResult::TX_MISSING_INPUTS)
42994308
{

0 commit comments

Comments
 (0)