Update networking code to libp2p 0.14 (#4383)

* Entirely update substrate-telemetry to futures 0.3

* Add a Closed error

* Update to libp2p 0.14

* More work

* More work

* More work

* More work

* Fix warnings

* Remove unwrap()

* Work on tests fixing

* Fix network tests

* Fix external network tests

* Update libp2p and restore Yamux in discovery test

* Ignore DNS if initializatio nfails

* Restore variables ordering

* Forgot browser-utils

* Fix downfall after merge

* Fix tests
This commit is contained in:
Pierre Krieger
2020-01-09 19:01:23 +01:00
committed by Gavin Wood
parent 6e572a9477
commit ca997cf1e4
29 changed files with 842 additions and 812 deletions
+16 -7
View File
@@ -17,7 +17,7 @@
//! Contains the `Node` struct, which handles communications with a single telemetry endpoint.
use bytes::BytesMut;
use futures::{prelude::*, compat::{Future01CompatExt as _, Compat01As03}};
use futures::prelude::*;
use futures_timer::Delay;
use libp2p::Multiaddr;
use libp2p::core::transport::Transport;
@@ -42,7 +42,7 @@ enum NodeSocket<TTrans: Transport> {
/// We're connected to the node. This is the normal state.
Connected(NodeSocketConnected<TTrans>),
/// We are currently dialing the node.
Dialing(Compat01As03<TTrans::Dial>),
Dialing(TTrans::Dial),
/// A new connection should be started as soon as possible.
ReconnectNow,
/// Waiting before attempting to dial again.
@@ -76,6 +76,9 @@ pub enum NodeEvent<TSinkErr> {
pub enum ConnectionError<TSinkErr> {
/// The connection timed-out.
Timeout,
/// Reading from the socket returned and end-of-file, indicating that the socket has been
/// closed.
Closed,
/// The sink errored.
Sink(TSinkErr),
}
@@ -106,7 +109,7 @@ where TTrans: Clone + Unpin, TTrans::Dial: Unpin,
/// Sends a WebSocket frame to the node. Returns an error if we are not connected to the node.
///
/// After calling this method, you should call `poll` in order for it to be properly processed.
pub fn send_message(&mut self, payload: Vec<u8>) -> Result<(), ()> {
pub fn send_message(&mut self, payload: impl Into<BytesMut>) -> Result<(), ()> {
if let NodeSocket::Connected(NodeSocketConnected { pending, .. }) = &mut self.socket {
if pending.len() <= MAX_PENDING {
trace!(target: "telemetry", "Adding log entry to queue for {:?}", self.addr);
@@ -163,7 +166,7 @@ where TTrans: Clone + Unpin, TTrans::Dial: Unpin,
NodeSocket::ReconnectNow => match self.transport.clone().dial(self.addr.clone()) {
Ok(d) => {
debug!(target: "telemetry", "Started dialing {}", self.addr);
socket = NodeSocket::Dialing(d.compat());
socket = NodeSocket::Dialing(d);
}
Err(err) => {
warn!(target: "telemetry", "Error while dialing {}: {:?}", self.addr, err);
@@ -212,10 +215,13 @@ where TTrans::Output: Sink<BytesMut, Error = TSinkErr>
) -> Poll<Result<futures::never::Never, ConnectionError<TSinkErr>>> {
while let Some(item) = self.pending.pop_front() {
if let Poll::Ready(_) = Sink::poll_ready(Pin::new(&mut self.sink), cx) {
if let Poll::Ready(result) = Sink::poll_ready(Pin::new(&mut self.sink), cx) {
if let Err(err) = result {
return Poll::Ready(Err(ConnectionError::Sink(err)))
}
let item_len = item.len();
if let Err(err) = Sink::start_send(Pin::new(&mut self.sink), item) {
self.timeout = None;
return Poll::Ready(Err(ConnectionError::Sink(err)))
}
trace!(
@@ -270,7 +276,10 @@ where TTrans::Output: Sink<BytesMut, Error = TSinkErr>
Poll::Ready(Some(Err(err))) => {
return Poll::Ready(Err(ConnectionError::Sink(err)))
},
Poll::Pending | Poll::Ready(None) => {},
Poll::Ready(None) => {
return Poll::Ready(Err(ConnectionError::Closed))
},
Poll::Pending => {},
}
Poll::Pending