From e1e7be63b88857517795c38d923f12854c9da8b8 Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Sat, 21 Jul 2018 18:19:30 -0300 Subject: [PATCH] more fixes on dht functionals --- lbrynet/dht/protocol.py | 2 +- tests/functional/dht/dht_test_environment.py | 1 - tests/functional/dht/mock_transport.py | 10 ++++++---- tests/functional/dht/test_iterative_find.py | 15 ++++++++------- tests/functional/dht/test_store.py | 10 ++++++---- 5 files changed, 21 insertions(+), 17 deletions(-) diff --git a/lbrynet/dht/protocol.py b/lbrynet/dht/protocol.py index 9f6dbe391..464a254ca 100644 --- a/lbrynet/dht/protocol.py +++ b/lbrynet/dht/protocol.py @@ -396,7 +396,7 @@ class KademliaProtocol(protocol.DatagramProtocol): def _sendError(self, contact, rpcID, exceptionType, exceptionMessage): """ Send an RPC error message to the specified contact """ - exceptionType, exceptionMessage = exceptionType.encode(), exceptionMessage.encode() + exceptionMessage = exceptionMessage.encode() msg = msgtypes.ErrorMessage(rpcID, self._node.node_id, exceptionType, exceptionMessage) msgPrimitive = self._translator.toPrimitive(msg) encodedMsg = self._encoder.encode(msgPrimitive) diff --git a/tests/functional/dht/dht_test_environment.py b/tests/functional/dht/dht_test_environment.py index e7cdf7c55..3bda2a981 100644 --- a/tests/functional/dht/dht_test_environment.py +++ b/tests/functional/dht/dht_test_environment.py @@ -143,7 +143,6 @@ class TestKademliaBase(unittest.TestCase): for node in self.nodes: contact_addresses = {contact.address for contact in node.contacts} routable.update(contact_addresses) - print(routable, node_addresses) self.assertSetEqual(routable, node_addresses) @defer.inlineCallbacks diff --git a/tests/functional/dht/mock_transport.py b/tests/functional/dht/mock_transport.py index deb1ea8be..f02efb1aa 100644 --- a/tests/functional/dht/mock_transport.py +++ b/tests/functional/dht/mock_transport.py @@ -1,6 +1,8 @@ import struct import hashlib import logging +from binascii import unhexlify, hexlify + from twisted.internet import defer, error from lbrynet.dht.encoding import Bencode from lbrynet.dht.error import DecodeError @@ -16,9 +18,9 @@ _datagram_formatter = DefaultFormat() log = logging.getLogger() MOCK_DHT_NODES = [ - b"cc8db9d0dd9b65b103594b5f992adf09f18b310958fa451d61ce8d06f3ee97a91461777c2b7dea1a89d02d2f23eb0e4f", - b"83a3a398eead3f162fbbe1afb3d63482bb5b6d3cdd8f9b0825c1dfa58dffd3f6f6026d6e64d6d4ae4c3dfe2262e734ba", - b"b6928ff25778a7bbb5d258d3b3a06e26db1654f3d2efce8c26681d43f7237cdf2e359a4d309c4473d5d89ec99fb4f573", + unhexlify("cc8db9d0dd9b65b103594b5f992adf09f18b310958fa451d61ce8d06f3ee97a91461777c2b7dea1a89d02d2f23eb0e4f"), + unhexlify("83a3a398eead3f162fbbe1afb3d63482bb5b6d3cdd8f9b0825c1dfa58dffd3f6f6026d6e64d6d4ae4c3dfe2262e734ba"), + unhexlify("b6928ff25778a7bbb5d258d3b3a06e26db1654f3d2efce8c26681d43f7237cdf2e359a4d309c4473d5d89ec99fb4f573"), ] MOCK_DHT_SEED_DNS = { # these map to mock nodes 0, 1, and 2 @@ -125,7 +127,7 @@ def mock_node_generator(count=None, mock_node_ids=MOCK_DHT_NODES): break if num >= len(mock_node_ids): h = hashlib.sha384() - h.update(b"node %i" % num) + h.update(("node %i" % num).encode()) node_id = h.digest() else: node_id = mock_node_ids[num] diff --git a/tests/functional/dht/test_iterative_find.py b/tests/functional/dht/test_iterative_find.py index f38caf604..3bfd2492a 100644 --- a/tests/functional/dht/test_iterative_find.py +++ b/tests/functional/dht/test_iterative_find.py @@ -1,8 +1,9 @@ from lbrynet.dht import constants from lbrynet.dht.distance import Distance -from dht_test_environment import TestKademliaBase import logging +from tests.functional.dht.dht_test_environment import TestKademliaBase + log = logging.getLogger() @@ -14,14 +15,14 @@ class TestFindNode(TestKademliaBase): network_size = 35 def test_find_node(self): - last_node_id = self.nodes[-1].node_id.encode('hex') - to_last_node = Distance(last_node_id.decode('hex')) + last_node_id = self.nodes[-1].node_id + to_last_node = Distance(last_node_id) for n in self.nodes: - find_close_nodes_result = n._routingTable.findCloseNodes(last_node_id.decode('hex'), constants.k) + find_close_nodes_result = n._routingTable.findCloseNodes(last_node_id, constants.k) self.assertTrue(len(find_close_nodes_result) == constants.k) - found_ids = [c.id.encode('hex') for c in find_close_nodes_result] - self.assertListEqual(found_ids, sorted(found_ids, key=lambda x: to_last_node(x.decode('hex')))) - if last_node_id in [c.id.encode('hex') for c in n.contacts]: + found_ids = [c.id for c in find_close_nodes_result] + self.assertListEqual(found_ids, sorted(found_ids, key=lambda x: to_last_node(x))) + if last_node_id in [c.id for c in n.contacts]: self.assertTrue(found_ids[0] == last_node_id) else: self.assertTrue(last_node_id not in found_ids) diff --git a/tests/functional/dht/test_store.py b/tests/functional/dht/test_store.py index 6ae608cbf..00cdbb443 100644 --- a/tests/functional/dht/test_store.py +++ b/tests/functional/dht/test_store.py @@ -1,8 +1,10 @@ import struct +from binascii import hexlify + from twisted.internet import defer from lbrynet.dht import constants from lbrynet.core.utils import generate_id -from dht_test_environment import TestKademliaBase +from .dht_test_environment import TestKademliaBase import logging log = logging.getLogger() @@ -22,7 +24,7 @@ class TestStoreExpiration(TestKademliaBase): all_nodes = set(self.nodes).union(set(self._seeds)) # verify the nodes we think stored it did actually store it - storing_nodes = [node for node in all_nodes if node.node_id.encode('hex') in storing_node_ids] + storing_nodes = [node for node in all_nodes if hexlify(node.node_id) in storing_node_ids] self.assertEqual(len(storing_nodes), len(storing_node_ids)) self.assertEqual(len(storing_nodes), constants.k) for node in storing_nodes: @@ -35,7 +37,7 @@ class TestStoreExpiration(TestKademliaBase): self.assertEqual(len(datastore_result), 1) expanded_peers = [] for peer in datastore_result: - host = ".".join([str(ord(d)) for d in peer[:4]]) + host = ".".join([str(d) for d in peer[:4]]) port, = struct.unpack('>H', peer[4:6]) peer_node_id = peer[6:] if (host, port, peer_node_id) not in expanded_peers: @@ -85,7 +87,7 @@ class TestStoreExpiration(TestKademliaBase): self.assertEqual(len(datastore_result), 1) expanded_peers = [] for peer in datastore_result: - host = ".".join([str(ord(d)) for d in peer[:4]]) + host = ".".join([str(d) for d in peer[:4]]) port, = struct.unpack('>H', peer[4:6]) peer_node_id = peer[6:] if (host, port, peer_node_id) not in expanded_peers: