Light friendly dapps (#5634)

* move native_contracts ABIs to JSON files, add urlhint

* port hash-fetch to futures, fix tests

* fix dapps compilation, defer async port to later

* activate dapps server in the light client

* better formatting
This commit is contained in:
Robert Habermeier
2017-05-18 12:44:09 +02:00
committed by Gav Wood
parent 95d9706fe1
commit b1eab698d2
23 changed files with 347 additions and 240 deletions

View File

@@ -20,12 +20,16 @@ use std::sync::Arc;
use dir::default_data_path;
use ethcore::client::{Client, BlockChainClient, BlockId};
use ethcore::transaction::{Transaction, Action};
use ethsync::LightSync;
use futures::{future, IntoFuture, Future, BoxFuture};
use hash_fetch::fetch::Client as FetchClient;
use hash_fetch::urlhint::ContractClient;
use helpers::replace_home;
use light::client::Client as LightClient;
use light::on_demand::{self, OnDemand};
use rpc_apis::SignerService;
use parity_reactor;
use util::{Bytes, Address, U256};
use util::{Bytes, Address};
#[derive(Debug, PartialEq, Clone)]
pub struct Configuration {
@@ -60,22 +64,61 @@ impl ContractClient for FullRegistrar {
})
}
fn call(&self, address: Address, data: Bytes) -> Result<Bytes, String> {
let from = Address::default();
let transaction = Transaction {
nonce: self.client.latest_nonce(&from),
action: Action::Call(address),
gas: U256::from(50_000_000),
gas_price: U256::default(),
value: U256::default(),
data: data,
}.fake_sign(from);
fn call(&self, address: Address, data: Bytes) -> BoxFuture<Bytes, String> {
self.client.call_contract(BlockId::Latest, address, data)
.into_future()
.boxed()
}
}
self.client.call(&transaction, BlockId::Latest, Default::default())
.map_err(|e| format!("{:?}", e))
.map(|executed| {
executed.output
})
/// 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
.transaction_proof(ctx, on_demand::request::TransactionProof {
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()),
header: header,
env_info: env_info,
engine: self.client.engine().clone(),
})
.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(),
}
}
}

View File

@@ -279,7 +279,7 @@ fn execute_light(cmd: RunCmd, can_restart: bool, logger: Arc<RotatingLogger>) ->
secret_store: account_provider,
logger: logger,
settings: Arc::new(cmd.net_settings),
on_demand: on_demand,
on_demand: on_demand.clone(),
cache: cache,
transaction_queue: txq,
dapps_interface: match cmd.dapps_conf.enabled {
@@ -290,7 +290,7 @@ fn execute_light(cmd: RunCmd, can_restart: bool, logger: Arc<RotatingLogger>) ->
true => Some(cmd.http_conf.port),
false => None,
},
fetch: fetch,
fetch: fetch.clone(),
geth_compatibility: cmd.geth_compatibility,
remote: event_loop.remote(),
});
@@ -301,9 +301,29 @@ fn execute_light(cmd: RunCmd, can_restart: bool, logger: Arc<RotatingLogger>) ->
stats: rpc_stats.clone(),
};
// the dapps server
let dapps_deps = {
let contract_client = Arc::new(::dapps::LightRegistrar {
client: service.client().clone(),
sync: light_sync.clone(),
on_demand: on_demand,
});
let sync = light_sync.clone();
dapps::Dependencies {
sync_status: Arc::new(move || sync.is_major_importing()),
contract_client: contract_client,
remote: event_loop.raw_remote(),
fetch: fetch,
signer: deps_for_rpc_apis.signer_service.clone(),
}
};
let dapps_middleware = dapps::new(cmd.dapps_conf.clone(), dapps_deps)?;
// start rpc servers
let _ws_server = rpc::new_ws(cmd.ws_conf, &dependencies)?;
let _http_server = rpc::new_http(cmd.http_conf.clone(), &dependencies, None)?;
let _http_server = rpc::new_http(cmd.http_conf.clone(), &dependencies, dapps_middleware)?;
let _ipc_server = rpc::new_ipc(cmd.ipc_conf, &dependencies)?;
// the signer server
@@ -315,8 +335,6 @@ fn execute_light(cmd: RunCmd, can_restart: bool, logger: Arc<RotatingLogger>) ->
let signing_queue = deps_for_rpc_apis.signer_service.queue();
let _signer_server = signer::start(cmd.signer_conf.clone(), signing_queue, signer_deps)?;
// TODO: Dapps
// minimal informant thread. Just prints block number every 5 seconds.
// TODO: integrate with informant.rs
let informant_client = service.client().clone();