2019-11-21 11:47:54 +08:00
|
|
|
from typing import List, Sequence, Tuple
|
|
|
|
|
2019-03-18 07:33:10 +08:00
|
|
|
import multiaddr
|
2019-12-01 16:26:16 +08:00
|
|
|
import trio
|
2019-02-25 09:58:23 +08:00
|
|
|
|
2019-03-18 07:33:10 +08:00
|
|
|
from libp2p import new_node
|
2019-11-21 11:47:54 +08:00
|
|
|
from libp2p.host.basic_host import BasicHost
|
|
|
|
from libp2p.host.host_interface import IHost
|
2019-10-15 07:01:16 +08:00
|
|
|
from libp2p.kademlia.network import KademliaServer
|
2019-11-21 11:47:54 +08:00
|
|
|
from libp2p.network.stream.net_stream_interface import INetStream
|
|
|
|
from libp2p.network.swarm import Swarm
|
2019-07-25 23:11:27 +08:00
|
|
|
from libp2p.peer.peerinfo import info_from_p2p_addr
|
2019-11-21 11:47:54 +08:00
|
|
|
from libp2p.routing.interfaces import IPeerRouting
|
2019-10-15 07:01:16 +08:00
|
|
|
from libp2p.routing.kademlia.kademlia_peer_router import KadmeliaPeerRouter
|
2019-11-21 11:47:54 +08:00
|
|
|
from libp2p.typing import StreamHandlerFn, TProtocol
|
|
|
|
|
|
|
|
from .constants import MAX_READ_LEN
|
2019-09-06 17:26:40 +08:00
|
|
|
|
2019-07-25 23:11:27 +08:00
|
|
|
|
2019-11-26 19:24:30 +08:00
|
|
|
async def connect_swarm(swarm_0: Swarm, swarm_1: Swarm) -> None:
|
2019-09-14 23:37:01 +08:00
|
|
|
peer_id = swarm_1.get_peer_id()
|
|
|
|
addrs = tuple(
|
|
|
|
addr
|
|
|
|
for transport in swarm_1.listeners.values()
|
|
|
|
for addr in transport.get_addrs()
|
|
|
|
)
|
|
|
|
swarm_0.peerstore.add_addrs(peer_id, addrs, 10000)
|
2019-11-26 19:24:30 +08:00
|
|
|
await swarm_0.dial_peer(peer_id)
|
2019-09-14 23:37:01 +08:00
|
|
|
assert swarm_0.get_peer_id() in swarm_1.connections
|
|
|
|
assert swarm_1.get_peer_id() in swarm_0.connections
|
|
|
|
|
|
|
|
|
2019-11-21 11:47:54 +08:00
|
|
|
async def connect(node1: IHost, node2: IHost) -> None:
|
2019-10-24 14:41:10 +08:00
|
|
|
"""Connect node1 to node2."""
|
2019-07-25 23:11:27 +08:00
|
|
|
addr = node2.get_addrs()[0]
|
|
|
|
info = info_from_p2p_addr(addr)
|
|
|
|
await node1.connect(info)
|
2019-02-25 09:58:23 +08:00
|
|
|
|
|
|
|
|
2019-11-21 11:47:54 +08:00
|
|
|
async def set_up_nodes_by_transport_opt(
|
2019-11-19 14:01:12 +08:00
|
|
|
transport_opt_list: Sequence[Sequence[str]], nursery: trio.Nursery
|
2019-11-21 11:47:54 +08:00
|
|
|
) -> Tuple[BasicHost, ...]:
|
2019-03-18 07:33:10 +08:00
|
|
|
nodes_list = []
|
|
|
|
for transport_opt in transport_opt_list:
|
2019-11-19 14:01:12 +08:00
|
|
|
node = new_node(transport_opt=transport_opt)
|
2019-11-19 18:04:48 +08:00
|
|
|
await node.get_network().listen(
|
|
|
|
multiaddr.Multiaddr(transport_opt[0]), nursery=nursery
|
|
|
|
)
|
2019-03-18 07:33:10 +08:00
|
|
|
nodes_list.append(node)
|
|
|
|
return tuple(nodes_list)
|
|
|
|
|
2019-08-01 06:00:12 +08:00
|
|
|
|
2019-11-21 11:47:54 +08:00
|
|
|
async def set_up_nodes_by_transport_and_disc_opt(
|
|
|
|
transport_disc_opt_list: Sequence[Tuple[Sequence[str], IPeerRouting]]
|
|
|
|
) -> Tuple[BasicHost, ...]:
|
2019-10-15 07:01:16 +08:00
|
|
|
nodes_list = []
|
|
|
|
for transport_opt, disc_opt in transport_disc_opt_list:
|
|
|
|
node = await new_node(transport_opt=transport_opt, disc_opt=disc_opt)
|
|
|
|
await node.get_network().listen(multiaddr.Multiaddr(transport_opt[0]))
|
|
|
|
nodes_list.append(node)
|
|
|
|
return tuple(nodes_list)
|
|
|
|
|
|
|
|
|
2019-11-21 11:47:54 +08:00
|
|
|
async def set_up_routers(
|
|
|
|
router_confs: Tuple[int, int] = (0, 0)
|
|
|
|
) -> List[KadmeliaPeerRouter]:
|
2019-11-06 09:24:52 +08:00
|
|
|
"""The default ``router_confs`` selects two free ports local to this
|
|
|
|
machine."""
|
2019-11-21 11:47:54 +08:00
|
|
|
bootstrap_node = KademliaServer() # type: ignore
|
2019-10-15 07:01:16 +08:00
|
|
|
await bootstrap_node.listen(router_confs[0])
|
|
|
|
|
|
|
|
routers = [KadmeliaPeerRouter(bootstrap_node)]
|
|
|
|
for port in router_confs[1:]:
|
2019-11-21 11:47:54 +08:00
|
|
|
node = KademliaServer() # type: ignore
|
2019-10-15 07:01:16 +08:00
|
|
|
await node.listen(port)
|
|
|
|
|
2019-11-06 09:08:18 +08:00
|
|
|
await node.bootstrap_node(bootstrap_node.address)
|
2019-10-15 07:01:16 +08:00
|
|
|
routers.append(KadmeliaPeerRouter(node))
|
|
|
|
return routers
|
|
|
|
|
|
|
|
|
2019-11-21 11:47:54 +08:00
|
|
|
async def echo_stream_handler(stream: INetStream) -> None:
|
2019-03-18 07:33:10 +08:00
|
|
|
while True:
|
2019-09-06 17:26:40 +08:00
|
|
|
read_string = (await stream.read(MAX_READ_LEN)).decode()
|
2019-03-18 07:33:10 +08:00
|
|
|
|
|
|
|
resp = "ack:" + read_string
|
|
|
|
await stream.write(resp.encode())
|
2019-03-18 09:15:14 +08:00
|
|
|
|
2019-08-01 06:00:12 +08:00
|
|
|
|
2019-11-21 11:47:54 +08:00
|
|
|
async def perform_two_host_set_up(
|
|
|
|
handler: StreamHandlerFn = echo_stream_handler
|
|
|
|
) -> Tuple[BasicHost, BasicHost]:
|
2019-03-18 09:15:14 +08:00
|
|
|
transport_opt_list = [["/ip4/127.0.0.1/tcp/0"], ["/ip4/127.0.0.1/tcp/0"]]
|
2019-03-18 09:30:56 +08:00
|
|
|
(node_a, node_b) = await set_up_nodes_by_transport_opt(transport_opt_list)
|
2019-03-18 09:15:14 +08:00
|
|
|
|
2019-11-21 11:47:54 +08:00
|
|
|
node_b.set_stream_handler(TProtocol("/echo/1.0.0"), handler)
|
2019-03-18 09:15:14 +08:00
|
|
|
|
|
|
|
# Associate the peer with local ip address (see default parameters of Libp2p())
|
|
|
|
node_a.get_peerstore().add_addrs(node_b.get_id(), node_b.get_addrs(), 10)
|
2019-03-18 09:30:56 +08:00
|
|
|
return node_a, node_b
|