Refactor into system service, add exe redirect.

This commit is contained in:
Gav Wood
2016-12-10 23:58:39 +01:00
parent 316c25d9f8
commit fa30dfd4b9
18 changed files with 392 additions and 383 deletions

View File

@@ -84,7 +84,7 @@ impl Client {
impl HashFetch for Client {
fn fetch(&self, hash: H256, on_done: Box<Fn(Result<PathBuf, Error>) + Send>) -> Result<(), Error> {
debug!(target: "dapps", "Fetching: {:?}", hash);
debug!(target: "fetch", "Fetching: {:?}", hash);
let url = try!(
self.contract.resolve(hash.to_vec()).map(|content| match content {
@@ -97,7 +97,7 @@ impl HashFetch for Client {
}).ok_or_else(|| Error::NoResolution)
);
debug!(target: "dapps", "Resolved {:?} to {:?}. Fetching...", hash, url);
debug!(target: "fetch", "Resolved {:?} to {:?}. Fetching...", hash, url);
self.fetch.lock().request_async(&url, Default::default(), Box::new(move |result| {
fn validate_hash(hash: H256, result: Result<PathBuf, FetchError>) -> Result<PathBuf, Error> {
@@ -112,7 +112,7 @@ impl HashFetch for Client {
}
}
debug!(target: "dapps", "Content fetched, validating hash ({:?})", hash);
debug!(target: "fetch", "Content fetched, validating hash ({:?})", hash);
on_done(validate_hash(hash, result))
})).map_err(Into::into)
}

View File

@@ -70,10 +70,7 @@ use factory::Factories;
use rlp::{decode, View, UntrustedRlp};
use state_db::StateDB;
use rand::OsRng;
use client::updater::Updater;
use client::registry::Registry;
use client::fetch::FetchHandler;
use fetch::{self, HashFetch};
// re-export
pub use types::blockchain_info::BlockChainInfo;
@@ -144,7 +141,6 @@ pub struct Client {
panic_handler: Arc<PanicHandler>,
verifier: Box<Verifier>,
miner: Arc<Miner>,
updater: Mutex<Option<Updater>>,
sleep_state: Mutex<SleepState>,
liveness: AtomicBool,
io_channel: Mutex<IoChannel<ClientIoMessage>>,
@@ -156,7 +152,6 @@ pub struct Client {
rng: Mutex<OsRng>,
on_mode_change: Mutex<Option<Box<FnMut(&Mode) + 'static + Send>>>,
registrar: Mutex<Option<Registry>>,
fetch_service: Mutex<Option<Arc<HashFetch>>>,
}
impl Client {
@@ -247,7 +242,6 @@ impl Client {
import_lock: Mutex::new(()),
panic_handler: panic_handler,
miner: miner,
updater: Mutex::new(None),
io_channel: Mutex::new(message_channel),
notify: RwLock::new(Vec::new()),
queue_transactions: AtomicUsize::new(0),
@@ -257,23 +251,12 @@ impl Client {
rng: Mutex::new(try!(OsRng::new().map_err(::util::UtilError::StdIo))),
on_mode_change: Mutex::new(None),
registrar: Mutex::new(None),
fetch_service: Mutex::new(None),
});
if let Some(reg_addr) = client.additional_params().get("registrar").and_then(|s| Address::from_str(s).ok()) {
trace!(target: "client", "Found registrar at {}", reg_addr);
let weak = Arc::downgrade(&client);
let fetch = Arc::new(fetch::Client::new(Arc::new(FetchHandler::new(weak.clone()))));
let registrar = Registry::new(reg_addr, move |a, d| weak.upgrade().ok_or("No client!".into()).and_then(|c| c.call_contract(a, d)));
// TODO [ToDr] The address might not be available when client is starting (but may be available later).
// Shouldn't this be moved inside the `Updater`?
if let Ok(ops_addr) = registrar.get_address(&(&b"operations"[..]).sha3(), "A") {
if !ops_addr.is_zero() {
trace!(target: "client", "Found operations at {}", ops_addr);
*client.updater.lock() = Some(Updater::new(Arc::downgrade(&client), Arc::downgrade(&fetch), ops_addr, client.config.update_policy.clone()));
}
}
*client.registrar.lock() = Some(registrar);
*client.fetch_service.lock() = Some(fetch);
}
Ok(client)
}
@@ -686,12 +669,6 @@ impl Client {
pub fn tick(&self) {
self.check_garbage();
self.check_snooze();
if let Some(ref mut updater) = *self.updater.lock() {
updater.tick();
if updater.installed.is_some() {
info!("Client should restart now.");
}
}
}
fn check_garbage(&self) {
@@ -734,30 +711,6 @@ impl Client {
}
}
/// Like `call`, but with various defaults. Designed to be used for calling contracts.
pub fn call_contract(&self, address: Address, data: Bytes) -> Result<Bytes, String> {
let from = Address::default();
let transaction = Transaction {
nonce: self.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);
self.call(&transaction, BlockId::Latest, Default::default())
.map_err(|e| format!("{:?}", e))
.map(|executed| {
executed.output
})
}
/// Get the updater object.
pub fn updater(&self) -> MutexGuard<Option<Updater>> {
self.updater.lock()
}
/// Look up the block number for the given block ID.
pub fn block_number(&self, id: BlockId) -> Option<BlockNumber> {
match id {
@@ -1377,6 +1330,34 @@ impl BlockChainClient for Client {
earliest_state: self.state_db.lock().journal_db().earliest_era().unwrap_or(0),
}
}
fn call_contract(&self, address: Address, data: Bytes) -> Result<Bytes, String> {
let from = Address::default();
let transaction = Transaction {
nonce: self.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);
self.call(&transaction, BlockId::Latest, Default::default())
.map_err(|e| format!("{:?}", e))
.map(|executed| {
executed.output
})
}
fn registrar_address(&self) -> Option<Address> {
self.registrar.lock().as_ref().map(|r| r.address.clone())
}
fn registry_address(&self, name: String) -> Option<Address> {
self.registrar.lock().as_ref()
.and_then(|r| r.get_address(&(name.as_bytes().sha3()), "A").ok())
.and_then(|a| if a.is_zero() { None } else { Some(a) })
}
}
impl MiningBlockChainClient for Client {

View File

@@ -66,35 +66,6 @@ impl FromStr for DatabaseCompactionProfile {
}
}
/// Filter for releases.
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum UpdateFilter {
/// All releases following the same track.
All,
/// As with `All`, but only those which are known to be critical.
Critical,
/// None.
None,
}
/// The policy for auto-updating.
#[derive(Debug, Eq, PartialEq, Clone)]
pub struct UpdatePolicy {
/// Download potential updates.
pub enable_downloading: bool,
/// Which of those downloaded should be automatically installed.
pub filter: UpdateFilter,
}
impl Default for UpdatePolicy {
fn default() -> Self {
UpdatePolicy {
enable_downloading: false,
filter: UpdateFilter::None,
}
}
}
/// Operating mode for the client.
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum Mode {
@@ -130,8 +101,6 @@ impl Display for Mode {
/// Client configuration. Includes configs for all sub-systems.
#[derive(Debug, PartialEq, Default)]
pub struct ClientConfig {
/// Updater policy.
pub update_policy: UpdatePolicy,
/// Block queue configuration.
pub queue: QueueConfig,
/// Blockchain configuration.

View File

@@ -1,49 +0,0 @@
// 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::sync::Weak;
use std::str::FromStr;
use util::{Bytes, Address};
use client::{Client, BlockChainClient};
use fetch;
/// Client wrapper implementing `fetch::urlhint::ContractClient`
pub struct FetchHandler {
client: Weak<Client>,
}
impl FetchHandler {
/// Creates new wrapper
pub fn new(client: Weak<Client>) -> Self {
FetchHandler { client: client }
}
}
impl fetch::urlhint::ContractClient for FetchHandler {
fn registrar(&self) -> Result<Address, String> {
self.client.upgrade().ok_or_else(|| "Client not available".to_owned())?
.additional_params()
.get("registrar")
.and_then(|s| Address::from_str(s).ok())
.ok_or_else(|| "Registrar not available".into())
}
fn call(&self, address: Address, data: Bytes) -> Result<Bytes, String> {
self.client.upgrade().ok_or_else(|| "Client not available".to_owned())?
.call_contract(address, data)
}
}

View File

@@ -16,18 +16,15 @@
//! Blockchain database client.
mod operations;
mod registry;
mod config;
mod error;
mod test_client;
mod trace;
mod client;
mod updater;
mod fetch;
pub use self::client::*;
pub use self::config::{Mode, ClientConfig, UpdatePolicy, UpdateFilter, DatabaseCompactionProfile, BlockChainConfig, VMType};
pub use self::config::{Mode, ClientConfig, DatabaseCompactionProfile, BlockChainConfig, VMType};
pub use self::error::Error;
pub use self::test_client::{TestBlockChainClient, EachBlockWith};
pub use self::chain_notify::ChainNotify;

File diff suppressed because one or more lines are too long

View File

@@ -9,7 +9,7 @@ use util::Uint;
pub struct Registry {
contract: ethabi::Contract,
address: util::Address,
pub address: util::Address,
do_call: Box<Fn(util::Address, Vec<u8>) -> Result<Vec<u8>, String> + Send + 'static>,
}
impl Registry {

View File

@@ -679,4 +679,10 @@ impl BlockChainClient for TestBlockChainClient {
earliest_state: 1,
}
}
fn call_contract(&self, _address: Address, _data: Bytes) -> Result<Bytes, String> { Ok(vec![]) }
fn registrar_address(&self) -> Option<Address> { None }
fn registry_address(&self, _name: String) -> Option<Address> { None }
}

View File

@@ -257,6 +257,15 @@ pub trait BlockChainClient : Sync + Send {
/// Returns information about pruning/data availability.
fn pruning_info(&self) -> PruningInfo;
/// Like `call`, but with various defaults. Designed to be used for calling contracts.
fn call_contract(&self, address: Address, data: Bytes) -> Result<Bytes, String>;
/// Get the address of the registry itself.
fn registrar_address(&self) -> Option<Address>;
/// Get the address of a particular blockchain service, if available.
fn registry_address(&self, name: String) -> Option<Address>;
}
impl IpcConfig for BlockChainClient { }

View File

@@ -1,274 +0,0 @@
// 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::sync::{Weak};
use std::{io, os, fs, env};
use std::path::{Path, PathBuf};
use util::misc::{VersionInfo, ReleaseTrack/*, platform*/};
use util::{Address, H160, H256, FixedHash, Mutex};
use client::operations::Operations;
use client::{Client, UpdatePolicy, UpdateFilter, BlockId};
use fetch::HashFetch;
use fetch;
#[derive(Debug, Clone, PartialEq)]
pub struct ReleaseInfo {
pub version: VersionInfo,
pub is_critical: bool,
pub fork: u64,
pub binary: Option<H256>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct OperationsInfo {
pub fork: u64,
pub track: ReleaseInfo,
pub minor: Option<ReleaseInfo>,
}
pub struct Updater {
client: Weak<Client>,
fetch: Weak<HashFetch>,
operations: Operations,
update_policy: UpdatePolicy,
fetching: Mutex<Option<ReleaseInfo>>,
// These don't change
pub this: VersionInfo,
pub this_fork: Option<u64>,
// This does change
pub latest: Option<OperationsInfo>,
pub ready: Option<ReleaseInfo>,
// If Some, client should restart itself.
pub installed: Option<ReleaseInfo>,
}
const CLIENT_ID: &'static str = "parity";
fn platform() -> String {
"test".to_owned()
}
impl Updater {
pub fn new(client: Weak<Client>, fetch: Weak<fetch::Client>, operations: Address, update_policy: UpdatePolicy) -> Self {
let mut u = Updater {
client: client.clone(),
fetch: fetch.clone(),
operations: Operations::new(operations, move |a, d| client.upgrade().ok_or("No client!".into()).and_then(|c| c.call_contract(a, d))),
update_policy: update_policy,
fetching: Mutex::new(None),
this: VersionInfo::this(),
this_fork: None,
latest: None,
ready: None,
installed: None,
};
u.this_fork = u.operations.release(CLIENT_ID, &u.this.hash.into()).ok()
.and_then(|(fork, track, _, _)| if track > 0 {Some(fork as u64)} else {None});
// TODO!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! REMOVE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
if u.this.track == ReleaseTrack::Unknown {
u.this.track = ReleaseTrack::Nightly;
}
u.latest = u.collect_latest().ok();
u
}
/// Is the currently running client capable of supporting the current chain?
/// `Some` answer or `None` if information on the running client is not available.
pub fn is_capable(&self) -> Option<bool> {
self.latest.as_ref().and_then(|latest| {
self.this_fork.map(|this_fork| {
let current_number = self.client.upgrade().map_or(0, |c| c.block_number(BlockId::Latest).unwrap_or(0));
this_fork >= latest.fork || current_number < latest.fork
})
})
}
/// The release which is ready to be upgraded to, if any. If this returns `Some`, then
/// `execute_upgrade` may be called.
pub fn upgrade_ready(&self) -> Option<ReleaseInfo> {
self.ready.clone()
}
#[cfg(windows)]
fn symlink<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> {
os::windows::fs::symlink_file(src, dst)
}
#[cfg(not(windows))]
fn symlink<P: AsRef<Path>, Q: AsRef<Path>>(src: P, dst: Q) -> io::Result<()> {
os::unix::fs::symlink(src, dst)
}
/// Actually upgrades the client. Assumes that the binary has been downloaded.
/// @returns `true` on success.
pub fn execute_upgrade(&mut self) -> bool {
(|| -> Result<bool, String> {
if let Some(r) = self.ready.take() {
let p = Self::update_file_path(&r.version);
let n = Self::updates_latest();
let _ = fs::remove_file(&n);
match Self::symlink(p, n) {
Ok(_) => {
info!("Completed upgrade to {}", &r.version);
self.installed = Some(r);
Ok(true)
}
Err(e) => {
self.ready = Some(r);
Err(format!("Unable to create soft-link for update {:?}", e))
}
}
} else {
warn!("Execute upgrade called when no upgrade ready.");
Ok(false)
}
})().unwrap_or_else(|e| { warn!("{}", e); false })
}
/// Returns true iff the current version is capable of forming consensus.
pub fn consensus_capable(&self) -> bool {
/* if let Some(ref latest) = self.latest {
*/ unimplemented!()
}
/// Our version info.
pub fn version_info(&self) -> &VersionInfo { &self.this }
/// Information gathered concerning the release.
pub fn info(&self) -> &Option<OperationsInfo> { &self.latest }
fn collect_release_info(&self, release_id: &H256) -> Result<ReleaseInfo, String> {
let (fork, track, semver, is_critical) = self.operations.release(CLIENT_ID, release_id)?;
let latest_binary = self.operations.checksum(CLIENT_ID, release_id, &platform())?;
Ok(ReleaseInfo {
version: VersionInfo::from_raw(semver, track, release_id.clone().into()),
is_critical: is_critical,
fork: fork as u64,
binary: if latest_binary.is_zero() { None } else { Some(latest_binary) },
})
}
fn collect_latest(&self) -> Result<OperationsInfo, String> {
if self.this.track == ReleaseTrack::Unknown {
return Err(format!("Current executable ({}) is unreleased.", H160::from(self.this.hash)));
}
let latest_in_track = self.operations.latest_in_track(CLIENT_ID, self.this.track.into())?;
let in_track = self.collect_release_info(&latest_in_track)?;
let mut in_minor = Some(in_track.clone());
const PROOF: &'static str = "in_minor initialised and assigned with Some; loop breaks if None assigned; qed";
while in_minor.as_ref().expect(PROOF).version.track != self.this.track {
let track = match in_minor.as_ref().expect(PROOF).version.track {
ReleaseTrack::Beta => ReleaseTrack::Stable,
ReleaseTrack::Nightly => ReleaseTrack::Beta,
_ => { in_minor = None; break; }
};
in_minor = Some(self.collect_release_info(&self.operations.latest_in_track(CLIENT_ID, track.into())?)?);
}
Ok(OperationsInfo {
fork: self.operations.latest_fork()? as u64,
track: in_track,
minor: in_minor,
})
}
fn update_file_path(v: &VersionInfo) -> PathBuf {
let mut dest = PathBuf::from(env::home_dir().unwrap().to_str().expect("env filesystem paths really should be valid; qed"));
dest.push(".parity-updates");
dest.push(format!("parity-{}.{}.{}-{:?}", v.version.major, v.version.minor, v.version.patch, v.hash));
dest
}
fn updates_latest() -> PathBuf {
let mut dest = PathBuf::from(env::home_dir().unwrap().to_str().expect("env filesystem paths really should be valid; qed"));
dest.push(".parity-updates");
dest.push("parity");
dest
}
fn fetch_done(&mut self, result: Result<PathBuf, fetch::Error>) {
(|| -> Result<(), String> {
let fetched = self.fetching.lock().take().unwrap();
let b = result.map_err(|e| format!("Unable to fetch update ({}): {:?}", fetched.version, e))?;
info!("Fetched latest version ({}) OK to {}", fetched.version, b.display());
let dest = Self::update_file_path(&fetched.version);
fs::create_dir_all(dest.parent().expect("at least one thing pushed; qed")).map_err(|e| format!("Unable to create updates path: {:?}", e))?;
fs::copy(&b, &dest).map_err(|e| format!("Unable to copy update: {:?}", e))?;
info!("Copied file to {}", dest.display());
let auto = match self.update_policy.filter {
UpdateFilter::All => true,
UpdateFilter::Critical if fetched.is_critical /* TODO: or is on a bad fork */ => true,
_ => false,
};
self.ready = Some(fetched);
if auto {
self.execute_upgrade();
}
Ok(())
})().unwrap_or_else(|e| warn!("{}", e));
}
pub fn tick(&mut self) {
info!(target: "updater", "Current release is {}", self.this);
self.latest = self.collect_latest().ok();
let current_number = self.client.upgrade().map_or(0, |c| c.block_number(BlockId::Latest).unwrap_or(0));
if let Some(ref latest) = self.latest {
info!(target: "updater", "Latest release in our track is v{} it is {}critical ({} binary is {})",
latest.track.version,
if latest.track.is_critical {""} else {"non-"},
platform(),
if let Some(ref b) = latest.track.binary {
format!("{}", b)
} else {
"unreleased".into()
}
);
if self.update_policy.enable_downloading && latest.track.version.hash != self.version_info().hash && self.installed.as_ref().or(self.ready.as_ref()).map_or(true, |t| *t != latest.track) {
if let Some(b) = latest.track.binary {
let mut fetching = self.fetching.lock();
if fetching.is_none() {
info!("Attempting to get parity binary {}", b);
let c = self.client.clone();
let f = move |r: Result<PathBuf, fetch::Error>| {
if let Some(client) = c.upgrade() {
if let Some(updater) = client.updater().as_mut() {
updater.fetch_done(r);
}
}
};
if let Some(fetch) = self.fetch.clone().upgrade() {
fetch.fetch(b, Box::new(f)).ok();
*fetching = Some(latest.track.clone());
}
}
}
}
info!(target: "updater", "Fork: this/current/latest/latest-known: {}/#{}/#{}/#{}", match self.this_fork { Some(f) => format!("#{}", f), None => "unknown".into(), }, current_number, latest.track.fork, latest.fork);
}
}
}