Skip to content

Commit

Permalink
[net] split PushInventory()
Browse files Browse the repository at this point in the history
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().

(cherry picked from commit bitcoin/bitcoin@f52d403)

Zcash: Adjusted to account for bitcoin/bitcoin#15759 not having been
backported.
  • Loading branch information
jnewbery authored and str4d committed Aug 13, 2021
1 parent 85a87aa commit 8469683
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4155,7 +4155,7 @@ bool ActivateBestChain(CValidationState& state, const CChainParams& chainparams,
LOCK(cs_vNodes);
for (CNode* pnode : vNodes)
if (nNewHeight > (pnode->nStartingHeight != -1 ? pnode->nStartingHeight - 2000 : nBlockEstimate))
pnode->PushInventory(CInv(MSG_BLOCK, hashNewTip));
pnode->PushBlockInventory(hashNewTip);
}
// Notify external listeners about the new tip.
GetMainSignals().UpdatedBlockTip(pindexNewTip);
Expand Down Expand Up @@ -6129,7 +6129,7 @@ void static ProcessGetData(CNode* pfrom, const Consensus::Params& consensusParam
// Trigger the peer node to send a getblocks request for the next batch of inventory
if (inv.hash == pfrom->hashContinue)
{
// Bypass PushInventory, this must send even if redundant,
// Bypass PushBlockInventory, this must send even if redundant,
// and we want it right after the last block so they don't
// wait for other stuff first.
vector<CInv> vInv;
Expand Down Expand Up @@ -6586,7 +6586,7 @@ bool static ProcessMessage(const CChainParams& chainparams, CNode* pfrom, string
LogPrint("net", " getblocks stopping, pruned or too old block at %d %s\n", pindex->nHeight, pindex->GetBlockHash().ToString());
break;
}
pfrom->PushInventory(CInv(MSG_BLOCK, pindex->GetBlockHash()));
pfrom->PushBlockInventory(pindex->GetBlockHash());
if (--nLimit <= 0)
{
// When this block is requested, we'll send an inv that'll
Expand Down
3 changes: 1 addition & 2 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2031,11 +2031,10 @@ instance_of_cnetcleanup;

void RelayTransaction(const CTransaction& tx)
{
CInv inv(MSG_TX, tx.GetHash());
LOCK(cs_vNodes);
for (CNode* pnode : vNodes)
{
pnode->PushInventory(inv);
pnode->PushTxInventory(tx.GetHash());
}
}

Expand Down
16 changes: 9 additions & 7 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -475,18 +475,20 @@ class CNode
}
}

void PushInventory(const CInv& inv)
void PushTxInventory(const uint256& hash)
{
LOCK(cs_inventory);
if (inv.type == MSG_TX) {
if (!filterInventoryKnown.contains(inv.hash)) {
setInventoryTxToSend.insert(inv.hash);
}
} else if (inv.type == MSG_BLOCK) {
vInventoryBlockToSend.push_back(inv.hash);
if (!filterInventoryKnown.contains(hash)) {
setInventoryTxToSend.insert(hash);
}
}

void PushBlockInventory(const uint256& hash)
{
LOCK(cs_inventory);
vInventoryBlockToSend.push_back(hash);
}

void AskFor(const CInv& inv);

// TODO: Document the postcondition of this function. Is cs_vSend locked?
Expand Down

0 comments on commit 8469683

Please sign in to comment.