Skip to content

Commit f52d403

Browse files
committed
[net] split PushInventory()
PushInventory() is currently called with a CInv object, which can be a MSG_TX or MSG_BLOCK. PushInventory() only uses the type to determine whether to add the hash to setInventoryTxToSend or vInventoryBlockToSend. Since the caller always knows what type of inventory they're pushing, the CInv is wastefully constructed and thrown away, and tx/block relay is being split out, we split the function into PushTxInventory() and PushBlockInventory().
1 parent c2bcb99 commit f52d403

File tree

2 files changed

+16
-15
lines changed

2 files changed

+16
-15
lines changed

src/net.h

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -973,19 +973,21 @@ class CNode
973973
}
974974
}
975975

976-
void PushInventory(const CInv& inv)
976+
void PushTxInventory(const uint256& hash)
977977
{
978-
if (inv.type == MSG_TX && m_tx_relay != nullptr) {
979-
LOCK(m_tx_relay->cs_tx_inventory);
980-
if (!m_tx_relay->filterInventoryKnown.contains(inv.hash)) {
981-
m_tx_relay->setInventoryTxToSend.insert(inv.hash);
982-
}
983-
} else if (inv.type == MSG_BLOCK) {
984-
LOCK(cs_inventory);
985-
vInventoryBlockToSend.push_back(inv.hash);
978+
if (m_tx_relay == nullptr) return;
979+
LOCK(m_tx_relay->cs_tx_inventory);
980+
if (!m_tx_relay->filterInventoryKnown.contains(hash)) {
981+
m_tx_relay->setInventoryTxToSend.insert(hash);
986982
}
987983
}
988984

985+
void PushBlockInventory(const uint256& hash)
986+
{
987+
LOCK(cs_inventory);
988+
vInventoryBlockToSend.push_back(hash);
989+
}
990+
989991
void PushBlockHash(const uint256 &hash)
990992
{
991993
LOCK(cs_inventory);

src/net_processing.cpp

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

14201420
void RelayTransaction(const uint256& txid, const CConnman& connman)
14211421
{
1422-
CInv inv(MSG_TX, txid);
1423-
connman.ForEachNode([&inv](CNode* pnode)
1422+
connman.ForEachNode([&txid](CNode* pnode)
14241423
{
1425-
pnode->PushInventory(inv);
1424+
pnode->PushTxInventory(txid);
14261425
});
14271426
}
14281427

@@ -1608,7 +1607,7 @@ void static ProcessGetBlockData(CNode& pfrom, const CChainParams& chainparams, c
16081607
// Trigger the peer node to send a getblocks request for the next batch of inventory
16091608
if (inv.hash == pfrom.hashContinue)
16101609
{
1611-
// Bypass PushInventory, this must send even if redundant,
1610+
// Bypass PushBlockInventory, this must send even if redundant,
16121611
// and we want it right after the last block so they don't
16131612
// wait for other stuff first.
16141613
std::vector<CInv> vInv;
@@ -2657,7 +2656,7 @@ bool ProcessMessage(CNode& pfrom, const std::string& msg_type, CDataStream& vRec
26572656
LogPrint(BCLog::NET, " getblocks stopping, pruned or too old block at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
26582657
break;
26592658
}
2660-
pfrom.PushInventory(CInv(MSG_BLOCK, pindex->GetBlockHash()));
2659+
pfrom.PushBlockInventory(pindex->GetBlockHash());
26612660
if (--nLimit <= 0)
26622661
{
26632662
// When this block is requested, we'll send an inv that'll
@@ -4081,7 +4080,7 @@ bool PeerLogicValidation::SendMessages(CNode* pto)
40814080

40824081
// If the peer's chain has this block, don't inv it back.
40834082
if (!PeerHasHeader(&state, pindex)) {
4084-
pto->PushInventory(CInv(MSG_BLOCK, hashToAnnounce));
4083+
pto->PushBlockInventory(hashToAnnounce);
40854084
LogPrint(BCLog::NET, "%s: sending inv peer=%d hash=%s\n", __func__,
40864085
pto->GetId(), hashToAnnounce.ToString());
40874086
}

0 commit comments

Comments
 (0)