From b6fae706ce8a41f2c805566e076a69894d805a20 Mon Sep 17 00:00:00 2001 From: paladz <453256728@qq.com> Date: Wed, 5 Jun 2019 11:27:02 +0800 Subject: [PATCH] edit for code review --- protocol/consensus_node_manager.go | 8 ++------ protocol/state/vote_result.go | 10 ++++++++-- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/protocol/consensus_node_manager.go b/protocol/consensus_node_manager.go index 8d008fb5..99693ab1 100644 --- a/protocol/consensus_node_manager.go +++ b/protocol/consensus_node_manager.go @@ -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 diff --git a/protocol/state/vote_result.go b/protocol/state/vote_result.go index 1944fd73..129f3a35 100644 --- a/protocol/state/vote_result.go +++ b/protocol/state/vote_result.go @@ -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 } -- 2.11.0