Rewrite network protocol/service to use channels (#1340)

* rewrite network protocol/service to use channels

* remove use of unwrap

* re-introduce with_spec

* remove unnecessary mut

* remove unused param

* improve with_spec, add with_gossip

* rename job to task

* style: re-add comma

* remove extra string allocs

* rename use of channel

* turn TODO into FIXME

* remove mut in match

* remove Self in new

* pass headers by value to network service

* remove network sender from service

* remove TODO

* better expect

* rationalize use of network sender in ondemand
This commit is contained in:
Gregory Terzian
2019-02-06 19:54:02 +08:00
committed by Bastian Köcher
parent 8aae19e2db
commit a2d2ed69ab
19 changed files with 1314 additions and 903 deletions
@@ -154,6 +154,8 @@ pub enum ServiceEvent {
protocol: ProtocolId,
/// Version of the protocol that was opened.
version: u8,
/// Node debug info
debug_info: String,
},
/// A custom protocol substream has been closed.
@@ -162,6 +164,8 @@ pub enum ServiceEvent {
node_index: NodeIndex,
/// Protocol that has been closed.
protocol: ProtocolId,
/// Node debug info
debug_info: String,
},
/// Sustom protocol substreams has been closed.
@@ -172,6 +176,8 @@ pub enum ServiceEvent {
node_index: NodeIndex,
/// Protocols that have been closed.
protocols: Vec<ProtocolId>,
/// Node debug info
debug_info: String,
},
/// Receives a message on a custom protocol stream.
@@ -348,6 +354,15 @@ impl Service {
}
}
/// Get debug info for a given peer.
pub fn peer_debug_info(&self, who: NodeIndex) -> String {
if let (Some(peer_id), Some(addr)) = (self.peer_id_of_node(who), self.node_endpoint(who)) {
format!("{:?} through {:?}", peer_id, addr)
} else {
"unknown".to_string()
}
}
/// Returns the `NodeIndex` of a peer, or assigns one if none exists.
fn index_of_peer_or_assign(&mut self, peer: PeerId, endpoint: ConnectedPoint) -> NodeIndex {
match self.index_by_id.entry(peer) {
@@ -385,6 +400,7 @@ impl Service {
node_index,
protocol: protocol_id,
version,
debug_info: self.peer_debug_info(node_index),
})))
}
Ok(Async::Ready(Some(BehaviourOut::CustomProtocolClosed { protocol_id, peer_id, result }))) => {
@@ -393,6 +409,7 @@ impl Service {
break Ok(Async::Ready(Some(ServiceEvent::ClosedCustomProtocol {
node_index,
protocol: protocol_id,
debug_info: self.peer_debug_info(node_index),
})))
}
Ok(Async::Ready(Some(BehaviourOut::CustomMessage { protocol_id, peer_id, data }))) => {
+6 -6
View File
@@ -96,21 +96,21 @@ impl NetworkConfiguration {
}
/// The severity of misbehaviour of a peer that is reported.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Severity<'a> {
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Severity {
/// Peer is timing out. Could be bad connectivity of overload of work on either of our sides.
Timeout,
/// Peer has been notably useless. E.g. unable to answer a request that we might reasonably consider
/// it could answer.
Useless(&'a str),
Useless(String),
/// Peer has behaved in an invalid manner. This doesn't necessarily need to be Byzantine, but peer
/// must have taken concrete action in order to behave in such a way which is wantanly invalid.
Bad(&'a str),
Bad(String),
}
impl<'a> fmt::Display for Severity<'a> {
impl fmt::Display for Severity {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match *self {
match self {
Severity::Timeout => write!(fmt, "Timeout"),
Severity::Useless(r) => write!(fmt, "Useless ({})", r),
Severity::Bad(r) => write!(fmt, "Bad ({})", r),