Changeset View
Changeset View
Standalone View
Standalone View
src/validation.cpp
Show First 20 Lines • Show All 946 Lines • ▼ Show 20 Lines | bool ReadBlockFromDisk(CBlock &block, const FlatFilePos &pos, | ||||
try { | try { | ||||
filein >> block; | filein >> block; | ||||
} catch (const std::exception &e) { | } catch (const std::exception &e) { | ||||
return error("%s: Deserialize or I/O error - %s at %s", __func__, | return error("%s: Deserialize or I/O error - %s at %s", __func__, | ||||
e.what(), pos.ToString()); | e.what(), pos.ToString()); | ||||
} | } | ||||
// Check the header | // Check the header | ||||
if (!CheckProofOfWork(block.GetHash(), block.nBits, config)) { | if (!CheckProofOfWork(block.GetHash(), block.nBits, | ||||
config.GetChainParams().GetConsensus())) { | |||||
return error("ReadBlockFromDisk: Errors in block header at %s", | return error("ReadBlockFromDisk: Errors in block header at %s", | ||||
pos.ToString()); | pos.ToString()); | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
bool ReadBlockFromDisk(CBlock &block, const CBlockIndex *pindex, | bool ReadBlockFromDisk(CBlock &block, const CBlockIndex *pindex, | ||||
▲ Show 20 Lines • Show All 2,474 Lines • ▼ Show 20 Lines | |||||
* 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( | static bool CheckBlockHeader( | ||||
const Config &config, const CBlockHeader &block, CValidationState &state, | const Config &config, const CBlockHeader &block, CValidationState &state, | ||||
BlockValidationOptions validationOptions = BlockValidationOptions()) { | BlockValidationOptions validationOptions = BlockValidationOptions()) { | ||||
// 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, config)) { | !CheckProofOfWork(block.GetHash(), block.nBits, | ||||
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, | ||||
▲ Show 20 Lines • Show All 125 Lines • ▼ Show 20 Lines | static bool ContextualCheckBlockHeader(const Config &config, | ||||
const CBlockIndex *pindexPrev, | const CBlockIndex *pindexPrev, | ||||
int64_t nAdjustedTime) { | int64_t nAdjustedTime) { | ||||
assert(pindexPrev != nullptr); | assert(pindexPrev != nullptr); | ||||
const int nHeight = pindexPrev->nHeight + 1; | const int nHeight = pindexPrev->nHeight + 1; | ||||
// Check proof of work | // Check proof of work | ||||
const Consensus::Params &consensusParams = | const Consensus::Params &consensusParams = | ||||
config.GetChainParams().GetConsensus(); | config.GetChainParams().GetConsensus(); | ||||
if (block.nBits != GetNextWorkRequired(pindexPrev, &block, config)) { | if (block.nBits != | ||||
GetNextWorkRequired(pindexPrev, &block, consensusParams)) { | |||||
LogPrintf("bad bits after height: %d\n", pindexPrev->nHeight); | LogPrintf("bad bits after height: %d\n", pindexPrev->nHeight); | ||||
return state.DoS(100, false, REJECT_INVALID, "bad-diffbits", false, | return state.DoS(100, false, REJECT_INVALID, "bad-diffbits", false, | ||||
"incorrect proof of work"); | "incorrect proof of work"); | ||||
} | } | ||||
// Check against checkpoints | // Check against checkpoints | ||||
if (fCheckpointsEnabled) { | if (fCheckpointsEnabled) { | ||||
const CCheckpointData &checkpoints = | const CCheckpointData &checkpoints = | ||||
▲ Show 20 Lines • Show All 791 Lines • ▼ Show 20 Lines | CBlockIndex *CChainState::InsertBlockIndex(const uint256 &hash) { | ||||
mi = mapBlockIndex.insert(std::make_pair(hash, pindexNew)).first; | mi = mapBlockIndex.insert(std::make_pair(hash, pindexNew)).first; | ||||
pindexNew->phashBlock = &((*mi).first); | pindexNew->phashBlock = &((*mi).first); | ||||
return pindexNew; | return pindexNew; | ||||
} | } | ||||
bool CChainState::LoadBlockIndex(const Config &config, | bool CChainState::LoadBlockIndex(const Config &config, | ||||
CBlockTreeDB &blocktree) { | CBlockTreeDB &blocktree) { | ||||
if (!blocktree.LoadBlockIndexGuts(config, [this](const uint256 &hash) { | if (!blocktree.LoadBlockIndexGuts(config.GetChainParams().GetConsensus(), | ||||
[this](const uint256 &hash) { | |||||
return this->InsertBlockIndex(hash); | return this->InsertBlockIndex(hash); | ||||
})) { | })) { | ||||
return false; | return false; | ||||
} | } | ||||
boost::this_thread::interruption_point(); | boost::this_thread::interruption_point(); | ||||
// Calculate nChainWork | // Calculate nChainWork | ||||
std::vector<std::pair<int, CBlockIndex *>> vSortedByHeight; | std::vector<std::pair<int, CBlockIndex *>> vSortedByHeight; | ||||
vSortedByHeight.reserve(mapBlockIndex.size()); | vSortedByHeight.reserve(mapBlockIndex.size()); | ||||
▲ Show 20 Lines • Show All 1,297 Lines • Show Last 20 Lines |