Fixing RPC modules compatibility

This commit is contained in:
Tomasz Drwięga 2016-05-04 14:03:29 +02:00
parent 58fd0175bf
commit c3f33aefdd
6 changed files with 48 additions and 10 deletions

1
Cargo.lock generated
View File

@ -348,6 +348,7 @@ dependencies = [
"libc 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"mio 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"nix 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
"rocksdb 0.4.3 (git+https://github.com/arkpar/rust-rocksdb.git)",
"rust-crypto 0.2.35 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -112,7 +112,7 @@ pub fn setup_rpc_server(
server.add_delegate(PersonalClient::new(&deps.secret_store).to_delegate())
},
"ethcore" => {
// not adding to modules, since `ethcore` is not supported in geth
modules.insert("ethcore".to_owned(), "1.0".to_owned());
server.add_delegate(EthcoreClient::new(&deps.miner, deps.logger.clone(), deps.settings.clone()).to_delegate())
},
"traces" => {

View File

@ -22,23 +22,41 @@ use v1::traits::Rpc;
/// RPC generic methods implementation.
pub struct RpcClient {
modules: BTreeMap<String, String>,
valid_apis: Vec<String>,
}
impl RpcClient {
/// Creates new `RpcClient`.
pub fn new(modules: BTreeMap<String, String>) -> Self {
// geth 1.3.6 fails upon receiving unknown api
let valid_apis = vec!["web3", "eth", "net", "personal", "rpc"];
RpcClient {
modules: modules
modules: modules,
valid_apis: valid_apis.into_iter().map(|x| x.to_owned()).collect(),
}
}
}
impl Rpc for RpcClient {
fn rpc_modules(&self, _: Params) -> Result<Value, Error> {
let modules = self.modules.iter()
.fold(BTreeMap::new(), |mut map, (k, v)| {
map.insert(k.to_owned(), Value::String(v.to_owned()));
map
});
Ok(Value::Object(modules))
}
fn modules(&self, _: Params) -> Result<Value, Error> {
let modules = self.modules.iter().fold(BTreeMap::new(), |mut map, (k, v)| {
map.insert(k.to_owned(), Value::String(v.to_owned()));
map
});
let modules = self.modules.iter()
.filter(|&(k, _v)| {
self.valid_apis.contains(k)
})
.fold(BTreeMap::new(), |mut map, (k, v)| {
map.insert(k.to_owned(), Value::String(v.to_owned()));
map
});
Ok(Value::Object(modules))
}
}

View File

@ -27,3 +27,5 @@ mod web3;
mod personal;
#[cfg(test)]
mod ethcore;
#[cfg(test)]
mod rpc;

View File

@ -22,17 +22,31 @@ use v1::{Rpc, RpcClient};
fn rpc_client() -> RpcClient {
let mut modules = BTreeMap::new();
modules.insert("rpc".to_owned(), "1.0".to_owned());
modules.insert("web3".to_owned(), "1.0".to_owned());
modules.insert("ethcore".to_owned(), "1.0".to_owned());
RpcClient::new(modules)
}
#[test]
fn modules() {
let rpc = rpc_client().to_delegate();
let io = IoHandler::new();
io.add_delegate(rpc);
let request = r#"{"jsonrpc": "2.0", "method": "modules", "params": [], "id": 1}"#;
let response = r#"{"jsonrpc":"2.0","result":{"rpc":"1.0","web3":"1.0"},"id":1}"#;
assert_eq!(io.handle_request(request), Some(response.to_owned()));
}
#[test]
fn rpc_modules() {
let rpc = rpc_client().to_delegate();
let io = IoHandler::new();
io.add_delegate(rpc);
let request = r#"{"jsonrpc": "2.0", "method": "modules", "params": [], "id": 1}"#;
let response = r#"{"jsonrpc":"2.0","result":{"eth": "1.0"},"id":1}"#;
let request = r#"{"jsonrpc": "2.0", "method": "rpc_modules", "params": [], "id": 1}"#;
let response = r#"{"jsonrpc":"2.0","result":{"ethcore":"1.0","rpc":"1.0","web3":"1.0"},"id":1}"#;
assert_eq!(io.handle_request(request), Some(response.to_owned()));
}

View File

@ -22,16 +22,19 @@ use jsonrpc_core::*;
/// RPC Interface.
pub trait Rpc: Sized + Send + Sync + 'static {
/// Returns supported modules.
/// Returns supported modules for Geth 1.3.6
fn modules(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Returns supported modules for Geth 1.4.0
fn rpc_modules(&self, _: Params) -> Result<Value, Error> { rpc_unimplemented!() }
/// Should be used to convert object to io delegate.
fn to_delegate(self) -> IoDelegate<Self> {
let mut delegate = IoDelegate::new(Arc::new(self));
// Geth 1.3.6 compatibility
delegate.add_method("modules", Rpc::modules);
// Geth 1.4.0 compatibility
delegate.add_method("rpc_modules", Rpc::modules);
delegate.add_method("rpc_modules", Rpc::rpc_modules);
delegate
}
}