diff --git a/lbry/dht/node.py b/lbry/dht/node.py index 745789e89..d44ce5cd9 100644 --- a/lbry/dht/node.py +++ b/lbry/dht/node.py @@ -1,7 +1,6 @@ import logging import asyncio import typing -import binascii import socket from lbry.utils import resolve_host from lbry.dht import constants @@ -80,7 +79,7 @@ class Node: await fut async def announce_blob(self, blob_hash: str) -> typing.List[bytes]: - hash_value = binascii.unhexlify(blob_hash.encode()) + hash_value = bytes.fromhex(blob_hash) assert len(hash_value) == constants.HASH_LENGTH peers = await self.peer_search(hash_value) @@ -95,7 +94,7 @@ class Node: stored_to = [node_id for node_id, contacted in stored_to_tup if contacted] if stored_to: log.debug( - "Stored %s to %i of %i attempted peers", binascii.hexlify(hash_value).decode()[:8], + "Stored %s to %i of %i attempted peers", hash_value.hex()[:8], len(stored_to), len(peers) ) else: @@ -223,7 +222,7 @@ class Node: # prioritize peers who reply to a dht ping first # this minimizes attempting to make tcp connections that won't work later to dead or unreachable peers - async for results in self.get_iterative_value_finder(binascii.unhexlify(blob_hash.encode())): + async for results in self.get_iterative_value_finder(bytes.fromhex(blob_hash)): to_put = [] for peer in results: if peer.address == self.protocol.external_ip and self.protocol.peer_port == peer.tcp_port: diff --git a/lbry/dht/peer.py b/lbry/dht/peer.py index b1abaa8fb..c69f5001c 100644 --- a/lbry/dht/peer.py +++ b/lbry/dht/peer.py @@ -1,7 +1,6 @@ import typing import asyncio import logging -from binascii import hexlify from dataclasses import dataclass, field from functools import lru_cache from lbry.utils import is_valid_public_ipv4 as _is_valid_public_ipv4, LRUCache @@ -154,7 +153,7 @@ class KademliaPeer: def __post_init__(self): if self._node_id is not None: if not len(self._node_id) == constants.HASH_LENGTH: - raise ValueError("invalid node_id: {}".format(hexlify(self._node_id).decode())) + raise ValueError("invalid node_id: {}".format(self._node_id.hex())) if self.udp_port is not None and not 1024 <= self.udp_port <= 65535: raise ValueError(f"invalid udp port: {self.address}:{self.udp_port}") if self.tcp_port is not None and not 1024 <= self.tcp_port <= 65535: diff --git a/lbry/dht/protocol/iterative_find.py b/lbry/dht/protocol/iterative_find.py index 7d49996f9..9e11438bd 100644 --- a/lbry/dht/protocol/iterative_find.py +++ b/lbry/dht/protocol/iterative_find.py @@ -1,5 +1,4 @@ import asyncio -from binascii import hexlify from itertools import chain from collections import defaultdict import typing @@ -198,7 +197,7 @@ class IterativeFinder: added += 1 log.debug("running %d probes", len(self.running_probes)) if not added and not self.running_probes: - log.debug("search for %s exhausted", hexlify(self.key)[:8]) + log.debug("search for %s exhausted", self.key.hex()[:8]) self.search_exhausted() def _schedule_probe(self, peer: 'KademliaPeer'): @@ -271,7 +270,7 @@ class IterativeNodeFinder(IterativeFinder): self.yielded_peers: typing.Set['KademliaPeer'] = set() async def send_probe(self, peer: 'KademliaPeer') -> FindNodeResponse: - log.debug("probing %s:%d %s", peer.address, peer.udp_port, hexlify(peer.node_id)[:8] if peer.node_id else '') + log.debug("probing %s:%d %s", peer.address, peer.udp_port, peer.node_id.hex()[:8] if peer.node_id else '') response = await self.protocol.get_rpc_peer(peer).find_node(self.key) return FindNodeResponse(self.key, response) diff --git a/lbry/dht/protocol/protocol.py b/lbry/dht/protocol/protocol.py index d6c0d3347..66165740b 100644 --- a/lbry/dht/protocol/protocol.py +++ b/lbry/dht/protocol/protocol.py @@ -4,7 +4,6 @@ import functools import hashlib import asyncio import typing -import binascii import random from asyncio.protocols import DatagramProtocol from asyncio.transports import DatagramTransport @@ -97,7 +96,7 @@ class KademliaRPC: if not rpc_contact.tcp_port or peer.compact_address_tcp() != rpc_contact.compact_address_tcp() ] # if we don't have k storing peers to return and we have this hash locally, include our contact information - if len(peers) < constants.K and binascii.hexlify(key).decode() in self.protocol.data_store.completed_blobs: + if len(peers) < constants.K and key.hex() in self.protocol.data_store.completed_blobs: peers.append(self.compact_address()) if not peers: response[PAGE_KEY] = 0 @@ -415,8 +414,8 @@ class KademliaProtocol(DatagramProtocol): self._wakeup_routing_task.clear() def _handle_rpc(self, sender_contact: 'KademliaPeer', message: RequestDatagram): - assert sender_contact.node_id != self.node_id, (binascii.hexlify(sender_contact.node_id)[:8].decode(), - binascii.hexlify(self.node_id)[:8].decode()) + assert sender_contact.node_id != self.node_id, (sender_contact.node_id.hex()[:8], + self.node_id.hex()[:8]) method = message.method if method not in [b'ping', b'store', b'findNode', b'findValue']: raise AttributeError('Invalid method: %s' % message.method.decode()) @@ -561,7 +560,7 @@ class KademliaProtocol(DatagramProtocol): message = decode_datagram(datagram) except (ValueError, TypeError, DecodeError): self.peer_manager.report_failure(address[0], address[1]) - log.warning("Couldn't decode dht datagram from %s: %s", address, binascii.hexlify(datagram).decode()) + log.warning("Couldn't decode dht datagram from %s: %s", address, datagram.hex()) return if isinstance(message, RequestDatagram): @@ -603,7 +602,7 @@ class KademliaProtocol(DatagramProtocol): if len(data) > constants.MSG_SIZE_LIMIT: log.warning("cannot send datagram larger than %i bytes (packet is %i bytes)", constants.MSG_SIZE_LIMIT, len(data)) - log.debug("Packet is too large to send: %s", binascii.hexlify(data[:3500]).decode()) + log.debug("Packet is too large to send: %s", data[:3500].hex()) raise ValueError( f"cannot send datagram larger than {constants.MSG_SIZE_LIMIT} bytes (packet is {len(data)} bytes)" ) @@ -663,13 +662,13 @@ class KademliaProtocol(DatagramProtocol): res = await self.get_rpc_peer(peer).store(hash_value) if res != b"OK": raise ValueError(res) - log.debug("Stored %s to %s", binascii.hexlify(hash_value).decode()[:8], peer) + log.debug("Stored %s to %s", hash_value.hex()[:8], peer) return peer.node_id, True try: return await __store() except asyncio.TimeoutError: - log.debug("Timeout while storing blob_hash %s at %s", binascii.hexlify(hash_value).decode()[:8], peer) + log.debug("Timeout while storing blob_hash %s at %s", hash_value.hex()[:8], peer) return peer.node_id, False except ValueError as err: log.error("Unexpected response: %s", err)