Refactor multiaddr crate. (#498)

Refactor multiaddr crate.

- Remove `AddrComponent`. Instead `Protocol` directly contains its
associated data.

- Various smaller changes around conversions to Multiaddr from other
types, e.g. socket addresses.

- Expand tests to include property tests which test encoding/decoding
identity.
This commit is contained in:
Toralf Wittner
2018-09-20 19:51:00 +02:00
committed by GitHub
parent 1969bde4fe
commit 84b089cacc
18 changed files with 669 additions and 748 deletions

View File

@ -21,7 +21,7 @@
use futures::{future, stream, Future, Stream};
use identify_transport::{IdentifyTransport, IdentifyTransportOutcome};
use libp2p_core::{PeerId, MuxedTransport, Transport};
use multiaddr::{AddrComponent, Multiaddr};
use multiaddr::{Protocol, Multiaddr};
use std::io::{Error as IoError, ErrorKind as IoErrorKind};
use tokio_io::{AsyncRead, AsyncWrite};
@ -98,7 +98,7 @@ where
.map(move |info| {
let peer_id = info.info.public_key.clone().into_peer_id();
debug!("Identified {} as {:?}", original_addr, peer_id);
AddrComponent::P2P(peer_id.into()).into()
Protocol::P2p(peer_id.into()).into()
})) as Box<Future<Item = _, Error = _> + Send>;
(out, real_addr)
});
@ -199,7 +199,7 @@ where
.map(move |info| {
let peer_id = info.info.public_key.clone().into_peer_id();
debug!("Identified {} as {:?}", original_addr, peer_id);
AddrComponent::P2P(peer_id.into()).into()
Protocol::P2p(peer_id.into()).into()
})) as Box<Future<Item = _, Error = _> + Send>;
(out, real_addr)
});
@ -254,7 +254,7 @@ where
.map(move |info| {
let peer_id = info.info.public_key.clone().into_peer_id();
debug!("Identified {} as {:?}", original_addr, peer_id);
AddrComponent::P2P(peer_id.into()).into()
Protocol::P2p(peer_id.into()).into()
})) as Box<Future<Item = _, Error = _> + Send>;
(out, real_addr)
});
@ -288,7 +288,7 @@ fn multiaddr_to_peerid(addr: Multiaddr) -> Result<PeerId, Multiaddr> {
return Err(addr)
}
match addr.iter().last() {
Some(AddrComponent::P2P(ref peer_id)) => {
Some(Protocol::P2p(ref peer_id)) => {
match PeerId::from_multihash(peer_id.clone()) {
Ok(peer_id) => Ok(peer_id),
Err(_) => Err(addr),
@ -307,7 +307,7 @@ mod tests {
use PeerIdTransport;
use futures::{Future, Stream};
use libp2p_core::{Transport, PeerId, PublicKey};
use multiaddr::{AddrComponent, Multiaddr};
use multiaddr::{Protocol, Multiaddr};
use std::io::Error as IoError;
use std::iter;
@ -361,7 +361,7 @@ mod tests {
});
let future = transport
.dial(iter::once(AddrComponent::P2P(peer_id.into())).collect())
.dial(iter::once(Protocol::P2p(peer_id.into())).collect())
.unwrap_or_else(|_| panic!())
.then::<_, Result<(), ()>>(|_| Ok(()));