Skip to content

Commit 7987a81

Browse files
committed
update protocol codes to match the one from https://github.com/multiformats/multicodec/
1 parent f9b34cc commit 7987a81

File tree

5 files changed

+62
-91
lines changed

5 files changed

+62
-91
lines changed

multiaddr/codec.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from .protocols import P_DCCP
1010
from .protocols import P_IP4
1111
from .protocols import P_IP6
12-
from .protocols import P_IPFS
12+
from .protocols import P_P2P
1313
from .protocols import P_ONION
1414
from .protocols import protocol_with_code
1515
from .protocols import protocol_with_name
@@ -126,18 +126,18 @@ def address_string_to_bytes(proto, addr_string):
126126

127127
return b''.join([onion_host_bytes,
128128
binascii.hexlify(encode_big_endian_16(port))])
129-
elif proto.code == P_IPFS: # ipfs
129+
elif proto.code == P_P2P: # ipfs
130130
# the address is a varint prefixed multihash string representation
131131
try:
132132
mm = base58.b58decode(addr_string)
133133
except Exception as ex:
134-
raise ValueError("failed to parse ipfs addr: %s %s"
134+
raise ValueError("failed to parse p2p addr: %s %s"
135135
% (addr_string, str(ex)))
136136
size = code_to_varint(len(mm))
137137
mm = binascii.hexlify(mm)
138138
if len(mm) < 10:
139139
# TODO - port go-multihash so we can do this correctly
140-
raise ValueError("invalid IPFS multihash: %s" % mm)
140+
raise ValueError("invalid P2P multihash: %s" % mm)
141141
return b''.join([size, mm])
142142
else:
143143
raise ValueError("failed to parse %s addr: unknown" % proto.name)
@@ -157,7 +157,7 @@ def address_bytes_to_string(proto, buf):
157157
addr = base64.b32encode(addr_bytes).decode('ascii').lower()
158158
port = str(decode_big_endian_16(port_bytes))
159159
return ':'.join([addr, port])
160-
elif proto.code == P_IPFS:
160+
elif proto.code == P_P2P:
161161
buf = binascii.unhexlify(buf)
162162
size, num_bytes_read = read_varint_code(buf)
163163
buf = buf[num_bytes_read:]

multiaddr/protocols.csv

Lines changed: 0 additions & 27 deletions
This file was deleted.

multiaddr/protocols.py

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,64 +3,63 @@
33
import six
44
import varint
55

6+
# source of protocols https://github.com/multiformats/multicodec/blob/master/table.csv#L382
67
# replicating table here to:
78
# 1. avoid parsing the csv
89
# 2. ensuring errors in the csv don't screw up code.
910
# 3. changing a number has to happen in two places.
10-
P_IP4 = 4
11-
P_TCP = 6
12-
P_UDP = 17
13-
P_DCCP = 33
14-
P_IP6 = 41
15-
P_IP6ZONE = 42 # rfc4007 IPv6 zone
16-
P_DNS = 53 # reserved
17-
P_DNS4 = 54
18-
P_DNS6 = 55
19-
P_DNSADDR = 56
20-
P_SCTP = 132
21-
P_UDT = 301
22-
P_UTP = 302
23-
P_UNIX = 400
24-
P_P2P = 421 # preferred over /ipfs
25-
P_IPFS = 421 # backwards compatibility; equivalent to /p2p
26-
P_ONION = 444
27-
P_QUIC = 460
28-
P_HTTP = 480
29-
P_HTTPS = 443
30-
P_WS = 477
31-
P_WSS = 478
32-
P_P2P_WEBSOCKET_STAR = 479
33-
P_P2P_WEBRTC_STAR = 275
34-
P_P2P_WEBRTC_DIRECT = 276
35-
P_P2P_CIRCUIT = 290
11+
P_IP4 = 0x04
12+
P_IP6 = 0x29
13+
P_IP6ZONE = 0x2A
14+
P_TCP = 0x06
15+
P_UDP = 0x0111
16+
P_DCCP = 0x21
17+
P_SCTP = 0x84
18+
P_UDT = 0x012D
19+
P_UTP = 0x012E
20+
P_P2P = 0x01A5
21+
P_HTTP = 0x01E0
22+
P_HTTPS = 0x01BB
23+
P_QUIC = 0x01CC
24+
P_WS = 0x01DD
25+
P_WSS = 0x01DE
26+
P_ONION = 0x01BC
27+
P_P2P_CIRCUIT = 0x0122
28+
P_DNS = 0x35
29+
P_DNS4 = 0x36
30+
P_DNS6 = 0x37
31+
P_DNSADDR = 0x38
32+
P_P2P_WEBSOCKET_STAR = 0x01DF
33+
P_P2P_WEBRTC_STAR = 0x0113
34+
P_P2P_WEBRTC_DIRECT = 0x0114
35+
P_UNIX = 0x0190
3636

3737
_CODES = [
3838
P_IP4,
39+
P_IP6,
40+
P_IP6ZONE,
3941
P_TCP,
4042
P_UDP,
4143
P_DCCP,
42-
P_IP6,
43-
P_IP6ZONE,
44-
P_DNS,
45-
P_DNS4,
46-
P_DNS6,
47-
P_DNSADDR,
4844
P_SCTP,
4945
P_UDT,
5046
P_UTP,
51-
P_UNIX,
5247
P_P2P,
53-
P_IPFS,
54-
P_ONION,
55-
P_QUIC,
5648
P_HTTP,
5749
P_HTTPS,
50+
P_QUIC,
5851
P_WS,
5952
P_WSS,
53+
P_ONION,
54+
P_P2P_CIRCUIT,
55+
P_DNS,
56+
P_DNS4,
57+
P_DNS6,
58+
P_DNSADDR,
6059
P_P2P_WEBSOCKET_STAR,
6160
P_P2P_WEBRTC_STAR,
6261
P_P2P_WEBRTC_DIRECT,
63-
P_P2P_CIRCUIT,
62+
P_UNIX,
6463
]
6564

6665

@@ -103,7 +102,7 @@ def __eq__(self, other):
103102
self.vcode == other.vcode))
104103

105104
def __ne__(self, other):
106-
return not (self == other)
105+
return not self == other
107106

108107
def __repr__(self):
109108
return "Protocol(code={code}, name='{name}', size={size})".format(
@@ -159,9 +158,7 @@ def read_varint_code(buf):
159158
Protocol(P_SCTP, 16, 'sctp', code_to_varint(P_SCTP)),
160159
Protocol(P_UDT, 0, 'udt', code_to_varint(P_UDT)),
161160
Protocol(P_UTP, 0, 'utp', code_to_varint(P_UTP)),
162-
Protocol(P_UNIX, LENGTH_PREFIXED_VAR_SIZE, 'unix', code_to_varint(P_UNIX)),
163-
Protocol(P_P2P, LENGTH_PREFIXED_VAR_SIZE, 'p2p', code_to_varint(P_P2P)),
164-
Protocol(P_IPFS, LENGTH_PREFIXED_VAR_SIZE, 'ipfs', code_to_varint(P_IPFS)),
161+
Protocol(P_P2P, LENGTH_PREFIXED_VAR_SIZE, 'p2p', code_to_varint(P_P2P)),
165162
Protocol(P_ONION, 96, 'onion', code_to_varint(P_ONION)),
166163
Protocol(P_QUIC, 0, 'quic', code_to_varint(P_QUIC)),
167164
Protocol(P_HTTP, 0, 'http', code_to_varint(P_HTTP)),
@@ -172,6 +169,7 @@ def read_varint_code(buf):
172169
Protocol(P_P2P_WEBRTC_STAR, 0, 'p2p-webrtc-star', code_to_varint(P_P2P_WEBRTC_STAR)),
173170
Protocol(P_P2P_WEBRTC_DIRECT, 0, 'p2p-webrtc-direct', code_to_varint(P_P2P_WEBRTC_DIRECT)),
174171
Protocol(P_P2P_CIRCUIT, 0, 'p2p-circuit', code_to_varint(P_P2P_CIRCUIT)),
172+
Protocol(P_UNIX, LENGTH_PREFIXED_VAR_SIZE, 'unix', code_to_varint(P_UNIX)),
175173
]
176174

177175
_names_to_protocols = dict((proto.name, proto) for proto in PROTOCOLS)

tests/test_codec.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,38 @@
2020
(_names_to_protocols['tcp'], b'abcd', '43981'),
2121
(_names_to_protocols['onion'], b'9a18087306369043091f04d2',
2222
'timaq4ygg2iegci7:1234'),
23-
(_names_to_protocols['ipfs'],
23+
(_names_to_protocols['p2p'],
2424
b'221220d52ebb89d85b02a284948203a62ff28389c57c9f42beec4ec20db76a68911c0b',
2525
'QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC'),
2626
]
2727

2828
BYTES_MAP_STR_TEST_DATA = [
29-
("/ip4/127.0.0.1/udp/1234", b'047f0000011104d2'),
29+
("/ip4/127.0.0.1/udp/1234", b'047f000001910204d2'),
3030
("/ip4/127.0.0.1/tcp/4321", b'047f0000010610e1'),
3131
("/ip4/127.0.0.1/udp/1234/ip4/127.0.0.1/tcp/4321",
32-
b'047f0000011104d2047f0000010610e1')
32+
b'047f000001910204d2047f0000010610e1')
3333
]
3434

3535

3636
@pytest.mark.parametrize("proto, buf, expected", [
3737
(_names_to_protocols['https'], b'\x01\x02\x03', 0),
3838
(_names_to_protocols['ip4'], b'\x01\x02\x03', 4),
39-
(_names_to_protocols['ipfs'], b'\x40\x50\x60\x51', 65),
39+
(_names_to_protocols['p2p'], b'\x40\x50\x60\x51', 65),
4040
])
4141
def test_size_for_addr(proto, buf, expected):
4242
assert size_for_addr(proto, buf) == expected
4343

4444

4545
@pytest.mark.parametrize("buf, expected", [
4646
# "/ip4/127.0.0.1/udp/1234/ip4/127.0.0.1/tcp/4321"
47-
(b'047f0000011104d2047f0000010610e1',
47+
(b'047f000001910204d2047f0000010610e1',
4848
[b'\x04\x7f\x00\x00\x01',
49-
b'\x11\x04\xd2',
49+
b'\x91\x02\04\xd2',
5050
b'\x04\x7f\x00\x00\x01',
5151
b'\x06\x10\xe1']),
5252
])
5353
def test_bytes_split(buf, expected):
54-
assert bytes_split(buf) == expected
54+
assert bytes_split(buf) == expected
5555

5656

5757
@pytest.mark.parametrize("proto, buf, expected", ADDR_BYTES_MAP_STR_TEST_DATA)
@@ -103,7 +103,7 @@ def __init__(self, code, size, name, vcode):
103103
(_names_to_protocols['onion'], 'timaq4ygg2iegci7:a'),
104104
(_names_to_protocols['onion'], 'timaq4ygg2iegci7:0'),
105105
(_names_to_protocols['onion'], 'timaq4ygg2iegci7:71234'),
106-
(_names_to_protocols['ipfs'], '15230d52ebb89d85b02a284948203a'),
106+
(_names_to_protocols['p2p'], '15230d52ebb89d85b02a284948203a'),
107107
])
108108
def test_address_string_to_bytes_value_error(proto, address):
109109
with pytest.raises(ValueError):
@@ -112,7 +112,7 @@ def test_address_string_to_bytes_value_error(proto, address):
112112

113113
@pytest.mark.parametrize("proto, buf", [
114114
(DummyProtocol(234, 32, 'test', b'123'), b'0a0b0c0d'),
115-
(_names_to_protocols['ipfs'], b'15230d52ebb89d85b02a284948203a'),
115+
(_names_to_protocols['p2p'], b'15230d52ebb89d85b02a284948203a'),
116116
(_names_to_protocols['tcp'], b'ffffffff')
117117
])
118118
def test_address_bytes_to_string_value_error(proto, buf):

tests/test_multiaddr.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from multiaddr.protocols import protocols_with_string
99
from multiaddr.protocols import P_IP4
1010
from multiaddr.protocols import P_IP6
11-
from multiaddr.protocols import P_IPFS
11+
from multiaddr.protocols import P_P2P
1212
from multiaddr.protocols import P_UTP
1313
from multiaddr.protocols import P_TCP
1414
from multiaddr.protocols import P_UDP
@@ -40,8 +40,8 @@
4040
"/ip4/127.0.0.1/udp",
4141
"/ip4/127.0.0.1/tcp/jfodsajfidosajfoidsa",
4242
"/ip4/127.0.0.1/tcp",
43-
"/ip4/127.0.0.1/ipfs",
44-
"/ip4/127.0.0.1/ipfs/tcp"])
43+
"/ip4/127.0.0.1/p2p",
44+
"/ip4/127.0.0.1/p2p/tcp"])
4545
def test_invalid(addr_str):
4646
with pytest.raises(ValueError):
4747
Multiaddr(addr_str)
@@ -63,19 +63,19 @@ def test_invalid(addr_str):
6363
"/sctp/1234",
6464
"/udp/65535",
6565
"/tcp/65535",
66-
"/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
66+
"/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
6767
"/udp/1234/sctp/1234",
6868
"/udp/1234/udt",
6969
"/udp/1234/utp",
7070
"/tcp/1234/http",
7171
"/tcp/1234/https",
72-
"/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234",
72+
"/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234",
7373
"/ip4/127.0.0.1/udp/1234",
7474
"/ip4/127.0.0.1/udp/0",
7575
"/ip4/127.0.0.1/tcp/1234",
7676
"/ip4/127.0.0.1/tcp/1234/",
77-
"/ip4/127.0.0.1/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
78-
"/ip4/127.0.0.1/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234"]) # nopep8
77+
"/ip4/127.0.0.1/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC",
78+
"/ip4/127.0.0.1/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234"]) # nopep8
7979
def test_valid(addr_str):
8080
ma = Multiaddr(addr_str)
8181
assert str(ma) == addr_str.rstrip("/")
@@ -184,14 +184,14 @@ def assert_value_for_proto(multi, proto, expected):
184184
def test_get_value():
185185
ma = Multiaddr(
186186
"/ip4/127.0.0.1/utp/tcp/5555/udp/1234/utp/"
187-
"ipfs/QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP")
187+
"p2p/QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP")
188188

189189
assert_value_for_proto(ma, P_IP4, "127.0.0.1")
190190
assert_value_for_proto(ma, P_UTP, "")
191191
assert_value_for_proto(ma, P_TCP, "5555")
192192
assert_value_for_proto(ma, P_UDP, "1234")
193193
assert_value_for_proto(
194-
ma, P_IPFS, "QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP")
194+
ma, P_P2P, "QmbHVEEepCi7rn7VL7Exxpd2Ci9NNB6ifvqwhsrbRMgQFP")
195195

196196
with pytest.raises(ProtocolNotFoundException):
197197
ma.value_for_protocol(P_IP6)

0 commit comments

Comments
 (0)