Split specialization context from network context (#2745)

* Split specialization context from network context

* Line width
This commit is contained in:
Pierre Krieger
2019-06-03 20:26:17 +02:00
committed by GitHub
parent b88c46bfd1
commit 4213fe15cf
4 changed files with 45 additions and 11 deletions
+23 -2
View File
@@ -18,7 +18,6 @@
use crate::PeerId;
use runtime_primitives::traits::Block as BlockT;
use crate::protocol::Context;
/// A specialization of the substrate network protocol. Handles events and sends messages.
pub trait NetworkSpecialization<B: BlockT>: Send + Sync + 'static {
@@ -32,7 +31,12 @@ pub trait NetworkSpecialization<B: BlockT>: Send + Sync + 'static {
fn on_disconnect(&mut self, ctx: &mut Context<B>, who: PeerId);
/// Called when a network-specific message arrives.
fn on_message(&mut self, ctx: &mut Context<B>, who: PeerId, message: &mut Option<crate::message::Message<B>>);
fn on_message(
&mut self,
ctx: &mut Context<B>,
who: PeerId,
message: &mut Option<crate::message::Message<B>>
);
/// Called on abort.
#[deprecated(note = "This method is never called; aborting corresponds to dropping the object")]
@@ -46,6 +50,23 @@ pub trait NetworkSpecialization<B: BlockT>: Send + Sync + 'static {
fn on_block_imported(&mut self, _ctx: &mut Context<B>, _hash: B::Hash, _header: &B::Header) { }
}
/// Context for a network-specific handler.
pub trait Context<B: BlockT> {
/// Adjusts the reputation of the peer. Use this to point out that a peer has been malign or
/// irresponsible or appeared lazy.
fn report_peer(&mut self, who: PeerId, reputation: i32);
/// Force disconnecting from a peer. Use this when a peer misbehaved.
fn disconnect_peer(&mut self, who: PeerId);
/// Send a consensus message to a peer.
#[deprecated(note = "This method shouldn't have been part of the specialization API")]
fn send_consensus(&mut self, _who: PeerId, _consensus: crate::message::generic::ConsensusMessage) {}
/// Send a chain-specific message to a peer.
fn send_chain_specific(&mut self, who: PeerId, message: Vec<u8>);
}
/// Construct a simple protocol that is composed of several sub protocols.
/// Each "sub protocol" needs to implement `Specialization` and needs to provide a `new()` function.
/// For more fine grained implementations, this macro is not usable.