OSDN Git Service

tmp save (#123)
[bytom/vapor.git] / protocol / block.go
index a89047c..e3f0f83 100644 (file)
@@ -3,6 +3,7 @@ package protocol
 import (
        log "github.com/sirupsen/logrus"
 
+       "github.com/vapor/config"
        "github.com/vapor/errors"
        "github.com/vapor/event"
        "github.com/vapor/protocol/bc"
@@ -90,12 +91,12 @@ func (c *Chain) connectBlock(block *types.Block) (err error) {
        }
 
        voteResultMap := make(map[uint64]*state.VoteResult)
-       if err := c.bbft.ApplyBlock(voteResultMap, block); err != nil {
+       if err := c.consensusNodeManager.applyBlock(voteResultMap, block); err != nil {
                return err
        }
 
        node := c.index.GetNode(&bcBlock.ID)
-       if c.bbft.isIrreversible(block) && block.Height > irreversibleNode.Height {
+       if c.isIrreversible(node) && block.Height > irreversibleNode.Height {
                irreversibleNode = node
        }
 
@@ -114,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 {
@@ -129,15 +130,17 @@ 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 {
+
+               if err := c.consensusNodeManager.detachBlock(voteResultMap, b); err != nil {
                        return err
                }
 
@@ -154,19 +157,21 @@ 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
                }
 
-               if err := c.bbft.ApplyBlock(voteResultMap, b); err != nil {
+               if err := c.consensusNodeManager.applyBlock(voteResultMap, b); err != nil {
                        return err
                }
 
-               if c.bbft.isIrreversible(b) && b.Height > irreversibleNode.Height {
+               if c.isIrreversible(attachNode) && b.Height > irreversibleNode.Height {
                        irreversibleNode = attachNode
                }
 
@@ -178,27 +183,21 @@ func (c *Chain) reorganizeChain(node *state.BlockNode) error {
 
 // SaveBlock will validate and save block into storage
 func (c *Chain) saveBlock(block *types.Block) error {
-       if err := c.bbft.ValidateBlock(block); err != nil {
+       if _, err := c.validateSign(block); err != nil {
                return errors.Sub(ErrBadBlock, err)
        }
 
        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)
        }
 
-       signature, err := c.bbft.SignBlock(block)
+       signature, err := c.SignBlock(block)
        if err != nil {
                return errors.Sub(ErrBadBlock, err)
        }
 
-       if len(signature) != 0 {
-               if err := c.txPool.eventDispatcher.Post(event.BlockSignatureEvent{BlockHash: block.Hash(), Signature: signature}); err != nil {
-                       return err
-               }
-       }
-
-       bcBlock := types.MapBlock(block)
        if err := c.store.SaveBlock(block, bcBlock.TransactionStatus); err != nil {
                return err
        }
@@ -210,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.eventDispatcher.Post(event.BlockSignatureEvent{BlockHash: block.Hash(), Signature: signature, XPub: xPub}); err != nil {
+                       return err
+               }
+       }
        return nil
 }
 
@@ -269,15 +275,14 @@ 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")
                return c.orphanManage.BlockExist(&blockHash), nil
        }
 
-       parent := c.index.GetNode(&block.PreviousBlockHash)
-       if parent == nil {
+       if parent := c.index.GetNode(&block.PreviousBlockHash); parent == nil {
                c.orphanManage.Add(block)
                return true, nil
        }
@@ -301,20 +306,3 @@ 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 {
-       isBestIrreversible, err := c.bbft.ProcessBlockSignature(signature, pubkey, blockHeight, blockHash)
-       if err != nil {
-               return err
-       }
-
-       if isBestIrreversible {
-               bestIrreversibleNode := c.index.GetNode(blockHash)
-               if err := c.store.SaveChainNodeStatus(c.bestNode, bestIrreversibleNode); err != nil {
-                       return err
-               }
-
-               c.bestIrreversibleNode = bestIrreversibleNode
-       }
-       return nil
-}