Merge pull request #1041 from ethcore/rpc_modules
Fixing RPC modules compatibility
This commit is contained in:
commit
0b1442faa2
@ -109,7 +109,7 @@ fn setup_rpc_server(apis: Vec<&str>, deps: &Arc<Dependencies>) -> 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" => {
|
||||
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
@ -27,3 +27,5 @@ mod web3;
|
||||
mod personal;
|
||||
#[cfg(test)]
|
||||
mod ethcore;
|
||||
#[cfg(test)]
|
||||
mod rpc;
|
||||
|
@ -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()));
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user