5
5
import random
6
6
from time import time
7
7
8
- from network import protocol , state , queues , addresses
9
8
import connectionpool
10
9
from network import dandelion_ins
11
10
from threads import StoppableThread
@@ -34,6 +33,13 @@ class InvThread(StoppableThread):
34
33
35
34
name = "InvBroadcaster"
36
35
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
+
37
43
@staticmethod
38
44
def handleLocallyGenerated (stream , hashId ):
39
45
"""Locally generated inventory items require special handling"""
@@ -45,13 +51,13 @@ def handleLocallyGenerated(stream, hashId):
45
51
connection .objectsNewToThem [hashId ] = time ()
46
52
47
53
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
49
55
chunk = []
50
56
while True :
51
57
# Dandelion fluff trigger by expiration
52
- handleExpiredDandelion (dandelion_ins .expire (queues .invQueue ))
58
+ handleExpiredDandelion (dandelion_ins .expire (self . queues .invQueue ))
53
59
try :
54
- data = queues .invQueue .get (False )
60
+ data = self . queues .invQueue .get (False )
55
61
chunk .append ((data [0 ], data [1 ]))
56
62
# locally generated
57
63
if len (data ) == 2 or data [2 ] is None :
@@ -78,7 +84,7 @@ def run(self): # pylint: disable=too-many-branches
78
84
if random .randint (1 , 100 ) >= dandelion_ins .enabled : # nosec B311
79
85
fluffs .append (inv [1 ])
80
86
# 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 :
82
88
stems .append (inv [1 ])
83
89
else :
84
90
fluffs .append (inv [1 ])
@@ -87,20 +93,20 @@ def run(self): # pylint: disable=too-many-branches
87
93
88
94
if fluffs :
89
95
random .shuffle (fluffs )
90
- connection .append_write_buf (protocol .CreatePacket (
96
+ connection .append_write_buf (self . protocol .CreatePacket (
91
97
'inv' ,
92
- addresses .encodeVarint (
98
+ self . addresses .encodeVarint (
93
99
len (fluffs )) + '' .join (fluffs )))
94
100
if stems :
95
101
random .shuffle (stems )
96
- connection .append_write_buf (protocol .CreatePacket (
102
+ connection .append_write_buf (self . protocol .CreatePacket (
97
103
'dinv' ,
98
- addresses .encodeVarint (
104
+ self . addresses .encodeVarint (
99
105
len (stems )) + '' .join (stems )))
100
106
101
- queues .invQueue .iterate ()
107
+ self . queues .invQueue .iterate ()
102
108
for _ in range (len (chunk )):
103
- queues .invQueue .task_done ()
109
+ self . queues .invQueue .task_done ()
104
110
105
111
dandelion_ins .reRandomiseStems ()
106
112
0 commit comments