From eb08cfd504d5f7fe2b5cd7b8b8518dc4078a9eb3 Mon Sep 17 00:00:00 2001 From: Pierre Krieger Date: Tue, 3 Sep 2019 14:28:07 +0200 Subject: [PATCH] Remove libp2p-observed (#1238) --- Cargo.toml | 1 - protocols/observed/Cargo.toml | 21 ----- protocols/observed/src/lib.rs | 142 ---------------------------------- 3 files changed, 164 deletions(-) delete mode 100644 protocols/observed/Cargo.toml delete mode 100644 protocols/observed/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index c6617bf4..cbef55bd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -68,7 +68,6 @@ members = [ "protocols/identify", "protocols/kad", "protocols/noise", - "protocols/observed", "protocols/ping", "protocols/plaintext", "protocols/secio", diff --git a/protocols/observed/Cargo.toml b/protocols/observed/Cargo.toml deleted file mode 100644 index a26f6fc6..00000000 --- a/protocols/observed/Cargo.toml +++ /dev/null @@ -1,21 +0,0 @@ -[package] -name = "libp2p-observed-address" -edition = "2018" -description = "Observed addresses protocol for libp2p" -version = "0.12.0" -authors = ["Parity Technologies "] -license = "MIT" -repository = "https://github.com/libp2p/rust-libp2p" -keywords = ["peer-to-peer", "libp2p", "networking"] -categories = ["network-programming", "asynchronous"] - -[dependencies] -bytes = "0.4" -futures = "0.1" -libp2p-core = { version = "0.12.0", path = "../../core" } -tokio-codec = "0.1" -tokio-io = "0.1" -unsigned-varint = { version = "0.2.1", features = ["codec"] } - -[dev-dependencies] -tokio = "0.1" diff --git a/protocols/observed/src/lib.rs b/protocols/observed/src/lib.rs deleted file mode 100644 index dd89925b..00000000 --- a/protocols/observed/src/lib.rs +++ /dev/null @@ -1,142 +0,0 @@ -// Copyright 2018 Parity Technologies (UK) Ltd. -// -// Permission is hereby granted, free of charge, to any person obtaining a -// copy of this software and associated documentation files (the "Software"), -// to deal in the Software without restriction, including without limitation -// the rights to use, copy, modify, merge, publish, distribute, sublicense, -// and/or sell copies of the Software, and to permit persons to whom the -// Software is furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS -// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING -// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -// DEALINGS IN THE SOFTWARE. - -//! Connection upgrade to allow retrieving the externally visible address (as dialer) or -//! to report the externally visible address (as listener). - -use bytes::Bytes; -use futures::{future, prelude::*}; -use libp2p_core::{Multiaddr, upgrade::{InboundUpgrade, OutboundUpgrade, UpgradeInfo, Negotiated}}; -use std::{convert::TryFrom, io, iter}; -use tokio_codec::{FramedRead, FramedWrite}; -use tokio_io::{AsyncRead, AsyncWrite}; -use unsigned_varint::codec::UviBytes; - -/// The connection upgrade type to retrieve or report externally visible addresses. -pub struct Observed {} - -impl Observed { - pub fn new() -> Self { - Observed {} - } -} - -impl UpgradeInfo for Observed { - type Info = &'static [u8]; - type InfoIter = iter::Once; - - fn protocol_info(&self) -> Self::InfoIter { - iter::once(b"/paritytech/observed-address/0.1.0") - } -} - -impl InboundUpgrade for Observed -where - C: AsyncRead + AsyncWrite + Send + 'static -{ - type Output = Sender>; - type Error = io::Error; - type Future = Box + Send>; - - fn upgrade_inbound(self, conn: Negotiated, _: Self::Info) -> Self::Future { - let io = FramedWrite::new(conn, UviBytes::default()); - Box::new(future::ok(Sender { io })) - } -} - -impl OutboundUpgrade for Observed -where - C: AsyncRead + AsyncWrite + Send + 'static -{ - type Output = Multiaddr; - type Error = io::Error; - type Future = Box + Send>; - - fn upgrade_outbound(self, conn: Negotiated, _: Self::Info) -> Self::Future { - let io = FramedRead::new(conn, UviBytes::default()); - let future = io.into_future() - .map_err(|(e, _): (io::Error, FramedRead, UviBytes>)| e) - .and_then(move |(bytes, _)| { - if let Some(b) = bytes { - let ma = Multiaddr::try_from(b) - .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; - Ok(ma) - } else { - Err(io::ErrorKind::InvalidData.into()) - } - }); - Box::new(future) - } -} - -/// `Sender` allows reporting back the observed address to the remote endpoint. -pub struct Sender { - io: FramedWrite -} - -impl Sender { - /// Send address `a` to remote as the observed address. - pub fn send_address(self, a: Multiaddr) -> impl Future { - self.io.send(Bytes::from(a.to_vec())).map(|_io| ()) - } -} - -#[cfg(test)] -mod tests { - use libp2p_core::{Multiaddr, upgrade::{apply_inbound, apply_outbound}}; - use tokio::runtime::current_thread; - use tokio::net::{TcpListener, TcpStream}; - use super::*; - - #[test] - fn observed_address() { - let server = TcpListener::bind(&"127.0.0.1:0".parse().unwrap()).unwrap(); - let server_addr = server.local_addr().unwrap(); - - let observed_addr1: Multiaddr = "/ip4/127.0.0.1/tcp/10000".parse().unwrap(); - let observed_addr2 = observed_addr1.clone(); - - let server = server.incoming() - .into_future() - .map_err(|_| panic!()) - .and_then(move |(conn, _)| { - apply_inbound(conn.unwrap(), Observed::new()) - }) - .map_err(|_| panic!()) - .and_then(move |sender| sender.send_address(observed_addr1)); - - let client = TcpStream::connect(&server_addr) - .map_err(|_| panic!()) - .and_then(|conn| { - apply_outbound(conn, Observed::new()) - }) - .map_err(|_| panic!()) - .map(move |addr| { - eprintln!("{} {}", addr, observed_addr2); - assert_eq!(addr, observed_addr2) - }); - - current_thread::block_on_all(future::lazy(move || { - current_thread::spawn(server.map_err(|e| panic!("server error: {}", e)).map(|_| ())); - client - })) - .unwrap(); - } -}