11import json
22import time
3- from copy import deepcopy
43from datetime import datetime , timezone
54from enum import Enum
65from typing import Any , Dict , List , Optional , Tuple , Union
76
87import cbor2
98import requests
109import websocket
11- from cachetools import Cache , LRUCache , TTLCache
10+ from cachetools import Cache , LRUCache , TTLCache , func
1211
1312from pycardano .address import Address
1413from pycardano .backend .base import (
@@ -214,9 +213,12 @@ def _parse_cost_models(self, ogmios_result: JsonDict) -> Dict[str, Dict[str, int
214213 @property
215214 def genesis_param (self ) -> GenesisParameters :
216215 """Get chain genesis parameters"""
217- if not self ._genesis_param or self ._is_chain_tip_updated ():
218- self ._genesis_param = self ._fetch_genesis_param ()
219- return self ._genesis_param
216+
217+ @func .lru_cache (maxsize = 10 )
218+ def _genesis_param_cache (slot ) -> GenesisParameters :
219+ return self ._fetch_genesis_param ()
220+
221+ return _genesis_param_cache (self .last_block_slot )
220222
221223 def _fetch_genesis_param (self ) -> GenesisParameters :
222224 result = self ._query_genesis_config ()
@@ -250,10 +252,11 @@ def epoch(self) -> int:
250252 return self ._query_current_epoch ()
251253
252254 @property
255+ @func .ttl_cache (ttl = 1 )
253256 def last_block_slot (self ) -> int :
254- """Slot number of last block"""
255257 result = self ._query_chain_tip ()
256- return result ["slot" ]
258+ slot = result ["slot" ]
259+ return slot
257260
258261 def _utxos (self , address : str ) -> List [UTxO ]:
259262 """Get all UTxOs associated with an address.
@@ -264,15 +267,16 @@ def _utxos(self, address: str) -> List[UTxO]:
264267 Returns:
265268 List[UTxO]: A list of UTxOs.
266269 """
267- if (self .last_block_slot , address ) in self ._utxo_cache :
268- return deepcopy (self ._utxo_cache [(self .last_block_slot , address )])
270+ key = (self .last_block_slot , address )
271+ if key in self ._utxo_cache :
272+ return self ._utxo_cache [key ]
269273
270274 if self ._kupo_url :
271275 utxos = self ._utxos_kupo (address )
272276 else :
273277 utxos = self ._utxos_ogmios (address )
274278
275- self ._utxo_cache [( self . last_block_slot , address ) ] = deepcopy ( utxos )
279+ self ._utxo_cache [key ] = utxos
276280
277281 return utxos
278282
@@ -287,9 +291,7 @@ def _get_datum_from_kupo(self, datum_hash: str) -> Optional[RawCBOR]:
287291 """
288292 datum = self ._datum_cache .get (datum_hash , None )
289293
290- if datum is not None or (
291- datum_hash in self ._datum_cache and not self ._is_chain_tip_updated ()
292- ):
294+ if datum is not None :
293295 return datum
294296
295297 if self ._kupo_url is None :
0 commit comments