OSDN Git Service

fix dpos nodes less than config number (#101)
authoroysheng <33340252+oysheng@users.noreply.github.com>
Fri, 31 May 2019 03:16:25 +0000 (11:16 +0800)
committerPaladz <yzhu101@uottawa.ca>
Fri, 31 May 2019 03:16:25 +0000 (11:16 +0800)
* fix dpos nodes less than config number

* optimise

* fix

protocol/bbft.go
protocol/consensus_node_manager.go

index 9d4de08..987397f 100644 (file)
@@ -9,12 +9,12 @@ import (
 
        "github.com/vapor/config"
        "github.com/vapor/crypto/ed25519"
+       "github.com/vapor/crypto/ed25519/chainkd"
        "github.com/vapor/errors"
        "github.com/vapor/event"
        "github.com/vapor/protocol/bc"
        "github.com/vapor/protocol/bc/types"
        "github.com/vapor/protocol/state"
-       "github.com/vapor/crypto/ed25519/chainkd"
 )
 
 const (
@@ -44,12 +44,17 @@ func newBbft(store Store, blockIndex *state.BlockIndex, orphanManage *OrphanMana
 }
 
 func (b *bbft) isIrreversible(block *types.Block) bool {
+       consensusNodes, err := b.consensusNodeManager.getConsensusNodesByVoteResult(&block.PreviousBlockHash)
+       if err != nil {
+               return false
+       }
+
        signNum, err := b.validateSign(block)
        if err != nil {
                return false
        }
 
-       return signNum > (NumOfConsensusNode * 2 / 3)
+       return signNum > (uint64(len(consensusNodes)) * 2 / 3)
 }
 
 // NextLeaderTime returns the start time of the specified public key as the next leader node
@@ -81,7 +86,6 @@ func (b *bbft) ProcessBlockSignature(signature []byte, xPub [64]byte, blockHeigh
                return false, err
        }
 
-
        if chainkd.XPub(xPub).Verify(blockHash.Bytes(), signature) {
                return false, errInvalidSignature
        }
index e90ee3c..51acca9 100644 (file)
@@ -24,8 +24,8 @@ const (
 )
 
 var (
-       errNotFoundConsensusNode   = errors.New("can not found consensus node")
-       errNotFoundBlockNode       = errors.New("can not find block node")
+       errNotFoundConsensusNode = errors.New("can not found consensus node")
+       errNotFoundBlockNode     = errors.New("can not find block node")
 )
 
 type consensusNode struct {
@@ -138,12 +138,12 @@ func (c *consensusNodeManager) getPrevRoundVoteLastBlock(prevBlockHash *bc.Hash)
        if prevBlockNode == nil {
                return nil, errNotFoundBlockNode
        }
-       
+
        blockHeight := prevBlockNode.Height + 1
 
        prevVoteRoundLastBlockHeight := blockHeight/roundVoteBlockNums*roundVoteBlockNums - 1
        // first round
-       if blockHeight / roundVoteBlockNums == 0 {
+       if blockHeight/roundVoteBlockNums == 0 {
                prevVoteRoundLastBlockHeight = 0
        }
 
@@ -197,7 +197,7 @@ func (c *consensusNodeManager) getConsensusNodesByVoteResult(prevBlockHash *bc.H
        sort.Sort(consensusNodeSlice(nodes))
 
        result := make(map[string]*consensusNode)
-       for i := 0; i < NumOfConsensusNode; i++ {
+       for i := 0; i < len(nodes) && i < NumOfConsensusNode; i++ {
                node := nodes[i]
                node.order = uint64(i)
                result[node.pubkey] = node
@@ -265,8 +265,8 @@ func (c *consensusNodeManager) applyBlock(voteResultMap map[uint64]*state.VoteRe
 
        if voteResult == nil {
                voteResult = &state.VoteResult{
-                       Seq:           voteSeq,
-                       NumOfVote:     make(map[string]uint64),
+                       Seq:       voteSeq,
+                       NumOfVote: make(map[string]uint64),
                }
        }