use crate::common::{types::TeyrchainClient, ConstructNodeRuntimeApi, NodeBlock};
use pezsc_network::{
config::FullNetworkConfiguration, service::traits::NetworkService, NetworkBackend,
};
use pezsc_service::{Configuration, TaskManager};
use pezsc_statement_store::Store;
use std::sync::Arc;
use teyrchains_common::Hash;
pub(crate) fn new_statement_handler_proto<
Block: NodeBlock,
RuntimeApi,
Net: NetworkBackend<Block, Hash>,
>(
client: &TeyrchainClient<Block, RuntimeApi>,
teyrchain_config: &Configuration,
metrics: &pezsc_network::NotificationMetrics,
net_config: &mut FullNetworkConfiguration<Block, Hash, Net>,
) -> pezsc_network_statement::StatementHandlerPrototype {
let (statement_handler_proto, statement_config) =
pezsc_network_statement::StatementHandlerPrototype::new::<_, _, Net>(
client.chain_info().genesis_hash,
teyrchain_config.chain_spec.fork_id(),
metrics.clone(),
Arc::clone(&net_config.peer_store_handle()),
);
net_config.add_notification_protocol(statement_config);
statement_handler_proto
}
pub(crate) fn build_statement_store<
Block: NodeBlock,
RuntimeApi: ConstructNodeRuntimeApi<Block, TeyrchainClient<Block, RuntimeApi>>,
>(
teyrchain_config: &Configuration,
task_manager: &mut TaskManager,
client: Arc<TeyrchainClient<Block, RuntimeApi>>,
network: Arc<dyn NetworkService + 'static>,
sync_service: Arc<pezsc_network_sync::service::syncing_service::SyncingService<Block>>,
local_keystore: Arc<pezsc_keystore::LocalKeystore>,
statement_handler_proto: pezsc_network_statement::StatementHandlerPrototype,
) -> pezsc_service::error::Result<Arc<Store>> {
let statement_store = pezsc_statement_store::Store::new_shared(
&teyrchain_config.data_path,
Default::default(),
client,
local_keystore,
teyrchain_config.prometheus_registry(),
&task_manager.spawn_handle(),
)
.map_err(|e| pezsc_service::Error::Application(Box::new(e) as Box<_>))?;
let statement_protocol_executor = {
let spawn_handle = task_manager.spawn_handle();
Box::new(move |fut| {
spawn_handle.spawn("network-statement-validator", Some("networking"), fut);
})
};
let statement_handler = statement_handler_proto.build(
network,
sync_service,
statement_store.clone(),
teyrchain_config.prometheus_registry(),
statement_protocol_executor,
)?;
task_manager.spawn_handle().spawn(
"network-statement-handler",
Some("networking"),
statement_handler.run(),
);
Ok(statement_store)
}