Skip to content

Commit

Permalink
Add chain set head command (ethereum#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
ferranbt authored Nov 11, 2021
1 parent 1cd6c84 commit 2191490
Show file tree
Hide file tree
Showing 8 changed files with 379 additions and 73 deletions.
31 changes: 31 additions & 0 deletions command/chain.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package main

import (
"github.com/mitchellh/cli"
)

// ChainCommand is the command to group the peers commands
type ChainCommand struct {
UI cli.Ui
}

// Help implements the cli.Command interface
func (c *ChainCommand) Help() string {
return `Usage: bor chain <subcommand>
This command groups actions to interact with the chain.
Set the new head of the chain:
$ bor chain sethead <number>`
}

// Synopsis implements the cli.Command interface
func (c *ChainCommand) Synopsis() string {
return "Interact with the chain"
}

// Run implements the cli.Command interface
func (c *ChainCommand) Run(args []string) int {
return cli.RunResultHelp
}
91 changes: 91 additions & 0 deletions command/chain_sethead.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package main

import (
"context"
"fmt"
"strconv"

"github.com/ethereum/go-ethereum/command/flagset"
"github.com/ethereum/go-ethereum/command/server/proto"
)

// ChainSetHeadCommand is the command to group the peers commands
type ChainSetHeadCommand struct {
*Meta2

yes bool
}

// Help implements the cli.Command interface
func (c *ChainSetHeadCommand) Help() string {
return `Usage: bor chain sethead <number> [--yes]
This command sets the current chain to a certain block`
}

func (c *ChainSetHeadCommand) Flags() *flagset.Flagset {
flags := c.NewFlagSet("chain sethead")

flags.BoolFlag(&flagset.BoolFlag{
Name: "yes",
Usage: "Force set head",
Default: false,
Value: &c.yes,
})
return flags
}

// Synopsis implements the cli.Command interface
func (c *ChainSetHeadCommand) Synopsis() string {
return "Set the new head of the chain"
}

// Run implements the cli.Command interface
func (c *ChainSetHeadCommand) Run(args []string) int {
flags := c.Flags()
if err := flags.Parse(args); err != nil {
c.UI.Error(err.Error())
return 1
}

args = flags.Args()
if len(args) != 1 {
c.UI.Error("No number provided")
return 1
}

borClt, err := c.BorConn()
if err != nil {
c.UI.Error(err.Error())
return 1
}

arg := args[0]
fmt.Println(arg)

number, err := strconv.Atoi(arg)
if err != nil {
c.UI.Error(err.Error())
return 1
}

if !c.yes {
response, err := c.UI.Ask("Are you sure you want to reset the database? (y/n)")
if err != nil {
c.UI.Error(err.Error())
return 1
}
if response != "y" {
c.UI.Output("set head aborted")
return 0
}
}

if _, err := borClt.ChainSetHead(context.Background(), &proto.ChainSetHeadRequest{Number: uint64(number)}); err != nil {
c.UI.Error(err.Error())
return 1
}

c.UI.Output("Done!")
return 0
}
10 changes: 10 additions & 0 deletions command/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ func commands() map[string]cli.CommandFactory {
Meta2: meta2,
}, nil
},
"chain": func() (cli.Command, error) {
return &ChainCommand{
UI: ui,
}, nil
},
"chain sethead": func() (cli.Command, error) {
return &ChainSetHeadCommand{
Meta2: meta2,
}, nil
},
"account": func() (cli.Command, error) {
return &Account{
UI: ui,
Expand Down
Loading

0 comments on commit 2191490

Please sign in to comment.