handle attempts to add peers with no node id and issue a warning

This commit is contained in:
Victor Shyba 2019-10-01 18:22:19 -03:00
parent 624f986978
commit 05a28ea346
2 changed files with 14 additions and 0 deletions

View file

@ -323,6 +323,9 @@ class KademliaProtocol(DatagramProtocol):
return args, {}
async def _add_peer(self, peer: 'KademliaPeer'):
if not peer.node_id:
log.warning("Tried adding a peer with no node id!")
return False
for p in self.routing_table.get_peers():
if (p.address, p.udp_port) == (peer.address, peer.udp_port) and p.node_id != peer.node_id:
self.routing_table.remove_peer(p)

View file

@ -28,6 +28,7 @@ expected_ranges = [
)
]
class TestRouting(AsyncioTestCase):
async def test_fill_one_bucket(self):
loop = asyncio.get_event_loop()
@ -65,6 +66,16 @@ class TestRouting(AsyncioTestCase):
for node in nodes.values():
node.protocol.stop()
async def test_cant_add_peer_without_a_node_id_gracefully(self):
loop = asyncio.get_event_loop()
node = Node(loop, PeerManager(loop), constants.generate_id(), 4444, 4444, 3333, '1.2.3.4')
bad_peer = make_kademlia_peer(None, '1.2.3.4', 5555)
with self.assertLogs(level='WARNING') as logged:
self.assertFalse(await node.protocol._add_peer(bad_peer))
self.assertEqual(1, len(logged.output))
self.assertTrue(logged.output[0].endswith('Tried adding a peer with no node id!'))
async def test_split_buckets(self):
loop = asyncio.get_event_loop()
peer_addresses = [