Skip to content

Commit 42ca561

Browse files
jnewberyjonatack
authored andcommitted
[net processing] Split AlreadyHave() into separate block and tx functions
1 parent 39f1dc9 commit 42ca561

File tree

1 file changed

+36
-42
lines changed

1 file changed

+36
-42
lines changed

src/net_processing.cpp

Lines changed: 36 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1422,47 +1422,38 @@ void PeerLogicValidation::BlockChecked(const CBlock& block, const BlockValidatio
14221422
//
14231423

14241424

1425-
bool static AlreadyHave(const CInv& inv, const CTxMemPool& mempool) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
1425+
bool static AlreadyHaveTx(const CInv& inv, const CTxMemPool& mempool) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
14261426
{
1427-
switch (inv.type)
1427+
assert(recentRejects);
1428+
if (::ChainActive().Tip()->GetBlockHash() != hashRecentRejectsChainTip) {
1429+
// If the chain tip has changed previously rejected transactions
1430+
// might be now valid, e.g. due to a nLockTime'd tx becoming valid,
1431+
// or a double-spend. Reset the rejects filter and give those
1432+
// txs a second chance.
1433+
hashRecentRejectsChainTip = ::ChainActive().Tip()->GetBlockHash();
1434+
recentRejects->reset();
1435+
}
1436+
14281437
{
1429-
case MSG_TX:
1430-
case MSG_WITNESS_TX:
1431-
case MSG_WTX:
1432-
{
1433-
assert(recentRejects);
1434-
if (::ChainActive().Tip()->GetBlockHash() != hashRecentRejectsChainTip)
1435-
{
1436-
// If the chain tip has changed previously rejected transactions
1437-
// might be now valid, e.g. due to a nLockTime'd tx becoming valid,
1438-
// or a double-spend. Reset the rejects filter and give those
1439-
// txs a second chance.
1440-
hashRecentRejectsChainTip = ::ChainActive().Tip()->GetBlockHash();
1441-
recentRejects->reset();
1442-
}
1438+
LOCK(g_cs_orphans);
1439+
if (!inv.IsMsgWtx() && mapOrphanTransactions.count(inv.hash)) {
1440+
return true;
1441+
} else if (inv.IsMsgWtx() && g_orphans_by_wtxid.count(inv.hash)) {
1442+
return true;
1443+
}
1444+
}
14431445

1444-
{
1445-
LOCK(g_cs_orphans);
1446-
if (!inv.IsMsgWtx() && mapOrphanTransactions.count(inv.hash)) {
1447-
return true;
1448-
} else if (inv.IsMsgWtx() && g_orphans_by_wtxid.count(inv.hash)) {
1449-
return true;
1450-
}
1451-
}
1446+
{
1447+
LOCK(g_cs_recent_confirmed_transactions);
1448+
if (g_recent_confirmed_transactions->contains(inv.hash)) return true;
1449+
}
14521450

1453-
{
1454-
LOCK(g_cs_recent_confirmed_transactions);
1455-
if (g_recent_confirmed_transactions->contains(inv.hash)) return true;
1456-
}
1451+
return recentRejects->contains(inv.hash) || mempool.exists(ToGenTxid(inv));
1452+
}
14571453

1458-
return recentRejects->contains(inv.hash) || mempool.exists(ToGenTxid(inv));
1459-
}
1460-
case MSG_BLOCK:
1461-
case MSG_WITNESS_BLOCK:
1462-
return LookupBlockIndex(inv.hash) != nullptr;
1463-
}
1464-
// Don't know what it is, just say we already got one
1465-
return true;
1454+
bool static AlreadyHaveBlock(const CInv& inv, const CTxMemPool& mempool) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
1455+
{
1456+
return LookupBlockIndex(inv.hash) != nullptr;
14661457
}
14671458

14681459
void RelayTransaction(const uint256& txid, const uint256& wtxid, const CConnman& connman)
@@ -2670,10 +2661,10 @@ void PeerLogicValidation::ProcessMessage(CNode& pfrom, const std::string& msg_ty
26702661
if (inv.IsMsgWtx()) continue;
26712662
}
26722663

2673-
bool fAlreadyHave = AlreadyHave(inv, m_mempool);
2674-
LogPrint(BCLog::NET, "got inv: %s %s peer=%d\n", inv.ToString(), fAlreadyHave ? "have" : "new", pfrom.GetId());
2675-
26762664
if (inv.type == MSG_BLOCK) {
2665+
bool fAlreadyHave = AlreadyHaveBlock(inv, m_mempool);
2666+
LogPrint(BCLog::NET, "got inv: %s %s peer=%d\n", inv.ToString(), fAlreadyHave ? "have" : "new", pfrom.GetId());
2667+
26772668
UpdateBlockAvailability(pfrom.GetId(), inv.hash);
26782669
if (!fAlreadyHave && !fImporting && !fReindex && !mapBlocksInFlight.count(inv.hash)) {
26792670
// Headers-first is the primary method of announcement on
@@ -2684,6 +2675,9 @@ void PeerLogicValidation::ProcessMessage(CNode& pfrom, const std::string& msg_ty
26842675
best_block = &inv.hash;
26852676
}
26862677
} else {
2678+
bool fAlreadyHave = AlreadyHaveTx(inv, mempool);
2679+
LogPrint(BCLog::NET, "got inv: %s %s peer=%d\n", inv.ToString(), fAlreadyHave ? "have" : "new", pfrom.GetId());
2680+
26872681
pfrom.AddKnownTx(inv.hash);
26882682
if (fBlocksOnly) {
26892683
LogPrint(BCLog::NET, "transaction (%s) inv sent in violation of protocol, disconnecting peer=%d\n", inv.hash.ToString(), pfrom.GetId());
@@ -2963,7 +2957,7 @@ void PeerLogicValidation::ProcessMessage(CNode& pfrom, const std::string& msg_ty
29632957
// already; and an adversary can already relay us old transactions
29642958
// (older than our recency filter) if trying to DoS us, without any need
29652959
// for witness malleation.
2966-
if (!AlreadyHave(CInv(MSG_WTX, wtxid), m_mempool) &&
2960+
if (!AlreadyHaveTx(CInv(MSG_WTX, wtxid), m_mempool) &&
29672961
AcceptToMemoryPool(m_mempool, state, ptx, &lRemovedTxn, false /* bypass_limits */, 0 /* nAbsurdFee */)) {
29682962
m_mempool.check(&::ChainstateActive().CoinsTip());
29692963
RelayTransaction(tx.GetHash(), tx.GetWitnessHash(), m_connman);
@@ -3017,7 +3011,7 @@ void PeerLogicValidation::ProcessMessage(CNode& pfrom, const std::string& msg_ty
30173011
// protocol for getting all unconfirmed parents.
30183012
CInv _inv(MSG_TX, parent_txid);
30193013
pfrom.AddKnownTx(parent_txid);
3020-
if (!AlreadyHave(_inv, m_mempool)) RequestTx(State(pfrom.GetId()), ToGenTxid(_inv), current_time);
3014+
if (!AlreadyHaveTx(_inv, m_mempool)) RequestTx(State(pfrom.GetId()), ToGenTxid(_inv), current_time);
30213015
}
30223016
AddOrphanTx(ptx, pfrom.GetId());
30233017

@@ -4568,7 +4562,7 @@ bool PeerLogicValidation::SendMessages(CNode* pto)
45684562
// processing at a later time, see below)
45694563
tx_process_time.erase(tx_process_time.begin());
45704564
CInv inv(gtxid.IsWtxid() ? MSG_WTX : (MSG_TX | GetFetchFlags(*pto)), gtxid.GetHash());
4571-
if (!AlreadyHave(inv, m_mempool)) {
4565+
if (!AlreadyHaveTx(inv, m_mempool)) {
45724566
// If this transaction was last requested more than 1 minute ago,
45734567
// then request.
45744568
const auto last_request_time = GetTxRequestTime(gtxid);

0 commit comments

Comments
 (0)