forked from ethereum/web3.py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.py
444 lines (394 loc) · 14.9 KB
/
cache.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
import functools
import threading
import time
from typing import (
TYPE_CHECKING,
Any,
Callable,
Collection,
Dict,
Set,
Type,
cast,
)
import lru
from web3._utils.caching import (
generate_cache_key,
)
from web3._utils.compat import (
Literal,
TypedDict,
)
from web3.types import ( # noqa: F401
BlockData,
BlockNumber,
Middleware,
RPCEndpoint,
RPCResponse,
)
if TYPE_CHECKING:
from web3 import Web3 # noqa: F401
SIMPLE_CACHE_RPC_WHITELIST = cast(Set[RPCEndpoint], {
'web3_clientVersion',
'web3_sha3',
'net_version',
# 'net_peerCount',
# 'net_listening',
'eth_protocolVersion',
# 'eth_syncing',
# 'eth_coinbase',
# 'eth_mining',
# 'eth_hashrate',
# 'eth_gasPrice',
# 'eth_accounts',
# 'eth_blockNumber',
# 'eth_getBalance',
# 'eth_getStorageAt',
# 'eth_getTransactionCount',
'eth_getBlockTransactionCountByHash',
# 'eth_getBlockTransactionCountByNumber',
'eth_getUncleCountByBlockHash',
# 'eth_getUncleCountByBlockNumber',
# 'eth_getCode',
# 'eth_sign',
# 'eth_sendTransaction',
# 'eth_sendRawTransaction',
# 'eth_call',
# 'eth_estimateGas',
'eth_getBlockByHash',
# 'eth_getBlockByNumber',
'eth_getTransactionByHash',
'eth_getTransactionByBlockHashAndIndex',
# 'eth_getTransactionByBlockNumberAndIndex',
# 'eth_getTransactionReceipt',
'eth_getRawTransactionByHash',
'eth_getUncleByBlockHashAndIndex',
# 'eth_getUncleByBlockNumberAndIndex',
# 'eth_getCompilers',
# 'eth_compileLLL',
# 'eth_compileSolidity',
# 'eth_compileSerpent',
# 'eth_newFilter',
# 'eth_newBlockFilter',
# 'eth_newPendingTransactionFilter',
# 'eth_uninstallFilter',
# 'eth_getFilterChanges',
# 'eth_getFilterLogs',
# 'eth_getLogs',
# 'eth_getWork',
# 'eth_submitWork',
# 'eth_submitHashrate',
})
def _should_cache(method: RPCEndpoint, params: Any, response: RPCResponse) -> bool:
if 'error' in response:
return False
elif 'result' not in response:
return False
if response['result'] is None:
return False
return True
def construct_simple_cache_middleware(
cache_class: Type[Dict[Any, Any]],
rpc_whitelist: Collection[RPCEndpoint] = SIMPLE_CACHE_RPC_WHITELIST,
should_cache_fn: Callable[[RPCEndpoint, Any, RPCResponse], bool] = _should_cache
) -> Middleware:
"""
Constructs a middleware which caches responses based on the request
``method`` and ``params``
:param cache: Any dictionary-like object
:param rpc_whitelist: A set of RPC methods which may have their responses cached.
:param should_cache_fn: A callable which accepts ``method`` ``params`` and
``response`` and returns a boolean as to whether the response should be
cached.
"""
def simple_cache_middleware(
make_request: Callable[[RPCEndpoint, Any], RPCResponse], web3: "Web3"
) -> Callable[[RPCEndpoint, Any], RPCResponse]:
cache = cache_class()
lock = threading.Lock()
def middleware(
method: RPCEndpoint, params: Any
) -> RPCResponse:
lock_acquired = lock.acquire(blocking=False)
try:
if lock_acquired and method in rpc_whitelist:
cache_key = generate_cache_key((method, params))
if cache_key not in cache:
response = make_request(method, params)
if should_cache_fn(method, params, response):
cache[cache_key] = response
return response
return cache[cache_key]
else:
return make_request(method, params)
finally:
if lock_acquired:
lock.release()
return middleware
return simple_cache_middleware
_simple_cache_middleware = construct_simple_cache_middleware(
cache_class=cast(Type[Dict[Any, Any]], functools.partial(lru.LRU, 256)),
)
TIME_BASED_CACHE_RPC_WHITELIST = cast(Set[RPCEndpoint], {
# 'web3_clientVersion',
# 'web3_sha3',
# 'net_version',
# 'net_peerCount',
# 'net_listening',
# 'eth_protocolVersion',
# 'eth_syncing',
'eth_coinbase',
# 'eth_mining',
# 'eth_hashrate',
# 'eth_gasPrice',
'eth_accounts',
# 'eth_blockNumber',
# 'eth_getBalance',
# 'eth_getStorageAt',
# 'eth_getTransactionCount',
# 'eth_getBlockTransactionCountByHash',
# 'eth_getBlockTransactionCountByNumber',
# 'eth_getUncleCountByBlockHash',
# 'eth_getUncleCountByBlockNumber',
# 'eth_getCode',
# 'eth_sign',
# 'eth_sendTransaction',
# 'eth_sendRawTransaction',
# 'eth_call',
# 'eth_estimateGas',
# 'eth_getBlockByHash',
# 'eth_getBlockByNumber',
# 'eth_getTransactionByHash',
# 'eth_getTransactionByBlockHashAndIndex',
# 'eth_getTransactionByBlockNumberAndIndex',
# 'eth_getTransactionReceipt',
# 'eth_getUncleByBlockHashAndIndex',
# 'eth_getUncleByBlockNumberAndIndex',
# 'eth_getCompilers',
# 'eth_compileLLL',
# 'eth_compileSolidity',
# 'eth_compileSerpent',
# 'eth_newFilter',
# 'eth_newBlockFilter',
# 'eth_newPendingTransactionFilter',
# 'eth_uninstallFilter',
# 'eth_getFilterChanges',
# 'eth_getFilterLogs',
# 'eth_getLogs',
# 'eth_getWork',
# 'eth_submitWork',
# 'eth_submitHashrate',
})
def construct_time_based_cache_middleware(
cache_class: Callable[..., Dict[Any, Any]],
cache_expire_seconds: int = 15,
rpc_whitelist: Collection[RPCEndpoint] = TIME_BASED_CACHE_RPC_WHITELIST,
should_cache_fn: Callable[[RPCEndpoint, Any, RPCResponse], bool] = _should_cache
) -> Middleware:
"""
Constructs a middleware which caches responses based on the request
``method`` and ``params`` for a maximum amount of time as specified
:param cache: Any dictionary-like object
:param cache_expire_seconds: The number of seconds an item may be cached
before it should expire.
:param rpc_whitelist: A set of RPC methods which may have their responses cached.
:param should_cache_fn: A callable which accepts ``method`` ``params`` and
``response`` and returns a boolean as to whether the response should be
cached.
"""
def time_based_cache_middleware(
make_request: Callable[[RPCEndpoint, Any], Any], web3: "Web3"
) -> Callable[[RPCEndpoint, Any], RPCResponse]:
cache = cache_class()
lock = threading.Lock()
def middleware(method: RPCEndpoint, params: Any) -> RPCResponse:
lock_acquired = lock.acquire(blocking=False)
try:
if lock_acquired and method in rpc_whitelist:
cache_key = generate_cache_key((method, params))
if cache_key in cache:
# check that the cached response is not expired.
cached_at, cached_response = cache[cache_key]
cached_for = time.time() - cached_at
if cached_for <= cache_expire_seconds:
return cached_response
else:
del cache[cache_key]
# cache either missed or expired so make the request.
response = make_request(method, params)
if should_cache_fn(method, params, response):
cache[cache_key] = (time.time(), response)
return response
else:
return make_request(method, params)
finally:
if lock_acquired:
lock.release()
return middleware
return time_based_cache_middleware
_time_based_cache_middleware = construct_time_based_cache_middleware(
cache_class=functools.partial(lru.LRU, 256),
)
BLOCK_NUMBER_RPC_WHITELIST = cast(Set[RPCEndpoint], {
# 'web3_clientVersion',
# 'web3_sha3',
# 'net_version',
# 'net_peerCount',
# 'net_listening',
# 'eth_protocolVersion',
# 'eth_syncing',
# 'eth_coinbase',
# 'eth_mining',
# 'eth_hashrate',
'eth_gasPrice',
# 'eth_accounts',
'eth_blockNumber',
'eth_getBalance',
'eth_getStorageAt',
'eth_getTransactionCount',
# 'eth_getBlockTransactionCountByHash',
'eth_getBlockTransactionCountByNumber',
# 'eth_getUncleCountByBlockHash',
'eth_getUncleCountByBlockNumber',
'eth_getCode',
# 'eth_sign',
# 'eth_sendTransaction',
# 'eth_sendRawTransaction',
'eth_call',
'eth_estimateGas',
# 'eth_getBlockByHash',
'eth_getBlockByNumber',
# 'eth_getTransactionByHash',
# 'eth_getTransactionByBlockHashAndIndex',
'eth_getTransactionByBlockNumberAndIndex',
'eth_getTransactionReceipt',
# 'eth_getUncleByBlockHashAndIndex',
'eth_getUncleByBlockNumberAndIndex',
# 'eth_getCompilers',
# 'eth_compileLLL',
# 'eth_compileSolidity',
# 'eth_compileSerpent',
# 'eth_newFilter',
# 'eth_newBlockFilter',
# 'eth_newPendingTransactionFilter',
# 'eth_uninstallFilter',
# 'eth_getFilterChanges',
# 'eth_getFilterLogs',
'eth_getLogs',
# 'eth_getWork',
# 'eth_submitWork',
# 'eth_submitHashrate',
})
AVG_BLOCK_TIME_KEY: Literal['avg_block_time'] = 'avg_block_time'
AVG_BLOCK_SAMPLE_SIZE_KEY: Literal['avg_block_sample_size'] = 'avg_block_sample_size'
AVG_BLOCK_TIME_UPDATED_AT_KEY: Literal['avg_block_time_updated_at'] = 'avg_block_time_updated_at'
def _is_latest_block_number_request(method: RPCEndpoint, params: Any) -> bool:
if method != 'eth_getBlockByNumber':
return False
elif params[:1] == ['latest']:
return True
return False
BlockInfoCache = TypedDict("BlockInfoCache", {
"avg_block_time": float,
"avg_block_sample_size": int,
"avg_block_time_updated_at": float,
"latest_block": BlockData,
}, total=False)
def construct_latest_block_based_cache_middleware(
cache_class: Callable[..., Dict[Any, Any]],
rpc_whitelist: Collection[RPCEndpoint] = BLOCK_NUMBER_RPC_WHITELIST,
average_block_time_sample_size: int = 240,
default_average_block_time: int = 15,
should_cache_fn: Callable[[RPCEndpoint, Any, RPCResponse], bool] = _should_cache
) -> Middleware:
"""
Constructs a middleware which caches responses based on the request
``method``, ``params``, and the current latest block hash.
:param cache: Any dictionary-like object
:param cache_expire_seconds: The number of seconds an item may be cached
before it should expire.
:param rpc_whitelist: A set of RPC methods which may have their responses cached.
:param should_cache_fn: A callable which accepts ``method`` ``params`` and
``response`` and returns a boolean as to whether the response should be
cached.
.. note::
This middleware avoids re-fetching the current latest block for each
request by tracking the current average block time and only requesting
a new block when the last seen latest block is older than the average
block time.
"""
def latest_block_based_cache_middleware(
make_request: Callable[[RPCEndpoint, Any], Any], web3: "Web3"
) -> Callable[[RPCEndpoint, Any], RPCResponse]:
cache = cache_class()
block_info: BlockInfoCache = {}
def _update_block_info_cache() -> None:
avg_block_time = block_info.get(AVG_BLOCK_TIME_KEY, default_average_block_time)
avg_block_sample_size = block_info.get(AVG_BLOCK_SAMPLE_SIZE_KEY, 0)
avg_block_time_updated_at = block_info.get(AVG_BLOCK_TIME_UPDATED_AT_KEY, 0)
# compute age as counted by number of blocks since the avg_block_time
if avg_block_time == 0:
avg_block_time_age_in_blocks: float = avg_block_sample_size
else:
avg_block_time_age_in_blocks = (
(time.time() - avg_block_time_updated_at) / avg_block_time
)
if avg_block_time_age_in_blocks >= avg_block_sample_size:
# If the length of time since the average block time as
# measured by blocks is greater than or equal to the number of
# blocks sampled then we need to recompute the average block
# time.
latest_block = web3.eth.get_block('latest')
ancestor_block_number = BlockNumber(max(
0,
latest_block['number'] - average_block_time_sample_size,
))
ancestor_block = web3.eth.get_block(ancestor_block_number)
sample_size = latest_block['number'] - ancestor_block_number
block_info[AVG_BLOCK_SAMPLE_SIZE_KEY] = sample_size
if sample_size != 0:
block_info[AVG_BLOCK_TIME_KEY] = (
(latest_block['timestamp'] - ancestor_block['timestamp']) / sample_size
)
else:
block_info[AVG_BLOCK_TIME_KEY] = avg_block_time
block_info[AVG_BLOCK_TIME_UPDATED_AT_KEY] = time.time()
if 'latest_block' in block_info:
latest_block = block_info['latest_block']
time_since_latest_block = time.time() - latest_block['timestamp']
# latest block is too old so update cache
if time_since_latest_block > avg_block_time:
block_info['latest_block'] = web3.eth.get_block('latest')
else:
# latest block has not been fetched so we fetch it.
block_info['latest_block'] = web3.eth.get_block('latest')
lock = threading.Lock()
def middleware(method: RPCEndpoint, params: Any) -> RPCResponse:
lock_acquired = lock.acquire(blocking=False)
try:
should_try_cache = (
lock_acquired
and method in rpc_whitelist
and not _is_latest_block_number_request(method, params)
)
if should_try_cache:
_update_block_info_cache()
latest_block_hash = block_info['latest_block']['hash']
cache_key = generate_cache_key((latest_block_hash, method, params))
if cache_key in cache:
return cache[cache_key]
response = make_request(method, params)
if should_cache_fn(method, params, response):
cache[cache_key] = response
return response
else:
return make_request(method, params)
finally:
if lock_acquired:
lock.release()
return middleware
return latest_block_based_cache_middleware
_latest_block_based_cache_middleware = construct_latest_block_based_cache_middleware(
cache_class=functools.partial(lru.LRU, 256),
rpc_whitelist=BLOCK_NUMBER_RPC_WHITELIST,
)