OSDN Git Service

Merge branch 'v0.1' into edit
[bytom/vapor.git] / protocol / block.go
index cfb9417..f47987c 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"
@@ -187,7 +188,8 @@ 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)
        }
 
@@ -196,13 +198,6 @@ func (c *Chain) saveBlock(block *types.Block) error {
                return errors.Sub(ErrBadBlock, err)
        }
 
-       if len(signature) != 0 {
-               if err := c.bbft.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
        }
@@ -214,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
 }
 
@@ -270,18 +272,19 @@ func (c *Chain) blockProcesser() {
 
 // ProcessBlock is the entry for handle block insert
 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")
-       }
-
+       //skip if the block has been processed before
        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 {
+       //return error if the block height is lower than the irreversible block hegith
+       if block.Height <= c.IrreversibleBlockHeight() {
+               return false, errors.New("the height of block below the height of irreversible block")
+       }
+
+       if parent := c.index.GetNode(&block.PreviousBlockHash); parent == nil {
                c.orphanManage.Add(block)
                return true, nil
        }
@@ -306,13 +309,13 @@ 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)
+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 isBestIrreversible {
+       if isIrreversible && blockHeight > c.IrreversibleBlockHeight() {
                bestIrreversibleNode := c.index.GetNode(blockHash)
                if err := c.store.SaveChainNodeStatus(c.bestNode, bestIrreversibleNode); err != nil {
                        return err