From c552134c7cbff79008627ff78efc5f17204ef3b1 Mon Sep 17 00:00:00 2001 From: Stuckinaboot Date: Wed, 10 Apr 2019 17:43:09 -0400 Subject: [PATCH] Add more basic platform to debug --- .../sharding/most_basic_connect/driver.py | 50 +++++++++++++++++++ examples/sharding/most_basic_connect/node.py | 41 +++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 examples/sharding/most_basic_connect/driver.py create mode 100644 examples/sharding/most_basic_connect/node.py diff --git a/examples/sharding/most_basic_connect/driver.py b/examples/sharding/most_basic_connect/driver.py new file mode 100644 index 0000000..9ddb05a --- /dev/null +++ b/examples/sharding/most_basic_connect/driver.py @@ -0,0 +1,50 @@ +import asyncio +import json +import multiaddr +import sys +import time +from libp2p.peer.id import ID +from node import Node +from libp2p.peer.peerinfo import info_from_p2p_addr +from tests.utils import cleanup +from Crypto.PublicKey import RSA +from libp2p.peer.id import id_from_public_key + +""" +Driver is called in the following way +python receiver_driver.py topology_config.json "my_node_id" +""" + +SLEEP_TIME = 5 + +async def connect(node1, node2_addr): + # node1 connects to node2 + info = info_from_p2p_addr(node2_addr) + await node1.connect(info) + +async def main(): + # Create Node + my_transport_opt_str = sys.argv[1] + node = await Node.create(my_transport_opt_str) + + # Allow for all nodes to start up + # await asyncio.sleep(SLEEP_TIME) + + if len(sys.argv) == 3: + neighbor_addr_str = sys.argv[2] + + new_key = RSA.generate(2048, e=65537) + id_opt = id_from_public_key(new_key.publickey()) + + # Add p2p part + neighbor_addr_str += "/p2p/" + id_opt.pretty() + + # Convert neighbor_addr_str to multiaddr + neighbor_addr = multiaddr.Multiaddr(neighbor_addr_str) + await connect(node.libp2p_node, neighbor_addr) + await asyncio.sleep(5) + +if __name__ == "__main__": + loop = asyncio.get_event_loop() + loop.run_until_complete(main()) + loop.close() \ No newline at end of file diff --git a/examples/sharding/most_basic_connect/node.py b/examples/sharding/most_basic_connect/node.py new file mode 100644 index 0000000..bb0c8e9 --- /dev/null +++ b/examples/sharding/most_basic_connect/node.py @@ -0,0 +1,41 @@ +import asyncio +import multiaddr + +from timeit import default_timer as timer + +from tests.utils import cleanup +from tests.pubsub.utils import generate_RPC_packet, message_id_generator +from libp2p import new_node +from libp2p.peer.id import ID +from libp2p.pubsub.pubsub import Pubsub +from libp2p.pubsub.floodsub import FloodSub + +SUPPORTED_PUBSUB_PROTOCOLS = ["/floodsub/1.0.0"] +TOPIC = "eth" + +class Node(): + def __init__(self): + pass + + @classmethod + async def create(cls, transport_opt_str): + """ + 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 = Node() + + id_opt = ID("peer-") + + print("Sender id: " + id_opt.pretty()) + print("Transport opt is " + transport_opt_str) + + libp2p_node = await new_node(transport_opt=[transport_opt_str]) + await libp2p_node.get_network().listen(multiaddr.Multiaddr(transport_opt_str)) + + self.libp2p_node = libp2p_node + + return self \ No newline at end of file