more fixes on dht functionals

This commit is contained in:
Victor Shyba 2018-07-21 18:19:30 -03:00 committed by Jack Robison
parent e1314a9d1e
commit e1e7be63b8
No known key found for this signature in database
GPG key ID: DF25C68FE0239BB2
5 changed files with 21 additions and 17 deletions

View file

@ -396,7 +396,7 @@ class KademliaProtocol(protocol.DatagramProtocol):
def _sendError(self, contact, rpcID, exceptionType, exceptionMessage): def _sendError(self, contact, rpcID, exceptionType, exceptionMessage):
""" Send an RPC error message to the specified contact """ 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) msg = msgtypes.ErrorMessage(rpcID, self._node.node_id, exceptionType, exceptionMessage)
msgPrimitive = self._translator.toPrimitive(msg) msgPrimitive = self._translator.toPrimitive(msg)
encodedMsg = self._encoder.encode(msgPrimitive) encodedMsg = self._encoder.encode(msgPrimitive)

View file

@ -143,7 +143,6 @@ class TestKademliaBase(unittest.TestCase):
for node in self.nodes: for node in self.nodes:
contact_addresses = {contact.address for contact in node.contacts} contact_addresses = {contact.address for contact in node.contacts}
routable.update(contact_addresses) routable.update(contact_addresses)
print(routable, node_addresses)
self.assertSetEqual(routable, node_addresses) self.assertSetEqual(routable, node_addresses)
@defer.inlineCallbacks @defer.inlineCallbacks

View file

@ -1,6 +1,8 @@
import struct import struct
import hashlib import hashlib
import logging import logging
from binascii import unhexlify, hexlify
from twisted.internet import defer, error from twisted.internet import defer, error
from lbrynet.dht.encoding import Bencode from lbrynet.dht.encoding import Bencode
from lbrynet.dht.error import DecodeError from lbrynet.dht.error import DecodeError
@ -16,9 +18,9 @@ _datagram_formatter = DefaultFormat()
log = logging.getLogger() log = logging.getLogger()
MOCK_DHT_NODES = [ MOCK_DHT_NODES = [
b"cc8db9d0dd9b65b103594b5f992adf09f18b310958fa451d61ce8d06f3ee97a91461777c2b7dea1a89d02d2f23eb0e4f", unhexlify("cc8db9d0dd9b65b103594b5f992adf09f18b310958fa451d61ce8d06f3ee97a91461777c2b7dea1a89d02d2f23eb0e4f"),
b"83a3a398eead3f162fbbe1afb3d63482bb5b6d3cdd8f9b0825c1dfa58dffd3f6f6026d6e64d6d4ae4c3dfe2262e734ba", unhexlify("83a3a398eead3f162fbbe1afb3d63482bb5b6d3cdd8f9b0825c1dfa58dffd3f6f6026d6e64d6d4ae4c3dfe2262e734ba"),
b"b6928ff25778a7bbb5d258d3b3a06e26db1654f3d2efce8c26681d43f7237cdf2e359a4d309c4473d5d89ec99fb4f573", unhexlify("b6928ff25778a7bbb5d258d3b3a06e26db1654f3d2efce8c26681d43f7237cdf2e359a4d309c4473d5d89ec99fb4f573"),
] ]
MOCK_DHT_SEED_DNS = { # these map to mock nodes 0, 1, and 2 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 break
if num >= len(mock_node_ids): if num >= len(mock_node_ids):
h = hashlib.sha384() h = hashlib.sha384()
h.update(b"node %i" % num) h.update(("node %i" % num).encode())
node_id = h.digest() node_id = h.digest()
else: else:
node_id = mock_node_ids[num] node_id = mock_node_ids[num]

View file

@ -1,8 +1,9 @@
from lbrynet.dht import constants from lbrynet.dht import constants
from lbrynet.dht.distance import Distance from lbrynet.dht.distance import Distance
from dht_test_environment import TestKademliaBase
import logging import logging
from tests.functional.dht.dht_test_environment import TestKademliaBase
log = logging.getLogger() log = logging.getLogger()
@ -14,14 +15,14 @@ class TestFindNode(TestKademliaBase):
network_size = 35 network_size = 35
def test_find_node(self): def test_find_node(self):
last_node_id = self.nodes[-1].node_id.encode('hex') last_node_id = self.nodes[-1].node_id
to_last_node = Distance(last_node_id.decode('hex')) to_last_node = Distance(last_node_id)
for n in self.nodes: 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) self.assertTrue(len(find_close_nodes_result) == constants.k)
found_ids = [c.id.encode('hex') for c in find_close_nodes_result] 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.decode('hex')))) self.assertListEqual(found_ids, sorted(found_ids, key=lambda x: to_last_node(x)))
if last_node_id in [c.id.encode('hex') for c in n.contacts]: if last_node_id in [c.id for c in n.contacts]:
self.assertTrue(found_ids[0] == last_node_id) self.assertTrue(found_ids[0] == last_node_id)
else: else:
self.assertTrue(last_node_id not in found_ids) self.assertTrue(last_node_id not in found_ids)

View file

@ -1,8 +1,10 @@
import struct import struct
from binascii import hexlify
from twisted.internet import defer from twisted.internet import defer
from lbrynet.dht import constants from lbrynet.dht import constants
from lbrynet.core.utils import generate_id from lbrynet.core.utils import generate_id
from dht_test_environment import TestKademliaBase from .dht_test_environment import TestKademliaBase
import logging import logging
log = logging.getLogger() log = logging.getLogger()
@ -22,7 +24,7 @@ class TestStoreExpiration(TestKademliaBase):
all_nodes = set(self.nodes).union(set(self._seeds)) all_nodes = set(self.nodes).union(set(self._seeds))
# verify the nodes we think stored it did actually store it # 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), len(storing_node_ids))
self.assertEqual(len(storing_nodes), constants.k) self.assertEqual(len(storing_nodes), constants.k)
for node in storing_nodes: for node in storing_nodes:
@ -35,7 +37,7 @@ class TestStoreExpiration(TestKademliaBase):
self.assertEqual(len(datastore_result), 1) self.assertEqual(len(datastore_result), 1)
expanded_peers = [] expanded_peers = []
for peer in datastore_result: 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]) port, = struct.unpack('>H', peer[4:6])
peer_node_id = peer[6:] peer_node_id = peer[6:]
if (host, port, peer_node_id) not in expanded_peers: if (host, port, peer_node_id) not in expanded_peers:
@ -85,7 +87,7 @@ class TestStoreExpiration(TestKademliaBase):
self.assertEqual(len(datastore_result), 1) self.assertEqual(len(datastore_result), 1)
expanded_peers = [] expanded_peers = []
for peer in datastore_result: 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]) port, = struct.unpack('>H', peer[4:6])
peer_node_id = peer[6:] peer_node_id = peer[6:]
if (host, port, peer_node_id) not in expanded_peers: if (host, port, peer_node_id) not in expanded_peers: