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):
""" 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)

View file

@ -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

View file

@ -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]

View file

@ -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)

View file

@ -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: