OSDN Git Service

fix transaction API response json format and bytomcli API (#446)
authoroysheng <33340252+oysheng@users.noreply.github.com>
Wed, 21 Mar 2018 06:05:57 +0000 (14:05 +0800)
committerPaladz <yzhu101@uottawa.ca>
Wed, 21 Mar 2018 06:05:57 +0000 (14:05 +0800)
* standard transaction reserve utxo filter out contract utxo

* add interface get-transaction
motify response json name

* modify json name for api

* fix transaction API response json format

* bytomcli add get-block

* retired xprv

blockchain/hsm.go
blockchain/transact.go
cmd/bytomcli/commands/block.go
cmd/bytomcli/commands/bytomcli.go

index 45650c8..444d8d9 100644 (file)
@@ -37,7 +37,7 @@ func (bcr *BlockchainReactor) pseudohsmListKeys(ctx context.Context) Response {
 
 func (bcr *BlockchainReactor) pseudohsmDeleteKey(ctx context.Context, x struct {
        Password string       `json:"password"`
-       XPub     chainkd.XPub `json:"xpubs"`
+       XPub     chainkd.XPub `json:"xpub"`
 }) Response {
        if err := bcr.hsm.XDelete(x.XPub, x.Password); err != nil {
                return NewErrorResponse(err)
index 4e51c8a..d3e4c23 100644 (file)
@@ -145,7 +145,7 @@ func (bcr *BlockchainReactor) submitSingle(ctx context.Context, tpl *txbuilder.T
                return nil, errors.Wrapf(err, "tx %s", tpl.Transaction.ID.String())
        }
 
-       return map[string]string{"txid": tpl.Transaction.ID.String()}, nil
+       return map[string]string{"tx_id": tpl.Transaction.ID.String()}, nil
 }
 
 // finalizeTxWait calls FinalizeTx and then waits for confirmation of
@@ -223,7 +223,7 @@ func (bcr *BlockchainReactor) submit(ctx context.Context, tpl *txbuilder.Templat
                return NewErrorResponse(err)
        }
 
-       log.WithField("txid", txID["txid"]).Info("submit single tx")
+       log.WithField("tx_id", txID["tx_id"]).Info("submit single tx")
        return NewSuccessResponse(txID)
 }
 
@@ -244,6 +244,6 @@ func (bcr *BlockchainReactor) signSubmit(ctx context.Context, x struct {
                return NewErrorResponse(err)
        }
 
-       log.WithField("txid", txID["txid"]).Info("submit single tx")
+       log.WithField("tx_id", txID["tx_id"]).Info("submit single tx")
        return NewSuccessResponse(txID)
 }
index a63a795..c316f6a 100755 (executable)
@@ -1,11 +1,15 @@
 package commands
 
 import (
+       "encoding/hex"
        "os"
        "strconv"
+       "unicode"
 
        "github.com/spf13/cobra"
        jww "github.com/spf13/jwalterweatherman"
+
+       chainjson "github.com/bytom/encoding/json"
        "github.com/bytom/util"
 )
 
@@ -35,12 +39,53 @@ var blockHeightCmd = &cobra.Command{
        },
 }
 
-var getBlockByHashCmd = &cobra.Command{
-       Use:   "get-block-by-hash <hash>",
-       Short: "Get a whole block matching the given hash",
+var getBlockCmd = &cobra.Command{
+       Use:   "get-block <hash> | <height>",
+       Short: "Get a whole block matching the given hash or height",
        Args:  cobra.ExactArgs(1),
        Run: func(cmd *cobra.Command, args []string) {
-               data, exitCode := util.ClientCall("/get-block-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)
+                       }
+               }
+
+               blockReq := &struct {
+                       BlockHeight uint64             `json:"block_height"`
+                       BlockHash   chainjson.HexBytes `json:"block_hash"`
+               }{BlockHeight: height, BlockHash: hash}
+
+               data, exitCode := util.ClientCall("/get-block", blockReq)
                if exitCode != util.Success {
                        os.Exit(exitCode)
                }
@@ -74,26 +119,6 @@ var getBlockTransactionsCountByHashCmd = &cobra.Command{
        },
 }
 
-var getBlockByHeightCmd = &cobra.Command{
-       Use:   "get-block-by-height <height>",
-       Short: "Get a whole block matching the given height",
-       Args:  cobra.ExactArgs(1),
-       Run: func(cmd *cobra.Command, args []string) {
-               height, err := strconv.ParseUint(args[0], 10, 64)
-               if err != nil {
-                       jww.ERROR.Printf("Invalid height value")
-                       os.Exit(util.ErrLocalExe)
-               }
-
-               data, exitCode := util.ClientCall("/get-block-by-height", height)
-               if exitCode != util.Success {
-                       os.Exit(exitCode)
-               }
-
-               printJSON(data)
-       },
-}
-
 var getBlockTransactionsCountByHeightCmd = &cobra.Command{
        Use:   "get-block-transactions-count-by-height",
        Short: "Get the transactions count of a block matching the given height",
index b3f48e5..dfd2a26 100644 (file)
@@ -101,10 +101,9 @@ func AddCommands() {
 
        BytomcliCmd.AddCommand(blockHeightCmd)
        BytomcliCmd.AddCommand(blockHashCmd)
-       BytomcliCmd.AddCommand(getBlockByHashCmd)
+       BytomcliCmd.AddCommand(getBlockCmd)
        BytomcliCmd.AddCommand(getBlockHeaderByHashCmd)
        BytomcliCmd.AddCommand(getBlockTransactionsCountByHashCmd)
-       BytomcliCmd.AddCommand(getBlockByHeightCmd)
        BytomcliCmd.AddCommand(getBlockTransactionsCountByHeightCmd)
 
        BytomcliCmd.AddCommand(createKeyCmd)