OSDN Git Service

accumulate vote (#103)
authormuscle_boy <shenao.78@163.com>
Fri, 31 May 2019 06:39:12 +0000 (14:39 +0800)
committerPaladz <yzhu101@uottawa.ca>
Fri, 31 May 2019 06:39:12 +0000 (14:39 +0800)
netsync/consensusmgr/block_fetcher_test.go
netsync/consensusmgr/consensus_msg.go
netsync/consensusmgr/consensus_msg_test.go
netsync/consensusmgr/handle.go
protocol/block.go
protocol/consensus_node_manager.go

index dd7ec0c..b02b802 100644 (file)
@@ -45,7 +45,7 @@ func (c *chain) ProcessBlock(block *types.Block) (bool, error) {
        return false, nil
 }
 
-func (c *chain) ProcessBlockSignature(signature, pubkey []byte, blockHeight uint64, blockHash *bc.Hash) error {
+func (c *chain) ProcessBlockSignature(signature []byte, pubkey [64]byte, blockHeight uint64, blockHash *bc.Hash) error {
        return nil
 }
 
index a6bbb7b..40f74c7 100644 (file)
@@ -48,11 +48,11 @@ type BlockSignatureMsg struct {
        BlockHash [32]byte
        Height    uint64
        Signature []byte
-       PubKey    []byte
+       PubKey    [64]byte
 }
 
 //NewBlockSignatureMsg create new block signature msg.
-func NewBlockSignatureMsg(blockHash bc.Hash, height uint64, signature []byte, pubKey []byte) ConsensusMessage {
+func NewBlockSignatureMsg(blockHash bc.Hash, height uint64, signature []byte, pubKey [64]byte) ConsensusMessage {
        hash := blockHash.Byte32()
        return &BlockSignatureMsg{BlockHash: hash, Height: height, Signature: signature, PubKey: pubKey}
 }
index 254fa8d..a535fd4 100644 (file)
@@ -26,7 +26,7 @@ func TestDecodeMessage(t *testing.T) {
                        msg: &BlockSignatureMsg{
                                BlockHash: [32]byte{0x01},
                                Signature: []byte{0x00},
-                               PubKey:    []byte{0x01},
+                               PubKey:    [64]byte{0x01},
                        },
                        msgType: blockSignatureByte,
                },
@@ -57,7 +57,7 @@ func TestBlockSignBroadcastMsg(t *testing.T) {
                BlockHash: [32]byte{0x01},
                Height:    100,
                Signature: []byte{0x00},
-               PubKey:    []byte{0x01},
+               PubKey:    [64]byte{0x01},
        }
        signatureBroadcastMsg := NewBroadcastMsg(NewBlockSignatureMsg(bc.NewHash(blockSignMsg.BlockHash), blockSignMsg.Height, blockSignMsg.Signature, blockSignMsg.PubKey), consensusChannel)
 
@@ -141,14 +141,14 @@ func TestBlockSignatureMsg(t *testing.T) {
                BlockHash: [32]byte{0x01},
                Height:    100,
                Signature: []byte{0x00},
-               PubKey:    []byte{0x01},
+               PubKey:    [64]byte{0x01},
        }
        gotMsg := NewBlockSignatureMsg(bc.NewHash(msg.BlockHash), msg.Height, msg.Signature, msg.PubKey)
 
        if !reflect.DeepEqual(gotMsg, msg) {
                t.Fatalf("test block signature message err. got:%s\n want:%s", spew.Sdump(gotMsg), spew.Sdump(msg))
        }
-       wantString := "{block_hash: 0100000000000000000000000000000000000000000000000000000000000000,block_height:100,signature:00,pubkey:01}"
+       wantString := "{block_hash: 0100000000000000000000000000000000000000000000000000000000000000,block_height:100,signature:00,pubkey:01000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000}"
        if gotMsg.String() != wantString {
                t.Fatalf("test block signature message err. got string:%s\n want string:%s", gotMsg.String(), wantString)
        }
index 7c09f70..2d380f3 100644 (file)
@@ -24,7 +24,7 @@ type Chain interface {
        BestBlockHeight() uint64
        GetHeaderByHash(*bc.Hash) (*types.BlockHeader, error)
        ProcessBlock(*types.Block) (bool, error)
-       ProcessBlockSignature(signature, pubkey []byte, blockHeight uint64, blockHash *bc.Hash) error
+       ProcessBlockSignature(signature []byte, pubkey [64]byte, blockHeight uint64, blockHash *bc.Hash) error
 }
 
 type blockMsg struct {
@@ -167,7 +167,7 @@ func (m *Manager) blockSignatureMsgBroadcastLoop() {
                                return
                        }
 
-                       blockSignatureMsg := NewBroadcastMsg(NewBlockSignatureMsg(ev.BlockHash, blockHeader.Height, ev.Signature, ev.XPub[:]), consensusChannel)
+                       blockSignatureMsg := NewBroadcastMsg(NewBlockSignatureMsg(ev.BlockHash, blockHeader.Height, ev.Signature, ev.XPub), consensusChannel)
                        if err := m.peers.BroadcastMsg(blockSignatureMsg); err != nil {
                                logrus.WithFields(logrus.Fields{"module": logModule, "err": err}).Error("failed on broadcast BlockSignBroadcastMsg.")
                                return
index 1b1b304..8018113 100644 (file)
@@ -308,12 +308,7 @@ func (c *Chain) processBlock(block *types.Block) (bool, error) {
        return false, nil
 }
 
-func (c *Chain) ProcessBlockSignature(signature, pubKey []byte, blockHeight uint64, blockHash *bc.Hash) error {
-       var xPub [64]byte
-       for i := 0; i < 64; i++ {
-               xPub[i] = pubKey[i]
-       }
-       
+func (c *Chain) ProcessBlockSignature(signature []byte, xPub [64]byte, blockHeight uint64, blockHash *bc.Hash) error { 
        isIrreversible, err := c.bbft.ProcessBlockSignature(signature, xPub, blockHeight, blockHash)
        if err != nil {
                return err
index 51acca9..8f0cb7a 100644 (file)
@@ -254,24 +254,11 @@ func (c *consensusNodeManager) reorganizeVoteResult(voteResult *state.VoteResult
 
 func (c *consensusNodeManager) applyBlock(voteResultMap map[uint64]*state.VoteResult, block *types.Block) (err error) {
        voteSeq := block.Height / roundVoteBlockNums
-       voteResult := voteResultMap[voteSeq]
-
-       if voteResult == nil {
-               voteResult, err = c.store.GetVoteResult(voteSeq)
-               if err != nil && err != ErrNotFoundVoteResult {
-                       return err
-               }
-       }
-
-       if voteResult == nil {
-               voteResult = &state.VoteResult{
-                       Seq:       voteSeq,
-                       NumOfVote: make(map[string]uint64),
-               }
+       voteResult, err := c.getVoteResult(voteResultMap, voteSeq)
+       if err != nil {
+               return err
        }
 
-       voteResultMap[voteSeq] = voteResult
-
        emptyHash := bc.Hash{}
        if voteResult.LastBlockHash != emptyHash && voteResult.LastBlockHash != block.PreviousBlockHash {
                return errors.New("bbft append block error, the block parent hash is not equals last block hash of vote result")
@@ -308,6 +295,42 @@ func (c *consensusNodeManager) applyBlock(voteResultMap map[uint64]*state.VoteRe
        return nil
 }
 
+func (c *consensusNodeManager) getVoteResult(voteResultMap map[uint64]*state.VoteResult, seq uint64) (*state.VoteResult, error) {
+       var err error
+       voteResult := voteResultMap[seq]
+       if voteResult == nil {
+               prevVoteResult := voteResultMap[seq - 1]
+               voteResult = &state.VoteResult {
+                       Seq: seq,
+                       NumOfVote: prevVoteResult.NumOfVote,
+                       Finalized: false,
+               }
+       }
+
+       if voteResult == nil {
+               voteResult, err = c.store.GetVoteResult(seq)
+               if err != nil && err != ErrNotFoundVoteResult {
+                       return nil, err
+               }
+       }
+
+       if voteResult == nil {
+               voteResult, err := c.store.GetVoteResult(seq - 1)
+               if err != nil && err != ErrNotFoundVoteResult {
+                       return nil, err
+               }
+               // previous round voting must have finalized
+               if !voteResult.Finalized {
+                       return nil, errors.New("previous round voting has not finalized")
+               }
+
+               voteResult.Finalized = false
+               voteResult.LastBlockHash = bc.Hash{}
+       }
+       voteResultMap[seq] = voteResult
+       return voteResult, nil
+}
+
 func (c *consensusNodeManager) detachBlock(voteResultMap map[uint64]*state.VoteResult, block *types.Block) error {
        voteSeq := block.Height / roundVoteBlockNums
        voteResult := voteResultMap[voteSeq]