OSDN Git Service

s
authoripqhjjybj <250657661@qq.com>
Tue, 31 Dec 2019 11:18:18 +0000 (19:18 +0800)
committeripqhjjybj <250657661@qq.com>
Tue, 31 Dec 2019 11:18:18 +0000 (19:18 +0800)
api/api.go
api/block_retrieve.go
cmd/vaporcli/commands/block.go
protocol/block.go

index e6ed44b..9543929 100644 (file)
@@ -277,7 +277,7 @@ func (a *API) buildHandler() {
        m.Handle("/get-block-header", jsonHandler(a.getBlockHeader))
        m.Handle("/get-block-count", jsonHandler(a.getBlockCount))
 
-       m.Handle("/rollback-height", jsonHandler(a.rollbackHeight))
+       m.Handle("/rollback", jsonHandler(a.rollback))
 
        m.Handle("/is-mining", jsonHandler(a.isMining))
        m.Handle("/set-mining", jsonHandler(a.setMining))
index da47548..2e31188 100644 (file)
@@ -24,10 +24,10 @@ func (a *API) getBlockCount() Response {
 }
 
 // return rollback result
-func (a *API) rollbackHeight(ctx context.Context, x struct {
-       RollbackHeight int64 `json:"height"`
+func (a *API) rollback(ctx context.Context, x struct {
+       Height int64 `json:"height"`
 }) Response {
-       height := x.RollbackHeight
+       height := x.Height
        err := a.chain.Rollback(height)
        if err != nil {
                log.Errorf("rollback: %v", err)
index 917c1f3..13223ee 100644 (file)
@@ -27,7 +27,7 @@ var (
 )
 
 var rollbackCmd = &cobra.Command{
-       Use:   "rollback-height <height>",
+       Use:   "rollback <height>",
        Short: "rollback the chain to target height",
        Args:  cobra.ExactArgs(1),
        Run: func(cmd *cobra.Command, args []string) {
@@ -52,10 +52,10 @@ var rollbackCmd = &cobra.Command{
                }
 
                rollbackReq := &struct {
-                       RollbackHeight int64 `json:"height"`
-               }{RollbackHeight: height}
+                       Height int64 `json:"height"`
+               }{Height: height}
 
-               data, exitCode := util.ClientCall("/rollback-height", rollbackReq)
+               data, exitCode := util.ClientCall("/rollback", rollbackReq)
                if exitCode != util.Success {
                        os.Exit(exitCode)
                }
index ca17273..a04f99a 100644 (file)
@@ -137,6 +137,44 @@ func (c *Chain) connectBlock(block *types.Block) (err error) {
        return nil
 }
 
+func (c *Chain) rollbackBlock(detachBlockHeader *types.BlockHeader, consensusResult *state.ConsensusResult, utxoView *state.UtxoViewpoint) (*types.Block, error) {
+       detachHash := detachBlockHeader.Hash()
+       block, err := c.store.GetBlock(&detachHash)
+       if err != nil {
+               return block, err
+       }
+
+       detachBlock := types.MapBlock(block)
+
+       if err := consensusResult.DetachBlock(block); err != nil {
+               return block, err
+       }
+
+       if err := c.store.GetTransactionsUtxo(utxoView, detachBlock.Transactions); err != nil {
+               return block, err
+       }
+
+       txStatus, err := c.GetTransactionStatus(&detachBlock.ID)
+       if err != nil {
+               return block, err
+       }
+
+       if err := utxoView.DetachBlock(detachBlock, txStatus); err != nil {
+               return block, err
+       }
+
+       for _, p := range c.subProtocols {
+               if err := p.DetachBlock(block); err != nil {
+                       return block, errors.Wrap(err, p.Name(), "sub protocol detach block")
+               }
+       }
+
+       blockHash := detachBlockHeader.Hash()
+       log.WithFields(log.Fields{"module": logModule, "height": detachBlockHeader.Height, "hash": blockHash.String()}).Debug("detach from mainchain")
+
+       return block, nil
+}
+
 func (c *Chain) Rollback(height int64) error {
        if height <= -1 {
                return nil
@@ -162,40 +200,12 @@ func (c *Chain) Rollback(height int64) error {
                        break
                }
 
-               detachHash := detachBlockHeader.Hash()
-               block, err := c.GetBlockByHash(&detachHash)
-               if err != nil {
-                       return err
-               }
-
-               detachBlock := types.MapBlock(block)
-
-               if err := consensusResult.DetachBlock(block); err != nil {
-                       return err
-               }
-
-               if err := c.store.GetTransactionsUtxo(utxoView, detachBlock.Transactions); err != nil {
-                       return err
-               }
+               block, err := c.rollbackBlock(detachBlockHeader, consensusResult, utxoView)
 
-               txStatus, err := c.GetTransactionStatus(&detachBlock.ID)
                if err != nil {
                        return err
                }
 
-               if err := utxoView.DetachBlock(detachBlock, txStatus); err != nil {
-                       return err
-               }
-
-               for _, p := range c.subProtocols {
-                       if err := p.DetachBlock(block); err != nil {
-                               return errors.Wrap(err, p.Name(), "sub protocol detach block")
-                       }
-               }
-
-               blockHash := detachBlockHeader.Hash()
-               log.WithFields(log.Fields{"module": logModule, "height": detachBlockHeader.Height, "hash": blockHash.String()}).Debug("detach from mainchain")
-
                c.store.DeleteBlock(block)
 
                prevBlockHash := detachBlockHeader.PreviousBlockHash
@@ -229,42 +239,19 @@ func (c *Chain) reorganizeChain(blockHeader *types.BlockHeader) error {
 
        txsToRestore := map[bc.Hash]*types.Tx{}
        for _, detachBlockHeader := range detachBlockHeaders {
-               detachHash := detachBlockHeader.Hash()
-               b, err := c.store.GetBlock(&detachHash)
-               if err != nil {
-                       return err
-               }
 
-               detachBlock := types.MapBlock(b)
-               if err := c.store.GetTransactionsUtxo(utxoView, detachBlock.Transactions); err != nil {
-                       return err
-               }
+               b, err := c.rollbackBlock(detachBlockHeader, consensusResult, utxoView)
 
-               txStatus, err := c.GetTransactionStatus(&detachBlock.ID)
                if err != nil {
                        return err
                }
 
-               if err := utxoView.DetachBlock(detachBlock, txStatus); err != nil {
-                       return err
-               }
-
-               if err := consensusResult.DetachBlock(b); err != nil {
-                       return err
-               }
-
-               for _, p := range c.subProtocols {
-                       if err := p.DetachBlock(b); err != nil {
-                               return errors.Wrap(err, p.Name(), "sub protocol detach block")
-                       }
-               }
-
                for _, tx := range b.Transactions {
                        txsToRestore[tx.ID] = tx
                }
 
-               blockHash := blockHeader.Hash()
-               log.WithFields(log.Fields{"module": logModule, "height": blockHeader.Height, "hash": blockHash.String()}).Debug("detach from mainchain")
+               // blockHash := blockHeader.Hash()
+               // log.WithFields(log.Fields{"module": logModule, "height": blockHeader.Height, "hash": blockHash.String()}).Debug("detach from mainchain")
        }
 
        txsToRemove := map[bc.Hash]*types.Tx{}