Unverified Commit 84fb1056 authored by Steven Allen's avatar Steven Allen Committed by GitHub

Merge pull request #66 from multiformats/feat/p2p-transition

begin transition from /ipfs/ to /p2p/ multiaddrs
parents 997959a3 7f59fc3f
......@@ -45,6 +45,8 @@ func TestConstructFails(t *testing.T) {
"/ip4/127.0.0.1/quic/1234",
"/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",
"/unix",
"/ip4/1.2.3.4/tcp/80/unix",
}
......@@ -74,12 +76,14 @@ func TestConstructSucceeds(t *testing.T) {
"/udp/65535",
"/tcp/65535",
"/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
"/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
"/udp/1234/sctp/1234",
"/udp/1234/udt",
"/udp/1234/utp",
"/tcp/1234/http",
"/tcp/1234/https",
"/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234",
"/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234",
"/ip4/127.0.0.1/udp/1234",
"/ip4/127.0.0.1/udp/0",
"/ip4/127.0.0.1/tcp/1234",
......@@ -87,10 +91,13 @@ func TestConstructSucceeds(t *testing.T) {
"/ip4/127.0.0.1/udp/1234/quic",
"/ip4/127.0.0.1/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
"/ip4/127.0.0.1/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234",
"/ip4/127.0.0.1/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
"/ip4/127.0.0.1/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234",
"/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",
}
for _, a := range cases {
......@@ -342,6 +349,7 @@ func TestGetValue(t *testing.T) {
assertValueForProto(t, a, P_TCP, "5555")
assertValueForProto(t, a, P_UDP, "1234")
assertValueForProto(t, a, P_IPFS, "QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP")
assertValueForProto(t, a, P_P2P, "QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP")
_, err := a.ValueForProtocol(P_IP6)
switch err {
......@@ -432,3 +440,41 @@ func TestBinaryRepresentation(t *testing.T) {
t.Errorf("expected %x, got %x", expected, ma.Bytes())
}
}
func TestRoundTrip(t *testing.T) {
for _, s := range []string{
"/unix/a/b/c/d",
"/ip4/127.0.0.1/tcp/123",
"/ip4/127.0.0.1/udp/123",
"/ip4/127.0.0.1/udp/123/ip6/::",
"/ipfs/QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP",
"/ipfs/QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP/unix/a/b/c",
} {
ma, err := NewMultiaddr(s)
if err != nil {
t.Errorf("error when parsing %q: %s", s, err)
continue
}
if ma.String() != s {
t.Errorf("failed to round trip %q", s)
}
}
}
// XXX: Change this test when we switch to /p2p by default.
func TestIPFSvP2P(t *testing.T) {
var (
p2pAddr = "/p2p/QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP"
ipfsAddr = "/ipfs/QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP"
)
for _, s := range []string{p2pAddr, ipfsAddr} {
ma, err := NewMultiaddr(s)
if err != nil {
t.Errorf("error when parsing %q: %s", s, err)
}
if ma.String() != ipfsAddr {
t.Errorf("expected %q, got %q", ipfsAddr, ma.String())
}
}
}
......@@ -33,7 +33,8 @@ const (
P_UDT = 0x012D
P_UTP = 0x012E
P_UNIX = 0x0190
P_IPFS = 0x01A5
P_P2P = 0x01A5
P_IPFS = 0x01A5 // alias for backwards compatability
P_HTTP = 0x01E0
P_HTTPS = 0x01BB
P_ONION = 0x01BC
......@@ -46,21 +47,76 @@ const (
// Protocols is the list of multiaddr protocols supported by this module.
var Protocols = []Protocol{
Protocol{P_IP4, 32, "ip4", CodeToVarint(P_IP4), false, TranscoderIP4},
Protocol{P_TCP, 16, "tcp", CodeToVarint(P_TCP), false, TranscoderPort},
Protocol{P_UDP, 16, "udp", CodeToVarint(P_UDP), false, TranscoderPort},
Protocol{P_DCCP, 16, "dccp", CodeToVarint(P_DCCP), false, TranscoderPort},
Protocol{P_IP6, 128, "ip6", CodeToVarint(P_IP6), false, TranscoderIP6},
// these require varint:
Protocol{P_SCTP, 16, "sctp", CodeToVarint(P_SCTP), false, TranscoderPort},
Protocol{P_ONION, 96, "onion", CodeToVarint(P_ONION), false, TranscoderOnion},
Protocol{P_UTP, 0, "utp", CodeToVarint(P_UTP), false, nil},
Protocol{P_UDT, 0, "udt", CodeToVarint(P_UDT), false, nil},
Protocol{P_QUIC, 0, "quic", CodeToVarint(P_QUIC), false, nil},
Protocol{P_HTTP, 0, "http", CodeToVarint(P_HTTP), false, nil},
Protocol{P_HTTPS, 0, "https", CodeToVarint(P_HTTPS), false, nil},
Protocol{P_IPFS, LengthPrefixedVarSize, "ipfs", CodeToVarint(P_IPFS), false, TranscoderIPFS},
Protocol{P_UNIX, LengthPrefixedVarSize, "unix", CodeToVarint(P_UNIX), true, TranscoderUnix},
protoIP4,
protoTCP,
protoUDP,
protoDCCP,
protoIP6,
protoSCTP,
protoONION,
protoUTP,
protoUDT,
protoQUIC,
protoHTTP,
protoHTTPS,
protoP2P,
protoUNIX,
}
var (
protoIP4 = Protocol{P_IP4, 32, "ip4", CodeToVarint(P_IP4), false, TranscoderIP4}
protoTCP = Protocol{P_TCP, 16, "tcp", CodeToVarint(P_TCP), false, TranscoderPort}
protoUDP = Protocol{P_UDP, 16, "udp", CodeToVarint(P_UDP), false, TranscoderPort}
protoDCCP = Protocol{P_DCCP, 16, "dccp", CodeToVarint(P_DCCP), false, TranscoderPort}
protoIP6 = Protocol{P_IP6, 128, "ip6", CodeToVarint(P_IP6), false, TranscoderIP6}
// these require varint
protoSCTP = Protocol{P_SCTP, 16, "sctp", CodeToVarint(P_SCTP), false, TranscoderPort}
protoONION = Protocol{P_ONION, 96, "onion", CodeToVarint(P_ONION), false, TranscoderOnion}
protoUTP = Protocol{P_UTP, 0, "utp", CodeToVarint(P_UTP), false, nil}
protoUDT = Protocol{P_UDT, 0, "udt", CodeToVarint(P_UDT), false, nil}
protoQUIC = Protocol{P_QUIC, 0, "quic", CodeToVarint(P_QUIC), false, nil}
protoHTTP = Protocol{P_HTTP, 0, "http", CodeToVarint(P_HTTP), false, nil}
protoHTTPS = Protocol{P_HTTPS, 0, "https", CodeToVarint(P_HTTPS), false, nil}
protoP2P = Protocol{P_P2P, LengthPrefixedVarSize, "ipfs", CodeToVarint(P_P2P), false, TranscoderP2P}
protoUNIX = Protocol{P_UNIX, LengthPrefixedVarSize, "unix", CodeToVarint(P_UNIX), true, TranscoderUnix}
)
var ProtocolsByName = map[string]Protocol{}
func init() {
for _, p := range Protocols {
ProtocolsByName[p.Name] = p
}
// explicitly set both of these
ProtocolsByName["p2p"] = protoP2P
ProtocolsByName["ipfs"] = protoP2P
}
// SwapToP2pMultiaddrs is a function to make the transition from /ipfs/...
// multiaddrs to /p2p/... multiaddrs easier
// The first stage of the rollout is to ship this package to all users so
// that all users of multiaddr can parse both /ipfs/ and /p2p/ multiaddrs
// as the same code (P_P2P). During this stage of the rollout, all addresses
// with P_P2P will continue printing as /ipfs/, so that older clients without
// the new parsing code won't break.
// Once the network has adopted the new parsing code broadly enough, users of
// multiaddr can add a call to this method to an init function in their codebase.
// This will cause any P_P2P multiaddr to print out as /p2p/ instead of /ipfs/.
// Note that the binary serialization of this multiaddr does not change at any
// point. This means that this code is not a breaking network change at any point
func SwapToP2pMultiaddrs() {
for i := range Protocols {
if Protocols[i].Code == P_P2P {
Protocols[i].Name = "p2p"
break
}
}
protoP2P.Name = "p2p"
ProtocolsByName["ipfs"] = protoP2P
ProtocolsByName["p2p"] = protoP2P
}
func AddProtocol(p Protocol) error {
......@@ -74,17 +130,13 @@ func AddProtocol(p Protocol) error {
}
Protocols = append(Protocols, p)
ProtocolsByName[p.Name] = p
return nil
}
// ProtocolWithName returns the Protocol description with given string name.
func ProtocolWithName(s string) Protocol {
for _, p := range Protocols {
if p.Name == s {
return p
}
}
return Protocol{}
return ProtocolsByName[s]
}
// ProtocolWithCode returns the Protocol description with given protocol code.
......
......@@ -121,20 +121,20 @@ func onionBtS(b []byte) (string, error) {
return addr + ":" + strconv.Itoa(int(port)), nil
}
var TranscoderIPFS = NewTranscoderFromFunctions(ipfsStB, ipfsBtS)
var TranscoderP2P = NewTranscoderFromFunctions(p2pStB, p2pBtS)
func ipfsStB(s string) ([]byte, error) {
func p2pStB(s string) ([]byte, error) {
// the address is a varint prefixed multihash string representation
m, err := mh.FromB58String(s)
if err != nil {
return nil, fmt.Errorf("failed to parse ipfs addr: %s %s", s, err)
return nil, fmt.Errorf("failed to parse p2p addr: %s %s", s, err)
}
size := CodeToVarint(len(m))
b := append(size, m...)
return b, nil
}
func ipfsBtS(b []byte) (string, error) {
func p2pBtS(b []byte) (string, error) {
// the address is a varint-prefixed multihash string representation
size, n, err := ReadVarintCode(b)
if err != nil {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment