2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-04-21 13:57:27 +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-01-06 16:05:58 +01:00
|
|
|
use std::path::PathBuf;
|
2016-04-21 13:57:27 +02:00
|
|
|
use std::sync::Arc;
|
2017-01-30 10:46:50 +01:00
|
|
|
|
2018-01-11 17:49:10 +01:00
|
|
|
use bytes::Bytes;
|
2017-01-30 10:46:50 +01:00
|
|
|
use dir::default_data_path;
|
2017-12-24 09:34:43 +01:00
|
|
|
use dir::helpers::replace_home;
|
2018-03-03 18:42:13 +01:00
|
|
|
use ethcore::client::{Client, BlockChainClient, BlockId, CallContract};
|
2017-05-18 12:44:09 +02:00
|
|
|
use ethsync::LightSync;
|
2018-02-09 09:32:06 +01:00
|
|
|
use futures::{Future, future, IntoFuture};
|
2017-01-30 10:46:50 +01:00
|
|
|
use hash_fetch::fetch::Client as FetchClient;
|
2018-03-12 21:46:27 +01:00
|
|
|
use registrar::{RegistrarClient, Asynchronous};
|
2017-09-05 17:54:05 +02:00
|
|
|
use light::client::LightChainClient;
|
2017-05-18 12:44:09 +02:00
|
|
|
use light::on_demand::{self, OnDemand};
|
2017-08-28 14:11:55 +02:00
|
|
|
use node_health::{SyncStatus, NodeHealth};
|
2017-05-24 12:24:07 +02:00
|
|
|
use rpc;
|
2017-04-03 10:27:37 +02:00
|
|
|
use rpc_apis::SignerService;
|
2018-01-11 17:49:10 +01:00
|
|
|
use transaction::{Transaction, Action};
|
2018-01-10 13:35:18 +01:00
|
|
|
use ethereum_types::Address;
|
2016-04-21 13:57:27 +02:00
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
2016-04-21 13:57:27 +02:00
|
|
|
pub struct Configuration {
|
|
|
|
pub enabled: bool,
|
2017-01-06 16:05:58 +01:00
|
|
|
pub dapps_path: PathBuf,
|
|
|
|
pub extra_dapps: Vec<PathBuf>,
|
2017-07-11 13:45:23 +02:00
|
|
|
pub extra_embed_on: Vec<(String, u16)>,
|
2017-08-10 18:32:10 +02:00
|
|
|
pub extra_script_src: Vec<(String, u16)>,
|
2016-04-21 13:57:27 +02:00
|
|
|
}
|
|
|
|
|
2016-07-25 16:09:47 +02:00
|
|
|
impl Default for Configuration {
|
|
|
|
fn default() -> Self {
|
2016-12-13 23:38:29 +01:00
|
|
|
let data_dir = default_data_path();
|
2016-07-25 16:09:47 +02:00
|
|
|
Configuration {
|
|
|
|
enabled: true,
|
2017-01-06 16:05:58 +01:00
|
|
|
dapps_path: replace_home(&data_dir, "$BASE/dapps").into(),
|
|
|
|
extra_dapps: vec![],
|
2017-07-11 13:45:23 +02:00
|
|
|
extra_embed_on: vec![],
|
2017-08-10 18:32:10 +02:00
|
|
|
extra_script_src: vec![],
|
2016-07-25 16:09:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-24 12:24:07 +02:00
|
|
|
impl Configuration {
|
2017-09-21 14:52:44 +02:00
|
|
|
pub fn address(&self, address: Option<::parity_rpc::Host>) -> Option<::parity_rpc::Host> {
|
2017-05-24 12:24:07 +02:00
|
|
|
match self.enabled {
|
|
|
|
true => address,
|
|
|
|
false => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-22 22:00:52 +01:00
|
|
|
/// Registrar implementation of the full client.
|
|
|
|
pub struct FullRegistrar {
|
|
|
|
/// Handle to the full client.
|
2016-08-23 19:28:21 +02:00
|
|
|
pub client: Arc<Client>,
|
2017-03-22 22:00:52 +01:00
|
|
|
}
|
|
|
|
|
2018-02-09 09:32:06 +01:00
|
|
|
impl FullRegistrar {
|
|
|
|
pub fn new(client: Arc<Client>) -> Self {
|
|
|
|
FullRegistrar {
|
|
|
|
client,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-12 21:46:27 +01:00
|
|
|
impl RegistrarClient for FullRegistrar {
|
|
|
|
type Call = Asynchronous;
|
|
|
|
|
|
|
|
fn registrar_address(&self) -> Result<Address, String> {
|
2018-02-09 09:32:06 +01:00
|
|
|
self.client.registrar_address()
|
2017-03-22 22:00:52 +01:00
|
|
|
.ok_or_else(|| "Registrar not defined.".into())
|
|
|
|
}
|
|
|
|
|
2018-03-12 21:46:27 +01:00
|
|
|
fn call_contract(&self, address: Address, data: Bytes) -> Self::Call {
|
2018-02-09 09:32:06 +01:00
|
|
|
Box::new(self.client.call_contract(BlockId::Latest, address, data).into_future())
|
2017-05-18 12:44:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Registrar implementation for the light client.
|
2017-09-05 17:54:05 +02:00
|
|
|
pub struct LightRegistrar<T> {
|
2017-05-18 12:44:09 +02:00
|
|
|
/// The light client.
|
2017-09-05 17:54:05 +02:00
|
|
|
pub client: Arc<T>,
|
2017-05-18 12:44:09 +02:00
|
|
|
/// Handle to the on-demand service.
|
|
|
|
pub on_demand: Arc<OnDemand>,
|
|
|
|
/// Handle to the light network service.
|
|
|
|
pub sync: Arc<LightSync>,
|
|
|
|
}
|
|
|
|
|
2018-03-12 21:46:27 +01:00
|
|
|
impl<T: LightChainClient + 'static> RegistrarClient for LightRegistrar<T> {
|
|
|
|
type Call = Box<Future<Item = Bytes, Error = String> + Send>;
|
|
|
|
|
|
|
|
fn registrar_address(&self) -> Result<Address, String> {
|
2017-05-18 12:44:09 +02:00
|
|
|
self.client.engine().additional_params().get("registrar")
|
|
|
|
.ok_or_else(|| "Registrar not defined.".into())
|
|
|
|
.and_then(|registrar| {
|
|
|
|
registrar.parse().map_err(|e| format!("Invalid registrar address: {:?}", e))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-03-12 21:46:27 +01:00
|
|
|
fn call_contract(&self, address: Address, data: Bytes) -> Self::Call {
|
2017-09-05 17:54:05 +02:00
|
|
|
let header = self.client.best_block_header();
|
|
|
|
let env_info = self.client.env_info(BlockId::Hash(header.hash()))
|
|
|
|
.ok_or_else(|| format!("Cannot fetch env info for header {}", header.hash()));
|
|
|
|
|
|
|
|
let env_info = match env_info {
|
2018-02-09 09:32:06 +01:00
|
|
|
Ok(e) => e,
|
2017-10-05 12:35:01 +02:00
|
|
|
Err(e) => return Box::new(future::err(e)),
|
2017-09-05 17:54:05 +02:00
|
|
|
};
|
2017-05-18 12:44:09 +02:00
|
|
|
|
|
|
|
let maybe_future = self.sync.with_context(move |ctx| {
|
|
|
|
self.on_demand
|
2017-05-23 14:35:34 +02:00
|
|
|
.request(ctx, on_demand::request::TransactionProof {
|
2017-05-18 12:44:09 +02:00
|
|
|
tx: Transaction {
|
2017-06-28 09:10:57 +02:00
|
|
|
nonce: self.client.engine().account_start_nonce(header.number()),
|
2017-05-18 12:44:09 +02:00
|
|
|
action: Action::Call(address),
|
2017-07-27 13:50:12 +02:00
|
|
|
gas: 50_000.into(), // should be enough for all registry lookups. TODO: exponential backoff
|
2017-05-18 12:44:09 +02:00
|
|
|
gas_price: 0.into(),
|
|
|
|
value: 0.into(),
|
|
|
|
data: data,
|
|
|
|
}.fake_sign(Address::default()),
|
2017-05-23 18:05:17 +02:00
|
|
|
header: header.into(),
|
2017-05-18 12:44:09 +02:00
|
|
|
env_info: env_info,
|
|
|
|
engine: self.client.engine().clone(),
|
|
|
|
})
|
2017-05-23 18:05:17 +02:00
|
|
|
.expect("No back-references; therefore all back-refs valid; qed")
|
2017-05-18 12:44:09 +02:00
|
|
|
.then(|res| match res {
|
|
|
|
Ok(Ok(executed)) => Ok(executed.output),
|
|
|
|
Ok(Err(e)) => Err(format!("Failed to execute transaction: {}", e)),
|
|
|
|
Err(_) => Err(format!("On-demand service dropped request unexpectedly.")),
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
match maybe_future {
|
2017-10-05 12:35:01 +02:00
|
|
|
Some(fut) => Box::new(fut),
|
|
|
|
None => Box::new(future::err("cannot query registry: network disabled".into())),
|
2017-05-18 12:44:09 +02:00
|
|
|
}
|
2017-03-22 22:00:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: light client implementation forwarding to OnDemand and waiting for future
|
|
|
|
// to resolve.
|
2017-05-24 12:24:07 +02:00
|
|
|
#[derive(Clone)]
|
2017-04-03 12:44:52 +02:00
|
|
|
pub struct Dependencies {
|
2017-08-28 14:11:55 +02:00
|
|
|
pub node_health: NodeHealth,
|
2017-04-06 19:32:30 +02:00
|
|
|
pub sync_status: Arc<SyncStatus>,
|
2018-03-12 21:46:27 +01:00
|
|
|
pub contract_client: Arc<RegistrarClient<Call=Asynchronous>>,
|
2016-12-22 18:26:39 +01:00
|
|
|
pub fetch: FetchClient,
|
2016-12-27 11:15:02 +01:00
|
|
|
pub signer: Arc<SignerService>,
|
2017-05-24 12:24:07 +02:00
|
|
|
pub ui_address: Option<(String, u16)>,
|
2016-04-21 13:57:27 +02:00
|
|
|
}
|
|
|
|
|
2017-05-24 12:24:07 +02:00
|
|
|
pub fn new(configuration: Configuration, deps: Dependencies) -> Result<Option<Middleware>, String> {
|
2016-04-21 13:57:27 +02:00
|
|
|
if !configuration.enabled {
|
2016-07-25 16:09:47 +02:00
|
|
|
return Ok(None);
|
2016-04-21 13:57:27 +02:00
|
|
|
}
|
|
|
|
|
2017-05-24 12:24:07 +02:00
|
|
|
server::dapps_middleware(
|
2017-01-06 16:05:58 +01:00
|
|
|
deps,
|
|
|
|
configuration.dapps_path,
|
|
|
|
configuration.extra_dapps,
|
2017-07-11 12:23:46 +02:00
|
|
|
rpc::DAPPS_DOMAIN,
|
2017-07-11 13:45:23 +02:00
|
|
|
configuration.extra_embed_on,
|
2017-08-10 18:32:10 +02:00
|
|
|
configuration.extra_script_src,
|
2017-05-24 12:24:07 +02:00
|
|
|
).map(Some)
|
|
|
|
}
|
|
|
|
|
2017-08-28 14:11:55 +02:00
|
|
|
pub fn new_ui(enabled: bool, deps: Dependencies) -> Result<Option<Middleware>, String> {
|
2017-05-24 12:24:07 +02:00
|
|
|
if !enabled {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
|
|
|
|
server::ui_middleware(
|
|
|
|
deps,
|
2017-07-11 12:23:46 +02:00
|
|
|
rpc::DAPPS_DOMAIN,
|
2017-04-03 10:27:37 +02:00
|
|
|
).map(Some)
|
2016-04-21 13:57:27 +02:00
|
|
|
}
|
|
|
|
|
2017-08-28 14:11:55 +02:00
|
|
|
pub use self::server::{Middleware, service};
|
2016-08-23 19:28:21 +02:00
|
|
|
|
2016-05-26 18:21:15 +02:00
|
|
|
#[cfg(not(feature = "dapps"))]
|
2016-08-23 19:28:21 +02:00
|
|
|
mod server {
|
|
|
|
use super::Dependencies;
|
2017-05-24 12:24:07 +02:00
|
|
|
use std::sync::Arc;
|
2017-01-09 11:16:05 +01:00
|
|
|
use std::path::PathBuf;
|
2017-04-13 16:32:07 +02:00
|
|
|
use parity_rpc::{hyper, RequestMiddleware, RequestMiddlewareAction};
|
2017-05-24 12:24:07 +02:00
|
|
|
use rpc_apis;
|
2016-08-23 19:28:21 +02:00
|
|
|
|
2017-04-06 19:32:30 +02:00
|
|
|
pub struct Middleware;
|
2017-04-03 10:27:37 +02:00
|
|
|
impl RequestMiddleware for Middleware {
|
2017-11-20 11:02:48 +01:00
|
|
|
fn on_request(&self, _req: hyper::Request) -> RequestMiddlewareAction {
|
2017-04-03 10:27:37 +02:00
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn dapps_middleware(
|
2016-08-23 19:28:21 +02:00
|
|
|
_deps: Dependencies,
|
2017-01-06 16:05:58 +01:00
|
|
|
_dapps_path: PathBuf,
|
|
|
|
_extra_dapps: Vec<PathBuf>,
|
2017-07-11 12:23:46 +02:00
|
|
|
_dapps_domain: &str,
|
2017-07-11 13:45:23 +02:00
|
|
|
_extra_embed_on: Vec<(String, u16)>,
|
2017-08-10 18:32:10 +02:00
|
|
|
_extra_script_src: Vec<(String, u16)>,
|
2017-04-03 10:27:37 +02:00
|
|
|
) -> Result<Middleware, String> {
|
2016-08-23 19:28:21 +02:00
|
|
|
Err("Your Parity version has been compiled without WebApps support.".into())
|
|
|
|
}
|
2017-05-24 12:24:07 +02:00
|
|
|
|
|
|
|
pub fn ui_middleware(
|
|
|
|
_deps: Dependencies,
|
2017-07-11 12:23:46 +02:00
|
|
|
_dapps_domain: &str,
|
2017-05-24 12:24:07 +02:00
|
|
|
) -> Result<Middleware, String> {
|
|
|
|
Err("Your Parity version has been compiled without UI support.".into())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn service(_: &Option<Middleware>) -> Option<Arc<rpc_apis::DappsService>> {
|
|
|
|
None
|
|
|
|
}
|
2016-04-21 13:57:27 +02:00
|
|
|
}
|
|
|
|
|
2016-05-26 18:21:15 +02:00
|
|
|
#[cfg(feature = "dapps")]
|
2016-08-23 19:28:21 +02:00
|
|
|
mod server {
|
|
|
|
use super::Dependencies;
|
2017-01-06 16:05:58 +01:00
|
|
|
use std::path::PathBuf;
|
2016-08-23 19:28:21 +02:00
|
|
|
use std::sync::Arc;
|
2017-05-24 12:24:07 +02:00
|
|
|
use rpc_apis;
|
2016-08-23 19:28:21 +02:00
|
|
|
|
2017-04-03 10:27:37 +02:00
|
|
|
use parity_dapps;
|
2016-08-23 19:28:21 +02:00
|
|
|
|
2017-04-05 11:37:45 +02:00
|
|
|
pub use parity_dapps::Middleware;
|
2016-08-23 19:28:21 +02:00
|
|
|
|
2017-04-03 10:27:37 +02:00
|
|
|
pub fn dapps_middleware(
|
2016-08-23 19:28:21 +02:00
|
|
|
deps: Dependencies,
|
2017-01-06 16:05:58 +01:00
|
|
|
dapps_path: PathBuf,
|
|
|
|
extra_dapps: Vec<PathBuf>,
|
2017-07-11 12:23:46 +02:00
|
|
|
dapps_domain: &str,
|
2017-07-11 13:45:23 +02:00
|
|
|
extra_embed_on: Vec<(String, u16)>,
|
2017-08-10 18:32:10 +02:00
|
|
|
extra_script_src: Vec<(String, u16)>,
|
2017-04-03 10:27:37 +02:00
|
|
|
) -> Result<Middleware, String> {
|
2017-05-24 12:24:07 +02:00
|
|
|
let signer = deps.signer;
|
2017-06-22 20:05:40 +02:00
|
|
|
let web_proxy_tokens = Arc::new(move |token| signer.web_proxy_access_token_domain(&token));
|
2017-04-03 10:27:37 +02:00
|
|
|
|
2017-05-24 12:24:07 +02:00
|
|
|
Ok(parity_dapps::Middleware::dapps(
|
2017-10-05 12:35:01 +02:00
|
|
|
deps.fetch.pool(),
|
2017-08-28 14:11:55 +02:00
|
|
|
deps.node_health,
|
2017-05-24 12:24:07 +02:00
|
|
|
deps.ui_address,
|
2017-07-11 13:45:23 +02:00
|
|
|
extra_embed_on,
|
2017-08-10 18:32:10 +02:00
|
|
|
extra_script_src,
|
2017-04-03 10:27:37 +02:00
|
|
|
dapps_path,
|
|
|
|
extra_dapps,
|
2017-05-24 12:24:07 +02:00
|
|
|
dapps_domain,
|
2017-04-03 12:44:52 +02:00
|
|
|
deps.contract_client,
|
|
|
|
deps.sync_status,
|
2017-04-03 10:27:37 +02:00
|
|
|
web_proxy_tokens,
|
2017-05-24 12:24:07 +02:00
|
|
|
deps.fetch,
|
2017-04-03 10:27:37 +02:00
|
|
|
))
|
2016-04-21 13:57:27 +02:00
|
|
|
}
|
2017-05-24 12:24:07 +02:00
|
|
|
|
|
|
|
pub fn ui_middleware(
|
|
|
|
deps: Dependencies,
|
2017-07-11 12:23:46 +02:00
|
|
|
dapps_domain: &str,
|
2017-05-24 12:24:07 +02:00
|
|
|
) -> Result<Middleware, String> {
|
|
|
|
Ok(parity_dapps::Middleware::ui(
|
2017-10-05 12:35:01 +02:00
|
|
|
deps.fetch.pool(),
|
2017-08-28 14:11:55 +02:00
|
|
|
deps.node_health,
|
2017-07-11 12:23:46 +02:00
|
|
|
dapps_domain,
|
2017-05-24 12:24:07 +02:00
|
|
|
deps.contract_client,
|
|
|
|
deps.sync_status,
|
|
|
|
deps.fetch,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn service(middleware: &Option<Middleware>) -> Option<Arc<rpc_apis::DappsService>> {
|
|
|
|
middleware.as_ref().map(|m| Arc::new(DappsServiceWrapper {
|
2017-08-09 19:06:40 +02:00
|
|
|
endpoints: m.endpoints().clone(),
|
2017-05-24 12:24:07 +02:00
|
|
|
}) as Arc<rpc_apis::DappsService>)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct DappsServiceWrapper {
|
|
|
|
endpoints: parity_dapps::Endpoints,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl rpc_apis::DappsService for DappsServiceWrapper {
|
|
|
|
fn list_dapps(&self) -> Vec<rpc_apis::LocalDapp> {
|
|
|
|
self.endpoints.list()
|
|
|
|
.into_iter()
|
|
|
|
.map(|app| rpc_apis::LocalDapp {
|
2018-02-15 11:05:20 +01:00
|
|
|
id: app.id.unwrap_or_else(|| "unknown".into()),
|
2017-05-24 12:24:07 +02:00
|
|
|
name: app.name,
|
|
|
|
description: app.description,
|
|
|
|
version: app.version,
|
|
|
|
author: app.author,
|
|
|
|
icon_url: app.icon_url,
|
2017-11-21 15:02:54 +01:00
|
|
|
local_url: app.local_url,
|
2017-05-24 12:24:07 +02:00
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
2017-08-09 19:06:40 +02:00
|
|
|
|
|
|
|
fn refresh_local_dapps(&self) -> bool {
|
|
|
|
self.endpoints.refresh_local_dapps();
|
|
|
|
true
|
|
|
|
}
|
2017-05-24 12:24:07 +02:00
|
|
|
}
|
2016-08-23 19:28:21 +02:00
|
|
|
}
|