Skip to content

Commit e19586a

Browse files
committed
Merge #10135: [p2p] Send the correct error code in reject messages
5d08c9c Send the correct error code in reject messages (John Newbery) Tree-SHA512: 0cd3ef3ae202584b138cc0bbfba4125635822e0c5a755fb9276a604b39286959ab22dabc3104aa5d7e71358cd69d965de2a333ff04bf3e8ed43cf0296ac01264
2 parents a27dbc5 + 5d08c9c commit e19586a

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
@@ -856,8 +856,8 @@ void PeerLogicValidation::BlockChecked(const CBlock& block, const CValidationSta
856856

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

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

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

28902892
return true;
28912893
}
@@ -3086,7 +3088,7 @@ static bool AcceptBlockHeader(const CBlockHeader& block, CValidationState& state
30863088
CBlockIndex* pindexPrev = NULL;
30873089
BlockMap::iterator mi = mapBlockIndex.find(block.hashPrevBlock);
30883090
if (mi == mapBlockIndex.end())
3089-
return state.DoS(10, error("%s: prev block not found", __func__), 0, "bad-prevblk");
3091+
return state.DoS(10, error("%s: prev block not found", __func__), 0, "prev-blk-not-found");
30903092
pindexPrev = (*mi).second;
30913093
if (pindexPrev->nStatus & BLOCK_FAILED_MASK)
30923094
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)