OSDN Git Service

Edit consensus (#116)
[bytom/vapor.git] / protocol / consensus_node_manager.go
index 4a3a997..77b1b6d 100644 (file)
@@ -3,9 +3,9 @@ package protocol
 import (
        "encoding/hex"
        "sort"
-       "time"
 
        "github.com/vapor/config"
+       "github.com/vapor/consensus"
        "github.com/vapor/errors"
        "github.com/vapor/math/checked"
        "github.com/vapor/protocol/bc"
@@ -13,16 +13,6 @@ import (
        "github.com/vapor/protocol/state"
 )
 
-const (
-       NumOfConsensusNode = 21
-       RoundVoteBlockNums = 1000
-
-       // BlockTimeInterval indicate product one block per 500 milliseconds
-       BlockTimeInterval = 500
-       // BlockNumEachNode indicate product three blocks per node in succession
-       BlockNumEachNode = 3
-)
-
 var (
        errNotFoundConsensusNode = errors.New("can not found consensus node")
        errNotFoundBlockNode     = errors.New("can not find block node")
@@ -65,8 +55,8 @@ func (c *consensusNodeManager) getConsensusNode(prevBlockHash *bc.Hash, pubkey s
        return node, nil
 }
 
-func (c *consensusNodeManager) isBlocker(block *types.Block, pubKey string) (bool, error) {
-       consensusNode, err := c.getConsensusNode(&block.PreviousBlockHash, pubKey)
+func (c *consensusNodeManager) isBlocker(prevBlockHash *bc.Hash, pubKey string, timeStamp uint64) (bool, error) {
+       consensusNode, err := c.getConsensusNode(prevBlockHash, pubKey)
        if err != nil && err != errNotFoundConsensusNode {
                return false, err
        }
@@ -75,57 +65,24 @@ func (c *consensusNodeManager) isBlocker(block *types.Block, pubKey string) (boo
                return false, nil
        }
 
-       prevVoteRoundLastBlock, err := c.getPrevRoundVoteLastBlock(&block.PreviousBlockHash)
+       prevVoteRoundLastBlock, err := c.getPrevRoundVoteLastBlock(prevBlockHash)
        if err != nil {
                return false, err
        }
 
-       startTimestamp := prevVoteRoundLastBlock.Timestamp + BlockTimeInterval
-
-       begin := getLastBlockTimeInTimeRange(startTimestamp, block.Timestamp, consensusNode.order)
-       end := begin + BlockNumEachNode*BlockTimeInterval
-       return block.Timestamp >= begin && block.Timestamp < end, nil
-}
-
-func (c *consensusNodeManager) nextLeaderTimeRange(pubkey []byte, prevBlockHash *bc.Hash) (uint64, uint64, error) {
-       consensusNode, err := c.getConsensusNode(prevBlockHash, hex.EncodeToString(pubkey))
-       if err != nil {
-               return 0, 0, err
-       }
-
-       prevRoundLastBlock, err := c.getPrevRoundVoteLastBlock(prevBlockHash)
-       if err != nil {
-               return 0, 0, err
-       }
-
-       startTime := prevRoundLastBlock.Timestamp + BlockTimeInterval
-
-       nextLeaderTime, err := nextLeaderTimeHelper(startTime, uint64(time.Now().UnixNano()/1e6), consensusNode.order)
-       if err != nil {
-               return 0, 0, err
-       }
-
-       return nextLeaderTime, nextLeaderTime + BlockNumEachNode*BlockTimeInterval, nil
-}
-
-func nextLeaderTimeHelper(startTime, now, nodeOrder uint64) (uint64, error) {
-       nextLeaderTimestamp := getLastBlockTimeInTimeRange(startTime, now, nodeOrder)
-       roundBlockTime := uint64(BlockNumEachNode * NumOfConsensusNode * BlockTimeInterval)
-
-       if now > nextLeaderTimestamp {
-               nextLeaderTimestamp += roundBlockTime
-       }
-
-       return nextLeaderTimestamp, nil
+       startTimestamp := prevVoteRoundLastBlock.Timestamp + consensus.BlockTimeInterval
+       begin := getLastBlockTimeInTimeRange(startTimestamp, timeStamp, consensusNode.order)
+       end := begin + consensus.BlockNumEachNode*consensus.BlockTimeInterval
+       return timeStamp >= begin && timeStamp < end, nil
 }
 
 func getLastBlockTimeInTimeRange(startTimestamp, endTimestamp, order uint64) uint64 {
        // One round of product block time for all consensus nodes
-       roundBlockTime := uint64(BlockNumEachNode * NumOfConsensusNode * BlockTimeInterval)
+       roundBlockTime := uint64(consensus.BlockNumEachNode * consensus.NumOfConsensusNode * consensus.BlockTimeInterval)
        // The start time of the last round of product block
        lastRoundStartTime := startTimestamp + (endTimestamp-startTimestamp)/roundBlockTime*roundBlockTime
        // The time of product block of the consensus in last round
-       return lastRoundStartTime + order*(BlockNumEachNode*BlockTimeInterval)
+       return lastRoundStartTime + order*(consensus.BlockNumEachNode*consensus.BlockTimeInterval)
 }
 
 func (c *consensusNodeManager) getPrevRoundVoteLastBlock(prevBlockHash *bc.Hash) (*state.BlockNode, error) {
@@ -136,9 +93,9 @@ func (c *consensusNodeManager) getPrevRoundVoteLastBlock(prevBlockHash *bc.Hash)
 
        blockHeight := prevBlockNode.Height + 1
 
-       prevVoteRoundLastBlockHeight := blockHeight/RoundVoteBlockNums*RoundVoteBlockNums - 1
+       prevVoteRoundLastBlockHeight := blockHeight/consensus.RoundVoteBlockNums*consensus.RoundVoteBlockNums - 1
        // first round
-       if blockHeight/RoundVoteBlockNums == 0 {
+       if blockHeight/consensus.RoundVoteBlockNums == 0 {
                prevVoteRoundLastBlockHeight = 0
        }
 
@@ -155,7 +112,7 @@ func (c *consensusNodeManager) getConsensusNodesByVoteResult(prevBlockHash *bc.H
                return nil, errNotFoundBlockNode
        }
 
-       seq := (prevBlockNode.Height + 1) / RoundVoteBlockNums
+       seq := (prevBlockNode.Height + 1) / consensus.RoundVoteBlockNums
        voteResult, err := c.store.GetVoteResult(seq)
        if err != nil {
                // TODO find previous round vote
@@ -181,10 +138,12 @@ func (c *consensusNodeManager) getConsensusNodesByVoteResult(prevBlockHash *bc.H
 
        var nodes []*consensusNode
        for pubkey, voteNum := range voteResult.NumOfVote {
-               nodes = append(nodes, &consensusNode{
-                       pubkey:  pubkey,
-                       voteNum: voteNum,
-               })
+               if voteNum >= consensus.MinVoteNum {
+                       nodes = append(nodes, &consensusNode{
+                               pubkey:  pubkey,
+                               voteNum: voteNum,
+                       })
+               }
        }
        // In principle, there is no need to sort all voting nodes.
        // if there is a performance problem, consider the optimization later.
@@ -192,7 +151,7 @@ func (c *consensusNodeManager) getConsensusNodesByVoteResult(prevBlockHash *bc.H
        sort.Sort(consensusNodeSlice(nodes))
 
        result := make(map[string]*consensusNode)
-       for i := 0; i < len(nodes) && i < NumOfConsensusNode; i++ {
+       for i := 0; i < len(nodes) && i < consensus.NumOfConsensusNode; i++ {
                node := nodes[i]
                node.order = uint64(i)
                result[node.pubkey] = node
@@ -286,14 +245,14 @@ func (c *consensusNodeManager) applyBlock(voteResultMap map[uint64]*state.VoteRe
                }
        }
 
-       voteResult.Finalized = (block.Height+1)%RoundVoteBlockNums == 0
+       voteResult.Finalized = (block.Height+1)%consensus.RoundVoteBlockNums == 0
        return nil
 }
 
 func (c *consensusNodeManager) getVoteResult(voteResultMap map[uint64]*state.VoteResult, blockHeight uint64) (*state.VoteResult, error) {
        var err error
        // This round of voting prepares for the next round
-       seq := blockHeight /RoundVoteBlockNums + 1
+       seq := blockHeight/consensus.RoundVoteBlockNums + 1
        voteResult := voteResultMap[seq]
        if blockHeight == 0 {
                voteResult = &state.VoteResult{
@@ -348,7 +307,7 @@ func (c *consensusNodeManager) getVoteResult(voteResultMap map[uint64]*state.Vot
 }
 
 func (c *consensusNodeManager) detachBlock(voteResultMap map[uint64]*state.VoteResult, block *types.Block) error {
-       voteSeq := block.Height / RoundVoteBlockNums
+       voteSeq := block.Height / consensus.RoundVoteBlockNums
        voteResult := voteResultMap[voteSeq]
 
        if voteResult == nil {