aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWladimir J. van der Laan <laanwj@gmail.com>2017-02-16 10:22:17 +0100
committerWladimir J. van der Laan <laanwj@gmail.com>2017-02-16 10:23:41 +0100
commit1e92e041ddc8232bcf64f09fd70891b80ac05c66 (patch)
treebe67cb270126a6fa21ff7f542ebc5afb6b1f194a
parent7a93af8340d95add7f0d9ddf051aa242302cfc10 (diff)
parentba803efb687c2cb408176c546a544a8466c652ea (diff)
downloadbitcoin-1e92e041ddc8232bcf64f09fd70891b80ac05c66.tar.xz
Merge #9765: Harden against mistakes handling invalid blocks
ba803ef Harden against mistakes handling invalid blocks (Suhas Daftuar)
-rwxr-xr-xqa/rpc-tests/p2p-fullblocktest.py4
-rw-r--r--src/validation.cpp16
2 files changed, 13 insertions, 7 deletions
diff --git a/qa/rpc-tests/p2p-fullblocktest.py b/qa/rpc-tests/p2p-fullblocktest.py
index e4b889d761..44e5f10322 100755
--- a/qa/rpc-tests/p2p-fullblocktest.py
+++ b/qa/rpc-tests/p2p-fullblocktest.py
@@ -398,7 +398,7 @@ class FullBlockTest(ComparisonTestFramework):
# Extend the b26 chain to make sure bitcoind isn't accepting b26
b27 = block(27, spend=out[7])
- yield rejected(RejectResult(16, b'bad-prevblk'))
+ yield rejected(RejectResult(0, b'bad-prevblk'))
# Now try a too-large-coinbase script
tip(15)
@@ -410,7 +410,7 @@ class FullBlockTest(ComparisonTestFramework):
# Extend the b28 chain to make sure bitcoind isn't accepting b28
b29 = block(29, spend=out[7])
- yield rejected(RejectResult(16, b'bad-prevblk'))
+ yield rejected(RejectResult(0, b'bad-prevblk'))
# b30 has a max-sized coinbase scriptSig.
tip(23)
diff --git a/src/validation.cpp b/src/validation.cpp
index fe8f8365be..4ce0723b21 100644
--- a/src/validation.cpp
+++ b/src/validation.cpp
@@ -3187,7 +3187,7 @@ static bool AcceptBlock(const std::shared_ptr<const CBlock>& pblock, CValidation
}
if (fNewBlock) *fNewBlock = true;
- if (!CheckBlock(block, state, chainparams.GetConsensus(), GetAdjustedTime()) ||
+ if (!CheckBlock(block, state, chainparams.GetConsensus()) ||
!ContextualCheckBlock(block, state, chainparams.GetConsensus(), pindex->pprev)) {
if (state.IsInvalid() && !state.CorruptionPossible()) {
pindex->nStatus |= BLOCK_FAILED_VALID;
@@ -3229,13 +3229,19 @@ static bool AcceptBlock(const std::shared_ptr<const CBlock>& pblock, CValidation
bool ProcessNewBlock(const CChainParams& chainparams, const std::shared_ptr<const CBlock> pblock, bool fForceProcessing, bool *fNewBlock)
{
{
- LOCK(cs_main);
-
- // Store to disk
CBlockIndex *pindex = NULL;
if (fNewBlock) *fNewBlock = false;
CValidationState state;
- bool ret = AcceptBlock(pblock, state, chainparams, &pindex, fForceProcessing, NULL, fNewBlock);
+ // Ensure that CheckBlock() passes before calling AcceptBlock, as
+ // belt-and-suspenders.
+ bool ret = CheckBlock(*pblock, state, chainparams.GetConsensus());
+
+ LOCK(cs_main);
+
+ if (ret) {
+ // Store to disk
+ ret = AcceptBlock(pblock, state, chainparams, &pindex, fForceProcessing, NULL, fNewBlock);
+ }
CheckBlockIndex(chainparams.GetConsensus());
if (!ret) {
GetMainSignals().BlockChecked(*pblock, state);