Skip to content

Commit a209d65

Browse files
committed
Moved dandelion runtime var from state to network->dandelion
1 parent 3a04e35 commit a209d65

10 files changed

Lines changed: 35 additions & 32 deletions

File tree

src/network/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ def start(config, state):
1414
from .announcethread import AnnounceThread
1515
import connectionpool # pylint: disable=relative-import
1616
from .addrthread import AddrThread
17-
from .dandelion import Dandelion
1817
from .downloadthread import DownloadThread
1918
from .invthread import InvThread
2019
from .networkthread import BMNetworkThread
@@ -23,8 +22,6 @@ def start(config, state):
2322
from .uploadthread import UploadThread
2423

2524
readKnownNodes()
26-
# init, needs to be early because other thread may access it early
27-
state.Dandelion = Dandelion()
2825
connectionpool.pool.connectToStream(1)
2926
for thread in (
3027
BMNetworkThread(), InvThread(), AddrThread(),

src/network/bmobject.py

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

77
import protocol
88
import state
9+
import dandelion
910
import connectionpool
1011
from highlevelcrypto import calculateInventoryHash
1112

@@ -112,7 +113,7 @@ def checkAlreadyHave(self):
112113
or advertise it unnecessarily)
113114
"""
114115
# if it's a stem duplicate, pretend we don't have it
115-
if state.Dandelion.hasHash(self.inventoryHash):
116+
if dandelion.instance.hasHash(self.inventoryHash):
116117
return
117118
if self.inventoryHash in state.Inventory:
118119
raise BMObjectAlreadyHaveError()

src/network/bmproto.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import knownnodes
1616
import protocol
1717
import state
18+
import dandelion
1819
import connectionpool
1920
from bmconfigparser import config
2021
from queues import invQueue, objectProcessorQueue, portCheckerQueue
@@ -337,27 +338,27 @@ def bm_command_getdata(self):
337338
self.pendingUpload[str(i)] = now
338339
return True
339340

340-
def _command_inv(self, dandelion=False):
341+
def _command_inv(self, extend_dandelion_stem=False):
341342
"""
342343
Common inv announce implementation:
343-
both inv and dinv depending on *dandelion* kwarg
344+
both inv and dinv depending on *extend_dandelion_stem* kwarg
344345
"""
345346
items = self.decode_payload_content("l32s")
346347

347348
if len(items) > protocol.MAX_OBJECT_COUNT:
348349
logger.error(
349-
'Too many items in %sinv message!', 'd' if dandelion else '')
350+
'Too many items in %sinv message!', 'd' if extend_dandelion_stem else '')
350351
raise BMProtoExcessiveDataError()
351352

352353
# ignore dinv if dandelion turned off
353-
if dandelion and not state.dandelion_enabled:
354+
if extend_dandelion_stem and not state.dandelion_enabled:
354355
return True
355356

356357
for i in map(str, items):
357-
if i in state.Inventory and not state.Dandelion.hasHash(i):
358+
if i in state.Inventory and not dandelion.instance.hasHash(i):
358359
continue
359-
if dandelion and not state.Dandelion.hasHash(i):
360-
state.Dandelion.addHash(i, self)
360+
if extend_dandelion_stem and not dandelion.instance.hasHash(i):
361+
dandelion.instance.addHash(i, self)
361362
self.handleReceivedInventory(i)
362363

363364
return True
@@ -419,9 +420,9 @@ def bm_command_object(self):
419420
except KeyError:
420421
pass
421422

422-
if self.object.inventoryHash in state.Inventory and state.Dandelion.hasHash(
423+
if self.object.inventoryHash in state.Inventory and dandelion.instance.hasHash(
423424
self.object.inventoryHash):
424-
state.Dandelion.removeHash(
425+
dandelion.instance.removeHash(
425426
self.object.inventoryHash, "cycle detection")
426427

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

src/network/dandelion.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,3 +192,6 @@ def reRandomiseStems(self):
192192
self.nodeMap = {}
193193
# hashMap stays to cater for pending stems
194194
self.refresh = time() + REASSIGN_INTERVAL
195+
196+
197+
instance = Dandelion()

src/network/downloadthread.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import addresses
77
import helper_random
88
import protocol
9+
import dandelion
910
import connectionpool
1011
from objectracker import missingObjects
1112
from threads import StoppableThread
@@ -59,7 +60,7 @@ def run(self):
5960
payload = bytearray()
6061
chunkCount = 0
6162
for chunk in request:
62-
if chunk in state.Inventory and not state.Dandelion.hasHash(chunk):
63+
if chunk in state.Inventory and not dandelion.instance.hasHash(chunk):
6364
try:
6465
del i.objectsNewToMe[chunk]
6566
except KeyError:

src/network/invthread.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import addresses
99
import protocol
1010
import state
11+
import dandelion
1112
import connectionpool
1213
from queues import invQueue
1314
from threads import StoppableThread
@@ -39,10 +40,10 @@ class InvThread(StoppableThread):
3940
@staticmethod
4041
def handleLocallyGenerated(stream, hashId):
4142
"""Locally generated inventory items require special handling"""
42-
state.Dandelion.addHash(hashId, stream=stream)
43+
dandelion.instance.addHash(hashId, stream=stream)
4344
for connection in connectionpool.pool.connections():
4445
if state.dandelion_enabled and connection != \
45-
state.Dandelion.objectChildStem(hashId):
46+
dandelion.instance.objectChildStem(hashId):
4647
continue
4748
connection.objectsNewToThem[hashId] = time()
4849

@@ -51,7 +52,7 @@ def run(self): # pylint: disable=too-many-branches
5152
chunk = []
5253
while True:
5354
# Dandelion fluff trigger by expiration
54-
handleExpiredDandelion(state.Dandelion.expire())
55+
handleExpiredDandelion(dandelion.instance.expire())
5556
try:
5657
data = invQueue.get(False)
5758
chunk.append((data[0], data[1]))
@@ -74,7 +75,7 @@ def run(self): # pylint: disable=too-many-branches
7475
except KeyError:
7576
continue
7677
try:
77-
if connection == state.Dandelion.objectChildStem(inv[1]):
78+
if connection == dandelion.instance.objectChildStem(inv[1]):
7879
# Fluff trigger by RNG
7980
# auto-ignore if config set to 0, i.e. dandelion is off
8081
if random.randint(1, 100) >= state.dandelion_enabled: # nosec B311
@@ -104,7 +105,7 @@ def run(self): # pylint: disable=too-many-branches
104105
for _ in range(len(chunk)):
105106
invQueue.task_done()
106107

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

110111
self.stop.wait(1)

src/network/objectracker.py

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

7-
import state
7+
import dandelion
88
import connectionpool
99
from randomtrackingdict import RandomTrackingDict
1010

@@ -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 state.Dandelion.hasHash(hashid)
111-
or state.Dandelion.objectChildStem(hashid) == i):
110+
not dandelion.instance.hasHash(hashid)
111+
or dandelion.instance.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-
state.Dandelion.setHashStream(hashid, streamNumber)
117+
dandelion.instance.setHashStream(hashid, streamNumber)
118118

119119
if i == self:
120120
try:

src/network/tcp.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import l10n
1616
import protocol
1717
import state
18+
import dandelion
1819
import connectionpool
1920
from bmconfigparser import config
2021
from highlevelcrypto import randomBytes
@@ -168,7 +169,7 @@ def set_connection_fully_established(self):
168169
knownnodes.increaseRating(self.destination)
169170
knownnodes.addKnownNode(
170171
self.streams, self.destination, time.time())
171-
state.Dandelion.maybeAddStem(self)
172+
dandelion.instance.maybeAddStem(self)
172173
self.sendAddr()
173174
self.sendBigInv()
174175

@@ -230,7 +231,7 @@ def sendChunk():
230231
with self.objectsNewToThemLock:
231232
for objHash in state.Inventory.unexpired_hashes_by_stream(stream):
232233
# don't advertise stem objects on bigInv
233-
if state.Dandelion.hasHash(objHash):
234+
if dandelion.instance.hasHash(objHash):
234235
continue
235236
bigInvList[objHash] = 0
236237
objectCount = 0
@@ -292,7 +293,7 @@ def handle_close(self):
292293
if host_is_global:
293294
knownnodes.addKnownNode(
294295
self.streams, self.destination, time.time())
295-
state.Dandelion.maybeRemoveStem(self)
296+
dandelion.instance.maybeRemoveStem(self)
296297
else:
297298
self.checkTimeOffsetNotification()
298299
if host_is_global:

src/network/uploadthread.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import helper_random
77
import protocol
88
import state
9+
import dandelion
910
import connectionpool
1011
from randomtrackingdict import RandomTrackingDict
1112
from threads import StoppableThread
@@ -40,8 +41,8 @@ def run(self):
4041
chunk_count = 0
4142
for chunk in request:
4243
del i.pendingUpload[chunk]
43-
if state.Dandelion.hasHash(chunk) and \
44-
i != state.Dandelion.objectChildStem(chunk):
44+
if dandelion.instance.hasHash(chunk) and \
45+
i != dandelion.instance.objectChildStem(chunk):
4546
i.antiIntersectionDelay()
4647
self.logger.info(
4748
'%s asked for a stem object we didn\'t offer to it.',

src/state.py

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

9797

9898
Inventory = Placeholder("Inventory")
99-
100-
101-
Dandelion = Placeholder("Dandelion")

0 commit comments

Comments
 (0)