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/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")
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
}
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) {
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
}
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
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.
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
}
}
- 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{
}
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 {