From e578759a3fa5f81859be6887b63583dda42c1896 Mon Sep 17 00:00:00 2001 From: anand k Date: Thu, 13 Jun 2024 07:03:36 +0530 Subject: [PATCH] Reduced helper_randon dependency from network module --- src/network/addrthread.py | 6 +++--- src/network/asyncore_pollchoose.py | 12 ++++++------ src/network/connectionpool.py | 8 ++++---- src/network/downloadthread.py | 4 ++-- src/network/tcp.py | 3 +-- src/network/uploadthread.py | 4 ++-- 6 files changed, 18 insertions(+), 19 deletions(-) diff --git a/src/network/addrthread.py b/src/network/addrthread.py index a0e869e3..a77e609c 100644 --- a/src/network/addrthread.py +++ b/src/network/addrthread.py @@ -1,11 +1,11 @@ """ Announce addresses as they are received from other hosts """ +import random from six.moves import queue # magic imports! import connectionpool -from helper_random import randomshuffle from protocol import assembleAddrMessage from queues import addrQueue # FIXME: init with queue @@ -29,9 +29,9 @@ class AddrThread(StoppableThread): if chunk: # Choose peers randomly connections = connectionpool.pool.establishedConnections() - randomshuffle(connections) + random.shuffle(connections) for i in connections: - randomshuffle(chunk) + random.shuffle(chunk) filtered = [] for stream, peer, seen, destination in chunk: # peer's own address or address received from peer diff --git a/src/network/asyncore_pollchoose.py b/src/network/asyncore_pollchoose.py index bdd312c6..9d0ffc1b 100644 --- a/src/network/asyncore_pollchoose.py +++ b/src/network/asyncore_pollchoose.py @@ -9,6 +9,7 @@ Basic infrastructure for asynchronous socket service clients and servers. import os import select import socket +import random import sys import time import warnings @@ -19,7 +20,6 @@ from errno import ( ) from threading import current_thread -import helper_random try: from errno import WSAEWOULDBLOCK @@ -233,13 +233,13 @@ def select_poller(timeout=0.0, map=None): if err.args[0] in (WSAENOTSOCK, ): return - for fd in helper_random.randomsample(r, len(r)): + for fd in random.sample(r, len(r)): obj = map.get(fd) if obj is None: continue read(obj) - for fd in helper_random.randomsample(w, len(w)): + for fd in random.sample(w, len(w)): obj = map.get(fd) if obj is None: continue @@ -297,7 +297,7 @@ def poll_poller(timeout=0.0, map=None): except socket.error as err: if err.args[0] in (EBADF, WSAENOTSOCK, EINTR): return - for fd, flags in helper_random.randomsample(r, len(r)): + for fd, flags in random.sample(r, len(r)): obj = map.get(fd) if obj is None: continue @@ -357,7 +357,7 @@ def epoll_poller(timeout=0.0, map=None): if err.args[0] != EINTR: raise r = [] - for fd, flags in helper_random.randomsample(r, len(r)): + for fd, flags in random.sample(r, len(r)): obj = map.get(fd) if obj is None: continue @@ -420,7 +420,7 @@ def kqueue_poller(timeout=0.0, map=None): events = kqueue_poller.pollster.control(updates, selectables, timeout) if len(events) > 1: - events = helper_random.randomsample(events, len(events)) + events = random.sample(events, len(events)) for event in events: fd = event.ident diff --git a/src/network/connectionpool.py b/src/network/connectionpool.py index 36c91c18..519b7b67 100644 --- a/src/network/connectionpool.py +++ b/src/network/connectionpool.py @@ -7,9 +7,9 @@ import re import socket import sys import time +import random import asyncore_pollchoose as asyncore -import helper_random import knownnodes import protocol import state @@ -210,7 +210,7 @@ class BMConnectionPool(object): connection_base = TCPConnection elif proxy_type == 'SOCKS5': connection_base = Socks5BMConnection - hostname = helper_random.randomchoice([ + hostname = random.choice([ # nosec B311 'quzwelsuziwqgpt2.onion', None ]) elif proxy_type == 'SOCKS4a': @@ -222,7 +222,7 @@ class BMConnectionPool(object): bootstrapper = bootstrap(connection_base) if not hostname: - port = helper_random.randomchoice([8080, 8444]) + port = random.choice([8080, 8444]) # nosec B311 hostname = 'bootstrap%s.bitmessage.org' % port else: port = 8444 @@ -289,7 +289,7 @@ class BMConnectionPool(object): state.maximumNumberOfHalfOpenConnections - pending): try: chosen = self.trustedPeer or chooseConnection( - helper_random.randomchoice(self.streams)) + random.choice(self.streams)) # nosec B311 except ValueError: continue if chosen in self.outboundConnections: diff --git a/src/network/downloadthread.py b/src/network/downloadthread.py index 30a3f2fe..7c8bccb6 100644 --- a/src/network/downloadthread.py +++ b/src/network/downloadthread.py @@ -2,9 +2,9 @@ `DownloadThread` class definition """ import time +import random import state import addresses -import helper_random import protocol import connectionpool from network import dandelion_ins @@ -43,7 +43,7 @@ class DownloadThread(StoppableThread): requested = 0 # Choose downloading peers randomly connections = connectionpool.pool.establishedConnections() - helper_random.randomshuffle(connections) + random.shuffle(connections) requestChunk = max(int( min(self.maxRequestChunk, len(missingObjects)) / len(connections)), 1) if connections else 1 diff --git a/src/network/tcp.py b/src/network/tcp.py index a739e256..f2dce07d 100644 --- a/src/network/tcp.py +++ b/src/network/tcp.py @@ -11,7 +11,6 @@ import time # magic imports! import addresses -import helper_random import l10n import protocol import state @@ -201,7 +200,7 @@ class TCPConnection(BMProto, TLSDispatcher): elemCount = min( len(filtered), maxAddrCount / 2 if n else maxAddrCount) - addrs[s] = helper_random.randomsample(filtered, elemCount) + addrs[s] = random.sample(filtered, elemCount) for substream in addrs: for peer, params in addrs[substream]: templist.append((substream, peer, params["lastseen"])) diff --git a/src/network/uploadthread.py b/src/network/uploadthread.py index e91f08fa..60209832 100644 --- a/src/network/uploadthread.py +++ b/src/network/uploadthread.py @@ -3,7 +3,7 @@ """ import time -import helper_random +import random import protocol import state import connectionpool @@ -24,7 +24,7 @@ class UploadThread(StoppableThread): uploaded = 0 # Choose uploading peers randomly connections = connectionpool.pool.establishedConnections() - helper_random.randomshuffle(connections) + random.shuffle(connections) for i in connections: now = time.time() # avoid unnecessary delay -- 2.45.1