Skip to content

Commit 374f6c9

Browse files
committed
Refactor: move CheckBlockSignature function call inside CheckBlock.
Github-Pull: #2295 Rebased-From: 47cae28
1 parent a78dfbe commit 374f6c9

File tree

4 files changed

+12
-9
lines changed

4 files changed

+12
-9
lines changed

src/blockassembler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ std::unique_ptr<CBlockTemplate> BlockAssembler::CreateNewBlock(const CScript& sc
226226
if (chainActive.Tip() != pindexPrev) return nullptr; // new block came in, move on
227227

228228
CValidationState state;
229-
if (!TestBlockValidity(state, *pblock, pindexPrev, false, false)) {
229+
if (!TestBlockValidity(state, *pblock, pindexPrev, false, false, false)) {
230230
throw std::runtime_error(
231231
strprintf("%s: TestBlockValidity failed: %s", __func__, FormatStateMessage(state)));
232232
}

src/primitives/block.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class CBlock : public CBlockHeader
9494
std::vector<unsigned char> vchBlockSig;
9595

9696
// memory only
97-
mutable bool fChecked;
97+
mutable bool fChecked{false};
9898

9999
CBlock()
100100
{

src/validation.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,7 +1376,7 @@ static bool ConnectBlock(const CBlock& block, CValidationState& state, CBlockInd
13761376
{
13771377
AssertLockHeld(cs_main);
13781378
// Check it again in case a previous version let a bad block in
1379-
if (!fAlreadyChecked && !CheckBlock(block, state, !fJustCheck, !fJustCheck)) {
1379+
if (!fAlreadyChecked && !CheckBlock(block, state, !fJustCheck, !fJustCheck, !fJustCheck)) {
13801380
if (state.CorruptionPossible()) {
13811381
// We don't write down blocks to disk if they may have been
13821382
// corrupted, so this should be impossible unless we're having hardware
@@ -2776,6 +2776,12 @@ bool CheckBlock(const CBlock& block, CValidationState& state, bool fCheckPOW, bo
27762776
return state.DoS(100, error("%s : out-of-bounds SigOpCount", __func__),
27772777
REJECT_INVALID, "bad-blk-sigops", true);
27782778

2779+
// Check PoS signature.
2780+
if (fCheckSig && !CheckBlockSignature(block)) {
2781+
return state.DoS(100, error("%s : bad proof-of-stake block signature", __func__),
2782+
REJECT_INVALID, "bad-PoS-sig", true);
2783+
}
2784+
27792785
if (fCheckPOW && fCheckMerkleRoot && fCheckSig)
27802786
block.fChecked = true;
27812787

@@ -3268,9 +3274,6 @@ bool ProcessNewBlock(CValidationState& state, CNode* pfrom, const std::shared_pt
32683274
// check block
32693275
bool checked = CheckBlock(*pblock, state);
32703276

3271-
if (!CheckBlockSignature(*pblock))
3272-
return error("%s : bad proof-of-stake block signature", __func__);
3273-
32743277
if (pblock->GetHash() != consensus.hashGenesisBlock && pfrom != NULL) {
32753278
//if we get this far, check if the prev block is our prev block, if not then request sync and return false
32763279
BlockMap::iterator mi = mapBlockIndex.find(pblock->hashPrevBlock);
@@ -3315,7 +3318,7 @@ bool ProcessNewBlock(CValidationState& state, CNode* pfrom, const std::shared_pt
33153318
return true;
33163319
}
33173320

3318-
bool TestBlockValidity(CValidationState& state, const CBlock& block, CBlockIndex* const pindexPrev, bool fCheckPOW, bool fCheckMerkleRoot)
3321+
bool TestBlockValidity(CValidationState& state, const CBlock& block, CBlockIndex* const pindexPrev, bool fCheckPOW, bool fCheckMerkleRoot, bool fCheckBlockSig)
33193322
{
33203323
AssertLockHeld(cs_main);
33213324
assert(pindexPrev);
@@ -3332,7 +3335,7 @@ bool TestBlockValidity(CValidationState& state, const CBlock& block, CBlockIndex
33323335
// NOTE: CheckBlockHeader is called by CheckBlock
33333336
if (!ContextualCheckBlockHeader(block, state, pindexPrev))
33343337
return error("%s: ContextualCheckBlockHeader failed: %s", __func__, FormatStateMessage(state));
3335-
if (!CheckBlock(block, state, fCheckPOW, fCheckMerkleRoot))
3338+
if (!CheckBlock(block, state, fCheckPOW, fCheckMerkleRoot, fCheckBlockSig))
33363339
return error("%s: CheckBlock failed: %s", __func__, FormatStateMessage(state));
33373340
if (!ContextualCheckBlock(block, state, pindexPrev))
33383341
return error("%s: ContextualCheckBlock failed: %s", __func__, FormatStateMessage(state));

src/validation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ bool ContextualCheckBlockHeader(const CBlockHeader& block, CValidationState& sta
332332
bool ContextualCheckBlock(const CBlock& block, CValidationState& state, CBlockIndex* pindexPrev);
333333

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

337337
/** Store block on disk. If dbp is provided, the file is known to already reside on disk */
338338
bool AcceptBlock(const CBlock& block, CValidationState& state, CBlockIndex** pindex, CDiskBlockPos* dbp = NULL, bool fAlreadyCheckedBlock = false);

0 commit comments

Comments
 (0)