diff --git a/rpc/api/api.go b/rpc/api/api.go index 28b824658..e870ec58e 100644 --- a/rpc/api/api.go +++ b/rpc/api/api.go @@ -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, }, ",") ) diff --git a/rpc/api/txpool.go b/rpc/api/txpool.go new file mode 100644 index 000000000..f340c501f --- /dev/null +++ b/rpc/api/txpool.go @@ -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 +} diff --git a/rpc/api/txpool_js.go b/rpc/api/txpool_js.go new file mode 100644 index 000000000..06528d1c4 --- /dev/null +++ b/rpc/api/txpool_js.go @@ -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; } + }) + ] +}); +` diff --git a/rpc/api/utils.go b/rpc/api/utils.go index 072abf883..b44a325a8 100644 --- a/rpc/api/utils.go +++ b/rpc/api/utils.go @@ -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 }