From 932485f7e7ead65792c1c688912f948e3c2f71cf Mon Sep 17 00:00:00 2001 From: Bas van Kervel Date: Fri, 5 Jun 2015 07:56:04 +0200 Subject: [PATCH] added net api --- cmd/console/js.go | 8 +++++++- rpc/api/admin.go | 36 +++++++++++++++++++++++++++++++++++- rpc/api/admin_js.go | 5 +++++ rpc/api/api.go | 2 +- rpc/api/net.go | 12 ++++++++++-- rpc/api/net_js.go | 14 +++++++------- 6 files changed, 65 insertions(+), 12 deletions(-) create mode 100644 rpc/api/admin_js.go diff --git a/cmd/console/js.go b/cmd/console/js.go index 243b527b4e..a1908a789c 100644 --- a/cmd/console/js.go +++ b/cmd/console/js.go @@ -128,6 +128,11 @@ func (js *jsre) apiBindings(ipcpath string) { utils.Fatalf("Error loading miner.js: %v", err) } + err = js.re.Compile("net.js", api.Net_JS) + if err != nil { + utils.Fatalf("Error loading net.js: %v", err) + } + _, err = js.re.Eval("web3.setProvider(jeth)") if err != nil { utils.Fatalf("Error setting web3 provider: %v", err) @@ -136,8 +141,9 @@ func (js *jsre) apiBindings(ipcpath string) { var eth = web3.eth; var shh = web3.shh; var db = web3.db; -var net = web3.net; +var net = web3.network; `) + if err != nil { utils.Fatalf("Error setting namespaces: %v", err) } diff --git a/rpc/api/admin.go b/rpc/api/admin.go index b1b2aa9374..4b9c665b8d 100644 --- a/rpc/api/admin.go +++ b/rpc/api/admin.go @@ -13,10 +13,44 @@ const ( var ( // mapping between methods and handlers AdminMapping = map[string]adminhandler{ - //"admin_startMiner": (*admin).StartMiner, +// "admin_startRPC": (*admin).StartRPC, +// "admin_startRPC": (*admin).StopRPC, +// "admin_startRPC": (*admin).NodeInfo, +// "admin_startRPC": (*admin).StartRPC, +// "admin_startRPC": (*admin).StartRPC, +// "admin_startRPC": (*admin).StartRPC, +// "admin_startRPC": (*admin).StartRPC, +// "admin_startRPC": (*admin).StartRPC, +// "admin_startRPC": (*admin).StartRPC, +// "admin_startRPC": (*admin).StartRPC, +// "admin_startRPC": (*admin).StartRPC, +// "admin_startRPC": (*admin).StartRPC, } ) +/* +addPeer: [Function], + startRPC: [Function], + stopRPC: [Function], + nodeInfo: [Function], + peers: [Function], + newAccount: [Function], + unlock: [Function], + import: [Function], + export: [Function], + verbosity: [Function], + progress: [Function], + setSolc: [Function], + contractInfo: { + start: [Function], + stop: [Function], + newRegistry: [Function], + get: [Function], + register: [Function], + registerUrl: [Function] + }, + */ + // admin callback handler type adminhandler func(*admin, *shared.Request) (interface{}, error) diff --git a/rpc/api/admin_js.go b/rpc/api/admin_js.go new file mode 100644 index 0000000000..fd286ada90 --- /dev/null +++ b/rpc/api/admin_js.go @@ -0,0 +1,5 @@ +package api + +const Admin_JS = ` + +` \ No newline at end of file diff --git a/rpc/api/api.go b/rpc/api/api.go index c1d6f4c41e..affcde12fc 100644 --- a/rpc/api/api.go +++ b/rpc/api/api.go @@ -77,7 +77,7 @@ func ParseApiString(apiString string, codec codec.Codec, eth *e.Ethereum) ([]Eth case "miner": apis[i] = NewMiner(eth, codec) case "net": - apis[i] = NewNet(xeth, codec) + apis[i] = NewNet(xeth, eth, codec) case "web3": apis[i] = NewWeb3(xeth, codec) default: diff --git a/rpc/api/net.go b/rpc/api/net.go index 465dcc2ef3..0bc3ef3283 100644 --- a/rpc/api/net.go +++ b/rpc/api/net.go @@ -4,6 +4,7 @@ import ( "github.com/ethereum/go-ethereum/rpc/codec" "github.com/ethereum/go-ethereum/rpc/shared" "github.com/ethereum/go-ethereum/xeth" + e "github.com/ethereum/go-ethereum/eth" ) var ( @@ -12,6 +13,7 @@ var ( "net_version": (*net).Version, "net_peerCount": (*net).PeerCount, "net_listening": (*net).IsListening, + "net_peers": (*net).Peers, } ) @@ -21,14 +23,16 @@ type nethandler func(*net, *shared.Request) (interface{}, error) // net api provider type net struct { xeth *xeth.XEth + ethereum *e.Ethereum methods map[string]nethandler codec codec.ApiCoder } // create a new net api instance -func NewNet(xeth *xeth.XEth, coder codec.Codec) *net { +func NewNet(xeth *xeth.XEth, eth *e.Ethereum, coder codec.Codec) *net { return &net{ xeth: xeth, + ethereum: eth, methods: netMapping, codec: coder.New(nil), } @@ -65,9 +69,13 @@ func (self *net) Version(req *shared.Request) (interface{}, error) { // Number of connected peers func (self *net) PeerCount(req *shared.Request) (interface{}, error) { - return newHexNum(self.xeth.PeerCount()), nil + return self.xeth.PeerCount(), nil } func (self *net) IsListening(req *shared.Request) (interface{}, error) { return self.xeth.IsListening(), nil } + +func (self *net) Peers(req *shared.Request) (interface{}, error) { + return self.ethereum.PeersInfo(), nil +} diff --git a/rpc/api/net_js.go b/rpc/api/net_js.go index 0e46629218..0a0c831cdb 100644 --- a/rpc/api/net_js.go +++ b/rpc/api/net_js.go @@ -2,7 +2,7 @@ package api const Net_JS = ` web3.extend({ - property: 'miner', + property: 'network', methods: [ new web3.extend.Method({ @@ -13,18 +13,18 @@ web3.extend({ outputFormatter: web3.extend.formatters.formatOutputString }), new web3.extend.Method({ - name: 'stop', - call: 'net_getPeerCount', + name: 'getPeerCount', + call: 'net_peerCount', params: 0, inputFormatter: [], outputFormatter: web3.extend.formatters.formatOutputString }), new web3.extend.Method({ - name: 'stop', - call: 'miner_stop', + name: 'peers', + call: 'net_peers', params: 0, inputFormatter: [], - outputFormatter: web3.extend.formatters.formatOutputBool + outputFormatter: function(obj) { return obj; } }) ], properties: @@ -36,7 +36,7 @@ web3.extend({ }), new web3.extend.Property({ name: 'peerCount', - getter: 'net_getPeerCount', + getter: 'net_peerCount', outputFormatter: web3.extend.utils.toDecimal }) ]