OSDN Git Service

add api get-unconfirmed-transaction
authoroysheng <oysheng@bytom.io>
Tue, 17 Apr 2018 07:49:46 +0000 (15:49 +0800)
committeroysheng <oysheng@bytom.io>
Tue, 17 Apr 2018 07:49:46 +0000 (15:49 +0800)
add list-unconfirmed-transactions

api/api.go
api/query.go
cmd/bytomcli/commands/bytomcli.go
cmd/bytomcli/commands/transaction.go

index 5e36e5d..427c2f9 100644 (file)
@@ -197,7 +197,9 @@ func (a *API) buildHandler() {
 
                m.Handle("/get-transaction", jsonHandler(a.getTransaction))
                m.Handle("/list-transactions", jsonHandler(a.listTransactions))
-               m.Handle("/get-txpool", jsonHandler(a.getChainTxPool))
+
+               m.Handle("/get-unconfirmed-transaction", jsonHandler(a.getUnconfirmedTx))
+               m.Handle("/list-unconfirmed-transactions", jsonHandler(a.listUnconformTxs))
 
                m.Handle("/list-balances", jsonHandler(a.listBalances))
                m.Handle("/list-unspent-outputs", jsonHandler(a.listUnspentOutputs))
index 07de7dc..237d5b4 100644 (file)
@@ -6,6 +6,7 @@ import (
 
        log "github.com/sirupsen/logrus"
 
+       "encoding/hex"
        "github.com/bytom/account"
        "github.com/bytom/blockchain/query"
        "github.com/bytom/consensus"
@@ -88,12 +89,52 @@ func (a *API) listTransactions(ctx context.Context, filter struct {
        return NewSuccessResponse(transactions)
 }
 
+// POST /get-unconfirmed-transaction
+func (a *API) getUnconfirmedTx(ctx context.Context, filter struct {
+       TxID string `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[:])
+
+       txHash := bc.NewHash(tmpTxID)
+       txPool := a.chain.GetTxPool()
+       txDesc, err := txPool.GetTransaction(&txHash)
+       if err != nil {
+               return NewErrorResponse(err)
+       }
+
+       tx := &BlockTx{
+               ID:         txDesc.Tx.ID,
+               Version:    txDesc.Tx.Version,
+               Size:       txDesc.Tx.SerializedSize,
+               TimeRange:  txDesc.Tx.TimeRange,
+               Inputs:     []*query.AnnotatedInput{},
+               Outputs:    []*query.AnnotatedOutput{},
+               StatusFail: false,
+       }
+
+       for i := range txDesc.Tx.Inputs {
+               tx.Inputs = append(tx.Inputs, a.wallet.BuildAnnotatedInput(txDesc.Tx, uint32(i)))
+       }
+       for i := range txDesc.Tx.Outputs {
+               tx.Outputs = append(tx.Outputs, a.wallet.BuildAnnotatedOutput(txDesc.Tx, i))
+       }
+
+       return NewSuccessResponse(tx)
+}
+
 type getTxPoolResp struct {
        TxID bc.Hash `json:"tx_id"`
 }
 
-// POST /get-txpool
-func (a *API) getChainTxPool(ctx context.Context) Response {
+// POST /list-unconform-transactions
+func (a *API) listUnconformTxs(ctx context.Context) Response {
        txIDs := []getTxPoolResp{}
 
        txPool := a.chain.GetTxPool()
index 90952cc..1c1ca6c 100644 (file)
@@ -91,6 +91,10 @@ func AddCommands() {
 
        BytomcliCmd.AddCommand(getTransactionCmd)
        BytomcliCmd.AddCommand(listTransactionsCmd)
+
+       BytomcliCmd.AddCommand(getUnconfirmedTransactionCmd)
+       BytomcliCmd.AddCommand(listUnconfirmedTransactionsCmd)
+
        BytomcliCmd.AddCommand(listUnspentOutputsCmd)
        BytomcliCmd.AddCommand(listBalancesCmd)
 
@@ -99,7 +103,6 @@ func AddCommands() {
        BytomcliCmd.AddCommand(submitTransactionCmd)
        BytomcliCmd.AddCommand(estimateTransactionGasCmd)
        BytomcliCmd.AddCommand(signSubTransactionCmd)
-       BytomcliCmd.AddCommand(getTxPoolCmd)
 
        BytomcliCmd.AddCommand(getBlockCountCmd)
        BytomcliCmd.AddCommand(blockHashCmd)
index 0d21789..8b0b54d 100644 (file)
@@ -304,20 +304,6 @@ var signSubTransactionCmd = &cobra.Command{
        },
 }
 
-var getTxPoolCmd = &cobra.Command{
-       Use:   "get-txpool",
-       Short: "get txpool transactions hashes",
-       Args:  cobra.NoArgs,
-       Run: func(cmd *cobra.Command, args []string) {
-               data, exitCode := util.ClientCall("/get-txpool")
-               if exitCode != util.Success {
-                       os.Exit(exitCode)
-               }
-
-               printJSONList(data)
-       },
-}
-
 var getTransactionCmd = &cobra.Command{
        Use:   "get-transaction <hash>",
        Short: "get the transaction by matching the given transaction hash",
@@ -356,6 +342,38 @@ var listTransactionsCmd = &cobra.Command{
        },
 }
 
+var getUnconfirmedTransactionCmd = &cobra.Command{
+       Use:   "get-unconfirmed-transaction <hash>",
+       Short: "get unconfirmed transaction by matching the given transaction hash",
+       Args:  cobra.ExactArgs(1),
+       Run: func(cmd *cobra.Command, args []string) {
+               txInfo := &struct {
+                       TxID string `json:"tx_id"`
+               }{TxID: args[0]}
+
+               data, exitCode := util.ClientCall("/get-unconfirmed-transaction", txInfo)
+               if exitCode != util.Success {
+                       os.Exit(exitCode)
+               }
+
+               printJSON(data)
+       },
+}
+
+var listUnconfirmedTransactionsCmd = &cobra.Command{
+       Use:   "list-unconfirmed-transactions",
+       Short: "list unconfirmed transactions hashes",
+       Args:  cobra.NoArgs,
+       Run: func(cmd *cobra.Command, args []string) {
+               data, exitCode := util.ClientCall("/list-unconfirmed-transactions")
+               if exitCode != util.Success {
+                       os.Exit(exitCode)
+               }
+
+               printJSONList(data)
+       },
+}
+
 var gasRateCmd = &cobra.Command{
        Use:   "gas-rate",
        Short: "Print the current gas rate",