Change validation & collation protocol names to include genesis hash & fork id (#5876)

This commit is contained in:
Dmitry Markin
2022-08-30 19:50:22 +03:00
committed by GitHub
parent 6e04112b93
commit 13ea167bd7
19 changed files with 739 additions and 177 deletions
+2 -1
View File
@@ -28,7 +28,8 @@ use parking_lot::Mutex;
use sp_consensus::SyncOracle;
use polkadot_node_network_protocol::{
peer_set::PeerSet, PeerId, ProtocolVersion, UnifiedReputationChange as Rep, View,
peer_set::{PeerSet, ProtocolVersion},
PeerId, UnifiedReputationChange as Rep, View,
};
/// Peer set info for network initialization.
+2 -2
View File
@@ -23,7 +23,7 @@ pub struct Metrics(pub(crate) Option<MetricsInner>);
fn peer_set_label(peer_set: PeerSet, version: ProtocolVersion) -> &'static str {
// Higher level code is meant to protect against this ever happening.
peer_set.get_protocol_name_static(version).unwrap_or("<internal error>")
peer_set.get_protocol_label(version).unwrap_or("<internal error>")
}
#[allow(missing_docs)]
@@ -98,7 +98,7 @@ impl Metrics {
self.0.as_ref().map(|metrics| {
metrics
.desired_peer_count
.with_label_values(&[peer_set.get_default_protocol_name()])
.with_label_values(&[peer_set.get_label()])
.set(size as u64)
});
}
+15 -17
View File
@@ -31,9 +31,9 @@ use sc_network_common::{
};
use polkadot_node_network_protocol::{
peer_set::PeerSet,
peer_set::{PeerSet, PeerSetProtocolNames, ProtocolVersion},
request_response::{OutgoingRequest, Recipient, ReqProtocolNames, Requests},
PeerId, ProtocolVersion, UnifiedReputationChange as Rep,
PeerId, UnifiedReputationChange as Rep,
};
use polkadot_primitives::v2::{AuthorityDiscoveryId, Block, Hash};
@@ -52,6 +52,7 @@ pub(crate) fn send_message<M>(
mut peers: Vec<PeerId>,
peer_set: PeerSet,
version: ProtocolVersion,
protocol_names: &PeerSetProtocolNames,
message: M,
metrics: &super::Metrics,
) where
@@ -67,11 +68,13 @@ pub(crate) fn send_message<M>(
// list. The message payload can be quite large. If the underlying
// network used `Bytes` this would not be necessary.
let last_peer = peers.pop();
// optimization: generate the protocol name once.
let protocol_name = protocol_names.get_name(peer_set, version);
peers.into_iter().for_each(|peer| {
net.write_notification(peer, peer_set, message.clone());
net.write_notification(peer, protocol_name.clone(), message.clone());
});
if let Some(peer) = last_peer {
net.write_notification(peer, peer_set, message);
net.write_notification(peer, protocol_name, message);
}
}
@@ -108,11 +111,11 @@ pub trait Network: Clone + Send + 'static {
/// Report a given peer as either beneficial (+) or costly (-) according to the given scalar.
fn report_peer(&self, who: PeerId, cost_benefit: Rep);
/// Disconnect a given peer from the peer set specified without harming reputation.
fn disconnect_peer(&self, who: PeerId, peer_set: PeerSet);
/// Disconnect a given peer from the protocol specified without harming reputation.
fn disconnect_peer(&self, who: PeerId, protocol: Cow<'static, str>);
/// Write a notification to a peer on the given peer-set's protocol.
fn write_notification(&self, who: PeerId, peer_set: PeerSet, message: Vec<u8>);
/// Write a notification to a peer on the given protocol.
fn write_notification(&self, who: PeerId, protocol: Cow<'static, str>, message: Vec<u8>);
}
#[async_trait]
@@ -137,17 +140,12 @@ impl Network for Arc<NetworkService<Block, Hash>> {
NetworkService::report_peer(&**self, who, cost_benefit.into_base_rep());
}
fn disconnect_peer(&self, who: PeerId, peer_set: PeerSet) {
NetworkService::disconnect_peer(&**self, who, peer_set.into_default_protocol_name());
fn disconnect_peer(&self, who: PeerId, protocol: Cow<'static, str>) {
NetworkService::disconnect_peer(&**self, who, protocol);
}
fn write_notification(&self, who: PeerId, peer_set: PeerSet, message: Vec<u8>) {
NetworkService::write_notification(
&**self,
who,
peer_set.into_default_protocol_name(),
message,
);
fn write_notification(&self, who: PeerId, protocol: Cow<'static, str>, message: Vec<u8>) {
NetworkService::write_notification(&**self, who, protocol, message);
}
async fn start_request<AD: AuthorityDiscovery>(
+73 -21
View File
@@ -27,9 +27,11 @@ use sp_consensus::SyncOracle;
use polkadot_node_network_protocol::{
self as net_protocol,
peer_set::{PeerSet, PerPeerSet},
v1 as protocol_v1, ObservedRole, OurView, PeerId, ProtocolVersion,
UnifiedReputationChange as Rep, View,
peer_set::{
CollationVersion, PeerSet, PeerSetProtocolNames, PerPeerSet, ProtocolVersion,
ValidationVersion,
},
v1 as protocol_v1, ObservedRole, OurView, PeerId, UnifiedReputationChange as Rep, View,
};
use polkadot_node_subsystem::{
@@ -80,6 +82,7 @@ pub struct NetworkBridgeRx<N, AD> {
sync_oracle: Box<dyn SyncOracle + Send>,
shared: Shared,
metrics: Metrics,
peerset_protocol_names: PeerSetProtocolNames,
}
impl<N, AD> NetworkBridgeRx<N, AD> {
@@ -92,9 +95,17 @@ impl<N, AD> NetworkBridgeRx<N, AD> {
authority_discovery_service: AD,
sync_oracle: Box<dyn SyncOracle + Send>,
metrics: Metrics,
peerset_protocol_names: PeerSetProtocolNames,
) -> Self {
let shared = Shared::default();
Self { network_service, authority_discovery_service, sync_oracle, shared, metrics }
Self {
network_service,
authority_discovery_service,
sync_oracle,
shared,
metrics,
peerset_protocol_names,
}
}
}
@@ -147,6 +158,7 @@ async fn handle_network_messages<AD>(
mut authority_discovery_service: AD,
metrics: Metrics,
shared: Shared,
peerset_protocol_names: PeerSetProtocolNames,
) -> Result<(), Error>
where
AD: validator_discovery::AuthorityDiscovery + Send,
@@ -166,13 +178,14 @@ where
}) => {
let role = ObservedRole::from(role);
let (peer_set, version) = {
let (peer_set, version) = match PeerSet::try_from_protocol_name(&protocol) {
None => continue,
Some(p) => p,
};
let (peer_set, version) =
match peerset_protocol_names.try_get_protocol(&protocol) {
None => continue,
Some(p) => p,
};
if let Some(fallback) = negotiated_fallback {
match PeerSet::try_from_protocol_name(&fallback) {
match peerset_protocol_names.try_get_protocol(&fallback) {
None => {
gum::debug!(
target: LOG_TARGET,
@@ -210,7 +223,7 @@ where
target: LOG_TARGET,
action = "PeerConnected",
peer_set = ?peer_set,
version,
version = %version,
peer = ?peer,
role = ?role
);
@@ -245,7 +258,7 @@ where
NetworkBridgeEvent::PeerConnected(
peer.clone(),
role,
1,
version,
maybe_authority,
),
NetworkBridgeEvent::PeerViewChange(peer.clone(), View::default()),
@@ -259,6 +272,7 @@ where
vec![peer],
PeerSet::Validation,
version,
&peerset_protocol_names,
WireMessage::<protocol_v1::ValidationProtocol>::ViewUpdate(local_view),
&metrics,
);
@@ -269,7 +283,7 @@ where
NetworkBridgeEvent::PeerConnected(
peer.clone(),
role,
1,
version,
maybe_authority,
),
NetworkBridgeEvent::PeerViewChange(peer.clone(), View::default()),
@@ -283,6 +297,7 @@ where
vec![peer],
PeerSet::Collation,
version,
&peerset_protocol_names,
WireMessage::<protocol_v1::CollationProtocol>::ViewUpdate(local_view),
&metrics,
);
@@ -290,7 +305,7 @@ where
}
},
Some(NetworkEvent::NotificationStreamClosed { remote: peer, protocol }) => {
let (peer_set, version) = match PeerSet::try_from_protocol_name(&protocol) {
let (peer_set, version) = match peerset_protocol_names.try_get_protocol(&protocol) {
None => continue,
Some(peer_set) => peer_set,
};
@@ -317,7 +332,7 @@ where
w
};
if was_connected && version == peer_set.get_default_version() {
if was_connected && version == peer_set.get_main_version() {
match peer_set {
PeerSet::Validation =>
dispatch_validation_event_to_all(
@@ -355,7 +370,8 @@ where
.filter_map(|(protocol, msg_bytes)| {
// version doesn't matter because we always receive on the 'correct'
// protocol name, not the negotiated fallback.
let (peer_set, _version) = PeerSet::try_from_protocol_name(protocol)?;
let (peer_set, _version) =
peerset_protocol_names.try_get_protocol(protocol)?;
if peer_set == PeerSet::Validation {
if expected_versions[PeerSet::Validation].is_none() {
return Some(Err(UNCONNECTED_PEERSET_COST))
@@ -384,7 +400,8 @@ where
.filter_map(|(protocol, msg_bytes)| {
// version doesn't matter because we always receive on the 'correct'
// protocol name, not the negotiated fallback.
let (peer_set, _version) = PeerSet::try_from_protocol_name(protocol)?;
let (peer_set, _version) =
peerset_protocol_names.try_get_protocol(protocol)?;
if peer_set == PeerSet::Collation {
if expected_versions[PeerSet::Collation].is_none() {
@@ -422,7 +439,9 @@ where
if !v_messages.is_empty() {
let (events, reports) =
if expected_versions[PeerSet::Validation] == Some(1) {
if expected_versions[PeerSet::Validation] ==
Some(ValidationVersion::V1.into())
{
handle_v1_peer_messages::<protocol_v1::ValidationProtocol, _>(
remote.clone(),
PeerSet::Validation,
@@ -453,7 +472,9 @@ where
if !c_messages.is_empty() {
let (events, reports) =
if expected_versions[PeerSet::Collation] == Some(1) {
if expected_versions[PeerSet::Collation] ==
Some(CollationVersion::V1.into())
{
handle_v1_peer_messages::<protocol_v1::CollationProtocol, _>(
remote.clone(),
PeerSet::Collation,
@@ -494,6 +515,7 @@ async fn run_incoming_orchestra_signals<Context, N, AD>(
shared: Shared,
sync_oracle: Box<dyn SyncOracle + Send>,
metrics: Metrics,
peerset_protocol_names: PeerSetProtocolNames,
) -> Result<(), Error>
where
N: Network,
@@ -574,6 +596,7 @@ where
&shared,
finalized_number,
&metrics,
&peerset_protocol_names,
);
}
}
@@ -619,6 +642,7 @@ where
metrics,
sync_oracle,
shared,
peerset_protocol_names,
} = bridge;
let (task, network_event_handler) = handle_network_messages(
@@ -628,6 +652,7 @@ where
authority_discovery_service.clone(),
metrics.clone(),
shared.clone(),
peerset_protocol_names.clone(),
)
.remote_handle();
@@ -641,6 +666,7 @@ where
shared,
sync_oracle,
metrics,
peerset_protocol_names,
);
futures::pin_mut!(orchestra_signal_handler);
@@ -667,6 +693,7 @@ fn update_our_view<Net, Context>(
shared: &Shared,
finalized_number: BlockNumber,
metrics: &Metrics,
peerset_protocol_names: &PeerSetProtocolNames,
) where
Net: Network,
{
@@ -700,11 +727,18 @@ fn update_our_view<Net, Context>(
send_validation_message_v1(
net,
validation_peers,
peerset_protocol_names,
WireMessage::ViewUpdate(new_view.clone()),
metrics,
);
send_collation_message_v1(net, collation_peers, WireMessage::ViewUpdate(new_view), metrics);
send_collation_message_v1(
net,
collation_peers,
peerset_protocol_names,
WireMessage::ViewUpdate(new_view),
metrics,
);
let our_view = OurView::new(
live_heads.iter().take(MAX_VIEW_HEADS).cloned().map(|a| (a.hash, a.span)),
@@ -778,19 +812,37 @@ fn handle_v1_peer_messages<RawMessage: Decode, OutMessage: From<RawMessage>>(
fn send_validation_message_v1(
net: &mut impl Network,
peers: Vec<PeerId>,
peerset_protocol_names: &PeerSetProtocolNames,
message: WireMessage<protocol_v1::ValidationProtocol>,
metrics: &Metrics,
) {
send_message(net, peers, PeerSet::Validation, 1, message, metrics);
send_message(
net,
peers,
PeerSet::Validation,
ValidationVersion::V1.into(),
peerset_protocol_names,
message,
metrics,
);
}
fn send_collation_message_v1(
net: &mut impl Network,
peers: Vec<PeerId>,
peerset_protocol_names: &PeerSetProtocolNames,
message: WireMessage<protocol_v1::CollationProtocol>,
metrics: &Metrics,
) {
send_message(net, peers, PeerSet::Collation, 1, message, metrics)
send_message(
net,
peers,
PeerSet::Collation,
CollationVersion::V1.into(),
peerset_protocol_names,
message,
metrics,
);
}
async fn dispatch_validation_event_to_all(
+74 -17
View File
@@ -31,6 +31,7 @@ use std::{
use sc_network::{Event as NetworkEvent, IfDisconnected};
use polkadot_node_network_protocol::{
peer_set::PeerSetProtocolNames,
request_response::{outgoing::Requests, ReqProtocolNames},
view, ObservedRole, Versioned,
};
@@ -46,7 +47,7 @@ use polkadot_node_subsystem_test_helpers::{
SingleItemSink, SingleItemStream, TestSubsystemContextHandle,
};
use polkadot_node_subsystem_util::metered;
use polkadot_primitives::v2::AuthorityDiscoveryId;
use polkadot_primitives::v2::{AuthorityDiscoveryId, Hash};
use sc_network::Multiaddr;
use sp_keyring::Sr25519Keyring;
@@ -68,6 +69,7 @@ pub enum NetworkAction {
struct TestNetwork {
net_events: Arc<Mutex<Option<SingleItemStream<NetworkEvent>>>>,
action_tx: Arc<Mutex<metered::UnboundedMeteredSender<NetworkAction>>>,
protocol_names: Arc<PeerSetProtocolNames>,
}
#[derive(Clone, Debug)]
@@ -78,9 +80,12 @@ struct TestAuthorityDiscovery;
struct TestNetworkHandle {
action_rx: metered::UnboundedMeteredReceiver<NetworkAction>,
net_tx: SingleItemSink<NetworkEvent>,
protocol_names: PeerSetProtocolNames,
}
fn new_test_network() -> (TestNetwork, TestNetworkHandle, TestAuthorityDiscovery) {
fn new_test_network(
protocol_names: PeerSetProtocolNames,
) -> (TestNetwork, TestNetworkHandle, TestAuthorityDiscovery) {
let (net_tx, net_rx) = polkadot_node_subsystem_test_helpers::single_item_sink();
let (action_tx, action_rx) = metered::unbounded();
@@ -88,8 +93,9 @@ fn new_test_network() -> (TestNetwork, TestNetworkHandle, TestAuthorityDiscovery
TestNetwork {
net_events: Arc::new(Mutex::new(Some(net_rx))),
action_tx: Arc::new(Mutex::new(action_tx)),
protocol_names: Arc::new(protocol_names.clone()),
},
TestNetworkHandle { action_rx, net_tx },
TestNetworkHandle { action_rx, net_tx, protocol_names },
TestAuthorityDiscovery,
)
}
@@ -130,14 +136,20 @@ impl Network for TestNetwork {
.unwrap();
}
fn disconnect_peer(&self, who: PeerId, peer_set: PeerSet) {
fn disconnect_peer(&self, who: PeerId, protocol: Cow<'static, str>) {
let (peer_set, version) = self.protocol_names.try_get_protocol(&protocol).unwrap();
assert_eq!(version, peer_set.get_main_version());
self.action_tx
.lock()
.unbounded_send(NetworkAction::DisconnectPeer(who, peer_set))
.unwrap();
}
fn write_notification(&self, who: PeerId, peer_set: PeerSet, message: Vec<u8>) {
fn write_notification(&self, who: PeerId, protocol: Cow<'static, str>, message: Vec<u8>) {
let (peer_set, version) = self.protocol_names.try_get_protocol(&protocol).unwrap();
assert_eq!(version, peer_set.get_main_version());
self.action_tx
.lock()
.unbounded_send(NetworkAction::WriteNotification(who, peer_set, message))
@@ -181,7 +193,7 @@ impl TestNetworkHandle {
async fn connect_peer(&mut self, peer: PeerId, peer_set: PeerSet, role: ObservedRole) {
self.send_network_event(NetworkEvent::NotificationStreamOpened {
remote: peer,
protocol: peer_set.into_default_protocol_name(),
protocol: self.protocol_names.get_main_name(peer_set),
negotiated_fallback: None,
role: role.into(),
})
@@ -191,7 +203,7 @@ impl TestNetworkHandle {
async fn disconnect_peer(&mut self, peer: PeerId, peer_set: PeerSet) {
self.send_network_event(NetworkEvent::NotificationStreamClosed {
remote: peer,
protocol: peer_set.into_default_protocol_name(),
protocol: self.protocol_names.get_main_name(peer_set),
})
.await;
}
@@ -199,7 +211,7 @@ impl TestNetworkHandle {
async fn peer_message(&mut self, peer: PeerId, peer_set: PeerSet, message: Vec<u8>) {
self.send_network_event(NetworkEvent::NotificationsReceived {
remote: peer,
messages: vec![(peer_set.into_default_protocol_name(), message.into())],
messages: vec![(self.protocol_names.get_main_name(peer_set), message.into())],
})
.await;
}
@@ -285,8 +297,12 @@ fn test_harness<T: Future<Output = VirtualOverseer>>(
sync_oracle: Box<dyn SyncOracle + Send>,
test: impl FnOnce(TestHarness) -> T,
) {
let genesis_hash = Hash::repeat_byte(0xff);
let fork_id = None;
let peerset_protocol_names = PeerSetProtocolNames::new(genesis_hash, fork_id);
let pool = sp_core::testing::TaskExecutor::new();
let (mut network, network_handle, discovery) = new_test_network();
let (mut network, network_handle, discovery) = new_test_network(peerset_protocol_names.clone());
let (context, virtual_overseer) =
polkadot_node_subsystem_test_helpers::make_subsystem_context(pool);
let network_stream = network.event_stream();
@@ -297,6 +313,7 @@ fn test_harness<T: Future<Output = VirtualOverseer>>(
metrics: Metrics(None),
sync_oracle,
shared: Shared::default(),
peerset_protocol_names,
};
let network_bridge = run_network_in(bridge, context, network_stream)
@@ -656,7 +673,12 @@ fn peer_view_updates_sent_via_overseer() {
// bridge will inform about all connected peers.
{
assert_sends_validation_event_to_all(
NetworkBridgeEvent::PeerConnected(peer.clone(), ObservedRole::Full, 1, None),
NetworkBridgeEvent::PeerConnected(
peer.clone(),
ObservedRole::Full,
ValidationVersion::V1.into(),
None,
),
&mut virtual_overseer,
)
.await;
@@ -699,7 +721,12 @@ fn peer_messages_sent_via_overseer() {
// bridge will inform about all connected peers.
{
assert_sends_validation_event_to_all(
NetworkBridgeEvent::PeerConnected(peer.clone(), ObservedRole::Full, 1, None),
NetworkBridgeEvent::PeerConnected(
peer.clone(),
ObservedRole::Full,
ValidationVersion::V1.into(),
None,
),
&mut virtual_overseer,
)
.await;
@@ -769,7 +796,12 @@ fn peer_disconnect_from_just_one_peerset() {
// bridge will inform about all connected peers.
{
assert_sends_validation_event_to_all(
NetworkBridgeEvent::PeerConnected(peer.clone(), ObservedRole::Full, 1, None),
NetworkBridgeEvent::PeerConnected(
peer.clone(),
ObservedRole::Full,
ValidationVersion::V1.into(),
None,
),
&mut virtual_overseer,
)
.await;
@@ -783,7 +815,12 @@ fn peer_disconnect_from_just_one_peerset() {
{
assert_sends_collation_event_to_all(
NetworkBridgeEvent::PeerConnected(peer.clone(), ObservedRole::Full, 1, None),
NetworkBridgeEvent::PeerConnected(
peer.clone(),
ObservedRole::Full,
ValidationVersion::V1.into(),
None,
),
&mut virtual_overseer,
)
.await;
@@ -852,7 +889,12 @@ fn relays_collation_protocol_messages() {
// bridge will inform about all connected peers.
{
assert_sends_validation_event_to_all(
NetworkBridgeEvent::PeerConnected(peer_a.clone(), ObservedRole::Full, 1, None),
NetworkBridgeEvent::PeerConnected(
peer_a.clone(),
ObservedRole::Full,
ValidationVersion::V1.into(),
None,
),
&mut virtual_overseer,
)
.await;
@@ -866,7 +908,12 @@ fn relays_collation_protocol_messages() {
{
assert_sends_collation_event_to_all(
NetworkBridgeEvent::PeerConnected(peer_b.clone(), ObservedRole::Full, 1, None),
NetworkBridgeEvent::PeerConnected(
peer_b.clone(),
ObservedRole::Full,
ValidationVersion::V1.into(),
None,
),
&mut virtual_overseer,
)
.await;
@@ -945,7 +992,12 @@ fn different_views_on_different_peer_sets() {
// bridge will inform about all connected peers.
{
assert_sends_validation_event_to_all(
NetworkBridgeEvent::PeerConnected(peer.clone(), ObservedRole::Full, 1, None),
NetworkBridgeEvent::PeerConnected(
peer.clone(),
ObservedRole::Full,
ValidationVersion::V1.into(),
None,
),
&mut virtual_overseer,
)
.await;
@@ -959,7 +1011,12 @@ fn different_views_on_different_peer_sets() {
{
assert_sends_collation_event_to_all(
NetworkBridgeEvent::PeerConnected(peer.clone(), ObservedRole::Full, 1, None),
NetworkBridgeEvent::PeerConnected(
peer.clone(),
ObservedRole::Full,
ValidationVersion::V1.into(),
None,
),
&mut virtual_overseer,
)
.await;
+46 -6
View File
@@ -18,7 +18,9 @@
use super::*;
use polkadot_node_network_protocol::{
peer_set::PeerSet, request_response::ReqProtocolNames, v1 as protocol_v1, PeerId, Versioned,
peer_set::{CollationVersion, PeerSet, PeerSetProtocolNames, ValidationVersion},
request_response::ReqProtocolNames,
v1 as protocol_v1, PeerId, Versioned,
};
use polkadot_node_subsystem::{
@@ -53,6 +55,7 @@ pub struct NetworkBridgeTx<N, AD> {
authority_discovery_service: AD,
metrics: Metrics,
req_protocol_names: ReqProtocolNames,
peerset_protocol_names: PeerSetProtocolNames,
}
impl<N, AD> NetworkBridgeTx<N, AD> {
@@ -65,8 +68,15 @@ impl<N, AD> NetworkBridgeTx<N, AD> {
authority_discovery_service: AD,
metrics: Metrics,
req_protocol_names: ReqProtocolNames,
peerset_protocol_names: PeerSetProtocolNames,
) -> Self {
Self { network_service, authority_discovery_service, metrics, req_protocol_names }
Self {
network_service,
authority_discovery_service,
metrics,
req_protocol_names,
peerset_protocol_names,
}
}
}
@@ -91,12 +101,14 @@ async fn handle_subsystem_messages<Context, N, AD>(
mut authority_discovery_service: AD,
metrics: Metrics,
req_protocol_names: ReqProtocolNames,
peerset_protocol_names: PeerSetProtocolNames,
) -> Result<(), Error>
where
N: Network,
AD: validator_discovery::AuthorityDiscovery + Clone,
{
let mut validator_discovery = validator_discovery::Service::<N, AD>::new();
let mut validator_discovery =
validator_discovery::Service::<N, AD>::new(peerset_protocol_names.clone());
loop {
match ctx.recv().fuse().await? {
@@ -112,6 +124,7 @@ where
msg,
&metrics,
&req_protocol_names,
&peerset_protocol_names,
)
.await;
},
@@ -128,6 +141,7 @@ async fn handle_incoming_subsystem_communication<Context, N, AD>(
msg: NetworkBridgeTxMessage,
metrics: &Metrics,
req_protocol_names: &ReqProtocolNames,
peerset_protocol_names: &PeerSetProtocolNames,
) -> (N, AD)
where
N: Network,
@@ -150,7 +164,9 @@ where
peer_set = ?peer_set,
);
network_service.disconnect_peer(peer, peer_set);
// [`NetworkService`] keeps track of the protocols by their main name.
let protocol = peerset_protocol_names.get_main_name(peer_set);
network_service.disconnect_peer(peer, protocol);
},
NetworkBridgeTxMessage::SendValidationMessage(peers, msg) => {
gum::trace!(
@@ -163,6 +179,7 @@ where
Versioned::V1(msg) => send_validation_message_v1(
&mut network_service,
peers,
peerset_protocol_names,
WireMessage::ProtocolMessage(msg),
&metrics,
),
@@ -180,6 +197,7 @@ where
Versioned::V1(msg) => send_validation_message_v1(
&mut network_service,
peers,
peerset_protocol_names,
WireMessage::ProtocolMessage(msg),
&metrics,
),
@@ -197,6 +215,7 @@ where
Versioned::V1(msg) => send_collation_message_v1(
&mut network_service,
peers,
peerset_protocol_names,
WireMessage::ProtocolMessage(msg),
&metrics,
),
@@ -214,6 +233,7 @@ where
Versioned::V1(msg) => send_collation_message_v1(
&mut network_service,
peers,
peerset_protocol_names,
WireMessage::ProtocolMessage(msg),
&metrics,
),
@@ -296,6 +316,7 @@ where
authority_discovery_service,
metrics,
req_protocol_names,
peerset_protocol_names,
} = bridge;
handle_subsystem_messages(
@@ -304,6 +325,7 @@ where
authority_discovery_service,
metrics,
req_protocol_names,
peerset_protocol_names,
)
.await?;
@@ -313,17 +335,35 @@ where
fn send_validation_message_v1(
net: &mut impl Network,
peers: Vec<PeerId>,
protocol_names: &PeerSetProtocolNames,
message: WireMessage<protocol_v1::ValidationProtocol>,
metrics: &Metrics,
) {
send_message(net, peers, PeerSet::Validation, 1, message, metrics);
send_message(
net,
peers,
PeerSet::Validation,
ValidationVersion::V1.into(),
protocol_names,
message,
metrics,
);
}
fn send_collation_message_v1(
net: &mut impl Network,
peers: Vec<PeerId>,
protocol_names: &PeerSetProtocolNames,
message: WireMessage<protocol_v1::CollationProtocol>,
metrics: &Metrics,
) {
send_message(net, peers, PeerSet::Collation, 1, message, metrics)
send_message(
net,
peers,
PeerSet::Collation,
CollationVersion::V1.into(),
protocol_names,
message,
metrics,
);
}
+30 -10
View File
@@ -25,6 +25,7 @@ use std::{borrow::Cow, collections::HashSet};
use sc_network::{Event as NetworkEvent, IfDisconnected};
use polkadot_node_network_protocol::{
peer_set::PeerSetProtocolNames,
request_response::{outgoing::Requests, ReqProtocolNames},
ObservedRole, Versioned,
};
@@ -55,6 +56,7 @@ pub enum NetworkAction {
struct TestNetwork {
net_events: Arc<Mutex<Option<metered::MeteredReceiver<NetworkEvent>>>>,
action_tx: Arc<Mutex<metered::UnboundedMeteredSender<NetworkAction>>>,
peerset_protocol_names: Arc<PeerSetProtocolNames>,
}
#[derive(Clone, Debug)]
@@ -65,9 +67,12 @@ struct TestAuthorityDiscovery;
struct TestNetworkHandle {
action_rx: metered::UnboundedMeteredReceiver<NetworkAction>,
net_tx: metered::MeteredSender<NetworkEvent>,
peerset_protocol_names: PeerSetProtocolNames,
}
fn new_test_network() -> (TestNetwork, TestNetworkHandle, TestAuthorityDiscovery) {
fn new_test_network(
peerset_protocol_names: PeerSetProtocolNames,
) -> (TestNetwork, TestNetworkHandle, TestAuthorityDiscovery) {
let (net_tx, net_rx) = metered::channel(10);
let (action_tx, action_rx) = metered::unbounded();
@@ -75,8 +80,9 @@ fn new_test_network() -> (TestNetwork, TestNetworkHandle, TestAuthorityDiscovery
TestNetwork {
net_events: Arc::new(Mutex::new(Some(net_rx))),
action_tx: Arc::new(Mutex::new(action_tx)),
peerset_protocol_names: Arc::new(peerset_protocol_names.clone()),
},
TestNetworkHandle { action_rx, net_tx },
TestNetworkHandle { action_rx, net_tx, peerset_protocol_names },
TestAuthorityDiscovery,
)
}
@@ -117,14 +123,20 @@ impl Network for TestNetwork {
.unwrap();
}
fn disconnect_peer(&self, who: PeerId, peer_set: PeerSet) {
fn disconnect_peer(&self, who: PeerId, protocol: Cow<'static, str>) {
let (peer_set, version) = self.peerset_protocol_names.try_get_protocol(&protocol).unwrap();
assert_eq!(version, peer_set.get_main_version());
self.action_tx
.lock()
.unbounded_send(NetworkAction::DisconnectPeer(who, peer_set))
.unwrap();
}
fn write_notification(&self, who: PeerId, peer_set: PeerSet, message: Vec<u8>) {
fn write_notification(&self, who: PeerId, protocol: Cow<'static, str>, message: Vec<u8>) {
let (peer_set, version) = self.peerset_protocol_names.try_get_protocol(&protocol).unwrap();
assert_eq!(version, peer_set.get_main_version());
self.action_tx
.lock()
.unbounded_send(NetworkAction::WriteNotification(who, peer_set, message))
@@ -158,7 +170,7 @@ impl TestNetworkHandle {
async fn connect_peer(&mut self, peer: PeerId, peer_set: PeerSet, role: ObservedRole) {
self.send_network_event(NetworkEvent::NotificationStreamOpened {
remote: peer,
protocol: peer_set.into_default_protocol_name(),
protocol: self.peerset_protocol_names.get_main_name(peer_set),
negotiated_fallback: None,
role: role.into(),
})
@@ -178,16 +190,24 @@ struct TestHarness {
}
fn test_harness<T: Future<Output = VirtualOverseer>>(test: impl FnOnce(TestHarness) -> T) {
let genesis_hash = Hash::repeat_byte(0xff);
let fork_id = None;
let req_protocol_names = ReqProtocolNames::new(genesis_hash, fork_id);
let peerset_protocol_names = PeerSetProtocolNames::new(genesis_hash, fork_id);
let pool = sp_core::testing::TaskExecutor::new();
let (network, network_handle, discovery) = new_test_network();
let (network, network_handle, discovery) = new_test_network(peerset_protocol_names.clone());
let (context, virtual_overseer) =
polkadot_node_subsystem_test_helpers::make_subsystem_context(pool);
let genesis_hash = Hash::repeat_byte(0xff);
let protocol_names = ReqProtocolNames::new(genesis_hash, None);
let bridge_out = NetworkBridgeTx::new(network, discovery, Metrics(None), protocol_names);
let bridge_out = NetworkBridgeTx::new(
network,
discovery,
Metrics(None),
req_protocol_names,
peerset_protocol_names,
);
let network_bridge_out_fut = run_network_out(bridge_out, context)
.map_err(|e| panic!("bridge-out subsystem execution failed {:?}", e))
@@ -27,7 +27,7 @@ use sc_network::multiaddr::{self, Multiaddr};
pub use polkadot_node_network_protocol::authority_discovery::AuthorityDiscovery;
use polkadot_node_network_protocol::{
peer_set::{PeerSet, PerPeerSet},
peer_set::{PeerSet, PeerSetProtocolNames, PerPeerSet},
PeerId,
};
use polkadot_primitives::v2::AuthorityDiscoveryId;
@@ -36,6 +36,7 @@ const LOG_TARGET: &str = "parachain::validator-discovery";
pub(super) struct Service<N, AD> {
state: PerPeerSet<StatePerPeerSet>,
peerset_protocol_names: PeerSetProtocolNames,
// PhantomData used to make the struct generic instead of having generic methods
_phantom: PhantomData<(N, AD)>,
}
@@ -46,8 +47,8 @@ struct StatePerPeerSet {
}
impl<N: Network, AD: AuthorityDiscovery> Service<N, AD> {
pub fn new() -> Self {
Self { state: Default::default(), _phantom: PhantomData }
pub fn new(peerset_protocol_names: PeerSetProtocolNames) -> Self {
Self { state: Default::default(), peerset_protocol_names, _phantom: PhantomData }
}
/// Connect to already resolved addresses.
@@ -76,20 +77,26 @@ impl<N: Network, AD: AuthorityDiscovery> Service<N, AD> {
// ask the network to connect to these nodes and not disconnect
// from them until removed from the set
//
// for peer-set management, the default should be used regardless of
// for peer-set management, the main protocol name should be used regardless of
// the negotiated version.
if let Err(e) = network_service
.set_reserved_peers(peer_set.into_default_protocol_name(), newly_requested)
.set_reserved_peers(
self.peerset_protocol_names.get_main_name(peer_set),
newly_requested,
)
.await
{
gum::warn!(target: LOG_TARGET, err = ?e, "AuthorityDiscoveryService returned an invalid multiaddress");
}
// the addresses are known to be valid
//
// for peer-set management, the default should be used regardless of
// for peer-set management, the main protocol name should be used regardless of
// the negotiated version.
let _ = network_service
.remove_from_peers_set(peer_set.into_default_protocol_name(), peers_to_remove)
.remove_from_peers_set(
self.peerset_protocol_names.get_main_name(peer_set),
peers_to_remove,
)
.await;
network_service
@@ -166,6 +173,7 @@ mod tests {
request_response::{outgoing::Requests, ReqProtocolNames},
PeerId,
};
use polkadot_primitives::v2::Hash;
use sc_network::{Event as NetworkEvent, IfDisconnected};
use sp_keyring::Sr25519Keyring;
use std::{
@@ -174,7 +182,11 @@ mod tests {
};
fn new_service() -> Service<TestNetwork, TestAuthorityDiscovery> {
Service::new()
let genesis_hash = Hash::repeat_byte(0xff);
let fork_id = None;
let protocol_names = PeerSetProtocolNames::new(genesis_hash, fork_id);
Service::new(protocol_names)
}
fn new_network() -> (TestNetwork, TestAuthorityDiscovery) {
@@ -248,11 +260,11 @@ mod tests {
panic!()
}
fn disconnect_peer(&self, _: PeerId, _: PeerSet) {
fn disconnect_peer(&self, _: PeerId, _: Cow<'static, str>) {
panic!()
}
fn write_notification(&self, _: PeerId, _: PeerSet, _: Vec<u8>) {
fn write_notification(&self, _: PeerId, _: Cow<'static, str>, _: Vec<u8>) {
panic!()
}
}