Skip to content

Commit 1e92e04

Browse files
committed
Merge #9765: Harden against mistakes handling invalid blocks
ba803ef Harden against mistakes handling invalid blocks (Suhas Daftuar)
2 parents 7a93af8 + ba803ef commit 1e92e04

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

qa/rpc-tests/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(16, b'bad-prevblk'))
401+
yield rejected(RejectResult(0, b'bad-prevblk'))
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(16, b'bad-prevblk'))
413+
yield rejected(RejectResult(0, b'bad-prevblk'))
414414

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

src/validation.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3187,7 +3187,7 @@ static bool AcceptBlock(const std::shared_ptr<const CBlock>& pblock, CValidation
31873187
}
31883188
if (fNewBlock) *fNewBlock = true;
31893189

3190-
if (!CheckBlock(block, state, chainparams.GetConsensus(), GetAdjustedTime()) ||
3190+
if (!CheckBlock(block, state, chainparams.GetConsensus()) ||
31913191
!ContextualCheckBlock(block, state, chainparams.GetConsensus(), pindex->pprev)) {
31923192
if (state.IsInvalid() && !state.CorruptionPossible()) {
31933193
pindex->nStatus |= BLOCK_FAILED_VALID;
@@ -3229,13 +3229,19 @@ static bool AcceptBlock(const std::shared_ptr<const CBlock>& pblock, CValidation
32293229
bool ProcessNewBlock(const CChainParams& chainparams, const std::shared_ptr<const CBlock> pblock, bool fForceProcessing, bool *fNewBlock)
32303230
{
32313231
{
3232-
LOCK(cs_main);
3233-
3234-
// Store to disk
32353232
CBlockIndex *pindex = NULL;
32363233
if (fNewBlock) *fNewBlock = false;
32373234
CValidationState state;
3238-
bool ret = AcceptBlock(pblock, state, chainparams, &pindex, fForceProcessing, NULL, fNewBlock);
3235+
// Ensure that CheckBlock() passes before calling AcceptBlock, as
3236+
// belt-and-suspenders.
3237+
bool ret = CheckBlock(*pblock, state, chainparams.GetConsensus());
3238+
3239+
LOCK(cs_main);
3240+
3241+
if (ret) {
3242+
// Store to disk
3243+
ret = AcceptBlock(pblock, state, chainparams, &pindex, fForceProcessing, NULL, fNewBlock);
3244+
}
32393245
CheckBlockIndex(chainparams.GetConsensus());
32403246
if (!ret) {
32413247
GetMainSignals().BlockChecked(*pblock, state);

0 commit comments

Comments
 (0)