From 708600f3b7df4cf166a2fcdfd3c0dc70b70812f0 Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Thu, 28 Sep 2023 15:17:46 +0200 Subject: [PATCH] validation: log new header from unsolicited block There are three ways we can learn about a new header: 1. HEADERS message 2. As part of a CMPCTBLOCK message 3. As part of an unsolicited BLOCK message This commit handles the third case. To prevent DoS we only log the header if it's new and has sufficient proof-of-work. --- src/net_processing.cpp | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 175e18377027fb..f014e694d079b9 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -1075,7 +1075,7 @@ class PeerManagerImpl final : public PeerManager void AddAddressKnown(Peer& peer, const CAddress& addr) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex); void PushAddress(Peer& peer, const CAddress& addr) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex); - void LogBlockHeader(const CBlockIndex& index, const CNode& peer, bool via_compact_block); + void LogBlockHeader(uint256 block_hash, std::optional height, const CNode& peer, bool via_compact_block = false, bool via_unsolicited_block = false); }; const CNodeState* PeerManagerImpl::State(NodeId pnode) const EXCLUSIVE_LOCKS_REQUIRED(cs_main) @@ -2989,7 +2989,7 @@ void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, Peer& peer, assert(pindexLast); if (processed && received_new_header) { - LogBlockHeader(*pindexLast, pfrom, /*via_compact_block=*/false); + LogBlockHeader(pindexLast->GetBlockHash(), pindexLast->nHeight, pfrom); } // Consider fetching more headers if we are not using our headers-sync mechanism. @@ -3357,7 +3357,7 @@ void PeerManagerImpl::ProcessCompactBlockTxns(CNode& pfrom, Peer& peer, const Bl return; } -void PeerManagerImpl::LogBlockHeader(const CBlockIndex& index, const CNode& peer, bool via_compact_block) { +void PeerManagerImpl::LogBlockHeader(const uint256 hash, const std::optional height, const CNode& peer, bool via_compact_block, bool via_unsolicited_block) { // To prevent log spam, this function should only be called after it was determined that a // header is both new and valid. // @@ -3369,10 +3369,11 @@ void PeerManagerImpl::LogBlockHeader(const CBlockIndex& index, const CNode& peer // Having this log by default when not in IBD ensures broad availability of // this data in case investigation is merited. const auto msg = strprintf( - "Saw new %sheader hash=%s height=%d peer=%d%s", + "Saw new %sheader%s hash=%s%s peer=%d%s", via_compact_block ? "cmpctblock " : "", - index.GetBlockHash().ToString(), - index.nHeight, + via_unsolicited_block ? " via unsolicited block" : "", + hash.ToString(), + height ? strprintf(" height=%d", height.value()) : "", peer.GetId(), fLogIPs ? strprintf(" peeraddr=%s", peer.addr.ToStringAddrPort()) : "" ); @@ -4448,7 +4449,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type, // If AcceptBlockHeader returned true, it set pindex assert(pindex); if (received_new_header) { - LogBlockHeader(*pindex, pfrom, /*via_compact_block=*/true); + LogBlockHeader(pindex->GetBlockHash(), pindex->nHeight, pfrom, /*via_compact_block=*/true); } bool fProcessBLOCKTXN = false; @@ -4739,7 +4740,10 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type, min_pow_checked = true; } } - ProcessBlock(pfrom, pblock, forceProcessing, min_pow_checked); + bool new_block = ProcessBlock(pfrom, pblock, forceProcessing, min_pow_checked); + if (new_block && min_pow_checked) { + LogBlockHeader(hash, /*height=*/{}, pfrom, /*via_compact_block=*/false, /*via_unsolicited_block=*/true); + } return; }