Merge branch 'fix_peer_ping'

This commit is contained in:
Jack Robison 2018-10-01 08:32:30 -04:00
commit d0b739a0b9
No known key found for this signature in database
GPG key ID: DF25C68FE0239BB2
2 changed files with 16 additions and 12 deletions

View file

@ -451,20 +451,10 @@ class Node(MockKademliaHelper):
@rtype: twisted.internet.defer.Deferred @rtype: twisted.internet.defer.Deferred
""" """
try: try:
contact = self._routingTable.getContact(contactID) df = defer.succeed(self._routingTable.getContact(contactID))
df = defer.Deferred()
df.callback(contact)
except (ValueError, IndexError): except (ValueError, IndexError):
def parseResults(nodes):
node_ids = [c.id for c in nodes]
if contactID in nodes:
contact = nodes[node_ids.index(contactID)]
return contact
else:
return None
df = self.iterativeFindNode(contactID) df = self.iterativeFindNode(contactID)
df.addCallback(parseResults) df.addCallback(lambda nodes: ([node for node in nodes if node.id == contactID] or (None,))[0])
return df return df
@rpcmethod @rpcmethod

View file

@ -269,3 +269,17 @@ class KademliaProtocolTest(unittest.TestCase):
self.assertTrue(self.node._dataStore.hasPeersForBlob(fake_blob)) self.assertTrue(self.node._dataStore.hasPeersForBlob(fake_blob))
self.assertEqual(len(self.node._dataStore.getStoringContacts()), 1) self.assertEqual(len(self.node._dataStore.getStoringContacts()), 1)
self.assertIs(self.node._dataStore.getStoringContacts()[0], self.remote_contact) self.assertIs(self.node._dataStore.getStoringContacts()[0], self.remote_contact)
@defer.inlineCallbacks
def test_find_node(self):
self.node.addContact(self.node.contact_manager.make_contact(
self.remote_contact.id, self.remote_contact.address, self.remote_contact.port, self.node._protocol)
)
result = self.node.findContact(b'0'*48)
for _ in range(6):
self._reactor.advance(1)
self.assertEqual((yield result), None)
result = self.node.findContact(self.remote_contact.id)
for _ in range(6):
self._reactor.advance(1)
self.assertEqual((yield result).id, self.remote_contact.id)