Skip to content

Commit

Permalink
validation: log new header from unsolicited block
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Sjors committed Nov 28, 2023
1 parent 386febd commit 708600f
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> 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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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<int> 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.
//
Expand All @@ -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()) : ""
);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 708600f

Please sign in to comment.