Skip to content

Commit

Permalink
[lbry] claimtrie: allow cli block command to output hash, height, or …
Browse files Browse the repository at this point in the history
…both
  • Loading branch information
roylee17 committed May 27, 2022
1 parent e3ab278 commit 449712d
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions claimtrie/cmd/cmd/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ func NewBlocCommands() *cobra.Command {
}
func NewBlockBestCommand() *cobra.Command {

var showHash bool
var showHeight bool

cmd := &cobra.Command{
Use: "best",
Short: "Show the height and hash of the best block",
Short: "Show the block hash and height of the best block",
RunE: func(cmd *cobra.Command, args []string) error {

db, err := loadBlocksDB()
Expand All @@ -42,23 +45,36 @@ func NewBlockBestCommand() *cobra.Command {
}

state := chain.BestSnapshot()
fmt.Printf("Block %7d: %s\n", state.Height, state.Hash.String())

switch {
case showHeight && showHash:
fmt.Printf("%s:%d\n", state.Hash, state.Height)
case !showHeight && showHash:
fmt.Printf("%s\n", state.Hash)
case showHeight && !showHash:
fmt.Printf("%d\n", state.Height)
}

return nil
},
}

cmd.Flags().BoolVar(&showHeight, "showheight", true, "Display block height")
cmd.Flags().BoolVar(&showHash, "showhash", true, "Display block hash")

return cmd
}

func NewBlockListCommand() *cobra.Command {

var fromHeight int32
var toHeight int32
var showHash bool
var showHeight bool

cmd := &cobra.Command{
Use: "list",
Short: "List merkle hash of blocks between <from_height> <to_height>",
Short: "List block hash and height between blocks <from_height> <to_height>",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {

Expand All @@ -82,7 +98,14 @@ func NewBlockListCommand() *cobra.Command {
if err != nil {
return errors.Wrapf(err, "load hash for %d", ht)
}
fmt.Printf("Block %7d: %s\n", ht, hash.String())
switch {
case showHeight && showHash:
fmt.Printf("%s:%d\n", hash, ht)
case !showHeight && showHash:
fmt.Printf("%s\n", hash)
case showHeight && !showHash:
fmt.Printf("%d\n", ht)
}
}

return nil
Expand All @@ -91,6 +114,8 @@ func NewBlockListCommand() *cobra.Command {

cmd.Flags().Int32Var(&fromHeight, "from", 0, "From height (inclusive)")
cmd.Flags().Int32Var(&toHeight, "to", 0, "To height (inclusive)")
cmd.Flags().BoolVar(&showHeight, "showheight", true, "Display block height")
cmd.Flags().BoolVar(&showHash, "showhash", true, "Display block hash")
cmd.Flags().SortFlags = false

return cmd
Expand Down

0 comments on commit 449712d

Please sign in to comment.