2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-06-01 19:37:34 +02:00
|
|
|
// 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/>.
|
|
|
|
|
2017-02-04 22:18:19 +01:00
|
|
|
use std::cmp::PartialEq;
|
2016-06-01 19:37:34 +02:00
|
|
|
use std::collections::BTreeMap;
|
2016-07-25 16:09:47 +02:00
|
|
|
use std::collections::HashSet;
|
2016-06-01 19:37:34 +02:00
|
|
|
use std::str::FromStr;
|
|
|
|
use std::sync::Arc;
|
2017-02-04 22:18:19 +01:00
|
|
|
|
|
|
|
pub use ethcore_rpc::SignerService;
|
|
|
|
|
2016-06-20 00:10:34 +02:00
|
|
|
use ethcore::account_provider::AccountProvider;
|
2017-02-04 22:18:19 +01:00
|
|
|
use ethcore::client::Client;
|
|
|
|
use ethcore::miner::{Miner, ExternalMiner};
|
2016-10-31 17:32:53 +01:00
|
|
|
use ethcore::snapshot::SnapshotService;
|
2017-01-11 20:02:27 +01:00
|
|
|
use ethcore_rpc::{Metadata, NetworkSettings};
|
2017-02-04 22:18:19 +01:00
|
|
|
use ethcore_rpc::informant::{Middleware, RpcStats, ClientNotifier};
|
2017-02-08 20:44:40 +01:00
|
|
|
use ethcore_rpc::dispatch::FullDispatcher;
|
2017-02-04 22:18:19 +01:00
|
|
|
use ethsync::{ManageNetwork, SyncProvider};
|
2016-12-22 18:26:39 +01:00
|
|
|
use hash_fetch::fetch::Client as FetchClient;
|
2017-02-04 22:18:19 +01:00
|
|
|
use jsonrpc_core::{MetaIoHandler};
|
|
|
|
use updater::Updater;
|
2017-03-22 06:23:40 +01:00
|
|
|
use ethcore_logger::RotatingLogger;
|
2016-06-01 19:37:34 +02:00
|
|
|
|
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 {
|
2016-11-06 12:51:53 +01:00
|
|
|
/// Web3 (Safe)
|
2016-06-01 19:37:34 +02:00
|
|
|
Web3,
|
2016-11-06 12:51:53 +01:00
|
|
|
/// Net (Safe)
|
2016-06-01 19:37:34 +02:00
|
|
|
Net,
|
2016-11-06 12:51:53 +01:00
|
|
|
/// Eth (Safe)
|
2016-06-01 19:37:34 +02:00
|
|
|
Eth,
|
2016-11-09 19:41:47 +01:00
|
|
|
/// Geth-compatible "personal" API (DEPRECATED; only used in `--geth` mode.)
|
2016-11-06 12:51:53 +01:00
|
|
|
Personal,
|
|
|
|
/// Signer - Confirm transactions in Signer (UNSAFE: Passwords, List of transactions)
|
2016-06-07 22:52:48 +02:00
|
|
|
Signer,
|
2016-11-06 12:51:53 +01:00
|
|
|
/// Parity - Custom extensions (Safe)
|
|
|
|
Parity,
|
|
|
|
/// Parity Accounts extensions (UNSAFE: Passwords, Side Effects (new account))
|
|
|
|
ParityAccounts,
|
|
|
|
/// Parity - Set methods (UNSAFE: Side Effects affecting node operation)
|
|
|
|
ParitySet,
|
|
|
|
/// Traces (Safe)
|
2016-06-01 19:37:34 +02:00
|
|
|
Traces,
|
2016-11-06 12:51:53 +01:00
|
|
|
/// Rpc (Safe)
|
2016-06-01 19:37:34 +02:00
|
|
|
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),
|
2016-11-06 12:51:53 +01:00
|
|
|
"personal" => Ok(Personal),
|
2016-06-07 22:52:48 +02:00
|
|
|
"signer" => Ok(Signer),
|
2016-11-06 12:51:53 +01:00
|
|
|
"parity" => Ok(Parity),
|
|
|
|
"parity_accounts" => Ok(ParityAccounts),
|
|
|
|
"parity_set" => Ok(ParitySet),
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-03 10:27:37 +02:00
|
|
|
#[derive(Debug, Clone)]
|
2016-07-25 16:09:47 +02:00
|
|
|
pub enum ApiSet {
|
|
|
|
SafeContext,
|
|
|
|
UnsafeContext,
|
2016-11-04 09:58:39 +01:00
|
|
|
IpcContext,
|
2016-07-25 16:09:47 +02:00
|
|
|
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-09-21 12:44:49 +02:00
|
|
|
pub signer_service: Arc<SignerService>,
|
2016-06-01 19:37:34 +02:00
|
|
|
pub client: Arc<Client>,
|
2016-10-31 17:32:53 +01:00
|
|
|
pub snapshot: Arc<SnapshotService>,
|
2016-07-16 15:51:06 +02:00
|
|
|
pub sync: Arc<SyncProvider>,
|
|
|
|
pub net: Arc<ManageNetwork>,
|
2017-03-29 17:07:58 +02:00
|
|
|
pub secret_store: Option<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-12-11 19:14:42 +01:00
|
|
|
pub updater: Arc<Updater>,
|
2016-08-03 15:31:00 +02:00
|
|
|
pub geth_compatibility: bool,
|
2016-11-09 19:41:47 +01:00
|
|
|
pub dapps_interface: Option<String>,
|
2016-10-24 12:21:15 +02:00
|
|
|
pub dapps_port: Option<u16>,
|
2016-12-22 18:26:39 +01:00
|
|
|
pub fetch: FetchClient,
|
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"),
|
2016-11-06 12:51:53 +01:00
|
|
|
Api::Personal => ("personal", "1.0"),
|
2016-06-07 22:52:48 +02:00
|
|
|
Api::Signer => ("signer", "1.0"),
|
2016-11-06 12:51:53 +01:00
|
|
|
Api::Parity => ("parity", "1.0"),
|
|
|
|
Api::ParityAccounts => ("parity_accounts", "1.0"),
|
|
|
|
Api::ParitySet => ("parity_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> {
|
2016-11-06 12:51:53 +01:00
|
|
|
let mut safe_list = vec![Api::Web3, Api::Net, Api::Eth, Api::Parity, Api::Traces, Api::Rpc]
|
|
|
|
.into_iter().collect();
|
2016-07-25 16:09:47 +02:00
|
|
|
match *self {
|
|
|
|
ApiSet::List(ref apis) => apis.clone(),
|
2016-11-06 12:51:53 +01:00
|
|
|
ApiSet::UnsafeContext => safe_list,
|
2016-11-04 09:58:39 +01:00
|
|
|
ApiSet::IpcContext => {
|
2016-11-06 12:51:53 +01:00
|
|
|
safe_list.insert(Api::ParityAccounts);
|
|
|
|
safe_list
|
2016-11-04 09:58:39 +01:00
|
|
|
},
|
2016-10-24 15:10:13 +02:00
|
|
|
ApiSet::SafeContext => {
|
2016-11-06 12:51:53 +01:00
|
|
|
safe_list.insert(Api::ParityAccounts);
|
|
|
|
safe_list.insert(Api::ParitySet);
|
|
|
|
safe_list.insert(Api::Signer);
|
|
|
|
safe_list
|
2016-07-25 16:09:47 +02:00
|
|
|
},
|
|
|
|
}
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-06 12:51:53 +01:00
|
|
|
macro_rules! add_signing_methods {
|
2017-01-11 20:02:27 +01:00
|
|
|
($namespace:ident, $handler:expr, $deps:expr) => {
|
|
|
|
{
|
|
|
|
let handler = &mut $handler;
|
|
|
|
let deps = &$deps;
|
2017-02-08 20:44:40 +01:00
|
|
|
let dispatcher = FullDispatcher::new(Arc::downgrade(&deps.client), Arc::downgrade(&deps.miner));
|
2017-01-11 20:02:27 +01:00
|
|
|
if deps.signer_service.is_enabled() {
|
2017-02-08 20:44:40 +01:00
|
|
|
handler.extend_with($namespace::to_delegate(SigningQueueClient::new(&deps.signer_service, dispatcher, &deps.secret_store)))
|
2017-01-11 20:02:27 +01:00
|
|
|
} else {
|
2017-02-08 20:44:40 +01:00
|
|
|
handler.extend_with($namespace::to_delegate(SigningUnsafeClient::new(&deps.secret_store, dispatcher)))
|
2017-01-11 20:02:27 +01:00
|
|
|
}
|
2016-11-06 12:51:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-04 22:18:19 +01:00
|
|
|
pub fn setup_rpc(stats: Arc<RpcStats>, deps: Arc<Dependencies>, apis: ApiSet) -> MetaIoHandler<Metadata, Middleware> {
|
2016-06-01 19:37:34 +02:00
|
|
|
use ethcore_rpc::v1::*;
|
|
|
|
|
2017-02-04 22:18:19 +01:00
|
|
|
let mut handler = MetaIoHandler::with_middleware(Middleware::new(stats, ClientNotifier {
|
|
|
|
client: deps.client.clone(),
|
|
|
|
}));
|
|
|
|
|
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<_>>();
|
2017-02-08 20:44:40 +01:00
|
|
|
let dispatcher = FullDispatcher::new(Arc::downgrade(&deps.client), Arc::downgrade(&deps.miner));
|
|
|
|
|
2016-06-01 19:37:34 +02:00
|
|
|
for api in &apis {
|
|
|
|
match *api {
|
|
|
|
Api::Web3 => {
|
2017-01-11 20:02:27 +01:00
|
|
|
handler.extend_with(Web3Client::new().to_delegate());
|
2016-06-01 19:37:34 +02:00
|
|
|
},
|
|
|
|
Api::Net => {
|
2017-01-11 20:02:27 +01:00
|
|
|
handler.extend_with(NetClient::new(&deps.sync).to_delegate());
|
2016-06-01 19:37:34 +02:00
|
|
|
},
|
|
|
|
Api::Eth => {
|
2016-07-25 16:09:47 +02:00
|
|
|
let client = EthClient::new(
|
|
|
|
&deps.client,
|
2016-10-31 17:32:53 +01:00
|
|
|
&deps.snapshot,
|
2016-07-25 16:09:47 +02:00
|
|
|
&deps.sync,
|
|
|
|
&deps.secret_store,
|
|
|
|
&deps.miner,
|
|
|
|
&deps.external_miner,
|
2016-08-03 15:31:00 +02:00
|
|
|
EthClientOptions {
|
2017-03-10 10:25:13 +01:00
|
|
|
pending_nonce_from_queue: deps.geth_compatibility,
|
2016-08-03 15:31:00 +02:00
|
|
|
allow_pending_receipt_query: !deps.geth_compatibility,
|
|
|
|
send_block_number_in_get_work: !deps.geth_compatibility,
|
|
|
|
}
|
2016-07-25 16:09:47 +02:00
|
|
|
);
|
2017-01-11 20:02:27 +01:00
|
|
|
handler.extend_with(client.to_delegate());
|
2016-07-25 16:09:47 +02:00
|
|
|
|
|
|
|
let filter_client = EthFilterClient::new(&deps.client, &deps.miner);
|
2017-01-11 20:02:27 +01:00
|
|
|
handler.extend_with(filter_client.to_delegate());
|
2016-06-01 19:37:34 +02:00
|
|
|
|
2017-01-11 20:02:27 +01:00
|
|
|
add_signing_methods!(EthSigning, handler, deps);
|
2016-06-01 19:37:34 +02:00
|
|
|
},
|
2016-11-06 12:51:53 +01:00
|
|
|
Api::Personal => {
|
2017-02-08 20:44:40 +01:00
|
|
|
handler.extend_with(PersonalClient::new(&deps.secret_store, dispatcher.clone(), deps.geth_compatibility).to_delegate());
|
2016-06-07 22:52:48 +02:00
|
|
|
},
|
|
|
|
Api::Signer => {
|
2017-02-08 20:44:40 +01:00
|
|
|
handler.extend_with(SignerClient::new(&deps.secret_store, dispatcher.clone(), &deps.signer_service).to_delegate());
|
2016-06-01 19:37:34 +02:00
|
|
|
},
|
2016-11-06 12:51:53 +01:00
|
|
|
Api::Parity => {
|
2016-10-24 12:21:15 +02:00
|
|
|
let signer = match deps.signer_service.is_enabled() {
|
|
|
|
true => Some(deps.signer_service.clone()),
|
|
|
|
false => None,
|
|
|
|
};
|
2017-01-11 20:02:27 +01:00
|
|
|
handler.extend_with(ParityClient::new(
|
2016-10-24 12:21:15 +02:00
|
|
|
&deps.client,
|
|
|
|
&deps.miner,
|
|
|
|
&deps.sync,
|
2016-12-11 23:15:52 +01:00
|
|
|
&deps.updater,
|
2016-10-24 12:21:15 +02:00
|
|
|
&deps.net_service,
|
2016-11-06 12:51:53 +01:00
|
|
|
&deps.secret_store,
|
2016-10-24 12:21:15 +02:00
|
|
|
deps.logger.clone(),
|
|
|
|
deps.settings.clone(),
|
|
|
|
signer,
|
2016-11-09 19:41:47 +01:00
|
|
|
deps.dapps_interface.clone(),
|
2016-10-24 12:21:15 +02:00
|
|
|
deps.dapps_port,
|
2016-11-06 12:51:53 +01:00
|
|
|
).to_delegate());
|
|
|
|
|
2017-01-11 20:02:27 +01:00
|
|
|
add_signing_methods!(EthSigning, handler, deps);
|
|
|
|
add_signing_methods!(ParitySigning, handler, deps);
|
2016-11-06 12:51:53 +01:00
|
|
|
},
|
|
|
|
Api::ParityAccounts => {
|
2017-02-04 22:18:19 +01:00
|
|
|
handler.extend_with(ParityAccountsClient::new(&deps.secret_store).to_delegate());
|
2016-06-01 19:37:34 +02:00
|
|
|
},
|
2016-11-06 12:51:53 +01:00
|
|
|
Api::ParitySet => {
|
2017-01-11 20:02:27 +01:00
|
|
|
handler.extend_with(ParitySetClient::new(
|
2016-12-22 18:26:39 +01:00
|
|
|
&deps.client,
|
|
|
|
&deps.miner,
|
|
|
|
&deps.updater,
|
|
|
|
&deps.net_service,
|
|
|
|
deps.fetch.clone(),
|
|
|
|
).to_delegate())
|
2016-06-07 22:52:48 +02:00
|
|
|
},
|
2016-06-01 19:37:34 +02:00
|
|
|
Api::Traces => {
|
2017-01-11 20:02:27 +01:00
|
|
|
handler.extend_with(TracesClient::new(&deps.client, &deps.miner).to_delegate())
|
2016-06-01 19:37:34 +02:00
|
|
|
},
|
|
|
|
Api::Rpc => {
|
|
|
|
let modules = to_modules(&apis);
|
2017-01-11 20:02:27 +01:00
|
|
|
handler.extend_with(RpcClient::new(modules).to_delegate());
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-01-11 20:02:27 +01:00
|
|
|
handler
|
2016-06-01 19:37:34 +02:00
|
|
|
}
|
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());
|
2016-11-06 12:51:53 +01:00
|
|
|
assert_eq!(Api::Personal, "personal".parse().unwrap());
|
2016-07-25 16:09:47 +02:00
|
|
|
assert_eq!(Api::Signer, "signer".parse().unwrap());
|
2016-11-06 12:51:53 +01:00
|
|
|
assert_eq!(Api::Parity, "parity".parse().unwrap());
|
|
|
|
assert_eq!(Api::ParityAccounts, "parity_accounts".parse().unwrap());
|
|
|
|
assert_eq!(Api::ParitySet, "parity_set".parse().unwrap());
|
2016-07-25 16:09:47 +02:00
|
|
|
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() {
|
2016-11-06 12:51:53 +01:00
|
|
|
let expected = vec![
|
|
|
|
// make sure this list contains only SAFE methods
|
|
|
|
Api::Web3, Api::Net, Api::Eth, Api::Parity, Api::Traces, Api::Rpc
|
|
|
|
].into_iter().collect();
|
2016-07-25 16:09:47 +02:00
|
|
|
assert_eq!(ApiSet::UnsafeContext.list_apis(), expected);
|
|
|
|
}
|
|
|
|
|
2016-11-06 12:51:53 +01:00
|
|
|
#[test]
|
|
|
|
fn test_api_set_ipc_context() {
|
|
|
|
let expected = vec![
|
|
|
|
// safe
|
|
|
|
Api::Web3, Api::Net, Api::Eth, Api::Parity, Api::Traces, Api::Rpc,
|
|
|
|
// semi-safe
|
|
|
|
Api::ParityAccounts
|
|
|
|
].into_iter().collect();
|
|
|
|
assert_eq!(ApiSet::IpcContext.list_apis(), expected);
|
|
|
|
}
|
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
#[test]
|
|
|
|
fn test_api_set_safe_context() {
|
2016-11-06 12:51:53 +01:00
|
|
|
let expected = vec![
|
|
|
|
// safe
|
|
|
|
Api::Web3, Api::Net, Api::Eth, Api::Parity, Api::Traces, Api::Rpc,
|
|
|
|
// semi-safe
|
|
|
|
Api::ParityAccounts,
|
|
|
|
// Unsafe
|
|
|
|
Api::ParitySet, Api::Signer,
|
|
|
|
].into_iter().collect();
|
2016-07-25 16:09:47 +02:00
|
|
|
assert_eq!(ApiSet::SafeContext.list_apis(), expected);
|
|
|
|
}
|
|
|
|
}
|