OSDN Git Service

modify miner block to propose block (#92)
[bytom/vapor.git] / protocol / block.go
index 1894f68..8ae535a 100644 (file)
@@ -4,6 +4,7 @@ import (
        log "github.com/sirupsen/logrus"
 
        "github.com/vapor/errors"
+       "github.com/vapor/event"
        "github.com/vapor/protocol/bc"
        "github.com/vapor/protocol/bc/types"
        "github.com/vapor/protocol/state"
@@ -74,6 +75,7 @@ func (c *Chain) calcReorganizeNodes(node *state.BlockNode) ([]*state.BlockNode,
 }
 
 func (c *Chain) connectBlock(block *types.Block) (err error) {
+       irreversibleNode := c.bestIrreversibleNode
        bcBlock := types.MapBlock(block)
        if bcBlock.TransactionStatus, err = c.store.GetTransactionStatus(&bcBlock.ID); err != nil {
                return err
@@ -86,19 +88,22 @@ func (c *Chain) connectBlock(block *types.Block) (err error) {
        if err := utxoView.ApplyBlock(bcBlock, bcBlock.TransactionStatus); err != nil {
                return err
        }
+
+       voteResultMap := make(map[uint64]*state.VoteResult)
+       if err := c.bbft.ApplyBlock(voteResultMap, block); err != nil {
+               return err
+       }
+
        node := c.index.GetNode(&bcBlock.ID)
-       if err := c.setState(node, utxoView); err != nil {
+       if c.bbft.isIrreversible(block) && block.Height > irreversibleNode.Height {
+               irreversibleNode = node
+       }
+
+       if err := c.setState(node, irreversibleNode, utxoView, voteResultMap); err != nil {
                return err
        }
+
        for _, tx := range block.Transactions {
-               for key, value := range tx.Entries {
-                       switch value.(type) {
-                       case *bc.Claim:
-                               c.store.SetWithdrawSpent(&key)
-                       default:
-                               continue
-                       }
-               }
                c.txPool.RemoveTransaction(&tx.Tx.ID)
        }
        return nil
@@ -107,13 +112,19 @@ func (c *Chain) connectBlock(block *types.Block) (err error) {
 func (c *Chain) reorganizeChain(node *state.BlockNode) error {
        attachNodes, detachNodes := c.calcReorganizeNodes(node)
        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 {
                        return err
                }
 
+               if b.Height <= irreversibleNode.Height {
+                       return errors.New("the height of rollback block below the height of irreversible block")
+               }
+
                detachBlock := types.MapBlock(b)
                if err := c.store.GetTransactionsUtxo(utxoView, detachBlock.Transactions); err != nil {
                        return err
@@ -125,8 +136,12 @@ func (c *Chain) reorganizeChain(node *state.BlockNode) error {
                if err := utxoView.DetachBlock(detachBlock, txStatus); err != nil {
                        return err
                }
+               
+               if err := c.bbft.DetachBlock(voteResultMap, b); err != nil {
+                       return err
+               }
 
-               log.WithFields(log.Fields{"height": node.Height, "hash": node.Hash.String()}).Debug("detach from mainchain")
+               log.WithFields(log.Fields{"module": logModule, "height": node.Height, "hash": node.Hash.String()}).Debug("detach from mainchain")
        }
 
        for _, attachNode := range attachNodes {
@@ -147,20 +162,43 @@ func (c *Chain) reorganizeChain(node *state.BlockNode) error {
                        return err
                }
 
-               log.WithFields(log.Fields{"height": node.Height, "hash": node.Hash.String()}).Debug("attach from mainchain")
+               if err := c.bbft.ApplyBlock(voteResultMap, b); err != nil {
+                       return err
+               }
+
+               if c.bbft.isIrreversible(b) && b.Height > irreversibleNode.Height {
+                       irreversibleNode = attachNode
+               }
+
+               log.WithFields(log.Fields{"module": logModule, "height": node.Height, "hash": node.Hash.String()}).Debug("attach from mainchain")
        }
 
-       return c.setState(node, utxoView)
+       return c.setState(node, irreversibleNode, utxoView, voteResultMap)
 }
 
 // SaveBlock will validate and save block into storage
 func (c *Chain) saveBlock(block *types.Block) error {
-       bcBlock := types.MapBlock(block)
+       if err := c.bbft.ValidateBlock(block); err != nil {
+               return errors.Sub(ErrBadBlock, err)
+       }
+
        parent := c.index.GetNode(&block.PreviousBlockHash)
+       if err := validation.ValidateBlock(types.MapBlock(block), parent); err != nil {
+               return errors.Sub(ErrBadBlock, err)
+       }
 
-       if err := validation.ValidateBlock(bcBlock, parent, block, c, c.engine, c.Authoritys, c.position); err != nil {
+       signature, err := c.bbft.SignBlock(block)
+       if err != nil {
                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
        }
@@ -186,11 +224,11 @@ func (c *Chain) saveSubBlock(block *types.Block) *types.Block {
        for _, prevOrphan := range prevOrphans {
                orphanBlock, ok := c.orphanManage.Get(prevOrphan)
                if !ok {
-                       log.WithFields(log.Fields{"hash": prevOrphan.String()}).Warning("saveSubBlock fail to get block from orphanManage")
+                       log.WithFields(log.Fields{"module": logModule, "hash": prevOrphan.String()}).Warning("saveSubBlock fail to get block from orphanManage")
                        continue
                }
                if err := c.saveBlock(orphanBlock); err != nil {
-                       log.WithFields(log.Fields{"hash": prevOrphan.String(), "height": orphanBlock.Height}).Warning("saveSubBlock fail to save block")
+                       log.WithFields(log.Fields{"module": logModule, "hash": prevOrphan.String(), "height": orphanBlock.Height}).Warning("saveSubBlock fail to save block")
                        continue
                }
 
@@ -228,13 +266,18 @@ 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")
+       }
+       
        blockHash := block.Hash()
        if c.BlockExist(&blockHash) {
-               log.WithFields(log.Fields{"hash": blockHash.String(), "height": block.Height}).Info("block has been processed")
+               log.WithFields(log.Fields{"module": logModule, "hash": blockHash.String(), "height": block.Height}).Info("block has been processed")
                return c.orphanManage.BlockExist(&blockHash), nil
        }
 
-       if parent := c.index.GetNode(&block.PreviousBlockHash); parent == nil {
+       parent := c.index.GetNode(&block.PreviousBlockHash)
+       if parent == nil {
                c.orphanManage.Add(block)
                return true, nil
        }
@@ -248,13 +291,30 @@ func (c *Chain) processBlock(block *types.Block) (bool, error) {
        bestNode := c.index.GetNode(&bestBlockHash)
 
        if bestNode.Parent == c.bestNode {
-               log.Debug("append block to the end of mainchain")
+               log.WithFields(log.Fields{"module": logModule}).Debug("append block to the end of mainchain")
                return false, c.connectBlock(bestBlock)
        }
 
-       if bestNode.Height > c.bestNode.Height && bestNode.WorkSum.Cmp(c.bestNode.WorkSum) >= 0 {
-               log.Debug("start to reorganize chain")
+       if bestNode.Height > c.bestNode.Height {
+               log.WithFields(log.Fields{"module": logModule}).Debug("start to reorganize chain")
                return false, c.reorganizeChain(bestNode)
        }
        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
+}