github.com/klaytn/klaytn@v1.12.1/api/api_public_tx_pool.go (about)

     1  // Modifications Copyright 2019 The klaytn Authors
     2  // Copyright 2015 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from internal/ethapi/api.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package api
    22  
    23  import (
    24  	"fmt"
    25  
    26  	"github.com/klaytn/klaytn/blockchain/types"
    27  	"github.com/klaytn/klaytn/common/hexutil"
    28  )
    29  
    30  // PublicTxPoolAPI offers and API for the transaction pool. It only operates on data that is non confidential.
    31  type PublicTxPoolAPI struct {
    32  	b Backend
    33  }
    34  
    35  // NewPublicTxPoolAPI creates a new tx pool service that gives information about the transaction pool.
    36  func NewPublicTxPoolAPI(b Backend) *PublicTxPoolAPI {
    37  	return &PublicTxPoolAPI{b}
    38  }
    39  
    40  // Content returns the transactions contained within the transaction pool.
    41  func (s *PublicTxPoolAPI) Content() map[string]map[string]map[string]map[string]interface{} {
    42  	content := map[string]map[string]map[string]map[string]interface{}{
    43  		"pending": make(map[string]map[string]map[string]interface{}),
    44  		"queued":  make(map[string]map[string]map[string]interface{}),
    45  	}
    46  	pending, queue := s.b.TxPoolContent()
    47  
    48  	// Flatten the pending transactions
    49  	for account, txs := range pending {
    50  		dump := make(map[string]map[string]interface{})
    51  		for _, tx := range txs {
    52  			dump[fmt.Sprintf("%d", tx.Nonce())] = newRPCPendingTransaction(tx)
    53  		}
    54  		content["pending"][account.Hex()] = dump
    55  	}
    56  	// Flatten the queued transactions
    57  	for account, txs := range queue {
    58  		dump := make(map[string]map[string]interface{})
    59  		for _, tx := range txs {
    60  			dump[fmt.Sprintf("%d", tx.Nonce())] = newRPCPendingTransaction(tx)
    61  		}
    62  		content["queued"][account.Hex()] = dump
    63  	}
    64  	return content
    65  }
    66  
    67  // Status returns the number of pending and queued transaction in the pool.
    68  func (s *PublicTxPoolAPI) Status() map[string]hexutil.Uint {
    69  	pending, queue := s.b.Stats()
    70  	return map[string]hexutil.Uint{
    71  		"pending": hexutil.Uint(pending),
    72  		"queued":  hexutil.Uint(queue),
    73  	}
    74  }
    75  
    76  // Inspect retrieves the content of the transaction pool and flattens it into an
    77  // easily inspectable list.
    78  func (s *PublicTxPoolAPI) Inspect() map[string]map[string]map[string]string {
    79  	content := map[string]map[string]map[string]string{
    80  		"pending": make(map[string]map[string]string),
    81  		"queued":  make(map[string]map[string]string),
    82  	}
    83  	pending, queue := s.b.TxPoolContent()
    84  
    85  	// Define a formatter to flatten a transaction into a string
    86  	format := func(tx *types.Transaction) string {
    87  		if to := tx.To(); to != nil {
    88  			return fmt.Sprintf("%s: %v peb + %v gas × %v peb", tx.To().Hex(), tx.Value(), tx.Gas(), tx.GasPrice())
    89  		}
    90  		return fmt.Sprintf("contract creation: %v peb + %v gas × %v peb", tx.Value(), tx.Gas(), tx.GasPrice())
    91  	}
    92  	// Flatten the pending transactions
    93  	for account, txs := range pending {
    94  		dump := make(map[string]string)
    95  		for _, tx := range txs {
    96  			dump[fmt.Sprintf("%d", tx.Nonce())] = format(tx)
    97  		}
    98  		content["pending"][account.Hex()] = dump
    99  	}
   100  	// Flatten the queued transactions
   101  	for account, txs := range queue {
   102  		dump := make(map[string]string)
   103  		for _, tx := range txs {
   104  			dump[fmt.Sprintf("%d", tx.Nonce())] = format(tx)
   105  		}
   106  		content["queued"][account.Hex()] = dump
   107  	}
   108  	return content
   109  }