github.com/turingchain2020/turingchain@v1.1.21/system/dapp/commands/mempool.go (about)

     1  // Copyright Turing Corp. 2018 All Rights Reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package commands
     6  
     7  import (
     8  	"github.com/turingchain2020/turingchain/rpc/jsonclient"
     9  	rpctypes "github.com/turingchain2020/turingchain/rpc/types"
    10  	"github.com/turingchain2020/turingchain/system/dapp/commands/types"
    11  	ctypes "github.com/turingchain2020/turingchain/types"
    12  	"github.com/spf13/cobra"
    13  )
    14  
    15  // MempoolCmd mempool command
    16  func MempoolCmd() *cobra.Command {
    17  	cmd := &cobra.Command{
    18  		Use:   "mempool",
    19  		Short: "Mempool management",
    20  		Args:  cobra.MinimumNArgs(1),
    21  	}
    22  
    23  	cmd.AddCommand(
    24  		GetMempoolCmd(),
    25  		GetLastMempoolCmd(),
    26  		GetProperFeeCmd(),
    27  	)
    28  
    29  	return cmd
    30  }
    31  
    32  // GetMempoolCmd get mempool
    33  func GetMempoolCmd() *cobra.Command {
    34  	cmd := &cobra.Command{
    35  		Use:   "list",
    36  		Short: "List mempool txs",
    37  		Run:   listMempoolTxs,
    38  	}
    39  	addGetMempoolFlags(cmd)
    40  	return cmd
    41  }
    42  
    43  func addGetMempoolFlags(cmd *cobra.Command) {
    44  	cmd.Flags().BoolP("all", "a", false, "show all tx in mempool")
    45  }
    46  
    47  func listMempoolTxs(cmd *cobra.Command, args []string) {
    48  	rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
    49  	isAll, _ := cmd.Flags().GetBool("all")
    50  	params := &ctypes.ReqGetMempool{
    51  		IsAll: isAll,
    52  	}
    53  	var res rpctypes.ReplyTxList
    54  	ctx := jsonclient.NewRPCCtx(rpcLaddr, "Turingchain.GetMempool", params, &res)
    55  	ctx.SetResultCb(parseListMempoolTxsRes)
    56  	ctx.Run()
    57  }
    58  
    59  func parseListMempoolTxsRes(arg interface{}) (interface{}, error) {
    60  	res := arg.(*rpctypes.ReplyTxList)
    61  	var result types.TxListResult
    62  	for _, v := range res.Txs {
    63  		result.Txs = append(result.Txs, types.DecodeTransaction(v))
    64  	}
    65  	return result, nil
    66  }
    67  
    68  // GetLastMempoolCmd  get last 10 txs of mempool
    69  func GetLastMempoolCmd() *cobra.Command {
    70  	cmd := &cobra.Command{
    71  		Use:   "last_txs",
    72  		Short: "Get latest mempool txs",
    73  		Run:   lastMempoolTxs,
    74  	}
    75  	return cmd
    76  }
    77  
    78  func lastMempoolTxs(cmd *cobra.Command, args []string) {
    79  	rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
    80  	var res rpctypes.ReplyTxList
    81  	ctx := jsonclient.NewRPCCtx(rpcLaddr, "Turingchain.GetLastMemPool", nil, &res)
    82  	ctx.SetResultCb(parselastMempoolTxsRes)
    83  	ctx.Run()
    84  }
    85  
    86  func parselastMempoolTxsRes(arg interface{}) (interface{}, error) {
    87  	res := arg.(*rpctypes.ReplyTxList)
    88  	var result types.TxListResult
    89  	for _, v := range res.Txs {
    90  		result.Txs = append(result.Txs, types.DecodeTransaction(v))
    91  	}
    92  	return result, nil
    93  }
    94  
    95  // GetProperFeeCmd  get last proper fee
    96  func GetProperFeeCmd() *cobra.Command {
    97  	cmd := &cobra.Command{
    98  		Use:   "proper_fee",
    99  		Short: "Get latest proper fee",
   100  		Run:   properFee,
   101  	}
   102  	return cmd
   103  }
   104  
   105  func properFee(cmd *cobra.Command, args []string) {
   106  	rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
   107  	var res rpctypes.ReplyProperFee
   108  	ctx := jsonclient.NewRPCCtx(rpcLaddr, "Turingchain.GetProperFee", nil, &res)
   109  	ctx.SetResultCb(nil)
   110  	ctx.Run()
   111  }