Skip to content

Commit 5a85515

Browse files
committed
Network refactoring
- use dependency injection to reduce circular imports
1 parent 2c0a0eb commit 5a85515

16 files changed

Lines changed: 91 additions & 74 deletions

‎src/api.py‎

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,6 @@
9090
sqlStoredProcedure)
9191
from highlevelcrypto import calculateInventoryHash
9292

93-
try:
94-
from network import connectionpool
95-
except ImportError:
96-
connectionpool = None
97-
9893
from network import StoppableThread, invQueue, stats
9994
from version import softwareVersion
10095

@@ -1551,18 +1546,18 @@ def HandleListConnections(self):
15511546
Returns bitmessage connection information as dict with keys *inbound*,
15521547
*outbound*.
15531548
"""
1554-
if connectionpool is None or connectionpool.pool is None:
1549+
if stats.pool is None:
15551550
raise APIError(21, 'Network is not started.')
15561551
inboundConnections = []
15571552
outboundConnections = []
1558-
for i in connectionpool.pool.inboundConnections.values():
1553+
for i in stats.pool.inboundConnections.values():
15591554
inboundConnections.append({
15601555
'host': i.destination.host,
15611556
'port': i.destination.port,
15621557
'fullyEstablished': i.fullyEstablished,
15631558
'userAgent': str(i.userAgent)
15641559
})
1565-
for i in connectionpool.pool.outboundConnections.values():
1560+
for i in stats.pool.outboundConnections.values():
15661561
outboundConnections.append({
15671562
'host': i.destination.host,
15681563
'port': i.destination.port,

‎src/network/__init__.py‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,35 @@ def start(config, state):
2929
from .knownnodes import readKnownNodes
3030
from .receivequeuethread import ReceiveQueueThread
3131
from .uploadthread import UploadThread
32+
from . import stats
3233

3334
# create the connection pool
34-
connectionpool.pool = BMConnectionPool()
35+
pool = BMConnectionPool()
36+
connectionpool.pool = pool
3537

3638
# check and set dandelion enabled value at network startup
3739
dandelion_ins.init_dandelion_enabled(config)
3840
# pass pool instance into dandelion class instance
39-
dandelion_ins.init_pool(connectionpool.pool)
41+
dandelion_ins.init_pool(pool)
42+
43+
# init stats with pool reference
44+
stats.init(pool)
4045

4146
readKnownNodes()
42-
connectionpool.pool.connectToStream(1)
47+
pool.connectToStream(1)
4348
for thread in (
44-
BMNetworkThread(), InvThread(), AddrThread(),
45-
DownloadThread(), UploadThread()
49+
BMNetworkThread(pool), InvThread(pool), AddrThread(pool),
50+
DownloadThread(pool), UploadThread(pool)
4651
):
4752
thread.daemon = True
4853
thread.start()
4954

5055
# Optional components
5156
for i in range(config.getint('threads', 'receive')):
52-
thread = ReceiveQueueThread(i)
57+
thread = ReceiveQueueThread(i, pool)
5358
thread.daemon = True
5459
thread.start()
5560
if config.safeGetBoolean('bitmessagesettings', 'udp'):
56-
state.announceThread = AnnounceThread()
61+
state.announceThread = AnnounceThread(pool)
5762
state.announceThread.daemon = True
5863
state.announceThread.start()

‎src/network/addrthread.py‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import random
55
from six.moves import queue
66

7-
from . import connectionpool
87
from protocol import assembleAddrMessage
98
from network import addrQueue # FIXME: init with queue
109

@@ -15,6 +14,10 @@ class AddrThread(StoppableThread):
1514
"""(Node) address broadcasting thread"""
1615
name = "AddrBroadcaster"
1716

17+
def __init__(self, pool):
18+
super(AddrThread, self).__init__()
19+
self.pool = pool
20+
1821
def run(self):
1922
while not self._stopped:
2023
chunk = []
@@ -27,7 +30,7 @@ def run(self):
2730

2831
if chunk:
2932
# Choose peers randomly
30-
connections = connectionpool.pool.establishedConnections()
33+
connections = self.pool.establishedConnections()
3134
random.shuffle(connections)
3235
for i in connections:
3336
random.shuffle(chunk)

‎src/network/announcethread.py‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"""
44
import time
55

6-
from . import connectionpool
76
from bmconfigparser import config
87
from protocol import assembleAddrMessage
98

@@ -16,6 +15,10 @@ class AnnounceThread(StoppableThread):
1615
name = "Announcer"
1716
announceInterval = 60
1817

18+
def __init__(self, pool):
19+
super(AnnounceThread, self).__init__()
20+
self.pool = pool
21+
1922
def run(self):
2023
lastSelfAnnounced = 0
2124
while not self._stopped:
@@ -26,13 +29,12 @@ def run(self):
2629
if processed == 0:
2730
self.stop.wait(10)
2831

29-
@staticmethod
30-
def announceSelf():
32+
def announceSelf(self):
3133
"""Announce our presence"""
32-
for connection in connectionpool.pool.udpSockets.values():
34+
for connection in self.pool.udpSockets.values():
3335
if not connection.announcing:
3436
continue
35-
for stream in connectionpool.pool.streams:
37+
for stream in self.pool.streams:
3638
addr = (
3739
stream,
3840
Peer(

‎src/network/bmobject.py‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import protocol
88
import state
9-
from . import connectionpool
109
from network import dandelion_ins
1110
from highlevelcrypto import calculateInventoryHash
1211

@@ -93,14 +92,14 @@ def checkEOLSanity(self):
9392
# .. todo:: remove from download queue
9493
raise BMObjectExpiredError()
9594

96-
def checkStream(self):
95+
def checkStream(self, streams):
9796
"""Check if object's stream matches streams we are interested in"""
9897
if self.streamNumber < protocol.MIN_VALID_STREAM \
9998
or self.streamNumber > protocol.MAX_VALID_STREAM:
10099
logger.warning(
101100
'The object has invalid stream: %s', self.streamNumber)
102101
raise BMObjectInvalidError()
103-
if self.streamNumber not in connectionpool.pool.streams:
102+
if self.streamNumber not in streams:
104103
logger.debug(
105104
'The streamNumber %i isn\'t one we are interested in.',
106105
self.streamNumber)

‎src/network/bmproto.py‎

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import addresses
1515
import protocol
1616
import state
17-
from . import connectionpool
1817
from . import knownnodes
1918
from bmconfigparser import config
2019
from queues import objectProcessorQueue
@@ -395,27 +394,27 @@ def bm_command_object(self):
395394
self.object.checkAlreadyHave()
396395
except (BMObjectExpiredError, BMObjectAlreadyHaveError,
397396
BMObjectInsufficientPOWError):
398-
BMProto.stopDownloadingObject(self.object.inventoryHash)
397+
self.stopDownloadingObject(self.object.inventoryHash)
399398
raise
400399
try:
401-
self.object.checkStream()
400+
self.object.checkStream(self.pool.streams)
402401
except BMObjectUnwantedStreamError:
403402
acceptmismatch = config.getboolean(
404403
"inventory", "acceptmismatch")
405-
BMProto.stopDownloadingObject(
404+
self.stopDownloadingObject(
406405
self.object.inventoryHash, acceptmismatch)
407406
if not acceptmismatch:
408407
raise
409408
except BMObjectInvalidError:
410-
BMProto.stopDownloadingObject(self.object.inventoryHash)
409+
self.stopDownloadingObject(self.object.inventoryHash)
411410
raise
412411

413412
try:
414413
self.object.checkObjectByType()
415414
objectProcessorQueue.put((
416415
self.object.objectType, buffer(self.object.data))) # noqa: F821
417416
except BMObjectInvalidError:
418-
BMProto.stopDownloadingObject(self.object.inventoryHash, True)
417+
self.stopDownloadingObject(self.object.inventoryHash, True)
419418
else:
420419
try:
421420
del missingObjects[self.object.inventoryHash]
@@ -448,7 +447,7 @@ def bm_command_addr(self):
448447
for seenTime, stream, _, ip, port in self._decode_addr():
449448
ip = str(ip)
450449
if (
451-
stream not in connectionpool.pool.streams
450+
stream not in self.pool.streams
452451
# FIXME: should check against complete list
453452
or ip.startswith('bootstrap')
454453
):
@@ -547,7 +546,7 @@ def bm_command_version(self):
547546
if not self.isOutbound:
548547
self.append_write_buf(protocol.assembleVersionMessage(
549548
self.destination.host, self.destination.port,
550-
connectionpool.pool.streams, dandelion_ins.enabled, True,
549+
self.pool.streams, dandelion_ins.enabled, True,
551550
nodeid=self.nodeid))
552551
logger.debug(
553552
'%(host)s:%(port)i sending version',
@@ -603,7 +602,7 @@ def peerValidityChecks(self):
603602
'Closed connection to %s because there is no overlapping'
604603
' interest in streams.', self.destination)
605604
return False
606-
if connectionpool.pool.inboundConnections.get(
605+
if self.pool.inboundConnections.get(
607606
self.destination):
608607
try:
609608
if not protocol.checkSocksIP(self.destination.host):
@@ -621,8 +620,8 @@ def peerValidityChecks(self):
621620
# or server full report the same error to counter deanonymisation
622621
if (
623622
Peer(self.destination.host, self.peerNode.port)
624-
in connectionpool.pool.inboundConnections
625-
or len(connectionpool.pool)
623+
in self.pool.inboundConnections
624+
or len(self.pool)
626625
> config.safeGetInt(
627626
'bitmessagesettings', 'maxtotalconnections')
628627
+ config.safeGetInt(
@@ -634,7 +633,7 @@ def peerValidityChecks(self):
634633
'Closed connection to %s due to server full'
635634
' or duplicate inbound/outbound.', self.destination)
636635
return False
637-
if connectionpool.pool.isAlreadyConnected(self.nonce):
636+
if self.pool.isAlreadyConnected(self.nonce):
638637
self.append_write_buf(protocol.assembleErrorMessage(
639638
errorText="I'm connected to myself. Closing connection.",
640639
fatal=2))
@@ -645,10 +644,9 @@ def peerValidityChecks(self):
645644

646645
return True
647646

648-
@staticmethod
649-
def stopDownloadingObject(hashId, forwardAnyway=False):
647+
def stopDownloadingObject(self, hashId, forwardAnyway=False):
650648
"""Stop downloading object *hashId*"""
651-
for connection in connectionpool.pool.connections():
649+
for connection in self.pool.connections():
652650
try:
653651
del connection.objectsNewToMe[hashId]
654652
except KeyError:

‎src/network/connectionpool.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ def isAlreadyConnected(self, nodeid):
120120
def addConnection(self, connection):
121121
"""Add a connection object to our internal dict"""
122122
from .udp import UDPSocket
123+
connection.pool = self
123124
if isinstance(connection, UDPSocket):
124125
return
125126
if connection.isOutbound:
@@ -182,6 +183,7 @@ def startListening(self, bind=None):
182183
port = config.safeGetInt("bitmessagesettings", "port")
183184
# correct port even if it changed
184185
ls = TCPServer(host=bind, port=port)
186+
ls.pool = self
185187
self.listeningSockets[ls.destination] = ls
186188

187189
def startUDPSocket(self, bind=None):
@@ -198,6 +200,7 @@ def startUDPSocket(self, bind=None):
198200
udpSocket = UDPSocket(announcing=False)
199201
else:
200202
udpSocket = UDPSocket(host=bind, announcing=True)
203+
udpSocket.pool = self
201204
self.udpSockets[udpSocket.listening.host] = udpSocket
202205

203206
def startBootstrappers(self):

‎src/network/downloadthread.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import state
77
import addresses
88
import protocol
9-
from . import connectionpool
109
from network import dandelion_ins
1110
from .objectracker import missingObjects
1211
from .threads import StoppableThread
@@ -20,8 +19,9 @@ class DownloadThread(StoppableThread):
2019
cleanInterval = 60
2120
requestExpires = 3600
2221

23-
def __init__(self):
22+
def __init__(self, pool):
2423
super(DownloadThread, self).__init__(name="Downloader")
24+
self.pool = pool
2525
self.lastCleaned = time.time()
2626

2727
def cleanPending(self):
@@ -42,7 +42,7 @@ def run(self):
4242
while not self._stopped:
4343
requested = 0
4444
# Choose downloading peers randomly
45-
connections = connectionpool.pool.establishedConnections()
45+
connections = self.pool.establishedConnections()
4646
random.shuffle(connections)
4747
requestChunk = max(int(
4848
min(self.maxRequestChunk, len(missingObjects))

‎src/network/invthread.py‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,16 @@
99
import addresses
1010
import protocol
1111
import state
12-
from . import connectionpool
1312
from network import dandelion_ins, invQueue
1413
from .threads import StoppableThread
1514

1615

17-
def handleExpiredDandelion(expired):
16+
def handleExpiredDandelion(expired, pool):
1817
"""For expired dandelion objects, mark all remotes as not having
1918
the object"""
2019
if not expired:
2120
return
22-
for i in connectionpool.pool.connections():
21+
for i in pool.connections():
2322
if not i.fullyEstablished:
2423
continue
2524
for x in expired:
@@ -37,11 +36,14 @@ class InvThread(StoppableThread):
3736

3837
name = "InvBroadcaster"
3938

40-
@staticmethod
41-
def handleLocallyGenerated(stream, hashId):
39+
def __init__(self, pool):
40+
super(InvThread, self).__init__()
41+
self.pool = pool
42+
43+
def handleLocallyGenerated(self, stream, hashId):
4244
"""Locally generated inventory items require special handling"""
4345
dandelion_ins.addHash(hashId, stream=stream)
44-
for connection in connectionpool.pool.connections():
46+
for connection in self.pool.connections():
4547
if dandelion_ins.enabled and connection != \
4648
dandelion_ins.objectChildStem(hashId):
4749
continue
@@ -52,7 +54,7 @@ def run(self): # pylint: disable=too-many-branches
5254
chunk = []
5355
while True:
5456
# Dandelion fluff trigger by expiration
55-
handleExpiredDandelion(dandelion_ins.expire(invQueue))
57+
handleExpiredDandelion(dandelion_ins.expire(invQueue), self.pool)
5658
try:
5759
data = invQueue.get(False)
5860
chunk.append((data[0], data[1]))
@@ -63,7 +65,7 @@ def run(self): # pylint: disable=too-many-branches
6365
break
6466

6567
if chunk:
66-
for connection in connectionpool.pool.connections():
68+
for connection in self.pool.connections():
6769
fluffs = []
6870
stems = []
6971
for inv in chunk:

0 commit comments

Comments
 (0)