From d32cb96c47fc2238caffcf9f0e448d2d99e66f3f Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Thu, 1 Feb 2018 21:11:56 -0600 Subject: [PATCH] blockchain: Validate early votebits in header sanity. This moves the test for validating that the vote bits specified by the header are a specific value before reaching stake validation height into the checkBlockHeaderSanity function where it more naturally belongs since it is solely dependent on the header. --- blockchain/validate.go | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/blockchain/validate.go b/blockchain/validate.go index 4182a219..567caf0b 100644 --- a/blockchain/validate.go +++ b/blockchain/validate.go @@ -410,8 +410,8 @@ func checkBlockHeaderSanity(header *wire.BlockHeader, timeSource MedianTimeSourc return ruleError(ErrTimeTooNew, str) } - // The block must not contain any votes or revocations before stake - // validation begins. + // A block must not contain any votes or revocations and its vote bits + // must be 0x0001 before stake validation begins. if header.Height < stakeValidationHeight { if header.Voters > 0 { errStr := fmt.Sprintf("block at height %d commits to "+ @@ -420,6 +420,7 @@ func checkBlockHeaderSanity(header *wire.BlockHeader, timeSource MedianTimeSourc stakeValidationHeight) return ruleError(ErrInvalidEarlyStakeTx, errStr) } + if header.Revocations > 0 { errStr := fmt.Sprintf("block at height %d commits to "+ "%d revocations before stake validation height %d", @@ -427,6 +428,15 @@ func checkBlockHeaderSanity(header *wire.BlockHeader, timeSource MedianTimeSourc stakeValidationHeight) return ruleError(ErrInvalidEarlyStakeTx, errStr) } + + if header.VoteBits != earlyVoteBitsValue { + errStr := fmt.Sprintf("block at height %d commits to "+ + "invalid vote bits before stake validation "+ + "height %d (expected %x, got %x)", + header.Height, stakeValidationHeight, + earlyVoteBitsValue, header.VoteBits) + return ruleError(ErrInvalidEarlyVoteBits, errStr) + } } // A block must not contain more votes than the minimum required to @@ -658,18 +668,6 @@ func checkBlockSanity(block *dcrutil.Block, timeSource MedianTimeSource, flags B } } - // Blocks before stake validation height may only have 0x0001 as their - // VoteBits in the header. - if int64(header.Height) < chainParams.StakeValidationHeight { - if header.VoteBits != earlyVoteBitsValue { - str := fmt.Sprintf("pre stake validation height "+ - "block %v contained an invalid votebits value"+ - " (expected %v, got %v)", block.Hash(), - earlyVoteBitsValue, header.VoteBits) - return ruleError(ErrInvalidEarlyVoteBits, str) - } - } - return nil }