55import random
66from time import time
77
8- from network import protocol , state , queues , addresses
98import connectionpool
109from network import dandelion_ins
1110from threads import StoppableThread
@@ -34,6 +33,13 @@ class InvThread(StoppableThread):
3433
3534 name = "InvBroadcaster"
3635
36+ def __init__ (self , protocol , state , queues , addresses ):
37+ self .protocol = protocol
38+ self .state = state
39+ self .queues = queues
40+ self .addresses = addresses
41+ StoppableThread .__init__ (self )
42+
3743 @staticmethod
3844 def handleLocallyGenerated (stream , hashId ):
3945 """Locally generated inventory items require special handling"""
@@ -45,13 +51,13 @@ def handleLocallyGenerated(stream, hashId):
4551 connection .objectsNewToThem [hashId ] = time ()
4652
4753 def run (self ): # pylint: disable=too-many-branches
48- while not state .shutdown : # pylint: disable=too-many-nested-blocks
54+ while not self . state .shutdown : # pylint: disable=too-many-nested-blocks
4955 chunk = []
5056 while True :
5157 # Dandelion fluff trigger by expiration
52- handleExpiredDandelion (dandelion_ins .expire (queues .invQueue ))
58+ handleExpiredDandelion (dandelion_ins .expire (self . queues .invQueue ))
5359 try :
54- data = queues .invQueue .get (False )
60+ data = self . queues .invQueue .get (False )
5561 chunk .append ((data [0 ], data [1 ]))
5662 # locally generated
5763 if len (data ) == 2 or data [2 ] is None :
@@ -78,7 +84,7 @@ def run(self): # pylint: disable=too-many-branches
7884 if random .randint (1 , 100 ) >= dandelion_ins .enabled : # nosec B311
7985 fluffs .append (inv [1 ])
8086 # send a dinv only if the stem node supports dandelion
81- elif connection .services & protocol .NODE_DANDELION > 0 :
87+ elif connection .services & self . protocol .NODE_DANDELION > 0 :
8288 stems .append (inv [1 ])
8389 else :
8490 fluffs .append (inv [1 ])
@@ -87,20 +93,20 @@ def run(self): # pylint: disable=too-many-branches
8793
8894 if fluffs :
8995 random .shuffle (fluffs )
90- connection .append_write_buf (protocol .CreatePacket (
96+ connection .append_write_buf (self . protocol .CreatePacket (
9197 'inv' ,
92- addresses .encodeVarint (
98+ self . addresses .encodeVarint (
9399 len (fluffs )) + '' .join (fluffs )))
94100 if stems :
95101 random .shuffle (stems )
96- connection .append_write_buf (protocol .CreatePacket (
102+ connection .append_write_buf (self . protocol .CreatePacket (
97103 'dinv' ,
98- addresses .encodeVarint (
104+ self . addresses .encodeVarint (
99105 len (stems )) + '' .join (stems )))
100106
101- queues .invQueue .iterate ()
107+ self . queues .invQueue .iterate ()
102108 for _ in range (len (chunk )):
103- queues .invQueue .task_done ()
109+ self . queues .invQueue .task_done ()
104110
105111 dandelion_ins .reRandomiseStems ()
106112
0 commit comments