adapt for multiaddr release

This commit is contained in:
zixuanzh 2019-05-12 16:28:52 -04:00
parent 82682ab03f
commit fe67cdb480
3 changed files with 38 additions and 4 deletions

View File

@ -1,6 +1,6 @@
import multiaddr
import multiaddr.util
from .utils import join, split
from .id import id_b58_decode
from .peerdata import PeerData
@ -16,7 +16,7 @@ def info_from_p2p_addr(addr):
if not addr:
raise InvalidAddrError()
parts = multiaddr.util.split(addr)
parts = split(addr)
if not parts:
raise InvalidAddrError()
@ -30,7 +30,7 @@ def info_from_p2p_addr(addr):
# we might have received just an / p2p part, which means there's no addr.
if len(parts) > 1:
addr = multiaddr.util.join(parts[:-1])
addr = join(parts[:-1])
peer_data = PeerData()
peer_data.addrs = [addr]

34
libp2p/peer/utils.py Normal file
View File

@ -0,0 +1,34 @@
from multiaddr import Multiaddr
from multiaddr.protocols import protocol_with_code, read_varint_code
def size_for_addr(proto, buf):
if proto.size >= 0:
return proto.size // 8
size, num_bytes_read = read_varint_code(buf)
return size + num_bytes_read
def bytes_split(buf):
ret = []
while buf:
code, num_bytes_read = read_varint_code(buf)
proto = protocol_with_code(code)
size = size_for_addr(proto, buf[num_bytes_read:])
length = size + num_bytes_read
ret.append(buf[:length])
buf = buf[length:]
return ret
def split(_multiaddr):
"""Return the sub-address portions of a multiaddr"""
addrs = []
splitted_addrs = bytes_split(_multiaddr.to_bytes())
for addr in splitted_addrs:
addrs.append(Multiaddr(addr))
return addrs
def join(multiaddrs):
addr_list = []
for addr in multiaddrs:
addr_list.append(addr.to_bytes())
return Multiaddr(b''.join(addr_list))

View File

@ -28,7 +28,7 @@ class TCP(ITransport):
:return: return True if successful
"""
_multiaddr = multiaddr
_multiaddr = _multiaddr.decapsulate('/p2p')
# _multiaddr = _multiaddr.decapsulate('/p2p')
coroutine = asyncio.start_server(self.handler,
_multiaddr.value_for_protocol('ip4'),