def task(): """ Assigned to a worker to retrieve (pop) a node from the reachable set and attempt to establish and maintain connection with the node. """ node = REDIS_CONN.spop('reachable') (address, port, start_height) = eval(node) handshake_msgs = [] connection = Connection((address, port), socket_timeout=SETTINGS['socket_timeout'], user_agent=SETTINGS['user_agent'], start_height=start_height) try: connection.open() handshake_msgs = connection.handshake() except (ProtocolError, socket.error) as err: logging.debug("Closing {} ({})".format(connection.to_addr, err)) connection.close() if len(handshake_msgs) == 0: return for msgs in keepalive(connection, handshake_msgs[0]): save_inv(connection.to_addr, msgs)
def task(): """ Assigned to a worker to retrieve (pop) a node from the reachable set and attempt to establish and maintain connection with the node. """ node = REDIS_CONN.spop('reachable') if node is None: return (address, port, height) = eval(node) handshake_msgs = [] connection = Connection((address, port), socket_timeout=SETTINGS['socket_timeout'], user_agent=SETTINGS['user_agent'], height=height) try: connection.open() handshake_msgs = connection.handshake() except (ProtocolError, ConnectionError, socket.error) as err: logging.debug("Closing {} ({})".format(connection.to_addr, err)) connection.close() if len(handshake_msgs) == 0: return keepalive(connection, handshake_msgs[0])
def connect(redis_conn, key): """ Establishes connection with a node to: 1) Send version message 2) Receive version and verack message 3) Send getaddr message 4) Receive addr message containing list of peering nodes Stores state and height for node in Redis. """ handshake_msgs = [] addr_msgs = [] redis_conn.hset(key, 'state', "") # Set Redis hash for a new node (address, port) = key[5:].split("-", 1) height = redis_conn.get('height') if height is None: height = 0 else: height = int(height) connection = Connection((address, int(port)), socket_timeout=SETTINGS['socket_timeout'], user_agent=SETTINGS['user_agent'], height=height) try: logging.debug("Connecting to {}".format(connection.to_addr)) connection.open() handshake_msgs = connection.handshake() addr_msgs = connection.getaddr() except (ProtocolError, ConnectionError, socket.error) as err: logging.debug("{}: {}".format(connection.to_addr, err)) finally: connection.close() gevent.sleep(0.3) redis_pipe = redis_conn.pipeline() if len(handshake_msgs) > 0: height_key = "height:{}-{}".format(address, port) redis_pipe.setex(height_key, SETTINGS['max_age'], handshake_msgs[0].get('height', 0)) now = int(time.time()) peers = enumerate_node(redis_pipe, addr_msgs, now) logging.debug("{} Peers: {}".format(connection.to_addr, peers)) redis_pipe.hset(key, 'state', "up") redis_pipe.execute()
def connect(redis_conn, key): """ Establishes connection with a node to: 1) Send version message 2) Receive version and verack message 3) Send getaddr message 4) Receive addr message containing list of peering nodes Stores node in Redis. """ handshake_msgs = [] addr_msgs = [] redis_conn.hset(key, TAG_FIELD, "") # Set Redis hash for a new node (address, port) = key[5:].split("-", 1) start_height = int(redis_conn.get('start_height')) connection = Connection((address, int(port)), socket_timeout=SETTINGS['socket_timeout'], user_agent=SETTINGS['user_agent'], start_height=start_height) try: connection.open() handshake_msgs = connection.handshake() addr_msgs = connection.getaddr() except (ProtocolError, socket.error) as err: logging.debug("{}: {}".format(connection.to_addr, err)) finally: connection.close() redis_pipe = redis_conn.pipeline() if len(handshake_msgs) > 0: start_height_key = "start_height:{}-{}".format(address, port) version_msg = handshake_msgs[0] peers = enumerate_node(redis_pipe, start_height_key, version_msg, addr_msgs) logging.debug("[{}] Peers: {}".format(connection.to_addr, peers)) redis_pipe.hset(key, TAG_FIELD, GREEN) redis_pipe.execute()
def connect(redis_conn, key): """ Establishes connection with a node to: 1) Send version message 2) Receive version and verack message 3) Send getaddr message 4) Receive addr message containing list of peering nodes Stores node in Redis. """ handshake_msgs = [] addr_msg = {} redis_conn.hset(key, TAG_FIELD, "") # Set Redis hash for a new node (address, port) = key[5:].split("-", 1) start_height = int(redis_conn.get('start_height')) connection = Connection((address, int(port)), socket_timeout=SETTINGS['socket_timeout'], user_agent=SETTINGS['user_agent'], start_height=start_height) try: connection.open() handshake_msgs = connection.handshake() addr_msg = connection.getaddr() except ProtocolError as err: logging.debug("{}".format(err)) except socket.error as err: logging.debug("{}".format(err)) finally: connection.close() redis_pipe = redis_conn.pipeline() if len(handshake_msgs) > 0: addNode(handshake_msgs[0]) enumerate_node(redis_pipe, key, handshake_msgs[0], addr_msg) redis_pipe.hset(key, TAG_FIELD, GREEN) redis_pipe.execute()
def task(): """ Assigned to a worker to retrieve (pop) a node from the reachable set and attempt to establish and maintain connection with the node. """ node = REDIS_CONN.spop('reachable') (address, port, start_height) = eval(node) handshake_msgs = [] connection = Connection((address, port), socket_timeout=SETTINGS['socket_timeout'], user_agent=SETTINGS['user_agent'], start_height=start_height) try: connection.open() handshake_msgs = connection.handshake() except ProtocolError as err: connection.close() except socket.error as err: connection.close() if len(handshake_msgs) > 0: keepalive(connection, handshake_msgs[0])