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
+25 -26
View File
@@ -23,7 +23,7 @@ use futures::prelude::*;
use futures::channel::mpsc::{channel, Sender, Receiver};
use libp2p::PeerId;
use log::trace;
use sp_runtime::{traits::Block as BlockT, ConsensusEngineId};
use sp_runtime::traits::Block as BlockT;
use std::{
borrow::Cow,
collections::{HashMap, VecDeque},
@@ -38,7 +38,7 @@ pub struct GossipEngine<B: BlockT> {
state_machine: ConsensusGossip<B>,
network: Box<dyn Network<B> + Send>,
periodic_maintenance_interval: futures_timer::Delay,
engine_id: ConsensusEngineId,
protocol: Cow<'static, str>,
/// Incoming events from the network.
network_event_stream: Pin<Box<dyn Stream<Item = Event> + Send>>,
@@ -68,20 +68,21 @@ impl<B: BlockT> GossipEngine<B> {
/// Create a new instance.
pub fn new<N: Network<B> + Send + Clone + 'static>(
network: N,
engine_id: ConsensusEngineId,
protocol_name: impl Into<Cow<'static, str>>,
protocol: impl Into<Cow<'static, str>>,
validator: Arc<dyn Validator<B>>,
) -> Self where B: 'static {
let protocol = protocol.into();
// We grab the event stream before registering the notifications protocol, otherwise we
// might miss events.
let network_event_stream = network.event_stream();
network.register_notifications_protocol(engine_id, protocol_name.into());
network.register_notifications_protocol(protocol.clone());
GossipEngine {
state_machine: ConsensusGossip::new(validator, engine_id),
state_machine: ConsensusGossip::new(validator, protocol.clone()),
network: Box::new(network),
periodic_maintenance_interval: futures_timer::Delay::new(PERIODIC_MAINTENANCE_INTERVAL),
engine_id,
protocol,
network_event_stream,
message_sinks: HashMap::new(),
@@ -181,21 +182,21 @@ impl<B: BlockT> Future for GossipEngine<B> {
ForwardingState::Idle => {
match this.network_event_stream.poll_next_unpin(cx) {
Poll::Ready(Some(event)) => match event {
Event::NotificationStreamOpened { remote, engine_id, role } => {
if engine_id != this.engine_id {
Event::NotificationStreamOpened { remote, protocol, role } => {
if protocol != this.protocol {
continue;
}
this.state_machine.new_peer(&mut *this.network, remote, role);
}
Event::NotificationStreamClosed { remote, engine_id } => {
if engine_id != this.engine_id {
Event::NotificationStreamClosed { remote, protocol } => {
if protocol != this.protocol {
continue;
}
this.state_machine.peer_disconnected(&mut *this.network, remote);
},
Event::NotificationsReceived { remote, messages } => {
let messages = messages.into_iter().filter_map(|(engine, data)| {
if engine == this.engine_id {
if engine == this.protocol {
Some(data.to_vec())
} else {
None
@@ -299,6 +300,7 @@ mod tests {
use rand::Rng;
use sc_network::ObservedRole;
use sp_runtime::{testing::H256, traits::{Block as BlockT}};
use std::borrow::Cow;
use std::convert::TryInto;
use std::sync::{Arc, Mutex};
use substrate_test_runtime_client::runtime::Block;
@@ -329,11 +331,11 @@ mod tests {
unimplemented!();
}
fn write_notification(&self, _: PeerId, _: ConsensusEngineId, _: Vec<u8>) {
fn write_notification(&self, _: PeerId, _: Cow<'static, str>, _: Vec<u8>) {
unimplemented!();
}
fn register_notifications_protocol(&self, _: ConsensusEngineId, _: Cow<'static, str>) {}
fn register_notifications_protocol(&self, _: Cow<'static, str>) {}
fn announce(&self, _: B::Hash, _: Vec<u8>) {
unimplemented!();
@@ -361,8 +363,7 @@ mod tests {
let network = TestNetwork::default();
let mut gossip_engine = GossipEngine::<Block>::new(
network.clone(),
[1, 2, 3, 4],
"my_protocol",
"/my_protocol",
Arc::new(AllowAll{}),
);
@@ -383,14 +384,13 @@ mod tests {
#[test]
fn keeps_multiple_subscribers_per_topic_updated_with_both_old_and_new_messages() {
let topic = H256::default();
let engine_id = [1, 2, 3, 4];
let protocol = Cow::Borrowed("/my_protocol");
let remote_peer = PeerId::random();
let network = TestNetwork::default();
let mut gossip_engine = GossipEngine::<Block>::new(
network.clone(),
engine_id.clone(),
"my_protocol",
protocol.clone(),
Arc::new(AllowAll{}),
);
@@ -404,7 +404,7 @@ mod tests {
event_sender.start_send(
Event::NotificationStreamOpened {
remote: remote_peer.clone(),
engine_id: engine_id.clone(),
protocol: protocol.clone(),
role: ObservedRole::Authority,
}
).expect("Event stream is unbounded; qed.");
@@ -413,7 +413,7 @@ mod tests {
let events = messages.iter().cloned().map(|m| {
Event::NotificationsReceived {
remote: remote_peer.clone(),
messages: vec![(engine_id, m.into())]
messages: vec![(protocol.clone(), m.into())]
}
}).collect::<Vec<_>>();
@@ -498,7 +498,7 @@ mod tests {
}
fn prop(channels: Vec<ChannelLengthAndTopic>, notifications: Vec<Vec<Message>>) {
let engine_id = [1, 2, 3, 4];
let protocol = Cow::Borrowed("/my_protocol");
let remote_peer = PeerId::random();
let network = TestNetwork::default();
@@ -524,8 +524,7 @@ mod tests {
let mut gossip_engine = GossipEngine::<Block>::new(
network.clone(),
engine_id.clone(),
"my_protocol",
protocol.clone(),
Arc::new(TestValidator{}),
);
@@ -558,7 +557,7 @@ mod tests {
event_sender.start_send(
Event::NotificationStreamOpened {
remote: remote_peer.clone(),
engine_id: engine_id.clone(),
protocol: protocol.clone(),
role: ObservedRole::Authority,
}
).expect("Event stream is unbounded; qed.");
@@ -576,7 +575,7 @@ mod tests {
message.push(i_notification.try_into().unwrap());
message.push(i_message.try_into().unwrap());
(engine_id, message.into())
(protocol.clone(), message.into())
}).collect();
event_sender.start_send(Event::NotificationsReceived {