"github.com/vapor/account"
"github.com/vapor/config"
+ "github.com/vapor/consensus"
"github.com/vapor/event"
"github.com/vapor/proposal"
"github.com/vapor/protocol"
func (b *BlockProposer) generateBlocks() {
xpub := config.CommonConfig.PrivateKey().XPub()
xpubStr := hex.EncodeToString(xpub[:])
- ticker := time.NewTicker(time.Millisecond * 500)
+ ticker := time.NewTicker(consensus.BlockTimeInterval * time.Millisecond)
defer ticker.Stop()
for {
bestBlockHeader := b.chain.BestBlockHeader()
bestBlockHash := bestBlockHeader.Hash()
nextBlockTime := uint64(time.Now().UnixNano() / 1e6)
- if minNextBlockTime := bestBlockHeader.Timestamp + uint64(500); nextBlockTime < minNextBlockTime {
+ if minNextBlockTime := bestBlockHeader.Timestamp + consensus.BlockTimeInterval; nextBlockTime < minNextBlockTime {
nextBlockTime = minNextBlockTime
}
"sort"
"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
- MinVoteNum = 5000000
-
- // 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 false, err
}
- startTimestamp := prevVoteRoundLastBlock.Timestamp + BlockTimeInterval
+ startTimestamp := prevVoteRoundLastBlock.Timestamp + consensus.BlockTimeInterval
begin := getLastBlockTimeInTimeRange(startTimestamp, timeStamp, consensusNode.order)
- end := begin + BlockNumEachNode*BlockTimeInterval
+ 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 {
- if voteNum >= MinVoteNum {
+ if voteNum >= consensus.MinVoteNum {
nodes = append(nodes, &consensusNode{
pubkey: pubkey,
voteNum: voteNum,
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 {