diff --git a/client/relay-chain-minimal-node/Cargo.toml b/client/relay-chain-minimal-node/Cargo.toml index 6c9cb255363..911c33ffc3b 100644 --- a/client/relay-chain-minimal-node/Cargo.toml +++ b/client/relay-chain-minimal-node/Cargo.toml @@ -41,6 +41,7 @@ cumulus-relay-chain-interface = { path = "../relay-chain-interface" } cumulus-relay-chain-rpc-interface = { path = "../relay-chain-rpc-interface" } cumulus-primitives-core = { path = "../../primitives/core" } +array-bytes = "4.1" lru = "0.8" tracing = "0.1.25" async-trait = "0.1.52" diff --git a/client/relay-chain-minimal-node/src/network.rs b/client/relay-chain-minimal-node/src/network.rs index a5237f5ea65..2381c68f170 100644 --- a/client/relay-chain-minimal-node/src/network.rs +++ b/client/relay-chain-minimal-node/src/network.rs @@ -20,14 +20,21 @@ use polkadot_service::{BlockT, NumberFor}; use polkadot_node_network_protocol::PeerId; use sc_network::{NetworkService, SyncState}; -use sc_network_common::sync::{Metrics, SyncStatus}; +use sc_client_api::HeaderBackend; +use sc_network_common::{ + config::{ + NonDefaultSetConfig, NonReservedPeerMode, NotificationHandshake, ProtocolId, SetConfig, + }, + protocol::role::Roles, + sync::{message::BlockAnnouncesHandshake, Metrics, SyncStatus}, +}; use sc_network_light::light_client_requests; use sc_network_sync::{block_request_handler, state_request_handler}; use sc_service::{error::Error, Configuration, NetworkStarter, SpawnTaskHandle}; use sp_consensus::BlockOrigin; use sp_runtime::Justifications; -use std::sync::Arc; +use std::{iter, sync::Arc}; use crate::BlockChainRpcClient; @@ -59,6 +66,16 @@ pub(crate) fn build_collator_network( let light_client_request_protocol_config = light_client_requests::generate_protocol_config(&protocol_id, genesis_hash, None); + let chain_sync = DummyChainSync; + let block_announce_config = chain_sync.get_block_announce_proto_config( + &protocol_id.clone(), + None, + Roles::from(&config.role), + client.info().best_number, + client.info().best_hash, + genesis_hash, + ); + let network_params = sc_network::config::Params { role: config.role.clone(), executor: { @@ -68,12 +85,13 @@ pub(crate) fn build_collator_network( })) }, fork_id: None, - chain_sync: Box::new(DummyChainSync), + chain_sync: Box::new(chain_sync), network_config: config.network.clone(), chain: client.clone(), import_queue: Box::new(DummyImportQueue), protocol_id, metrics_registry: config.prometheus_config.as_ref().map(|config| config.registry.clone()), + // block_announce_config, block_request_protocol_config, state_request_protocol_config, warp_sync_protocol_config: None, @@ -116,6 +134,54 @@ pub(crate) fn build_collator_network( /// we provide a noop implementation. struct DummyChainSync; +impl DummyChainSync { + pub fn get_block_announce_proto_config( + &self, + protocol_id: ProtocolId, + fork_id: &Option, + roles: Roles, + best_number: NumberFor, + best_hash: B::Hash, + genesis_hash: B::Hash, + ) -> NonDefaultSetConfig { + let block_announces_protocol = { + let genesis_hash = genesis_hash.as_ref(); + if let Some(ref fork_id) = fork_id { + format!( + "/{}/{}/block-announces/1", + array_bytes::bytes2hex("", genesis_hash), + fork_id + ) + } else { + format!("/{}/block-announces/1", array_bytes::bytes2hex("", genesis_hash)) + } + }; + + NonDefaultSetConfig { + notifications_protocol: block_announces_protocol.into(), + fallback_names: iter::once( + format!("/{}/block-announces/1", protocol_id.as_ref()).into(), + ) + .collect(), + max_notification_size: 1024 * 1024, + handshake: Some(NotificationHandshake::new(BlockAnnouncesHandshake::::build( + roles, + best_number, + best_hash, + genesis_hash, + ))), + // NOTE: `set_config` will be ignored by `protocol.rs` as the block announcement + // protocol is still hardcoded into the peerset. + set_config: SetConfig { + in_peers: 0, + out_peers: 0, + reserved_nodes: Vec::new(), + non_reserved_mode: NonReservedPeerMode::Deny, + }, + } + } +} + impl sc_network_common::sync::ChainSync for DummyChainSync { fn peer_info(&self, _who: &PeerId) -> Option> { None