77from threading import RLock
88from time import time
99
10- import connectionpool
11- import state
12- from queues import invQueue
1310
1411# randomise routes after 600 seconds
1512REASSIGN_INTERVAL = 600
@@ -37,6 +34,8 @@ def __init__(self):
3734 # when to rerandomise routes
3835 self .refresh = time () + REASSIGN_INTERVAL
3936 self .lock = RLock ()
37+ self .enabled = None
38+ self .pool = None
4039
4140 @staticmethod
4241 def poissonTimeout (start = None , average = 0 ):
@@ -47,10 +46,23 @@ def poissonTimeout(start=None, average=0):
4746 average = FLUFF_TRIGGER_MEAN_DELAY
4847 return start + expovariate (1.0 / average ) + FLUFF_TRIGGER_FIXED_DELAY
4948
49+ def init_pool (self , pool ):
50+ """pass pool instance"""
51+ self .pool = pool
52+
53+ def init_dandelion_enabled (self , config ):
54+ """Check if Dandelion is enabled and set value in enabled attribute"""
55+ dandelion_enabled = config .safeGetInt ('network' , 'dandelion' )
56+ # dandelion requires outbound connections, without them,
57+ # stem objects will get stuck forever
58+ if not config .safeGetBoolean (
59+ 'bitmessagesettings' , 'sendoutgoingconnections' ):
60+ dandelion_enabled = 0
61+ self .enabled = dandelion_enabled
62+
5063 def addHash (self , hashId , source = None , stream = 1 ):
51- """Add inventory vector to dandelion stem"""
52- if not state .dandelion_enabled :
53- return
64+ """Add inventory vector to dandelion stem return status of dandelion enabled"""
65+ assert self .enabled is not None
5466 with self .lock :
5567 self .hashMap [hashId ] = Stem (
5668 self .getNodeStem (source ),
@@ -89,7 +101,7 @@ def objectChildStem(self, hashId):
89101 """Child (i.e. next) node for an inventory vector during stem mode"""
90102 return self .hashMap [hashId ].child
91103
92- def maybeAddStem (self , connection ):
104+ def maybeAddStem (self , connection , invQueue ):
93105 """
94106 If we had too few outbound connections, add the current one to the
95107 current stem list. Dandelion as designed by the authors should
@@ -163,7 +175,7 @@ def getNodeStem(self, node=None):
163175 self .nodeMap [node ] = self .pickStem (node )
164176 return self .nodeMap [node ]
165177
166- def expire (self ):
178+ def expire (self , invQueue ):
167179 """Switch expired objects from stem to fluff mode"""
168180 with self .lock :
169181 deadline = time ()
@@ -179,19 +191,18 @@ def expire(self):
179191
180192 def reRandomiseStems (self ):
181193 """Re-shuffle stem mapping (parent <-> child pairs)"""
194+ assert self .pool is not None
195+ if self .refresh > time ():
196+ return
197+
182198 with self .lock :
183199 try :
184200 # random two connections
185201 self .stem = sample (
186- connectionpool .BMConnectionPool (
187- ).outboundConnections .values (), MAX_STEMS )
202+ self .pool .outboundConnections .values (), MAX_STEMS )
188203 # not enough stems available
189204 except ValueError :
190- self .stem = connectionpool .BMConnectionPool (
191- ).outboundConnections .values ()
205+ self .stem = self .pool .outboundConnections .values ()
192206 self .nodeMap = {}
193207 # hashMap stays to cater for pending stems
194208 self .refresh = time () + REASSIGN_INTERVAL
195-
196-
197- instance = Dandelion ()
0 commit comments