[net processing] Split AlreadyHave() into separate block and tx functions

This commit is contained in:
John Newbery 2020-07-30 10:30:24 +01:00 committed by Jon Atack
parent 39f1dc9445
commit 42ca5618ca
No known key found for this signature in database
GPG key ID: 4F5721B3D0E3921D

View file

@ -1422,17 +1422,10 @@ void PeerLogicValidation::BlockChecked(const CBlock& block, const BlockValidatio
// //
bool static AlreadyHave(const CInv& inv, const CTxMemPool& mempool) EXCLUSIVE_LOCKS_REQUIRED(cs_main) bool static AlreadyHaveTx(const CInv& inv, const CTxMemPool& mempool) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
{ {
switch (inv.type)
{
case MSG_TX:
case MSG_WITNESS_TX:
case MSG_WTX:
{
assert(recentRejects); assert(recentRejects);
if (::ChainActive().Tip()->GetBlockHash() != hashRecentRejectsChainTip) if (::ChainActive().Tip()->GetBlockHash() != hashRecentRejectsChainTip) {
{
// If the chain tip has changed previously rejected transactions // If the chain tip has changed previously rejected transactions
// might be now valid, e.g. due to a nLockTime'd tx becoming valid, // might be now valid, e.g. due to a nLockTime'd tx becoming valid,
// or a double-spend. Reset the rejects filter and give those // or a double-spend. Reset the rejects filter and give those
@ -1456,13 +1449,11 @@ bool static AlreadyHave(const CInv& inv, const CTxMemPool& mempool) EXCLUSIVE_LO
} }
return recentRejects->contains(inv.hash) || mempool.exists(ToGenTxid(inv)); return recentRejects->contains(inv.hash) || mempool.exists(ToGenTxid(inv));
} }
case MSG_BLOCK:
case MSG_WITNESS_BLOCK: bool static AlreadyHaveBlock(const CInv& inv, const CTxMemPool& mempool) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
{
return LookupBlockIndex(inv.hash) != nullptr; return LookupBlockIndex(inv.hash) != nullptr;
}
// Don't know what it is, just say we already got one
return true;
} }
void RelayTransaction(const uint256& txid, const uint256& wtxid, const CConnman& connman) 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
if (inv.IsMsgWtx()) continue; if (inv.IsMsgWtx()) continue;
} }
bool fAlreadyHave = AlreadyHave(inv, m_mempool); if (inv.type == MSG_BLOCK) {
bool fAlreadyHave = AlreadyHaveBlock(inv, m_mempool);
LogPrint(BCLog::NET, "got inv: %s %s peer=%d\n", inv.ToString(), fAlreadyHave ? "have" : "new", pfrom.GetId()); LogPrint(BCLog::NET, "got inv: %s %s peer=%d\n", inv.ToString(), fAlreadyHave ? "have" : "new", pfrom.GetId());
if (inv.type == MSG_BLOCK) {
UpdateBlockAvailability(pfrom.GetId(), inv.hash); UpdateBlockAvailability(pfrom.GetId(), inv.hash);
if (!fAlreadyHave && !fImporting && !fReindex && !mapBlocksInFlight.count(inv.hash)) { if (!fAlreadyHave && !fImporting && !fReindex && !mapBlocksInFlight.count(inv.hash)) {
// Headers-first is the primary method of announcement on // Headers-first is the primary method of announcement on
@ -2684,6 +2675,9 @@ void PeerLogicValidation::ProcessMessage(CNode& pfrom, const std::string& msg_ty
best_block = &inv.hash; best_block = &inv.hash;
} }
} else { } else {
bool fAlreadyHave = AlreadyHaveTx(inv, mempool);
LogPrint(BCLog::NET, "got inv: %s %s peer=%d\n", inv.ToString(), fAlreadyHave ? "have" : "new", pfrom.GetId());
pfrom.AddKnownTx(inv.hash); pfrom.AddKnownTx(inv.hash);
if (fBlocksOnly) { if (fBlocksOnly) {
LogPrint(BCLog::NET, "transaction (%s) inv sent in violation of protocol, disconnecting peer=%d\n", inv.hash.ToString(), pfrom.GetId()); 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
// already; and an adversary can already relay us old transactions // already; and an adversary can already relay us old transactions
// (older than our recency filter) if trying to DoS us, without any need // (older than our recency filter) if trying to DoS us, without any need
// for witness malleation. // for witness malleation.
if (!AlreadyHave(CInv(MSG_WTX, wtxid), m_mempool) && if (!AlreadyHaveTx(CInv(MSG_WTX, wtxid), m_mempool) &&
AcceptToMemoryPool(m_mempool, state, ptx, &lRemovedTxn, false /* bypass_limits */, 0 /* nAbsurdFee */)) { AcceptToMemoryPool(m_mempool, state, ptx, &lRemovedTxn, false /* bypass_limits */, 0 /* nAbsurdFee */)) {
m_mempool.check(&::ChainstateActive().CoinsTip()); m_mempool.check(&::ChainstateActive().CoinsTip());
RelayTransaction(tx.GetHash(), tx.GetWitnessHash(), m_connman); RelayTransaction(tx.GetHash(), tx.GetWitnessHash(), m_connman);
@ -3017,7 +3011,7 @@ void PeerLogicValidation::ProcessMessage(CNode& pfrom, const std::string& msg_ty
// protocol for getting all unconfirmed parents. // protocol for getting all unconfirmed parents.
CInv _inv(MSG_TX, parent_txid); CInv _inv(MSG_TX, parent_txid);
pfrom.AddKnownTx(parent_txid); pfrom.AddKnownTx(parent_txid);
if (!AlreadyHave(_inv, m_mempool)) RequestTx(State(pfrom.GetId()), ToGenTxid(_inv), current_time); if (!AlreadyHaveTx(_inv, m_mempool)) RequestTx(State(pfrom.GetId()), ToGenTxid(_inv), current_time);
} }
AddOrphanTx(ptx, pfrom.GetId()); AddOrphanTx(ptx, pfrom.GetId());
@ -4568,7 +4562,7 @@ bool PeerLogicValidation::SendMessages(CNode* pto)
// processing at a later time, see below) // processing at a later time, see below)
tx_process_time.erase(tx_process_time.begin()); tx_process_time.erase(tx_process_time.begin());
CInv inv(gtxid.IsWtxid() ? MSG_WTX : (MSG_TX | GetFetchFlags(*pto)), gtxid.GetHash()); CInv inv(gtxid.IsWtxid() ? MSG_WTX : (MSG_TX | GetFetchFlags(*pto)), gtxid.GetHash());
if (!AlreadyHave(inv, m_mempool)) { if (!AlreadyHaveTx(inv, m_mempool)) {
// If this transaction was last requested more than 1 minute ago, // If this transaction was last requested more than 1 minute ago,
// then request. // then request.
const auto last_request_time = GetTxRequestTime(gtxid); const auto last_request_time = GetTxRequestTime(gtxid);