OSDN Git Service

accumulate vote (#103)
[bytom/vapor.git] / protocol / block.go
index 983e6cd..8018113 100644 (file)
@@ -4,6 +4,8 @@ import (
        log "github.com/sirupsen/logrus"
 
        "github.com/vapor/errors"
+       "github.com/vapor/event"
+       "github.com/vapor/config"
        "github.com/vapor/protocol/bc"
        "github.com/vapor/protocol/bc/types"
        "github.com/vapor/protocol/state"
@@ -113,7 +115,7 @@ func (c *Chain) reorganizeChain(node *state.BlockNode) error {
        utxoView := state.NewUtxoViewpoint()
        voteResultMap := make(map[uint64]*state.VoteResult)
        irreversibleNode := c.bestIrreversibleNode
-       
+
        for _, detachNode := range detachNodes {
                b, err := c.store.GetBlock(&detachNode.Hash)
                if err != nil {
@@ -128,14 +130,16 @@ func (c *Chain) reorganizeChain(node *state.BlockNode) error {
                if err := c.store.GetTransactionsUtxo(utxoView, detachBlock.Transactions); err != nil {
                        return err
                }
+
                txStatus, err := c.GetTransactionStatus(&detachBlock.ID)
                if err != nil {
                        return err
                }
+
                if err := utxoView.DetachBlock(detachBlock, txStatus); err != nil {
                        return err
                }
-               
+
                if err := c.bbft.DetachBlock(voteResultMap, b); err != nil {
                        return err
                }
@@ -153,10 +157,12 @@ func (c *Chain) reorganizeChain(node *state.BlockNode) error {
                if err := c.store.GetTransactionsUtxo(utxoView, attachBlock.Transactions); err != nil {
                        return err
                }
+
                txStatus, err := c.GetTransactionStatus(&attachBlock.ID)
                if err != nil {
                        return err
                }
+
                if err := utxoView.ApplyBlock(attachBlock, txStatus); err != nil {
                        return err
                }
@@ -182,15 +188,16 @@ func (c *Chain) saveBlock(block *types.Block) error {
        }
 
        parent := c.index.GetNode(&block.PreviousBlockHash)
-       if err := validation.ValidateBlock(types.MapBlock(block), parent); err != nil {
+       bcBlock := types.MapBlock(block)
+       if err := validation.ValidateBlock(bcBlock, parent); err != nil {
                return errors.Sub(ErrBadBlock, err)
        }
 
-       if err := c.bbft.SignBlock(block); err != nil {
+       signature, err := c.bbft.SignBlock(block)
+       if err != nil {
                return errors.Sub(ErrBadBlock, err)
        }
 
-       bcBlock := types.MapBlock(block)
        if err := c.store.SaveBlock(block, bcBlock.TransactionStatus); err != nil {
                return err
        }
@@ -202,6 +209,13 @@ func (c *Chain) saveBlock(block *types.Block) error {
        }
 
        c.index.AddNode(node)
+
+       if len(signature) != 0 {
+               xPub := config.CommonConfig.PrivateKey().XPub()
+               if err := c.bbft.eventDispatcher.Post(event.BlockSignatureEvent{BlockHash: block.Hash(), Signature: signature, XPub: xPub}); err != nil {
+                       return err
+               }
+       }
        return nil
 }
 
@@ -261,7 +275,7 @@ func (c *Chain) processBlock(block *types.Block) (bool, error) {
        if block.Height <= c.bestIrreversibleNode.Height {
                return false, errors.New("the height of block below the height of irreversible block")
        }
-       
+
        blockHash := block.Hash()
        if c.BlockExist(&blockHash) {
                log.WithFields(log.Fields{"module": logModule, "hash": blockHash.String(), "height": block.Height}).Info("block has been processed")
@@ -293,3 +307,20 @@ func (c *Chain) processBlock(block *types.Block) (bool, error) {
        }
        return false, nil
 }
+
+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
+       }
+
+       if isIrreversible && blockHeight > c.bestIrreversibleNode.Height {
+               bestIrreversibleNode := c.index.GetNode(blockHash)
+               if err := c.store.SaveChainNodeStatus(c.bestNode, bestIrreversibleNode); err != nil {
+                       return err
+               }
+
+               c.bestIrreversibleNode = bestIrreversibleNode
+       }
+       return nil
+}