github.com/jeffallen/go-ethereum@v1.1.4-0.20150910155051-571d3236c49c/rpc/api/txpool.go (about)

     1  // Copyright 2015 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package api
    18  
    19  import (
    20  	"github.com/ethereum/go-ethereum/eth"
    21  	"github.com/ethereum/go-ethereum/rpc/codec"
    22  	"github.com/ethereum/go-ethereum/rpc/shared"
    23  	"github.com/ethereum/go-ethereum/xeth"
    24  )
    25  
    26  const (
    27  	TxPoolApiVersion = "1.0"
    28  )
    29  
    30  var (
    31  	// mapping between methods and handlers
    32  	txpoolMapping = map[string]txpoolhandler{
    33  		"txpool_status": (*txPoolApi).Status,
    34  	}
    35  )
    36  
    37  // net callback handler
    38  type txpoolhandler func(*txPoolApi, *shared.Request) (interface{}, error)
    39  
    40  // txpool api provider
    41  type txPoolApi struct {
    42  	xeth     *xeth.XEth
    43  	ethereum *eth.Ethereum
    44  	methods  map[string]txpoolhandler
    45  	codec    codec.ApiCoder
    46  }
    47  
    48  // create a new txpool api instance
    49  func NewTxPoolApi(xeth *xeth.XEth, eth *eth.Ethereum, coder codec.Codec) *txPoolApi {
    50  	return &txPoolApi{
    51  		xeth:     xeth,
    52  		ethereum: eth,
    53  		methods:  txpoolMapping,
    54  		codec:    coder.New(nil),
    55  	}
    56  }
    57  
    58  // collection with supported methods
    59  func (self *txPoolApi) Methods() []string {
    60  	methods := make([]string, len(self.methods))
    61  	i := 0
    62  	for k := range self.methods {
    63  		methods[i] = k
    64  		i++
    65  	}
    66  	return methods
    67  }
    68  
    69  // Execute given request
    70  func (self *txPoolApi) Execute(req *shared.Request) (interface{}, error) {
    71  	if callback, ok := self.methods[req.Method]; ok {
    72  		return callback(self, req)
    73  	}
    74  
    75  	return nil, shared.NewNotImplementedError(req.Method)
    76  }
    77  
    78  func (self *txPoolApi) Name() string {
    79  	return shared.TxPoolApiName
    80  }
    81  
    82  func (self *txPoolApi) ApiVersion() string {
    83  	return TxPoolApiVersion
    84  }
    85  
    86  func (self *txPoolApi) Status(req *shared.Request) (interface{}, error) {
    87  	pending, queue := self.ethereum.TxPool().Stats()
    88  	return map[string]int{
    89  		"pending": pending,
    90  		"queued":  queue,
    91  	}, nil
    92  }