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
--- a/node/cli/src/command.rs
+++ b/node/cli/src/command.rs
@@ -33,9 +33,9 @@
 // limitations under the License.
 
 use crate::{
-	chain_spec::{self, RuntimeId, RuntimeIdentification},
+	chain_spec::{self, RuntimeId, RuntimeIdentification, ServiceId, ServiceIdentification},
 	cli::{Cli, RelayChainCli, Subcommand},
-	service::new_partial,
+	service::{new_partial, start_node, start_dev_node},
 };
 
 #[cfg(feature = "unique-runtime")]
@@ -210,6 +210,7 @@
 			>(
 				&$config,
 				crate::service::parachain_build_import_queue,
+				ServiceId::Prod,
 			)?;
 			let task_manager = $components.task_manager;
 
@@ -245,6 +246,34 @@
 	}}
 }
 
+macro_rules! start_node_using_chain_runtime {
+	($start_node_fn:ident($config:expr $(, $($args:expr),+)?) $($code:tt)*) => {
+		match $config.chain_spec.runtime_id() {
+			#[cfg(feature = "unique-runtime")]
+			RuntimeId::Unique => $start_node_fn::<
+				unique_runtime::Runtime,
+				unique_runtime::RuntimeApi,
+				UniqueRuntimeExecutor,
+			>($config $(, $($args),+)?) $($code)*,
+
+			#[cfg(feature = "quartz-runtime")]
+			RuntimeId::Quartz => $start_node_fn::<
+				quartz_runtime::Runtime,
+				quartz_runtime::RuntimeApi,
+				QuartzRuntimeExecutor,
+			>($config $(, $($args),+)?) $($code)*,
+
+			RuntimeId::Opal => $start_node_fn::<
+				opal_runtime::Runtime,
+				opal_runtime::RuntimeApi,
+				OpalRuntimeExecutor,
+			>($config $(, $($args),+)?) $($code)*,
+
+			RuntimeId::Unknown(chain) => Err(no_runtime_err!(chain).into()),
+		}
+	};
+}
+
 /// Parse command line arguments into service configuration.
 pub fn run() -> Result<()> {
 	let cli = Cli::from_args();
@@ -365,7 +394,20 @@
 			let runner = cli.create_runner(&cli.run.normalize())?;
 
 			runner.run_node_until_exit(|config| async move {
-				let para_id = chain_spec::Extensions::try_get(&*config.chain_spec)
+				let extensions = chain_spec::Extensions::try_get(&*config.chain_spec);
+
+				let service_id = config.chain_spec.service_id();
+				let relay_chain_id = extensions.map(|e| e.relay_chain.clone());
+				let is_dev_service = matches![service_id, ServiceId::Dev]
+									|| relay_chain_id == Some("dev-service".into());
+
+				if is_dev_service {
+					return start_node_using_chain_runtime! {
+						start_dev_node(config).map_err(Into::into)
+					};
+				};
+
+				let para_id = extensions
 					.map(|e| e.para_id)
 					.ok_or("Could not find parachain ID in chain-spec.")?;
 
@@ -376,10 +418,10 @@
 						.chain(cli.relaychain_args.iter()),
 				);
 
-				let id = ParaId::from(para_id);
+				let para_id = ParaId::from(para_id);
 
 				let parachain_account =
-					AccountIdConversion::<polkadot_primitives::v0::AccountId>::into_account(&id);
+					AccountIdConversion::<polkadot_primitives::v0::AccountId>::into_account(&para_id);
 
 				let state_version =
 					RelayChainCli::native_runtime_version(&config.chain_spec).state_version();
@@ -395,7 +437,7 @@
 				)
 				.map_err(|err| format!("Relay chain argument error: {}", err))?;
 
-				info!("Parachain id: {:?}", id);
+				info!("Parachain id: {:?}", para_id);
 				info!("Parachain Account: {}", parachain_account);
 				info!("Parachain genesis state: {}", genesis_state);
 				info!("Parachain genesis hash: {}", genesis_hash);
@@ -408,37 +450,11 @@
 					}
 				);
 
-				match config.chain_spec.runtime_id() {
-					#[cfg(feature = "unique-runtime")]
-					RuntimeId::Unique => crate::service::start_node::<
-						unique_runtime::Runtime,
-						unique_runtime::RuntimeApi,
-						UniqueRuntimeExecutor,
-					>(config, polkadot_config, id)
-					.await
-					.map(|r| r.0)
-					.map_err(Into::into),
-
-					#[cfg(feature = "quartz-runtime")]
-					RuntimeId::Quartz => crate::service::start_node::<
-						quartz_runtime::Runtime,
-						quartz_runtime::RuntimeApi,
-						QuartzRuntimeExecutor,
-					>(config, polkadot_config, id)
-					.await
-					.map(|r| r.0)
-					.map_err(Into::into),
-
-					RuntimeId::Opal => crate::service::start_node::<
-						opal_runtime::Runtime,
-						opal_runtime::RuntimeApi,
-						OpalRuntimeExecutor,
-					>(config, polkadot_config, id)
-					.await
-					.map(|r| r.0)
-					.map_err(Into::into),
-
-					RuntimeId::Unknown(chain) => Err(no_runtime_err!(chain).into()),
+				start_node_using_chain_runtime! {
+					start_node(config, polkadot_config, para_id)
+						.await
+						.map(|r| r.0)
+						.map_err(Into::into)
 				}
 			})
 		}
modifiednode/cli/src/service.rsdiffbeforeafterboth
57use fc_mapping_sync::{MappingSyncWorker, SyncStrategy};57use fc_mapping_sync::{MappingSyncWorker, SyncStrategy};
5858
59use unique_runtime_common::types::{AuraId, RuntimeInstance, AccountId, Balance, Index, Hash, Block};59use unique_runtime_common::types::{AuraId, RuntimeInstance, AccountId, Balance, Index, Hash, Block};
60use crate::chain_spec::ServiceId;
6061
61/// Native executor instance.62/// Native executor instance.
62pub struct UniqueRuntimeExecutor;63pub struct UniqueRuntimeExecutor;
125 sc_service::TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<ExecutorDispatch>>;126 sc_service::TFullClient<Block, RuntimeApi, NativeElseWasmExecutor<ExecutorDispatch>>;
126type FullBackend = sc_service::TFullBackend<Block>;127type FullBackend = sc_service::TFullBackend<Block>;
127type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;128type FullSelectChain = sc_consensus::LongestChain<FullBackend, Block>;
129type MaybeSelectChain = Option<FullSelectChain>;
128130
129/// Starts a `ServiceBuilder` for a full service.131/// Starts a `ServiceBuilder` for a full service.
130///132///
134pub fn new_partial<RuntimeApi, ExecutorDispatch, BIQ>(136pub fn new_partial<RuntimeApi, ExecutorDispatch, BIQ>(
135 config: &Configuration,137 config: &Configuration,
136 build_import_queue: BIQ,138 build_import_queue: BIQ,
139 service_id: ServiceId,
137) -> Result<140) -> Result<
138 PartialComponents<141 PartialComponents<
139 FullClient<RuntimeApi, ExecutorDispatch>,142 FullClient<RuntimeApi, ExecutorDispatch>,
140 FullBackend,143 FullBackend,
141 FullSelectChain,144 MaybeSelectChain,
142 sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,145 sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>,
143 sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, ExecutorDispatch>>,146 sc_transaction_pool::FullPool<Block, FullClient<RuntimeApi, ExecutorDispatch>>,
144 (147 (
215 telemetry218 telemetry
216 });219 });
217220
218 let select_chain = sc_consensus::LongestChain::new(backend.clone());221 let select_chain = match service_id {
222 ServiceId::Prod => Some(sc_consensus::LongestChain::new(backend.clone())),
223 ServiceId::Dev => None
224 };
219225
220 let transaction_pool = sc_transaction_pool::BasicPool::new_full(226 let transaction_pool = sc_transaction_pool::BasicPool::new_full(
221 config.transaction_pool.clone(),227 config.transaction_pool.clone(),
318324
319 let params =325 let params =
320 new_partial::<RuntimeApi, ExecutorDispatch, BIQ>(&parachain_config, build_import_queue)?;326 new_partial::<RuntimeApi, ExecutorDispatch, BIQ>(
327 &parachain_config, build_import_queue, ServiceId::Prod
328 )?;
321 let (mut telemetry, filter_pool, frontier_backend, telemetry_worker_handle, fee_history_cache) =329 let (mut telemetry, filter_pool, frontier_backend, telemetry_worker_handle, fee_history_cache) =
322 params.other;330 params.other;
357 let rpc_client = client.clone();365 let rpc_client = client.clone();
358 let rpc_pool = transaction_pool.clone();366 let rpc_pool = transaction_pool.clone();
359 let select_chain = params.select_chain.clone();367 let select_chain = params.select_chain
368 .expect("select_chain always exists when running Prod service; qed")
369 .clone();
360 let rpc_network = network.clone();370 let rpc_network = network.clone();
361371
639 .await649 .await
640}650}
651
652fn dev_build_import_queue<RuntimeApi, ExecutorDispatch>(
653 client: Arc<FullClient<RuntimeApi, ExecutorDispatch>>,
654 config: &Configuration,
655 _: Option<TelemetryHandle>,
656 task_manager: &TaskManager,
657) -> Result<sc_consensus::DefaultImportQueue<Block, FullClient<RuntimeApi, ExecutorDispatch>>, sc_service::Error>
658where
659 RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>
660 + Send
661 + Sync
662 + 'static,
663 RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>
664 + sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>,
665 ExecutorDispatch: NativeExecutionDispatch + 'static,
666{
667 Ok(sc_consensus_manual_seal::import_queue(
668 Box::new(client.clone()),
669 &task_manager.spawn_essential_handle(),
670 config.prometheus_registry(),
671 ))
672}
673
674/// Builds a new development service. This service uses instant seal, and mocks
675/// the parachain inherent
676pub fn start_dev_node<Runtime, RuntimeApi, ExecutorDispatch>(config: Configuration)
677 -> sc_service::error::Result<TaskManager>
678where
679 Runtime: RuntimeInstance + Send + Sync + 'static,
680 <Runtime as RuntimeInstance>::CrossAccountId: Serialize,
681 for<'de> <Runtime as RuntimeInstance>::CrossAccountId: Deserialize<'de>,
682 RuntimeApi: sp_api::ConstructRuntimeApi<Block, FullClient<RuntimeApi, ExecutorDispatch>>
683 + Send
684 + Sync
685 + 'static,
686 RuntimeApi::RuntimeApi: sp_transaction_pool::runtime_api::TaggedTransactionQueue<Block>
687 + fp_rpc::EthereumRuntimeRPCApi<Block>
688 + sp_session::SessionKeys<Block>
689 + sp_block_builder::BlockBuilder<Block>
690 + pallet_transaction_payment_rpc_runtime_api::TransactionPaymentApi<Block, Balance>
691 + sp_api::ApiExt<Block, StateBackend = sc_client_api::StateBackendFor<FullBackend, Block>>
692 + up_rpc::UniqueApi<Block, Runtime::CrossAccountId, AccountId>
693 + substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>
694 + sp_api::Metadata<Block>
695 + sp_offchain::OffchainWorkerApi<Block>
696 + cumulus_primitives_core::CollectCollationInfo<Block>
697 + sp_consensus_aura::AuraApi<Block, AuraId>,
698 ExecutorDispatch: NativeExecutionDispatch + 'static,
699{
700 use futures::Stream;
701 use sc_consensus_manual_seal::{run_manual_seal, EngineCommand, ManualSealParams};
702 use fc_consensus::FrontierBlockImport;
703 use sc_client_api::HeaderBackend;
704
705 let sc_service::PartialComponents {
706 client,
707 backend,
708 mut task_manager,
709 import_queue,
710 keystore_container,
711 select_chain: maybe_select_chain,
712 transaction_pool,
713 other:
714 (
715 telemetry,
716 filter_pool,
717 frontier_backend,
718 _telemetry_worker_handle,
719 fee_history_cache,
720 ),
721 } = new_partial::<RuntimeApi, ExecutorDispatch, _>(
722 &config,
723 dev_build_import_queue::<RuntimeApi, ExecutorDispatch>,
724 ServiceId::Dev
725 )?;
726
727 let block_data_cache = Arc::new(fc_rpc::EthBlockDataCache::new(
728 task_manager.spawn_handle(),
729 overrides_handle::<_, _, Runtime>(client.clone()),
730 50,
731 50,
732 ));
733
734 let (network, system_rpc_tx, network_starter) =
735 sc_service::build_network(sc_service::BuildNetworkParams {
736 config: &config,
737 client: client.clone(),
738 transaction_pool: transaction_pool.clone(),
739 spawn_handle: task_manager.spawn_handle(),
740 import_queue,
741 block_announce_validator_builder: None,
742 warp_sync: None,
743 })?;
744
745 if config.offchain_worker.enabled {
746 sc_service::build_offchain_workers(
747 &config,
748 task_manager.spawn_handle(),
749 client.clone(),
750 network.clone(),
751 );
752 }
753
754 let prometheus_registry = config.prometheus_registry().cloned();
755 let collator = config.role.is_authority();
756
757 let select_chain = maybe_select_chain.clone().expect(
758 "`new_partial` builds a `LongestChainRule` when building dev service.\
759 We specified the dev service when calling `new_partial`.\
760 Therefore, a `LongestChainRule` is present. qed.",
761 );
762
763 if collator {
764 let block_import =
765 FrontierBlockImport::new(client.clone(), client.clone(), frontier_backend.clone());
766
767 let env = sc_basic_authorship::ProposerFactory::new(
768 task_manager.spawn_handle(),
769 client.clone(),
770 transaction_pool.clone(),
771 prometheus_registry.as_ref(),
772 telemetry.as_ref().map(|x| x.handle()),
773 );
774
775 let commands_stream: Box<dyn Stream<Item = EngineCommand<Hash>> + Send + Sync + Unpin> =
776 Box::new(
777 // This bit cribbed from the implementation of instant seal.
778 transaction_pool
779 .pool()
780 .validated_pool()
781 .import_notification_stream()
782 .map(|_| EngineCommand::SealNewBlock {
783 create_empty: true, // was false in Moonbeam
784 finalize: false,
785 parent_hash: None,
786 sender: None,
787 }),
788 );
789
790 let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;
791 let client_set_aside_for_cidp = client.clone();
792
793 task_manager.spawn_essential_handle().spawn_blocking(
794 "authorship_task",
795 Some("block-authoring"),
796 run_manual_seal(ManualSealParams {
797 block_import,
798 env,
799 client: client.clone(),
800 pool: transaction_pool.clone(),
801 commands_stream,
802 select_chain: select_chain.clone(),
803 consensus_data_provider: None,
804 create_inherent_data_providers: move |block: Hash, ()| {
805 let current_para_block = client_set_aside_for_cidp
806 .number(block)
807 .expect("Header lookup should succeed")
808 .expect("Header passed in as parent should be present in backend.");
809
810 let client_for_xcm = client_set_aside_for_cidp.clone();
811 async move {
812 let time = sp_timestamp::InherentDataProvider::from_system_time();
813
814 let mocked_parachain = cumulus_primitives_parachain_inherent::MockValidationDataInherentDataProvider {
815 current_para_block,
816 relay_offset: 1000,
817 relay_blocks_per_para_block: 2,
818 xcm_config: cumulus_primitives_parachain_inherent::MockXcmConfig::new(
819 &*client_for_xcm,
820 block,
821 Default::default(),
822 Default::default(),
823 ),
824 raw_downward_messages: vec![],
825 raw_horizontal_messages: vec![],
826 };
827
828 let slot =
829 sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_duration(
830 *time,
831 slot_duration.slot_duration(),
832 );
833
834 Ok((time, slot, mocked_parachain))
835 }
836 },
837 }),
838 );
839 }
840
841 task_manager.spawn_essential_handle().spawn(
842 "frontier-mapping-sync-worker",
843 Some("block-authoring"),
844 MappingSyncWorker::new(
845 client.import_notification_stream(),
846 Duration::new(6, 0),
847 client.clone(),
848 backend.clone(),
849 frontier_backend.clone(),
850 SyncStrategy::Normal,
851 )
852 .for_each(|()| futures::future::ready(())),
853 );
854
855 let subscription_executor = sc_rpc::SubscriptionTaskExecutor::new(task_manager.spawn_handle());
856 let rpc_client = client.clone();
857 let rpc_pool = transaction_pool.clone();
858 let rpc_network = network.clone();
859 let rpc_frontier_backend = frontier_backend.clone();
860 let rpc_extensions_builder = Box::new(move |deny_unsafe, _| {
861 let full_deps = unique_rpc::FullDeps {
862 backend: rpc_frontier_backend.clone(),
863 deny_unsafe,
864 client: rpc_client.clone(),
865 pool: rpc_pool.clone(),
866 graph: rpc_pool.pool().clone(),
867 // TODO: Unhardcode
868 enable_dev_signer: false,
869 filter_pool: filter_pool.clone(),
870 network: rpc_network.clone(),
871 select_chain: select_chain.clone(),
872 is_authority: collator,
873 // TODO: Unhardcode
874 max_past_logs: 10000,
875 block_data_cache: block_data_cache.clone(),
876 fee_history_cache: fee_history_cache.clone(),
877 // TODO: Unhardcode
878 fee_history_limit: 2048,
879 };
880
881 Ok(unique_rpc::create_full::<_, _, _, _, Runtime, RuntimeApi, _>(
882 full_deps,
883 subscription_executor.clone(),
884 ))
885 });
886
887 sc_service::spawn_tasks(sc_service::SpawnTasksParams {
888 network,
889 client,
890 keystore: keystore_container.sync_keystore(),
891 task_manager: &mut task_manager,
892 transaction_pool,
893 rpc_extensions_builder,
894 backend,
895 system_rpc_tx,
896 config,
897 telemetry: None,
898 })?;
899
900 network_starter.start_network();
901 Ok(task_manager)
902}
641903