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
|
|
|
|
|
|
|
use dir::default_data_path;
|
2017-03-22 22:00:52 +01:00
|
|
|
use ethcore::client::{Client, BlockChainClient, BlockId};
|
|
|
|
use ethcore::transaction::{Transaction, Action};
|
2017-05-18 12:44:09 +02:00
|
|
|
use ethsync::LightSync;
|
|
|
|
use futures::{future, IntoFuture, Future, BoxFuture};
|
2017-01-30 10:46:50 +01:00
|
|
|
use hash_fetch::fetch::Client as FetchClient;
|
2017-03-22 22:00:52 +01:00
|
|
|
use hash_fetch::urlhint::ContractClient;
|
2016-07-25 16:09:47 +02:00
|
|
|
use helpers::replace_home;
|
2017-05-18 12:44:09 +02:00
|
|
|
use light::client::Client as LightClient;
|
|
|
|
use light::on_demand::{self, OnDemand};
|
2017-05-24 12:24:07 +02:00
|
|
|
use rpc;
|
2017-04-03 10:27:37 +02:00
|
|
|
use rpc_apis::SignerService;
|
2017-03-22 07:02:14 +01:00
|
|
|
use parity_reactor;
|
2017-05-18 12:44:09 +02:00
|
|
|
use util::{Bytes, 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>,
|
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![],
|
2016-07-25 16:09:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-24 12:24:07 +02:00
|
|
|
impl Configuration {
|
|
|
|
pub fn address(&self, address: Option<(String, u16)>) -> Option<(String, u16)> {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
impl ContractClient for FullRegistrar {
|
|
|
|
fn registrar(&self) -> Result<Address, String> {
|
|
|
|
self.client.additional_params().get("registrar")
|
|
|
|
.ok_or_else(|| "Registrar not defined.".into())
|
|
|
|
.and_then(|registrar| {
|
|
|
|
registrar.parse().map_err(|e| format!("Invalid registrar address: {:?}", e))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2017-05-18 12:44:09 +02:00
|
|
|
fn call(&self, address: Address, data: Bytes) -> BoxFuture<Bytes, String> {
|
|
|
|
self.client.call_contract(BlockId::Latest, address, data)
|
|
|
|
.into_future()
|
|
|
|
.boxed()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Registrar implementation for the light client.
|
|
|
|
pub struct LightRegistrar {
|
|
|
|
/// The light client.
|
|
|
|
pub client: Arc<LightClient>,
|
|
|
|
/// Handle to the on-demand service.
|
|
|
|
pub on_demand: Arc<OnDemand>,
|
|
|
|
/// Handle to the light network service.
|
|
|
|
pub sync: Arc<LightSync>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ContractClient for LightRegistrar {
|
|
|
|
fn registrar(&self) -> Result<Address, String> {
|
|
|
|
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))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&self, address: Address, data: Bytes) -> BoxFuture<Bytes, String> {
|
|
|
|
let (header, env_info) = (self.client.best_block_header(), self.client.latest_env_info());
|
|
|
|
|
|
|
|
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 {
|
|
|
|
nonce: self.client.engine().account_start_nonce(),
|
|
|
|
action: Action::Call(address),
|
|
|
|
gas: 50_000_000.into(),
|
|
|
|
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 {
|
|
|
|
Some(fut) => fut.boxed(),
|
|
|
|
None => future::err("cannot query registry: network disabled".into()).boxed(),
|
|
|
|
}
|
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-04-06 19:32:30 +02:00
|
|
|
pub sync_status: Arc<SyncStatus>,
|
2017-03-22 22:00:52 +01:00
|
|
|
pub contract_client: Arc<ContractClient>,
|
2017-03-22 07:02:14 +01:00
|
|
|
pub remote: parity_reactor::TokioRemote,
|
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-05-24 12:24:07 +02:00
|
|
|
rpc::DAPPS_DOMAIN.into(),
|
|
|
|
).map(Some)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn new_ui(enabled: bool, deps: Dependencies) -> Result<Option<Middleware>, String> {
|
|
|
|
if !enabled {
|
|
|
|
return Ok(None);
|
|
|
|
}
|
|
|
|
|
|
|
|
server::ui_middleware(
|
|
|
|
deps,
|
|
|
|
rpc::DAPPS_DOMAIN.into(),
|
2017-04-03 10:27:37 +02:00
|
|
|
).map(Some)
|
2016-04-21 13:57:27 +02:00
|
|
|
}
|
|
|
|
|
2017-05-24 12:24:07 +02:00
|
|
|
pub use self::server::{SyncStatus, 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 type SyncStatus = Fn() -> bool;
|
2017-04-03 10:27:37 +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 {
|
|
|
|
fn on_request(
|
2017-04-06 19:32:30 +02:00
|
|
|
&self, _req: &hyper::server::Request<hyper::net::HttpStream>, _control: &hyper::Control
|
2017-04-03 10:27:37 +02:00
|
|
|
) -> RequestMiddlewareAction {
|
|
|
|
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-05-24 12:24:07 +02:00
|
|
|
_dapps_domain: String,
|
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,
|
|
|
|
_dapps_domain: String,
|
|
|
|
) -> 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;
|
2017-01-11 20:02:27 +01:00
|
|
|
use parity_reactor;
|
2016-08-23 19:28:21 +02:00
|
|
|
|
2017-04-05 11:37:45 +02:00
|
|
|
pub use parity_dapps::Middleware;
|
2017-04-06 19:32:30 +02:00
|
|
|
pub use parity_dapps::SyncStatus;
|
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-05-24 12:24:07 +02:00
|
|
|
dapps_domain: String,
|
2017-04-03 10:27:37 +02:00
|
|
|
) -> Result<Middleware, String> {
|
2017-05-24 12:24:07 +02:00
|
|
|
let signer = deps.signer;
|
2017-04-03 10:27:37 +02:00
|
|
|
let parity_remote = parity_reactor::Remote::new(deps.remote.clone());
|
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-04-03 10:27:37 +02:00
|
|
|
parity_remote,
|
2017-05-24 12:24:07 +02:00
|
|
|
deps.ui_address,
|
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,
|
|
|
|
dapps_domain: String,
|
|
|
|
) -> Result<Middleware, String> {
|
|
|
|
let parity_remote = parity_reactor::Remote::new(deps.remote.clone());
|
|
|
|
Ok(parity_dapps::Middleware::ui(
|
|
|
|
parity_remote,
|
|
|
|
deps.contract_client,
|
|
|
|
deps.sync_status,
|
|
|
|
deps.fetch,
|
|
|
|
dapps_domain,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn service(middleware: &Option<Middleware>) -> Option<Arc<rpc_apis::DappsService>> {
|
|
|
|
middleware.as_ref().map(|m| Arc::new(DappsServiceWrapper {
|
|
|
|
endpoints: m.endpoints()
|
|
|
|
}) 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 {
|
|
|
|
id: app.id,
|
|
|
|
name: app.name,
|
|
|
|
description: app.description,
|
|
|
|
version: app.version,
|
|
|
|
author: app.author,
|
|
|
|
icon_url: app.icon_url,
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
}
|
2016-08-23 19:28:21 +02:00
|
|
|
}
|