2016-06-01 19:37:34 +02:00
|
|
|
// Copyright 2015, 2016 Ethcore (UK) Ltd.
|
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
use std::collections::BTreeMap;
|
2016-07-25 16:09:47 +02:00
|
|
|
use std::collections::HashSet;
|
|
|
|
use std::cmp::PartialEq;
|
2016-06-01 19:37:34 +02:00
|
|
|
use std::str::FromStr;
|
|
|
|
use std::sync::Arc;
|
2016-07-25 16:09:47 +02:00
|
|
|
use util::RotatingLogger;
|
2016-06-01 19:57:34 +02:00
|
|
|
use ethcore::miner::{Miner, ExternalMiner};
|
2016-06-01 19:37:34 +02:00
|
|
|
use ethcore::client::Client;
|
2016-06-20 00:10:34 +02:00
|
|
|
use ethcore::account_provider::AccountProvider;
|
2016-07-25 16:09:47 +02:00
|
|
|
use ethsync::{ManageNetwork, SyncProvider};
|
2016-08-05 10:32:04 +02:00
|
|
|
use ethcore_rpc::{Extendable, NetworkSettings};
|
2016-06-01 19:37:34 +02:00
|
|
|
pub use ethcore_rpc::ConfirmationsQueue;
|
|
|
|
|
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
#[derive(Debug, PartialEq, Clone, Eq, Hash)]
|
2016-06-01 19:37:34 +02:00
|
|
|
pub enum Api {
|
|
|
|
Web3,
|
|
|
|
Net,
|
|
|
|
Eth,
|
|
|
|
Personal,
|
2016-06-07 22:52:48 +02:00
|
|
|
Signer,
|
2016-06-01 19:37:34 +02:00
|
|
|
Ethcore,
|
2016-06-07 22:52:48 +02:00
|
|
|
EthcoreSet,
|
2016-06-01 19:37:34 +02:00
|
|
|
Traces,
|
|
|
|
Rpc,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for Api {
|
2016-07-25 16:09:47 +02:00
|
|
|
type Err = String;
|
2016-06-01 19:37:34 +02:00
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
use self::Api::*;
|
|
|
|
|
|
|
|
match s {
|
|
|
|
"web3" => Ok(Web3),
|
|
|
|
"net" => Ok(Net),
|
|
|
|
"eth" => Ok(Eth),
|
|
|
|
"personal" => Ok(Personal),
|
2016-06-07 22:52:48 +02:00
|
|
|
"signer" => Ok(Signer),
|
2016-06-01 19:37:34 +02:00
|
|
|
"ethcore" => Ok(Ethcore),
|
2016-06-07 22:52:48 +02:00
|
|
|
"ethcore_set" => Ok(EthcoreSet),
|
2016-06-01 19:37:34 +02:00
|
|
|
"traces" => Ok(Traces),
|
|
|
|
"rpc" => Ok(Rpc),
|
2016-07-25 16:09:47 +02:00
|
|
|
api => Err(format!("Unknown api: {}", api))
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum ApiSet {
|
|
|
|
SafeContext,
|
|
|
|
UnsafeContext,
|
|
|
|
List(HashSet<Api>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for ApiSet {
|
|
|
|
fn default() -> Self {
|
|
|
|
ApiSet::UnsafeContext
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PartialEq for ApiSet {
|
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
self.list_apis() == other.list_apis()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for ApiSet {
|
|
|
|
type Err = String;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
s.split(',')
|
|
|
|
.map(Api::from_str)
|
|
|
|
.collect::<Result<_, _>>()
|
|
|
|
.map(ApiSet::List)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-01 19:37:34 +02:00
|
|
|
pub struct Dependencies {
|
2016-06-10 15:39:34 +02:00
|
|
|
pub signer_port: Option<u16>,
|
2016-06-01 19:37:34 +02:00
|
|
|
pub signer_queue: Arc<ConfirmationsQueue>,
|
|
|
|
pub client: Arc<Client>,
|
2016-07-16 15:51:06 +02:00
|
|
|
pub sync: Arc<SyncProvider>,
|
|
|
|
pub net: Arc<ManageNetwork>,
|
2016-06-20 00:10:34 +02:00
|
|
|
pub secret_store: Arc<AccountProvider>,
|
2016-06-01 19:37:34 +02:00
|
|
|
pub miner: Arc<Miner>,
|
|
|
|
pub external_miner: Arc<ExternalMiner>,
|
|
|
|
pub logger: Arc<RotatingLogger>,
|
|
|
|
pub settings: Arc<NetworkSettings>,
|
2016-07-11 17:02:42 +02:00
|
|
|
pub net_service: Arc<ManageNetwork>,
|
2016-08-03 15:31:00 +02:00
|
|
|
pub geth_compatibility: bool,
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn to_modules(apis: &[Api]) -> BTreeMap<String, String> {
|
|
|
|
let mut modules = BTreeMap::new();
|
|
|
|
for api in apis {
|
|
|
|
let (name, version) = match *api {
|
|
|
|
Api::Web3 => ("web3", "1.0"),
|
|
|
|
Api::Net => ("net", "1.0"),
|
|
|
|
Api::Eth => ("eth", "1.0"),
|
|
|
|
Api::Personal => ("personal", "1.0"),
|
2016-06-07 22:52:48 +02:00
|
|
|
Api::Signer => ("signer", "1.0"),
|
2016-06-01 19:37:34 +02:00
|
|
|
Api::Ethcore => ("ethcore", "1.0"),
|
2016-06-07 22:52:48 +02:00
|
|
|
Api::EthcoreSet => ("ethcore_set", "1.0"),
|
2016-06-01 19:37:34 +02:00
|
|
|
Api::Traces => ("traces", "1.0"),
|
|
|
|
Api::Rpc => ("rpc", "1.0"),
|
|
|
|
};
|
|
|
|
modules.insert(name.into(), version.into());
|
|
|
|
}
|
|
|
|
modules
|
|
|
|
}
|
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
impl ApiSet {
|
|
|
|
pub fn list_apis(&self) -> HashSet<Api> {
|
|
|
|
match *self {
|
|
|
|
ApiSet::List(ref apis) => apis.clone(),
|
|
|
|
ApiSet::UnsafeContext => {
|
|
|
|
vec![Api::Web3, Api::Net, Api::Eth, Api::Personal, Api::Ethcore, Api::Traces, Api::Rpc]
|
|
|
|
.into_iter().collect()
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
vec![Api::Web3, Api::Net, Api::Eth, Api::Personal, Api::Signer, Api::Ethcore, Api::EthcoreSet, Api::Traces, Api::Rpc]
|
|
|
|
.into_iter().collect()
|
|
|
|
},
|
|
|
|
}
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn setup_rpc<T: Extendable>(server: T, deps: Arc<Dependencies>, apis: ApiSet) -> T {
|
|
|
|
use ethcore_rpc::v1::*;
|
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
// it's turned into vector, cause ont of the cases requires &[]
|
|
|
|
let apis = apis.list_apis().into_iter().collect::<Vec<_>>();
|
2016-06-01 19:37:34 +02:00
|
|
|
for api in &apis {
|
|
|
|
match *api {
|
|
|
|
Api::Web3 => {
|
|
|
|
server.add_delegate(Web3Client::new().to_delegate());
|
|
|
|
},
|
|
|
|
Api::Net => {
|
|
|
|
server.add_delegate(NetClient::new(&deps.sync).to_delegate());
|
|
|
|
},
|
|
|
|
Api::Eth => {
|
2016-07-25 16:09:47 +02:00
|
|
|
let client = EthClient::new(
|
|
|
|
&deps.client,
|
|
|
|
&deps.sync,
|
|
|
|
&deps.secret_store,
|
|
|
|
&deps.miner,
|
|
|
|
&deps.external_miner,
|
2016-08-03 15:31:00 +02:00
|
|
|
EthClientOptions {
|
|
|
|
allow_pending_receipt_query: !deps.geth_compatibility,
|
|
|
|
send_block_number_in_get_work: !deps.geth_compatibility,
|
|
|
|
}
|
2016-07-25 16:09:47 +02:00
|
|
|
);
|
|
|
|
server.add_delegate(client.to_delegate());
|
|
|
|
|
|
|
|
let filter_client = EthFilterClient::new(&deps.client, &deps.miner);
|
|
|
|
server.add_delegate(filter_client.to_delegate());
|
2016-06-01 19:37:34 +02:00
|
|
|
|
2016-06-10 15:39:34 +02:00
|
|
|
if deps.signer_port.is_some() {
|
2016-06-22 21:32:26 +02:00
|
|
|
server.add_delegate(EthSigningQueueClient::new(&deps.signer_queue, &deps.client, &deps.miner, &deps.secret_store).to_delegate());
|
2016-06-01 19:37:34 +02:00
|
|
|
} else {
|
|
|
|
server.add_delegate(EthSigningUnsafeClient::new(&deps.client, &deps.secret_store, &deps.miner).to_delegate());
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Api::Personal => {
|
2016-08-05 23:33:14 +02:00
|
|
|
server.add_delegate(PersonalClient::new(&deps.secret_store, &deps.client, &deps.miner, deps.signer_port, deps.geth_compatibility).to_delegate());
|
2016-06-07 22:52:48 +02:00
|
|
|
},
|
|
|
|
Api::Signer => {
|
|
|
|
server.add_delegate(SignerClient::new(&deps.secret_store, &deps.client, &deps.miner, &deps.signer_queue).to_delegate());
|
2016-06-01 19:37:34 +02:00
|
|
|
},
|
|
|
|
Api::Ethcore => {
|
2016-06-21 14:55:25 +02:00
|
|
|
let queue = deps.signer_port.map(|_| deps.signer_queue.clone());
|
2016-08-12 12:16:20 +02:00
|
|
|
server.add_delegate(EthcoreClient::new(&deps.client, &deps.miner, &deps.sync, &deps.net_service, deps.logger.clone(), deps.settings.clone(), queue).to_delegate())
|
2016-06-01 19:37:34 +02:00
|
|
|
},
|
2016-06-07 22:52:48 +02:00
|
|
|
Api::EthcoreSet => {
|
2016-07-05 17:50:46 +02:00
|
|
|
server.add_delegate(EthcoreSetClient::new(&deps.client, &deps.miner, &deps.net_service).to_delegate())
|
2016-06-07 22:52:48 +02:00
|
|
|
},
|
2016-06-01 19:37:34 +02:00
|
|
|
Api::Traces => {
|
2016-06-02 13:14:49 +02:00
|
|
|
server.add_delegate(TracesClient::new(&deps.client, &deps.miner).to_delegate())
|
2016-06-01 19:37:34 +02:00
|
|
|
},
|
|
|
|
Api::Rpc => {
|
|
|
|
let modules = to_modules(&apis);
|
|
|
|
server.add_delegate(RpcClient::new(modules).to_delegate());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
server
|
|
|
|
}
|
2016-07-25 16:09:47 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::{Api, ApiSet};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_api_parsing() {
|
|
|
|
assert_eq!(Api::Web3, "web3".parse().unwrap());
|
|
|
|
assert_eq!(Api::Net, "net".parse().unwrap());
|
|
|
|
assert_eq!(Api::Eth, "eth".parse().unwrap());
|
|
|
|
assert_eq!(Api::Personal, "personal".parse().unwrap());
|
|
|
|
assert_eq!(Api::Signer, "signer".parse().unwrap());
|
|
|
|
assert_eq!(Api::Ethcore, "ethcore".parse().unwrap());
|
|
|
|
assert_eq!(Api::EthcoreSet, "ethcore_set".parse().unwrap());
|
|
|
|
assert_eq!(Api::Traces, "traces".parse().unwrap());
|
|
|
|
assert_eq!(Api::Rpc, "rpc".parse().unwrap());
|
|
|
|
assert!("rp".parse::<Api>().is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_api_set_default() {
|
|
|
|
assert_eq!(ApiSet::UnsafeContext, ApiSet::default());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_api_set_parsing() {
|
|
|
|
assert_eq!(ApiSet::List(vec![Api::Web3, Api::Eth].into_iter().collect()), "web3,eth".parse().unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_api_set_unsafe_context() {
|
|
|
|
let expected = vec![Api::Web3, Api::Net, Api::Eth, Api::Personal, Api::Ethcore, Api::Traces, Api::Rpc]
|
|
|
|
.into_iter().collect();
|
|
|
|
assert_eq!(ApiSet::UnsafeContext.list_apis(), expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_api_set_safe_context() {
|
|
|
|
let expected = vec![Api::Web3, Api::Net, Api::Eth, Api::Personal, Api::Signer, Api::Ethcore, Api::EthcoreSet, Api::Traces, Api::Rpc]
|
|
|
|
.into_iter().collect();
|
|
|
|
assert_eq!(ApiSet::SafeContext.list_apis(), expected);
|
|
|
|
}
|
|
|
|
}
|