Companion for paritytech/substrate#12828 (#1939)

* Companion for paritytech/substrate#12764

* Remove `async-trait`

* Companion for paritytech/substrate#12828

* carg fmt

* Update client/relay-chain-minimal-node/src/network.rs

Co-authored-by: Bastian Köcher <git@kchr.de>

* update lockfile for {"polkadot", "substrate"}

---------

Co-authored-by: Bastian Köcher <git@kchr.de>
Co-authored-by: parity-processbot <>
This commit is contained in:
Aaro Altonen
2023-03-06 20:25:56 +02:00
committed by GitHub
parent 012d4a8b71
commit f8064eee4c
12 changed files with 372 additions and 541 deletions
@@ -56,6 +56,8 @@ pub(crate) struct CollatorOverseerGenArgs<'a> {
pub runtime_client: Arc<BlockChainRpcClient>,
/// Underlying network service implementation.
pub network_service: Arc<sc_network::NetworkService<Block, PHash>>,
/// Syncing oracle.
pub sync_oracle: Box<dyn sp_consensus::SyncOracle + Send>,
/// Underlying authority discovery service.
pub authority_discovery_service: AuthorityDiscoveryService,
/// Receiver for collation request protocol
@@ -79,6 +81,7 @@ fn build_overseer<'a>(
CollatorOverseerGenArgs {
runtime_client,
network_service,
sync_oracle,
authority_discovery_service,
collation_req_receiver,
available_data_req_receiver,
@@ -121,7 +124,7 @@ fn build_overseer<'a>(
.network_bridge_rx(NetworkBridgeRxSubsystem::new(
network_service.clone(),
authority_discovery_service.clone(),
Box::new(network_service.clone()),
sync_oracle,
network_bridge_metrics.clone(),
peer_set_protocol_names.clone(),
))
@@ -152,7 +152,7 @@ async fn new_minimal_relay_chain(
let (collation_req_receiver, available_data_req_receiver) =
build_request_response_protocol_receivers(&request_protocol_names, &mut config);
let (network, network_starter) =
let (network, network_starter, sync_oracle) =
network::build_collator_network(network::BuildCollatorNetworkParams {
config: &config,
client: relay_chain_rpc_client.clone(),
@@ -171,6 +171,7 @@ async fn new_minimal_relay_chain(
let overseer_args = CollatorOverseerGenArgs {
runtime_client: relay_chain_rpc_client.clone(),
network_service: network.clone(),
sync_oracle,
authority_discovery_service,
collation_req_receiver,
available_data_req_receiver,
@@ -17,21 +17,15 @@
use polkadot_core_primitives::{Block, Hash};
use polkadot_service::{BlockT, NumberFor};
use polkadot_node_network_protocol::PeerId;
use sc_network::{NetworkService, SyncState};
use sc_network::NetworkService;
use sc_client_api::HeaderBackend;
use sc_consensus::{BlockImportError, BlockImportStatus, JustificationSyncLink, Link};
use sc_network_common::{
config::{
NonDefaultSetConfig, NonReservedPeerMode, NotificationHandshake, ProtocolId, SetConfig,
},
protocol::role::Roles,
service::NetworkSyncForkRequest,
sync::{
message::{BlockAnnouncesHandshake, BlockRequest},
BadPeer, Metrics, OnBlockData, PollBlockAnnounceValidation, SyncStatus,
},
sync::message::BlockAnnouncesHandshake,
};
use sc_service::{error::Error, Configuration, NetworkStarter, SpawnTaskHandle};
@@ -53,12 +47,14 @@ pub(crate) struct BuildCollatorNetworkParams<'a> {
/// Build the network service, the network status sinks and an RPC sender.
pub(crate) fn build_collator_network(
params: BuildCollatorNetworkParams,
) -> Result<(Arc<NetworkService<Block, Hash>>, NetworkStarter), Error> {
) -> Result<
(Arc<NetworkService<Block, Hash>>, NetworkStarter, Box<dyn sp_consensus::SyncOracle + Send>),
Error,
> {
let BuildCollatorNetworkParams { config, client, spawn_handle, genesis_hash } = params;
let protocol_id = config.protocol_id();
let chain_sync = DummyChainSync;
let block_announce_config = chain_sync.get_block_announce_proto_config::<Block>(
let block_announce_config = get_block_announce_proto_config::<Block>(
protocol_id.clone(),
&None,
Roles::from(&config.role),
@@ -76,13 +72,11 @@ pub(crate) fn build_collator_network(
})
},
fork_id: None,
chain_sync: Box::new(chain_sync),
network_config: config.network.clone(),
chain: client.clone(),
protocol_id,
metrics_registry: config.prometheus_config.as_ref().map(|config| config.registry.clone()),
block_announce_config,
chain_sync_service: Box::new(DummyChainSyncService::<Block>(Default::default())),
request_response_protocol_configs: Vec::new(),
};
@@ -113,246 +107,56 @@ pub(crate) fn build_collator_network(
let network_starter = NetworkStarter::new(network_start_tx);
Ok((network_service, network_starter))
Ok((network_service, network_starter, Box::new(SyncOracle {})))
}
/// Empty ChainSync shell. Syncing code is not necessary for
/// the minimal node, but network currently requires it. So
/// we provide a noop implementation.
struct DummyChainSync;
struct SyncOracle;
impl DummyChainSync {
pub fn get_block_announce_proto_config<B: BlockT>(
&self,
protocol_id: ProtocolId,
fork_id: &Option<String>,
roles: Roles,
best_number: NumberFor<B>,
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))
}
};
impl sp_consensus::SyncOracle for SyncOracle {
fn is_major_syncing(&self) -> bool {
false
}
NonDefaultSetConfig {
notifications_protocol: block_announces_protocol.into(),
fallback_names: iter::once(
format!("/{}/block-announces/1", protocol_id.as_ref()).into(),
)
fn is_offline(&self) -> bool {
true
}
}
fn get_block_announce_proto_config<B: BlockT>(
protocol_id: ProtocolId,
fork_id: &Option<String>,
roles: Roles,
best_number: NumberFor<B>,
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::<B>::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,
},
}
max_notification_size: 1024 * 1024,
handshake: Some(NotificationHandshake::new(BlockAnnouncesHandshake::<B>::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<B: BlockT> sc_network_common::sync::ChainSync<B> for DummyChainSync {
fn peer_info(&self, _who: &PeerId) -> Option<sc_network_common::sync::PeerInfo<B>> {
None
}
fn status(&self) -> sc_network_common::sync::SyncStatus<B> {
SyncStatus {
state: SyncState::Idle,
best_seen_block: None,
num_peers: 0,
queued_blocks: 0,
state_sync: None,
warp_sync: None,
}
}
fn num_sync_requests(&self) -> usize {
0
}
fn num_downloaded_blocks(&self) -> usize {
0
}
fn num_peers(&self) -> usize {
0
}
fn new_peer(
&mut self,
_who: PeerId,
_best_hash: <B as BlockT>::Hash,
_best_number: polkadot_service::NumberFor<B>,
) -> Result<
Option<sc_network_common::sync::message::BlockRequest<B>>,
sc_network_common::sync::BadPeer,
> {
Ok(None)
}
fn update_chain_info(
&mut self,
_best_hash: &<B as BlockT>::Hash,
_best_number: polkadot_service::NumberFor<B>,
) {
}
fn request_justification(
&mut self,
_hash: &<B as BlockT>::Hash,
_number: polkadot_service::NumberFor<B>,
) {
}
fn clear_justification_requests(&mut self) {}
fn set_sync_fork_request(
&mut self,
_peers: Vec<PeerId>,
_hash: &<B as BlockT>::Hash,
_number: polkadot_service::NumberFor<B>,
) {
}
fn on_block_data(
&mut self,
_who: &PeerId,
_request: Option<sc_network_common::sync::message::BlockRequest<B>>,
_response: sc_network_common::sync::message::BlockResponse<B>,
) -> Result<sc_network_common::sync::OnBlockData<B>, sc_network_common::sync::BadPeer> {
unimplemented!("Not supported on the RPC collator")
}
fn on_block_justification(
&mut self,
_who: PeerId,
_response: sc_network_common::sync::message::BlockResponse<B>,
) -> Result<sc_network_common::sync::OnBlockJustification<B>, sc_network_common::sync::BadPeer>
{
unimplemented!("Not supported on the RPC collator")
}
fn on_justification_import(
&mut self,
_hash: <B as BlockT>::Hash,
_number: polkadot_service::NumberFor<B>,
_success: bool,
) {
}
fn on_block_finalized(
&mut self,
_hash: &<B as BlockT>::Hash,
_number: polkadot_service::NumberFor<B>,
) {
}
fn push_block_announce_validation(
&mut self,
_who: PeerId,
_hash: <B as BlockT>::Hash,
_announce: sc_network_common::sync::message::BlockAnnounce<<B as BlockT>::Header>,
_is_best: bool,
) {
}
fn poll_block_announce_validation(
&mut self,
_cx: &mut std::task::Context,
) -> std::task::Poll<sc_network_common::sync::PollBlockAnnounceValidation<<B as BlockT>::Header>>
{
std::task::Poll::Pending
}
fn peer_disconnected(&mut self, _who: &PeerId) {}
fn metrics(&self) -> sc_network_common::sync::Metrics {
Metrics {
queued_blocks: 0,
fork_targets: 0,
justifications: sc_network_common::sync::metrics::Metrics {
pending_requests: 0,
active_requests: 0,
importing_requests: 0,
failed_requests: 0,
},
}
}
fn block_response_into_blocks(
&self,
_request: &sc_network_common::sync::message::BlockRequest<B>,
_response: sc_network_common::sync::OpaqueBlockResponse,
) -> Result<Vec<sc_network_common::sync::message::BlockData<B>>, String> {
unimplemented!("Not supported on the RPC collator")
}
fn poll(
&mut self,
_cx: &mut std::task::Context,
) -> std::task::Poll<PollBlockAnnounceValidation<B::Header>> {
std::task::Poll::Pending
}
fn send_block_request(&mut self, _who: PeerId, _request: BlockRequest<B>) {
unimplemented!("Not supported on the RPC collator")
}
fn num_active_peers(&self) -> usize {
0
}
fn process_block_response_data(&mut self, _blocks_to_import: Result<OnBlockData<B>, BadPeer>) {}
}
struct DummyChainSyncService<B>(std::marker::PhantomData<B>);
impl<B: BlockT> NetworkSyncForkRequest<B::Hash, NumberFor<B>> for DummyChainSyncService<B> {
fn set_sync_fork_request(&self, _peers: Vec<PeerId>, _hash: B::Hash, _number: NumberFor<B>) {}
}
impl<B: BlockT> JustificationSyncLink<B> for DummyChainSyncService<B> {
fn request_justification(&self, _hash: &B::Hash, _number: NumberFor<B>) {}
fn clear_justification_requests(&self) {}
}
impl<B: BlockT> Link<B> for DummyChainSyncService<B> {
fn blocks_processed(
&mut self,
_imported: usize,
_count: usize,
_results: Vec<(Result<BlockImportStatus<NumberFor<B>>, BlockImportError>, B::Hash)>,
) {
}
fn justification_imported(
&mut self,
_who: PeerId,
_hash: &B::Hash,
_number: NumberFor<B>,
_success: bool,
) {
}
fn request_justification(&mut self, _hash: &B::Hash, _number: NumberFor<B>) {}
}