inital commit with eth_blockNumber working
This commit is contained in:
parent
7cb8cbe056
commit
82373ab7a4
@ -20,10 +20,13 @@ time = "0.1"
|
|||||||
evmjit = { path = "rust-evmjit", optional = true }
|
evmjit = { path = "rust-evmjit", optional = true }
|
||||||
ethash = { path = "ethash" }
|
ethash = { path = "ethash" }
|
||||||
num_cpus = "0.2"
|
num_cpus = "0.2"
|
||||||
|
jsonrpc-core = { version = "1.0", optional = true }
|
||||||
|
jsonrpc-http-server = { version = "1.0", optional = true }
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
jit = ["evmjit"]
|
jit = ["evmjit"]
|
||||||
evm_debug = []
|
evm_debug = []
|
||||||
|
rpc = ["jsonrpc-core", "jsonrpc-http-server"]
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "client"
|
name = "client"
|
||||||
|
53
src/bin/client/ethrpc.rs
Normal file
53
src/bin/client/ethrpc.rs
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
extern crate jsonrpc_core;
|
||||||
|
extern crate jsonrpc_http_server;
|
||||||
|
|
||||||
|
use std::sync::{Arc, RwLock};
|
||||||
|
use self::jsonrpc_core::{IoHandler, IoDelegate, Params, Value, Error, ErrorCode};
|
||||||
|
use ethcore::client::*;
|
||||||
|
|
||||||
|
struct Eth {
|
||||||
|
client: Arc<RwLock<Client>>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Eth {
|
||||||
|
fn new(client: Arc<RwLock<Client>>) -> Self {
|
||||||
|
Eth {
|
||||||
|
client: client
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn block_number(&self, params: Params) -> Result<Value, Error> {
|
||||||
|
match params {
|
||||||
|
Params::None => Ok(Value::U64(self.client.read().unwrap().chain_info().best_block_number)),
|
||||||
|
_ => Err(Error::new(ErrorCode::InvalidParams)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct EthRpc;
|
||||||
|
|
||||||
|
impl EthRpc {
|
||||||
|
fn build_handler(client: Arc<RwLock<Client>>) -> IoHandler {
|
||||||
|
let mut handler = IoHandler::new();
|
||||||
|
let mut eth = IoDelegate::new(Arc::new(Eth::new(client)));
|
||||||
|
eth.add_method("eth_blockNumber", Eth::block_number);
|
||||||
|
handler.add_delegate(eth);
|
||||||
|
handler
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct HttpServer {
|
||||||
|
server: jsonrpc_http_server::Server
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HttpServer {
|
||||||
|
pub fn new(client: Arc<RwLock<Client>>, threads: usize) -> HttpServer {
|
||||||
|
HttpServer {
|
||||||
|
server: jsonrpc_http_server::Server::new(EthRpc::build_handler(client), threads)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn start_async(self, addr: &str) {
|
||||||
|
self.server.start_async(addr)
|
||||||
|
}
|
||||||
|
}
|
@ -4,6 +4,9 @@ extern crate rustc_serialize;
|
|||||||
extern crate log;
|
extern crate log;
|
||||||
extern crate env_logger;
|
extern crate env_logger;
|
||||||
|
|
||||||
|
#[cfg(feature = "rpc")]
|
||||||
|
mod ethrpc;
|
||||||
|
|
||||||
use std::io::stdin;
|
use std::io::stdin;
|
||||||
use std::env;
|
use std::env;
|
||||||
use log::{LogLevelFilter};
|
use log::{LogLevelFilter};
|
||||||
@ -26,10 +29,22 @@ fn setup_log() {
|
|||||||
builder.init().unwrap();
|
builder.init().unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[cfg(feature = "rpc")]
|
||||||
|
fn setup_rpc_server(client: Arc<RwLock<Client>>) {
|
||||||
|
let server = ethrpc::HttpServer::new(client, 1);
|
||||||
|
server.start_async("127.0.0.1:3030");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(not(feature = "rpc"))]
|
||||||
|
fn setup_rpc_server(_client: Arc<RwLock<Client>>) {
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
setup_log();
|
setup_log();
|
||||||
let spec = ethereum::new_frontier();
|
let spec = ethereum::new_frontier();
|
||||||
let mut service = ClientService::start(spec).unwrap();
|
let mut service = ClientService::start(spec).unwrap();
|
||||||
|
setup_rpc_server(service.client());
|
||||||
let io_handler = Box::new(ClientIoHandler { client: service.client(), timer: 0, info: Default::default() });
|
let io_handler = Box::new(ClientIoHandler { client: service.client(), timer: 0, info: Default::default() });
|
||||||
service.io().register_handler(io_handler).expect("Error registering IO handler");
|
service.io().register_handler(io_handler).expect("Error registering IO handler");
|
||||||
loop {
|
loop {
|
||||||
|
Loading…
Reference in New Issue
Block a user