Remove necessity to pass ConsensusEngineId when registering notifications protocol (#7549)

* Remove necessity to pass ConsensusEngineId when registering notifications protocol

* Line width

* Fix tests protocol name

* Other renames

* Doc update

* Change issue in TODO
This commit is contained in:
Pierre Krieger
2020-11-18 16:05:35 +01:00
committed by GitHub
parent 22a02d3e7a
commit 1eae9f5792
18 changed files with 228 additions and 284 deletions
@@ -33,7 +33,6 @@
//!
use crate::Event;
use super::maybe_utf8_bytes_to_string;
use futures::{prelude::*, channel::mpsc, ready, stream::FusedStream};
use parking_lot::Mutex;
@@ -228,23 +227,23 @@ impl Metrics {
.with_label_values(&["dht", "sent", name])
.inc_by(num);
}
Event::NotificationStreamOpened { engine_id, .. } => {
Event::NotificationStreamOpened { protocol, .. } => {
self.events_total
.with_label_values(&[&format!("notif-open-{:?}", engine_id), "sent", name])
.with_label_values(&[&format!("notif-open-{:?}", protocol), "sent", name])
.inc_by(num);
},
Event::NotificationStreamClosed { engine_id, .. } => {
Event::NotificationStreamClosed { protocol, .. } => {
self.events_total
.with_label_values(&[&format!("notif-closed-{:?}", engine_id), "sent", name])
.with_label_values(&[&format!("notif-closed-{:?}", protocol), "sent", name])
.inc_by(num);
},
Event::NotificationsReceived { messages, .. } => {
for (engine_id, message) in messages {
for (protocol, message) in messages {
self.events_total
.with_label_values(&[&format!("notif-{:?}", engine_id), "sent", name])
.with_label_values(&[&format!("notif-{:?}", protocol), "sent", name])
.inc_by(num);
self.notifications_sizes
.with_label_values(&[&maybe_utf8_bytes_to_string(engine_id), "sent", name])
.with_label_values(&[protocol, "sent", name])
.inc_by(num.saturating_mul(u64::try_from(message.len()).unwrap_or(u64::max_value())));
}
},
@@ -258,23 +257,23 @@ impl Metrics {
.with_label_values(&["dht", "received", name])
.inc();
}
Event::NotificationStreamOpened { engine_id, .. } => {
Event::NotificationStreamOpened { protocol, .. } => {
self.events_total
.with_label_values(&[&format!("notif-open-{:?}", engine_id), "received", name])
.with_label_values(&[&format!("notif-open-{:?}", protocol), "received", name])
.inc();
},
Event::NotificationStreamClosed { engine_id, .. } => {
Event::NotificationStreamClosed { protocol, .. } => {
self.events_total
.with_label_values(&[&format!("notif-closed-{:?}", engine_id), "received", name])
.with_label_values(&[&format!("notif-closed-{:?}", protocol), "received", name])
.inc();
},
Event::NotificationsReceived { messages, .. } => {
for (engine_id, message) in messages {
for (protocol, message) in messages {
self.events_total
.with_label_values(&[&format!("notif-{:?}", engine_id), "received", name])
.with_label_values(&[&format!("notif-{:?}", protocol), "received", name])
.inc();
self.notifications_sizes
.with_label_values(&[&maybe_utf8_bytes_to_string(engine_id), "received", name])
.with_label_values(&[&protocol, "received", name])
.inc_by(u64::try_from(message.len()).unwrap_or(u64::max_value()));
}
},
+23 -23
View File
@@ -21,7 +21,7 @@ use crate::{config, Event, NetworkService, NetworkWorker};
use libp2p::PeerId;
use futures::prelude::*;
use sp_runtime::traits::{Block as BlockT, Header as _};
use std::{sync::Arc, time::Duration};
use std::{borrow::Cow, sync::Arc, time::Duration};
use substrate_test_runtime_client::{TestClientBuilder, TestClientBuilderExt as _};
type TestNetworkService = NetworkService<
@@ -121,24 +121,24 @@ fn build_test_full_node(config: config::NetworkConfiguration)
(service, event_stream)
}
const ENGINE_ID: sp_runtime::ConsensusEngineId = *b"foo\0";
const PROTOCOL_NAME: Cow<'static, str> = Cow::Borrowed("/foo");
/// Builds two nodes and their associated events stream.
/// The nodes are connected together and have the `ENGINE_ID` protocol registered.
/// The nodes are connected together and have the `PROTOCOL_NAME` protocol registered.
fn build_nodes_one_proto()
-> (Arc<TestNetworkService>, impl Stream<Item = Event>, Arc<TestNetworkService>, impl Stream<Item = Event>)
{
let listen_addr = config::build_multiaddr![Memory(rand::random::<u64>())];
let (node1, events_stream1) = build_test_full_node(config::NetworkConfiguration {
notifications_protocols: vec![(ENGINE_ID, From::from("/foo"))],
notifications_protocols: vec![PROTOCOL_NAME],
listen_addresses: vec![listen_addr.clone()],
transport: config::TransportConfig::MemoryOnly,
.. config::NetworkConfiguration::new_local()
});
let (node2, events_stream2) = build_test_full_node(config::NetworkConfiguration {
notifications_protocols: vec![(ENGINE_ID, From::from("/foo"))],
notifications_protocols: vec![PROTOCOL_NAME],
listen_addresses: vec![],
reserved_nodes: vec![config::MultiaddrWithPeerId {
multiaddr: listen_addr,
@@ -161,10 +161,10 @@ fn notifications_state_consistent() {
// Write some initial notifications that shouldn't get through.
for _ in 0..(rand::random::<u8>() % 5) {
node1.write_notification(node2.local_peer_id().clone(), ENGINE_ID, b"hello world".to_vec());
node1.write_notification(node2.local_peer_id().clone(), PROTOCOL_NAME, b"hello world".to_vec());
}
for _ in 0..(rand::random::<u8>() % 5) {
node2.write_notification(node1.local_peer_id().clone(), ENGINE_ID, b"hello world".to_vec());
node2.write_notification(node1.local_peer_id().clone(), PROTOCOL_NAME, b"hello world".to_vec());
}
async_std::task::block_on(async move {
@@ -187,10 +187,10 @@ fn notifications_state_consistent() {
// Start by sending a notification from node1 to node2 and vice-versa. Part of the
// test consists in ensuring that notifications get ignored if the stream isn't open.
if rand::random::<u8>() % 5 >= 3 {
node1.write_notification(node2.local_peer_id().clone(), ENGINE_ID, b"hello world".to_vec());
node1.write_notification(node2.local_peer_id().clone(), PROTOCOL_NAME, b"hello world".to_vec());
}
if rand::random::<u8>() % 5 >= 3 {
node2.write_notification(node1.local_peer_id().clone(), ENGINE_ID, b"hello world".to_vec());
node2.write_notification(node1.local_peer_id().clone(), PROTOCOL_NAME, b"hello world".to_vec());
}
// Also randomly disconnect the two nodes from time to time.
@@ -219,31 +219,31 @@ fn notifications_state_consistent() {
};
match next_event {
future::Either::Left(Event::NotificationStreamOpened { remote, engine_id, .. }) => {
future::Either::Left(Event::NotificationStreamOpened { remote, protocol, .. }) => {
something_happened = true;
assert!(!node1_to_node2_open);
node1_to_node2_open = true;
assert_eq!(remote, *node2.local_peer_id());
assert_eq!(engine_id, ENGINE_ID);
assert_eq!(protocol, PROTOCOL_NAME);
}
future::Either::Right(Event::NotificationStreamOpened { remote, engine_id, .. }) => {
future::Either::Right(Event::NotificationStreamOpened { remote, protocol, .. }) => {
something_happened = true;
assert!(!node2_to_node1_open);
node2_to_node1_open = true;
assert_eq!(remote, *node1.local_peer_id());
assert_eq!(engine_id, ENGINE_ID);
assert_eq!(protocol, PROTOCOL_NAME);
}
future::Either::Left(Event::NotificationStreamClosed { remote, engine_id, .. }) => {
future::Either::Left(Event::NotificationStreamClosed { remote, protocol, .. }) => {
assert!(node1_to_node2_open);
node1_to_node2_open = false;
assert_eq!(remote, *node2.local_peer_id());
assert_eq!(engine_id, ENGINE_ID);
assert_eq!(protocol, PROTOCOL_NAME);
}
future::Either::Right(Event::NotificationStreamClosed { remote, engine_id, .. }) => {
future::Either::Right(Event::NotificationStreamClosed { remote, protocol, .. }) => {
assert!(node2_to_node1_open);
node2_to_node1_open = false;
assert_eq!(remote, *node1.local_peer_id());
assert_eq!(engine_id, ENGINE_ID);
assert_eq!(protocol, PROTOCOL_NAME);
}
future::Either::Left(Event::NotificationsReceived { remote, .. }) => {
assert!(node1_to_node2_open);
@@ -251,7 +251,7 @@ fn notifications_state_consistent() {
if rand::random::<u8>() % 5 >= 4 {
node1.write_notification(
node2.local_peer_id().clone(),
ENGINE_ID,
PROTOCOL_NAME,
b"hello world".to_vec()
);
}
@@ -262,7 +262,7 @@ fn notifications_state_consistent() {
if rand::random::<u8>() % 5 >= 4 {
node2.write_notification(
node1.local_peer_id().clone(),
ENGINE_ID,
PROTOCOL_NAME,
b"hello world".to_vec()
);
}
@@ -281,7 +281,7 @@ fn lots_of_incoming_peers_works() {
let listen_addr = config::build_multiaddr![Memory(rand::random::<u64>())];
let (main_node, _) = build_test_full_node(config::NetworkConfiguration {
notifications_protocols: vec![(ENGINE_ID, From::from("/foo"))],
notifications_protocols: vec![PROTOCOL_NAME],
listen_addresses: vec![listen_addr.clone()],
in_peers: u32::max_value(),
transport: config::TransportConfig::MemoryOnly,
@@ -298,7 +298,7 @@ fn lots_of_incoming_peers_works() {
let main_node_peer_id = main_node_peer_id.clone();
let (_dialing_node, event_stream) = build_test_full_node(config::NetworkConfiguration {
notifications_protocols: vec![(ENGINE_ID, From::from("/foo"))],
notifications_protocols: vec![PROTOCOL_NAME],
listen_addresses: vec![],
reserved_nodes: vec![config::MultiaddrWithPeerId {
multiaddr: listen_addr.clone(),
@@ -364,7 +364,7 @@ fn notifications_back_pressure() {
Event::NotificationStreamClosed { .. } => panic!(),
Event::NotificationsReceived { messages, .. } => {
for message in messages {
assert_eq!(message.0, ENGINE_ID);
assert_eq!(message.0, PROTOCOL_NAME);
assert_eq!(message.1, format!("hello #{}", received_notifications));
received_notifications += 1;
}
@@ -389,7 +389,7 @@ fn notifications_back_pressure() {
// Sending!
for num in 0..TOTAL_NOTIFS {
let notif = node1.notification_sender(node2_id.clone(), ENGINE_ID).unwrap();
let notif = node1.notification_sender(node2_id.clone(), PROTOCOL_NAME).unwrap();
notif.ready().await.unwrap().send(format!("hello #{}", num)).unwrap();
}