added txpool API

This commit is contained in:
Bas van Kervel 2015-06-10 10:37:10 +02:00 committed by Bas van Kervel
parent c6c443385b
commit 87b62f75a7
4 changed files with 91 additions and 1 deletions

View File

@ -13,6 +13,7 @@ const (
MergedApiName = "merged"
MinerApiName = "miner"
NetApiName = "net"
txPoolApiName = "txpool"
PersonalApiName = "personal"
Web3ApiName = "web3"
)
@ -20,7 +21,7 @@ const (
var (
// List with all API's which are offered over the IPC interface by default
DefaultIpcApis = strings.Join([]string{
AdminApiName, EthApiName, DebugApiName, MinerApiName, NetApiName, PersonalApiName, Web3ApiName,
AdminApiName, EthApiName, DebugApiName, MinerApiName, NetApiName, txPoolApiName, PersonalApiName, Web3ApiName,
}, ",")
)

67
rpc/api/txpool.go Normal file
View File

@ -0,0 +1,67 @@
package api
import (
"github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/rpc/codec"
"github.com/ethereum/go-ethereum/rpc/shared"
"github.com/ethereum/go-ethereum/xeth"
)
var (
// mapping between methods and handlers
txpoolMapping = map[string]txpoolhandler{
"txpool_status": (*txPoolApi).Status,
}
)
// net callback handler
type txpoolhandler func(*txPoolApi, *shared.Request) (interface{}, error)
// txpool api provider
type txPoolApi struct {
xeth *xeth.XEth
ethereum *eth.Ethereum
methods map[string]txpoolhandler
codec codec.ApiCoder
}
// create a new txpool api instance
func NewTxPoolApi(xeth *xeth.XEth, eth *eth.Ethereum, coder codec.Codec) *txPoolApi {
return &txPoolApi{
xeth: xeth,
ethereum: eth,
methods: txpoolMapping,
codec: coder.New(nil),
}
}
// collection with supported methods
func (self *txPoolApi) Methods() []string {
methods := make([]string, len(self.methods))
i := 0
for k := range self.methods {
methods[i] = k
i++
}
return methods
}
// Execute given request
func (self *txPoolApi) Execute(req *shared.Request) (interface{}, error) {
if callback, ok := self.methods[req.Method]; ok {
return callback(self, req)
}
return nil, shared.NewNotImplementedError(req.Method)
}
func (self *txPoolApi) Name() string {
return txPoolApiName
}
func (self *txPoolApi) Status(req *shared.Request) (interface{}, error) {
return map[string]int{
"pending": self.ethereum.TxPool().GetTransactions().Len(),
"queued": self.ethereum.TxPool().GetQueuedTransactions().Len(),
}, nil
}

18
rpc/api/txpool_js.go Normal file
View File

@ -0,0 +1,18 @@
package api
const TxPool_JS = `
web3._extend({
property: 'txpool',
methods:
[
],
properties:
[
new web3._extend.Property({
name: 'status',
getter: 'txpool_status',
outputFormatter: function(obj) { return obj; }
})
]
});
`

View File

@ -31,6 +31,8 @@ func ParseApiString(apistr string, codec codec.Codec, xeth *xeth.XEth, eth *eth.
apis[i] = NewMinerApi(eth, codec)
case NetApiName:
apis[i] = NewNetApi(xeth, eth, codec)
case txPoolApiName:
apis[i] = NewTxPoolApi(xeth, eth, codec)
case PersonalApiName:
apis[i] = NewPersonalApi(xeth, eth, codec)
case Web3ApiName:
@ -53,6 +55,8 @@ func Javascript(name string) string {
return Miner_JS
case NetApiName:
return Net_JS
case txPoolApiName:
return TxPool_JS
case PersonalApiName:
return Personal_JS
}