8 "github.com/vapor/config"
9 "github.com/vapor/errors"
10 "github.com/vapor/math/checked"
11 "github.com/vapor/protocol/bc"
12 "github.com/vapor/protocol/bc/types"
13 "github.com/vapor/protocol/state"
17 numOfConsensusNode = 21
18 roundVoteBlockNums = 1000
20 // BlockTimeInterval indicate product one block per 500 milliseconds
21 BlockTimeInterval = 500
22 // BlockNumEachNode indicate product three blocks per node in succession
27 errHasNoChanceProductBlock = errors.New("the node has no chance to product a block in this round of voting")
28 errNotFoundConsensusNode = errors.New("can not found consensus node")
29 errNotFoundBlockNode = errors.New("can not find block node")
32 type consensusNode struct {
38 type consensusNodeSlice []*consensusNode
40 func (c consensusNodeSlice) Len() int { return len(c) }
41 func (c consensusNodeSlice) Less(i, j int) bool { return c[i].voteNum > c[j].voteNum }
42 func (c consensusNodeSlice) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
44 type consensusNodeManager struct {
46 blockIndex *state.BlockIndex
49 func newConsensusNodeManager(store Store, blockIndex *state.BlockIndex) *consensusNodeManager {
50 return &consensusNodeManager{
52 blockIndex: blockIndex,
56 func (c *consensusNodeManager) getConsensusNode(blockHash *bc.Hash, pubkey string) (*consensusNode, error) {
57 consensusNodeMap, err := c.getConsensusNodesByVoteResult(blockHash)
62 node, exist := consensusNodeMap[pubkey]
64 return nil, errNotFoundConsensusNode
69 func (c *consensusNodeManager) isBlocker(blockHash *bc.Hash, pubkey string) (bool, error) {
70 blockNode := c.blockIndex.GetNode(blockHash)
72 return false, errNotFoundBlockNode
75 consensusNode, err := c.getConsensusNode(blockHash, pubkey)
76 if err != nil && err != errNotFoundConsensusNode {
80 if consensusNode == nil {
84 prevVoteRoundLastBlock, err := c.getPrevRoundVoteLastBlock(blockNode)
89 startTimestamp := prevVoteRoundLastBlock.Timestamp + BlockTimeInterval
91 begin := getLastBlockTimeInTimeRange(startTimestamp, blockNode.Timestamp, consensusNode.order)
92 end := begin + BlockNumEachNode*BlockTimeInterval
93 return blockNode.Timestamp >= begin && blockNode.Timestamp < end, nil
96 func (c *consensusNodeManager) nextLeaderTimeRange(pubkey []byte, bestBlockHash *bc.Hash) (uint64, uint64, error) {
97 bestBlockNode := c.blockIndex.GetNode(bestBlockHash)
98 if bestBlockNode == nil {
99 return 0, 0, errNotFoundBlockNode
102 consensusNode, err := c.getConsensusNode(bestBlockHash, hex.EncodeToString(pubkey))
107 prevRoundLastBlock, err := c.getPrevRoundVoteLastBlock(bestBlockNode)
112 startTime := prevRoundLastBlock.Timestamp + BlockTimeInterval
113 endTime := startTime + roundVoteBlockNums*BlockTimeInterval
115 nextLeaderTime, err := nextLeaderTimeHelper(startTime, endTime, uint64(time.Now().UnixNano()/1e6), consensusNode.order)
120 return nextLeaderTime, nextLeaderTime + BlockNumEachNode*BlockTimeInterval, nil
123 func nextLeaderTimeHelper(startTime, endTime, now, nodeOrder uint64) (uint64, error) {
124 nextLeaderTimestamp := getLastBlockTimeInTimeRange(startTime, now, nodeOrder)
125 roundBlockTime := uint64(BlockNumEachNode * numOfConsensusNode * BlockTimeInterval)
127 if int64(now-nextLeaderTimestamp) >= BlockNumEachNode*BlockTimeInterval {
128 nextLeaderTimestamp += roundBlockTime
129 if nextLeaderTimestamp >= endTime {
130 return 0, errHasNoChanceProductBlock
134 return nextLeaderTimestamp, nil
137 func getLastBlockTimeInTimeRange(startTimestamp, endTimestamp, order uint64) uint64 {
138 // One round of product block time for all consensus nodes
139 roundBlockTime := uint64(BlockNumEachNode * numOfConsensusNode * BlockTimeInterval)
140 // The start time of the last round of product block
141 lastRoundStartTime := startTimestamp + (endTimestamp-startTimestamp)/roundBlockTime*roundBlockTime
142 // The time of product block of the consensus in last round
143 return lastRoundStartTime + order*(BlockNumEachNode*BlockTimeInterval)
146 func (c *consensusNodeManager) getPrevRoundVoteLastBlock(blockNode *state.BlockNode) (*state.BlockNode, error) {
147 prevVoteRoundLastBlockHeight := blockNode.Height/roundVoteBlockNums*roundVoteBlockNums - 1
148 lastBlockNode := blockNode.GetParent(prevVoteRoundLastBlockHeight)
149 if lastBlockNode == nil {
150 return nil, errNotFoundBlockNode
152 return lastBlockNode, nil
155 func (c *consensusNodeManager) getConsensusNodesByVoteResult(blockHash *bc.Hash) (map[string]*consensusNode, error) {
156 blockNode := c.blockIndex.GetNode(blockHash)
157 if blockNode == nil {
158 return nil, errNotFoundBlockNode
161 seq := blockNode.Height / roundVoteBlockNums
163 return initVoteResult(), nil
166 voteResult, err := c.store.GetVoteResult(seq)
168 // fail to find vote result, try to construct
169 voteResult = &state.VoteResult{
171 NumOfVote: make(map[string]uint64),
176 lastBlockNode, err := c.getPrevRoundVoteLastBlock(blockNode)
181 if err := c.reorganizeVoteResult(voteResult, lastBlockNode); err != nil {
185 var nodes []*consensusNode
186 for pubkey, voteNum := range voteResult.NumOfVote {
187 nodes = append(nodes, &consensusNode{
192 // In principle, there is no need to sort all voting nodes.
193 // if there is a performance problem, consider the optimization later.
194 // TODO not consider the same number of votes
195 sort.Sort(consensusNodeSlice(nodes))
197 result := make(map[string]*consensusNode)
198 for i := 0; i < numOfConsensusNode; i++ {
200 node.order = uint64(i)
201 result[node.pubkey] = node
206 func (c *consensusNodeManager) reorganizeVoteResult(voteResult *state.VoteResult, forkChainNode *state.BlockNode) error {
207 var mainChainNode *state.BlockNode
208 emptyHash := bc.Hash{}
209 if voteResult.LastBlockHash != emptyHash {
210 mainChainNode = c.blockIndex.GetNode(&voteResult.LastBlockHash)
211 if mainChainNode == nil {
212 return errNotFoundBlockNode
216 var attachNodes []*state.BlockNode
217 var detachNodes []*state.BlockNode
219 for forkChainNode.Hash != mainChainNode.Hash && forkChainNode.Height >= (voteResult.Seq-1)*roundVoteBlockNums {
220 attachNodes = append([]*state.BlockNode{forkChainNode}, attachNodes...)
221 forkChainNode = forkChainNode.Parent
223 if mainChainNode != nil && forkChainNode.Height == mainChainNode.Height {
224 detachNodes = append(detachNodes, mainChainNode)
225 mainChainNode = mainChainNode.Parent
229 for _, node := range detachNodes {
230 block, err := c.store.GetBlock(&node.Hash)
235 if err := c.detachBlock(map[uint64]*state.VoteResult{voteResult.Seq: voteResult}, block); err != nil {
240 for _, node := range attachNodes {
241 block, err := c.store.GetBlock(&node.Hash)
246 if err := c.applyBlock(map[uint64]*state.VoteResult{voteResult.Seq: voteResult}, block); err != nil {
253 func (c *consensusNodeManager) applyBlock(voteResultMap map[uint64]*state.VoteResult, block *types.Block) (err error) {
254 voteSeq := block.Height / roundVoteBlockNums
255 voteResult := voteResultMap[voteSeq]
257 if voteResult == nil {
258 voteResult, err = c.store.GetVoteResult(voteSeq)
259 if err != nil && err != ErrNotFoundVoteResult {
264 if voteResult == nil {
265 voteResult = &state.VoteResult{
267 NumOfVote: make(map[string]uint64),
268 LastBlockHash: block.Hash(),
272 voteResultMap[voteSeq] = voteResult
274 if voteResult.LastBlockHash != block.PreviousBlockHash {
275 return errors.New("bbft append block error, the block parent hash is not equals last block hash of vote result")
278 for _, tx := range block.Transactions {
279 for _, input := range tx.Inputs {
280 unVoteInput, ok := input.TypedInput.(*types.UnvoteInput)
285 pubkey := hex.EncodeToString(unVoteInput.Vote)
286 voteResult.NumOfVote[pubkey], ok = checked.SubUint64(voteResult.NumOfVote[pubkey], unVoteInput.Amount)
288 return errVotingOperationOverFlow
291 for _, output := range tx.Outputs {
292 voteOutput, ok := output.TypedOutput.(*types.VoteTxOutput)
297 pubkey := hex.EncodeToString(voteOutput.Vote)
298 voteResult.NumOfVote[pubkey], ok = checked.AddUint64(voteResult.NumOfVote[pubkey], voteOutput.Amount)
300 return errVotingOperationOverFlow
305 voteResult.Finalized = (block.Height+1)%roundVoteBlockNums == 0
309 func (c *consensusNodeManager) detachBlock(voteResultMap map[uint64]*state.VoteResult, block *types.Block) error {
310 voteSeq := block.Height / roundVoteBlockNums
311 voteResult := voteResultMap[voteSeq]
313 if voteResult == nil {
314 voteResult, err := c.store.GetVoteResult(voteSeq)
318 voteResultMap[voteSeq] = voteResult
321 if voteResult.LastBlockHash != block.Hash() {
322 return errors.New("bbft detach block error, the block hash is not equals last block hash of vote result")
325 for _, tx := range block.Transactions {
326 for _, input := range tx.Inputs {
327 unVoteInput, ok := input.TypedInput.(*types.UnvoteInput)
332 pubkey := hex.EncodeToString(unVoteInput.Vote)
333 voteResult.NumOfVote[pubkey], ok = checked.AddUint64(voteResult.NumOfVote[pubkey], unVoteInput.Amount)
335 return errVotingOperationOverFlow
338 for _, output := range tx.Outputs {
339 voteOutput, ok := output.TypedOutput.(*types.VoteTxOutput)
344 pubkey := hex.EncodeToString(voteOutput.Vote)
345 voteResult.NumOfVote[pubkey], ok = checked.SubUint64(voteResult.NumOfVote[pubkey], voteOutput.Amount)
347 return errVotingOperationOverFlow
352 voteResult.Finalized = false
356 func initVoteResult() map[string]*consensusNode {
357 voteResult := map[string]*consensusNode{}
358 for i, pubkey := range config.CommonConfig.Federation.Xpubs {
359 pubkeyStr := pubkey.String()
360 voteResult[pubkeyStr] = &consensusNode{pubkey: pubkeyStr, voteNum: 0, order: uint64(i)}