Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 6199d86

Browse files
authored
Split the service initialisation up into seperate functions (#6332)
* Seperate out the complexity in ServiceBuilder::build_common into seperate functions * Fix line widths * Move some functions to their respective crates
1 parent 1f536e9 commit 6199d86

File tree

19 files changed

+452
-330
lines changed

19 files changed

+452
-330
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/consensus/aura/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,7 @@ pub fn import_queue<B, I, C, P, S>(
833833
P: Pair + Send + Sync + 'static,
834834
P::Public: Clone + Eq + Send + Sync + Hash + Debug + Encode + Decode,
835835
P::Signature: Encode + Decode,
836-
S: sp_core::traits::SpawnBlocking,
836+
S: sp_core::traits::SpawnNamed,
837837
{
838838
register_aura_inherent_data_provider(&inherent_data_providers, slot_duration.get())?;
839839
initialize_authorities_cache(&*client)?;

client/consensus/babe/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1291,7 +1291,7 @@ pub fn import_queue<Block: BlockT, Client, Inner>(
12911291
finality_proof_import: Option<BoxFinalityProofImport<Block>>,
12921292
client: Arc<Client>,
12931293
inherent_data_providers: InherentDataProviders,
1294-
spawner: &impl sp_core::traits::SpawnBlocking,
1294+
spawner: &impl sp_core::traits::SpawnNamed,
12951295
registry: Option<&Registry>,
12961296
) -> ClientResult<BabeImportQueue<Block, sp_api::TransactionFor<Client, Block>>> where
12971297
Inner: BlockImport<Block, Error = ConsensusError, Transaction = sp_api::TransactionFor<Client, Block>>

client/consensus/manual-seal/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl<B: BlockT> Verifier<B> for ManualSealVerifier {
7070
/// Instantiate the import queue for the manual seal consensus engine.
7171
pub fn import_queue<Block, Transaction>(
7272
block_import: BoxBlockImport<Block, Transaction>,
73-
spawner: &impl sp_core::traits::SpawnBlocking,
73+
spawner: &impl sp_core::traits::SpawnNamed,
7474
registry: Option<&Registry>,
7575
) -> BasicQueue<Block, Transaction>
7676
where

client/consensus/pow/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ pub fn import_queue<B, Transaction, Algorithm>(
466466
finality_proof_import: Option<BoxFinalityProofImport<B>>,
467467
algorithm: Algorithm,
468468
inherent_data_providers: InherentDataProviders,
469-
spawner: &impl sp_core::traits::SpawnBlocking,
469+
spawner: &impl sp_core::traits::SpawnNamed,
470470
registry: Option<&Registry>,
471471
) -> Result<
472472
PowImportQueue<B, Transaction>,

client/informant/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ sp-blockchain = { version = "2.0.0-rc3", path = "../../primitives/blockchain" }
2323
sp-runtime = { version = "2.0.0-rc3", path = "../../primitives/runtime" }
2424
sp-utils = { version = "2.0.0-rc2", path = "../../primitives/utils" }
2525
sp-transaction-pool = { version = "2.0.0-rc2", path = "../../primitives/transaction-pool" }
26+
parking_lot = "0.10.2"

client/informant/src/lib.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ use sc_network::{network_state::NetworkState, NetworkStatus};
2727
use sp_blockchain::HeaderMetadata;
2828
use sp_runtime::traits::{Block as BlockT, Header};
2929
use sp_transaction_pool::TransactionPool;
30-
use sp_utils::mpsc::TracingUnboundedReceiver;
30+
use sp_utils::{status_sinks, mpsc::tracing_unbounded};
3131
use std::fmt::Display;
3232
use std::sync::Arc;
3333
use std::time::Duration;
34+
use parking_lot::Mutex;
3435

3536
mod display;
3637

@@ -60,12 +61,7 @@ impl<T: TransactionPool + MallocSizeOf> TransactionPoolAndMaybeMallogSizeOf for
6061
/// Builds the informant and returns a `Future` that drives the informant.
6162
pub fn build<B: BlockT, C>(
6263
client: Arc<C>,
63-
network_status_stream_builder: impl FnOnce(
64-
Duration,
65-
) -> TracingUnboundedReceiver<(
66-
NetworkStatus<B>,
67-
NetworkState,
68-
)>,
64+
network_status_sinks: Arc<Mutex<status_sinks::StatusSinks<(NetworkStatus<B>, NetworkState)>>>,
6965
pool: Arc<impl TransactionPoolAndMaybeMallogSizeOf>,
7066
format: OutputFormat,
7167
) -> impl futures::Future<Output = ()>
@@ -76,7 +72,10 @@ where
7672
let mut display = display::InformantDisplay::new(format.clone());
7773

7874
let client_1 = client.clone();
79-
let display_notifications = network_status_stream_builder(Duration::from_millis(5000))
75+
let (network_status_sink, network_status_stream) = tracing_unbounded("mpsc_network_status");
76+
network_status_sinks.lock().push(Duration::from_millis(5000), network_status_sink);
77+
78+
let display_notifications = network_status_stream
8079
.for_each(move |(net_status, _)| {
8180
let info = client_1.usage_info();
8281
if let Some(ref usage) = info.usage {

client/offchain/src/lib.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ use sp_api::{ApiExt, ProvideRuntimeApi};
4141
use futures::future::Future;
4242
use log::{debug, warn};
4343
use sc_network::NetworkStateInfo;
44-
use sp_core::{offchain::{self, OffchainStorage}, ExecutionContext};
44+
use sp_core::{offchain::{self, OffchainStorage}, ExecutionContext, traits::SpawnNamed};
4545
use sp_runtime::{generic::BlockId, traits::{self, Header}};
46+
use futures::{prelude::*, future::ready};
4647

4748
mod api;
4849

@@ -161,6 +162,43 @@ impl<Client, Storage, Block> OffchainWorkers<
161162
}
162163
}
163164

165+
/// Inform the offchain worker about new imported blocks
166+
pub async fn notification_future<Client, Storage, Block, Spawner>(
167+
is_validator: bool,
168+
client: Arc<Client>,
169+
offchain: Arc<OffchainWorkers<Client, Storage, Block>>,
170+
spawner: Spawner,
171+
network_state_info: Arc<dyn NetworkStateInfo + Send + Sync>,
172+
)
173+
where
174+
Block: traits::Block,
175+
Client: ProvideRuntimeApi<Block> + sc_client_api::BlockchainEvents<Block> + Send + Sync + 'static,
176+
Client::Api: OffchainWorkerApi<Block>,
177+
Storage: OffchainStorage + 'static,
178+
Spawner: SpawnNamed
179+
{
180+
client.import_notification_stream().for_each(move |n| {
181+
if n.is_new_best {
182+
spawner.spawn(
183+
"offchain-on-block",
184+
offchain.on_block_imported(
185+
&n.header,
186+
network_state_info.clone(),
187+
is_validator,
188+
).boxed(),
189+
);
190+
} else {
191+
log::debug!(
192+
target: "sc_offchain",
193+
"Skipping offchain workers for non-canon block: {:?}",
194+
n.header,
195+
)
196+
}
197+
198+
ready(())
199+
}).await;
200+
}
201+
164202
#[cfg(test)]
165203
mod tests {
166204
use super::*;

0 commit comments

Comments
 (0)