Skip to content

Commit d3a185a

Browse files
committed
net: Move misbehaving logging to net logging category
This moves the error messages for misbehavior (when available) into the line that reports the misbehavior, as well as moves the logging to the `net` category. This is a continuation of #11583 and avoids serious-looking errors due to misbehaving peers. To do this, Misbehaving() gains an optional `message` argument. E.g. change: 2018-01-18 16:02:27 Misbehaving: x.x.x.x:62174 peer=164603 (80 -> 100) BAN THRESHOLD EXCEEDED 2018-01-18 16:02:27 ERROR: non-continuous headers sequence to 2018-01-18 16:02:27 Misbehaving: x.x.x.x:62174 peer=164603 (80 -> 100) BAN THRESHOLD EXCEEDED: non-continuous headers sequence
1 parent 898f560 commit d3a185a

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

src/net_processing.cpp

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ unsigned int LimitOrphanTxSize(unsigned int nMaxOrphans)
750750
}
751751

752752
// Requires cs_main.
753-
void Misbehaving(NodeId pnode, int howmuch)
753+
void Misbehaving(NodeId pnode, int howmuch, const std::string& message)
754754
{
755755
if (howmuch == 0)
756756
return;
@@ -761,12 +761,13 @@ void Misbehaving(NodeId pnode, int howmuch)
761761

762762
state->nMisbehavior += howmuch;
763763
int banscore = gArgs.GetArg("-banscore", DEFAULT_BANSCORE_THRESHOLD);
764+
std::string message_prefixed = message.empty() ? "" : (": " + message);
764765
if (state->nMisbehavior >= banscore && state->nMisbehavior - howmuch < banscore)
765766
{
766-
LogPrintf("%s: %s peer=%d (%d -> %d) BAN THRESHOLD EXCEEDED\n", __func__, state->name, pnode, state->nMisbehavior-howmuch, state->nMisbehavior);
767+
LogPrint(BCLog::NET, "%s: %s peer=%d (%d -> %d) BAN THRESHOLD EXCEEDED%s\n", __func__, state->name, pnode, state->nMisbehavior-howmuch, state->nMisbehavior, message_prefixed);
767768
state->fShouldBan = true;
768769
} else
769-
LogPrintf("%s: %s peer=%d (%d -> %d)\n", __func__, state->name, pnode, state->nMisbehavior-howmuch, state->nMisbehavior);
770+
LogPrint(BCLog::NET, "%s: %s peer=%d (%d -> %d)%s\n", __func__, state->name, pnode, state->nMisbehavior-howmuch, state->nMisbehavior, message_prefixed);
770771
}
771772

772773

@@ -1260,8 +1261,7 @@ inline void static SendBlockTransactions(const CBlock& block, const BlockTransac
12601261
for (size_t i = 0; i < req.indexes.size(); i++) {
12611262
if (req.indexes[i] >= block.vtx.size()) {
12621263
LOCK(cs_main);
1263-
Misbehaving(pfrom->GetId(), 100);
1264-
LogPrintf("Peer %d sent us a getblocktxn with out-of-bounds tx indices", pfrom->GetId());
1264+
Misbehaving(pfrom->GetId(), 100, strprintf("Peer %d sent us a getblocktxn with out-of-bounds tx indices", pfrom->GetId()));
12651265
return;
12661266
}
12671267
resp.txn[i] = block.vtx[req.indexes[i]];
@@ -1318,8 +1318,8 @@ bool static ProcessHeadersMessage(CNode *pfrom, CConnman *connman, const std::ve
13181318
uint256 hashLastBlock;
13191319
for (const CBlockHeader& header : headers) {
13201320
if (!hashLastBlock.IsNull() && header.hashPrevBlock != hashLastBlock) {
1321-
Misbehaving(pfrom->GetId(), 20);
1322-
return error("non-continuous headers sequence");
1321+
Misbehaving(pfrom->GetId(), 20, "non-continuous headers sequence");
1322+
return false;
13231323
}
13241324
hashLastBlock = header.GetHash();
13251325
}
@@ -1338,7 +1338,9 @@ bool static ProcessHeadersMessage(CNode *pfrom, CConnman *connman, const std::ve
13381338
if (state.IsInvalid(nDoS)) {
13391339
LOCK(cs_main);
13401340
if (nDoS > 0) {
1341-
Misbehaving(pfrom->GetId(), nDoS);
1341+
Misbehaving(pfrom->GetId(), nDoS, "invalid header received");
1342+
} else {
1343+
LogPrint(BCLog::NET, "peer=%d: invalid header received\n", pfrom->GetId());
13421344
}
13431345
if (punish_duplicate_invalid && mapBlockIndex.find(first_invalid_header.GetHash()) != mapBlockIndex.end()) {
13441346
// Goal: don't allow outbound peers to use up our outbound
@@ -1374,7 +1376,7 @@ bool static ProcessHeadersMessage(CNode *pfrom, CConnman *connman, const std::ve
13741376
// etc), and not just the duplicate-invalid case.
13751377
pfrom->fDisconnect = true;
13761378
}
1377-
return error("invalid header received");
1379+
return false;
13781380
}
13791381
}
13801382

@@ -1783,8 +1785,8 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
17831785
if (vAddr.size() > 1000)
17841786
{
17851787
LOCK(cs_main);
1786-
Misbehaving(pfrom->GetId(), 20);
1787-
return error("message addr size() = %u", vAddr.size());
1788+
Misbehaving(pfrom->GetId(), 20, strprintf("message addr size() = %u", vAddr.size()));
1789+
return false;
17881790
}
17891791

17901792
// Store the new addresses
@@ -1859,8 +1861,8 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
18591861
if (vInv.size() > MAX_INV_SZ)
18601862
{
18611863
LOCK(cs_main);
1862-
Misbehaving(pfrom->GetId(), 20);
1863-
return error("message inv size() = %u", vInv.size());
1864+
Misbehaving(pfrom->GetId(), 20, strprintf("message inv size() = %u", vInv.size()));
1865+
return false;
18641866
}
18651867

18661868
bool fBlocksOnly = !fRelayTxes;
@@ -1920,8 +1922,8 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
19201922
if (vInv.size() > MAX_INV_SZ)
19211923
{
19221924
LOCK(cs_main);
1923-
Misbehaving(pfrom->GetId(), 20);
1924-
return error("message getdata size() = %u", vInv.size());
1925+
Misbehaving(pfrom->GetId(), 20, strprintf("message getdata size() = %u", vInv.size()));
1926+
return false;
19251927
}
19261928

19271929
LogPrint(BCLog::NET, "received getdata (%u invsz) peer=%d\n", vInv.size(), pfrom->GetId());
@@ -2323,9 +2325,8 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
23232325
int nDoS;
23242326
if (state.IsInvalid(nDoS)) {
23252327
if (nDoS > 0) {
2326-
LogPrintf("Peer %d sent us invalid header via cmpctblock\n", pfrom->GetId());
23272328
LOCK(cs_main);
2328-
Misbehaving(pfrom->GetId(), nDoS);
2329+
Misbehaving(pfrom->GetId(), nDoS, strprintf("Peer %d sent us invalid header via cmpctblock\n", pfrom->GetId()));
23292330
} else {
23302331
LogPrint(BCLog::NET, "Peer %d sent us invalid header via cmpctblock\n", pfrom->GetId());
23312332
}
@@ -2411,8 +2412,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
24112412
ReadStatus status = partialBlock.InitData(cmpctblock, vExtraTxnForCompact);
24122413
if (status == READ_STATUS_INVALID) {
24132414
MarkBlockAsReceived(pindex->GetBlockHash()); // Reset in-flight state in case of whitelist
2414-
Misbehaving(pfrom->GetId(), 100);
2415-
LogPrintf("Peer %d sent us invalid compact block\n", pfrom->GetId());
2415+
Misbehaving(pfrom->GetId(), 100, strprintf("Peer %d sent us invalid compact block\n", pfrom->GetId()));
24162416
return true;
24172417
} else if (status == READ_STATUS_FAILED) {
24182418
// Duplicate txindexes, the block is now in-flight, so just request it
@@ -2539,8 +2539,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
25392539
ReadStatus status = partialBlock.FillBlock(*pblock, resp.txn);
25402540
if (status == READ_STATUS_INVALID) {
25412541
MarkBlockAsReceived(resp.blockhash); // Reset in-flight state in case of whitelist
2542-
Misbehaving(pfrom->GetId(), 100);
2543-
LogPrintf("Peer %d sent us invalid compact block/non-matching block transactions\n", pfrom->GetId());
2542+
Misbehaving(pfrom->GetId(), 100, strprintf("Peer %d sent us invalid compact block/non-matching block transactions\n", pfrom->GetId()));
25442543
return true;
25452544
} else if (status == READ_STATUS_FAILED) {
25462545
// Might have collided, fall back to getdata now :(
@@ -2602,8 +2601,8 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
26022601
unsigned int nCount = ReadCompactSize(vRecv);
26032602
if (nCount > MAX_HEADERS_RESULTS) {
26042603
LOCK(cs_main);
2605-
Misbehaving(pfrom->GetId(), 20);
2606-
return error("headers message size = %u", nCount);
2604+
Misbehaving(pfrom->GetId(), 20, strprintf("headers message size = %u", nCount));
2605+
return false;
26072606
}
26082607
headers.resize(nCount);
26092608
for (unsigned int n = 0; n < nCount; n++) {

src/net_processing.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,6 @@ struct CNodeStateStats {
7979
/** Get statistics from node state */
8080
bool GetNodeStateStats(NodeId nodeid, CNodeStateStats &stats);
8181
/** Increase a node's misbehavior score. */
82-
void Misbehaving(NodeId nodeid, int howmuch);
82+
void Misbehaving(NodeId nodeid, int howmuch, const std::string& message="");
8383

8484
#endif // BITCOIN_NET_PROCESSING_H

0 commit comments

Comments
 (0)