Skip to content

Commit

Permalink
Reduced helper_randon dependency from network module
Browse files Browse the repository at this point in the history
  • Loading branch information
anand-skss committed Jun 21, 2024
1 parent 205e253 commit e578759
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/network/addrthread.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -29,9 +29,9 @@ def run(self):
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
Expand Down
12 changes: 6 additions & 6 deletions src/network/asyncore_pollchoose.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import os
import select
import socket
import random
import sys
import time
import warnings
Expand All @@ -19,7 +20,6 @@
)
from threading import current_thread

import helper_random

try:
from errno import WSAEWOULDBLOCK
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/network/connectionpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import socket
import sys
import time
import random

import asyncore_pollchoose as asyncore
import helper_random
import knownnodes
import protocol
import state
Expand Down Expand Up @@ -210,7 +210,7 @@ def startBootstrappers(self):
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':
Expand All @@ -222,7 +222,7 @@ def startBootstrappers(self):

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
Expand Down Expand Up @@ -289,7 +289,7 @@ def loop(self): # pylint: disable=too-many-branches,too-many-statements
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:
Expand Down
4 changes: 2 additions & 2 deletions src/network/downloadthread.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -43,7 +43,7 @@ def run(self):
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
Expand Down
3 changes: 1 addition & 2 deletions src/network/tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

# magic imports!
import addresses
import helper_random
import l10n
import protocol
import state
Expand Down Expand Up @@ -201,7 +200,7 @@ def sendAddr(self):
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"]))
Expand Down
4 changes: 2 additions & 2 deletions src/network/uploadthread.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"""
import time

import helper_random
import random
import protocol
import state
import connectionpool
Expand All @@ -24,7 +24,7 @@ def run(self):
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
Expand Down

0 comments on commit e578759

Please sign in to comment.