OSDN Git Service

Edit consensus (#116)
authorPaladz <yzhu101@uottawa.ca>
Mon, 3 Jun 2019 05:51:24 +0000 (13:51 +0800)
committerGitHub <noreply@github.com>
Mon, 3 Jun 2019 05:51:24 +0000 (13:51 +0800)
* move the config file

* edit the mining block time

consensus/general.go
proposal/blockproposer/blockproposer.go
proposal/proposal.go
protocol/consensus_node_manager.go

index 2b513aa..910bce4 100644 (file)
@@ -22,6 +22,15 @@ const (
        //config parameter for vote
        VotePendingBlockNumber = uint64(10000)
 
        //config parameter for vote
        VotePendingBlockNumber = uint64(10000)
 
+       //DPOS parameter
+       NumOfConsensusNode = 7
+       BlockNumEachNode   = 3
+       RoundVoteBlockNums = NumOfConsensusNode * BlockNumEachNode * 100
+       MinVoteNum         = 5000000
+
+       // BlockTimeInterval indicate product one block per 500 milliseconds
+       BlockTimeInterval = 500
+
        // MaxTimeOffsetMs is the maximum number of seconds a block time is allowed to be ahead of the current time
        MaxTimeOffsetMs  = uint64(60 * 60 * 1000)
        MedianTimeBlocks = 11
        // MaxTimeOffsetMs is the maximum number of seconds a block time is allowed to be ahead of the current time
        MaxTimeOffsetMs  = uint64(60 * 60 * 1000)
        MedianTimeBlocks = 11
index be69447..afdc8d2 100644 (file)
@@ -9,6 +9,7 @@ import (
 
        "github.com/vapor/account"
        "github.com/vapor/config"
 
        "github.com/vapor/account"
        "github.com/vapor/config"
+       "github.com/vapor/consensus"
        "github.com/vapor/event"
        "github.com/vapor/proposal"
        "github.com/vapor/protocol"
        "github.com/vapor/event"
        "github.com/vapor/proposal"
        "github.com/vapor/protocol"
@@ -39,7 +40,7 @@ type BlockProposer struct {
 func (b *BlockProposer) generateBlocks() {
        xpub := config.CommonConfig.PrivateKey().XPub()
        xpubStr := hex.EncodeToString(xpub[:])
 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 {
        defer ticker.Stop()
 
        for {
@@ -52,7 +53,7 @@ func (b *BlockProposer) generateBlocks() {
                bestBlockHeader := b.chain.BestBlockHeader()
                bestBlockHash := bestBlockHeader.Hash()
                nextBlockTime := uint64(time.Now().UnixNano() / 1e6)
                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
                }
 
                        nextBlockTime = minNextBlockTime
                }
 
index e451148..6e5387b 100644 (file)
@@ -91,7 +91,7 @@ func NewBlockTemplate(c *protocol.Chain, txPool *protocol.TxPool, accountManager
                        PreviousBlockHash: preBlockHash,
                        Timestamp:         timestamp,
                        BlockCommitment:   types.BlockCommitment{},
                        PreviousBlockHash: preBlockHash,
                        Timestamp:         timestamp,
                        BlockCommitment:   types.BlockCommitment{},
-                       BlockWitness:      types.BlockWitness{Witness: make([][]byte, protocol.NumOfConsensusNode)},
+                       BlockWitness:      types.BlockWitness{Witness: make([][]byte, consensus.NumOfConsensusNode)},
                },
        }
        bcBlock := &bc.Block{BlockHeader: &bc.BlockHeader{Height: nextBlockHeight}}
                },
        }
        bcBlock := &bc.Block{BlockHeader: &bc.BlockHeader{Height: nextBlockHeight}}
index 675b680..77b1b6d 100644 (file)
@@ -5,6 +5,7 @@ import (
        "sort"
 
        "github.com/vapor/config"
        "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/errors"
        "github.com/vapor/math/checked"
        "github.com/vapor/protocol/bc"
@@ -12,17 +13,6 @@ import (
        "github.com/vapor/protocol/state"
 )
 
        "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")
 var (
        errNotFoundConsensusNode = errors.New("can not found consensus node")
        errNotFoundBlockNode     = errors.New("can not find block node")
@@ -80,19 +70,19 @@ func (c *consensusNodeManager) isBlocker(prevBlockHash *bc.Hash, pubKey string,
                return false, err
        }
 
                return false, err
        }
 
-       startTimestamp := prevVoteRoundLastBlock.Timestamp + BlockTimeInterval
+       startTimestamp := prevVoteRoundLastBlock.Timestamp + consensus.BlockTimeInterval
        begin := getLastBlockTimeInTimeRange(startTimestamp, timeStamp, consensusNode.order)
        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
        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
        // 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) {
 }
 
 func (c *consensusNodeManager) getPrevRoundVoteLastBlock(prevBlockHash *bc.Hash) (*state.BlockNode, error) {
@@ -103,9 +93,9 @@ func (c *consensusNodeManager) getPrevRoundVoteLastBlock(prevBlockHash *bc.Hash)
 
        blockHeight := prevBlockNode.Height + 1
 
 
        blockHeight := prevBlockNode.Height + 1
 
-       prevVoteRoundLastBlockHeight := blockHeight/RoundVoteBlockNums*RoundVoteBlockNums - 1
+       prevVoteRoundLastBlockHeight := blockHeight/consensus.RoundVoteBlockNums*consensus.RoundVoteBlockNums - 1
        // first round
        // first round
-       if blockHeight/RoundVoteBlockNums == 0 {
+       if blockHeight/consensus.RoundVoteBlockNums == 0 {
                prevVoteRoundLastBlockHeight = 0
        }
 
                prevVoteRoundLastBlockHeight = 0
        }
 
@@ -122,7 +112,7 @@ func (c *consensusNodeManager) getConsensusNodesByVoteResult(prevBlockHash *bc.H
                return nil, errNotFoundBlockNode
        }
 
                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
        voteResult, err := c.store.GetVoteResult(seq)
        if err != nil {
                // TODO find previous round vote
@@ -148,7 +138,7 @@ func (c *consensusNodeManager) getConsensusNodesByVoteResult(prevBlockHash *bc.H
 
        var nodes []*consensusNode
        for pubkey, voteNum := range voteResult.NumOfVote {
 
        var nodes []*consensusNode
        for pubkey, voteNum := range voteResult.NumOfVote {
-               if voteNum >= MinVoteNum {
+               if voteNum >= consensus.MinVoteNum {
                        nodes = append(nodes, &consensusNode{
                                pubkey:  pubkey,
                                voteNum: voteNum,
                        nodes = append(nodes, &consensusNode{
                                pubkey:  pubkey,
                                voteNum: voteNum,
@@ -161,7 +151,7 @@ func (c *consensusNodeManager) getConsensusNodesByVoteResult(prevBlockHash *bc.H
        sort.Sort(consensusNodeSlice(nodes))
 
        result := make(map[string]*consensusNode)
        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
                node := nodes[i]
                node.order = uint64(i)
                result[node.pubkey] = node
@@ -255,14 +245,14 @@ func (c *consensusNodeManager) applyBlock(voteResultMap map[uint64]*state.VoteRe
                }
        }
 
                }
        }
 
-       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
        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{
        voteResult := voteResultMap[seq]
        if blockHeight == 0 {
                voteResult = &state.VoteResult{
@@ -317,7 +307,7 @@ func (c *consensusNodeManager) getVoteResult(voteResultMap map[uint64]*state.Vot
 }
 
 func (c *consensusNodeManager) detachBlock(voteResultMap map[uint64]*state.VoteResult, block *types.Block) error {
 }
 
 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 {
        voteResult := voteResultMap[voteSeq]
 
        if voteResult == nil {