rpc net methods returns real peer count && protocol version

This commit is contained in:
debris 2016-01-28 10:54:49 +01:00
parent 7d327298dd
commit 07d6965ce7
2 changed files with 16 additions and 8 deletions

View File

@ -53,19 +53,19 @@ fn setup_log(init: &str) {
#[cfg(feature = "rpc")]
fn setup_rpc_server(client: Arc<Client>) {
fn setup_rpc_server(client: Arc<Client>, sync: Arc<EthSync>) {
use rpc::v1::*;
let mut server = rpc::HttpServer::new(1);
server.add_delegate(Web3Client::new().to_delegate());
server.add_delegate(EthClient::new(client.clone()).to_delegate());
server.add_delegate(EthFilterClient::new(client).to_delegate());
server.add_delegate(NetClient::new().to_delegate());
server.add_delegate(NetClient::new(sync).to_delegate());
server.start_async("127.0.0.1:3030");
}
#[cfg(not(feature = "rpc"))]
fn setup_rpc_server(_client: Arc<Client>) {
fn setup_rpc_server(_client: Arc<Client>, _sync: Arc<EthSync>) {
}
fn main() {
@ -81,7 +81,7 @@ fn main() {
let mut net_settings = NetworkConfiguration::new();
net_settings.boot_nodes = init_nodes;
let mut service = ClientService::start(spec, net_settings).unwrap();
setup_rpc_server(service.client());
setup_rpc_server(service.client(), service.sync());
let io_handler = Arc::new(ClientIoHandler { client: service.client(), info: Default::default(), sync: service.sync() });
service.io().register_handler(io_handler).expect("Error registering IO handler");

View File

@ -1,21 +1,29 @@
//! Net rpc implementation.
use std::sync::Arc;
use jsonrpc_core::*;
use ethcore::sync::EthSync;
use v1::traits::Net;
/// Net rpc implementation.
pub struct NetClient;
pub struct NetClient {
sync: Arc<EthSync>
}
impl NetClient {
/// Creates new NetClient.
pub fn new() -> Self { NetClient }
pub fn new(sync: Arc<EthSync>) -> Self {
NetClient {
sync: sync
}
}
}
impl Net for NetClient {
fn version(&self, _: Params) -> Result<Value, Error> {
Ok(Value::U64(63))
Ok(Value::U64(self.sync.status().protocol_version as u64))
}
fn peer_count(&self, _params: Params) -> Result<Value, Error> {
Ok(Value::U64(0))
Ok(Value::U64(self.sync.status().num_peers as u64))
}
}