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
168[dependencies.serde_json]168[dependencies.serde_json]
169version = '1.0.68'169version = '1.0.68'
170170
171[dependencies.sc-consensus-manual-seal]
172git = 'https://github.com/paritytech/substrate.git'
173branch = 'polkadot-v0.9.17'
171174
172################################################################################175################################################################################
173# Cumulus dependencies176# Cumulus dependencies
modifiednode/cli/src/chain_spec.rsdiffbeforeafterboth
68 }68 }
69}69}
70
71pub enum ServiceId {
72 Prod,
73 Dev
74}
75
76pub trait ServiceIdentification {
77 fn service_id(&self) -> ServiceId;
78}
79
80impl ServiceIdentification for Box<dyn sc_service::ChainSpec> {
81 fn service_id(&self) -> ServiceId {
82 if self.id().ends_with("dev") {
83 ServiceId::Dev
84 } else {
85 ServiceId::Prod
86 }
87 }
88}
7089
71/// Helper function to generate a crypto pair from seed90/// Helper function to generate a crypto pair from seed
72pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {91pub fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::Public {
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
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