Skip to content

Commit 9d3e39c

Browse files
committed
Log block header in net_processing
Previously ChainstateManager::AcceptBlockHeader would log when it saw a new header. This commit moves logging to the call site(s) in net_processing. The next commits will then log which peer sent it and whether it was part of a compact block. This commit changes behavior: - when multiple headers are received in a single message, only the last one is logged - if any of the headers are invalid, the valid ones are not logged This happens because net_processing calls ProcessNewBlockHeaders with multiple headers, which then calls AcceptBlockHeader one header at a time. Additionally: - when the header is received via a compact block, there's no more duplicate log (a later commit also unifies logging code paths)
1 parent cdc3299 commit 9d3e39c

File tree

3 files changed

+34
-18
lines changed

3 files changed

+34
-18
lines changed

src/net_processing.cpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,8 @@ class PeerManagerImpl final : public PeerManager
10711071

10721072
void AddAddressKnown(Peer& peer, const CAddress& addr) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex);
10731073
void PushAddress(Peer& peer, const CAddress& addr) EXCLUSIVE_LOCKS_REQUIRED(g_msgproc_mutex);
1074+
1075+
void LogBlockHeader(const CBlockIndex& index);
10741076
};
10751077

10761078
const CNodeState* PeerManagerImpl::State(NodeId pnode) const
@@ -2970,14 +2972,21 @@ void PeerManagerImpl::ProcessHeadersMessage(CNode& pfrom, Peer& peer,
29702972

29712973
// Now process all the headers.
29722974
BlockValidationState state;
2973-
if (!m_chainman.ProcessNewBlockHeaders(headers, /*min_pow_checked=*/true, state, &pindexLast)) {
2975+
const bool processed{m_chainman.ProcessNewBlockHeaders(headers,
2976+
/*min_pow_checked=*/true,
2977+
state, &pindexLast)};
2978+
if (!processed) {
29742979
if (state.IsInvalid()) {
29752980
MaybePunishNodeForBlock(pfrom.GetId(), state, via_compact_block, "invalid header received");
29762981
return;
29772982
}
29782983
}
29792984
assert(pindexLast);
29802985

2986+
if (processed && received_new_header) {
2987+
LogBlockHeader(*pindexLast);
2988+
}
2989+
29812990
// Consider fetching more headers if we are not using our headers-sync mechanism.
29822991
if (nCount == m_opts.max_headers_result && !have_headers_sync) {
29832992
// Headers message had its maximum size; the peer may have more headers.
@@ -3405,6 +3414,29 @@ void PeerManagerImpl::ProcessCompactBlockTxns(CNode& pfrom, Peer& peer, const Bl
34053414
return;
34063415
}
34073416

3417+
void PeerManagerImpl::LogBlockHeader(const CBlockIndex& index) {
3418+
// To prevent log spam, this function should only be called after it was determined that a
3419+
// header is both new and valid.
3420+
//
3421+
// These messages are valuable for detecting potential selfish mining behavior;
3422+
// if multiple displacing headers are seen near simultaneously across many
3423+
// nodes in the network, this might be an indication of selfish mining.
3424+
// In addition it can be used to identify peers which send us a header, but
3425+
// don't followup with a complete and valid (compact) block.
3426+
// Having this log by default when not in IBD ensures broad availability of
3427+
// this data in case investigation is merited.
3428+
const auto msg = strprintf(
3429+
"Saw new header hash=%s height=%d",
3430+
index.GetBlockHash().ToString(),
3431+
index.nHeight
3432+
);
3433+
if (m_chainman.IsInitialBlockDownload()) {
3434+
LogDebug(BCLog::VALIDATION, "%s", msg);
3435+
} else {
3436+
LogInfo("%s", msg);
3437+
}
3438+
}
3439+
34083440
void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type, DataStream& vRecv,
34093441
const std::chrono::microseconds time_received,
34103442
const std::atomic<bool>& interruptMsgProc)

src/validation.cpp

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4403,23 +4403,6 @@ bool ChainstateManager::AcceptBlockHeader(const CBlockHeader& block, BlockValida
44034403
if (ppindex)
44044404
*ppindex = pindex;
44054405

4406-
// Since this is the earliest point at which we have determined that a
4407-
// header is both new and valid, log here.
4408-
//
4409-
// These messages are valuable for detecting potential selfish mining behavior;
4410-
// if multiple displacing headers are seen near simultaneously across many
4411-
// nodes in the network, this might be an indication of selfish mining. Having
4412-
// this log by default when not in IBD ensures broad availability of this data
4413-
// in case investigation is merited.
4414-
const auto msg = strprintf(
4415-
"Saw new header hash=%s height=%d", hash.ToString(), pindex->nHeight);
4416-
4417-
if (IsInitialBlockDownload()) {
4418-
LogPrintLevel(BCLog::VALIDATION, BCLog::Level::Debug, "%s\n", msg);
4419-
} else {
4420-
LogPrintf("%s\n", msg);
4421-
}
4422-
44234406
return true;
44244407
}
44254408

src/validation.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,7 @@ class ChainstateManager
12181218
* @param[in] min_pow_checked True if proof-of-work anti-DoS checks have been done by caller for headers chain
12191219
* @param[out] state This may be set to an Error state if any error occurred processing them
12201220
* @param[out] ppindex If set, the pointer will be set to point to the last new block index object for the given headers
1221+
* @returns false if AcceptBlockHeader fails on any of the headers, true otherwise (including if headers were already known)
12211222
*/
12221223
bool ProcessNewBlockHeaders(std::span<const CBlockHeader> headers, bool min_pow_checked, BlockValidationState& state, const CBlockIndex** ppindex = nullptr) LOCKS_EXCLUDED(cs_main);
12231224

0 commit comments

Comments
 (0)