Skip to content

Commit 8abaa1e

Browse files
committed
v0.3.0 SubSquid now gets logs for future blocks and caches them. This aligns the query block ranges with the SubSquid data batches and therefore uses less, bigger requests. Can be disabled by setting DISABLE_SUBSQUID_LOOKAHEAD_CACHE env to true
1 parent 061b5f8 commit 8abaa1e

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

IceCreamSwapWeb3/Subsquid.py

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from typing import cast
22

3+
import os
4+
import json
35
import requests
46
from .FastChecksumAddress import to_checksum_address
57
from hexbytes import HexBytes
@@ -33,10 +35,13 @@ def get_text(url: str) -> str:
3335
res.raise_for_status()
3436
return res.text
3537

38+
# getting up to the next 100k blocks in anticipation of future queries.
39+
future_logs_cache = {}
3640
def get_filter(
3741
chain_id: int,
3842
filter_params: FilterParams,
3943
partial_allowed=False,
44+
disable_subsquid_look_ahead_cache: bool = os.getenv("DISABLE_SUBSQUID_LOOKAHEAD_CACHE", "false").lower() == "true",
4045
p_bar = None
4146
) -> tuple[int, list[LogReceipt]]:
4247
endpoints = get_endpoints()
@@ -62,7 +67,7 @@ def get_filter(
6267
raise ValueError(f"Subsquid has only indexed till block {latest_block}")
6368

6469
query = {
65-
"toBlock": to_block,
70+
"toBlock": to_block if disable_subsquid_look_ahead_cache else to_block + 100_000,
6671
"logs": [{}],
6772
"fields": {
6873
"log": {
@@ -95,14 +100,38 @@ def get_filter(
95100

96101
logs: list[LogReceipt] = []
97102
while from_block <= to_block:
98-
worker_url = get_text(f'{gateway_url}/{from_block}/worker')
103+
cache_key_query = query.copy()
104+
cache_key_query.pop("fromBlock", None)
105+
cache_key_query.pop("toBlock")
106+
cache_key = json.dumps(cache_key_query)
99107

100-
query['fromBlock'] = from_block
101-
res = requests.post(worker_url, json=query)
102-
res.raise_for_status()
103-
blocks = res.json()
108+
if cache_key in future_logs_cache and future_logs_cache[cache_key]["fromBlock"] == from_block:
109+
blocks = future_logs_cache.pop(cache_key)["blocks"]
110+
else:
111+
worker_url = get_text(f'{gateway_url}/{from_block}/worker')
112+
113+
query['fromBlock'] = from_block
114+
res = requests.post(worker_url, json=query)
115+
res.raise_for_status()
116+
blocks = res.json()
117+
118+
# got more results than needed right now. Caching additional results
119+
if blocks[-1]['header']['number'] > to_block:
120+
if not disable_subsquid_look_ahead_cache:
121+
if len(future_logs_cache) > 10:
122+
# limiting future_logs_cache to 10 items
123+
future_logs_cache.pop(next(iter(future_logs_cache)))
124+
future_blocks = [block for block in blocks if block['header']['number'] > to_block]
125+
future_logs_cache[cache_key] = {
126+
"fromBlock": to_block + 1,
127+
"blocks": future_blocks,
128+
}
129+
blocks = [block for block in blocks if block['header']['number'] <= to_block]
130+
last_processed_block = to_block
131+
else:
132+
last_processed_block = blocks[-1]['header']['number']
104133

105-
last_processed_block = blocks[-1]['header']['number']
134+
assert last_processed_block <= to_block
106135
if p_bar is not None:
107136
p_bar.update(last_processed_block-from_block+1)
108137
from_block = last_processed_block + 1

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from setuptools import setup, find_packages
22

3-
VERSION = '0.2.8'
3+
VERSION = '0.3.0'
44
DESCRIPTION = 'IceCreamSwap Web3.py wrapper'
55
LONG_DESCRIPTION = 'IceCreamSwap Web3.py wrapper with automatic retries, multicall and other advanced functionality'
66

0 commit comments

Comments
 (0)