Skip to content

Commit 664500f

Browse files
committed
Merge bitcoin/bitcoin#27278: Log new headers
2c3a90f log: on new valid header (James O'Beirne) e5ce857 log: net: new header over cmpctblock (James O'Beirne) Pull request description: Alternate to #27276. Devs were [suprised to realize](https://twitter.com/jamesob/status/1637237917201383425) last night that we don't have definitive logging for when a given header was first received. This logs to the main stream when new headers are received outside of IBD, as well as when headers come in over cmpctblocks. The rationale of not hiding these under log categories is that they may be useful to have widely available when debugging strange network activity, and the marginal volume is modest. ACKs for top commit: dergoegge: Code review ACK 2c3a90f achow101: ACK 2c3a90f Sjors: tACK 2c3a90f josibake: ACK bitcoin/bitcoin@2c3a90f Tree-SHA512: 49fdcbe07799c8adc24143d7e5054a0c93fef120d2e9d5fddbd3b119550d895e2985be6ac10dd1825ea23a6fa5479c1b76d5518c136fbd983fa76c0d39dc354f
2 parents f4e42a7 + 2c3a90f commit 664500f

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

src/net_processing.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4190,6 +4190,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
41904190
vRecv >> cmpctblock;
41914191

41924192
bool received_new_header = false;
4193+
const auto blockhash = cmpctblock.header.GetHash();
41934194

41944195
{
41954196
LOCK(cs_main);
@@ -4207,7 +4208,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
42074208
return;
42084209
}
42094210

4210-
if (!m_chainman.m_blockman.LookupBlockIndex(cmpctblock.header.GetHash())) {
4211+
if (!m_chainman.m_blockman.LookupBlockIndex(blockhash)) {
42114212
received_new_header = true;
42124213
}
42134214
}
@@ -4221,6 +4222,11 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
42214222
}
42224223
}
42234224

4225+
if (received_new_header) {
4226+
LogPrintfCategory(BCLog::NET, "Saw new cmpctblock header hash=%s peer=%d\n",
4227+
blockhash.ToString(), pfrom.GetId());
4228+
}
4229+
42244230
// When we succeed in decoding a block's txids from a cmpctblock
42254231
// message we typically jump to the BLOCKTXN handling code, with a
42264232
// dummy (empty) BLOCKTXN message, to re-use the logic there in
@@ -4263,7 +4269,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
42634269
// We requested this block for some reason, but our mempool will probably be useless
42644270
// so we just grab the block via normal getdata
42654271
std::vector<CInv> vInv(1);
4266-
vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(*peer), cmpctblock.header.GetHash());
4272+
vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(*peer), blockhash);
42674273
m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETDATA, vInv));
42684274
}
42694275
return;
@@ -4299,7 +4305,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
42994305
} else if (status == READ_STATUS_FAILED) {
43004306
// Duplicate txindexes, the block is now in-flight, so just request it
43014307
std::vector<CInv> vInv(1);
4302-
vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(*peer), cmpctblock.header.GetHash());
4308+
vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(*peer), blockhash);
43034309
m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETDATA, vInv));
43044310
return;
43054311
}
@@ -4312,7 +4318,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
43124318
if (req.indexes.empty()) {
43134319
// Dirty hack to jump to BLOCKTXN code (TODO: move message handling into their own functions)
43144320
BlockTransactions txn;
4315-
txn.blockhash = cmpctblock.header.GetHash();
4321+
txn.blockhash = blockhash;
43164322
blockTxnMsg << txn;
43174323
fProcessBLOCKTXN = true;
43184324
} else {
@@ -4342,7 +4348,7 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
43424348
// We requested this block, but its far into the future, so our
43434349
// mempool will probably be useless - request the block normally
43444350
std::vector<CInv> vInv(1);
4345-
vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(*peer), cmpctblock.header.GetHash());
4351+
vInv[0] = CInv(MSG_BLOCK | GetFetchFlags(*peer), blockhash);
43464352
m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETDATA, vInv));
43474353
return;
43484354
} else {

src/validation.cpp

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

3848+
// Since this is the earliest point at which we have determined that a
3849+
// header is both new and valid, log here.
3850+
//
3851+
// These messages are valuable for detecting potential selfish mining behavior;
3852+
// if multiple displacing headers are seen near simultaneously across many
3853+
// nodes in the network, this might be an indication of selfish mining. Having
3854+
// this log by default when not in IBD ensures broad availability of this data
3855+
// in case investigation is merited.
3856+
const auto msg = strprintf(
3857+
"Saw new header hash=%s height=%d", hash.ToString(), pindex->nHeight);
3858+
3859+
if (ActiveChainstate().IsInitialBlockDownload()) {
3860+
LogPrintLevel(BCLog::VALIDATION, BCLog::Level::Debug, "%s\n", msg);
3861+
} else {
3862+
LogPrintf("%s\n", msg);
3863+
}
3864+
38483865
return true;
38493866
}
38503867

0 commit comments

Comments
 (0)