Clean-ups in the network-gossip crate (#4542)

* Remove usage of sc_network::Context trait

* Remove Context::send_consensus

* Pass &mut dyn Network instead of &dyn Network

* Move Validator traits and related to separate module
This commit is contained in:
Pierre Krieger
2020-01-09 19:24:51 +01:00
committed by Gavin Wood
parent b61b3095ee
commit bc3d283e78
5 changed files with 159 additions and 235 deletions
+16 -62
View File
@@ -14,10 +14,9 @@
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
use crate::Network;
use crate::state_machine::{ConsensusGossip, Validator, TopicNotification};
use crate::{Network, Validator};
use crate::state_machine::{ConsensusGossip, TopicNotification};
use sc_network::Context;
use sc_network::message::generic::ConsensusMessage;
use sc_network::{Event, ReputationChange};
@@ -36,37 +35,29 @@ pub struct GossipEngine<B: BlockT> {
struct GossipEngineInner<B: BlockT> {
state_machine: ConsensusGossip<B>,
context: Box<dyn Context<B> + Send>,
context_ext: Box<dyn ContextExt<B> + Send>,
network: Box<dyn Network<B> + Send>,
}
impl<B: BlockT> GossipEngine<B> {
/// Create a new instance.
pub fn new<N: Network<B> + Send + Clone + 'static>(
network: N,
mut network: N,
executor: &impl futures::task::Spawn,
engine_id: ConsensusEngineId,
validator: Arc<dyn Validator<B>>,
) -> Self where B: 'static {
let mut state_machine = ConsensusGossip::new();
let mut context = Box::new(ContextOverService {
network: network.clone(),
});
let context_ext = Box::new(ContextOverService {
network: network.clone(),
});
// We grab the event stream before registering the notifications protocol, otherwise we
// might miss events.
let event_stream = network.event_stream();
network.register_notifications_protocol(engine_id);
state_machine.register_validator(&mut *context, engine_id, validator);
state_machine.register_validator(&mut network, engine_id, validator);
let inner = Arc::new(Mutex::new(GossipEngineInner {
state_machine,
context,
context_ext,
network: Box::new(network),
}));
let gossip_engine = GossipEngine {
@@ -82,7 +73,7 @@ impl<B: BlockT> GossipEngine<B> {
if let Some(inner) = inner.upgrade() {
let mut inner = inner.lock();
let inner = &mut *inner;
inner.state_machine.tick(&mut *inner.context);
inner.state_machine.tick(&mut *inner.network);
} else {
// We reach this branch if the `Arc<GossipEngineInner>` has no reference
// left. We can now let the task end.
@@ -107,7 +98,7 @@ impl<B: BlockT> GossipEngine<B> {
}
let mut inner = inner.lock();
let inner = &mut *inner;
inner.state_machine.new_peer(&mut *inner.context, remote, roles);
inner.state_machine.new_peer(&mut *inner.network, remote, roles);
}
Event::NotificationsStreamClosed { remote, engine_id: msg_engine_id } => {
if msg_engine_id != engine_id {
@@ -115,13 +106,13 @@ impl<B: BlockT> GossipEngine<B> {
}
let mut inner = inner.lock();
let inner = &mut *inner;
inner.state_machine.peer_disconnected(&mut *inner.context, remote);
inner.state_machine.peer_disconnected(&mut *inner.network, remote);
},
Event::NotificationsReceived { remote, messages } => {
let mut inner = inner.lock();
let inner = &mut *inner;
inner.state_machine.on_incoming(
&mut *inner.context,
&mut *inner.network,
remote,
messages.into_iter()
.filter_map(|(engine, data)| if engine == engine_id {
@@ -144,7 +135,7 @@ impl<B: BlockT> GossipEngine<B> {
}
pub fn report(&self, who: PeerId, reputation: ReputationChange) {
self.inner.lock().context.report_peer(who, reputation);
self.inner.lock().network.report_peer(who, reputation);
}
/// Registers a message without propagating it to any peers. The message
@@ -169,7 +160,7 @@ impl<B: BlockT> GossipEngine<B> {
pub fn broadcast_topic(&self, topic: B::Hash, force: bool) {
let mut inner = self.inner.lock();
let inner = &mut *inner;
inner.state_machine.broadcast_topic(&mut *inner.context, topic, force);
inner.state_machine.broadcast_topic(&mut *inner.network, topic, force);
}
/// Get data of valid, incoming messages for a topic (but might have expired meanwhile).
@@ -188,7 +179,7 @@ impl<B: BlockT> GossipEngine<B> {
) {
let mut inner = self.inner.lock();
let inner = &mut *inner;
inner.state_machine.send_topic(&mut *inner.context, who, topic, self.engine_id, force)
inner.state_machine.send_topic(&mut *inner.network, who, topic, self.engine_id, force)
}
/// Multicast a message to all peers.
@@ -205,7 +196,7 @@ impl<B: BlockT> GossipEngine<B> {
let mut inner = self.inner.lock();
let inner = &mut *inner;
inner.state_machine.multicast(&mut *inner.context, topic, message, force)
inner.state_machine.multicast(&mut *inner.network, topic, message, force)
}
/// Send addressed message to the given peers. The message is not kept or multicast
@@ -215,7 +206,7 @@ impl<B: BlockT> GossipEngine<B> {
let inner = &mut *inner;
for who in &who {
inner.state_machine.send_message(&mut *inner.context, who, ConsensusMessage {
inner.state_machine.send_message(&mut *inner.network, who, ConsensusMessage {
engine_id: self.engine_id,
data: data.clone(),
});
@@ -227,7 +218,7 @@ impl<B: BlockT> GossipEngine<B> {
/// Note: this method isn't strictly related to gossiping and should eventually be moved
/// somewhere else.
pub fn announce(&self, block: B::Hash, associated_data: Vec<u8>) {
self.inner.lock().context_ext.announce(block, associated_data);
self.inner.lock().network.announce(block, associated_data);
}
}
@@ -239,40 +230,3 @@ impl<B: BlockT> Clone for GossipEngine<B> {
}
}
}
struct ContextOverService<N> {
network: N,
}
impl<B: BlockT, N: Network<B>> Context<B> for ContextOverService<N> {
fn report_peer(&mut self, who: PeerId, reputation: ReputationChange) {
self.network.report_peer(who, reputation);
}
fn disconnect_peer(&mut self, who: PeerId) {
self.network.disconnect_peer(who)
}
fn send_consensus(&mut self, who: PeerId, messages: Vec<ConsensusMessage>) {
for message in messages {
self.network.write_notification(who.clone(), message.engine_id, message.data);
}
}
fn send_chain_specific(&mut self, _: PeerId, _: Vec<u8>) {
log::error!(
target: "sub-libp2p",
"send_chain_specific has been called in a context where it shouldn't"
);
}
}
trait ContextExt<B: BlockT> {
fn announce(&self, block: B::Hash, associated_data: Vec<u8>);
}
impl<B: BlockT, N: Network<B>> ContextExt<B> for ContextOverService<N> {
fn announce(&self, block: B::Hash, associated_data: Vec<u8>) {
Network::announce(&self.network, block, associated_data)
}
}