|
| 1 | +// Copyright (C) Parity Technologies (UK) Ltd. |
| 2 | +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 |
| 3 | + |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | + |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU General Public License for more details. |
| 13 | + |
| 14 | +// You should have received a copy of the GNU General Public License |
| 15 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +use sc_client_api::{ |
| 18 | + BlockBackend, BlockchainEvents, ExecutorProvider, ProofProvider, StorageProvider, UsageProvider, |
| 19 | +}; |
| 20 | +use sc_rpc_api::DenyUnsafe; |
| 21 | +use sc_service::{ |
| 22 | + gen_rpc_module, init_telemetry, propagate_transaction_notifications, start_rpc_servers, Error, |
| 23 | + MetricsService, RpcHandlers, SpawnTasksParams, |
| 24 | +}; |
| 25 | +use sc_transaction_pool_api::MaintainedTransactionPool; |
| 26 | +use sp_api::{CallApiAt, ProvideRuntimeApi}; |
| 27 | +use sp_blockchain::{HeaderBackend, HeaderMetadata}; |
| 28 | +use sp_consensus::block_validation::Chain; |
| 29 | +use sp_runtime::traits::{Block as BlockT, BlockIdTo}; |
| 30 | +use std::sync::Arc; |
| 31 | +use tracing::info; |
| 32 | + |
| 33 | +/// Spawn the tasks that are required to run a node. |
| 34 | +pub(super) fn spawn_tasks<TBl, TBackend, TExPool, TRpc, TCl>( |
| 35 | + params: SpawnTasksParams<TBl, TCl, TExPool, TRpc, TBackend>, |
| 36 | +) -> Result<RpcHandlers, Error> |
| 37 | +where |
| 38 | + TCl: ProvideRuntimeApi<TBl> |
| 39 | + + HeaderMetadata<TBl, Error = sp_blockchain::Error> |
| 40 | + + Chain<TBl> |
| 41 | + + BlockBackend<TBl> |
| 42 | + + BlockIdTo<TBl, Error = sp_blockchain::Error> |
| 43 | + + ProofProvider<TBl> |
| 44 | + + HeaderBackend<TBl> |
| 45 | + + BlockchainEvents<TBl> |
| 46 | + + ExecutorProvider<TBl> |
| 47 | + + UsageProvider<TBl> |
| 48 | + + StorageProvider<TBl, TBackend> |
| 49 | + + CallApiAt<TBl> |
| 50 | + + Send |
| 51 | + + 'static, |
| 52 | + <TCl as ProvideRuntimeApi<TBl>>::Api: sp_api::Metadata<TBl> |
| 53 | + + sp_transaction_pool::runtime_api::TaggedTransactionQueue<TBl> |
| 54 | + + sp_session::SessionKeys<TBl> |
| 55 | + + sp_api::ApiExt<TBl>, |
| 56 | + TBl: BlockT, |
| 57 | + TBl::Hash: Unpin, |
| 58 | + TBl::Header: Unpin, |
| 59 | + TBackend: 'static + sc_client_api::backend::Backend<TBl> + Send, |
| 60 | + TExPool: MaintainedTransactionPool<Block = TBl, Hash = <TBl as BlockT>::Hash> + 'static, |
| 61 | +{ |
| 62 | + let SpawnTasksParams { |
| 63 | + // TODO: Stop using `Configuration` once |
| 64 | + // https://github.com/paritytech/polkadot-sdk/pull/5364 is in our fork |
| 65 | + mut config, |
| 66 | + task_manager, |
| 67 | + client, |
| 68 | + backend, |
| 69 | + keystore, |
| 70 | + transaction_pool, |
| 71 | + rpc_builder, |
| 72 | + network, |
| 73 | + system_rpc_tx, |
| 74 | + tx_handler_controller, |
| 75 | + sync_service, |
| 76 | + telemetry, |
| 77 | + } = params; |
| 78 | + |
| 79 | + let chain_info = client.usage_info().chain; |
| 80 | + |
| 81 | + let sysinfo = sc_sysinfo::gather_sysinfo(); |
| 82 | + sc_sysinfo::print_sysinfo(&sysinfo); |
| 83 | + |
| 84 | + let telemetry = telemetry |
| 85 | + .map(|telemetry| { |
| 86 | + init_telemetry( |
| 87 | + &mut config, |
| 88 | + network.clone(), |
| 89 | + client.clone(), |
| 90 | + telemetry, |
| 91 | + Some(sysinfo), |
| 92 | + ) |
| 93 | + }) |
| 94 | + .transpose()?; |
| 95 | + |
| 96 | + info!("📦 Highest known block at #{}", chain_info.best_number); |
| 97 | + |
| 98 | + let spawn_handle = task_manager.spawn_handle(); |
| 99 | + |
| 100 | + // Inform the tx pool about imported and finalized blocks. |
| 101 | + spawn_handle.spawn( |
| 102 | + "txpool-notifications", |
| 103 | + Some("transaction-pool"), |
| 104 | + sc_transaction_pool::notification_future(client.clone(), transaction_pool.clone()), |
| 105 | + ); |
| 106 | + |
| 107 | + spawn_handle.spawn( |
| 108 | + "on-transaction-imported", |
| 109 | + Some("transaction-pool"), |
| 110 | + propagate_transaction_notifications( |
| 111 | + transaction_pool.clone(), |
| 112 | + tx_handler_controller, |
| 113 | + telemetry.clone(), |
| 114 | + ), |
| 115 | + ); |
| 116 | + |
| 117 | + // Periodically updated metrics and telemetry updates. |
| 118 | + spawn_handle.spawn( |
| 119 | + "telemetry-periodic-send", |
| 120 | + None, |
| 121 | + MetricsService::new(telemetry).run( |
| 122 | + client.clone(), |
| 123 | + transaction_pool.clone(), |
| 124 | + network.clone(), |
| 125 | + sync_service.clone(), |
| 126 | + ), |
| 127 | + ); |
| 128 | + |
| 129 | + let rpc_id_provider = config.rpc_id_provider.take(); |
| 130 | + |
| 131 | + // jsonrpsee RPC |
| 132 | + let gen_rpc_module = |deny_unsafe: DenyUnsafe| { |
| 133 | + gen_rpc_module( |
| 134 | + deny_unsafe, |
| 135 | + task_manager.spawn_handle(), |
| 136 | + client.clone(), |
| 137 | + transaction_pool.clone(), |
| 138 | + keystore.clone(), |
| 139 | + system_rpc_tx.clone(), |
| 140 | + &config, |
| 141 | + backend.clone(), |
| 142 | + &*rpc_builder, |
| 143 | + ) |
| 144 | + }; |
| 145 | + |
| 146 | + let rpc = config |
| 147 | + .rpc_addr |
| 148 | + .map(|_| start_rpc_servers(&config, gen_rpc_module, rpc_id_provider)) |
| 149 | + .transpose()?; |
| 150 | + let rpc_handlers = RpcHandlers::new(Arc::new(gen_rpc_module(DenyUnsafe::No)?)); |
| 151 | + |
| 152 | + // Spawn informant task |
| 153 | + spawn_handle.spawn( |
| 154 | + "informant", |
| 155 | + None, |
| 156 | + sc_informant::build( |
| 157 | + client.clone(), |
| 158 | + network, |
| 159 | + sync_service.clone(), |
| 160 | + config.informant_output_format, |
| 161 | + ), |
| 162 | + ); |
| 163 | + |
| 164 | + task_manager.keep_alive((config.base_path, rpc)); |
| 165 | + |
| 166 | + Ok(rpc_handlers) |
| 167 | +} |
0 commit comments