// Copyright 2017 Parity Technologies (UK) Ltd. // This file is part of Polkadot. // Polkadot 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. // Polkadot 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 Polkadot. If not, see . //! Polkadot service. Starts a thread that spins the network, the client and the transaction pool. //! Manages communication between them. extern crate futures; extern crate ed25519; extern crate clap; extern crate exit_future; extern crate tokio_timer; extern crate polkadot_primitives; extern crate polkadot_runtime; extern crate polkadot_executor; extern crate polkadot_api; extern crate polkadot_consensus as consensus; extern crate polkadot_transaction_pool as transaction_pool; extern crate substrate_keystore as keystore; extern crate substrate_runtime_io as runtime_io; extern crate substrate_primitives as primitives; extern crate substrate_runtime_primitives as runtime_primitives; extern crate substrate_network as network; extern crate substrate_codec as codec; extern crate substrate_executor; extern crate substrate_state_machine as state_machine; extern crate tokio_core; extern crate substrate_client as client; extern crate substrate_client_db as client_db; #[macro_use] extern crate substrate_telemetry; #[macro_use] extern crate error_chain; #[macro_use] extern crate slog; // needed until we can reexport `slog_info` from `substrate_telemetry` #[macro_use] extern crate log; mod components; mod error; mod config; use std::sync::Arc; use std::thread; use futures::prelude::*; use tokio_core::reactor::Core; use transaction_pool::TransactionPool; use keystore::Store as Keystore; use polkadot_api::PolkadotApi; use polkadot_primitives::{Block, BlockId, Hash}; use client::{Client, BlockchainEvents}; use network::ManageNetwork; use exit_future::Signal; pub use self::error::{ErrorKind, Error}; pub use self::components::{Components, FullComponents, LightComponents}; pub use config::{Configuration, Role}; /// Polkadot service. pub struct Service { thread: Option>, client: Arc>, network: Arc>, transaction_pool: Arc>, signal: Option, _consensus: Option, } /// Creates light client and register protocol with the network service pub fn new_light(config: Configuration) -> Result, error::Error> { Service::new(components::LightComponents, config) } /// Creates full client and register protocol with the network service pub fn new_full(config: Configuration) -> Result, error::Error> { let is_validator = (config.roles & Role::VALIDATOR) == Role::VALIDATOR; Service::new(components::FullComponents { is_validator }, config) } impl Service where Components: components::Components, client::error::Error: From<<<::Backend as client::backend::Backend>::State as state_machine::Backend>::Error>, { /// Creates and register protocol with the network service fn new(components: Components, config: Configuration) -> Result { use std::sync::Barrier; let (signal, exit) = ::exit_future::signal(); // Create client let executor = polkadot_executor::Executor::new(); let mut keystore = Keystore::open(config.keystore_path.into())?; for seed in &config.keys { keystore.generate_from_seed(seed)?; } if keystore.contents()?.is_empty() { let key = keystore.generate("")?; info!("Generated a new keypair: {:?}", key.public()); } let db_settings = client_db::DatabaseSettings { cache_size: None, path: config.database_path.into(), }; let (client, on_demand) = components.build_client(db_settings, executor, config.genesis_storage)?; let api = components.build_api(client.clone()); let best_header = client.best_block_header()?; info!("Best block is #{}", best_header.number); telemetry!("node.start"; "height" => best_header.number, "best" => ?best_header.hash()); let transaction_pool = Arc::new(TransactionPool::new(config.transaction_pool, api.clone())); let transaction_pool_adapter = components.build_network_tx_pool(client.clone(), transaction_pool.clone()); let network_params = network::Params { config: network::ProtocolConfig { roles: config.roles, }, network_config: config.network, chain: client.clone(), on_demand: on_demand.clone().map(|d| d as Arc), transaction_pool: transaction_pool_adapter, }; let network = network::Service::new(network_params)?; let barrier = ::std::sync::Arc::new(Barrier::new(2)); on_demand.map(|on_demand| on_demand.set_service_link(Arc::downgrade(&network))); let thread = { let client = client.clone(); let network = network.clone(); let txpool = transaction_pool.clone(); let thread_barrier = barrier.clone(); thread::spawn(move || { network.start_network(); thread_barrier.wait(); let mut core = Core::new().expect("tokio::Core could not be created"); // block notifications let network1 = network.clone(); let txpool1 = txpool.clone(); let events = client.import_notification_stream() .for_each(move |notification| { network1.on_block_imported(notification.hash, ¬ification.header); prune_imported(&*txpool1, notification.hash); Ok(()) }); core.handle().spawn(events); // transaction notifications let events = txpool.import_notification_stream() // TODO [ToDr] Consider throttling? .for_each(move |_| { network.trigger_repropagate(); Ok(()) }); core.handle().spawn(events); if let Err(e) = core.run(exit) { debug!("Polkadot service event loop shutdown with {:?}", e); } debug!("Polkadot service shutdown"); }) }; // wait for the network to start up before starting the consensus // service. barrier.wait(); // Spin consensus service if configured let consensus_service = components.build_consensus(client.clone(), network.clone(), transaction_pool.clone(), &keystore)?; Ok(Service { thread: Some(thread), client: client, network: network, transaction_pool: transaction_pool, signal: Some(signal), _consensus: consensus_service, }) } /// Get shared client instance. pub fn client(&self) -> Arc> { self.client.clone() } /// Get shared network instance. pub fn network(&self) -> Arc> { self.network.clone() } /// Get shared transaction pool instance. pub fn transaction_pool(&self) -> Arc> { self.transaction_pool.clone() } } /// Produce a task which prunes any finalized transactions from the pool. pub fn prune_imported(pool: &TransactionPool, hash: Hash) where A: PolkadotApi, { let block = BlockId::hash(hash); if let Err(e) = pool.cull(block) { warn!("Culling error: {:?}", e); } if let Err(e) = pool.retry_verification(block) { warn!("Re-verifying error: {:?}", e); } } impl Drop for Service where Components: components::Components { fn drop(&mut self) { self.network.stop_network(); if let Some(signal) = self.signal.take() { signal.fire(); } if let Some(thread) = self.thread.take() { thread.join().expect("The service thread has panicked"); } } }