def _is_node_missing(self, node_hash: Hash32) -> bool: if len(node_hash) != 32: raise ValidationError(f"Must request node by its 32-byte hash: 0x{node_hash.hex()}") self.logger.debug2("checking if node 0x%s is present", node_hash.hex()) return node_hash not in self._db
def track_peer_connection(self, remote: NodeAPI, is_outbound: bool, last_connected_at: Optional[datetime.datetime], genesis_hash: Hash32, protocol: str, protocol_version: int, network_id: int) -> None: uri = remote.uri() now = datetime.datetime.utcnow() if self._remote_exists(uri): self.logger.debug2("Updated ETH1 peer record: %s", remote) record = self._get_remote(uri) record.updated_at = now if last_connected_at is not None: record.last_connected_at = last_connected_at record.genesis_hash = genesis_hash.hex() record.protocol = protocol record.protocol_version = protocol_version record.network_id = network_id else: self.logger.debug2("New ETH1 peer record: %s", remote) record = Remote( uri=uri, is_outbound=is_outbound, created_at=now, updated_at=now, last_connected_at=last_connected_at, genesis_hash=genesis_hash.hex(), protocol=protocol, protocol_version=protocol_version, network_id=network_id, ) self.session.add(record) self.session.commit() # type: ignore
def __init__(self, session: BaseSession, genesis_hash: Hash32 = None, protocols: Tuple[str, ...] = None, protocol_versions: Tuple[int, ...] = None, network_id: int = None) -> None: self.session = session if genesis_hash is not None: self.genesis_hash = genesis_hash.hex() else: self.genesis_hash = genesis_hash self.protocols = protocols self.protocol_versions = protocol_versions self.network_id = network_id
def _is_node_missing(self, node_hash: Hash32) -> bool: if len(node_hash) != 32: raise ValidationError(f"Must request node by its 32-byte hash: 0x{node_hash.hex()}") self.logger.debug2("checking if node 0x%s is present", node_hash.hex()) if node_hash not in self._db: # Instead of immediately storing predicted nodes, we keep them in memory # So when we check if a node is available, we also check if prediction is in memory if node_hash in self._predicted_nodes: # Part of the benefit is that we can identify how effective our predictions are self._prediction_successes += 1 # Now we store the predictive node in the database self._db[node_hash] = self._predicted_nodes.pop(node_hash) return False else: return True else: return False
def humanize_hash(value: Hash32) -> str: value_as_hex = value.hex() head = value_as_hex[:DISPLAY_HASH_CHARS] tail = value_as_hex[-1 * DISPLAY_HASH_CHARS:] return "{0}..{1}".format(head, tail)