Skip to content

Commit 5d08c9c

Browse files
committed
Send the correct error code in reject messages
1 parent edc62c9 commit 5d08c9c

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

src/net_processing.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -855,8 +855,8 @@ void PeerLogicValidation::BlockChecked(const CBlock& block, const CValidationSta
855855

856856
int nDoS = 0;
857857
if (state.IsInvalid(nDoS)) {
858-
if (it != mapBlockSource.end() && State(it->second.first)) {
859-
assert (state.GetRejectCode() < REJECT_INTERNAL); // Blocks are never rejected with internal reject codes
858+
// Don't send reject message with code 0 or an internal reject code.
859+
if (it != mapBlockSource.end() && State(it->second.first) && state.GetRejectCode() > 0 && state.GetRejectCode() < REJECT_INTERNAL) {
860860
CBlockReject reject = {(unsigned char)state.GetRejectCode(), state.GetRejectReason().substr(0, MAX_REJECT_MESSAGE_LENGTH), hash};
861861
State(it->second.first)->rejects.push_back(reject);
862862
if (nDoS > 0 && it->second.second)
@@ -1942,7 +1942,7 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
19421942
LogPrint("mempoolrej", "%s from peer=%d was not accepted: %s\n", tx.GetHash().ToString(),
19431943
pfrom->id,
19441944
FormatStateMessage(state));
1945-
if (state.GetRejectCode() < REJECT_INTERNAL) // Never send AcceptToMemoryPool's internal codes over P2P
1945+
if (state.GetRejectCode() > 0 && state.GetRejectCode() < REJECT_INTERNAL) // Never send AcceptToMemoryPool's internal codes over P2P
19461946
connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::REJECT, strCommand, (unsigned char)state.GetRejectCode(),
19471947
state.GetRejectReason().substr(0, MAX_REJECT_MESSAGE_LENGTH), inv.hash));
19481948
if (nDoS > 0) {

src/validation.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,7 +1914,7 @@ bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockIndex* pin
19141914
REJECT_INVALID, "bad-cb-amount");
19151915

19161916
if (!control.Wait())
1917-
return state.DoS(100, false);
1917+
return state.DoS(100, error("%s: CheckQueue failed", __func__), REJECT_INVALID, "block-validation-failed");
19181918
int64_t nTime4 = GetTimeMicros(); nTimeVerify += nTime4 - nTime2;
19191919
LogPrint("bench", " - Verify %u txins: %.2fms (%.3fms/txin) [%.2fs]\n", nInputs - 1, 0.001 * (nTime4 - nTime2), nInputs <= 1 ? 0 : 0.001 * (nTime4 - nTime2) / (nInputs-1), nTimeVerify * 0.000001);
19201920

@@ -2881,10 +2881,12 @@ static bool CheckIndexAgainstCheckpoint(const CBlockIndex* pindexPrev, CValidati
28812881
return true;
28822882

28832883
int nHeight = pindexPrev->nHeight+1;
2884-
// Don't accept any forks from the main chain prior to last checkpoint
2884+
// Don't accept any forks from the main chain prior to last checkpoint.
2885+
// GetLastCheckpoint finds the last checkpoint in MapCheckpoints that's in our
2886+
// MapBlockIndex.
28852887
CBlockIndex* pcheckpoint = Checkpoints::GetLastCheckpoint(chainparams.Checkpoints());
28862888
if (pcheckpoint && nHeight < pcheckpoint->nHeight)
2887-
return state.DoS(100, error("%s: forked chain older than last checkpoint (height %d)", __func__, nHeight));
2889+
return state.DoS(100, error("%s: forked chain older than last checkpoint (height %d)", __func__, nHeight), REJECT_CHECKPOINT, "bad-fork-prior-to-checkpoint");
28882890

28892891
return true;
28902892
}
@@ -3083,7 +3085,7 @@ static bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state
30833085
CBlockIndex* pindexPrev = NULL;
30843086
BlockMap::iterator mi = mapBlockIndex.find(block.hashPrevBlock);
30853087
if (mi == mapBlockIndex.end())
3086-
return state.DoS(10, error("%s: prev block not found", __func__), 0, "bad-prevblk");
3088+
return state.DoS(10, error("%s: prev block not found", __func__), 0, "prev-blk-not-found");
30873089
pindexPrev = (*mi).second;
30883090
if (pindexPrev->nStatus & BLOCK_FAILED_MASK)
30893091
return state.DoS(100, error("%s: prev block invalid", __func__), REJECT_INVALID, "bad-prevblk");

test/functional/p2p-fullblocktest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ def update_block(block_number, new_transactions):
398398

399399
# Extend the b26 chain to make sure bitcoind isn't accepting b26
400400
b27 = block(27, spend=out[7])
401-
yield rejected(RejectResult(0, b'bad-prevblk'))
401+
yield rejected(False)
402402

403403
# Now try a too-large-coinbase script
404404
tip(15)
@@ -410,7 +410,7 @@ def update_block(block_number, new_transactions):
410410

411411
# Extend the b28 chain to make sure bitcoind isn't accepting b28
412412
b29 = block(29, spend=out[7])
413-
yield rejected(RejectResult(0, b'bad-prevblk'))
413+
yield rejected(False)
414414

415415
# b30 has a max-sized coinbase scriptSig.
416416
tip(23)

0 commit comments

Comments
 (0)