@@ -107,19 +107,13 @@ def __init__(self, w3: Web3) -> None:
107
107
108
108
self .lock = threading .Lock ()
109
109
self .event = threading .Event ()
110
- self .start ()
110
+ self .start_block_filter_loop ()
111
111
112
- def start (self ):
113
- latest = self .w3 .eth .get_block ("latest" )
114
- self .last_block = latest .hash
115
- self .last_block_seen = latest .timestamp
116
- self .last_request = time .time ()
117
- self .block_cache : OrderedDict = OrderedDict ()
118
- self .block_filter = self .w3 .eth .filter ("latest" )
119
-
120
- self .is_killed = False
112
+ def start_block_filter_loop (self ):
113
+ self .event .clear ()
121
114
self .loop_thread = threading .Thread (target = self .loop_exception_handler , daemon = True )
122
115
self .loop_thread .start ()
116
+ self .event .wait ()
123
117
124
118
@classmethod
125
119
def get_layer (cls , w3 : Web3 , network_type : str ) -> Optional [int ]:
@@ -151,6 +145,16 @@ def loop_exception_handler(self) -> None:
151
145
self .is_killed = True
152
146
153
147
def block_filter_loop (self ) -> None :
148
+ # initialize required state variables within the loop to avoid recursion death
149
+ latest = self .w3 .eth .get_block ("latest" )
150
+ self .last_block = latest .hash
151
+ self .last_block_seen = latest .timestamp
152
+ self .last_request = time .time ()
153
+ self .block_cache : OrderedDict = OrderedDict ()
154
+ self .block_filter = self .w3 .eth .filter ("latest" )
155
+ self .is_killed = False
156
+ self .event .set ()
157
+
154
158
while not self .is_killed :
155
159
# if the last RPC request was > 60 seconds ago, reduce the rate of updates.
156
160
# we eventually settle at one query per minute after 10 minutes of no requests.
@@ -229,7 +233,7 @@ def process_request(self, make_request: Callable, method: str, params: List) ->
229
233
230
234
if not self .loop_thread .is_alive ():
231
235
# restart the block filter loop if it has crashed (usually from a ConnectionError)
232
- self .start ()
236
+ self .start_block_filter_loop ()
233
237
234
238
with self .lock :
235
239
self .last_request = time .time ()
0 commit comments