-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathservice.rs
564 lines (510 loc) · 17.6 KB
/
service.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
// basically half of this code obsolete copy paste and need to be throw away replaced by proper and feature rich stuff for latest node https://github.com/paritytech/cumulus/blob/master/client/service/src/lib.rs
use crate::{
chain_spec,
client::{Client, FullBackend, FullClient},
rpc,
runtime::{
assets::ExtendWithAssetsApi, cosmwasm::ExtendWithCosmwasmApi,
crowdloan_rewards::ExtendWithCrowdloanRewardsApi, farming::ExtendWithFarmingApi,
ibc::ExtendWithIbcApi, lending::ExtendWithLendingApi, pablo::ExtendWithPabloApi,
staking_rewards::ExtendWithStakingRewardsApi, BaseHostRuntimeApis,
},
};
use common::OpaqueBlock;
use cumulus_client_cli::CollatorOptions;
use cumulus_client_consensus_aura::{AuraConsensus, BuildAuraConsensusParams, SlotProportion};
use cumulus_client_consensus_common::ParachainBlockImport;
use cumulus_client_network::BlockAnnounceValidator;
use cumulus_client_service::{
prepare_node_config, start_collator, start_full_node, StartCollatorParams, StartFullNodeParams,
};
use cumulus_primitives_core::ParaId;
use cumulus_relay_chain_inprocess_interface::build_inprocess_relay_chain;
use cumulus_relay_chain_interface::{RelayChainInterface, RelayChainResult};
use cumulus_relay_chain_minimal_node::build_minimal_relay_chain_node;
use polkadot_service::CollatorPair;
use sc_client_api::StateBackendFor;
use sc_consensus::ImportQueue;
use sc_executor::NativeExecutionDispatch;
use sc_network_common::service::NetworkBlock;
use sc_service::{Configuration, PartialComponents, TaskManager};
use sc_telemetry::{Telemetry, TelemetryHandle, TelemetryWorker, TelemetryWorkerHandle};
use sp_api::{ConstructRuntimeApi, StateBackend};
#[cfg(feature = "ocw")]
use sp_core::crypto::KeyTypeId;
use sp_runtime::traits::BlakeTwo256;
use sp_trie::PrefixedMemoryDB;
use std::{sync::Arc, time::Duration};
pub struct PicassoExecutor;
impl sc_executor::NativeExecutionDispatch for PicassoExecutor {
type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;
fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {
picasso_runtime::api::dispatch(method, data)
}
fn native_version() -> sc_executor::NativeVersion {
picasso_runtime::version::native_version()
}
}
pub struct ComposableExecutor;
impl sc_executor::NativeExecutionDispatch for ComposableExecutor {
type ExtendHostFunctions = frame_benchmarking::benchmarking::HostFunctions;
fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> {
composable_runtime::api::dispatch(method, data)
}
fn native_version() -> sc_executor::NativeVersion {
composable_runtime::version::native_version()
}
}
pub enum Executor {
Picasso(PicassoExecutor),
Composable(ComposableExecutor),
}
/// Starts a `ServiceBuilder` for a full service.
///
/// Use this macro if you don't actually need the full service, but just the builder in order to
/// be able to perform chain operations.
#[allow(clippy::type_complexity)]
pub fn new_chain_ops(
config: &Configuration,
) -> Result<
(
Arc<Client>,
Arc<FullBackend>,
sc_consensus::BasicQueue<OpaqueBlock, PrefixedMemoryDB<BlakeTwo256>>,
TaskManager,
),
sc_service::Error,
> {
let components = match config.chain_spec.id() {
chain if chain.contains("composable") => {
let components = new_partial::<composable_runtime::RuntimeApi, ComposableExecutor>(
config,
Some(chain_spec::composable::DALEK_END_BLOCK),
)?;
(
Arc::new(Client::from(components.client)),
components.backend,
components.import_queue,
components.task_manager,
)
},
chain if chain.contains("picasso") => {
let components = new_partial::<picasso_runtime::RuntimeApi, PicassoExecutor>(
config,
Some(chain_spec::picasso::DALEK_END_BLOCK),
)?;
(
Arc::new(Client::from(components.client)),
components.backend,
components.import_queue,
components.task_manager,
)
},
chain => panic!("Unknown chain: {}", chain),
};
Ok(components)
}
#[allow(clippy::type_complexity)]
pub fn new_partial<RuntimeApi, Executor>(
config: &Configuration,
// The block number until ed25519-dalek should be used for signature verification.
dalek_end_block: Option<u32>,
) -> Result<
PartialComponents<
FullClient<RuntimeApi, Executor>,
FullBackend,
(),
sc_consensus::DefaultImportQueue<OpaqueBlock, FullClient<RuntimeApi, Executor>>,
sc_transaction_pool::FullPool<OpaqueBlock, FullClient<RuntimeApi, Executor>>,
(
Option<Telemetry>,
Option<TelemetryWorkerHandle>,
ParachainBlockImport<OpaqueBlock, Arc<FullClient<RuntimeApi, Executor>>, FullBackend>,
),
>,
sc_service::Error,
>
where
RuntimeApi:
ConstructRuntimeApi<OpaqueBlock, FullClient<RuntimeApi, Executor>> + Send + Sync + 'static,
RuntimeApi::RuntimeApi: BaseHostRuntimeApis<
StateBackend = sc_client_api::StateBackendFor<FullBackend, OpaqueBlock>,
>,
sc_client_api::StateBackendFor<FullBackend, OpaqueBlock>: sp_api::StateBackend<BlakeTwo256>,
Executor: sc_executor::NativeExecutionDispatch + 'static,
{
let telemetry = config
.telemetry_endpoints
.clone()
.filter(|x| !x.is_empty())
.map(|endpoints| -> Result<_, sc_telemetry::Error> {
let worker = TelemetryWorker::new(16)?;
let telemetry = worker.handle().new_telemetry(endpoints);
Ok((worker, telemetry))
})
.transpose()?;
let executor = sc_executor::NativeElseWasmExecutor::<Executor>::new(
config.wasm_method,
config.default_heap_pages,
config.max_runtime_instances,
config.runtime_cache_size,
);
let (client, backend, keystore_container, task_manager) =
sc_service::new_full_parts::<OpaqueBlock, RuntimeApi, _>(
config,
telemetry.as_ref().map(|(_, telemetry)| telemetry.handle()),
executor,
)?;
let client = Arc::new(client);
use sc_client_api::ExecutorProvider;
client.execution_extensions().set_extensions_factory(
sc_client_api::execution_extensions::ExtensionBeforeBlock::<
crate::client::Block,
sp_io::UseDalekExt,
>::new(dalek_end_block.unwrap_or(0)),
);
let telemetry_worker_handle = telemetry.as_ref().map(|(worker, _)| worker.handle());
let telemetry = telemetry.map(|(worker, telemetry)| {
task_manager.spawn_handle().spawn("telemetry", None, worker.run());
telemetry
});
let transaction_pool = sc_transaction_pool::BasicPool::new_full(
config.transaction_pool.clone(),
config.role.is_authority().into(),
config.prometheus_registry(),
task_manager.spawn_essential_handle(),
client.clone(),
);
let (import_queue, parachain_block_import) = parachain_build_import_queue(
backend.clone(),
client.clone(),
config,
telemetry.as_ref().map(|telemetry| telemetry.handle()),
&task_manager,
)?;
let params = PartialComponents {
backend,
client,
import_queue,
keystore_container,
task_manager,
transaction_pool,
select_chain: (),
other: (telemetry, telemetry_worker_handle, parachain_block_import),
};
Ok(params)
}
/// Start the right parachain subsystem for the right chainspec.
pub async fn start_node(
config: Configuration,
polkadot_config: Configuration,
collator_options: CollatorOptions,
id: ParaId,
) -> sc_service::error::Result<TaskManager> {
let task_manager = match config.chain_spec.id() {
chain if chain.contains("composable") =>
crate::service::start_node_impl::<composable_runtime::RuntimeApi, ComposableExecutor>(
config,
polkadot_config,
collator_options,
id,
Some(chain_spec::composable::DALEK_END_BLOCK),
)
.await?,
chain if chain.contains("picasso") =>
crate::service::start_node_impl::<picasso_runtime::RuntimeApi, PicassoExecutor>(
config,
polkadot_config,
collator_options,
id,
Some(chain_spec::picasso::DALEK_END_BLOCK),
)
.await?,
_ => panic!("Unknown chain_id: {}", config.chain_spec.id()),
};
Ok(task_manager)
}
/// Start a node with the given parachain `Configuration` and relay chain `Configuration`.
///
/// This is the actual implementation that is abstract over the executor and the runtime api.
#[sc_tracing::logging::prefix_logs_with("Parachain")]
async fn start_node_impl<RuntimeApi, Executor>(
parachain_config: Configuration,
polkadot_config: Configuration,
collator_options: CollatorOptions,
id: ParaId,
// The block number until ed25519-dalek should be used for signature verification.
dalek_end_block: Option<u32>,
) -> sc_service::error::Result<TaskManager>
where
RuntimeApi:
ConstructRuntimeApi<OpaqueBlock, FullClient<RuntimeApi, Executor>> + Send + Sync + 'static,
RuntimeApi::RuntimeApi: BaseHostRuntimeApis<StateBackend = StateBackendFor<FullBackend, OpaqueBlock>>
+ ExtendWithStakingRewardsApi<RuntimeApi, Executor>
+ ExtendWithAssetsApi<RuntimeApi, Executor>
+ ExtendWithCrowdloanRewardsApi<RuntimeApi, Executor>
+ ExtendWithPabloApi<RuntimeApi, Executor>
+ ExtendWithFarmingApi<RuntimeApi, Executor>
+ ExtendWithLendingApi<RuntimeApi, Executor>
+ ExtendWithCosmwasmApi<RuntimeApi, Executor>
+ ExtendWithIbcApi<RuntimeApi, Executor>,
StateBackendFor<FullBackend, OpaqueBlock>: StateBackend<BlakeTwo256>,
Executor: NativeExecutionDispatch + 'static,
{
let parachain_config = prepare_node_config(parachain_config);
let params = new_partial::<RuntimeApi, Executor>(¶chain_config, dalek_end_block)?;
#[cfg(feature = "ocw")]
{
let keystore = params.keystore_container.sync_keystore();
if parachain_config.offchain_worker.enabled {
// Initialize seed for signing transaction using off-chain workers. This is a
// convenience so learners can see the transactions submitted simply running the node.
// Typically these keys should be inserted with RPC calls to `author_insertKey`.
// TODO(Jesse): remove in prod
{
sp_keystore::SyncCryptoStore::sr25519_generate_new(
&*keystore,
KeyTypeId(*b"orac"),
Some("//Alice"),
)
.expect("Creating key with account Alice should succeed.");
}
}
}
let (mut telemetry, telemetry_worker_handle, parachain_block_import) = params.other;
let client = params.client.clone();
let backend = params.backend.clone();
let mut task_manager = params.task_manager;
let (relay_chain_interface, collator_key) = build_relay_chain_interface(
polkadot_config,
¶chain_config,
telemetry_worker_handle,
&mut task_manager,
collator_options.clone(),
)
.await
.map_err(|e| sc_service::Error::Application(Box::new(e) as Box<_>))?;
let block_announce_validator = BlockAnnounceValidator::new(relay_chain_interface.clone(), id);
let force_authoring = parachain_config.force_authoring;
let validator = parachain_config.role.is_authority();
let prometheus_registry = parachain_config.prometheus_registry().cloned();
let transaction_pool = params.transaction_pool.clone();
let import_queue_service = params.import_queue.service();
let (network, system_rpc_tx, tx_handler_controller, start_network) =
sc_service::build_network(sc_service::BuildNetworkParams {
config: ¶chain_config,
client: client.clone(),
transaction_pool: transaction_pool.clone(),
spawn_handle: task_manager.spawn_handle(),
import_queue: params.import_queue,
// do the warp for remote debug https://github.com/paritytech/cumulus/blob/polkadot-v0.9.39/client/service/src/lib.rs
warp_sync_params: None,
block_announce_validator_builder: Some(Box::new(|_| {
Box::new(block_announce_validator)
})),
})?;
if parachain_config.offchain_worker.enabled {
sc_service::build_offchain_workers(
¶chain_config,
task_manager.spawn_handle(),
client.clone(),
network.clone(),
);
}
let rpc_builder = {
let client = client.clone();
let transaction_pool = transaction_pool.clone();
let chain_props = parachain_config.chain_spec.properties();
Box::new(move |deny_unsafe, _| {
let deps = rpc::FullDeps {
client: client.clone(),
pool: transaction_pool.clone(),
deny_unsafe,
chain_props: chain_props.clone(),
};
Ok(rpc::create(deps).expect("RPC failed to initialize"))
})
};
sc_service::spawn_tasks(sc_service::SpawnTasksParams {
rpc_builder,
client: client.clone(),
transaction_pool: transaction_pool.clone(),
task_manager: &mut task_manager,
config: parachain_config,
keystore: params.keystore_container.sync_keystore(),
backend: backend.clone(),
network: network.clone(),
tx_handler_controller,
system_rpc_tx,
telemetry: telemetry.as_mut(),
})?;
let announce_block = {
let network = network.clone();
Arc::new(move |hash, data| network.announce_block(hash, data))
};
let relay_chain_slot_duration = Duration::from_secs(6);
let overseer_handle = relay_chain_interface
.overseer_handle()
.map_err(|e| sc_service::Error::Application(Box::new(e)))?;
if validator {
let keystore = params.keystore_container.sync_keystore();
let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;
let proposer_factory = sc_basic_authorship::ProposerFactory::with_proof_recording(
task_manager.spawn_handle(),
client.clone(),
transaction_pool,
prometheus_registry.as_ref(),
telemetry.as_ref().map(|t| t.handle()),
);
let backoff_authoring_blocks =
Some(sc_consensus_slots::BackoffAuthoringOnFinalizedHeadLagging::default());
let relay_chain_interface_inherent = relay_chain_interface.clone();
let parachain_consensus =
AuraConsensus::build::<sp_consensus_aura::sr25519::AuthorityPair, _, _, _, _, _, _>(
BuildAuraConsensusParams {
proposer_factory,
create_inherent_data_providers: move |_, (relay_parent, validation_data)| {
let relay_chain_interface_inherent = relay_chain_interface_inherent.clone();
async move {
let parachain_inherent = cumulus_primitives_parachain_inherent::ParachainInherentData::create_at(
relay_parent,
&relay_chain_interface_inherent,
&validation_data,
id,
)
.await;
let time = sp_timestamp::InherentDataProvider::from_system_time();
let slot =
sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration(
*time,
slot_duration,
);
let parachain_inherent = parachain_inherent.ok_or_else(|| {
Box::<dyn std::error::Error + Send + Sync>::from(
"Failed to create parachain inherent",
)
})?;
Ok((slot, time, parachain_inherent))
}
},
block_import: parachain_block_import,
para_client: client.clone(),
backoff_authoring_blocks,
sync_oracle: network,
keystore,
force_authoring,
slot_duration,
telemetry: telemetry.as_ref().map(|t| t.handle()),
// We got around 500ms for proposing
block_proposal_slot_portion: SlotProportion::new(1_f32 / 24_f32),
// And a maximum of 750ms if slots are skipped
max_block_proposal_slot_portion: Some(SlotProportion::new(1_f32 / 16_f32)),
},
);
let spawner = task_manager.spawn_handle();
let params = StartCollatorParams {
para_id: id,
relay_chain_interface: relay_chain_interface.clone(),
block_status: client.clone(),
announce_block,
client: client.clone(),
task_manager: &mut task_manager,
spawner,
parachain_consensus,
import_queue: import_queue_service,
collator_key: collator_key.expect("Command line arguments do not allow this. qed"),
relay_chain_slot_duration,
recovery_handle: Box::new(overseer_handle),
};
start_collator(params).await?;
} else {
let params = StartFullNodeParams {
client: client.clone(),
announce_block,
task_manager: &mut task_manager,
para_id: id,
relay_chain_interface,
relay_chain_slot_duration,
import_queue: import_queue_service,
recovery_handle: Box::new(overseer_handle),
};
start_full_node(params)?;
}
start_network.start_network();
Ok(task_manager)
}
/// Build the import queue for the the parachain runtime.
#[allow(clippy::type_complexity)]
pub fn parachain_build_import_queue<RuntimeApi, Executor>(
backend: Arc<FullBackend>,
client: Arc<FullClient<RuntimeApi, Executor>>,
config: &Configuration,
telemetry: Option<TelemetryHandle>,
task_manager: &TaskManager,
) -> Result<
(
sc_consensus::DefaultImportQueue<OpaqueBlock, FullClient<RuntimeApi, Executor>>,
ParachainBlockImport<OpaqueBlock, Arc<FullClient<RuntimeApi, Executor>>, FullBackend>,
),
sc_service::Error,
>
where
RuntimeApi:
ConstructRuntimeApi<OpaqueBlock, FullClient<RuntimeApi, Executor>> + Send + Sync + 'static,
RuntimeApi::RuntimeApi: BaseHostRuntimeApis<
StateBackend = sc_client_api::StateBackendFor<FullBackend, OpaqueBlock>,
>,
Executor: NativeExecutionDispatch + 'static,
{
let block_import = ParachainBlockImport::new(client.clone(), backend);
let slot_duration = sc_consensus_aura::slot_duration(&*client)?;
cumulus_client_consensus_aura::import_queue::<
sp_consensus_aura::sr25519::AuthorityPair,
_,
_,
_,
_,
_,
>(cumulus_client_consensus_aura::ImportQueueParams {
block_import: block_import.clone(),
client,
create_inherent_data_providers: move |_, _| async move {
let time = sp_timestamp::InherentDataProvider::from_system_time();
let slot =
sp_consensus_aura::inherents::InherentDataProvider::from_timestamp_and_slot_duration(
*time,
slot_duration,
);
Ok((slot, time))
},
registry: config.prometheus_registry(),
spawner: &task_manager.spawn_essential_handle(),
telemetry,
})
.map(|shared| (shared, block_import))
.map_err(Into::into)
}
async fn build_relay_chain_interface(
polkadot_config: Configuration,
parachain_config: &Configuration,
telemetry_worker_handle: Option<TelemetryWorkerHandle>,
task_manager: &mut TaskManager,
collator_options: CollatorOptions,
) -> RelayChainResult<(Arc<(dyn RelayChainInterface + 'static)>, Option<CollatorPair>)> {
if !collator_options.relay_chain_rpc_urls.is_empty() {
build_minimal_relay_chain_node(
polkadot_config,
task_manager,
collator_options.relay_chain_rpc_urls,
)
.await
} else {
build_inprocess_relay_chain(
polkadot_config,
parachain_config,
telemetry_worker_handle,
task_manager,
None,
)
}
}