Changeset View
Changeset View
Standalone View
Standalone View
src/validation.cpp
Show First 20 Lines • Show All 3,437 Lines • ▼ Show 20 Lines | |||||
/** | /** | ||||
* Return true if the provided block header is valid. | * Return true if the provided block header is valid. | ||||
* Only verify PoW if blockValidationOptions is configured to do so. | * Only verify PoW if blockValidationOptions is configured to do so. | ||||
* This allows validation of headers on which the PoW hasn't been done. | * This allows validation of headers on which the PoW hasn't been done. | ||||
* For example: to validate template handed to mining software. | * For example: to validate template handed to mining software. | ||||
* Do not call this for any check that depends on the context. | * Do not call this for any check that depends on the context. | ||||
* For context-dependent calls, see ContextualCheckBlockHeader. | * For context-dependent calls, see ContextualCheckBlockHeader. | ||||
*/ | */ | ||||
static bool CheckBlockHeader(const Config &config, const CBlockHeader &block, | static bool CheckBlockHeader(const CBlockHeader &block, CValidationState &state, | ||||
CValidationState &state, | const Consensus::Params ¶ms, | ||||
BlockValidationOptions validationOptions) { | BlockValidationOptions validationOptions) { | ||||
// Check proof of work matches claimed amount | // Check proof of work matches claimed amount | ||||
if (validationOptions.shouldValidatePoW() && | if (validationOptions.shouldValidatePoW() && | ||||
!CheckProofOfWork(block.GetHash(), block.nBits, | !CheckProofOfWork(block.GetHash(), block.nBits, params)) { | ||||
config.GetChainParams().GetConsensus())) { | |||||
return state.DoS(50, false, REJECT_INVALID, "high-hash", false, | return state.DoS(50, false, REJECT_INVALID, "high-hash", false, | ||||
"proof of work failed"); | "proof of work failed"); | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
bool CheckBlock(const Config &config, const CBlock &block, | bool CheckBlock(const Config &config, const CBlock &block, | ||||
CValidationState &state, | CValidationState &state, | ||||
BlockValidationOptions validationOptions) { | BlockValidationOptions validationOptions) { | ||||
// These are checks that are independent of context. | // These are checks that are independent of context. | ||||
if (block.fChecked) { | if (block.fChecked) { | ||||
return true; | return true; | ||||
} | } | ||||
// Check that the header is valid (particularly PoW). This is mostly | // Check that the header is valid (particularly PoW). This is mostly | ||||
// redundant with the call in AcceptBlockHeader. | // redundant with the call in AcceptBlockHeader. | ||||
if (!CheckBlockHeader(config, block, state, validationOptions)) { | if (!CheckBlockHeader(block, state, config.GetChainParams().GetConsensus(), | ||||
validationOptions)) { | |||||
return false; | return false; | ||||
} | } | ||||
// Check the merkle root. | // Check the merkle root. | ||||
if (validationOptions.shouldValidateMerkleRoot()) { | if (validationOptions.shouldValidateMerkleRoot()) { | ||||
bool mutated; | bool mutated; | ||||
uint256 hashMerkleRoot2 = BlockMerkleRoot(block, &mutated); | uint256 hashMerkleRoot2 = BlockMerkleRoot(block, &mutated); | ||||
if (block.hashMerkleRoot != hashMerkleRoot2) { | if (block.hashMerkleRoot != hashMerkleRoot2) { | ||||
▲ Show 20 Lines • Show All 312 Lines • ▼ Show 20 Lines | if (hash != chainparams.GetConsensus().hashGenesisBlock) { | ||||
return state.Invalid(error("%s: block %s is marked invalid", | return state.Invalid(error("%s: block %s is marked invalid", | ||||
__func__, hash.ToString()), | __func__, hash.ToString()), | ||||
0, "duplicate"); | 0, "duplicate"); | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
if (!CheckBlockHeader(config, block, state, | if (!CheckBlockHeader(block, state, chainparams.GetConsensus(), | ||||
BlockValidationOptions(config))) { | BlockValidationOptions(config))) { | ||||
return error("%s: Consensus::CheckBlockHeader: %s, %s", __func__, | return error("%s: Consensus::CheckBlockHeader: %s, %s", __func__, | ||||
hash.ToString(), FormatStateMessage(state)); | hash.ToString(), FormatStateMessage(state)); | ||||
} | } | ||||
// Get prev block index | // Get prev block index | ||||
BlockMap::iterator mi = mapBlockIndex.find(block.hashPrevBlock); | BlockMap::iterator mi = mapBlockIndex.find(block.hashPrevBlock); | ||||
if (mi == mapBlockIndex.end()) { | if (mi == mapBlockIndex.end()) { | ||||
▲ Show 20 Lines • Show All 1,910 Lines • Show Last 20 Lines |