@@ -196,10 +196,20 @@ def get_logs(
196
196
except Exception as e :
197
197
print (f"Getting logs from SubSquid threw exception { repr (e )} , falling back to RPC" )
198
198
199
- if get_logs_by_block_hash or to_block > self .w3 .latest_seen_block - self .w3 .unstable_blocks :
199
+ last_stable_block = self .w3 .latest_seen_block - self .w3 .unstable_blocks
200
+ if get_logs_by_block_hash or to_block > last_stable_block :
200
201
# getting logs via from and to block range sometimes drops logs.
201
202
# This does not happen when getting them individually for each block by their block hash
202
203
# get all block hashes and ensure they build upon each other
204
+
205
+ # if only unstable blocks need to be gathered by hash, gather stable blocks as log range
206
+ results : list [LogReceipt ] = []
207
+ if not get_logs_by_block_hash and from_block < last_stable_block :
208
+ results += self .get_logs ({** filter_params , "toBlock" : last_stable_block }, ** kwargs )
209
+ from_block = last_stable_block + 1
210
+ assert to_block >= from_block
211
+ num_blocks = to_block - from_block + 1
212
+
203
213
with self .w3 .batch_requests () as batch :
204
214
batch .add_mapping ({
205
215
self .w3 .eth ._get_block : list (range (from_block , to_block + 1 ))
@@ -212,10 +222,10 @@ def get_logs(
212
222
block = blocks [i ]
213
223
if i != 0 :
214
224
assert block ["parentHash" ] == blocks [i - 1 ]["hash" ], f"{ blocks [i - 1 ]['hash' ].hex ()= } , { block ['parentHash' ].hex ()= } "
215
- if block_number == from_block and from_block_body is not None :
225
+ if from_block_body is not None and from_block_body [ "number" ] == block_number :
216
226
if block ["hash" ] != from_block_body ["hash" ]:
217
227
raise ForkedBlock (f"expected={ from_block_body ['hash' ].hex ()} , actual={ block ['hash' ].hex ()} " )
218
- if block_number == to_block and to_block_body is not None :
228
+ if to_block_body is not None and to_block_body [ "number" ] == block_number :
219
229
if block ["hash" ] != to_block_body ["hash" ]:
220
230
raise ForkedBlock (f"expected={ to_block_body ['hash' ].hex ()} , actual={ block ['hash' ].hex ()} " )
221
231
@@ -230,7 +240,7 @@ def get_logs(
230
240
assert len (results_per_block ) == num_blocks
231
241
if p_bar is not None :
232
242
p_bar .update (len (blocks ))
233
- results = sum (results_per_block , [])
243
+ results + = sum (results_per_block , [])
234
244
return results
235
245
236
246
# getting logs for a single block, which is not at the chain head. No drama
0 commit comments