py-libp2p/examples/sharding/receiver.py

83 lines
2.8 KiB
Python
Raw Normal View History

2019-04-07 02:23:52 +08:00
import multiaddr
2019-04-07 05:16:37 +08:00
import asyncio
2019-04-07 02:23:52 +08:00
from libp2p import new_node
2019-04-07 05:16:37 +08:00
from libp2p.peer.peerinfo import info_from_p2p_addr
2019-04-07 02:23:52 +08:00
from libp2p.pubsub.pubsub import Pubsub
from libp2p.pubsub.floodsub import FloodSub
2019-04-07 05:16:37 +08:00
from tests.pubsub.utils import message_id_generator
2019-04-10 09:37:25 +08:00
from libp2p.peer.id import ID
2019-04-07 02:23:52 +08:00
TOPIC = "eth"
2019-04-07 05:16:37 +08:00
SUPPORTED_PUBSUB_PROTOCOLS = ["/floodsub/1.0.0"]
2019-04-07 02:23:52 +08:00
class ReceiverNode():
"""
Node which has an internal balance mapping, meant to serve as
a dummy crypto blockchain. There is no actual blockchain, just a simple
map indicating how much crypto each user in the mappings holds
"""
def __init__(self):
self.next_msg_id_func = message_id_generator(0)
@classmethod
2019-04-10 09:37:25 +08:00
async def create(cls, node_id, transport_opt_str, ack_protocol, topic):
"""
2019-04-08 09:29:45 +08:00
Create a new ReceiverNode and attach a libp2p node, a floodsub, and a pubsub
instance to this new node
We use create as this serves as a factory function and allows us
to use async await, unlike the init function
"""
2019-04-07 05:16:37 +08:00
self = ReceiverNode()
2019-04-10 09:37:25 +08:00
id_opt = ID("peer-" + node_id)
libp2p_node = await new_node(transport_opt=[transport_opt_str])
2019-04-10 09:37:25 +08:00
await libp2p_node.get_network().listen(multiaddr.Multiaddr(transport_opt_str))
self.libp2p_node = libp2p_node
self.floodsub = None #FloodSub(SUPPORTED_PUBSUB_PROTOCOLS)
self.pubsub = None #Pubsub(self.libp2p_node, self.floodsub, "a")
self.pubsub_messages = None #await self.pubsub.subscribe(topic)
self.topic = topic
self.ack_protocol = ack_protocol
return self
2019-04-07 05:16:37 +08:00
async def wait_for_end(self, ack_stream):
2019-04-08 08:56:59 +08:00
# Continue waiting for end message, even if None (i.e. timeout) is received
msg = await ack_stream.read()
while msg is None:
msg = await ack_stream.read()
msg = msg.decode()
2019-04-07 05:16:37 +08:00
if msg == "end":
self.should_listen = False
2019-04-08 03:24:56 +08:00
print("End received")
2019-04-07 05:16:37 +08:00
async def start_receiving(self, sender_node_info):
2019-04-10 09:12:51 +08:00
print("Receiving started")
2019-04-07 05:16:37 +08:00
await self.libp2p_node.connect(sender_node_info)
2019-04-10 09:12:51 +08:00
print("Connection to sender confirmed")
print("Creating ack stream with ack protocol " + self.ack_protocol \
+ ", peer_id " + sender_node_info.peer_id.pretty())
ack_stream = await self.libp2p_node.new_stream(sender_node_info.peer_id, [self.ack_protocol])
2019-04-10 09:12:51 +08:00
print("Ack stream created")
2019-04-07 05:16:37 +08:00
asyncio.ensure_future(self.wait_for_end(ack_stream))
2019-04-10 09:12:51 +08:00
print("Listening for ack messages")
2019-04-07 05:16:37 +08:00
self.should_listen = True
ack_msg = self.topic
encoded_ack_msg = ack_msg.encode()
2019-04-07 05:16:37 +08:00
while self.should_listen:
msg = await self.pubsub_messages.get()
await ack_stream.write(encoded_ack_msg)
2019-04-08 02:25:59 +08:00
print("Receiver closed")