Add more basic platform to debug
This commit is contained in:
parent
db8eae74e5
commit
c552134c7c
50
examples/sharding/most_basic_connect/driver.py
Normal file
50
examples/sharding/most_basic_connect/driver.py
Normal file
|
@ -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()
|
41
examples/sharding/most_basic_connect/node.py
Normal file
41
examples/sharding/most_basic_connect/node.py
Normal file
|
@ -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
|
Loading…
Reference in New Issue
Block a user