def _discovery_loop(self): log.info('waiting for bootstrap') gevent.sleep(self.discovery_delay) while not self.is_stopped: try: num_peers, min_peers = self.num_peers( ), self.config['p2p']['min_peers'] kademlia_proto = self.app.services.discovery.protocol.kademlia if num_peers < min_peers: log.debug('missing peers', num_peers=num_peers, min_peers=min_peers, known=len(kademlia_proto.routing)) nodeid = kademlia.random_nodeid() kademlia_proto.find_node(nodeid) # fixme, should be a task gevent.sleep(self.discovery_delay) # wait for results neighbours = kademlia_proto.routing.neighbours(nodeid, 2) if not neighbours: gevent.sleep(self.connect_loop_delay) continue node = random.choice(neighbours) if node.pubkey in self.remote_pubkeys(): gevent.sleep(self.discovery_delay) continue log.debug('connecting random', node=node) local_pubkey = crypto.privtopub( decode_hex(self.config['node']['privkey_hex'])) if node.pubkey == local_pubkey: continue if node.pubkey in [p.remote_pubkey for p in self.peers]: continue self.connect((node.address.ip, node.address.tcp_port), node.pubkey) except AttributeError: # TODO: Is this the correct thing to do here? log.error("Discovery service not available.") break except Exception as e: log.error("discovery failed", error=e, num_peers=num_peers, min_peers=min_peers) gevent.sleep(self.connect_loop_delay) evt = gevent.event.Event() evt.wait()
def main(): "test connecting nodes" # stop on every unhandled exception! gevent.get_hub( ).SYSTEM_ERROR = BaseException # (KeyboardInterrupt, SystemExit, SystemError) app = get_app(30304, 'theapp') # app.config['p2p']['listen_host'] = '127.0.0.1' app.config['p2p']['listen_host'] = '0.0.0.0' print "this node is" proto = app.services.discovery.protocol.kademlia this_node = proto.this_node print this_node.pubkey.encode('hex') # add external node go_local = 'enode://6ed2fecb28ff17dec8647f08aa4368b57790000e0e9b33a7b91f32c41b6ca9ba21600e9a8c44248ce63a71544388c6745fa291f88f8b81e109ba3da11f7b41b9@127.0.0.1:30303' go_bootstrap = 'enode://6cdd090303f394a1cac34ecc9f7cda18127eafa2a3a06de39f6d920b0e583e062a7362097c7c65ee490a758b442acd5c80c6fce4b148c6a391e946b45131365b@54.169.166.226:30303' cpp_bootstrap = 'enode://24f904a876975ab5c7acbedc8ec26e6f7559b527c073c6e822049fee4df78f2e9c74840587355a068f2cdb36942679f7a377a6d8c5713ccf40b1d4b99046bba0@5.1.83.226:30303' n1 = 'enode://1d799d32547761cf66250f94b4ac1ebfc3246ce9bd87fbf90ef8d770faf48c4d96290ea0c72183d6c1ddca3d2725dad018a6c1c5d1971dbaa182792fa937e89d@162.247.54.200:1024' n2 = 'enode://1976e20d6ec2de2dd4df34d8e949994dc333da58c967c62ca84b4d545d3305942207565153e94367f5d571ef79ce6da93c5258e88ca14788c96fbbac40f4a4c7@52.0.216.64:30303' n3 = 'enode://14bb48727c8a103057ba06cc010c810e9d4beef746c54d948b681218195b3f1780945300c2534d422d6069f7a0e378c450db380f8efff8b4eccbb48c0c5bb9e8@179.218.168.19:30303' nb = 'enode://1976e20d6ec2de2dd4df34d8e949994dc333da58c967c62ca84b4d545d3305942207565153e94367f5d571ef79ce6da93c5258e88ca14788c96fbbac40f4a4c7@52.0.216.64:30303' node_uri = cpp_bootstrap r_node = discovery.Node.from_uri(node_uri) print "remote node is", r_node # add node to the routing table print "START & TEST BOOTSTRAP" app.config['p2p']['bootstrap_nodes'] = [node_uri] app.start() gevent.sleep(2.) print "TEST FIND_NODE" for i in range(10): nodeid = kademlia.random_nodeid() assert isinstance(nodeid, type(this_node.id)) proto.find_node(nodeid) gevent.sleep(1.) pinged = lambda: set(n for t, n, r in proto._expected_pongs.values()) for i in range(10): print 'num nodes', len(proto.routing) gevent.sleep(1) # proto.find_node(this_node.id) # for node in proto.routing: proto.ping(r_node) # proto.find_node(this_node.id) print 'nodes in routing' for node in proto.routing: print node.to_uri() print 'nodes we are waiting for pongs' for node in pinged(): print node.to_uri()
def main(): "test connecting nodes" # stop on every unhandled exception! gevent.get_hub().SYSTEM_ERROR = BaseException # (KeyboardInterrupt, SystemExit, SystemError) app = get_app(30304, 'theapp') # app.config['p2p']['listen_host'] = '127.0.0.1' app.config['p2p']['listen_host'] = '0.0.0.0' print "this node is" proto = app.services.discovery.protocol.kademlia this_node = proto.this_node print this_node.pubkey.encode('hex') # add external node go_local = 'enode://6ed2fecb28ff17dec8647f08aa4368b57790000e0e9b33a7b91f32c41b6ca9ba21600e9a8c44248ce63a71544388c6745fa291f88f8b81e109ba3da11f7b41b9@127.0.0.1:30303' go_bootstrap = 'enode://6cdd090303f394a1cac34ecc9f7cda18127eafa2a3a06de39f6d920b0e583e062a7362097c7c65ee490a758b442acd5c80c6fce4b148c6a391e946b45131365b@54.169.166.226:30303' cpp_bootstrap = 'enode://24f904a876975ab5c7acbedc8ec26e6f7559b527c073c6e822049fee4df78f2e9c74840587355a068f2cdb36942679f7a377a6d8c5713ccf40b1d4b99046bba0@5.1.83.226:30303' n1 = 'enode://1d799d32547761cf66250f94b4ac1ebfc3246ce9bd87fbf90ef8d770faf48c4d96290ea0c72183d6c1ddca3d2725dad018a6c1c5d1971dbaa182792fa937e89d@162.247.54.200:1024' n2 = 'enode://1976e20d6ec2de2dd4df34d8e949994dc333da58c967c62ca84b4d545d3305942207565153e94367f5d571ef79ce6da93c5258e88ca14788c96fbbac40f4a4c7@52.0.216.64:30303' n3 = 'enode://14bb48727c8a103057ba06cc010c810e9d4beef746c54d948b681218195b3f1780945300c2534d422d6069f7a0e378c450db380f8efff8b4eccbb48c0c5bb9e8@179.218.168.19:30303' nb = 'enode://1976e20d6ec2de2dd4df34d8e949994dc333da58c967c62ca84b4d545d3305942207565153e94367f5d571ef79ce6da93c5258e88ca14788c96fbbac40f4a4c7@52.0.216.64:30303' node_uri = cpp_bootstrap r_node = discovery.Node.from_uri(node_uri) print "remote node is", r_node # add node to the routing table print "START & TEST BOOTSTRAP" app.config['p2p']['bootstrap_nodes'] = [node_uri] app.start() gevent.sleep(2.) print "TEST FIND_NODE" for i in range(10): nodeid = kademlia.random_nodeid() assert isinstance(nodeid, type(this_node.id)) proto.find_node(nodeid) gevent.sleep(1.) pinged = lambda: set(n for t, n, r in proto._expected_pongs.values()) for i in range(10): print 'num nodes', len(proto.routing) gevent.sleep(1) # proto.find_node(this_node.id) # for node in proto.routing: proto.ping(r_node) # proto.find_node(this_node.id) print 'nodes in routing' for node in proto.routing: print node.to_uri() print 'nodes we are waiting for pongs' for node in pinged(): print node.to_uri()