OSDN Git Service

init delete the pow related (#55)
[bytom/vapor.git] / protocol / block.go
index 1771a99..ad4647b 100644 (file)
@@ -1,14 +1,8 @@
 package protocol
 
 import (
-       "encoding/json"
-       "fmt"
-
        log "github.com/sirupsen/logrus"
 
-       "github.com/vapor/common"
-       "github.com/vapor/consensus"
-       engine "github.com/vapor/consensus/consensus"
        "github.com/vapor/errors"
        "github.com/vapor/protocol/bc"
        "github.com/vapor/protocol/bc/types"
@@ -92,19 +86,13 @@ func (c *Chain) connectBlock(block *types.Block) (err error) {
        if err := utxoView.ApplyBlock(bcBlock, bcBlock.TransactionStatus); err != nil {
                return err
        }
+
        node := c.index.GetNode(&bcBlock.ID)
        if err := c.setState(node, utxoView); 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
@@ -132,7 +120,7 @@ func (c *Chain) reorganizeChain(node *state.BlockNode) error {
                        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 {
@@ -153,7 +141,7 @@ 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")
+               log.WithFields(log.Fields{"module": logModule, "height": node.Height, "hash": node.Hash.String()}).Debug("attach from mainchain")
        }
 
        return c.setState(node, utxoView)
@@ -164,10 +152,9 @@ func (c *Chain) saveBlock(block *types.Block) error {
        bcBlock := types.MapBlock(block)
        parent := c.index.GetNode(&block.PreviousBlockHash)
 
-       if err := validation.ValidateBlock(bcBlock, parent, block, c); err != nil {
+       if err := validation.ValidateBlock(bcBlock, parent); err != nil {
                return errors.Sub(ErrBadBlock, err)
        }
-
        if err := c.store.SaveBlock(block, bcBlock.TransactionStatus); err != nil {
                return err
        }
@@ -193,11 +180,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
                }
 
@@ -237,7 +224,7 @@ func (c *Chain) blockProcesser() {
 func (c *Chain) processBlock(block *types.Block) (bool, error) {
        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
        }
 
@@ -255,107 +242,13 @@ 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) ProcessDPoSConnectBlock(block *types.Block) {
-       mapTxFee := c.CalculateBalance(block, true)
-       fmt.Println(mapTxFee)
-}
-
-func DoVoting(block *types.Block, mapTxFee map[bc.Hash]uint64) error {
-       for _, tx := range block.Transactions {
-               to := tx.Outputs[0]
-               var delegate engine.TypedData
-
-               if err := json.Unmarshal(tx.TxData.ReferenceData, delegate); err != nil && to.Amount > 0 {
-                       continue
-               }
-               var (
-                       address common.Address
-                       err     error
-               )
-               address, err = common.NewAddressWitnessPubKeyHash(to.ControlProgram[2:], &consensus.ActiveNetParams)
-               if err != nil {
-                       address, err = common.NewAddressWitnessScriptHash(to.ControlProgram[2:], &consensus.ActiveNetParams)
-                       if err != nil {
-                               return errors.New("ControlProgram cannot be converted to address")
-                       }
-               }
-               hash := block.Hash()
-               height := block.Height
-               switch data := delegate.(type) {
-               case *engine.DelegateInfoList:
-                       continue
-               case *engine.RegisterForgerData:
-                       if mapTxFee[tx.Tx.ID] >= consensus.RegisrerForgerFee {
-                               engine.DposVote.ProcessRegister(address.EncodeAddress(), data.Name, hash, height)
-                       }
-               case *engine.VoteForgerData:
-                       if mapTxFee[tx.Tx.ID] >= consensus.VoteForgerFee {
-                               engine.DposVote.ProcessVote(address.EncodeAddress(), data.Forgers, hash, height)
-                       }
-               case *engine.CancelVoteForgerData:
-                       if mapTxFee[tx.Tx.ID] >= consensus.CancelVoteForgerFee {
-                               engine.DposVote.ProcessCancelVote(address.EncodeAddress(), data.Forgers, hash, height)
-                       }
-               }
-       }
-       return nil
-}
-
-func (c *Chain) CalculateBalance(block *types.Block, fIsAdd bool) map[bc.Hash]uint64 {
-
-       addressBalances := []engine.AddressBalance{}
-       mapTxFee := make(map[bc.Hash]uint64)
-       var (
-               address common.Address
-               err     error
-       )
-
-       for _, tx := range block.Transactions {
-               fee := uint64(0)
-               for _, input := range tx.Inputs {
-                       fee += input.Amount()
-                       value := int64(input.Amount())
-                       address, err = common.NewAddressWitnessPubKeyHash(input.ControlProgram()[2:], &consensus.ActiveNetParams)
-                       if err != nil {
-                               address, err = common.NewAddressWitnessScriptHash(input.ControlProgram()[2:], &consensus.ActiveNetParams)
-                               if err != nil {
-                                       continue
-                               }
-                       }
-                       if fIsAdd {
-                               value = 0 - value
-                       }
-                       addressBalances = append(addressBalances, engine.AddressBalance{address.EncodeAddress(), value})
-               }
-               for _, output := range tx.Outputs {
-                       fee -= output.Amount
-                       value := int64(output.Amount)
-                       address, err = common.NewAddressWitnessPubKeyHash(output.ControlProgram[2:], &consensus.ActiveNetParams)
-                       if err != nil {
-                               address, err = common.NewAddressWitnessScriptHash(output.ControlProgram[2:], &consensus.ActiveNetParams)
-                               if err != nil {
-                                       continue
-                               }
-                       }
-                       if fIsAdd {
-                               value = 0 - value
-                       }
-                       addressBalances = append(addressBalances, engine.AddressBalance{address.EncodeAddress(), value})
-               }
-               mapTxFee[tx.Tx.ID] = fee
-       }
-
-       engine.DposVote.UpdateAddressBalance(addressBalances)
-       return mapTxFee
-}