Skip to content

Commit 34ec0f9

Browse files
committed
if splitting eth.get_logs, setting fromBlockParentHash and toBlockHash to guarantee consistency between sub queries
1 parent 9629688 commit 34ec0f9

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

IceCreamSwapWeb3/EthAdvanced.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -251,17 +251,22 @@ def get_logs(
251251
# if we already know that the filter range is too large, split it
252252
if num_blocks > filter_block_range:
253253
results = []
254+
last_block_hash = filter_params.get("fromBlockParentHash")
254255
for filter_start in range(from_block, to_block + 1, filter_block_range):
255256
filter_end = min(filter_start + filter_block_range - 1, to_block)
256257
partial_filter = {
257258
**filter_params,
258259
"fromBlock": filter_start,
259260
"toBlock": filter_end,
260261
}
261-
if to_block_hash is not None and filter_end != to_block:
262-
del partial_filter["toBlockHash"]
263-
if from_block_parent_hash is not None and filter_start != from_block:
264-
del partial_filter["fromBlockParentHash"]
262+
if filter_end == to_block:
263+
to_block_hash = filter_params.get("toBlockHash")
264+
else:
265+
to_block_hash = self.get_block(filter_end)["hash"].to_0x_hex()
266+
partial_filter["fromBlockParentHash"] = last_block_hash
267+
partial_filter["toBlockHash"] = to_block_hash
268+
last_block_hash = to_block_hash
269+
265270
results += self.get_logs(partial_filter, **kwargs)
266271
return results
267272

@@ -303,10 +308,9 @@ def get_logs(
303308
mid_block = (from_block + to_block) // 2
304309
left_filter = {**filter_params, "toBlock": mid_block}
305310
right_filter = {**filter_params, "fromBlock": mid_block + 1}
306-
if to_block_hash is not None:
307-
del left_filter["toBlockHash"]
308-
if from_block_parent_hash is not None:
309-
del right_filter["fromBlockParentHash"]
311+
middle_block_hash = self.get_block(mid_block)["hash"].to_0x_hex()
312+
left_filter["toBlockHash"] = middle_block_hash
313+
right_filter["fromBlockParentHash"] = middle_block_hash
310314
return self.get_logs(left_filter, **kwargs) + self.get_logs(right_filter, **kwargs)
311315

312316
def get_logs_inner(self, filter_params: FilterParams, no_retry: bool = False):

0 commit comments

Comments
 (0)