Skip to content

Commit 56515c3

Browse files
instagibbsjtimon
authored andcommitted
9102: Really don't validate genesis block
1 parent 5042698 commit 56515c3

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

src/txdb.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,9 +274,11 @@ bool CBlockTreeDB::LoadBlockIndexGuts(const Consensus::Params& consensusParams,
274274
pindexNew->nStatus = diskindex.nStatus;
275275
pindexNew->nTx = diskindex.nTx;
276276

277-
if (!CheckProofOfWork(pindexNew->GetBlockHash(), pindexNew->nBits, consensusParams))
278-
return error("%s: CheckProofOfWork failed: %s", __func__, pindexNew->ToString());
279-
277+
const uint256 block_hash = pindexNew->GetBlockHash();
278+
if (!CheckProofOfWork(block_hash, pindexNew->nBits, consensusParams) &&
279+
block_hash != consensusParams.hashGenesisBlock) {
280+
return error("%s: CheckProofOfWork: %s, %s", __func__, block_hash.ToString(), pindexNew->ToString());
281+
}
280282
pcursor->Next();
281283
} else {
282284
return error("%s: failed to read value", __func__);

src/validation.cpp

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,8 +1091,11 @@ bool ReadBlockFromDisk(CBlock& block, const CDiskBlockPos& pos, const Consensus:
10911091
}
10921092

10931093
// Check the header
1094-
if (!CheckProofOfWork(block.GetHash(), block.nBits, consensusParams))
1094+
const uint256 block_hash = block.GetHash();
1095+
if (!CheckProofOfWork(block_hash, block.nBits, consensusParams) &&
1096+
block_hash != consensusParams.hashGenesisBlock) {
10951097
return error("ReadBlockFromDisk: Errors in block header at %s", pos.ToString());
1098+
}
10961099

10971100
return true;
10981101
}
@@ -1810,6 +1813,18 @@ bool CChainState::ConnectBlock(const CBlock& block, CValidationState& state, CBl
18101813
assert(*pindex->phashBlock == block.GetHash());
18111814
int64_t nTimeStart = GetTimeMicros();
18121815

1816+
// verify that the view's current state corresponds to the previous block
1817+
const uint256 hashPrevBlock = pindex->pprev == nullptr ? uint256() : pindex->pprev->GetBlockHash();
1818+
assert(hashPrevBlock == view.GetBestBlock());
1819+
1820+
// Special case for the genesis block, skipping connection of its transactions
1821+
// (its coinbase is unspendable)
1822+
if (block.GetHash() == chainparams.GetConsensus().hashGenesisBlock) {
1823+
if (!fJustCheck)
1824+
view.SetBestBlock(pindex->GetBlockHash());
1825+
return true;
1826+
}
1827+
18131828
// Check it again in case a previous version let a bad block in
18141829
// NOTE: We don't currently (re-)invoke ContextualCheckBlock() or
18151830
// ContextualCheckBlockHeader() here. This means that if we add a new
@@ -1833,18 +1848,6 @@ bool CChainState::ConnectBlock(const CBlock& block, CValidationState& state, CBl
18331848
return error("%s: Consensus::CheckBlock: %s", __func__, FormatStateMessage(state));
18341849
}
18351850

1836-
// verify that the view's current state corresponds to the previous block
1837-
uint256 hashPrevBlock = pindex->pprev == nullptr ? uint256() : pindex->pprev->GetBlockHash();
1838-
assert(hashPrevBlock == view.GetBestBlock());
1839-
1840-
// Special case for the genesis block, skipping connection of its transactions
1841-
// (its coinbase is unspendable)
1842-
if (block.GetHash() == chainparams.GetConsensus().hashGenesisBlock) {
1843-
if (!fJustCheck)
1844-
view.SetBestBlock(pindex->GetBlockHash());
1845-
return true;
1846-
}
1847-
18481851
nBlocksTotal++;
18491852

18501853
bool fScriptChecks = true;
@@ -3498,8 +3501,9 @@ bool CChainState::AcceptBlock(const std::shared_ptr<const CBlock>& pblock, CVali
34983501
if (pindex->nChainWork < nMinimumChainWork) return true;
34993502
}
35003503

3501-
if (!CheckBlock(block, state, chainparams.GetConsensus()) ||
3502-
!ContextualCheckBlock(block, state, chainparams.GetConsensus(), pindex->pprev)) {
3504+
if (chainparams.GetConsensus().hashGenesisBlock != block.GetHash() &&
3505+
(!CheckBlock(block, state, chainparams.GetConsensus()) ||
3506+
!ContextualCheckBlock(block, state, chainparams.GetConsensus(), pindex->pprev))) {
35033507
if (state.IsInvalid() && !state.CorruptionPossible()) {
35043508
pindex->nStatus |= BLOCK_FAILED_VALID;
35053509
setDirtyBlockIndex.insert(pindex);

0 commit comments

Comments
 (0)