OSDN Git Service

optimse api
authoroysheng <oysheng@bytom.io>
Tue, 17 Apr 2018 09:05:38 +0000 (17:05 +0800)
committeroysheng <oysheng@bytom.io>
Tue, 17 Apr 2018 09:05:38 +0000 (17:05 +0800)
api/api.go
api/query.go
cmd/bytomcli/commands/transaction.go

index 427c2f9..6eb382d 100644 (file)
@@ -199,7 +199,7 @@ func (a *API) buildHandler() {
                m.Handle("/list-transactions", jsonHandler(a.listTransactions))
 
                m.Handle("/get-unconfirmed-transaction", jsonHandler(a.getUnconfirmedTx))
-               m.Handle("/list-unconfirmed-transactions", jsonHandler(a.listUnconformTxs))
+               m.Handle("/list-unconfirmed-transactions", jsonHandler(a.listUnconfirmedTxs))
 
                m.Handle("/list-balances", jsonHandler(a.listBalances))
                m.Handle("/list-unspent-outputs", jsonHandler(a.listUnspentOutputs))
index 8dd339b..565a179 100644 (file)
@@ -2,7 +2,6 @@ package api
 
 import (
        "context"
-       "encoding/hex"
        "fmt"
 
        log "github.com/sirupsen/logrus"
@@ -10,6 +9,7 @@ import (
        "github.com/bytom/account"
        "github.com/bytom/blockchain/query"
        "github.com/bytom/consensus"
+       chainjson "github.com/bytom/encoding/json"
        "github.com/bytom/protocol/bc"
 )
 
@@ -91,16 +91,10 @@ func (a *API) listTransactions(ctx context.Context, filter struct {
 
 // POST /get-unconfirmed-transaction
 func (a *API) getUnconfirmedTx(ctx context.Context, filter struct {
-       TxID string `json:"tx_id"`
+       TxID chainjson.HexBytes `json:"tx_id"`
 }) Response {
-       txID, err := hex.DecodeString(filter.TxID)
-       if err != nil {
-               log.Errorf("convert txID[%s] string to byte err: %v", filter.TxID, err)
-               return NewErrorResponse(err)
-       }
-
        var tmpTxID [32]byte
-       copy(tmpTxID[:], txID[:])
+       copy(tmpTxID[:], filter.TxID[:])
 
        txHash := bc.NewHash(tmpTxID)
        txPool := a.chain.GetTxPool()
@@ -129,18 +123,14 @@ func (a *API) getUnconfirmedTx(ctx context.Context, filter struct {
        return NewSuccessResponse(tx)
 }
 
-type getTxPoolResp struct {
-       TxID bc.Hash `json:"tx_id"`
-}
-
 // POST /list-unconform-transactions
-func (a *API) listUnconformTxs(ctx context.Context) Response {
-       txIDs := []getTxPoolResp{}
+func (a *API) listUnconfirmedTxs(ctx context.Context) Response {
+       txIDs := []bc.Hash{}
 
        txPool := a.chain.GetTxPool()
        txs := txPool.GetTransactions()
        for _, txDesc := range txs {
-               txIDs = append(txIDs, getTxPoolResp{TxID: bc.Hash(txDesc.Tx.ID)})
+               txIDs = append(txIDs, bc.Hash(txDesc.Tx.ID))
        }
 
        return NewSuccessResponse(txIDs)
index 8b0b54d..4a24299 100644 (file)
@@ -1,6 +1,7 @@
 package commands
 
 import (
+       "encoding/hex"
        "encoding/json"
        "fmt"
        "os"
@@ -10,6 +11,7 @@ import (
 
        "github.com/bytom/api"
        "github.com/bytom/blockchain/txbuilder"
+       chainjson "github.com/bytom/encoding/json"
        "github.com/bytom/protocol/bc/types"
        "github.com/bytom/util"
 )
@@ -347,9 +349,15 @@ var getUnconfirmedTransactionCmd = &cobra.Command{
        Short: "get unconfirmed transaction by matching the given transaction hash",
        Args:  cobra.ExactArgs(1),
        Run: func(cmd *cobra.Command, args []string) {
+               txID, err := hex.DecodeString(args[0])
+               if err != nil {
+                       jww.ERROR.Println(err)
+                       os.Exit(util.ErrLocalExe)
+               }
+
                txInfo := &struct {
-                       TxID string `json:"tx_id"`
-               }{TxID: args[0]}
+                       TxID chainjson.HexBytes `json:"tx_id"`
+               }{TxID: txID}
 
                data, exitCode := util.ClientCall("/get-unconfirmed-transaction", txInfo)
                if exitCode != util.Success {