def start(self): self.upnp_component = self.component_manager.get_component( UPNP_COMPONENT) self.external_peer_port = self.upnp_component.upnp_redirects.get( "TCP", GCS("peer_port")) self.external_udp_port = self.upnp_component.upnp_redirects.get( "UDP", GCS("dht_node_port")) node_id = CS.get_node_id() if node_id is None: node_id = generate_id() external_ip = self.upnp_component.external_ip if not external_ip: log.warning("UPnP component failed to get external ip") external_ip = yield CS.get_external_ip() if not external_ip: log.warning("failed to get external ip") self.dht_node = Node(node_id=node_id, udpPort=GCS('dht_node_port'), externalUDPPort=self.external_udp_port, externalIP=external_ip, peerPort=self.external_peer_port) yield self.dht_node.start(GCS('known_dht_nodes'), block_on_join=False) log.info("Started the dht")
def setUp(self): del lbrynet.dht.protocol.reactor lbrynet.dht.protocol.reactor = twisted.internet.selectreactor.SelectReactor( ) self.node = Node(node_id='1' * 48, udpPort=9182, externalIP="127.0.0.1") self.protocol = lbrynet.dht.protocol.KademliaProtocol(self.node)
def join_network(udp_port, known_nodes): lbryid = generate_id() log.info('Creating node') node = Node(udpPort=udp_port, node_id=lbryid) log.info('Joining network') yield node.joinNetwork(known_nodes) defer.returnValue(node)
class DHTComponent(Component): component_name = DHT_COMPONENT depends_on = [UPNP_COMPONENT] def __init__(self, component_manager): super().__init__(component_manager) self.dht_node = None self.upnp_component = None self.external_udp_port = None self.external_peer_port = None @property def component(self): return self.dht_node def get_status(self): return { 'node_id': binascii.hexlify(conf.settings.get_node_id()), 'peers_in_routing_table': 0 if not self.dht_node else len(self.dht_node.contacts) } @defer.inlineCallbacks def start(self): self.upnp_component = self.component_manager.get_component( UPNP_COMPONENT) self.external_peer_port = self.upnp_component.upnp_redirects.get( "TCP", conf.settings["peer_port"]) self.external_udp_port = self.upnp_component.upnp_redirects.get( "UDP", conf.settings["dht_node_port"]) node_id = conf.settings.get_node_id() if node_id is None: node_id = generate_id() external_ip = self.upnp_component.external_ip if not external_ip: log.warning("UPnP component failed to get external ip") external_ip = yield get_external_ip() if not external_ip: log.warning("failed to get external ip") self.dht_node = Node(node_id=node_id, udpPort=conf.settings['dht_node_port'], externalUDPPort=self.external_udp_port, externalIP=external_ip, peerPort=self.external_peer_port) yield self.dht_node.start(conf.settings['known_dht_nodes'], block_on_join=False) log.info("Started the dht") @defer.inlineCallbacks def stop(self): yield self.dht_node.stop()
async def setup_network(self, size: int, start_port=40000, seed_nodes=1): for i in range(size): node_port = start_port + i node = Node(self.loop, PeerManager(self.loop), node_id=constants.generate_id(i), udp_port=node_port, internal_udp_port=node_port, peer_port=3333, external_ip='127.0.0.1') self.nodes.append(node) self.known_node_addresses.append(('127.0.0.1', node_port)) await node.start_listening('127.0.0.1') self.addCleanup(node.stop) for node in self.nodes: node.protocol.rpc_timeout = .5 node.protocol.ping_queue._default_delay = .5 node.start('127.0.0.1', self.known_node_addresses[:seed_nodes]) await asyncio.gather(*[node.joined.wait() for node in self.nodes])
def join_network(udp_port, known_nodes): lbryid = generate_id() log.info('Creating Node...') node = Node(udpPort=udp_port, lbryid=lbryid) log.info('Joining network...') d = node.joinNetwork(known_nodes) def log_network_size(): log.info("Approximate number of nodes in DHT: %s", str(node.getApproximateTotalDHTNodes())) log.info("Approximate number of blobs in DHT: %s", str(node.getApproximateTotalHashes())) d.addCallback(lambda _: log_network_size()) d.addCallback(lambda _: node) return d
def main(): parser = argparse.ArgumentParser(description="Launch a dht node which responds to rpc commands") parser.add_argument("node_port", help=("The UDP port on which the node will listen for connections " "from other dht nodes"), type=int) parser.add_argument("rpc_port", help="The TCP port on which the node will listen for rpc commands", type=int) parser.add_argument("dht_bootstrap_host", help="The IP of a DHT node to be used to bootstrap into the network", nargs='?') parser.add_argument("dht_bootstrap_port", help="The port of a DHT node to be used to bootstrap into the network", nargs='?', default=4000, type=int) parser.add_argument("--rpc_ip_address", help="The network interface on which to listen for rpc connections", default="127.0.0.1") args = parser.parse_args() def start_rpc(): rpc_node = RPCNode(node, shut_down) reactor.listenTCP(args.rpc_port, server.Site(rpc_node), interface=args.rpc_ip_address) def shut_down(): d = defer.maybeDeferred(node.stop) d.addBoth(lambda _: reactor.stop()) return d known_nodes = [] if args.dht_bootstrap_host: known_nodes.append((args.dht_bootstrap_host, args.dht_bootstrap_port)) node = Node(udpPort=args.node_port) node.joinNetwork(known_nodes) d = node._joinDeferred d.addCallback(lambda _: start_rpc()) reactor.run()
for line in lines: ipAddress, udpPort = line.split() knownNodes.append((ipAddress, int(udpPort))) else: knownNodes = None print '\nNOTE: You have not specified any remote DHT node(s) to connect to' print 'It will thus not be aware of any existing DHT, but will still function as' print ' a self-contained DHT (until another node contacts it).' print 'Run this script without any arguments for info.\n' # Set up SQLite-based data store (you could use an in-memory store instead, for example) # # Create the Entangled node. It extends the functionality of a # basic Kademlia node (but is fully backwards-compatible with a # Kademlia-only network) # # If you wish to have a pure Kademlia network, use the # entangled.kademlia.node.Node class instead print 'Creating Node' node = Node(udpPort=int(sys.argv[1]), node_id=lbryid) # Schedule the node to join the Kademlia/Entangled DHT node.joinNetwork(knownNodes) # Schedule the "storeValue() call to be invoked after 2.5 seconds, # using KEY and VALUE as arguments twisted.internet.reactor.callLater(2.5, getValue) # Start the Twisted reactor - this fires up all networking, and # allows the scheduled join operation to take place print 'Twisted reactor started (script will commence in 2.5 seconds)' twisted.internet.reactor.run()