github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/app/rpc/namespaces/eth/txpool/api.go (about) 1 package txpool 2 3 import ( 4 "fmt" 5 6 "github.com/ethereum/go-ethereum/common/hexutil" 7 clientcontext "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/client/context" 8 "github.com/fibonacci-chain/fbc/x/evm/watcher" 9 10 "github.com/fibonacci-chain/fbc/app/rpc/backend" 11 "github.com/fibonacci-chain/fbc/libs/tendermint/libs/log" 12 ) 13 14 // PublicTxPoolAPI offers and API for the transaction pool. It only operates on data that is non confidential. 15 type PublicTxPoolAPI struct { 16 clientCtx clientcontext.CLIContext 17 logger log.Logger 18 backend backend.Backend 19 } 20 21 // NewPublicTxPoolAPI creates a new tx pool service that gives information about the transaction pool. 22 func NewAPI(clientCtx clientcontext.CLIContext, log log.Logger, backend backend.Backend) *PublicTxPoolAPI { 23 api := &PublicTxPoolAPI{ 24 clientCtx: clientCtx, 25 backend: backend, 26 logger: log.With("module", "json-rpc", "namespace", "txpool"), 27 } 28 return api 29 } 30 31 // Content returns the transactions contained within the transaction pool. 32 func (s *PublicTxPoolAPI) Content() map[string]map[string]map[string]*watcher.Transaction { 33 addressList, err := s.backend.PendingAddressList() 34 if err != nil { 35 s.logger.Error("txpool.Content addressList err: ", err) 36 } 37 content := map[string]map[string]map[string]*watcher.Transaction{ 38 "queued": make(map[string]map[string]*watcher.Transaction), 39 } 40 41 for _, address := range addressList { 42 txs, err := s.backend.UserPendingTransactions(address, -1) 43 if err != nil { 44 s.logger.Error("txpool.Content err: ", err) 45 } 46 47 // Flatten the queued transactions 48 dump := make(map[string]*watcher.Transaction) 49 for _, tx := range txs { 50 dump[fmt.Sprintf("%d", tx.Nonce)] = tx 51 } 52 content["queued"][address] = dump 53 } 54 55 return content 56 } 57 58 // Status returns the number of pending and queued transaction in the pool. 59 func (s *PublicTxPoolAPI) Status() map[string]hexutil.Uint { 60 numRes, err := s.backend.PendingTransactionCnt() 61 if err != nil { 62 s.logger.Error("txpool.Status err: ", err) 63 return nil 64 } 65 return map[string]hexutil.Uint{ 66 "queued": hexutil.Uint(numRes), 67 } 68 } 69 70 // Inspect retrieves the content of the transaction pool and flattens it into an 71 // easily inspectable list. 72 func (s *PublicTxPoolAPI) Inspect() map[string]map[string]map[string]string { 73 addressList, err := s.backend.PendingAddressList() 74 if err != nil { 75 s.logger.Error("txpool.Inspect addressList err: ", err) 76 } 77 content := map[string]map[string]map[string]string{ 78 "queued": make(map[string]map[string]string), 79 } 80 for _, address := range addressList { 81 txs, err := s.backend.UserPendingTransactions(address, -1) 82 if err != nil { 83 s.logger.Error("txpool.Inspect err: ", err) 84 } 85 86 // Define a formatter to flatten a transaction into a string 87 var format = func(tx *watcher.Transaction) string { 88 if to := tx.To; to != nil { 89 return fmt.Sprintf("%s: %v wei + %v gas × %v wei", tx.To.Hex(), tx.Value, tx.Gas, tx.GasPrice) 90 } 91 return fmt.Sprintf("contract creation: %v wei + %v gas × %v wei", tx.Value, tx.Gas, tx.GasPrice) 92 } 93 94 // Flatten the queued transactions 95 dump := make(map[string]string) 96 for _, tx := range txs { 97 dump[fmt.Sprintf("%d", tx.Nonce)] = format(tx) 98 } 99 content["queued"][address] = dump 100 } 101 102 return content 103 }