package protocol
import (
- "github.com/vapor/config"
"github.com/vapor/consensus"
"github.com/vapor/errors"
"github.com/vapor/protocol/bc"
+ "github.com/vapor/protocol/bc/types"
"github.com/vapor/protocol/state"
)
errNotFoundBlockNode = errors.New("can not find block node")
)
-type consensusNodeManager struct {
- store Store
- blockIndex *state.BlockIndex
-}
-
-func newConsensusNodeManager(store Store, blockIndex *state.BlockIndex) *consensusNodeManager {
- return &consensusNodeManager{
- store: store,
- blockIndex: blockIndex,
- }
-}
-
-func (c *consensusNodeManager) getConsensusNode(prevBlockHash *bc.Hash, pubkey string) (*state.ConsensusNode, error) {
+func (c *Chain) getConsensusNode(prevBlockHash *bc.Hash, pubkey string) (*state.ConsensusNode, error) {
consensusNodeMap, err := c.getConsensusNodes(prevBlockHash)
if err != nil {
return nil, err
return node, nil
}
-func (c *consensusNodeManager) isBlocker(prevBlockHash *bc.Hash, pubKey string, timeStamp uint64) (bool, error) {
+// GetBlocker return blocker by specified timestamp
+func (c *Chain) 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) {
- node := c.blockIndex.GetNode(prevBlockHash)
- if node == nil {
+func (c *Chain) getPrevRoundLastBlock(prevBlockHash *bc.Hash) (*types.BlockHeader, error) {
+ blockHeader, err := c.store.GetBlockHeader(prevBlockHash)
+ if err != nil {
return nil, errNotFoundBlockNode
}
- for node.Height%consensus.RoundVoteBlockNums != 0 {
- node = node.Parent
+ for blockHeader.Height%consensus.RoundVoteBlockNums != 0 {
+ blockHeader, err = c.store.GetBlockHeader(&blockHeader.PreviousBlockHash)
+ if err != nil {
+ return nil, err
+ }
}
- return node, nil
+ return blockHeader, nil
}
-func (c *consensusNodeManager) getConsensusNodes(prevBlockHash *bc.Hash) (map[string]*state.ConsensusNode, error) {
- prevBlockNode := c.blockIndex.GetNode(prevBlockHash)
- if prevBlockNode == nil {
+func (c *Chain) getConsensusNodes(prevBlockHash *bc.Hash) (map[string]*state.ConsensusNode, error) {
+ prevBlockHeader, err := c.store.GetBlockHeader(prevBlockHash)
+ if err != nil {
return nil, errNotFoundBlockNode
}
- preSeq := state.CalcVoteSeq(prevBlockNode.Height+1) - 1
- if bestSeq := state.CalcVoteSeq(c.blockIndex.BestNode().Height); preSeq > bestSeq {
+ bestBlockHeader := c.bestBlockHeader
+ preSeq := state.CalcVoteSeq(prevBlockHeader.Height+1) - 1
+ if bestSeq := state.CalcVoteSeq(bestBlockHeader.Height); preSeq > bestSeq {
preSeq = bestSeq
}
- voteResult, err := c.store.GetVoteResult(preSeq)
+ lastBlockHeader, err := c.getPrevRoundLastBlock(prevBlockHash)
if err != nil {
return nil, err
}
- lastBlockNode, err := c.getPrevRoundLastBlock(prevBlockHash)
+ voteResult, err := c.getVoteResult(preSeq, lastBlockHeader)
if err != nil {
return nil, err
}
- if err := c.reorganizeVoteResult(voteResult, lastBlockNode); err != nil {
- return nil, err
- }
-
- result, err := voteResult.ConsensusNodes()
- if err != nil {
- return nil, err
- }
+ return voteResult.ConsensusNodes()
+}
- if len(result) != 0 {
- return result, nil
- }
- return federationNodes(), nil
+func (c *Chain) getBestVoteResult() (*state.VoteResult, error) {
+ bestBlockHeader := c.bestBlockHeader
+ seq := state.CalcVoteSeq(bestBlockHeader.Height)
+ return c.getVoteResult(seq, bestBlockHeader)
}
-func (c *consensusNodeManager) getBestVoteResult() (*state.VoteResult, error) {
- blockNode := c.blockIndex.BestNode()
- seq := state.CalcVoteSeq(blockNode.Height)
+// getVoteResult return the vote result
+// seq represent the sequence of vote
+// blockHeader represent the chain in which the result of the vote is located
+// Voting results need to be adjusted according to the chain
+func (c *Chain) getVoteResult(seq uint64, blockHeader *types.BlockHeader) (*state.VoteResult, error) {
voteResult, err := c.store.GetVoteResult(seq)
if err != nil {
return nil, err
}
- if err := c.reorganizeVoteResult(voteResult, blockNode); err != nil {
+ if err := c.reorganizeVoteResult(voteResult, blockHeader); err != nil {
return nil, err
}
return voteResult, nil
}
-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 := node; mainChainNode != forkChainNode; forkChainNode = forkChainNode.Parent {
- if forkChainNode.Height == mainChainNode.Height {
- detachNodes = append(detachNodes, mainChainNode)
- mainChainNode = mainChainNode.Parent
- }
- attachNodes = append([]*state.BlockNode{forkChainNode}, attachNodes...)
+func (c *Chain) reorganizeVoteResult(voteResult *state.VoteResult, blockHeader *types.BlockHeader) error {
+ mainChainBlockHeader, err := c.store.GetBlockHeader(&voteResult.BlockHash)
+ if err != nil {
+ return err
+ }
+
+ attachBlockHeaders, detachBlockHeaders, err := c.calcReorganizeChain(blockHeader, mainChainBlockHeader)
+ if err != nil {
+ return err
}
- for _, node := range detachNodes {
- block, err := c.store.GetBlock(&node.Hash)
+ for _, bh := range detachBlockHeaders {
+ blockHash := bh.Hash()
+ block, err := c.store.GetBlock(&blockHash)
if err != nil {
return err
}
}
}
- for _, node := range attachNodes {
- block, err := c.store.GetBlock(&node.Hash)
+ for _, bh := range attachBlockHeaders {
+ blockHash := bh.Hash()
+ block, err := c.store.GetBlock(&blockHash)
if err != nil {
return err
}
}
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
-}