Modify sender and receiver to be classes
This commit is contained in:
parent
ba358335df
commit
66427cd6f5
|
@ -7,23 +7,43 @@ from libp2p.pubsub.floodsub import FloodSub
|
||||||
|
|
||||||
TOPIC = "eth"
|
TOPIC = "eth"
|
||||||
|
|
||||||
|
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 main():
|
def __init__(self):
|
||||||
node = await new_node(transport_opt=["TODO"])
|
self.next_msg_id_func = message_id_generator(0)
|
||||||
await node.get_network().listen(multiaddr.Multiaddr("TODO"))
|
self.node_id = str(uuid.uuid1())
|
||||||
|
|
||||||
floodsub = FloodSub(["/floodsub/1.0.0"])
|
@classmethod
|
||||||
pubsub = Pubsub(node, floodsub, "unnecessary-id")
|
async def create(cls, sender_node_id):
|
||||||
|
"""
|
||||||
|
Create a new DummyAccountNode and attach a libp2p node, a floodsub, and a pubsub
|
||||||
|
instance to this new node
|
||||||
|
|
||||||
pubsub_messages = await pubsub.subscribe(TOPIC)
|
We use create as this serves as a factory function and allows us
|
||||||
|
to use async await, unlike the init function
|
||||||
|
"""
|
||||||
|
self = DummyAccountNode()
|
||||||
|
|
||||||
|
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")
|
||||||
|
|
||||||
|
self.pubsub_messages = await pubsub.subscribe(TOPIC)
|
||||||
|
ack_stream = await node.new_stream(sender_node_id, ["/ack/1.0.0"])
|
||||||
|
|
||||||
|
return self
|
||||||
|
|
||||||
|
async def start_receiving():
|
||||||
# TODO: get peer id
|
# TODO: get peer id
|
||||||
ack_stream = await node.new_stream("TODO", ["/ack/1"])
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
msg = await pubsub_messages.get()
|
msg = await pubsub_messages.get()
|
||||||
await ack_stream.write("ack")
|
await ack_stream.write("ack")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
main()
|
|
||||||
|
|
|
@ -8,32 +8,41 @@ from libp2p import new_node
|
||||||
from libp2p.pubsub.pubsub import Pubsub
|
from libp2p.pubsub.pubsub import Pubsub
|
||||||
from libp2p.pubsub.floodsub import FloodSub
|
from libp2p.pubsub.floodsub import FloodSub
|
||||||
|
|
||||||
|
SUPPORTED_PUBSUB_PROTOCOLS = ["/floodsub/1.0.0"]
|
||||||
TOPIC = "eth"
|
TOPIC = "eth"
|
||||||
NUM_RECEIVERS = 10
|
NUM_RECEIVERS = 10
|
||||||
TIME_LENGTH = 1000 # seconds, double check
|
TIME_LENGTH = 1000 # seconds, double check
|
||||||
ack_queue = asyncio.Queue()
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
node = await new_node(transport_opt=["TODO"])
|
|
||||||
await node.get_network().listen(multiaddr.Multiaddr("TODO"))
|
|
||||||
|
|
||||||
floodsub = FloodSub(["/floodsub/1.0.0"])
|
|
||||||
pubsub = Pubsub(node, floodsub, "unnecessary-id")
|
|
||||||
|
|
||||||
next_msg_id_func = message_id_generator(0)
|
|
||||||
|
|
||||||
|
class SenderNode():
|
||||||
"""
|
"""
|
||||||
if is_sharded:
|
Node which has an internal balance mapping, meant to serve as
|
||||||
# Subscribe to certain topics
|
a dummy crypto blockchain. There is no actual blockchain, just a simple
|
||||||
# TODO
|
map indicating how much crypto each user in the mappings holds
|
||||||
pass
|
|
||||||
else:
|
|
||||||
# Subscribe to just one topic
|
|
||||||
await pubsub.subscribe(TOPIC)
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.next_msg_id_func = message_id_generator(0)
|
||||||
|
self.node_id = str(uuid.uuid1())
|
||||||
|
self.ack_queue = asyncio.Queue()
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
async def create(cls):
|
||||||
|
"""
|
||||||
|
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
|
||||||
|
"""
|
||||||
|
self = SenderNode()
|
||||||
|
|
||||||
|
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")
|
||||||
await pubsub.subscribe(TOPIC)
|
await pubsub.subscribe(TOPIC)
|
||||||
|
|
||||||
async def ack_stream_handler(stream):
|
async def ack_stream_handler(stream):
|
||||||
|
@ -44,27 +53,27 @@ def main():
|
||||||
await ack_queue.put(ack)
|
await ack_queue.put(ack)
|
||||||
|
|
||||||
# Set handler for acks
|
# Set handler for acks
|
||||||
node.set_stream_handler("/ack/1", ack_stream_handler)
|
self.libp2p_node.set_stream_handler("/ack/1", ack_stream_handler)
|
||||||
|
|
||||||
|
return self
|
||||||
|
|
||||||
|
def perform_test():
|
||||||
# Time and loop
|
# Time and loop
|
||||||
start = timer()
|
start = timer()
|
||||||
curr_time = timer()
|
curr_time = timer()
|
||||||
|
|
||||||
|
my_id = str(self.libp2p_node.get_id())
|
||||||
|
msg_contents = "transaction"
|
||||||
|
|
||||||
while (curr_time - start) < TIME_LENGTH:
|
while (curr_time - start) < TIME_LENGTH:
|
||||||
# Send message (NOTE THIS IS JUST ONE TOPIC)
|
# Send message (NOTE THIS IS JUST ONE TOPIC)
|
||||||
my_id = str(node.get_id())
|
packet = generate_RPC_packet(my_id, [TOPIC], msg_contents, self.next_msg_id_func())
|
||||||
msg_contents = "transaction"
|
|
||||||
packet = generate_RPC_packet(my_id, [TOPIC], msg_contents, next_msg_id_func())
|
|
||||||
await floodsub.publish(my_id, packet.SerializeToString())
|
await floodsub.publish(my_id, packet.SerializeToString())
|
||||||
|
|
||||||
# Wait for acks
|
# Wait for acks
|
||||||
num_acks = 0
|
num_acks = 0
|
||||||
while num_acks < NUM_RECEIVERS:
|
while num_acks < NUM_RECEIVERS:
|
||||||
await ack_queue.get()
|
await self.ack_queue.get()
|
||||||
num_acks += 1
|
num_acks += 1
|
||||||
|
|
||||||
curr_time = timer()
|
curr_time = timer()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
main()
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user