mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-27 12:48:00 +00:00
Service factory refactor (#3382)
* Move Service::new to a macro * Move function calls to macros * Extract offchain_workers and start_rpc in separate function In follow-up commits, we want to be able to directly call maintain_transaction_pool, offchain_workers, and start_rpc, without having to implement the Components trait. This commit is a preliminary step: we extract the code to freestanding functions. * Introduce an AbstractService trait * Introduce NewService as an implementation detail of Service * Implement traits on NewService instead Instead of implementing AbstractService, Future, and Executor on Service, we implement them on NewService instead. The implementations of AbstractService, Future, and Executor on Service still exist, but they just wrap to the respective implementations for NewService. * Move components creation back to macro invocation Instead of having multiple $build_ parameters passed to the macro, let's group them all into one. This change is necessary for the follow-up commits, because we are going to call new_impl! only after all the components have already been built. * Add a $block parameter to new_impl This makes it possible to be explicit as what the generic parameter of the NewServiceis, without relying on type inference. * Introduce the ServiceBuilder struct Introduces a new builder-like ServiceBuilder struct that creates a NewService. * Macro-ify import_blocks, export_blocks and revert_chain Similar to the introduction of new_impl!, we extract the actual code into a macro, letting us get rid of the Components and Factory traits * Add export_blocks, import_blocks and revert_chain methods on ServiceBuilder Can be used as a replacement for the chain_ops::* methods * Add run_with_builder Instead of just run, adds run_with_builder to ParseAndPrepareExport/Import/Revert. This lets you run these operations with a ServiceBuilder instead of a ServiceFactory. * Transition node and node-template to ServiceBuilder * Transition transaction-factory to the new service factory This is technically a breaking change, but the transaction-factory crate is only ever used from within substrate-node, which this commit updates as well. * Remove old service factory * Adjust the AbstractService trait to be more usable We slightly change the trait bounds in order to make all the methods usable. * Make substrate-service-test compile * Fix the node-cli tests * Remove the old API * Remove the components module * Fix indentation on chain_ops * Line widths * Fix bad line widths commit * Line widths again 🤦 * Fix the sync test * Apply suggestions from code review Co-Authored-By: Gavin Wood <i@gavwood.com> * Address some concerns * Remove TelemetryOnConnect * Remove informant::start * Update jsonrpc * Rename factory to builder * Line widths 😩
This commit is contained in:
committed by
Bastian Köcher
parent
144bd228af
commit
5b8ebf7baf
@@ -29,8 +29,8 @@ pub mod informant;
|
||||
use client::ExecutionStrategies;
|
||||
use service::{
|
||||
config::Configuration,
|
||||
ServiceFactory, FactoryFullConfiguration, RuntimeGenesis,
|
||||
FactoryGenesis, PruningMode, ChainSpec,
|
||||
ServiceBuilderExport, ServiceBuilderImport, ServiceBuilderRevert,
|
||||
RuntimeGenesis, PruningMode, ChainSpec,
|
||||
};
|
||||
use network::{
|
||||
self, multiaddr::Protocol,
|
||||
@@ -317,13 +317,17 @@ pub struct ParseAndPrepareExport<'a> {
|
||||
|
||||
impl<'a> ParseAndPrepareExport<'a> {
|
||||
/// Runs the command and exports from the chain.
|
||||
pub fn run<F, S, E>(
|
||||
pub fn run_with_builder<C, G, F, B, S, E>(
|
||||
self,
|
||||
builder: F,
|
||||
spec_factory: S,
|
||||
exit: E,
|
||||
) -> error::Result<()>
|
||||
where S: FnOnce(&str) -> Result<Option<ChainSpec<FactoryGenesis<F>>>, String>,
|
||||
F: ServiceFactory,
|
||||
where S: FnOnce(&str) -> Result<Option<ChainSpec<G>>, String>,
|
||||
F: FnOnce(Configuration<C, G>) -> Result<B, error::Error>,
|
||||
B: ServiceBuilderExport,
|
||||
C: Default,
|
||||
G: RuntimeGenesis,
|
||||
E: IntoExit
|
||||
{
|
||||
let config = create_config_with_db_path(spec_factory, &self.params.shared_params, self.version)?;
|
||||
@@ -338,9 +342,8 @@ impl<'a> ParseAndPrepareExport<'a> {
|
||||
None => Box::new(stdout()),
|
||||
};
|
||||
|
||||
service::chain_ops::export_blocks::<F, _, _>(
|
||||
config, exit.into_exit(), file, from.into(), to.map(Into::into), json
|
||||
).map_err(Into::into)
|
||||
builder(config)?.export_blocks(exit.into_exit(), file, from.into(), to.map(Into::into), json)?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -352,13 +355,17 @@ pub struct ParseAndPrepareImport<'a> {
|
||||
|
||||
impl<'a> ParseAndPrepareImport<'a> {
|
||||
/// Runs the command and imports to the chain.
|
||||
pub fn run<F, S, E>(
|
||||
pub fn run_with_builder<C, G, F, B, S, E>(
|
||||
self,
|
||||
builder: F,
|
||||
spec_factory: S,
|
||||
exit: E,
|
||||
) -> error::Result<()>
|
||||
where S: FnOnce(&str) -> Result<Option<ChainSpec<FactoryGenesis<F>>>, String>,
|
||||
F: ServiceFactory,
|
||||
where S: FnOnce(&str) -> Result<Option<ChainSpec<G>>, String>,
|
||||
F: FnOnce(Configuration<C, G>) -> Result<B, error::Error>,
|
||||
B: ServiceBuilderImport,
|
||||
C: Default,
|
||||
G: RuntimeGenesis,
|
||||
E: IntoExit
|
||||
{
|
||||
let mut config = create_config_with_db_path(spec_factory, &self.params.shared_params, self.version)?;
|
||||
@@ -377,7 +384,7 @@ impl<'a> ParseAndPrepareImport<'a> {
|
||||
},
|
||||
};
|
||||
|
||||
let fut = service::chain_ops::import_blocks::<F, _, _>(config, exit.into_exit(), file)?;
|
||||
let fut = builder(config)?.import_blocks(exit.into_exit(), file)?;
|
||||
tokio::run(fut);
|
||||
Ok(())
|
||||
}
|
||||
@@ -440,67 +447,23 @@ pub struct ParseAndPrepareRevert<'a> {
|
||||
|
||||
impl<'a> ParseAndPrepareRevert<'a> {
|
||||
/// Runs the command and reverts the chain.
|
||||
pub fn run<F, S>(
|
||||
pub fn run_with_builder<C, G, F, B, S>(
|
||||
self,
|
||||
builder: F,
|
||||
spec_factory: S
|
||||
) -> error::Result<()>
|
||||
where S: FnOnce(&str) -> Result<Option<ChainSpec<FactoryGenesis<F>>>, String>,
|
||||
F: ServiceFactory {
|
||||
where S: FnOnce(&str) -> Result<Option<ChainSpec<G>>, String>,
|
||||
F: FnOnce(Configuration<C, G>) -> Result<B, error::Error>,
|
||||
B: ServiceBuilderRevert,
|
||||
C: Default,
|
||||
G: RuntimeGenesis {
|
||||
let config = create_config_with_db_path(spec_factory, &self.params.shared_params, self.version)?;
|
||||
let blocks = self.params.num;
|
||||
Ok(service::chain_ops::revert_chain::<F>(config, blocks.into())?)
|
||||
builder(config)?.revert_chain(blocks.into())?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse command line interface arguments and executes the desired command.
|
||||
///
|
||||
/// # Return value
|
||||
///
|
||||
/// A result that indicates if any error occurred.
|
||||
/// If no error occurred and a custom subcommand was found, the subcommand is returned.
|
||||
/// The user needs to handle this subcommand on its own.
|
||||
///
|
||||
/// # Remarks
|
||||
///
|
||||
/// `CC` is a custom subcommand. This needs to be an `enum`! If no custom subcommand is required,
|
||||
/// `NoCustom` can be used as type here.
|
||||
/// `RP` are custom parameters for the run command. This needs to be a `struct`! The custom
|
||||
/// parameters are visible to the user as if they were normal run command parameters. If no custom
|
||||
/// parameters are required, `NoCustom` can be used as type here.
|
||||
#[deprecated(
|
||||
note = "Use parse_and_prepare instead; see the source code of parse_and_execute for how to transition"
|
||||
)]
|
||||
pub fn parse_and_execute<'a, F, CC, RP, S, RS, E, I, T>(
|
||||
spec_factory: S,
|
||||
version: &VersionInfo,
|
||||
impl_name: &'static str,
|
||||
args: I,
|
||||
exit: E,
|
||||
run_service: RS,
|
||||
) -> error::Result<Option<CC>>
|
||||
where
|
||||
F: ServiceFactory,
|
||||
S: FnOnce(&str) -> Result<Option<ChainSpec<FactoryGenesis<F>>>, String>,
|
||||
CC: StructOpt + Clone + GetLogFilter,
|
||||
RP: StructOpt + Clone + AugmentClap,
|
||||
E: IntoExit,
|
||||
RS: FnOnce(E, RunCmd, RP, FactoryFullConfiguration<F>) -> Result<(), String>,
|
||||
I: IntoIterator<Item = T>,
|
||||
T: Into<std::ffi::OsString> + Clone,
|
||||
{
|
||||
match parse_and_prepare::<CC, RP, _>(version, impl_name, args) {
|
||||
ParseAndPrepare::Run(cmd) => cmd.run(spec_factory, exit, run_service),
|
||||
ParseAndPrepare::BuildSpec(cmd) => cmd.run(spec_factory),
|
||||
ParseAndPrepare::ExportBlocks(cmd) => cmd.run::<F, _, _>(spec_factory, exit),
|
||||
ParseAndPrepare::ImportBlocks(cmd) => cmd.run::<F, _, _>(spec_factory, exit),
|
||||
ParseAndPrepare::PurgeChain(cmd) => cmd.run(spec_factory),
|
||||
ParseAndPrepare::RevertChain(cmd) => cmd.run::<F, _>(spec_factory),
|
||||
ParseAndPrepare::CustomCommand(cmd) => return Ok(Some(cmd))
|
||||
}?;
|
||||
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
/// Create a `NodeKeyConfig` from the given `NodeKeyParams` in the context
|
||||
/// of an optional network config storage directory.
|
||||
fn node_key_config<P>(params: NodeKeyParams, net_config_dir: &Option<P>)
|
||||
|
||||
Reference in New Issue
Block a user