From dea70a9f198512db4c8937bf932bbfdd4e224d17 Mon Sep 17 00:00:00 2001 From: Jeromy Date: Thu, 24 May 2018 07:48:52 -0700 Subject: [PATCH] begin transition from /ipfs/ to /p2p/ multiaddrs --- multiaddr_test.go | 1 + protocols.go | 96 ++++++++++++++++++++++++++++++++++++----------- transcoders.go | 8 ++-- 3 files changed, 79 insertions(+), 26 deletions(-) diff --git a/multiaddr_test.go b/multiaddr_test.go index 58752e5..d491734 100644 --- a/multiaddr_test.go +++ b/multiaddr_test.go @@ -342,6 +342,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 { diff --git a/protocols.go b/protocols.go index 12cfb53..62166ea 100644 --- a/protocols.go +++ b/protocols.go @@ -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. diff --git a/transcoders.go b/transcoders.go index 7f7f645..9b56e3e 100644 --- a/transcoders.go +++ b/transcoders.go @@ -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 {