Skip to content

Commit

Permalink
Add checks when coming from database, to accept the blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
blondfrogs committed Jul 6, 2020
1 parent 8141607 commit c455cfc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
19 changes: 14 additions & 5 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3951,7 +3951,7 @@ static bool CheckBlockHeader(const CBlockHeader& block, CValidationState& state,
return true;
}

bool CheckBlock(const CBlock& block, CValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW, bool fCheckMerkleRoot)
bool CheckBlock(const CBlock& block, CValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW, bool fCheckMerkleRoot, bool fDBCheck)
{
// These are checks that are independent of context.

Expand Down Expand Up @@ -3996,10 +3996,19 @@ bool CheckBlock(const CBlock& block, CValidationState& state, const Consensus::P
return state.DoS(100, false, REJECT_INVALID, "bad-cb-multiple", false, "more than one coinbase");

// Check transactions
for (const auto& tx : block.vtx)
if (!CheckTransaction(*tx, state, true, false, true))
for (const auto& tx : block.vtx) {
// We only want to check the blocks when they are added to our chain
// We want to make sure when nodes shutdown and restart that they still
// verify the blocks in the database correctly even if Enforce Value BIP is active
bool fCheckBlock = true;
if (fDBCheck){
fCheckBlock = false;
}
if (!CheckTransaction(*tx, state, true, false, fCheckBlock))
return state.Invalid(false, state.GetRejectCode(), state.GetRejectReason(),
strprintf("Transaction check failed (tx hash %s) %s %s", tx->GetHash().ToString(), state.GetDebugMessage(), state.GetRejectReason()));
strprintf("Transaction check failed (tx hash %s) %s %s", tx->GetHash().ToString(),
state.GetDebugMessage(), state.GetRejectReason()));
}

unsigned int nSigOps = 0;
for (const auto& tx : block.vtx)
Expand Down Expand Up @@ -4878,7 +4887,7 @@ bool CVerifyDB::VerifyDB(const CChainParams& chainparams, CCoinsView *coinsview,
if (!ReadBlockFromDisk(block, pindex, chainparams.GetConsensus()))
return error("VerifyDB(): *** ReadBlockFromDisk failed at %d, hash=%s", pindex->nHeight, pindex->GetBlockHash().ToString());
// check level 1: verify block validity
if (nCheckLevel >= 1 && !CheckBlock(block, state, chainparams.GetConsensus(), true, true)) // fCheckAssetDuplicate set to false, because we don't want to fail because the asset exists in our database, when loading blocks from our asset databse
if (nCheckLevel >= 1 && !CheckBlock(block, state, chainparams.GetConsensus(), true, true, true)) // fCheckAssetDuplicate set to false, because we don't want to fail because the asset exists in our database, when loading blocks from our asset databse
return error("%s: *** found bad block at %d, hash=%s (%s)\n", __func__,
pindex->nHeight, pindex->GetBlockHash().ToString(), FormatStateMessage(state));
// check level 2: verify undo validity
Expand Down
2 changes: 1 addition & 1 deletion src/validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus
/** Functions for validating blocks and updating the block tree */

/** Context-independent validity checks */
bool CheckBlock(const CBlock& block, CValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW = true, bool fCheckMerkleRoot = true);
bool CheckBlock(const CBlock& block, CValidationState& state, const Consensus::Params& consensusParams, bool fCheckPOW = true, bool fCheckMerkleRoot = true, bool fDBCheck = false);

/** Check a block is completely valid from start to finish (only works on top of our current best block, with cs_main held) */
bool TestBlockValidity(CValidationState& state, const CChainParams& chainparams, const CBlock& block, CBlockIndex* pindexPrev, bool fCheckPOW = true, bool fCheckMerkleRoot = true);
Expand Down

0 comments on commit c455cfc

Please sign in to comment.