git.delta.rocks / unique-network / refs/commits / 8cc39ba6d3e1

difftreelog

Implement autoseal in dev mode

Daniel Shiposha2022-03-15parent: #a78501c.patch.diff
in: master

4 files changed

modifiednode/cli/Cargo.tomldiffbeforeafterboth
--- a/node/cli/Cargo.toml
+++ b/node/cli/Cargo.toml
@@ -168,6 +168,9 @@
 [dependencies.serde_json]
 version = '1.0.68'
 
+[dependencies.sc-consensus-manual-seal]
+git = 'https://github.com/paritytech/substrate.git'
+branch = 'polkadot-v0.9.17'
 
 ################################################################################
 # Cumulus dependencies
modifiednode/cli/src/chain_spec.rsdiffbeforeafterboth
--- a/node/cli/src/chain_spec.rs
+++ b/node/cli/src/chain_spec.rs
@@ -68,6 +68,25 @@
 	}
 }
 
+pub enum ServiceId {
+	Prod,
+	Dev
+}
+
+pub trait ServiceIdentification {
+	fn service_id(&self) -> ServiceId;
+}
+
+impl ServiceIdentification for Box<dyn sc_service::ChainSpec> {
+	fn service_id(&self) -> ServiceId {
+		if self.id().ends_with("dev") {
+			ServiceId::Dev
+		} else {
+			ServiceId::Prod
+		}
+	}
+}
+
 /// Helper function to generate a crypto pair from seed
 pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {
 	TPublic::Pair::from_string(&format!("//{}", seed), None)
modifiednode/cli/src/command.rsdiffbeforeafterboth
33// limitations under the License.33// limitations under the License.
3434
35use crate::{35use crate::{
36 chain_spec::{self, RuntimeId, RuntimeIdentification},36 chain_spec::{self, RuntimeId, RuntimeIdentification, ServiceId, ServiceIdentification},
37 cli::{Cli, RelayChainCli, Subcommand},37 cli::{Cli, RelayChainCli, Subcommand},
38 service::new_partial,38 service::{new_partial, start_node, start_dev_node},
39};39};
4040
41#[cfg(feature = "unique-runtime")]41#[cfg(feature = "unique-runtime")]
210 >(210 >(
211 &$config,211 &$config,
212 crate::service::parachain_build_import_queue,212 crate::service::parachain_build_import_queue,
213 ServiceId::Prod,
213 )?;214 )?;
214 let task_manager = $components.task_manager;215 let task_manager = $components.task_manager;
215216
245 }}246 }}
246}247}
248
249macro_rules! start_node_using_chain_runtime {
250 ($start_node_fn:ident($config:expr $(, $($args:expr),+)?) $($code:tt)*) => {
251 match $config.chain_spec.runtime_id() {
252 #[cfg(feature = "unique-runtime")]
253 RuntimeId::Unique => $start_node_fn::<
254 unique_runtime::Runtime,
255 unique_runtime::RuntimeApi,
256 UniqueRuntimeExecutor,
257 >($config $(, $($args),+)?) $($code)*,
258
259 #[cfg(feature = "quartz-runtime")]
260 RuntimeId::Quartz => $start_node_fn::<
261 quartz_runtime::Runtime,
262 quartz_runtime::RuntimeApi,
263 QuartzRuntimeExecutor,
264 >($config $(, $($args),+)?) $($code)*,
265
266 RuntimeId::Opal => $start_node_fn::<
267 opal_runtime::Runtime,
268 opal_runtime::RuntimeApi,
269 OpalRuntimeExecutor,
270 >($config $(, $($args),+)?) $($code)*,
271
272 RuntimeId::Unknown(chain) => Err(no_runtime_err!(chain).into()),
273 }
274 };
275}
247276
248/// Parse command line arguments into service configuration.277/// Parse command line arguments into service configuration.
249pub fn run() -> Result<()> {278pub fn run() -> Result<()> {
365 let runner = cli.create_runner(&cli.run.normalize())?;394 let runner = cli.create_runner(&cli.run.normalize())?;
366395
367 runner.run_node_until_exit(|config| async move {396 runner.run_node_until_exit(|config| async move {
368 let para_id = chain_spec::Extensions::try_get(&*config.chain_spec)397 let extensions = chain_spec::Extensions::try_get(&*config.chain_spec);
398
399 let service_id = config.chain_spec.service_id();
369 .map(|e| e.para_id)400 let relay_chain_id = extensions.map(|e| e.relay_chain.clone());
401 let is_dev_service = matches![service_id, ServiceId::Dev]
402 || relay_chain_id == Some("dev-service".into());
403
404 if is_dev_service {
405 return start_node_using_chain_runtime! {
406 start_dev_node(config).map_err(Into::into)
407 };
408 };
409
410 let para_id = extensions
411 .map(|e| e.para_id)
370 .ok_or("Could not find parachain ID in chain-spec.")?;412 .ok_or("Could not find parachain ID in chain-spec.")?;
371413
372 let polkadot_cli = RelayChainCli::new(414 let polkadot_cli = RelayChainCli::new(
376 .chain(cli.relaychain_args.iter()),418 .chain(cli.relaychain_args.iter()),
377 );419 );
378420
379 let id = ParaId::from(para_id);421 let para_id = ParaId::from(para_id);
380422
381 let parachain_account =423 let parachain_account =
382 AccountIdConversion::<polkadot_primitives::v0::AccountId>::into_account(&id);424 AccountIdConversion::<polkadot_primitives::v0::AccountId>::into_account(&para_id);
383425
384 let state_version =426 let state_version =
385 RelayChainCli::native_runtime_version(&config.chain_spec).state_version();427 RelayChainCli::native_runtime_version(&config.chain_spec).state_version();
395 )437 )
396 .map_err(|err| format!("Relay chain argument error: {}", err))?;438 .map_err(|err| format!("Relay chain argument error: {}", err))?;
397439
398 info!("Parachain id: {:?}", id);440 info!("Parachain id: {:?}", para_id);
399 info!("Parachain Account: {}", parachain_account);441 info!("Parachain Account: {}", parachain_account);
400 info!("Parachain genesis state: {}", genesis_state);442 info!("Parachain genesis state: {}", genesis_state);
401 info!("Parachain genesis hash: {}", genesis_hash);443 info!("Parachain genesis hash: {}", genesis_hash);
408 }450 }
409 );451 );
410452
411 match config.chain_spec.runtime_id() {453 start_node_using_chain_runtime! {
412 #[cfg(feature = "unique-runtime")]
413 RuntimeId::Unique => crate::service::start_node::<454 start_node(config, polkadot_config, para_id)
414 unique_runtime::Runtime,
415 unique_runtime::RuntimeApi,
416 UniqueRuntimeExecutor,
417 >(config, polkadot_config, id)
418 .await
419 .map(|r| r.0)
420 .map_err(Into::into),455 .await
421
422 #[cfg(feature = "quartz-runtime")]
423 RuntimeId::Quartz => crate::service::start_node::<
424 quartz_runtime::Runtime,
425 quartz_runtime::RuntimeApi,
426 QuartzRuntimeExecutor,
427 >(config, polkadot_config, id)
428 .await
429 .map(|r| r.0)
430 .map_err(Into::into),456 .map(|r| r.0)
431
432 RuntimeId::Opal => crate::service::start_node::<
433 opal_runtime::Runtime,
434 opal_runtime::RuntimeApi,
435 OpalRuntimeExecutor,
436 >(config, polkadot_config, id)
437 .await
438 .map(|r| r.0)
439 .map_err(Into::into),457 .map_err(Into::into)
440
441 RuntimeId::Unknown(chain) => Err(no_runtime_err!(chain).into()),
442 }458 }
443 })459 })
444 }460 }
modifiednode/cli/src/service.rsdiffbeforeafterboth
--- a/node/cli/src/service.rs
+++ b/node/cli/src/service.rs
@@ -57,6 +57,7 @@
 use fc_mapping_sync::{MappingSyncWorker, SyncStrategy};
 
 use unique_runtime_common::types::{AuraId, RuntimeInstance, AccountId, Balance, Index, Hash, Block};
+use crate::chain_spec::ServiceId;
 
 /// Native executor instance.
 pub struct UniqueRuntimeExecutor;
@@ -125,6 +126,7 @@
 	sc_service::TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<ExecutorDispatch>>;
 type FullBackend = sc_service::TFullBackend<Block>;
 type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;
+type MaybeSelectChain = Option<FullSelectChain>;
 
 /// Starts a `ServiceBuilder` for a full service.
 ///
@@ -134,11 +136,12 @@
 pub fn new_partial<RuntimeApi, ExecutorDispatch, BIQ>(
 	config: &Configuration,
 	build_import_queue: BIQ,
+	service_id: ServiceId,
 ) -> Result<
 	PartialComponents<
 		FullClient<RuntimeApi, ExecutorDispatch>,
 		FullBackend,
-		FullSelectChain,
+		MaybeSelectChain,
 		sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,
 		sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, ExecutorDispatch>>,
 		(
@@ -215,7 +218,10 @@
 		telemetry
 	});
 
-	let select_chain = sc_consensus::LongestChain::new(backend.clone());
+	let select_chain = match service_id {
+		ServiceId::Prod => Some(sc_consensus::LongestChain::new(backend.clone())),
+		ServiceId::Dev => None
+	};
 
 	let transaction_pool = sc_transaction_pool::BasicPool::new_full(
 		config.transaction_pool.clone(),
@@ -317,7 +323,9 @@
 	let parachain_config = prepare_node_config(parachain_config);
 
 	let params =
-		new_partial::<RuntimeApi, ExecutorDispatch, BIQ>(&parachain_config, build_import_queue)?;
+		new_partial::<RuntimeApi, ExecutorDispatch, BIQ>(
+			&parachain_config, build_import_queue, ServiceId::Prod
+		)?;
 	let (mut telemetry, filter_pool, frontier_backend, telemetry_worker_handle, fee_history_cache) =
 		params.other;
 
@@ -356,7 +364,9 @@
 	let subscription_executor = sc_rpc::SubscriptionTaskExecutor::new(task_manager.spawn_handle());
 	let rpc_client = client.clone();
 	let rpc_pool = transaction_pool.clone();
-	let select_chain = params.select_chain.clone();
+	let select_chain = params.select_chain
+							.expect("select_chain always exists when running Prod service; qed")
+							.clone();
 	let rpc_network = network.clone();
 
 	let rpc_frontier_backend = frontier_backend.clone();
@@ -638,3 +648,255 @@
 	)
 	.await
 }
+
+fn dev_build_import_queue<RuntimeApi, ExecutorDispatch>(
+	client: Arc<FullClient<RuntimeApi, ExecutorDispatch>>,
+	config: &Configuration,
+	_: Option<TelemetryHandle>,
+	task_manager: &TaskManager,
+) -> Result<sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>, sc_service::Error>
+where
+	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>
+	+ Send
+	+ Sync
+	+ 'static,
+	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>
+							+ sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>,
+	ExecutorDispatch: NativeExecutionDispatch + 'static,
+{
+	Ok(sc_consensus_manual_seal::import_queue(
+		Box::new(client.clone()),
+		&task_manager.spawn_essential_handle(),
+		config.prometheus_registry(),
+	))
+}
+
+/// Builds a new development service. This service uses instant seal, and mocks
+/// the parachain inherent
+pub fn start_dev_node<Runtime, RuntimeApi, ExecutorDispatch>(config: Configuration)
+	-> sc_service::error::Result<TaskManager>
+where
+	Runtime: RuntimeInstance + Send + Sync + 'static,
+	<Runtime as RuntimeInstance>::CrossAccountId: Serialize,
+	for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,
+	RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>
+		+ Send
+		+ Sync
+		+ 'static,
+	RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>
+							+ fp_rpc::EthereumRuntimeRPCApi<Block>
+							+ sp_session::SessionKeys<Block>
+							+ sp_block_builder::BlockBuilder<Block>
+							+ pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>
+							+ sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>
+							+ up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>
+							+ substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>
+							+ sp_api::Metadata<Block>
+							+ sp_offchain::OffchainWorkerApi<Block>
+							+ cumulus_primitives_core::CollectCollationInfo<Block>
+							+ sp_consensus_aura::AuraApi<Block, AuraId>,
+	ExecutorDispatch: NativeExecutionDispatch + 'static,
+{
+	use futures::Stream;
+	use sc_consensus_manual_seal::{run_manual_seal, EngineCommand, ManualSealParams};
+	use fc_consensus::FrontierBlockImport;
+	use sc_client_api::HeaderBackend;
+
+	let sc_service::PartialComponents {
+		client,
+		backend,
+		mut task_manager,
+		import_queue,
+		keystore_container,
+		select_chain: maybe_select_chain,
+		transaction_pool,
+		other:
+			(
+				telemetry,
+				filter_pool,
+				frontier_backend,
+				_telemetry_worker_handle,
+				fee_history_cache,
+			),
+	} = new_partial::<RuntimeApi, ExecutorDispatch, _>(
+		&config,
+		dev_build_import_queue::<RuntimeApi, ExecutorDispatch>,
+		ServiceId::Dev
+	)?;
+
+	let block_data_cache = Arc::new(fc_rpc::EthBlockDataCache::new(
+		task_manager.spawn_handle(),
+		overrides_handle::<_, _, Runtime>(client.clone()),
+		50,
+		50,
+	));
+
+	let (network, system_rpc_tx, network_starter) =
+		sc_service::build_network(sc_service::BuildNetworkParams {
+			config: &config,
+			client: client.clone(),
+			transaction_pool: transaction_pool.clone(),
+			spawn_handle: task_manager.spawn_handle(),
+			import_queue,
+			block_announce_validator_builder: None,
+			warp_sync: None,
+		})?;
+
+	if config.offchain_worker.enabled {
+		sc_service::build_offchain_workers(
+			&config,
+			task_manager.spawn_handle(),
+			client.clone(),
+			network.clone(),
+		);
+	}
+
+	let prometheus_registry = config.prometheus_registry().cloned();
+	let collator = config.role.is_authority();
+
+	let select_chain = maybe_select_chain.clone().expect(
+		"`new_partial` builds a `LongestChainRule` when building dev service.\
+			We specified the dev service when calling `new_partial`.\
+			Therefore, a `LongestChainRule` is present. qed.",
+	);
+
+	if collator {
+		let block_import =
+			FrontierBlockImport::new(client.clone(), client.clone(), frontier_backend.clone());
+
+		let env = sc_basic_authorship::ProposerFactory::new(
+			task_manager.spawn_handle(),
+			client.clone(),
+			transaction_pool.clone(),
+			prometheus_registry.as_ref(),
+			telemetry.as_ref().map(|x| x.handle()),
+		);
+
+		let commands_stream: Box<dyn Stream<Item = EngineCommand<Hash>> + Send + Sync + Unpin> =
+			Box::new(
+				// This bit cribbed from the implementation of instant seal.
+				transaction_pool
+					.pool()
+					.validated_pool()
+					.import_notification_stream()
+					.map(|_| EngineCommand::SealNewBlock {
+						create_empty: true, // was false in Moonbeam
+						finalize: false,
+						parent_hash: None,
+						sender: None,
+					}),
+			);
+
+		let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;
+		let client_set_aside_for_cidp = client.clone();
+
+		task_manager.spawn_essential_handle().spawn_blocking(
+			"authorship_task",
+			Some("block-authoring"),
+			run_manual_seal(ManualSealParams {
+				block_import,
+				env,
+				client: client.clone(),
+				pool: transaction_pool.clone(),
+				commands_stream,
+				select_chain: select_chain.clone(),
+				consensus_data_provider: None,
+				create_inherent_data_providers: move |block: Hash, ()| {
+					let current_para_block = client_set_aside_for_cidp
+						.number(block)
+						.expect("Header lookup should succeed")
+						.expect("Header passed in as parent should be present in backend.");
+
+					let client_for_xcm = client_set_aside_for_cidp.clone();
+					async move {
+						let time = sp_timestamp::InherentDataProvider::from_system_time();
+
+						let mocked_parachain = cumulus_primitives_parachain_inherent::MockValidationDataInherentDataProvider {
+							current_para_block,
+							relay_offset: 1000,
+							relay_blocks_per_para_block: 2,
+							xcm_config: cumulus_primitives_parachain_inherent::MockXcmConfig::new(
+								&*client_for_xcm,
+								block,
+								Default::default(),
+								Default::default(),
+							),
+							raw_downward_messages: vec![],
+							raw_horizontal_messages: vec![],
+						};
+
+						let slot =
+						sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_duration(
+							*time,
+							slot_duration.slot_duration(),
+						);
+
+						Ok((time, slot, mocked_parachain))
+					}
+				},
+			}),
+		);
+	}
+
+	task_manager.spawn_essential_handle().spawn(
+		"frontier-mapping-sync-worker",
+		Some("block-authoring"),
+		MappingSyncWorker::new(
+			client.import_notification_stream(),
+			Duration::new(6, 0),
+			client.clone(),
+			backend.clone(),
+			frontier_backend.clone(),
+			SyncStrategy::Normal,
+		)
+		.for_each(|()| futures::future::ready(())),
+	);
+
+	let subscription_executor = sc_rpc::SubscriptionTaskExecutor::new(task_manager.spawn_handle());
+	let rpc_client = client.clone();
+	let rpc_pool = transaction_pool.clone();
+	let rpc_network = network.clone();
+	let rpc_frontier_backend = frontier_backend.clone();
+	let rpc_extensions_builder = Box::new(move |deny_unsafe, _| {
+		let full_deps = unique_rpc::FullDeps {
+			backend: rpc_frontier_backend.clone(),
+			deny_unsafe,
+			client: rpc_client.clone(),
+			pool: rpc_pool.clone(),
+			graph: rpc_pool.pool().clone(),
+			// TODO: Unhardcode
+			enable_dev_signer: false,
+			filter_pool: filter_pool.clone(),
+			network: rpc_network.clone(),
+			select_chain: select_chain.clone(),
+			is_authority: collator,
+			// TODO: Unhardcode
+			max_past_logs: 10000,
+			block_data_cache: block_data_cache.clone(),
+			fee_history_cache: fee_history_cache.clone(),
+			// TODO: Unhardcode
+			fee_history_limit: 2048,
+		};
+
+		Ok(unique_rpc::create_full::<_, _, _, _, Runtime, RuntimeApi, _>(
+			full_deps,
+			subscription_executor.clone(),
+		))
+	});
+
+	sc_service::spawn_tasks(sc_service::SpawnTasksParams {
+		network,
+		client,
+		keystore: keystore_container.sync_keystore(),
+		task_manager: &mut task_manager,
+		transaction_pool,
+		rpc_extensions_builder,
+		backend,
+		system_rpc_tx,
+		config,
+		telemetry: None,
+	})?;
+
+	network_starter.start_network();
+	Ok(task_manager)
+}