referrerpolicy=no-referrer-when-downgrade

polkadot_omni_node_lib/common/
statement_store.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// This file is part of Cumulus.
3// SPDX-License-Identifier: Apache-2.0
4
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// 	http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17use crate::common::{types::ParachainClient, ConstructNodeRuntimeApi, NodeBlock};
18use parachains_common::Hash;
19use sc_network::{
20	config::FullNetworkConfiguration, service::traits::NetworkService, NetworkBackend,
21};
22use sc_service::{Configuration, TaskManager};
23use sc_statement_store::Store;
24use std::sync::Arc;
25
26/// Helper function to setup the statement store in `NodeSpec::start_node`.
27///
28/// Functions are tailored for internal usage, types are unnecessary opinionated for usage in
29/// `NodeSpec::start_node`.
30
31/// Build the statement handler prototype. Register the notification protocol in the network
32/// configuration.
33pub(crate) fn new_statement_handler_proto<
34	Block: NodeBlock,
35	RuntimeApi,
36	Net: NetworkBackend<Block, Hash>,
37>(
38	client: &ParachainClient<Block, RuntimeApi>,
39	parachain_config: &Configuration,
40	metrics: &sc_network::NotificationMetrics,
41	net_config: &mut FullNetworkConfiguration<Block, Hash, Net>,
42) -> sc_network_statement::StatementHandlerPrototype {
43	let (statement_handler_proto, statement_config) =
44		sc_network_statement::StatementHandlerPrototype::new::<_, _, Net>(
45			client.chain_info().genesis_hash,
46			parachain_config.chain_spec.fork_id(),
47			metrics.clone(),
48			Arc::clone(&net_config.peer_store_handle()),
49		);
50	net_config.add_notification_protocol(statement_config);
51	statement_handler_proto
52}
53
54/// Build the statement store, spawn the tasks.
55pub(crate) fn build_statement_store<
56	Block: NodeBlock,
57	RuntimeApi: ConstructNodeRuntimeApi<Block, ParachainClient<Block, RuntimeApi>>,
58>(
59	parachain_config: &Configuration,
60	task_manager: &mut TaskManager,
61	client: Arc<ParachainClient<Block, RuntimeApi>>,
62	network: Arc<dyn NetworkService + 'static>,
63	sync_service: Arc<sc_network_sync::service::syncing_service::SyncingService<Block>>,
64	local_keystore: Arc<sc_keystore::LocalKeystore>,
65	statement_handler_proto: sc_network_statement::StatementHandlerPrototype,
66) -> sc_service::error::Result<Arc<Store>> {
67	let statement_store = sc_statement_store::Store::new_shared(
68		&parachain_config.data_path,
69		Default::default(),
70		client,
71		local_keystore,
72		parachain_config.prometheus_registry(),
73		&task_manager.spawn_handle(),
74	)
75	.map_err(|e| sc_service::Error::Application(Box::new(e) as Box<_>))?;
76	let statement_protocol_executor = {
77		let spawn_handle = task_manager.spawn_handle();
78		Box::new(move |fut| {
79			spawn_handle.spawn("network-statement-validator", Some("networking"), fut);
80		})
81	};
82	let statement_handler = statement_handler_proto.build(
83		network,
84		sync_service,
85		statement_store.clone(),
86		parachain_config.prometheus_registry(),
87		statement_protocol_executor,
88	)?;
89	task_manager.spawn_handle().spawn(
90		"network-statement-handler",
91		Some("networking"),
92		statement_handler.run(),
93	);
94
95	Ok(statement_store)
96}