Skip to content

Commit 17de75b

Browse files
committed
Merge #19590: p2p, refactor: add CInv transaction message helpers; use in net processing
c251d71 p2p, refactoring: use CInv helpers in net_processing.cpp (Jon Atack) 4254cd9 p2p: add CInv transaction message helper methods (Jon Atack) Pull request description: Following the merge of wtxid relay in #18044, this is the first of three refactoring PRs (this one, #19610, and #19611) with no change in behavior, tightly scoped to ease review, to simplify the net processing code and improve encapsulation: - add `CInv` transaction message helper methods, defined in the class - use the new helpers in `net_processing.cpp` to simplify the code and improve encapsulation Test coverage is provided by the functional p2p tests, notably (from seeing which tests failed when breaking things to test coverage) `p2p_segwit`, `p2p_tx_download`, `p2p_feefilter`, and `p2p_permissions`. ACKs for top commit: fjahr: Code review ACK c251d71 laanwj: Code review ACK c251d71 vasild: ACK c251d71 theStack: Code-Review ACK c251d71 hebasto: ACK c251d71, I have reviewed the code and it looks OK, I agree it can be merged. Tree-SHA512: ead034b3c9e438909b4c5010c570d7930e69063c114290b051b7cebfd9bd5b19f573218bebe8a521256d32e830797f997adad3d85b4539c64ac5762b698e656d
2 parents 4ebe2f6 + c251d71 commit 17de75b

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

src/net_processing.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,9 +1441,9 @@ bool static AlreadyHave(const CInv& inv, const CTxMemPool& mempool) EXCLUSIVE_LO
14411441

14421442
{
14431443
LOCK(g_cs_orphans);
1444-
if (inv.type != MSG_WTX && mapOrphanTransactions.count(inv.hash)) {
1444+
if (!inv.IsMsgWtx() && mapOrphanTransactions.count(inv.hash)) {
14451445
return true;
1446-
} else if (inv.type == MSG_WTX && g_orphans_by_wtxid.count(inv.hash)) {
1446+
} else if (inv.IsMsgWtx() && g_orphans_by_wtxid.count(inv.hash)) {
14471447
return true;
14481448
}
14491449
}
@@ -1453,8 +1453,7 @@ bool static AlreadyHave(const CInv& inv, const CTxMemPool& mempool) EXCLUSIVE_LO
14531453
if (g_recent_confirmed_transactions->contains(inv.hash)) return true;
14541454
}
14551455

1456-
const bool by_wtxid = (inv.type == MSG_WTX);
1457-
return recentRejects->contains(inv.hash) || mempool.exists(inv.hash, by_wtxid);
1456+
return recentRejects->contains(inv.hash) || mempool.exists(inv.hash, inv.IsMsgWtx());
14581457
}
14591458
case MSG_BLOCK:
14601459
case MSG_WITNESS_BLOCK:
@@ -1715,7 +1714,7 @@ void static ProcessGetData(CNode& pfrom, const CChainParams& chainparams, CConnm
17151714
// Process as many TX items from the front of the getdata queue as
17161715
// possible, since they're common and it's efficient to batch process
17171716
// them.
1718-
while (it != pfrom.vRecvGetData.end() && (it->type == MSG_TX || it->type == MSG_WITNESS_TX || it->type == MSG_WTX)) {
1717+
while (it != pfrom.vRecvGetData.end() && it->IsGenTxMsg()) {
17191718
if (interruptMsgProc) return;
17201719
// The send buffer provides backpressure. If there's no space in
17211720
// the buffer, pause processing until the next call.
@@ -1728,10 +1727,10 @@ void static ProcessGetData(CNode& pfrom, const CChainParams& chainparams, CConnm
17281727
continue;
17291728
}
17301729

1731-
CTransactionRef tx = FindTxForGetData(pfrom, inv.hash, inv.type == MSG_WTX, mempool_req, now);
1730+
CTransactionRef tx = FindTxForGetData(pfrom, inv.hash, inv.IsMsgWtx(), mempool_req, now);
17321731
if (tx) {
17331732
// WTX and WITNESS_TX imply we serialize with witness
1734-
int nSendFlags = (inv.type == MSG_TX ? SERIALIZE_TRANSACTION_NO_WITNESS : 0);
1733+
int nSendFlags = (inv.IsMsgTx() ? SERIALIZE_TRANSACTION_NO_WITNESS : 0);
17351734
connman.PushMessage(&pfrom, msgMaker.Make(nSendFlags, NetMsgType::TX, *tx));
17361735
mempool.RemoveUnbroadcastTx(tx->GetHash());
17371736
// As we're going to send tx, make sure its unconfirmed parents are made requestable.
@@ -2650,15 +2649,15 @@ void ProcessMessage(
26502649

26512650
// ignore INVs that don't match wtxidrelay setting
26522651
if (State(pfrom.GetId())->m_wtxid_relay) {
2653-
if (inv.type == MSG_TX) continue;
2652+
if (inv.IsMsgTx()) continue;
26542653
} else {
2655-
if (inv.type == MSG_WTX) continue;
2654+
if (inv.IsMsgWtx()) continue;
26562655
}
26572656

26582657
bool fAlreadyHave = AlreadyHave(inv, mempool);
26592658
LogPrint(BCLog::NET, "got inv: %s %s peer=%d\n", inv.ToString(), fAlreadyHave ? "have" : "new", pfrom.GetId());
26602659

2661-
if (inv.type == MSG_TX) {
2660+
if (inv.IsMsgTx()) {
26622661
inv.type |= nFetchFlags;
26632662
}
26642663

@@ -3690,7 +3689,7 @@ void ProcessMessage(
36903689
vRecv >> vInv;
36913690
if (vInv.size() <= MAX_PEER_TX_IN_FLIGHT + MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
36923691
for (CInv &inv : vInv) {
3693-
if (inv.type == MSG_TX || inv.type == MSG_WITNESS_TX || inv.type == MSG_WTX) {
3692+
if (inv.IsGenTxMsg()) {
36943693
// If we receive a NOTFOUND message for a txid we requested, erase
36953694
// it from our data structures for this peer.
36963695
auto in_flight_it = state->m_tx_download.m_tx_in_flight.find(inv.hash);

src/protocol.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,14 @@ class CInv
430430
std::string GetCommand() const;
431431
std::string ToString() const;
432432

433+
// Single-message helper methods
434+
bool IsMsgTx() const { return type == MSG_TX; }
435+
bool IsMsgWtx() const { return type == MSG_WTX; }
436+
bool IsMsgWitnessTx() const { return type == MSG_WITNESS_TX; }
437+
438+
// Combined-message helper methods
439+
bool IsGenTxMsg() const { return type == MSG_TX || type == MSG_WTX || type == MSG_WITNESS_TX; }
440+
433441
int type;
434442
uint256 hash;
435443
};

0 commit comments

Comments
 (0)