Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Prev Previous commit
Next Next commit
Merge remote-tracking branch 'upstream/master' into re-do-5704
  • Loading branch information
tomaka committed Jun 30, 2020
commit c18cec3084af6814266af6fcbbbaabb241758b80
100 changes: 7 additions & 93 deletions client/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,8 @@ use wasm_timer::Instant;
use std::task::Poll;
use parking_lot::Mutex;

use client::Client;
use futures::{
Future, FutureExt, Stream, StreamExt,
compat::*,
sink::SinkExt,
stream,
task::{Spawn, FutureObj, SpawnError},
};
use sc_network::{NetworkService, NetworkStatus, network_state::NetworkState, PeerId};
use futures::{Future, FutureExt, Stream, StreamExt, stream, compat::*};
use sc_network::{NetworkStatus, network_state::NetworkState, PeerId};
use log::{log, warn, debug, error, Level};
use codec::{Encode, Decode};
use sp_runtime::generic::BlockId;
Expand Down Expand Up @@ -100,87 +93,8 @@ impl<T: MallocSizeOf> MallocSizeOfWasm for T {}
#[cfg(target_os = "unknown")]
impl<T> MallocSizeOfWasm for T {}

/// Substrate service.
pub struct Service<TBl, TCl, TSc, TNetStatus, TNet, TTxPool, TOc> {
client: Arc<TCl>,
task_manager: TaskManager,
select_chain: Option<TSc>,
network: Arc<TNet>,
// Sinks to propagate network status updates.
// For each element, every time the `Interval` fires we push an element on the sender.
network_status_sinks: Arc<status_sinks::StatusSinks<(TNetStatus, NetworkState)>>,
transaction_pool: Arc<TTxPool>,
// Send a signal when a spawned essential task has concluded. The next time
// the service future is polled it should complete with an error.
essential_failed_tx: TracingUnboundedSender<()>,
// A receiver for spawned essential-tasks concluding.
essential_failed_rx: TracingUnboundedReceiver<()>,
rpc_handlers: sc_rpc_server::RpcHandler<sc_rpc::Metadata>,
_rpc: Box<dyn std::any::Any + Send + Sync>,
_telemetry: Option<sc_telemetry::Telemetry>,
_telemetry_on_connect_sinks: Arc<Mutex<Vec<TracingUnboundedSender<()>>>>,
_offchain_workers: Option<Arc<TOc>>,
keystore: sc_keystore::KeyStorePtr,
marker: PhantomData<TBl>,
prometheus_registry: Option<prometheus_endpoint::Registry>,
// The base path is kept here because it can be a temporary directory which will be deleted
// when dropped
_base_path: Option<Arc<BasePath>>,
}

impl<TBl, TCl, TSc, TNetStatus, TNet, TTxPool, TOc> Unpin for Service<TBl, TCl, TSc, TNetStatus, TNet, TTxPool, TOc> {}

/// Abstraction over a Substrate service.
pub trait AbstractService: Future<Output = Result<(), Error>> + Send + Unpin + Spawn + 'static {
/// Type of block of this chain.
type Block: BlockT;
/// Backend storage for the client.
type Backend: 'static + BackendT<Self::Block>;
/// How to execute calls towards the runtime.
type CallExecutor: 'static + CallExecutor<Self::Block> + Send + Sync + Clone;
/// API that the runtime provides.
type RuntimeApi: Send + Sync;
/// Chain selection algorithm.
type SelectChain: sp_consensus::SelectChain<Self::Block>;
/// Transaction pool.
type TransactionPool: TransactionPool<Block = Self::Block> + MallocSizeOfWasm;
/// The generic Client type, the bounds here are the ones specifically required by
/// internal crates like sc_informant.
type Client:
HeaderMetadata<Self::Block, Error = sp_blockchain::Error> + UsageProvider<Self::Block>
+ BlockchainEvents<Self::Block> + HeaderBackend<Self::Block> + Send + Sync;

/// Get event stream for telemetry connection established events.
fn telemetry_on_connect_stream(&self) -> TracingUnboundedReceiver<()>;

/// return a shared instance of Telemetry (if enabled)
fn telemetry(&self) -> Option<sc_telemetry::Telemetry>;

/// Spawns a task in the background that runs the future passed as parameter.
///
/// Information about this task will be reported to Prometheus.
///
/// The task name is a `&'static str` as opposed to a `String`. The reason for that is that
/// in order to avoid memory consumption issues with the Prometheus metrics, the set of
/// possible task names has to be bounded.
#[deprecated(note = "Use `spawn_task_handle().spawn() instead.")]
fn spawn_task(&self, name: &'static str, task: impl Future<Output = ()> + Send + 'static);

/// Spawns a task in the background that runs the future passed as
/// parameter. The given task is considered essential, i.e. if it errors we
/// trigger a service exit.
#[deprecated(note = "Use `spawn_essential_task_handle().spawn() instead.")]
fn spawn_essential_task(&self, name: &'static str, task: impl Future<Output = ()> + Send + 'static);

/// Returns a handle for spawning essential tasks. Any task spawned through this handle is
/// considered essential, i.e. if it errors we trigger a service exit.
fn spawn_essential_task_handle(&self) -> SpawnEssentialTaskHandle;

/// Returns a handle for spawning tasks.
fn spawn_task_handle(&self) -> SpawnTaskHandle;

/// Returns the keystore that stores keys.
fn keystore(&self) -> sc_keystore::KeyStorePtr;
/// RPC handlers that can perform RPC queries.
pub struct RpcHandlers(sc_rpc_server::RpcHandler<sc_rpc::Metadata>);

impl RpcHandlers {
/// Starts an RPC query.
Expand All @@ -204,12 +118,12 @@ impl RpcHandlers {
/// Sinks to propagate network status updates.
/// For each element, every time the `Interval` fires we push an element on the sender.
pub struct NetworkStatusSinks<Block: BlockT>(
Arc<Mutex<status_sinks::StatusSinks<(NetworkStatus<Block>, NetworkState)>>>,
Arc<status_sinks::StatusSinks<(NetworkStatus<Block>, NetworkState)>>,
);

impl<Block: BlockT> NetworkStatusSinks<Block> {
fn new(
sinks: Arc<Mutex<status_sinks::StatusSinks<(NetworkStatus<Block>, NetworkState)>>>
sinks: Arc<status_sinks::StatusSinks<(NetworkStatus<Block>, NetworkState)>>
) -> Self {
Self(sinks)
}
Expand All @@ -218,7 +132,7 @@ impl<Block: BlockT> NetworkStatusSinks<Block> {
pub fn network_status(&self, interval: Duration)
-> TracingUnboundedReceiver<(NetworkStatus<Block>, NetworkState)> {
let (sink, stream) = tracing_unbounded("mpsc_network_status");
self.network_status_sinks.push(interval, sink);
self.0.push(interval, sink);
stream
}
}
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.