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

Companion PR for Add a build-sync-spec subcommand and remove the CHT roots from the light sync state. #1670

Merged
6 commits merged into from
Sep 11, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ pub enum Subcommand {
/// Build a chain specification.
BuildSpec(sc_cli::BuildSpecCmd),

/// Build a chain specification with a light client sync state.
BuildSyncSpec(sc_cli::BuildSyncSpecCmd),

/// Validate blocks.
CheckBlock(sc_cli::CheckBlockCmd),

Expand Down
34 changes: 34 additions & 0 deletions cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use service::{IdentifyVariant, self};
use service_new::{IdentifyVariant, self as service};
use sc_cli::{SubstrateCli, Result, RuntimeVersion, Role};
use crate::cli::{Cli, Subcommand};
use std::sync::Arc;

fn get_exec_name() -> Option<String> {
std::env::current_exe()
Expand Down Expand Up @@ -154,6 +155,39 @@ pub fn run() -> Result<()> {
let runner = cli.create_runner(cmd)?;
runner.sync_run(|config| cmd.run(config.chain_spec, config.network))
},
Some(Subcommand::BuildSyncSpec(cmd)) => {
let runner = cli.create_runner(cmd)?;
let chain_spec = &runner.config().chain_spec;

set_default_ss58_version(chain_spec);

let authority_discovery_enabled = cli.run.authority_discovery_enabled;
let grandpa_pause = if cli.run.grandpa_pause.is_empty() {
None
} else {
Some((cli.run.grandpa_pause[0], cli.run.grandpa_pause[1]))
};

if chain_spec.is_kusama() {
info!("----------------------------");
info!("This chain is not in any way");
info!(" endorsed by the ");
info!(" KUSAMA FOUNDATION ");
info!("----------------------------");
}

runner.async_run(|config| {
let chain_spec = config.chain_spec.cloned_box();
let network_config = config.network.clone();
let service::NewFull { task_manager, client, network_status_sinks, .. }
= service::new_full_nongeneric(
config, None, authority_discovery_enabled, grandpa_pause, false,
)?;
let client = Arc::new(client);

Ok((cmd.run(chain_spec, network_config, client, network_status_sinks), task_manager))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just pass the Configuration struct? chain_spec and network_config live on the Configuration object

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Configuration gets consumed by new_full_nongeneric, so it can't be used.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would argue that it should be clone, but that's for another time. This looks solid to me

})
},
Some(Subcommand::CheckBlock(cmd)) => {
let runner = cli.create_runner(cmd)?;
let chain_spec = &runner.config().chain_spec;
Expand Down
6 changes: 3 additions & 3 deletions node/test-service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use polkadot_primitives::v0::{
};
use polkadot_runtime_common::BlockHashCount;
use polkadot_service::{
new_full, FullNodeHandles, AbstractClient, ClientHandle, ExecuteWithClient,
new_full, NewFull, FullNodeHandles, AbstractClient, ClientHandle, ExecuteWithClient,
};
use polkadot_test_runtime::{Runtime, SignedExtra, SignedPayload, VERSION};
use sc_chain_spec::ChainSpec;
Expand Down Expand Up @@ -74,7 +74,7 @@ pub fn polkadot_test_new_full(
),
ServiceError,
> {
let (task_manager, client, handles, network, rpc_handlers) =
let NewFull { task_manager, client, node_handles, network, rpc_handlers, .. } =
new_full::<polkadot_test_runtime::RuntimeApi, PolkadotTestExecutor>(
config,
collating_for,
Expand All @@ -83,7 +83,7 @@ pub fn polkadot_test_new_full(
true,
)?;

Ok((task_manager, client, handles, network, rpc_handlers))
Ok((task_manager, client, node_handles, network, rpc_handlers))
}

/// A wrapper for the test client that implements `ClientHandle`.
Expand Down
45 changes: 45 additions & 0 deletions service/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,48 @@ impl sc_client_api::StorageProvider<Block, crate::FullBackend> for Client {
}
}
}

impl sp_blockchain::HeaderBackend<Block> for Client {
fn header(&self, id: BlockId<Block>) -> sp_blockchain::Result<Option<<Block as BlockT>::Header>> {
match self {
Self::Polkadot(client) => client.header(&id),
Self::Westend(client) => client.header(&id),
Self::Kusama(client) => client.header(&id),
}
}

fn info(&self) -> sp_blockchain::Info<Block> {
match self {
Self::Polkadot(client) => client.info(),
Self::Westend(client) => client.info(),
Self::Kusama(client) => client.info(),
}
}

fn status(&self, id: BlockId<Block>) -> sp_blockchain::Result<sp_blockchain::BlockStatus> {
match self {
Self::Polkadot(client) => client.status(id),
Self::Westend(client) => client.status(id),
Self::Kusama(client) => client.status(id),
}
}

fn number(
&self,
hash: <Block as BlockT>::Hash
) -> sp_blockchain::Result<Option<NumberFor<Block>>> {
match self {
Self::Polkadot(client) => client.number(hash),
Self::Westend(client) => client.number(hash),
Self::Kusama(client) => client.number(hash),
}
}

fn hash(&self, number: NumberFor<Block>) -> sp_blockchain::Result<Option<<Block as BlockT>::Hash>> {
match self {
Self::Polkadot(client) => client.hash(number),
Self::Westend(client) => client.hash(number),
Self::Kusama(client) => client.hash(number),
}
}
}
123 changes: 83 additions & 40 deletions service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,20 +243,36 @@ pub fn new_partial<RuntimeApi, Executor>(config: &mut Configuration, test: bool)
})
}

pub struct NewFull<C> {
pub task_manager: TaskManager,
pub client: C,
pub node_handles: FullNodeHandles,
pub network: Arc<sc_network::NetworkService<Block, <Block as BlockT>::Hash>>,
pub network_status_sinks: service::NetworkStatusSinks<Block>,
pub rpc_handlers: RpcHandlers,
}

impl<C> NewFull<C> {
fn with_client(self, func: impl FnOnce(C) -> Client) -> NewFull<Client> {
NewFull {
client: func(self.client),
task_manager: self.task_manager,
node_handles: self.node_handles,
network: self.network,
network_status_sinks: self.network_status_sinks,
rpc_handlers: self.rpc_handlers,
}
}
}

#[cfg(feature = "full-node")]
pub fn new_full<RuntimeApi, Executor>(
mut config: Configuration,
collating_for: Option<(CollatorId, parachain::Id)>,
authority_discovery_enabled: bool,
grandpa_pause: Option<(u32, u32)>,
test: bool,
) -> Result<(
TaskManager,
Arc<FullClient<RuntimeApi, Executor>>,
FullNodeHandles,
Arc<sc_network::NetworkService<Block, <Block as BlockT>::Hash>>,
RpcHandlers,
), Error>
) -> Result<NewFull<Arc<FullClient<RuntimeApi, Executor>>>, Error>
where
RuntimeApi: ConstructRuntimeApi<Block, FullClient<RuntimeApi, Executor>> + Send + Sync + 'static,
RuntimeApi::RuntimeApi:
Expand Down Expand Up @@ -318,7 +334,8 @@ pub fn new_full<RuntimeApi, Executor>(
on_demand: None,
remote_blockchain: None,
telemetry_connection_sinks: telemetry_connection_sinks.clone(),
network_status_sinks, system_rpc_tx,
network_status_sinks: network_status_sinks.clone(),
system_rpc_tx,
})?;

let (block_import, link_half, babe_link) = import_setup;
Expand Down Expand Up @@ -457,7 +474,45 @@ pub fn new_full<RuntimeApi, Executor>(

network_starter.start_network();

Ok((task_manager, client, FullNodeHandles, network, rpc_handlers))
Ok(NewFull {
task_manager, client, node_handles: FullNodeHandles, network, network_status_sinks,
rpc_handlers,
})
}

#[cfg(feature = "full-node")]
pub fn new_full_nongeneric(
config: Configuration,
collating_for: Option<(CollatorId, parachain::Id)>,
authority_discovery_enabled: bool,
grandpa_pause: Option<(u32, u32)>,
test: bool,
) -> Result<NewFull<Client>, Error> {
if config.chain_spec.is_kusama() {
new_full::<kusama_runtime::RuntimeApi, KusamaExecutor>(
config,
collating_for,
authority_discovery_enabled,
grandpa_pause,
test,
).map(|full| full.with_client(Client::Kusama))
} else if config.chain_spec.is_westend() {
new_full::<westend_runtime::RuntimeApi, WestendExecutor>(
config,
collating_for,
authority_discovery_enabled,
grandpa_pause,
false,
).map(|full| full.with_client(Client::Westend))
} else {
new_full::<polkadot_runtime::RuntimeApi, PolkadotExecutor>(
config,
collating_for,
authority_discovery_enabled,
grandpa_pause,
false,
).map(|full| full.with_client(Client::Polkadot))
}
}

/// Builds a new service for a light client.
Expand Down Expand Up @@ -607,15 +662,17 @@ pub fn polkadot_new_full(
FullNodeHandles,
), ServiceError>
{
let (service, client, handles, _, _) = new_full::<polkadot_runtime::RuntimeApi, PolkadotExecutor>(
let NewFull {
task_manager, client, node_handles, ..
} = new_full::<polkadot_runtime::RuntimeApi, PolkadotExecutor>(
config,
collating_for,
authority_discovery_enabled,
grandpa_pause,
false,
)?;

Ok((service, client, handles))
Ok((task_manager, client, node_handles))
}

/// Create a new Kusama service for a full node.
Expand All @@ -631,15 +688,17 @@ pub fn kusama_new_full(
FullNodeHandles
), ServiceError>
{
let (service, client, handles, _, _) = new_full::<kusama_runtime::RuntimeApi, KusamaExecutor>(
let NewFull {
task_manager, client, node_handles, ..
} = new_full::<kusama_runtime::RuntimeApi, KusamaExecutor>(
config,
collating_for,
authority_discovery_enabled,
grandpa_pause,
false,
)?;

Ok((service, client, handles))
Ok((task_manager, client, node_handles))
}

/// Create a new Westend service for a full node.
Expand All @@ -656,15 +715,17 @@ pub fn westend_new_full(
FullNodeHandles,
), ServiceError>
{
let (service, client, handles, _, _) = new_full::<westend_runtime::RuntimeApi, WestendExecutor>(
let NewFull {
task_manager, client, node_handles, ..
} = new_full::<westend_runtime::RuntimeApi, WestendExecutor>(
config,
collating_for,
authority_discovery_enabled,
grandpa_pause,
false,
)?;

Ok((service, client, handles))
Ok((task_manager, client, node_handles))
}

/// Handles to other sub-services that full nodes instantiate, which consumers
Expand Down Expand Up @@ -692,29 +753,11 @@ pub fn build_full(
authority_discovery_enabled: bool,
grandpa_pause: Option<(u32, u32)>,
) -> Result<(TaskManager, Client, FullNodeHandles), ServiceError> {
if config.chain_spec.is_kusama() {
new_full::<kusama_runtime::RuntimeApi, KusamaExecutor>(
config,
collating_for,
authority_discovery_enabled,
grandpa_pause,
false,
).map(|(task_manager, client, handles, _, _)| (task_manager, Client::Kusama(client), handles))
} else if config.chain_spec.is_westend() {
new_full::<westend_runtime::RuntimeApi, WestendExecutor>(
config,
collating_for,
authority_discovery_enabled,
grandpa_pause,
false,
).map(|(task_manager, client, handles, _, _)| (task_manager, Client::Westend(client), handles))
} else {
new_full::<polkadot_runtime::RuntimeApi, PolkadotExecutor>(
config,
collating_for,
authority_discovery_enabled,
grandpa_pause,
false,
).map(|(task_manager, client, handles, _, _)| (task_manager, Client::Polkadot(client), handles))
}
new_full_nongeneric(
config,
collating_for,
authority_discovery_enabled,
grandpa_pause,
false,
).map(|NewFull { task_manager, client, node_handles, .. }| (task_manager, client, node_handles))
}