github.com/palisadeinc/bor@v0.0.0-20230615125219-ab7196213d15/internal/cli/chain_sethead.go (about)

     1  package cli
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strconv"
     7  	"strings"
     8  
     9  	"github.com/ethereum/go-ethereum/internal/cli/flagset"
    10  	"github.com/ethereum/go-ethereum/internal/cli/server/proto"
    11  )
    12  
    13  // ChainSetHeadCommand is the command to group the peers commands
    14  type ChainSetHeadCommand struct {
    15  	*Meta2
    16  
    17  	yes bool
    18  }
    19  
    20  // MarkDown implements cli.MarkDown interface
    21  func (a *ChainSetHeadCommand) MarkDown() string {
    22  	items := []string{
    23  		"# Chain sethead",
    24  		"The ```chain sethead <number>``` command sets the current chain to a certain block.",
    25  		"## Arguments",
    26  		"- ```number```: The block number to roll back.",
    27  		a.Flags().MarkDown(),
    28  	}
    29  
    30  	return strings.Join(items, "\n\n")
    31  }
    32  
    33  // Help implements the cli.Command interface
    34  func (c *ChainSetHeadCommand) Help() string {
    35  	return `Usage: bor chain sethead <number> [--yes]
    36  
    37    This command sets the current chain to a certain block`
    38  }
    39  
    40  func (c *ChainSetHeadCommand) Flags() *flagset.Flagset {
    41  	flags := c.NewFlagSet("chain sethead")
    42  
    43  	flags.BoolFlag(&flagset.BoolFlag{
    44  		Name:    "yes",
    45  		Usage:   "Force set head",
    46  		Default: false,
    47  		Value:   &c.yes,
    48  	})
    49  
    50  	return flags
    51  }
    52  
    53  // Synopsis implements the cli.Command interface
    54  func (c *ChainSetHeadCommand) Synopsis() string {
    55  	return "Set the new head of the chain"
    56  }
    57  
    58  // Run implements the cli.Command interface
    59  func (c *ChainSetHeadCommand) Run(args []string) int {
    60  	flags := c.Flags()
    61  	if err := flags.Parse(args); err != nil {
    62  		c.UI.Error(err.Error())
    63  		return 1
    64  	}
    65  
    66  	args = flags.Args()
    67  	if len(args) != 1 {
    68  		c.UI.Error("No number provided")
    69  		return 1
    70  	}
    71  
    72  	borClt, err := c.BorConn()
    73  	if err != nil {
    74  		c.UI.Error(err.Error())
    75  		return 1
    76  	}
    77  
    78  	arg := args[0]
    79  	fmt.Println(arg)
    80  
    81  	number, err := strconv.Atoi(arg)
    82  	if err != nil {
    83  		c.UI.Error(err.Error())
    84  		return 1
    85  	}
    86  
    87  	if !c.yes {
    88  		response, err := c.UI.Ask("Are you sure you want to reset the database? (y/n)")
    89  		if err != nil {
    90  			c.UI.Error(err.Error())
    91  			return 1
    92  		}
    93  
    94  		if response != "y" {
    95  			c.UI.Output("set head aborted")
    96  			return 0
    97  		}
    98  	}
    99  
   100  	if _, err := borClt.ChainSetHead(context.Background(), &proto.ChainSetHeadRequest{Number: uint64(number)}); err != nil {
   101  		c.UI.Error(err.Error())
   102  		return 1
   103  	}
   104  
   105  	c.UI.Output("Done!")
   106  
   107  	return 0
   108  }