From 07ff0584345ec2c5167c2b9d1209043a682554df Mon Sep 17 00:00:00 2001 From: oysheng Date: Tue, 17 Apr 2018 15:49:46 +0800 Subject: [PATCH] add api get-unconfirmed-transaction add list-unconfirmed-transactions --- api/api.go | 4 +++- api/query.go | 45 +++++++++++++++++++++++++++++++++-- cmd/bytomcli/commands/bytomcli.go | 5 +++- cmd/bytomcli/commands/transaction.go | 46 +++++++++++++++++++++++++----------- 4 files changed, 82 insertions(+), 18 deletions(-) diff --git a/api/api.go b/api/api.go index 5e36e5db..427c2f98 100644 --- a/api/api.go +++ b/api/api.go @@ -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)) diff --git a/api/query.go b/api/query.go index 07de7dce..237d5b4f 100644 --- a/api/query.go +++ b/api/query.go @@ -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() diff --git a/cmd/bytomcli/commands/bytomcli.go b/cmd/bytomcli/commands/bytomcli.go index 90952cc8..1c1ca6c3 100644 --- a/cmd/bytomcli/commands/bytomcli.go +++ b/cmd/bytomcli/commands/bytomcli.go @@ -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) diff --git a/cmd/bytomcli/commands/transaction.go b/cmd/bytomcli/commands/transaction.go index 0d21789d..8b0b54d5 100644 --- a/cmd/bytomcli/commands/transaction.go +++ b/cmd/bytomcli/commands/transaction.go @@ -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 ", 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 ", + 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", -- 2.11.0