2020-01-07 14:14:34 +08:00
|
|
|
from typing import AsyncIterator
|
2019-08-31 00:18:22 +08:00
|
|
|
|
2020-01-07 14:14:34 +08:00
|
|
|
from async_generator import asynccontextmanager
|
2019-09-01 23:39:53 +08:00
|
|
|
import multiaddr
|
|
|
|
from multiaddr import Multiaddr
|
2019-08-31 00:18:22 +08:00
|
|
|
from p2pclient import Client
|
2020-01-07 14:14:34 +08:00
|
|
|
import trio
|
2019-08-31 00:18:22 +08:00
|
|
|
|
|
|
|
from libp2p.peer.id import ID
|
2019-09-01 23:39:53 +08:00
|
|
|
from libp2p.peer.peerinfo import PeerInfo, info_from_p2p_addr
|
2020-08-19 05:35:03 +08:00
|
|
|
from libp2p.security.noise.transport import PROTOCOL_ID as NOISE_PROTOCOL_ID
|
|
|
|
from libp2p.security.secio.transport import ID as SECIO_PROTOCOL_ID
|
2020-02-19 23:15:03 +08:00
|
|
|
from libp2p.typing import TProtocol
|
2019-08-31 00:18:22 +08:00
|
|
|
|
2019-09-01 23:39:53 +08:00
|
|
|
from .constants import LOCALHOST_IP
|
2019-08-31 00:18:22 +08:00
|
|
|
from .envs import GO_BIN_PATH
|
2020-01-07 14:14:34 +08:00
|
|
|
from .process import BaseInteractiveProcess
|
2019-08-31 00:18:22 +08:00
|
|
|
|
|
|
|
P2PD_PATH = GO_BIN_PATH / "p2pd"
|
|
|
|
|
|
|
|
|
2020-01-07 14:14:34 +08:00
|
|
|
class P2PDProcess(BaseInteractiveProcess):
|
2019-09-01 23:39:53 +08:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
control_maddr: Multiaddr,
|
2020-02-19 23:15:03 +08:00
|
|
|
security_protocol: TProtocol,
|
2019-09-02 17:32:15 +08:00
|
|
|
is_pubsub_enabled: bool = True,
|
|
|
|
is_gossipsub: bool = True,
|
|
|
|
is_pubsub_signing: bool = False,
|
|
|
|
is_pubsub_signing_strict: bool = False,
|
2019-09-01 23:39:53 +08:00
|
|
|
) -> None:
|
2020-08-19 05:35:03 +08:00
|
|
|
args = [f"-listen={control_maddr!s}"]
|
|
|
|
if security_protocol == SECIO_PROTOCOL_ID:
|
|
|
|
args.append("-secio")
|
|
|
|
if security_protocol == NOISE_PROTOCOL_ID:
|
|
|
|
args.append("-noise")
|
2019-09-01 23:39:53 +08:00
|
|
|
if is_pubsub_enabled:
|
|
|
|
args.append("-pubsub")
|
|
|
|
if is_gossipsub:
|
|
|
|
args.append("-pubsubRouter=gossipsub")
|
|
|
|
else:
|
|
|
|
args.append("-pubsubRouter=floodsub")
|
|
|
|
if not is_pubsub_signing:
|
|
|
|
args.append("-pubsubSign=false")
|
|
|
|
if not is_pubsub_signing_strict:
|
|
|
|
args.append("-pubsubSignStrict=false")
|
|
|
|
# NOTE:
|
|
|
|
# Two other params are possibly what we want to configure:
|
|
|
|
# - gossipsubHeartbeatInterval: GossipSubHeartbeatInitialDelay = 100 * time.Millisecond # noqa: E501
|
|
|
|
# - gossipsubHeartbeatInitialDelay: GossipSubHeartbeatInterval = 1 * time.Second
|
|
|
|
# Referece: https://github.com/libp2p/go-libp2p-daemon/blob/b95e77dbfcd186ccf817f51e95f73f9fd5982600/p2pd/main.go#L348-L353 # noqa: E501
|
2020-01-07 14:14:34 +08:00
|
|
|
self.proc = None
|
|
|
|
self.cmd = str(P2PD_PATH)
|
2019-09-01 23:39:53 +08:00
|
|
|
self.args = args
|
2020-01-07 14:14:34 +08:00
|
|
|
self.patterns = (b"Control socket:", b"Peer ID:", b"Peer Addrs:")
|
|
|
|
self.bytes_read = bytearray()
|
|
|
|
self.event_ready = trio.Event()
|
2019-09-01 23:39:53 +08:00
|
|
|
|
|
|
|
|
2019-08-31 00:18:22 +08:00
|
|
|
class Daemon:
|
2020-01-07 14:14:34 +08:00
|
|
|
p2pd_proc: BaseInteractiveProcess
|
2019-08-31 00:18:22 +08:00
|
|
|
control: Client
|
|
|
|
peer_info: PeerInfo
|
|
|
|
|
2019-09-01 23:39:53 +08:00
|
|
|
def __init__(
|
2020-01-07 14:14:34 +08:00
|
|
|
self, p2pd_proc: BaseInteractiveProcess, control: Client, peer_info: PeerInfo
|
2019-09-01 23:39:53 +08:00
|
|
|
) -> None:
|
|
|
|
self.p2pd_proc = p2pd_proc
|
2019-08-31 00:18:22 +08:00
|
|
|
self.control = control
|
|
|
|
self.peer_info = peer_info
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
|
|
|
return f"<Daemon {self.peer_id.to_string()[2:8]}>"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def peer_id(self) -> ID:
|
|
|
|
return self.peer_info.peer_id
|
|
|
|
|
|
|
|
@property
|
|
|
|
def listen_maddr(self) -> Multiaddr:
|
|
|
|
return self.peer_info.addrs[0]
|
|
|
|
|
2019-09-01 23:39:53 +08:00
|
|
|
async def close(self) -> None:
|
|
|
|
await self.p2pd_proc.close()
|
|
|
|
await self.control.close()
|
|
|
|
|
2019-08-31 00:18:22 +08:00
|
|
|
|
2020-01-07 14:14:34 +08:00
|
|
|
@asynccontextmanager
|
2019-08-31 00:18:22 +08:00
|
|
|
async def make_p2pd(
|
2019-09-03 16:55:42 +08:00
|
|
|
daemon_control_port: int,
|
|
|
|
client_callback_port: int,
|
2020-02-19 23:15:03 +08:00
|
|
|
security_protocol: TProtocol,
|
2019-11-21 11:47:54 +08:00
|
|
|
is_pubsub_enabled: bool = True,
|
|
|
|
is_gossipsub: bool = True,
|
|
|
|
is_pubsub_signing: bool = False,
|
|
|
|
is_pubsub_signing_strict: bool = False,
|
2020-01-07 14:14:34 +08:00
|
|
|
) -> AsyncIterator[Daemon]:
|
2019-09-03 16:55:42 +08:00
|
|
|
control_maddr = Multiaddr(f"/ip4/{LOCALHOST_IP}/tcp/{daemon_control_port}")
|
2019-09-01 23:39:53 +08:00
|
|
|
p2pd_proc = P2PDProcess(
|
|
|
|
control_maddr,
|
2020-02-19 23:15:03 +08:00
|
|
|
security_protocol,
|
2019-09-01 23:39:53 +08:00
|
|
|
is_pubsub_enabled,
|
|
|
|
is_gossipsub,
|
|
|
|
is_pubsub_signing,
|
|
|
|
is_pubsub_signing_strict,
|
2019-08-31 00:18:22 +08:00
|
|
|
)
|
2019-09-01 23:39:53 +08:00
|
|
|
await p2pd_proc.start()
|
2019-09-03 16:55:42 +08:00
|
|
|
client_callback_maddr = Multiaddr(f"/ip4/{LOCALHOST_IP}/tcp/{client_callback_port}")
|
2019-08-31 00:18:22 +08:00
|
|
|
p2pc = Client(control_maddr, client_callback_maddr)
|
2020-01-07 14:14:34 +08:00
|
|
|
|
|
|
|
async with p2pc.listen():
|
|
|
|
peer_id, maddrs = await p2pc.identify()
|
|
|
|
listen_maddr: Multiaddr = None
|
|
|
|
for maddr in maddrs:
|
|
|
|
try:
|
|
|
|
ip = maddr.value_for_protocol(multiaddr.protocols.P_IP4)
|
|
|
|
# NOTE: Check if this `maddr` uses `tcp`.
|
|
|
|
maddr.value_for_protocol(multiaddr.protocols.P_TCP)
|
|
|
|
except multiaddr.exceptions.ProtocolLookupError:
|
|
|
|
continue
|
|
|
|
if ip == LOCALHOST_IP:
|
|
|
|
listen_maddr = maddr
|
|
|
|
break
|
|
|
|
assert listen_maddr is not None, "no loopback maddr is found"
|
|
|
|
peer_info = info_from_p2p_addr(
|
|
|
|
listen_maddr.encapsulate(Multiaddr(f"/p2p/{peer_id.to_string()}"))
|
|
|
|
)
|
|
|
|
yield Daemon(p2pd_proc, p2pc, peer_info)
|