From 740654b563d6f103a48e33eaee8f0c67c6363c33 Mon Sep 17 00:00:00 2001 From: Lee Miller Date: Fri, 20 Oct 2023 05:41:10 +0300 Subject: [PATCH] Make tuples from sets before taking random samples --- minode/connection.py | 10 +++++----- minode/manager.py | 27 +++++++++++++++------------ 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/minode/connection.py b/minode/connection.py index ded5ae3..c74af5d 100644 --- a/minode/connection.py +++ b/minode/connection.py @@ -266,11 +266,11 @@ class Connection(threading.Thread): if len(shared.node_pool) > 10: addr.update({ structure.NetAddr(1, a[0], a[1]) - for a in random.sample(shared.node_pool, 10)}) + for a in random.sample(tuple(shared.node_pool), 10)}) if len(shared.unchecked_node_pool) > 10: addr.update({ structure.NetAddr(1, a[0], a[1]) - for a in random.sample(shared.unchecked_node_pool, 10)}) + for a in random.sample(tuple(shared.unchecked_node_pool), 10)}) if len(addr) != 0: self.send_queue.put(message.Addr(addr)) @@ -284,7 +284,7 @@ class Connection(threading.Thread): # We limit size of inv messaged to 10000 entries # because they might time out # in very slow networks (I2P) - pack = random.sample(to_send, 10000) + pack = random.sample(tuple(to_send), 10000) self.send_queue.put(message.Inv(pack)) to_send.difference_update(pack) else: @@ -456,7 +456,7 @@ class Connection(threading.Thread): logging.info( 'Queued %s vectors to get', len(self.vectors_to_get)) if len(self.vectors_to_get) > 64: - pack = random.sample(self.vectors_to_get, 64) + pack = random.sample(tuple(self.vectors_to_get), 64) self.send_queue.put(message.GetData(pack)) self.vectors_requested.update({ vector: time.time() for vector in pack @@ -486,7 +486,7 @@ class Connection(threading.Thread): logging.info( 'Preparing to send %s objects', len(self.vectors_to_send)) if len(self.vectors_to_send) > 16: - to_send = random.sample(self.vectors_to_send, 16) + to_send = random.sample(tuple(self.vectors_to_send), 16) self.vectors_to_send.difference_update(to_send) else: to_send = self.vectors_to_send.copy() diff --git a/minode/manager.py b/minode/manager.py index 3ff5f00..93a3f30 100644 --- a/minode/manager.py +++ b/minode/manager.py @@ -98,24 +98,26 @@ class Manager(threading.Thread): if shared.ip_enabled: if len(shared.unchecked_node_pool) > 16: to_connect.update(random.sample( - shared.unchecked_node_pool, 16)) + tuple(shared.unchecked_node_pool), 16)) else: to_connect.update(shared.unchecked_node_pool) shared.unchecked_node_pool.difference_update(to_connect) if len(shared.node_pool) > 8: - to_connect.update(random.sample(shared.node_pool, 8)) + to_connect.update(random.sample( + tuple(shared.node_pool), 8)) else: to_connect.update(shared.node_pool) if shared.i2p_enabled: if len(shared.i2p_unchecked_node_pool) > 16: - to_connect.update( - random.sample(shared.i2p_unchecked_node_pool, 16)) + to_connect.update(random.sample( + tuple(shared.i2p_unchecked_node_pool), 16)) else: to_connect.update(shared.i2p_unchecked_node_pool) shared.i2p_unchecked_node_pool.difference_update(to_connect) if len(shared.i2p_node_pool) > 8: - to_connect.update(random.sample(shared.i2p_node_pool, 8)) + to_connect.update(random.sample( + tuple(shared.i2p_node_pool), 8)) else: to_connect.update(shared.i2p_node_pool) @@ -214,17 +216,18 @@ class Manager(threading.Thread): @staticmethod def pickle_nodes(): if len(shared.node_pool) > 10000: - shared.node_pool = set(random.sample(shared.node_pool, 10000)) + shared.node_pool = set(random.sample( + tuple(shared.node_pool), 10000)) if len(shared.unchecked_node_pool) > 1000: - shared.unchecked_node_pool = set( - random.sample(shared.unchecked_node_pool, 1000)) + shared.unchecked_node_pool = set(random.sample( + tuple(shared.unchecked_node_pool), 1000)) if len(shared.i2p_node_pool) > 1000: - shared.i2p_node_pool = set( - random.sample(shared.i2p_node_pool, 1000)) + shared.i2p_node_pool = set(random.sample( + tuple(shared.i2p_node_pool), 1000)) if len(shared.i2p_unchecked_node_pool) > 100: - shared.i2p_unchecked_node_pool = set( - random.sample(shared.i2p_unchecked_node_pool, 100)) + shared.i2p_unchecked_node_pool = set(random.sample( + tuple(shared.i2p_unchecked_node_pool), 100)) try: with open(