)
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) {
}
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)
}
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
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
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{}