From 1c91ea122b98ec60e188b6290915304bff62dca0 Mon Sep 17 00:00:00 2001 From: erick yan <46879318+erickyan86@users.noreply.github.com> Date: Sun, 29 Sep 2019 17:01:21 +0800 Subject: [PATCH] add txpool cmd gettxsbyaccount (#502) --- cmd/ft/txpool.go | 42 +++++++++++++++++++++++++++++++++++------- rpcapi/txpool.go | 36 +++++++++++++++++++++++++++++++++--- 2 files changed, 68 insertions(+), 10 deletions(-) diff --git a/cmd/ft/txpool.go b/cmd/ft/txpool.go index 2d16e697..66bb1c8d 100644 --- a/cmd/ft/txpool.go +++ b/cmd/ft/txpool.go @@ -32,10 +32,17 @@ var contentCmd = &cobra.Command{ Use: "content ", Short: "Returns the transactions contained within the transaction pool.", Long: `Returns the transactions contained within the transaction pool.`, - Args: cobra.ExactArgs(1), + Args: cobra.RangeArgs(0, 1), Run: func(cmd *cobra.Command, args []string) { - var result interface{} - clientCall(ipcEndpoint, &result, "txpool_content", parseBool(args[0])) + var ( + result interface{} + fullTx bool + ) + if len(args) == 1 { + fullTx = parseBool(args[0]) + } + + clientCall(ipcEndpoint, &result, "txpool_content", fullTx) printJSON(result) }, } @@ -64,16 +71,37 @@ var setGasPriceCmd = &cobra.Command{ }, } -var getTxCmd = &cobra.Command{ - Use: "gettx ", +var getTxsCmd = &cobra.Command{ + Use: "gettxs ", Short: "Returns the transaction for the given hash", Long: `Returns the transaction for the given hash`, + Args: cobra.MinimumNArgs(1), Run: func(cmd *cobra.Command, args []string) { var result []*types.RPCTransaction - clientCall(ipcEndpoint, &result, "txpool_getPoolTransactions", args) + clientCall(ipcEndpoint, &result, "txpool_getTransactions", args) printJSONList(result) }, } + +var getTxsByAccountCmd = &cobra.Command{ + Use: "gettxsbyname ", + Short: "Returns the transaction for the given account", + Long: `Returns the transaction for the given account`, + Args: cobra.RangeArgs(1, 2), + Run: func(cmd *cobra.Command, args []string) { + var ( + result interface{} + fullTx bool + ) + if len(args) > 1 { + fullTx = parseBool(args[1]) + } + + clientCall(ipcEndpoint, &result, "txpool_getTransactionsByAccount", args[0], fullTx) + printJSON(result) + }, +} + var getPendingTxsCmd = &cobra.Command{ Use: "getpending ", Short: "Returns the pending transactions that are in the transaction pool", @@ -88,6 +116,6 @@ var getPendingTxsCmd = &cobra.Command{ func init() { RootCmd.AddCommand(txpoolCommand) - txpoolCommand.AddCommand(contentCmd, statusCmd, setGasPriceCmd, getTxCmd, getPendingTxsCmd) + txpoolCommand.AddCommand(contentCmd, statusCmd, setGasPriceCmd, getTxsCmd, getTxsByAccountCmd, getPendingTxsCmd) txpoolCommand.PersistentFlags().StringVarP(&ipcEndpoint, "ipcpath", "i", defaultIPCEndpoint(params.ClientIdentifier), "IPC Endpoint path") } diff --git a/rpcapi/txpool.go b/rpcapi/txpool.go index 40b80333..4f1d1598 100644 --- a/rpcapi/txpool.go +++ b/rpcapi/txpool.go @@ -79,7 +79,7 @@ func (s *PrivateTxPoolAPI) Content(fullTx bool) interface{} { return content } -// PendingTransactions returns the pending transactions that are in the transaction pool +// PendingTransactions returns the pending transactions that are in the transaction pool. func (s *PrivateTxPoolAPI) PendingTransactions(fullTx bool) (interface{}, error) { pending, err := s.b.TxPool().Pending() if err != nil { @@ -106,8 +106,8 @@ func (s *PrivateTxPoolAPI) PendingTransactions(fullTx bool) (interface{}, error) return txsHashes, nil } -// GetPoolTransactions txpool returns the transaction for the given hash -func (s *PrivateTxPoolAPI) GetPoolTransactions(hashes []common.Hash) []*types.RPCTransaction { +// GetTransactions txpool returns the transaction by the given hash. +func (s *PrivateTxPoolAPI) GetTransactions(hashes []common.Hash) []*types.RPCTransaction { var txs []*types.RPCTransaction for _, hash := range hashes { if tx := s.b.TxPool().Get(hash); tx != nil { @@ -117,6 +117,36 @@ func (s *PrivateTxPoolAPI) GetPoolTransactions(hashes []common.Hash) []*types.RP return txs } +// GetTransactionsByAccount txpool returns the transaction by the given account name. +func (s *PrivateTxPoolAPI) GetTransactionsByAccount(name common.Name, fullTx bool) interface{} { + content := map[string]map[string]interface{}{ + "pending": make(map[string]interface{}), + "queued": make(map[string]interface{}), + } + + txsFunc := func(name common.Name, m map[common.Name][]*types.Transaction, fullTx bool) map[string]interface{} { + dump := make(map[string]interface{}) + txs, ok := m[name] + if ok { + for _, tx := range txs { + if fullTx { + dump[fmt.Sprintf("%d", tx.GetActions()[0].Nonce())] = tx.NewRPCTransaction(common.Hash{}, 0, 0) + } else { + dump[fmt.Sprintf("%d", tx.GetActions()[0].Nonce())] = tx.Hash() + } + } + } + return dump + } + + pending, queue := s.b.TxPool().Content() + + content["pending"] = txsFunc(name, pending, fullTx) + content["queued"] = txsFunc(name, queue, fullTx) + + return content +} + // SetGasPrice set txpool gas price func (s *PrivateTxPoolAPI) SetGasPrice(gasprice *big.Int) bool { return s.b.SetGasPrice(gasprice)