123use nft_runtime::{self, opaque::Block, RuntimeApi};4use sc_client_api::ExecutorProvider;5use sc_consensus::LongestChain;6use sc_executor::native_executor_instance;7pub use sc_executor::NativeExecutor;8use sc_finality_grandpa::{9 FinalityProofProvider as GrandpaFinalityProofProvider, SharedVoterState,10 StorageAndProofProvider,11};12use sc_service::{error::Error as ServiceError, AbstractService, Configuration, ServiceBuilder};13use sp_consensus_aura::sr25519::AuthorityPair as AuraPair;14use sp_inherents::InherentDataProviders;15use std::sync::Arc;16use std::time::Duration;171819native_executor_instance!(20 pub Executor,21 nft_runtime::api::dispatch,22 nft_runtime::native_version,23);242526272829macro_rules! new_full_start {30 ($config:expr) => {{31 use jsonrpc_core::IoHandler;32 use sp_consensus_aura::sr25519::AuthorityPair as AuraPair;33 use std::sync::Arc;3435 let mut import_setup = None;36 let inherent_data_providers = sp_inherents::InherentDataProviders::new();3738 let builder = sc_service::ServiceBuilder::new_full::<39 nft_runtime::opaque::Block,40 nft_runtime::RuntimeApi,41 crate::service::Executor,42 >($config)?43 .with_select_chain(|_config, backend| Ok(sc_consensus::LongestChain::new(backend.clone())))?44 .with_transaction_pool(|builder| {45 let pool_api = sc_transaction_pool::FullChainApi::new(builder.client().clone());46 Ok(sc_transaction_pool::BasicPool::new(47 builder.config().transaction_pool.clone(),48 std::sync::Arc::new(pool_api),49 builder.prometheus_registry(),50 ))51 })?52 .with_import_queue(53 |_config, client, mut select_chain, _transaction_pool, spawn_task_handle, registry| {54 let select_chain = select_chain55 .take()56 .ok_or_else(|| sc_service::Error::SelectChainRequired)?;5758 let (grandpa_block_import, grandpa_link) = sc_finality_grandpa::block_import(59 client.clone(),60 &(client.clone() as Arc<_>),61 select_chain,62 )?;6364 let aura_block_import =65 sc_consensus_aura::AuraBlockImport::<_, _, _, AuraPair>::new(66 grandpa_block_import.clone(),67 client.clone(),68 );6970 let import_queue = sc_consensus_aura::import_queue::<_, _, _, AuraPair, _>(71 sc_consensus_aura::slot_duration(&*client)?,72 aura_block_import,73 Some(Box::new(grandpa_block_import.clone())),74 None,75 client,76 inherent_data_providers.clone(),77 spawn_task_handle,78 registry,79 )?;8081 import_setup = Some((grandpa_block_import, grandpa_link));8283 Ok(import_queue)84 },85 )?86 .with_rpc_extensions(|builder| -> Result<IoHandler<sc_rpc::Metadata>, _> {87 let handler = pallet_contracts_rpc::Contracts::new(builder.client().clone());88 let delegate = pallet_contracts_rpc::ContractsApi::to_delegate(handler);8990 let mut io = IoHandler::default();91 io.extend_with(delegate);92 Ok(io)93 })?;9495 (builder, import_setup, inherent_data_providers)96 }};97}9899100pub fn new_full(config: Configuration) -> Result<impl AbstractService, ServiceError> {101 let role = config.role.clone();102 let force_authoring = config.force_authoring;103 let name = config.network.node_name.clone();104 let disable_grandpa = config.disable_grandpa;105106 let (builder, mut import_setup, inherent_data_providers) = new_full_start!(config);107108 let (block_import, grandpa_link) = import_setup.take().expect(109 "Link Half and Block Import are present for Full Services or setup failed before. qed",110 );111112 let service = builder113 .with_finality_proof_provider(|client, backend| {114 115 let provider = client as Arc<dyn StorageAndProofProvider<_, _>>;116 Ok(Arc::new(GrandpaFinalityProofProvider::new(backend, provider)) as _)117 })?118 .build_full()?;119120 if role.is_authority() {121 let proposer = sc_basic_authorship::ProposerFactory::new(122 service.client(),123 service.transaction_pool(),124 service.prometheus_registry().as_ref(),125 );126127 let client = service.client();128 let select_chain = service129 .select_chain()130 .ok_or(ServiceError::SelectChainRequired)?;131132 let can_author_with =133 sp_consensus::CanAuthorWithNativeVersion::new(client.executor().clone());134135 let aura = sc_consensus_aura::start_aura::<_, _, _, _, _, AuraPair, _, _, _>(136 sc_consensus_aura::slot_duration(&*client)?,137 client,138 select_chain,139 block_import,140 proposer,141 service.network(),142 inherent_data_providers.clone(),143 force_authoring,144 service.keystore(),145 can_author_with,146 )?;147148 149 150 service151 .spawn_essential_task_handle()152 .spawn_blocking("aura", aura);153 }154155 156 157 let keystore = if role.is_authority() {158 Some(service.keystore() as sp_core::traits::BareCryptoStorePtr)159 } else {160 None161 };162163 let grandpa_config = sc_finality_grandpa::Config {164 165 gossip_duration: Duration::from_millis(333),166 justification_period: 512,167 name: Some(name),168 observer_enabled: false,169 keystore,170 is_authority: role.is_network_authority(),171 };172173 let enable_grandpa = !disable_grandpa;174 if enable_grandpa {175 176 177 178 179 180 181 let grandpa_config = sc_finality_grandpa::GrandpaParams {182 config: grandpa_config,183 link: grandpa_link,184 network: service.network(),185 inherent_data_providers: inherent_data_providers.clone(),186 telemetry_on_connect: Some(service.telemetry_on_connect_stream()),187 voting_rule: sc_finality_grandpa::VotingRulesBuilder::default().build(),188 prometheus_registry: service.prometheus_registry(),189 shared_voter_state: SharedVoterState::empty(),190 };191 192 193 194 service.spawn_essential_task_handle().spawn_blocking(195 "grandpa-voter",196 sc_finality_grandpa::run_grandpa_voter(grandpa_config)?,197 );198 } else {199 sc_finality_grandpa::setup_disabled_grandpa(200 service.client(),201 &inherent_data_providers,202 service.network(),203 )?;204 }205206 Ok(service)207}208209210pub fn new_light(config: Configuration) -> Result<impl AbstractService, ServiceError> {211 let inherent_data_providers = InherentDataProviders::new();212213 ServiceBuilder::new_light::<Block, RuntimeApi, Executor>(config)?214 .with_select_chain(|_config, backend| Ok(LongestChain::new(backend.clone())))?215 .with_transaction_pool(|builder| {216 let fetcher = builder217 .fetcher()218 .ok_or_else(|| "Trying to start light transaction pool without active fetcher")?;219220 let pool_api =221 sc_transaction_pool::LightChainApi::new(builder.client().clone(), fetcher.clone());222 let pool = sc_transaction_pool::BasicPool::with_revalidation_type(223 builder.config().transaction_pool.clone(),224 Arc::new(pool_api),225 builder.prometheus_registry(),226 sc_transaction_pool::RevalidationType::Light,227 );228 Ok(pool)229 })?230 .with_import_queue_and_fprb(231 |_config,232 client,233 backend,234 fetcher,235 _select_chain,236 _tx_pool,237 spawn_task_handle,238 prometheus_registry| {239 let fetch_checker = fetcher240 .map(|fetcher| fetcher.checker().clone())241 .ok_or_else(|| {242 "Trying to start light import queue without active fetch checker"243 })?;244 let grandpa_block_import = sc_finality_grandpa::light_block_import(245 client.clone(),246 backend,247 &(client.clone() as Arc<_>),248 Arc::new(fetch_checker),249 )?;250 let finality_proof_import = grandpa_block_import.clone();251 let finality_proof_request_builder =252 finality_proof_import.create_finality_proof_request_builder();253254 let import_queue = sc_consensus_aura::import_queue::<_, _, _, AuraPair, _>(255 sc_consensus_aura::slot_duration(&*client)?,256 grandpa_block_import,257 None,258 Some(Box::new(finality_proof_import)),259 client,260 inherent_data_providers.clone(),261 spawn_task_handle,262 prometheus_registry,263 )?;264265 Ok((import_queue, finality_proof_request_builder))266 },267 )?268 .with_finality_proof_provider(|client, backend| {269 270 let provider = client as Arc<dyn StorageAndProofProvider<_, _>>;271 Ok(Arc::new(GrandpaFinalityProofProvider::new(backend, provider)) as _)272 })?273 .build_light()274}