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-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
|
|
|
|
2019-04-07 02:55:06 +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-07 05:48:06 +08:00
|
|
|
async def create(cls, ack_protocol, topic):
|
2019-04-07 02:55:06 +08:00
|
|
|
"""
|
|
|
|
Create a new DummyAccountNode 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-07 02:55:06 +08:00
|
|
|
|
|
|
|
libp2p_node = await new_node(transport_opt=["/ip4/127.0.0.1/tcp/0"])
|
|
|
|
await libp2p_node.get_network().listen(multiaddr.Multiaddr("/ip4/127.0.0.1/tcp/0"))
|
|
|
|
|
|
|
|
self.libp2p_node = libp2p_node
|
|
|
|
|
|
|
|
self.floodsub = FloodSub(SUPPORTED_PUBSUB_PROTOCOLS)
|
|
|
|
self.pubsub = Pubsub(self.libp2p_node, self.floodsub, "a")
|
|
|
|
|
2019-04-07 05:48:06 +08:00
|
|
|
self.pubsub_messages = await self.pubsub.subscribe(topic)
|
|
|
|
self.topic = topic
|
|
|
|
|
|
|
|
self.ack_protocol = ack_protocol
|
2019-04-07 02:55:06 +08:00
|
|
|
|
|
|
|
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):
|
|
|
|
await self.libp2p_node.connect(sender_node_info)
|
2019-04-07 05:48:06 +08:00
|
|
|
ack_stream = await self.libp2p_node.new_stream(sender_node_info.peer_id, [self.ack_protocol])
|
2019-04-07 05:16:37 +08:00
|
|
|
asyncio.ensure_future(self.wait_for_end(ack_stream))
|
|
|
|
|
|
|
|
self.should_listen = True
|
2019-04-07 05:48:06 +08:00
|
|
|
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()
|
2019-04-07 05:48:06 +08:00
|
|
|
await ack_stream.write(encoded_ack_msg)
|
2019-04-08 02:25:59 +08:00
|
|
|
print("Receiver closed")
|