blockchain: Improve readability of parent approval.

This introduces two new functions to the blockchain package named
headerApprovesParent and voteBitsApproveParent and updates all call
sites in the package in order to improve the readability and more
clearly describe the intention of the code.
This commit is contained in:
Dave Collins 2018-02-02 18:29:33 -06:00
parent 53b592bcf7
commit dd5c048fd8
No known key found for this signature in database
GPG Key ID: B8904D9D9C93D1F2
5 changed files with 23 additions and 30 deletions

View File

@ -1169,10 +1169,8 @@ func countSpentOutputs(block *dcrutil.Block, parent *dcrutil.Block) int {
// We need to skip the regular tx tree if it's not valid.
// We also exclude the coinbase transaction since it can't
// spend anything.
regularTxTreeValid := dcrutil.IsFlagSet16(block.MsgBlock().Header.VoteBits,
dcrutil.BlockValid)
var numSpent int
if regularTxTreeValid {
if headerApprovesParent(&block.MsgBlock().Header) {
for _, tx := range parent.Transactions()[1:] {
numSpent += len(tx.MsgTx().TxIn)
}
@ -1193,10 +1191,7 @@ func countSpentOutputs(block *dcrutil.Block, parent *dcrutil.Block) int {
// adding the block.
func countNumberOfTransactions(block, parent *dcrutil.Block) uint64 {
var numTxns uint64
regularTxTreeValid := dcrutil.IsFlagSet16(block.MsgBlock().Header.VoteBits,
dcrutil.BlockValid)
if regularTxTreeValid {
if headerApprovesParent(&block.MsgBlock().Header) {
numTxns += uint64(len(parent.Transactions()))
}
numTxns += uint64(len(block.STransactions()))
@ -1696,9 +1691,7 @@ func (b *BlockChain) connectBestChain(node *blockNode, block, parent *dcrutil.Bl
}
validateStr := "validating"
txTreeRegularValid := dcrutil.IsFlagSet16(node.voteBits,
dcrutil.BlockValid)
if !txTreeRegularValid {
if !voteBitsApproveParent(node.voteBits) {
validateStr = "invalidating"
}

View File

@ -518,9 +518,7 @@ func dbFetchSpendJournalEntry(dbTx database.Tx, block *dcrutil.Block, parent *dc
serialized := spendBucket.Get(block.Hash()[:])
var blockTxns []*wire.MsgTx
regularTxTreeValid := dcrutil.IsFlagSet16(block.MsgBlock().Header.VoteBits,
dcrutil.BlockValid)
if regularTxTreeValid {
if headerApprovesParent(&block.MsgBlock().Header) {
blockTxns = append(blockTxns, parent.MsgBlock().Transactions[1:]...)
}
blockTxns = append(blockTxns, block.MsgBlock().STransactions...)

View File

@ -328,10 +328,7 @@ func CoinbasePaysTax(subsidyCache *SubsidyCache, tx *dcrutil.Tx, height int64, v
// network, or the function might panic.
func CalculateAddedSubsidy(block, parent *dcrutil.Block) int64 {
var subsidy int64
regularTxTreeValid := dcrutil.IsFlagSet16(block.MsgBlock().Header.VoteBits,
dcrutil.BlockValid)
if regularTxTreeValid {
if headerApprovesParent(&block.MsgBlock().Header) {
subsidy += parent.MsgBlock().Transactions[0].TxIn[0].ValueIn
}

View File

@ -457,8 +457,7 @@ func (view *UtxoViewpoint) connectTransaction(tx *dcrutil.Tx, blockHeight int64,
// In addition, when the 'stxos' argument is not nil, it will be updated to
// append an entry for each spent txout.
func (b *BlockChain) connectTransactions(view *UtxoViewpoint, block *dcrutil.Block, parent *dcrutil.Block, stxos *[]spentTxOut) error {
regularTxTreeValid := dcrutil.IsFlagSet16(block.MsgBlock().Header.VoteBits,
dcrutil.BlockValid)
regularTxTreeValid := headerApprovesParent(&block.MsgBlock().Header)
thisNodeStakeViewpoint := ViewpointPrevInvalidStake
if regularTxTreeValid {
thisNodeStakeViewpoint = ViewpointPrevValidStake
@ -470,9 +469,6 @@ func (b *BlockChain) connectTransactions(view *UtxoViewpoint, block *dcrutil.Blo
if err != nil {
return err
}
mBlock := block.MsgBlock()
votebits := mBlock.Header.VoteBits
regularTxTreeValid := dcrutil.IsFlagSet16(votebits, dcrutil.BlockValid)
if regularTxTreeValid {
for i, tx := range parent.Transactions() {
err := view.connectTransaction(tx, parent.Height(), uint32(i),
@ -521,8 +517,7 @@ func (b *BlockChain) disconnectTransactions(view *UtxoViewpoint, block *dcrutil.
// Loop backwards through all transactions so everything is unspent in
// reverse order. This is necessary since transactions later in a block
// can spend from previous ones.
regularTxTreeValid := dcrutil.IsFlagSet16(block.MsgBlock().Header.VoteBits,
dcrutil.BlockValid)
regularTxTreeValid := headerApprovesParent(&block.MsgBlock().Header)
thisNodeStakeViewpoint := ViewpointPrevInvalidStake
if regularTxTreeValid {
thisNodeStakeViewpoint = ViewpointPrevValidStake

View File

@ -58,6 +58,19 @@ var (
zeroHash = &chainhash.Hash{}
)
// voteBitsApproveParent returns whether or not the passed vote bits indicate
// the regular transaction tree of the parent block should be considered valid.
func voteBitsApproveParent(voteBits uint16) bool {
return dcrutil.IsFlagSet16(voteBits, dcrutil.BlockValid)
}
// approvesParent returns whether or not the vote bits in the passed header
// indicate the regular transaction tree of the parent block should be
// considered valid.
func headerApprovesParent(header *wire.BlockHeader) bool {
return voteBitsApproveParent(header.VoteBits)
}
// isNullOutpoint determines whether or not a previous transaction output point
// is set.
func isNullOutpoint(outpoint *wire.OutPoint) bool {
@ -1030,8 +1043,7 @@ func (b *BlockChain) CheckBlockStakeSanity(stakeValidationHeight int64, node *bl
ticketsPerBlock := int(b.chainParams.TicketsPerBlock)
txTreeRegularValid := dcrutil.IsFlagSet16(msgBlock.Header.VoteBits,
dcrutil.BlockValid)
txTreeRegularValid := headerApprovesParent(&msgBlock.Header)
parentStakeNode, err := b.fetchStakeNode(node.parent)
if err != nil {
@ -1113,8 +1125,7 @@ func (b *BlockChain) CheckBlockStakeSanity(stakeValidationHeight int64, node *bl
numSSGenTx++
// Check and store the vote for TxTreeRegular.
ssGenVoteBits := stake.SSGenVoteBits(msgTx)
if dcrutil.IsFlagSet16(ssGenVoteBits, dcrutil.BlockValid) {
if voteBitsApproveParent(stake.SSGenVoteBits(msgTx)) {
voteYea++
} else {
voteNay++
@ -2451,8 +2462,7 @@ func (b *BlockChain) checkConnectBlock(node *blockNode, block, parent *dcrutil.B
// signature operations in each of the input transaction public key
// scripts.
// Do this for all TxTrees.
regularTxTreeValid := dcrutil.IsFlagSet16(node.voteBits,
dcrutil.BlockValid)
regularTxTreeValid := voteBitsApproveParent(node.voteBits)
thisNodeStakeViewpoint := ViewpointPrevInvalidStake
thisNodeRegularViewpoint := ViewpointPrevInvalidRegular
if regularTxTreeValid {