OSDN Git Service

Cancel contract of btm inspection (#198)
[bytom/vapor.git] / protocol / consensus_node_manager.go
index 62fc3ad..66d1826 100644 (file)
@@ -1,46 +1,17 @@
 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"
-       "github.com/vapor/protocol/bc/types"
        "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 (
-       errHasNoChanceProductBlock = errors.New("the node has no chance to product a block in this round of voting")
-       errNotFoundConsensusNode   = errors.New("can not found consensus node")
-       errNotFoundBlockNode       = errors.New("can not find block node")
+       errNotFoundConsensusNode = errors.New("can not found consensus node")
+       errNotFoundBlockNode     = errors.New("can not find block node")
 )
 
-type consensusNode struct {
-       pubkey  string
-       voteNum uint64
-       order   uint64
-}
-
-type consensusNodeSlice []*consensusNode
-
-func (c consensusNodeSlice) Len() int           { return len(c) }
-func (c consensusNodeSlice) Less(i, j int) bool { return c[i].voteNum > c[j].voteNum }
-func (c consensusNodeSlice) Swap(i, j int)      { c[i], c[j] = c[j], c[i] }
-
 type consensusNodeManager struct {
        store      Store
        blockIndex *state.BlockIndex
@@ -53,8 +24,8 @@ func newConsensusNodeManager(store Store, blockIndex *state.BlockIndex) *consens
        }
 }
 
-func (c *consensusNodeManager) getConsensusNode(blockHash *bc.Hash, pubkey string) (*consensusNode, error) {
-       consensusNodeMap, err := c.getConsensusNodesByVoteResult(blockHash)
+func (c *consensusNodeManager) getConsensusNode(prevBlockHash *bc.Hash, pubkey string) (*state.ConsensusNode, error) {
+       consensusNodeMap, err := c.getConsensusNodes(prevBlockHash)
        if err != nil {
                return nil, err
        }
@@ -66,298 +37,137 @@ func (c *consensusNodeManager) getConsensusNode(blockHash *bc.Hash, pubkey strin
        return node, nil
 }
 
-func (c *consensusNodeManager) isBlocker(blockHash *bc.Hash, pubkey string) (bool, error) {
-       blockNode := c.blockIndex.GetNode(blockHash)
-       if blockNode == nil {
-               return false, errNotFoundBlockNode
-       }
-
-       consensusNode, err := c.getConsensusNode(blockHash, pubkey)
-       if err != nil && err != errNotFoundConsensusNode {
-               return false, err
-       }
-
-       if consensusNode == nil {
-               return false, nil
-       }
-
-       prevVoteRoundLastBlock, err := c.getPrevRoundVoteLastBlock(blockNode)
-       if err != nil {
-               return false, err
-       }
-
-       startTimestamp := prevVoteRoundLastBlock.Timestamp + BlockTimeInterval
-
-       begin := getLastBlockTimeInTimeRange(startTimestamp, blockNode.Timestamp, consensusNode.order)
-       end := begin + BlockNumEachNode*BlockTimeInterval
-       return blockNode.Timestamp >= begin && blockNode.Timestamp < end, nil
-}
-
-func (c *consensusNodeManager) nextLeaderTimeRange(pubkey []byte, bestBlockHash *bc.Hash) (uint64, uint64, error) {
-       bestBlockNode := c.blockIndex.GetNode(bestBlockHash)
-       if bestBlockNode == nil {
-               return 0, 0, errNotFoundBlockNode
-       }
-
-       consensusNode, err := c.getConsensusNode(bestBlockHash, hex.EncodeToString(pubkey))
-       if err != nil {
-               return 0, 0, err
-       }
-
-       prevRoundLastBlock, err := c.getPrevRoundVoteLastBlock(bestBlockNode)
+func (c *consensusNodeManager) getBlocker(prevBlockHash *bc.Hash, timeStamp uint64) (string, error) {
+       consensusNodeMap, err := c.getConsensusNodes(prevBlockHash)
        if err != nil {
-               return 0, 0, nil
+               return "", err
        }
 
-       startTime := prevRoundLastBlock.Timestamp + BlockTimeInterval
-       endTime := startTime + roundVoteBlockNums*BlockTimeInterval
-
-       nextLeaderTime, err := nextLeaderTimeHelper(startTime, endTime, uint64(time.Now().UnixNano()/1e6), consensusNode.order)
+       prevVoteRoundLastBlock, err := c.getPrevRoundLastBlock(prevBlockHash)
        if err != nil {
-               return 0, 0, err
+               return "", err
        }
 
-       return nextLeaderTime, nextLeaderTime + BlockNumEachNode*BlockTimeInterval, nil
-}
-
-func nextLeaderTimeHelper(startTime, endTime, now, nodeOrder uint64) (uint64, error) {
-       nextLeaderTimestamp := getLastBlockTimeInTimeRange(startTime, now, nodeOrder)
-       roundBlockTime := uint64(BlockNumEachNode * numOfConsensusNode * BlockTimeInterval)
-
-       if int64(now-nextLeaderTimestamp) >= BlockNumEachNode*BlockTimeInterval {
-               nextLeaderTimestamp += roundBlockTime
-               if nextLeaderTimestamp >= endTime {
-                       return 0, errHasNoChanceProductBlock
+       startTimestamp := prevVoteRoundLastBlock.Timestamp + consensus.BlockTimeInterval
+       order := getBlockerOrder(startTimestamp, timeStamp, uint64(len(consensusNodeMap)))
+       for xPub, consensusNode := range consensusNodeMap {
+               if consensusNode.Order == order {
+                       return xPub, nil
                }
        }
 
-       return nextLeaderTimestamp, nil
+       // impossible occur
+       return "", errors.New("can not find blocker by given timestamp")
 }
 
-func getLastBlockTimeInTimeRange(startTimestamp, endTimestamp, order uint64) uint64 {
+func getBlockerOrder(startTimestamp, blockTimestamp, numOfConsensusNode uint64) uint64 {
        // One round of product block time for all consensus nodes
-       roundBlockTime := uint64(BlockNumEachNode * numOfConsensusNode * BlockTimeInterval)
+       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*(BlockNumEachNode*BlockTimeInterval)
+       lastRoundStartTime := startTimestamp + (blockTimestamp-startTimestamp)/roundBlockTime*roundBlockTime
+       // Order of blocker
+       return (blockTimestamp - lastRoundStartTime) / (consensus.BlockNumEachNode * consensus.BlockTimeInterval)
 }
 
-func (c *consensusNodeManager) getPrevRoundVoteLastBlock(blockNode *state.BlockNode) (*state.BlockNode, error) {
-       prevVoteRoundLastBlockHeight := blockNode.Height/roundVoteBlockNums*roundVoteBlockNums - 1
-       lastBlockNode := blockNode.GetParent(prevVoteRoundLastBlockHeight)
-       if lastBlockNode == nil {
+func (c *consensusNodeManager) getPrevRoundLastBlock(prevBlockHash *bc.Hash) (*state.BlockNode, error) {
+       node := c.blockIndex.GetNode(prevBlockHash)
+       if node == nil {
                return nil, errNotFoundBlockNode
        }
-       return lastBlockNode, nil
+
+       for node.Height%consensus.RoundVoteBlockNums != 0 {
+               node = node.Parent
+       }
+       return node, nil
 }
 
-func (c *consensusNodeManager) getConsensusNodesByVoteResult(blockHash *bc.Hash) (map[string]*consensusNode, error) {
-       blockNode := c.blockIndex.GetNode(blockHash)
-       if blockNode == nil {
+func (c *consensusNodeManager) getConsensusNodes(prevBlockHash *bc.Hash) (map[string]*state.ConsensusNode, error) {
+       prevBlockNode := c.blockIndex.GetNode(prevBlockHash)
+       if prevBlockNode == nil {
                return nil, errNotFoundBlockNode
        }
 
-       seq := blockNode.Height / roundVoteBlockNums
-       if seq == 0 {
-               return initVoteResult(), nil
+       preSeq := state.CalcVoteSeq(prevBlockNode.Height+1) - 1
+       if bestSeq := state.CalcVoteSeq(c.blockIndex.BestNode().Height); preSeq > bestSeq {
+               preSeq = bestSeq
        }
 
-       voteResult, err := c.store.GetVoteResult(seq)
+       lastBlockNode, err := c.getPrevRoundLastBlock(prevBlockHash)
        if err != nil {
-               // fail to find vote result, try to construct
-               voteResult = &state.VoteResult{
-                       Seq:       seq,
-                       NumOfVote: make(map[string]uint64),
-                       Finalized: false,
-               }
+               return nil, err
        }
 
-       lastBlockNode, err := c.getPrevRoundVoteLastBlock(blockNode)
+       voteResult, err := c.getVoteResult(preSeq, lastBlockNode)
        if err != nil {
                return nil, err
        }
 
-       if err := c.reorganizeVoteResult(voteResult, lastBlockNode); err != 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
        }
 
-       var nodes []*consensusNode
-       for pubkey, voteNum := range voteResult.NumOfVote {
-               nodes = append(nodes, &consensusNode{
-                       pubkey:  pubkey,
-                       voteNum: voteNum,
-               })
+       if err := c.reorganizeVoteResult(voteResult, blockNode); err != nil {
+               return nil, err
        }
-       // In principle, there is no need to sort all voting nodes.
-       // if there is a performance problem, consider the optimization later.
-       // TODO not consider the same number of votes
-       sort.Sort(consensusNodeSlice(nodes))
 
-       result := make(map[string]*consensusNode)
-       for i := 0; i < numOfConsensusNode; i++ {
-               node := nodes[i]
-               node.order = uint64(i)
-               result[node.pubkey] = node
-       }
-       return result, nil
+       return voteResult, nil
 }
 
-func (c *consensusNodeManager) reorganizeVoteResult(voteResult *state.VoteResult, forkChainNode *state.BlockNode) error {
-       var mainChainNode *state.BlockNode
-       emptyHash := bc.Hash{}
-       if voteResult.LastBlockHash != emptyHash {
-               mainChainNode = c.blockIndex.GetNode(&voteResult.LastBlockHash)
-               if mainChainNode == nil {
-                       return errNotFoundBlockNode
-               }
-       }
-
+func (c *consensusNodeManager) reorganizeVoteResult(voteResult *state.VoteResult, node *state.BlockNode) error {
+       mainChainNode := c.blockIndex.GetNode(&voteResult.BlockHash)
        var attachNodes []*state.BlockNode
        var detachNodes []*state.BlockNode
-
-       for forkChainNode.Hash != mainChainNode.Hash && forkChainNode.Height >= (voteResult.Seq-1)*roundVoteBlockNums {
-               attachNodes = append([]*state.BlockNode{forkChainNode}, attachNodes...)
-               forkChainNode = forkChainNode.Parent
-
-               if mainChainNode != nil && 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
                }
        }
 
        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
                }
 
-               if err := c.detachBlock(map[uint64]*state.VoteResult{voteResult.Seq: voteResult}, block); err != nil {
+               if err := voteResult.DetachBlock(block); err != nil {
                        return err
                }
        }
 
        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
                }
 
-               if err := c.applyBlock(map[uint64]*state.VoteResult{voteResult.Seq: voteResult}, block); err != nil {
+               if err := voteResult.ApplyBlock(block); err != nil {
                        return err
                }
        }
        return nil
 }
-
-func (c *consensusNodeManager) applyBlock(voteResultMap map[uint64]*state.VoteResult, block *types.Block) (err error) {
-       voteSeq := block.Height / roundVoteBlockNums
-       voteResult := voteResultMap[voteSeq]
-
-       if voteResult == nil {
-               voteResult, err = c.store.GetVoteResult(voteSeq)
-               if err != nil && err != ErrNotFoundVoteResult {
-                       return err
-               }
-       }
-
-       if voteResult == nil {
-               voteResult = &state.VoteResult{
-                       Seq:           voteSeq,
-                       NumOfVote:     make(map[string]uint64),
-                       LastBlockHash: block.Hash(),
-               }
-       }
-
-       voteResultMap[voteSeq] = voteResult
-
-       if voteResult.LastBlockHash != block.PreviousBlockHash {
-               return errors.New("bbft append block error, the block parent hash is not equals last block hash of vote result")
-       }
-
-       for _, tx := range block.Transactions {
-               for _, input := range tx.Inputs {
-                       unVoteInput, ok := input.TypedInput.(*types.UnvoteInput)
-                       if !ok {
-                               continue
-                       }
-
-                       pubkey := hex.EncodeToString(unVoteInput.Vote)
-                       voteResult.NumOfVote[pubkey], ok = checked.SubUint64(voteResult.NumOfVote[pubkey], unVoteInput.Amount)
-                       if !ok {
-                               return errVotingOperationOverFlow
-                       }
-               }
-               for _, output := range tx.Outputs {
-                       voteOutput, ok := output.TypedOutput.(*types.VoteTxOutput)
-                       if !ok {
-                               continue
-                       }
-
-                       pubkey := hex.EncodeToString(voteOutput.Vote)
-                       voteResult.NumOfVote[pubkey], ok = checked.AddUint64(voteResult.NumOfVote[pubkey], voteOutput.Amount)
-                       if !ok {
-                               return errVotingOperationOverFlow
-                       }
-               }
-       }
-
-       voteResult.Finalized = (block.Height+1)%roundVoteBlockNums == 0
-       return nil
-}
-
-func (c *consensusNodeManager) detachBlock(voteResultMap map[uint64]*state.VoteResult, block *types.Block) error {
-       voteSeq := block.Height / roundVoteBlockNums
-       voteResult := voteResultMap[voteSeq]
-
-       if voteResult == nil {
-               voteResult, err := c.store.GetVoteResult(voteSeq)
-               if err != nil {
-                       return err
-               }
-               voteResultMap[voteSeq] = voteResult
-       }
-
-       if voteResult.LastBlockHash != block.Hash() {
-               return errors.New("bbft detach block error, the block hash is not equals last block hash of vote result")
-       }
-
-       for _, tx := range block.Transactions {
-               for _, input := range tx.Inputs {
-                       unVoteInput, ok := input.TypedInput.(*types.UnvoteInput)
-                       if !ok {
-                               continue
-                       }
-
-                       pubkey := hex.EncodeToString(unVoteInput.Vote)
-                       voteResult.NumOfVote[pubkey], ok = checked.AddUint64(voteResult.NumOfVote[pubkey], unVoteInput.Amount)
-                       if !ok {
-                               return errVotingOperationOverFlow
-                       }
-               }
-               for _, output := range tx.Outputs {
-                       voteOutput, ok := output.TypedOutput.(*types.VoteTxOutput)
-                       if !ok {
-                               continue
-                       }
-
-                       pubkey := hex.EncodeToString(voteOutput.Vote)
-                       voteResult.NumOfVote[pubkey], ok = checked.SubUint64(voteResult.NumOfVote[pubkey], voteOutput.Amount)
-                       if !ok {
-                               return errVotingOperationOverFlow
-                       }
-               }
-       }
-
-       voteResult.Finalized = false
-       return nil
-}
-
-func initVoteResult() map[string]*consensusNode {
-       voteResult := map[string]*consensusNode{}
-       for i, pubkey := range config.CommonConfig.Federation.Xpubs {
-               pubkeyStr := pubkey.String()
-               voteResult[pubkeyStr] = &consensusNode{pubkey: pubkeyStr, voteNum: 0, order: uint64(i)}
-       }
-       return voteResult
-}