2022-10-30 04:08:33 -04:00
|
|
|
use std::sync::{Arc, RwLock};
|
2022-10-22 07:36:13 -04:00
|
|
|
|
|
|
|
|
use sp_core::H256;
|
2022-07-15 00:05:00 -04:00
|
|
|
|
|
|
|
|
use sc_executor::NativeElseWasmExecutor;
|
2022-10-22 07:36:13 -04:00
|
|
|
use sc_service::{error::Error as ServiceError, Configuration, TaskManager};
|
|
|
|
|
use sc_network::{NetworkService, NetworkBlock};
|
2022-07-15 00:05:00 -04:00
|
|
|
use sc_telemetry::{Telemetry, TelemetryWorker};
|
|
|
|
|
|
|
|
|
|
use serai_runtime::{self, opaque::Block, RuntimeApi};
|
2022-10-30 04:08:33 -04:00
|
|
|
pub(crate) use serai_consensus::{
|
|
|
|
|
TendermintAuthority, ExecutorDispatch, Announce, FullClient, TendermintValidatorFirm,
|
|
|
|
|
};
|
2022-07-15 00:05:00 -04:00
|
|
|
|
|
|
|
|
type FullBackend = sc_service::TFullBackend<Block>;
|
2022-10-21 05:29:01 -04:00
|
|
|
type FullSelectChain = serai_consensus::TendermintSelectChain<Block, FullBackend>;
|
2022-07-15 00:05:00 -04:00
|
|
|
|
|
|
|
|
type PartialComponents = sc_service::PartialComponents<
|
|
|
|
|
FullClient,
|
|
|
|
|
FullBackend,
|
|
|
|
|
FullSelectChain,
|
|
|
|
|
sc_consensus::DefaultImportQueue<Block, FullClient>,
|
|
|
|
|
sc_transaction_pool::FullPool<Block, FullClient>,
|
|
|
|
|
Option<Telemetry>,
|
|
|
|
|
>;
|
|
|
|
|
|
2022-10-22 07:36:13 -04:00
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct NetworkAnnounce(Arc<RwLock<Option<Arc<NetworkService<Block, H256>>>>>);
|
|
|
|
|
impl NetworkAnnounce {
|
|
|
|
|
fn new() -> NetworkAnnounce {
|
|
|
|
|
NetworkAnnounce(Arc::new(RwLock::new(None)))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
impl Announce<Block> for NetworkAnnounce {
|
|
|
|
|
fn announce(&self, hash: H256) {
|
|
|
|
|
if let Some(network) = self.0.read().unwrap().as_ref() {
|
|
|
|
|
network.announce_block(hash, None);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-22 03:41:49 -04:00
|
|
|
pub fn new_partial(
|
|
|
|
|
config: &Configuration,
|
2022-10-30 04:08:33 -04:00
|
|
|
) -> Result<
|
|
|
|
|
(
|
|
|
|
|
(NetworkAnnounce, TendermintAuthority<TendermintValidatorFirm<NetworkAnnounce>>),
|
|
|
|
|
PartialComponents,
|
|
|
|
|
),
|
|
|
|
|
ServiceError,
|
|
|
|
|
> {
|
2022-07-15 00:05:00 -04:00
|
|
|
if config.keystore_remote.is_some() {
|
2022-07-15 01:26:07 -04:00
|
|
|
return Err(ServiceError::Other("Remote Keystores are not supported".to_string()));
|
2022-07-15 00:05:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 = NativeElseWasmExecutor::<ExecutorDispatch>::new(
|
|
|
|
|
config.wasm_method,
|
|
|
|
|
config.default_heap_pages,
|
|
|
|
|
config.max_runtime_instances,
|
2022-07-15 01:26:07 -04:00
|
|
|
config.runtime_cache_size,
|
2022-07-15 00:05:00 -04:00
|
|
|
);
|
|
|
|
|
|
2022-07-15 01:26:07 -04:00
|
|
|
let (client, backend, keystore_container, task_manager) =
|
|
|
|
|
sc_service::new_full_parts::<Block, RuntimeApi, _>(
|
|
|
|
|
config,
|
|
|
|
|
telemetry.as_ref().map(|(_, telemetry)| telemetry.handle()),
|
|
|
|
|
executor,
|
|
|
|
|
)?;
|
2022-07-15 00:05:00 -04:00
|
|
|
let client = Arc::new(client);
|
|
|
|
|
|
|
|
|
|
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(),
|
2022-07-15 01:26:07 -04:00
|
|
|
client.clone(),
|
2022-07-15 00:05:00 -04:00
|
|
|
);
|
|
|
|
|
|
2022-10-22 07:36:13 -04:00
|
|
|
let announce = NetworkAnnounce::new();
|
2022-10-22 03:41:49 -04:00
|
|
|
let (authority, import_queue) = serai_consensus::import_queue(
|
2022-10-21 23:36:24 -04:00
|
|
|
&task_manager,
|
|
|
|
|
client.clone(),
|
2022-10-22 07:36:13 -04:00
|
|
|
announce.clone(),
|
2022-10-21 23:36:24 -04:00
|
|
|
transaction_pool.clone(),
|
|
|
|
|
config.prometheus_registry(),
|
2022-10-22 03:41:49 -04:00
|
|
|
);
|
2022-10-21 05:29:01 -04:00
|
|
|
|
|
|
|
|
let select_chain = serai_consensus::TendermintSelectChain::new(backend.clone());
|
2022-07-15 00:05:00 -04:00
|
|
|
|
2022-10-22 03:41:49 -04:00
|
|
|
Ok((
|
2022-10-22 07:36:13 -04:00
|
|
|
(announce, authority),
|
2022-10-22 03:41:49 -04:00
|
|
|
sc_service::PartialComponents {
|
|
|
|
|
client,
|
|
|
|
|
backend,
|
|
|
|
|
task_manager,
|
|
|
|
|
import_queue,
|
|
|
|
|
keystore_container,
|
|
|
|
|
select_chain,
|
|
|
|
|
transaction_pool,
|
|
|
|
|
other: telemetry,
|
|
|
|
|
},
|
|
|
|
|
))
|
2022-07-15 00:05:00 -04:00
|
|
|
}
|
|
|
|
|
|
2022-10-22 03:41:49 -04:00
|
|
|
pub async fn new_full(config: Configuration) -> Result<TaskManager, ServiceError> {
|
|
|
|
|
let (
|
2022-10-22 07:36:13 -04:00
|
|
|
(announce, authority),
|
2022-10-22 03:41:49 -04:00
|
|
|
sc_service::PartialComponents {
|
|
|
|
|
client,
|
|
|
|
|
backend,
|
|
|
|
|
mut task_manager,
|
|
|
|
|
import_queue,
|
|
|
|
|
keystore_container,
|
|
|
|
|
select_chain: _,
|
|
|
|
|
other: mut telemetry,
|
|
|
|
|
transaction_pool,
|
|
|
|
|
},
|
|
|
|
|
) = new_partial(&config)?;
|
2022-07-15 00:05:00 -04:00
|
|
|
|
2022-09-29 13:33:09 -05:00
|
|
|
let (network, system_rpc_tx, tx_handler_controller, network_starter) =
|
2022-07-15 01:26:07 -04:00
|
|
|
sc_service::build_network(sc_service::BuildNetworkParams {
|
2022-07-15 00:05:00 -04:00
|
|
|
config: &config,
|
|
|
|
|
client: client.clone(),
|
|
|
|
|
transaction_pool: transaction_pool.clone(),
|
|
|
|
|
spawn_handle: task_manager.spawn_handle(),
|
|
|
|
|
import_queue,
|
|
|
|
|
block_announce_validator_builder: None,
|
|
|
|
|
warp_sync: None,
|
2022-07-15 01:26:07 -04:00
|
|
|
})?;
|
2022-10-22 07:36:13 -04:00
|
|
|
*announce.0.write().unwrap() = Some(network.clone());
|
2022-07-15 00:05:00 -04:00
|
|
|
|
|
|
|
|
if config.offchain_worker.enabled {
|
|
|
|
|
sc_service::build_offchain_workers(
|
|
|
|
|
&config,
|
|
|
|
|
task_manager.spawn_handle(),
|
|
|
|
|
client.clone(),
|
|
|
|
|
network.clone(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let rpc_extensions_builder = {
|
|
|
|
|
let client = client.clone();
|
|
|
|
|
let pool = transaction_pool.clone();
|
|
|
|
|
|
2022-07-15 01:26:07 -04:00
|
|
|
Box::new(move |deny_unsafe, _| {
|
|
|
|
|
crate::rpc::create_full(crate::rpc::FullDeps {
|
|
|
|
|
client: client.clone(),
|
|
|
|
|
pool: pool.clone(),
|
|
|
|
|
deny_unsafe,
|
|
|
|
|
})
|
|
|
|
|
.map_err(Into::into)
|
|
|
|
|
})
|
2022-07-15 00:05:00 -04:00
|
|
|
};
|
|
|
|
|
|
2022-10-22 03:41:49 -04:00
|
|
|
let is_authority = config.role.is_authority();
|
|
|
|
|
|
2022-07-15 01:26:07 -04:00
|
|
|
sc_service::spawn_tasks(sc_service::SpawnTasksParams {
|
|
|
|
|
network: network.clone(),
|
|
|
|
|
client: client.clone(),
|
|
|
|
|
keystore: keystore_container.sync_keystore(),
|
|
|
|
|
task_manager: &mut task_manager,
|
|
|
|
|
transaction_pool: transaction_pool.clone(),
|
|
|
|
|
rpc_builder: rpc_extensions_builder,
|
|
|
|
|
backend,
|
|
|
|
|
system_rpc_tx,
|
2022-09-29 13:33:09 -05:00
|
|
|
tx_handler_controller,
|
2022-07-15 01:26:07 -04:00
|
|
|
config,
|
|
|
|
|
telemetry: telemetry.as_mut(),
|
|
|
|
|
})?;
|
2022-07-15 00:05:00 -04:00
|
|
|
|
2022-10-22 03:41:49 -04:00
|
|
|
if is_authority {
|
2022-10-30 04:08:33 -04:00
|
|
|
authority.validate().await;
|
2022-07-15 00:05:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
network_starter.start_network();
|
|
|
|
|
Ok(task_manager)
|
|
|
|
|
}
|