OSDN Git Service

Cancel contract of btm inspection (#198)
[bytom/vapor.git] / protocol / consensus_node_manager.go
index 99693ab..66d1826 100644 (file)
@@ -1,7 +1,6 @@
 package protocol
 
 import (
-       "github.com/vapor/config"
        "github.com/vapor/consensus"
        "github.com/vapor/errors"
        "github.com/vapor/protocol/bc"
@@ -38,35 +37,36 @@ func (c *consensusNodeManager) getConsensusNode(prevBlockHash *bc.Hash, pubkey s
        return node, nil
 }
 
-func (c *consensusNodeManager) isBlocker(prevBlockHash *bc.Hash, pubKey string, timeStamp uint64) (bool, error) {
+func (c *consensusNodeManager) getBlocker(prevBlockHash *bc.Hash, timeStamp uint64) (string, error) {
        consensusNodeMap, err := c.getConsensusNodes(prevBlockHash)
        if err != nil {
-               return false, err
-       }
-
-       consensusNode := consensusNodeMap[pubKey]
-       if consensusNode == nil {
-               return false, nil
+               return "", err
        }
 
        prevVoteRoundLastBlock, err := c.getPrevRoundLastBlock(prevBlockHash)
        if err != nil {
-               return false, err
+               return "", err
        }
 
        startTimestamp := prevVoteRoundLastBlock.Timestamp + consensus.BlockTimeInterval
-       begin := getLastBlockTimeInTimeRange(startTimestamp, timeStamp, consensusNode.Order, uint64(len(consensusNodeMap)))
-       end := begin + consensus.BlockNumEachNode*consensus.BlockTimeInterval
-       return timeStamp >= begin && timeStamp < end, nil
+       order := getBlockerOrder(startTimestamp, timeStamp, uint64(len(consensusNodeMap)))
+       for xPub, consensusNode := range consensusNodeMap {
+               if consensusNode.Order == order {
+                       return xPub, nil
+               }
+       }
+
+       // impossible occur
+       return "", errors.New("can not find blocker by given timestamp")
 }
 
-func getLastBlockTimeInTimeRange(startTimestamp, endTimestamp, order, numOfConsensusNode uint64) uint64 {
+func getBlockerOrder(startTimestamp, blockTimestamp, numOfConsensusNode uint64) uint64 {
        // One round of product block time for all consensus nodes
        roundBlockTime := consensus.BlockNumEachNode * 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*(consensus.BlockNumEachNode*consensus.BlockTimeInterval)
+       lastRoundStartTime := startTimestamp + (blockTimestamp-startTimestamp)/roundBlockTime*roundBlockTime
+       // Order of blocker
+       return (blockTimestamp - lastRoundStartTime) / (consensus.BlockNumEachNode * consensus.BlockTimeInterval)
 }
 
 func (c *consensusNodeManager) getPrevRoundLastBlock(prevBlockHash *bc.Hash) (*state.BlockNode, error) {
@@ -87,15 +87,9 @@ func (c *consensusNodeManager) getConsensusNodes(prevBlockHash *bc.Hash) (map[st
                return nil, errNotFoundBlockNode
        }
 
-       seqHeight := prevBlockNode.Height + 1
-       if bestHeight := c.blockIndex.BestNode().Height; bestHeight < seqHeight && bestHeight != 0 {
-               seqHeight = bestHeight
-       }
-
-       preSeq := state.CalcVoteSeq(seqHeight) - 1
-       voteResult, err := c.store.GetVoteResult(preSeq)
-       if err != nil {
-               return nil, err
+       preSeq := state.CalcVoteSeq(prevBlockNode.Height+1) - 1
+       if bestSeq := state.CalcVoteSeq(c.blockIndex.BestNode().Height); preSeq > bestSeq {
+               preSeq = bestSeq
        }
 
        lastBlockNode, err := c.getPrevRoundLastBlock(prevBlockHash)
@@ -103,19 +97,25 @@ func (c *consensusNodeManager) getConsensusNodes(prevBlockHash *bc.Hash) (map[st
                return nil, err
        }
 
-       if err := c.reorganizeVoteResult(voteResult, lastBlockNode); err != nil {
+       voteResult, err := c.getVoteResult(preSeq, lastBlockNode)
+       if err != nil {
                return nil, err
        }
 
-       if len(voteResult.NumOfVote) == 0 {
-               return federationNodes(), nil
-       }
        return voteResult.ConsensusNodes()
 }
 
 func (c *consensusNodeManager) getBestVoteResult() (*state.VoteResult, error) {
        blockNode := c.blockIndex.BestNode()
        seq := state.CalcVoteSeq(blockNode.Height)
+       return c.getVoteResult(seq, blockNode)
+}
+
+// getVoteResult return the vote result
+// seq represent the sequence of vote
+// blockNode represent the chain in which the result of the vote is located
+// Voting results need to be adjusted according to the chain
+func (c *consensusNodeManager) getVoteResult(seq uint64, blockNode *state.BlockNode) (*state.VoteResult, error) {
        voteResult, err := c.store.GetVoteResult(seq)
        if err != nil {
                return nil, err
@@ -132,16 +132,24 @@ func (c *consensusNodeManager) reorganizeVoteResult(voteResult *state.VoteResult
        mainChainNode := c.blockIndex.GetNode(&voteResult.BlockHash)
        var attachNodes []*state.BlockNode
        var detachNodes []*state.BlockNode
-       for forkChainNode := node; mainChainNode != forkChainNode; node = node.Parent {
-               if forkChainNode.Height == mainChainNode.Height {
+       for forkChainNode := node; mainChainNode != forkChainNode; {
+               var forChainRollback, mainChainRollBack bool
+               if forChainRollback = forkChainNode.Height >= mainChainNode.Height; forChainRollback {
+                       attachNodes = append([]*state.BlockNode{forkChainNode}, attachNodes...)
+               }
+               if mainChainRollBack = forkChainNode.Height <= mainChainNode.Height; mainChainRollBack {
                        detachNodes = append(detachNodes, mainChainNode)
+               }
+               if forChainRollback {
+                       forkChainNode = forkChainNode.Parent
+               }
+               if mainChainRollBack {
                        mainChainNode = mainChainNode.Parent
                }
-               attachNodes = append([]*state.BlockNode{forkChainNode}, attachNodes...)
        }
 
        for _, node := range detachNodes {
-               block, err := c.store.GetBlock(&node.Hash)
+               block, err := c.store.GetBlock(&node.Hash, node.Height)
                if err != nil {
                        return err
                }
@@ -152,7 +160,7 @@ func (c *consensusNodeManager) reorganizeVoteResult(voteResult *state.VoteResult
        }
 
        for _, node := range attachNodes {
-               block, err := c.store.GetBlock(&node.Hash)
+               block, err := c.store.GetBlock(&node.Hash, node.Height)
                if err != nil {
                        return err
                }
@@ -163,11 +171,3 @@ func (c *consensusNodeManager) reorganizeVoteResult(voteResult *state.VoteResult
        }
        return nil
 }
-
-func federationNodes() map[string]*state.ConsensusNode {
-       voteResult := map[string]*state.ConsensusNode{}
-       for i, xpub := range config.CommonConfig.Federation.Xpubs {
-               voteResult[xpub.String()] = &state.ConsensusNode{XPub: xpub, VoteNum: 0, Order: uint64(i)}
-       }
-       return voteResult
-}