Skip to content

Commit 1c8ae8f

Browse files
committed
moved Dandelion in state - global runtime variable from singleton
1 parent 5faef8d commit 1c8ae8f

File tree

10 files changed

+27
-31
lines changed

10 files changed

+27
-31
lines changed

src/network/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
def start(config, state):
1818
"""Start network threads"""
19+
import state
1920
from .addrthread import AddrThread
2021
from .dandelion import Dandelion
2122
from .downloadthread import DownloadThread
@@ -27,7 +28,7 @@ def start(config, state):
2728

2829
readKnownNodes()
2930
# init, needs to be early because other thread may access it early
30-
Dandelion()
31+
state.Dandelion = Dandelion()
3132
BMConnectionPool().connectToStream(1)
3233
for thread in (
3334
BMNetworkThread(), InvThread(), AddrThread(),

src/network/bmobject.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import protocol
88
import state
99
from highlevelcrypto import calculateInventoryHash
10-
from network.dandelion import Dandelion
1110

1211
logger = logging.getLogger('default')
1312

@@ -112,7 +111,7 @@ def checkAlreadyHave(self):
112111
or advertise it unnecessarily)
113112
"""
114113
# if it's a stem duplicate, pretend we don't have it
115-
if Dandelion().hasHash(self.inventoryHash):
114+
if state.Dandelion.hasHash(self.inventoryHash):
116115
return
117116
if self.inventoryHash in state.Inventory:
118117
raise BMObjectAlreadyHaveError()

src/network/bmproto.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
BMObjectInsufficientPOWError, BMObjectInvalidError,
2626
BMObjectUnwantedStreamError
2727
)
28-
from network.dandelion import Dandelion
2928
from network.proxy import ProxyError
3029

3130
from node import Node, Peer
@@ -355,10 +354,10 @@ def _command_inv(self, dandelion=False):
355354
return True
356355

357356
for i in map(str, items):
358-
if i in state.Inventory and not Dandelion().hasHash(i):
357+
if i in state.Inventory and not state.Dandelion.hasHash(i):
359358
continue
360-
if dandelion and not Dandelion().hasHash(i):
361-
Dandelion().addHash(i, self)
359+
if dandelion and not state.Dandelion.hasHash(i):
360+
state.Dandelion.addHash(i, self)
362361
self.handleReceivedInventory(i)
363362

364363
return True
@@ -420,9 +419,9 @@ def bm_command_object(self):
420419
except KeyError:
421420
pass
422421

423-
if self.object.inventoryHash in state.Inventory and Dandelion().hasHash(
422+
if self.object.inventoryHash in state.Inventory and state.Dandelion.hasHash(
424423
self.object.inventoryHash):
425-
Dandelion().removeHash(
424+
state.Dandelion.removeHash(
426425
self.object.inventoryHash, "cycle detection")
427426

428427
state.Inventory[self.object.inventoryHash] = (

src/network/dandelion.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import connectionpool
1111
import state
1212
from queues import invQueue
13-
from singleton import Singleton
1413

1514
# randomise routes after 600 seconds
1615
REASSIGN_INTERVAL = 600
@@ -26,7 +25,6 @@
2625
logger = logging.getLogger('default')
2726

2827

29-
@Singleton
3028
class Dandelion: # pylint: disable=old-style-class
3129
"""Dandelion class for tracking stem/fluff stages."""
3230
def __init__(self):

src/network/downloadthread.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import addresses
77
import helper_random
88
import protocol
9-
from dandelion import Dandelion
109
from network.connectionpool import BMConnectionPool
1110
from objectracker import missingObjects
1211
from threads import StoppableThread
@@ -60,7 +59,7 @@ def run(self):
6059
payload = bytearray()
6160
chunkCount = 0
6261
for chunk in request:
63-
if chunk in state.Inventory and not Dandelion().hasHash(chunk):
62+
if chunk in state.Inventory and not state.Dandelion.hasHash(chunk):
6463
try:
6564
del i.objectsNewToMe[chunk]
6665
except KeyError:

src/network/invthread.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import protocol
1010
import state
1111
from network.connectionpool import BMConnectionPool
12-
from network.dandelion import Dandelion
1312
from queues import invQueue
1413
from threads import StoppableThread
1514

@@ -40,10 +39,10 @@ class InvThread(StoppableThread):
4039
@staticmethod
4140
def handleLocallyGenerated(stream, hashId):
4241
"""Locally generated inventory items require special handling"""
43-
Dandelion().addHash(hashId, stream=stream)
42+
state.Dandelion.addHash(hashId, stream=stream)
4443
for connection in BMConnectionPool().connections():
4544
if state.dandelion and connection != \
46-
Dandelion().objectChildStem(hashId):
45+
state.Dandelion.objectChildStem(hashId):
4746
continue
4847
connection.objectsNewToThem[hashId] = time()
4948

@@ -52,7 +51,7 @@ def run(self): # pylint: disable=too-many-branches
5251
chunk = []
5352
while True:
5453
# Dandelion fluff trigger by expiration
55-
handleExpiredDandelion(Dandelion().expire())
54+
handleExpiredDandelion(state.Dandelion.expire())
5655
try:
5756
data = invQueue.get(False)
5857
chunk.append((data[0], data[1]))
@@ -75,7 +74,7 @@ def run(self): # pylint: disable=too-many-branches
7574
except KeyError:
7675
continue
7776
try:
78-
if connection == Dandelion().objectChildStem(inv[1]):
77+
if connection == state.Dandelion.objectChildStem(inv[1]):
7978
# Fluff trigger by RNG
8079
# auto-ignore if config set to 0, i.e. dandelion is off
8180
if random.randint(1, 100) >= state.dandelion: # nosec B311
@@ -105,7 +104,7 @@ def run(self): # pylint: disable=too-many-branches
105104
for _ in range(len(chunk)):
106105
invQueue.task_done()
107106

108-
if Dandelion().refresh < time():
109-
Dandelion().reRandomiseStems()
107+
if state.Dandelion.refresh < time():
108+
state.Dandelion.reRandomiseStems()
110109

111110
self.stop.wait(1)

src/network/objectracker.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import time
55
from threading import RLock
66

7+
import state
78
import network.connectionpool
8-
from network.dandelion import Dandelion
99
from randomtrackingdict import RandomTrackingDict
1010

1111
haveBloom = False
@@ -107,14 +107,14 @@ def handleReceivedObject(self, streamNumber, hashid):
107107
del i.objectsNewToMe[hashid]
108108
except KeyError:
109109
if streamNumber in i.streams and (
110-
not Dandelion().hasHash(hashid)
111-
or Dandelion().objectChildStem(hashid) == i):
110+
not state.Dandelion.hasHash(hashid)
111+
or state.Dandelion.objectChildStem(hashid) == i):
112112
with i.objectsNewToThemLock:
113113
i.objectsNewToThem[hashid] = time.time()
114114
# update stream number,
115115
# which we didn't have when we just received the dinv
116116
# also resets expiration of the stem mode
117-
Dandelion().setHashStream(hashid, streamNumber)
117+
state.Dandelion.setHashStream(hashid, streamNumber)
118118

119119
if i == self:
120120
try:

src/network/tcp.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import knownnodes
2626
from network.advanceddispatcher import AdvancedDispatcher
2727
from network.bmproto import BMProto
28-
from network.dandelion import Dandelion
2928
from network.objectracker import ObjectTracker
3029
from network.socks4a import Socks4aConnection
3130
from network.socks5 import Socks5Connection
@@ -169,7 +168,7 @@ def set_connection_fully_established(self):
169168
knownnodes.increaseRating(self.destination)
170169
knownnodes.addKnownNode(
171170
self.streams, self.destination, time.time())
172-
Dandelion().maybeAddStem(self)
171+
state.Dandelion.maybeAddStem(self)
173172
self.sendAddr()
174173
self.sendBigInv()
175174

@@ -231,7 +230,7 @@ def sendChunk():
231230
with self.objectsNewToThemLock:
232231
for objHash in state.Inventory.unexpired_hashes_by_stream(stream):
233232
# don't advertise stem objects on bigInv
234-
if Dandelion().hasHash(objHash):
233+
if state.Dandelion.hasHash(objHash):
235234
continue
236235
bigInvList[objHash] = 0
237236
objectCount = 0
@@ -293,7 +292,7 @@ def handle_close(self):
293292
if host_is_global:
294293
knownnodes.addKnownNode(
295294
self.streams, self.destination, time.time())
296-
Dandelion().maybeRemoveStem(self)
295+
state.Dandelion.maybeRemoveStem(self)
297296
else:
298297
self.checkTimeOffsetNotification()
299298
if host_is_global:

src/network/uploadthread.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import protocol
88
import state
99
from network.connectionpool import BMConnectionPool
10-
from network.dandelion import Dandelion
1110
from randomtrackingdict import RandomTrackingDict
1211
from threads import StoppableThread
1312

@@ -41,8 +40,8 @@ def run(self):
4140
chunk_count = 0
4241
for chunk in request:
4342
del i.pendingUpload[chunk]
44-
if Dandelion().hasHash(chunk) and \
45-
i != Dandelion().objectChildStem(chunk):
43+
if state.Dandelion.hasHash(chunk) and \
44+
i != state.Dandelion.objectChildStem(chunk):
4645
i.antiIntersectionDelay()
4746
self.logger.info(
4847
'%s asked for a stem object we didn\'t offer to it.',

src/state.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,6 @@ def _raise(self):
9797

9898

9999
Inventory = Placeholder("Inventory")
100+
101+
102+
Dandelion = Placeholder("Dandelion")

0 commit comments

Comments
 (0)