basic layout of sync executable and minor fixes in the api

This commit is contained in:
NikVolf 2016-07-15 15:32:29 +02:00
parent c60e02d151
commit 507a4ea26c
7 changed files with 162 additions and 6 deletions

9
Cargo.lock generated
View File

@ -30,6 +30,7 @@ dependencies = [
"rpassword 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "rpassword 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc_version 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", "rustc_version 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
"schedule_recv 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"semver 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", "semver 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"syntex 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)", "syntex 0.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
"time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)",
@ -1129,6 +1130,14 @@ dependencies = [
"semver 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)", "semver 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]]
name = "schedule_recv"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"lazy_static 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "semver" name = "semver"
version = "0.1.20" version = "0.1.20"

View File

@ -38,6 +38,7 @@ ethcore-ipc-hypervisor = { path = "ipc/hypervisor" }
json-ipc-server = { git = "https://github.com/ethcore/json-ipc-server.git" } json-ipc-server = { git = "https://github.com/ethcore/json-ipc-server.git" }
ethcore-dapps = { path = "dapps", optional = true } ethcore-dapps = { path = "dapps", optional = true }
clippy = { version = "0.0.79", optional = true} clippy = { version = "0.0.79", optional = true}
schedule_recv = "0.1"
[target.'cfg(windows)'.dependencies] [target.'cfg(windows)'.dependencies]
winapi = "0.2" winapi = "0.2"
@ -61,6 +62,10 @@ travis-nightly = ["ethcore/json-tests", "dev"]
path = "parity/main.rs" path = "parity/main.rs"
name = "parity" name = "parity"
[[bin]]
path = "parity/sync/main.rs"
name = "sync"
[profile.release] [profile.release]
debug = true debug = true
lto = false lto = false

View File

@ -33,11 +33,12 @@ use service::{HypervisorService, IpcModuleId};
use std::process::{Command,Child}; use std::process::{Command,Child};
use std::collections::HashMap; use std::collections::HashMap;
pub use service::{HypervisorServiceClient, CLIENT_MODULE_ID}; pub use service::{HypervisorServiceClient, CLIENT_MODULE_ID, SYNC_MODULE_ID};
type BinaryId = &'static str; type BinaryId = &'static str;
const CLIENT_BINARY: BinaryId = "client"; const CLIENT_BINARY: BinaryId = "client";
const SYNC_BINARY: BinaryId = "sync";
pub struct Hypervisor { pub struct Hypervisor {
ipc_addr: String, ipc_addr: String,
@ -76,6 +77,7 @@ impl Hypervisor {
fn match_module(module_id: &IpcModuleId) -> Option<BinaryId> { fn match_module(module_id: &IpcModuleId) -> Option<BinaryId> {
match *module_id { match *module_id {
CLIENT_MODULE_ID => Some(CLIENT_BINARY), CLIENT_MODULE_ID => Some(CLIENT_BINARY),
SYNC_MODULE_ID => Some(SYNC_BINARY),
// none means the module is inside the main binary // none means the module is inside the main binary
_ => None _ => None
} }

View File

@ -26,6 +26,9 @@ pub type IpcModuleId = u64;
/// Blockhain database module id /// Blockhain database module id
pub const CLIENT_MODULE_ID: IpcModuleId = 2000; pub const CLIENT_MODULE_ID: IpcModuleId = 2000;
/// Sync module id
pub const SYNC_MODULE_ID: IpcModuleId = 2100;
/// IPC service that handles module management /// IPC service that handles module management
pub struct HypervisorService { pub struct HypervisorService {
check_list: RwLock<HashMap<IpcModuleId, bool>>, check_list: RwLock<HashMap<IpcModuleId, bool>>,

View File

@ -54,9 +54,9 @@ impl<S> GuardedSocket<S> where S: WithSocket<Socket> {
} }
impl<S> Deref for GuardedSocket<S> where S: WithSocket<Socket> { impl<S> Deref for GuardedSocket<S> where S: WithSocket<Socket> {
type Target = S; type Target = Arc<S>;
fn deref(&self) -> &S { fn deref(&self) -> &Arc<S> {
&self.client &self.client
} }
} }

137
parity/sync/main.rs Normal file
View File

@ -0,0 +1,137 @@
// 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/>.
//! Parity database ipc service
extern crate ethcore_ipc_nano as nanoipc;
extern crate ethcore_ipc_hypervisor as hypervisor;
extern crate ethcore_ipc as ipc;
extern crate ctrlc;
#[macro_use] extern crate log;
extern crate ethsync;
extern crate rustc_serialize;
extern crate docopt;
extern crate ethcore;
extern crate ethcore_util as util;
use std::sync::Arc;
use hypervisor::{HypervisorServiceClient, CLIENT_MODULE_ID, SYNC_MODULE_ID, HYPERVISOR_IPC_URL};
use ctrlc::CtrlC;
use std::sync::atomic::*;
use docopt::Docopt;
use ethcore::client::{BlockChainClient, RemoteClient};
use nanoipc::*;
use ethsync::{SyncProvider, SyncConfig, EthSync, ManageNetwork, NetworkConfiguration};
use std::thread;
use util::numbers::*;
use std::str::FromStr;
const USAGE: &'static str = "
Ethcore sync service
Usage:
sync <client-url> <network-id> <listen-address> <nat-enabled> <discovery-enabled> <ideal-peers> <config-path> <allow-non-reserved> [options]
Options:
--public-address IP Public address.
--boot-nodes LIST List of boot nodes.
--reserved-nodes LIST List of reserved peers,
--secret HEX Use node key hash
--udp-port UDP port
";
#[derive(Debug, RustcDecodable)]
struct Args {
arg_network_id: String,
arg_listen_address: String,
arg_nat_enabled: bool,
arg_discovery_enabled: bool,
arg_ideal_peers: u32,
arg_config_path: String,
arg_client_url: String,
arg_allow_non_reserved: bool,
flag_public_address: Option<String>,
flag_secret: Option<String>,
flag_boot_nodes: Vec<String>,
flag_reserved_nodes: Vec<String>,
flag_udp_port: Option<u16>,
}
impl Args {
pub fn into_config(self) -> (SyncConfig, NetworkConfiguration, String) {
let mut sync_config = SyncConfig::default();
sync_config.network_id = U256::from_str(&self.arg_network_id).unwrap();
let mut network_config = NetworkConfiguration {
udp_port: self.flag_udp_port,
nat_enabled: self.arg_nat_enabled,
boot_nodes: self.flag_boot_nodes,
listen_address: Some(self.arg_listen_address),
public_address: self.flag_public_address,
use_secret: self.flag_secret.as_ref().map(|s| H256::from_str(s).unwrap()),
discovery_enabled: self.arg_discovery_enabled,
ideal_peers: self.arg_ideal_peers,
config_path: Some(self.arg_config_path),
reserved_nodes: self.flag_reserved_nodes,
allow_non_reserved: self.arg_allow_non_reserved,
};
(sync_config, network_config, self.arg_client_url)
}
}
fn run_service<T: ?Sized + Send + Sync + 'static>(addr: &str, stop_guard: Arc<AtomicBool>, service: &Arc<T>) where Arc<T>: IpcInterface<T> {
let socket_url = addr.to_owned();
let service_spawn = service.clone();
std::thread::spawn(move || {
let mut worker = nanoipc::Worker::<T>::new(&service_spawn);
worker.add_reqrep(&socket_url).unwrap();
while !stop_guard.load(Ordering::Relaxed) {
worker.poll();
}
});
}
fn main() {
use std::ops::Deref;
let args: Args = Docopt::new(USAGE)
.and_then(|d| d.decode())
.unwrap_or_else(|e| e.exit());
let (sync_config, network_config, client_url) = args.into_config();
let remote_client = nanoipc::init_client::<RemoteClient<_>>(&client_url).unwrap();
remote_client.handshake().unwrap();
let stop = Arc::new(AtomicBool::new(false));
let sync = EthSync::new(sync_config, remote_client.clone(), network_config).unwrap();
run_service("ipc:///tmp/parity-sync.ipc", stop.clone(), &(sync.clone() as Arc<SyncProvider>));
run_service("ipc:///tmp/parity-manage-net.ipc", stop.clone(), &(sync.clone() as Arc<ManageNetwork>));
let hypervisor_client = nanoipc::init_client::<HypervisorServiceClient<_>>(HYPERVISOR_IPC_URL).unwrap();
hypervisor_client.handshake().unwrap();
hypervisor_client.module_ready(SYNC_MODULE_ID);
let terminate_stop = stop.clone();
CtrlC::set_handler(move || {
terminate_stop.store(true, Ordering::Relaxed);
});
while !stop.load(Ordering::Relaxed) {
thread::park_timeout(std::time::Duration::from_millis(1000));
}
}

View File

@ -19,7 +19,7 @@ use std::sync::Arc;
use util::network::{NetworkProtocolHandler, NetworkService, NetworkContext, PeerId, use util::network::{NetworkProtocolHandler, NetworkService, NetworkContext, PeerId,
NetworkConfiguration as BasicNetworkConfiguration, NonReservedPeerMode}; NetworkConfiguration as BasicNetworkConfiguration, NonReservedPeerMode};
use util::{TimerToken, U256, H256, UtilError, Secret, Populatable}; use util::{TimerToken, U256, H256, UtilError, Secret, Populatable};
use ethcore::client::{Client, ChainNotify}; use ethcore::client::{Client, BlockChainClient, ChainNotify};
use io::NetSyncIo; use io::NetSyncIo;
use chain::{ChainSync, SyncStatus}; use chain::{ChainSync, SyncStatus};
use std::net::{SocketAddr, AddrParseError}; use std::net::{SocketAddr, AddrParseError};
@ -67,7 +67,7 @@ pub struct EthSync {
impl EthSync { impl EthSync {
/// Creates and register protocol with the network service /// Creates and register protocol with the network service
pub fn new(config: SyncConfig, chain: Arc<Client>, network_config: NetworkConfiguration) -> Result<Arc<EthSync>, UtilError> { pub fn new(config: SyncConfig, chain: Arc<BlockChainClient>, network_config: NetworkConfiguration) -> Result<Arc<EthSync>, UtilError> {
let chain_sync = ChainSync::new(config, chain.deref()); let chain_sync = ChainSync::new(config, chain.deref());
let service = try!(NetworkService::new(try!(network_config.into_basic()))); let service = try!(NetworkService::new(try!(network_config.into_basic())));
let sync = Arc::new(EthSync{ let sync = Arc::new(EthSync{
@ -90,7 +90,7 @@ impl SyncProvider for EthSync {
struct SyncProtocolHandler { struct SyncProtocolHandler {
/// Shared blockchain client. TODO: this should evetually become an IPC endpoint /// Shared blockchain client. TODO: this should evetually become an IPC endpoint
chain: Arc<Client>, chain: Arc<BlockChainClient>,
/// Sync strategy /// Sync strategy
sync: RwLock<ChainSync>, sync: RwLock<ChainSync>,
} }