OSDN Git Service

modify import path (#1805)
[bytom/bytom.git] / cmd / bytomcli / commands / block.go
index bde3dbf..32450b9 100644 (file)
@@ -9,16 +9,29 @@ import (
        "github.com/spf13/cobra"
        jww "github.com/spf13/jwalterweatherman"
 
-       chainjson "github.com/bytom/encoding/json"
-       "github.com/bytom/util"
+       chainjson "github.com/bytom/bytom/encoding/json"
+       "github.com/bytom/bytom/util"
 )
 
-var blockHashCmd = &cobra.Command{
-       Use:   "block-hash",
+func init() {
+       getDifficultyCmd.PersistentFlags().StringVar(&blockHash, "hash", "", "hash of block")
+       getDifficultyCmd.PersistentFlags().IntVar(&blockHeight, "height", 0, "height of block")
+
+       getHashRateCmd.PersistentFlags().StringVar(&blockHash, "hash", "", "hash of block")
+       getHashRateCmd.PersistentFlags().IntVar(&blockHeight, "height", 0, "height of block")
+}
+
+var (
+       blockHash   = ""
+       blockHeight = 0
+)
+
+var getBlockHashCmd = &cobra.Command{
+       Use:   "get-block-hash",
        Short: "Get the hash of most recent block",
        Args:  cobra.NoArgs,
        Run: func(cmd *cobra.Command, args []string) {
-               data, exitCode := util.ClientCall("block-hash")
+               data, exitCode := util.ClientCall("get-block-hash")
                if exitCode != util.Success {
                        os.Exit(exitCode)
                }
@@ -93,12 +106,53 @@ var getBlockCmd = &cobra.Command{
        },
 }
 
-var getBlockHeaderByHashCmd = &cobra.Command{
-       Use:   "get-block-header-by-hash <hash>",
-       Short: "Get the header of a block matching the given hash",
+var getBlockHeaderCmd = &cobra.Command{
+       Use:   "get-block-header <hash> | <height>",
+       Short: "Get the header of a block matching the given hash or height",
        Args:  cobra.ExactArgs(1),
        Run: func(cmd *cobra.Command, args []string) {
-               data, exitCode := util.ClientCall("/get-block-header-by-hash", args[0])
+               var hash chainjson.HexBytes
+               var height uint64
+               var err error
+               isNumber := false
+
+               for _, ch := range args[0] {
+                       // check whether the char is hex digit
+                       if !(unicode.IsNumber(ch) || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F')) {
+                               jww.ERROR.Printf("Invalid value for hash or height")
+                               os.Exit(util.ErrLocalExe)
+                       }
+
+                       if !unicode.IsNumber(ch) {
+                               isNumber = true
+                       }
+               }
+
+               if isNumber {
+                       if len(args[0]) != 64 {
+                               jww.ERROR.Printf("Invalid hash length")
+                               os.Exit(util.ErrLocalExe)
+                       }
+
+                       hash, err = hex.DecodeString(args[0])
+                       if err != nil {
+                               jww.ERROR.Println(err)
+                               os.Exit(util.ErrLocalExe)
+                       }
+               } else {
+                       height, err = strconv.ParseUint(args[0], 10, 64)
+                       if err != nil {
+                               jww.ERROR.Printf("Invalid height value")
+                               os.Exit(util.ErrLocalExe)
+                       }
+               }
+
+               req := &struct {
+                       BlockHeight uint64             `json:"block_height"`
+                       BlockHash   chainjson.HexBytes `json:"block_hash"`
+               }{BlockHeight: height, BlockHash: hash}
+
+               data, exitCode := util.ClientCall("/get-block-header", req)
                if exitCode != util.Success {
                        os.Exit(exitCode)
                }
@@ -106,12 +160,28 @@ var getBlockHeaderByHashCmd = &cobra.Command{
        },
 }
 
-var getBlockTransactionsCountByHashCmd = &cobra.Command{
-       Use:   "get-block-transactions-count-by-hash <hash>",
-       Short: "Get the transactions count of a block matching the given hash",
-       Args:  cobra.ExactArgs(1),
+var getDifficultyCmd = &cobra.Command{
+       Use:   "get-difficulty",
+       Short: "Get the difficulty of most recent block",
+       Args:  cobra.NoArgs,
        Run: func(cmd *cobra.Command, args []string) {
-               data, exitCode := util.ClientCall("/get-block-transactions-count-by-hash", args[0])
+               var hash chainjson.HexBytes
+               var err error
+
+               if blockHash != "" {
+                       hash, err = hex.DecodeString(blockHash)
+                       if err != nil {
+                               jww.ERROR.Println(err)
+                               os.Exit(util.ErrLocalExe)
+                       }
+               }
+
+               req := &struct {
+                       BlockHeight uint64             `json:"block_height"`
+                       BlockHash   chainjson.HexBytes `json:"block_hash"`
+               }{BlockHeight: uint64(blockHeight), BlockHash: hash}
+
+               data, exitCode := util.ClientCall("/get-difficulty", req)
                if exitCode != util.Success {
                        os.Exit(exitCode)
                }
@@ -119,22 +189,31 @@ var getBlockTransactionsCountByHashCmd = &cobra.Command{
        },
 }
 
-var getBlockTransactionsCountByHeightCmd = &cobra.Command{
-       Use:   "get-block-transactions-count-by-height <height>",
-       Short: "Get the transactions count of a block matching the given height",
-       Args:  cobra.ExactArgs(1),
+var getHashRateCmd = &cobra.Command{
+       Use:   "get-hash-rate",
+       Short: "Get the nonce of most recent block",
+       Args:  cobra.NoArgs,
        Run: func(cmd *cobra.Command, args []string) {
-               ui64, err := strconv.ParseUint(args[0], 10, 64)
-               if err != nil {
-                       jww.ERROR.Printf("Invalid height value")
-                       os.Exit(util.ErrLocalExe)
+               var hash chainjson.HexBytes
+               var err error
+
+               if blockHash != "" {
+                       hash, err = hex.DecodeString(blockHash)
+                       if err != nil {
+                               jww.ERROR.Println(err)
+                               os.Exit(util.ErrLocalExe)
+                       }
                }
 
-               data, exitCode := util.ClientCall("/get-block-transactions-count-by-height", ui64)
+               req := &struct {
+                       BlockHeight uint64             `json:"block_height"`
+                       BlockHash   chainjson.HexBytes `json:"block_hash"`
+               }{BlockHeight: uint64(blockHeight), BlockHash: hash}
+
+               data, exitCode := util.ClientCall("/get-hash-rate", req)
                if exitCode != util.Success {
                        os.Exit(exitCode)
                }
-
                printJSON(data)
        },
 }