Skip to content

Commit

Permalink
Update the P2P multiaddr component to 421 (#383)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaka authored and gnunicorn committed Aug 6, 2018
1 parent 7d9836d commit e14a5af
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 50 deletions.
2 changes: 1 addition & 1 deletion floodsub/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ fn handle_packet_received(
publish.get_data().len());
continue;
}
let from: Multiaddr = AddrComponent::IPFS(from).into();
let from: Multiaddr = AddrComponent::P2P(from).into();

let topics = publish
.take_topicIDs()
Expand Down
2 changes: 1 addition & 1 deletion identify/src/peer_id_transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ fn multiaddr_to_peerid(addr: Multiaddr) -> Result<PeerId, Multiaddr> {
}

match components.last() {
Some(&AddrComponent::P2P(ref peer_id)) | Some(&AddrComponent::IPFS(ref peer_id)) => {
Some(&AddrComponent::P2P(ref peer_id)) => {
// TODO: `peer_id` is sometimes in fact a CID here
match PeerId::from_bytes(peer_id.clone()) {
Ok(peer_id) => Ok(peer_id),
Expand Down
10 changes: 5 additions & 5 deletions libp2p/examples/kademlia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ where
P: Peerstore + Clone,
{
const ADDRESSES: &[&str] = &[
"/ip4/127.0.0.1/tcp/4001/ipfs/QmQRx32wQkw3hB45j4UDw8V9Ju4mGbxMyhs2m8mpFrFkur",
"/ip4/127.0.0.1/tcp/4001/p2p/QmQRx32wQkw3hB45j4UDw8V9Ju4mGbxMyhs2m8mpFrFkur",
// TODO: add some bootstrap nodes here
];

Expand All @@ -265,12 +265,12 @@ where
.parse::<Multiaddr>()
.expect("failed to parse hard-coded multiaddr");

let ipfs_component = multiaddr.pop().expect("hard-coded multiaddr is empty");
let peer = match ipfs_component {
libp2p::multiaddr::AddrComponent::IPFS(key) => {
let p2p_component = multiaddr.pop().expect("hard-coded multiaddr is empty");
let peer = match p2p_component {
libp2p::multiaddr::AddrComponent::P2P(key) => {
PeerId::from_bytes(key).expect("invalid peer id")
}
_ => panic!("hard-coded multiaddr didn't end with /ipfs/"),
_ => panic!("hard-coded multiaddr didn't end with /p2p/"),
};

peer_store
Expand Down
26 changes: 3 additions & 23 deletions multiaddr/src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ pub enum Protocol {
UDT = 301,
UTP = 302,
UNIX = 400,
P2P = 420,
IPFS = 421,
P2P = 421,
HTTP = 480,
HTTPS = 443,
ONION = 444,
Expand Down Expand Up @@ -70,7 +69,6 @@ impl ToString for Protocol {
Protocol::UTP => "utp",
Protocol::UNIX => "unix",
Protocol::P2P => "p2p",
Protocol::IPFS => "ipfs",
Protocol::HTTP => "http",
Protocol::HTTPS => "https",
Protocol::ONION => "onion",
Expand Down Expand Up @@ -103,7 +101,6 @@ impl FromStr for Protocol {
"utp" => Ok(Protocol::UTP),
"unix" => Ok(Protocol::UNIX),
"p2p" => Ok(Protocol::P2P),
"ipfs" => Ok(Protocol::IPFS),
"http" => Ok(Protocol::HTTP),
"https" => Ok(Protocol::HTTPS),
"onion" => Ok(Protocol::ONION),
Expand Down Expand Up @@ -145,8 +142,7 @@ impl Protocol {
301 => Ok(Protocol::UDT),
302 => Ok(Protocol::UTP),
400 => Ok(Protocol::UNIX),
420 => Ok(Protocol::P2P),
421 => Ok(Protocol::IPFS),
421 => Ok(Protocol::P2P),
480 => Ok(Protocol::HTTP),
443 => Ok(Protocol::HTTPS),
444 => Ok(Protocol::ONION),
Expand Down Expand Up @@ -187,7 +183,6 @@ impl Protocol {
Protocol::UTP => ProtocolArgSize::Fixed { bytes: 0 },
Protocol::UNIX => ProtocolArgSize::Variable,
Protocol::P2P => ProtocolArgSize::Variable,
Protocol::IPFS => ProtocolArgSize::Variable,
Protocol::HTTP => ProtocolArgSize::Fixed { bytes: 0 },
Protocol::HTTPS => ProtocolArgSize::Fixed { bytes: 0 },
Protocol::ONION => ProtocolArgSize::Fixed { bytes: 10 },
Expand Down Expand Up @@ -263,10 +258,6 @@ impl Protocol {
let bytes = Cid::from(a)?.to_bytes();
Ok(AddrComponent::P2P(bytes))
}
Protocol::IPFS => {
let bytes = Cid::from(a)?.to_bytes();
Ok(AddrComponent::IPFS(bytes))
}
Protocol::ONION => unimplemented!(), // TODO:
Protocol::QUIC => Ok(AddrComponent::QUIC),
Protocol::UTP => Ok(AddrComponent::UTP),
Expand Down Expand Up @@ -301,7 +292,6 @@ pub enum AddrComponent {
UTP,
UNIX(String),
P2P(Vec<u8>),
IPFS(Vec<u8>),
HTTP,
HTTPS,
ONION(Vec<u8>),
Expand Down Expand Up @@ -332,7 +322,6 @@ impl AddrComponent {
AddrComponent::UTP => Protocol::UTP,
AddrComponent::UNIX(_) => Protocol::UNIX,
AddrComponent::P2P(_) => Protocol::P2P,
AddrComponent::IPFS(_) => Protocol::IPFS,
AddrComponent::HTTP => Protocol::HTTP,
AddrComponent::HTTPS => Protocol::HTTPS,
AddrComponent::ONION(_) => Protocol::ONION,
Expand Down Expand Up @@ -420,10 +409,6 @@ impl AddrComponent {
let bytes = Cid::from(data)?.to_bytes();
AddrComponent::P2P(bytes)
}
Protocol::IPFS => {
let bytes = Cid::from(data)?.to_bytes();
AddrComponent::IPFS(bytes)
}
Protocol::ONION => unimplemented!(), // TODO:
Protocol::QUIC => AddrComponent::QUIC,
Protocol::UTP => AddrComponent::UTP,
Expand Down Expand Up @@ -464,7 +449,7 @@ impl AddrComponent {
out.write_varint(bytes.len())?;
out.write_all(&bytes)?;
}
AddrComponent::P2P(bytes) | AddrComponent::IPFS(bytes) => {
AddrComponent::P2P(bytes) => {
out.write_varint(bytes.len())?;
out.write_all(&bytes)?;
}
Expand Down Expand Up @@ -508,11 +493,6 @@ impl ToString for AddrComponent {
let c = Cid::from(bytes.clone()).expect("cid is known to be valid");
format!("/p2p/{}", c)
},
AddrComponent::IPFS(ref bytes) => {
// TODO: meh for cloning
let c = Cid::from(bytes.clone()).expect("cid is known to be valid");
format!("/ipfs/{}", c)
},
AddrComponent::HTTP => format!("/http"),
AddrComponent::HTTPS => format!("/https"),
AddrComponent::ONION(_) => unimplemented!(),//format!("/onion"), // TODO:
Expand Down
38 changes: 19 additions & 19 deletions multiaddr/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,54 +54,54 @@ fn construct_success() {
ma_valid("/sctp/1234", "840104D2", vec![SCTP]);
ma_valid("/udp/65535", "11FFFF", vec![UDP]);
ma_valid("/tcp/65535", "06FFFF", vec![TCP]);
ma_valid("/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
ma_valid("/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
"A503221220D52EBB89D85B02A284948203A62FF28389C57C9F42BEEC4EC20DB76A68911C0B",
vec![IPFS]);
vec![P2P]);
ma_valid("/udp/1234/sctp/1234", "1104D2840104D2", vec![UDP, SCTP]);
ma_valid("/udp/1234/udt", "1104D2AD02", vec![UDP, UDT]);
ma_valid("/udp/1234/utp", "1104D2AE02", vec![UDP, UTP]);
ma_valid("/tcp/1234/http", "0604D2E003", vec![TCP, HTTP]);
ma_valid("/tcp/1234/https", "0604D2BB03", vec![TCP, HTTPS]);
ma_valid("/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234",
ma_valid("/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234",
"A503221220D52EBB89D85B02A284948203A62FF28389C57C9F42BEEC4EC20DB76A68911C0B0604D2",
vec![IPFS, TCP]);
vec![P2P, TCP]);
ma_valid("/ip4/127.0.0.1/udp/1234",
"047F0000011104D2",
vec![IP4, UDP]);
ma_valid("/ip4/127.0.0.1/udp/0", "047F000001110000", vec![IP4, UDP]);
ma_valid("/ip4/127.0.0.1/tcp/1234",
"047F0000010604D2",
vec![IP4, TCP]);
ma_valid("/ip4/127.0.0.1/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
ma_valid("/ip4/127.0.0.1/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
"047F000001A503221220D52EBB89D85B02A284948203A62FF28389C57C9F42BEEC4EC20DB76A68911C0B",
vec![IP4, IPFS]);
ma_valid("/ip4/127.0.0.1/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234",
vec![IP4, P2P]);
ma_valid("/ip4/127.0.0.1/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234",
"047F000001A503221220D52EBB89D85B02A284948203A\
62FF28389C57C9F42BEEC4EC20DB76A68911C0B0604D2",
vec![IP4, IPFS, TCP]);
vec![IP4, P2P, TCP]);
// /unix/a/b/c/d/e,
// /unix/stdio,
// /ip4/1.2.3.4/tcp/80/unix/a/b/c/d/e/f,
// /ip4/127.0.0.1/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234/unix/stdio
// /ip4/127.0.0.1/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234/unix/stdio
ma_valid("/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:\
7095/tcp/8000/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
7095/tcp/8000/ws/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
"29200108A07AC542013AC986FFFE317095061F40DD03A5\
03221220D52EBB89D85B02A284948203A62FF28389C57C9F42BEEC4EC20DB76A68911C0B",
vec![IP6, TCP, WS, IPFS]);
vec![IP6, TCP, WS, P2P]);
ma_valid("/p2p-webrtc-star/ip4/127.0.0.\
1/tcp/9090/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
1/tcp/9090/ws/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
"9302047F000001062382DD03A503221220D52EBB89D85B\
02A284948203A62FF28389C57C9F42BEEC4EC20DB76A68911C0B",
vec![Libp2pWebrtcStar, IP4, TCP, WS, IPFS]);
vec![Libp2pWebrtcStar, IP4, TCP, WS, P2P]);
ma_valid("/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:\
7095/tcp/8000/wss/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
7095/tcp/8000/wss/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
"29200108A07AC542013AC986FFFE317095061F40DE03A503221220D52EBB8\
9D85B02A284948203A62FF28389C57C9F42BEEC4EC20DB76A68911C0B",
vec![IP6, TCP, WSS, IPFS]);
ma_valid("/ip4/127.0.0.1/tcp/9090/p2p-circuit/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
vec![IP6, TCP, WSS, P2P]);
ma_valid("/ip4/127.0.0.1/tcp/9090/p2p-circuit/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
"047F000001062382A202A503221220D52EBB89D85B\
02A284948203A62FF28389C57C9F42BEEC4EC20DB76A68911C0B",
vec![IP4, TCP, P2pCircuit, IPFS]);
vec![IP4, TCP, P2pCircuit, P2P]);
}

#[test]
Expand All @@ -128,8 +128,8 @@ fn construct_fail() {
"/ip4/127.0.0.1/udp",
"/ip4/127.0.0.1/tcp/jfodsajfidosajfoidsa",
"/ip4/127.0.0.1/tcp",
"/ip4/127.0.0.1/ipfs",
"/ip4/127.0.0.1/ipfs/tcp",
"/ip4/127.0.0.1/p2p",
"/ip4/127.0.0.1/p2p/tcp",
"/p2p-circuit/50"];

for address in &addresses {
Expand Down
2 changes: 1 addition & 1 deletion relay/src/utility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ pub(crate) struct Peer {
impl Peer {
pub(crate) fn from(mut addr: Multiaddr) -> Option<Peer> {
match addr.pop() {
Some(AddrComponent::P2P(id)) | Some(AddrComponent::IPFS(id)) => {
Some(AddrComponent::P2P(id)) => {
PeerId::from_bytes(id).ok().map(|pid| {
if addr.iter().count() == 0 {
Peer {
Expand Down

0 comments on commit e14a5af

Please sign in to comment.