mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-14 02:51:08 +00:00
Make network-libp2p's Service generic over the message (#1708)
* Make network-libp2p's Service generic over the message * Apply suggestions from code review Co-Authored-By: tomaka <pierre.krieger1708@gmail.com> * Fix warning
This commit is contained in:
committed by
Bastian Köcher
parent
1f05a47cdb
commit
9e999cdd81
@@ -16,7 +16,6 @@
|
||||
|
||||
use crate::custom_proto::{CustomProtos, CustomProtosOut, RegisteredProtocols};
|
||||
use crate::{NetworkConfiguration, ProtocolId};
|
||||
use bytes::Bytes;
|
||||
use futures::prelude::*;
|
||||
use libp2p::NetworkBehaviour;
|
||||
use libp2p::core::{Multiaddr, PeerId, ProtocolsHandler, PublicKey};
|
||||
@@ -33,12 +32,12 @@ use void;
|
||||
|
||||
/// General behaviour of the network.
|
||||
#[derive(NetworkBehaviour)]
|
||||
#[behaviour(out_event = "BehaviourOut", poll_method = "poll")]
|
||||
pub struct Behaviour<TSubstream> {
|
||||
#[behaviour(out_event = "BehaviourOut<TMessage>", poll_method = "poll")]
|
||||
pub struct Behaviour<TMessage, TSubstream> {
|
||||
/// Periodically ping nodes, and close the connection if it's unresponsive.
|
||||
ping: Ping<TSubstream>,
|
||||
/// Custom protocols (dot, bbq, sub, etc.).
|
||||
custom_protocols: CustomProtos<TSubstream>,
|
||||
custom_protocols: CustomProtos<TMessage, TSubstream>,
|
||||
/// Discovers nodes of the network. Defined below.
|
||||
discovery: DiscoveryBehaviour<TSubstream>,
|
||||
/// Periodically identifies the remote and responds to incoming requests.
|
||||
@@ -46,13 +45,13 @@ pub struct Behaviour<TSubstream> {
|
||||
|
||||
/// Queue of events to produce for the outside.
|
||||
#[behaviour(ignore)]
|
||||
events: Vec<BehaviourOut>,
|
||||
events: Vec<BehaviourOut<TMessage>>,
|
||||
}
|
||||
|
||||
impl<TSubstream> Behaviour<TSubstream> {
|
||||
impl<TMessage, TSubstream> Behaviour<TMessage, TSubstream> {
|
||||
/// Builds a new `Behaviour`.
|
||||
// TODO: redundancy between config and local_public_key (https://github.com/libp2p/rust-libp2p/issues/745)
|
||||
pub fn new(config: &NetworkConfiguration, local_public_key: PublicKey, protocols: RegisteredProtocols) -> Self {
|
||||
pub fn new(config: &NetworkConfiguration, local_public_key: PublicKey, protocols: RegisteredProtocols<TMessage>) -> Self {
|
||||
let identify = {
|
||||
let proto_version = "/substrate/1.0".to_string();
|
||||
let user_agent = format!("{} ({})", config.client_version, config.node_name);
|
||||
@@ -78,7 +77,7 @@ impl<TSubstream> Behaviour<TSubstream> {
|
||||
/// Also note that even we have a valid open substream, it may in fact be already closed
|
||||
/// without us knowing, in which case the packet will not be received.
|
||||
#[inline]
|
||||
pub fn send_custom_message(&mut self, target: &PeerId, protocol_id: ProtocolId, data: impl Into<Bytes>) {
|
||||
pub fn send_custom_message(&mut self, target: &PeerId, protocol_id: ProtocolId, data: TMessage) {
|
||||
self.custom_protocols.send_packet(target, protocol_id, data)
|
||||
}
|
||||
|
||||
@@ -147,7 +146,7 @@ impl<TSubstream> Behaviour<TSubstream> {
|
||||
|
||||
/// Event that can be emitted by the behaviour.
|
||||
#[derive(Debug)]
|
||||
pub enum BehaviourOut {
|
||||
pub enum BehaviourOut<TMessage> {
|
||||
/// Opened a custom protocol with the remote.
|
||||
CustomProtocolOpen {
|
||||
/// Identifier of the protocol.
|
||||
@@ -176,8 +175,8 @@ pub enum BehaviourOut {
|
||||
peer_id: PeerId,
|
||||
/// Protocol which generated the message.
|
||||
protocol_id: ProtocolId,
|
||||
/// Data that has been received.
|
||||
data: Bytes,
|
||||
/// Message that has been received.
|
||||
message: TMessage,
|
||||
},
|
||||
|
||||
/// A substream with a remote is clogged. We should avoid sending more data to it if possible.
|
||||
@@ -187,7 +186,7 @@ pub enum BehaviourOut {
|
||||
/// Protocol which generated the message.
|
||||
protocol_id: ProtocolId,
|
||||
/// Copy of the messages that are within the buffer, for further diagnostic.
|
||||
messages: Vec<Bytes>,
|
||||
messages: Vec<TMessage>,
|
||||
},
|
||||
|
||||
/// We have obtained debug information from a peer.
|
||||
@@ -199,8 +198,8 @@ pub enum BehaviourOut {
|
||||
},
|
||||
}
|
||||
|
||||
impl From<CustomProtosOut> for BehaviourOut {
|
||||
fn from(other: CustomProtosOut) -> BehaviourOut {
|
||||
impl<TMessage> From<CustomProtosOut<TMessage>> for BehaviourOut<TMessage> {
|
||||
fn from(other: CustomProtosOut<TMessage>) -> BehaviourOut<TMessage> {
|
||||
match other {
|
||||
CustomProtosOut::CustomProtocolOpen { protocol_id, version, peer_id, endpoint } => {
|
||||
BehaviourOut::CustomProtocolOpen { protocol_id, version, peer_id, endpoint }
|
||||
@@ -208,8 +207,8 @@ impl From<CustomProtosOut> for BehaviourOut {
|
||||
CustomProtosOut::CustomProtocolClosed { protocol_id, peer_id, result } => {
|
||||
BehaviourOut::CustomProtocolClosed { protocol_id, peer_id, result }
|
||||
}
|
||||
CustomProtosOut::CustomMessage { protocol_id, peer_id, data } => {
|
||||
BehaviourOut::CustomMessage { protocol_id, peer_id, data }
|
||||
CustomProtosOut::CustomMessage { protocol_id, peer_id, message } => {
|
||||
BehaviourOut::CustomMessage { protocol_id, peer_id, message }
|
||||
}
|
||||
CustomProtosOut::Clogged { protocol_id, peer_id, messages } => {
|
||||
BehaviourOut::Clogged { protocol_id, peer_id, messages }
|
||||
@@ -218,19 +217,19 @@ impl From<CustomProtosOut> for BehaviourOut {
|
||||
}
|
||||
}
|
||||
|
||||
impl<TSubstream> NetworkBehaviourEventProcess<void::Void> for Behaviour<TSubstream> {
|
||||
impl<TMessage, TSubstream> NetworkBehaviourEventProcess<void::Void> for Behaviour<TMessage, TSubstream> {
|
||||
fn inject_event(&mut self, event: void::Void) {
|
||||
void::unreachable(event)
|
||||
}
|
||||
}
|
||||
|
||||
impl<TSubstream> NetworkBehaviourEventProcess<CustomProtosOut> for Behaviour<TSubstream> {
|
||||
fn inject_event(&mut self, event: CustomProtosOut) {
|
||||
impl<TMessage, TSubstream> NetworkBehaviourEventProcess<CustomProtosOut<TMessage>> for Behaviour<TMessage, TSubstream> {
|
||||
fn inject_event(&mut self, event: CustomProtosOut<TMessage>) {
|
||||
self.events.push(event.into());
|
||||
}
|
||||
}
|
||||
|
||||
impl<TSubstream> NetworkBehaviourEventProcess<IdentifyEvent> for Behaviour<TSubstream> {
|
||||
impl<TMessage, TSubstream> NetworkBehaviourEventProcess<IdentifyEvent> for Behaviour<TMessage, TSubstream> {
|
||||
fn inject_event(&mut self, event: IdentifyEvent) {
|
||||
match event {
|
||||
IdentifyEvent::Identified { peer_id, mut info, .. } => {
|
||||
@@ -260,7 +259,7 @@ impl<TSubstream> NetworkBehaviourEventProcess<IdentifyEvent> for Behaviour<TSubs
|
||||
}
|
||||
}
|
||||
|
||||
impl<TSubstream> NetworkBehaviourEventProcess<KademliaOut> for Behaviour<TSubstream> {
|
||||
impl<TMessage, TSubstream> NetworkBehaviourEventProcess<KademliaOut> for Behaviour<TMessage, TSubstream> {
|
||||
fn inject_event(&mut self, out: KademliaOut) {
|
||||
match out {
|
||||
KademliaOut::Discovered { peer_id, addresses, ty } => {
|
||||
@@ -282,7 +281,7 @@ impl<TSubstream> NetworkBehaviourEventProcess<KademliaOut> for Behaviour<TSubstr
|
||||
}
|
||||
}
|
||||
|
||||
impl<TSubstream> NetworkBehaviourEventProcess<PingEvent> for Behaviour<TSubstream> {
|
||||
impl<TMessage, TSubstream> NetworkBehaviourEventProcess<PingEvent> for Behaviour<TMessage, TSubstream> {
|
||||
fn inject_event(&mut self, event: PingEvent) {
|
||||
match event {
|
||||
PingEvent::PingSuccess { peer, time } => {
|
||||
@@ -292,8 +291,8 @@ impl<TSubstream> NetworkBehaviourEventProcess<PingEvent> for Behaviour<TSubstrea
|
||||
}
|
||||
}
|
||||
|
||||
impl<TSubstream> Behaviour<TSubstream> {
|
||||
fn poll<TEv>(&mut self) -> Async<NetworkBehaviourAction<TEv, BehaviourOut>> {
|
||||
impl<TMessage, TSubstream> Behaviour<TMessage, TSubstream> {
|
||||
fn poll<TEv>(&mut self) -> Async<NetworkBehaviourAction<TEv, BehaviourOut<TMessage>>> {
|
||||
if !self.events.is_empty() {
|
||||
return Async::Ready(NetworkBehaviourAction::GenerateEvent(self.events.remove(0)))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user