forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add chain set head command (ethereum#226)
- Loading branch information
Showing
8 changed files
with
379 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.