OSDN Git Service

edit for code review edit_bbft
authorpaladz <453256728@qq.com>
Wed, 5 Jun 2019 03:27:02 +0000 (11:27 +0800)
committerpaladz <453256728@qq.com>
Wed, 5 Jun 2019 03:27:02 +0000 (11:27 +0800)
protocol/consensus_node_manager.go
protocol/state/vote_result.go

index 8d008fb..99693ab 100644 (file)
@@ -92,7 +92,7 @@ func (c *consensusNodeManager) getConsensusNodes(prevBlockHash *bc.Hash) (map[st
                seqHeight = bestHeight
        }
 
-       preSeq := (seqHeight - 1) / consensus.RoundVoteBlockNums
+       preSeq := state.CalcVoteSeq(seqHeight) - 1
        voteResult, err := c.store.GetVoteResult(preSeq)
        if err != nil {
                return nil, err
@@ -115,11 +115,7 @@ func (c *consensusNodeManager) getConsensusNodes(prevBlockHash *bc.Hash) (map[st
 
 func (c *consensusNodeManager) getBestVoteResult() (*state.VoteResult, error) {
        blockNode := c.blockIndex.BestNode()
-       if blockNode.Height == 0 {
-               return c.store.GetVoteResult(0)
-       }
-
-       seq := (blockNode.Height-1)/consensus.RoundVoteBlockNums + 1
+       seq := state.CalcVoteSeq(blockNode.Height)
        voteResult, err := c.store.GetVoteResult(seq)
        if err != nil {
                return nil, err
index 1944fd7..129f3a3 100644 (file)
@@ -32,6 +32,12 @@ func (c byVote) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
 // seq 1 is the the block height 1, to block height RoundVoteBlockNums
 // seq 2 is the block height RoundVoteBlockNums + 1 to block height 2 * RoundVoteBlockNums
 // consensus node of the current round is the final result of previous round
+func CalcVoteSeq(blockHeight uint64) uint64 {
+       if blockHeight == 0 {
+               return 0
+       }
+       return (blockHeight-1)/consensus.RoundVoteBlockNums + 1
+}
 
 // VoteResult represents a snapshot of each round of DPOS voting
 // Seq indicates the sequence of current votes, which start from zero
@@ -82,7 +88,7 @@ func (v *VoteResult) ApplyBlock(block *types.Block) error {
 
        v.BlockHash = block.Hash()
        v.BlockHeight = block.Height
-       v.Seq = (block.Height-1)/consensus.RoundVoteBlockNums + 1
+       v.Seq = CalcVoteSeq(block.Height)
        return nil
 }
 
@@ -147,7 +153,7 @@ func (v *VoteResult) DetachBlock(block *types.Block) error {
 
        v.BlockHash = block.PreviousBlockHash
        v.BlockHeight = block.Height - 1
-       v.Seq = (block.Height-2)/consensus.RoundVoteBlockNums + 1
+       v.Seq = CalcVoteSeq(block.Height - 1)
        return nil
 }