github.com/shrimpyuk/bor@v0.2.15-0.20220224151350-fb4ec6020bae/internal/cli/chain_sethead.go (about)

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