Skip to content
Snippets Groups Projects
Commit 4213fe15 authored by Pierre Krieger's avatar Pierre Krieger Committed by GitHub
Browse files

Split specialization context from network context (#2745)

* Split specialization context from network context

* Line width
parent b88c46bf
No related merge requests found
......@@ -46,7 +46,8 @@ pub use service::{
NetworkService, NetworkWorker, FetchFuture, TransactionPool, ManageNetwork,
NetworkMsg, SyncProvider, ExHashT, ReportHandle,
};
pub use protocol::{ProtocolStatus, PeerInfo, Context};
pub use protocol::{ProtocolStatus, PeerInfo};
pub use specialization::Context;
pub use sync::{Status as SyncStatus, SyncState};
pub use network_libp2p::{
identity, multiaddr,
......
......@@ -32,7 +32,7 @@ use crate::message::{BlockAttributes, Direction, FromBlock, RequestId};
use crate::message::generic::{Message as GenericMessage, ConsensusMessage};
use crate::consensus_gossip::{ConsensusGossip, MessageRecipient as GossipMessageRecipient};
use crate::on_demand::{OnDemandCore, OnDemandNetwork, RequestData};
use crate::specialization::NetworkSpecialization;
use crate::specialization::{NetworkSpecialization, Context as SpecializationContext};
use crate::sync::{ChainSync, Context as SyncContext, Status as SyncStatus, SyncState};
use crate::service::{TransactionPool, ExHashT};
use crate::config::Roles;
......@@ -281,9 +281,6 @@ pub trait Context<B: BlockT> {
/// Send a consensus message to a peer.
fn send_consensus(&mut self, who: PeerId, consensus: ConsensusMessage);
/// Send a chain-specific message to a peer.
fn send_chain_specific(&mut self, who: PeerId, message: Vec<u8>);
}
/// Protocol context.
......@@ -315,6 +312,16 @@ impl<'a, B: BlockT + 'a, H: ExHashT + 'a> Context<B> for ProtocolContext<'a, B,
GenericMessage::Consensus(consensus)
)
}
}
impl<'a, B: BlockT + 'a, H: ExHashT + 'a> SpecializationContext<B> for ProtocolContext<'a, B, H> {
fn report_peer(&mut self, who: PeerId, reputation: i32) {
self.network_out.report_peer(who, reputation)
}
fn disconnect_peer(&mut self, who: PeerId) {
self.network_out.disconnect_peer(who)
}
fn send_chain_specific(&mut self, who: PeerId, message: Vec<u8>) {
send_message(
......
......@@ -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.
......
......@@ -50,7 +50,7 @@ use runtime_primitives::generic::BlockId;
use runtime_primitives::traits::{AuthorityIdFor, Block as BlockT, Digest, DigestItem, Header, NumberFor};
use runtime_primitives::{Justification, ConsensusEngineId};
use crate::service::{NetworkLink, NetworkMsg, ProtocolMsg, TransactionPool};
use crate::specialization::NetworkSpecialization;
use crate::specialization::{NetworkSpecialization, Context as SpecializationContext};
use test_client::{self, AccountKeyring};
pub use test_client::runtime::{Block, Extrinsic, Hash, Transfer};
......@@ -101,15 +101,20 @@ impl NetworkSpecialization<Block> for DummySpecialization {
vec![]
}
fn on_connect(&mut self, _ctx: &mut Context<Block>, _peer_id: PeerId, _status: crate::message::Status<Block>) {
fn on_connect(
&mut self,
_ctx: &mut SpecializationContext<Block>,
_peer_id: PeerId,
_status: crate::message::Status<Block>
) {
}
fn on_disconnect(&mut self, _ctx: &mut Context<Block>, _peer_id: PeerId) {
fn on_disconnect(&mut self, _ctx: &mut SpecializationContext<Block>, _peer_id: PeerId) {
}
fn on_message(
&mut self,
_ctx: &mut Context<Block>,
_ctx: &mut SpecializationContext<Block>,
_peer_id: PeerId,
_message: &mut Option<crate::message::Message<Block>>,
) {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment