Skip to content

Commit

Permalink
blockchain: Max revocations in block sanity.
Browse files Browse the repository at this point in the history
This moves the check for validating that the number of revocations does
not exceed the maximum allowed to the checkBlockSanity function where it
more naturally belongs since it does not require access to any previous
chain context.

It should be noted that this check previously used math.MaxUint8 and was
rather implicitly limited by the fact the header uses a uint8 for the
field, so this makes the requirement more explicit by defining a
constant.
  • Loading branch information
davecgh committed Feb 15, 2018
1 parent c3aa531 commit 8eda558
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions blockchain/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ const (
// earlyVoteBitsValue is the only value of VoteBits allowed in a block
// header before stake validation height.
earlyVoteBitsValue = 0x0001

// maxRevocationsPerBlock is the maximum number of revocations that are
// allowed per block.
maxRevocationsPerBlock = 255
)

var (
Expand Down Expand Up @@ -642,6 +646,15 @@ func checkBlockSanity(block *dcrutil.Block, timeSource MedianTimeSource, flags B
}
}

// A block must not contain more than the maximum allowed number of
// revocations.
if totalRevocations > maxRevocationsPerBlock {
errStr := fmt.Sprintf("block contains %d revocations which "+
"exceeds the maximum allowed amount of %d",
totalRevocations, maxRevocationsPerBlock)
return ruleError(ErrTooManyRevocations, errStr)
}

// A block header must commit to the actual number of tickets purchases that
// are in the block.
if int64(header.FreshStake) != totalTickets {
Expand Down Expand Up @@ -1227,7 +1240,6 @@ func (b *BlockChain) CheckBlockStakeSanity(stakeValidationHeight int64, node *bl
// PER BLOCK
// 3. Check and make sure that we have the same number of SSRtx tx as
// we do revocations.
// 4. Check for revocation overflows.
numSSRtxTx := 0
for _, staketx := range stakeTransactions {
msgTx := staketx.MsgTx()
Expand Down Expand Up @@ -1259,15 +1271,6 @@ func (b *BlockChain) CheckBlockStakeSanity(stakeValidationHeight int64, node *bl
// 3. Check and make sure that we have the same number of SSRtx tx as
// we do revocations. Already checked in checkBlockSanity.

// 4. Check for revocation overflows. Should be impossible given the
// above check, but check anyway.
if numSSRtxTx > math.MaxUint8 {
errStr := fmt.Sprintf("Error in stake consensus: the number "+
"of SSRtx tx in block %v was %v, overflowing the "+
"maximum allowed (255)", blockHash, numSSRtxTx)
return ruleError(ErrTooManyRevocations, errStr)
}

// -------------------------------------------------------------------
// Final Checks
// -------------------------------------------------------------------
Expand Down

0 comments on commit 8eda558

Please sign in to comment.