Skip to content

Commit 381b133

Browse files
committed
eth.get_logs now only gets last unstable blocks by their hashes and uses range filters for all blocks before. Unless get_logs_by_block_hash is set to True.
1 parent 4295414 commit 381b133

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

IceCreamSwapWeb3/EthAdvanced.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,20 @@ def get_logs(
196196
except Exception as e:
197197
print(f"Getting logs from SubSquid threw exception {repr(e)}, falling back to RPC")
198198

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:
200201
# getting logs via from and to block range sometimes drops logs.
201202
# This does not happen when getting them individually for each block by their block hash
202203
# 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+
203213
with self.w3.batch_requests() as batch:
204214
batch.add_mapping({
205215
self.w3.eth._get_block: list(range(from_block, to_block + 1))
@@ -212,10 +222,10 @@ def get_logs(
212222
block = blocks[i]
213223
if i != 0:
214224
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:
216226
if block["hash"] != from_block_body["hash"]:
217227
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:
219229
if block["hash"] != to_block_body["hash"]:
220230
raise ForkedBlock(f"expected={to_block_body['hash'].hex()}, actual={block['hash'].hex()}")
221231

@@ -230,7 +240,7 @@ def get_logs(
230240
assert len(results_per_block) == num_blocks
231241
if p_bar is not None:
232242
p_bar.update(len(blocks))
233-
results = sum(results_per_block, [])
243+
results += sum(results_per_block, [])
234244
return results
235245

236246
# getting logs for a single block, which is not at the chain head. No drama

0 commit comments

Comments
 (0)