Skip to content

Commit

Permalink
add txpool cmd gettxsbyaccount (fractalplatform#502)
Browse files Browse the repository at this point in the history
  • Loading branch information
erickyan86 authored Sep 29, 2019
1 parent d03c92f commit 1c91ea1
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 10 deletions.
42 changes: 35 additions & 7 deletions cmd/ft/txpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,17 @@ var contentCmd = &cobra.Command{
Use: "content <fullTx bool>",
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)
},
}
Expand Down Expand Up @@ -64,16 +71,37 @@ var setGasPriceCmd = &cobra.Command{
},
}

var getTxCmd = &cobra.Command{
Use: "gettx <txhashes string array> ",
var getTxsCmd = &cobra.Command{
Use: "gettxs <txhashes string array> ",
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 <account string> <fullTx bool> ",
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 <fullTx bool>",
Short: "Returns the pending transactions that are in the transaction pool",
Expand All @@ -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")
}
36 changes: 33 additions & 3 deletions rpcapi/txpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit 1c91ea1

Please sign in to comment.