Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More ogmios context optimizations #249

Merged
merged 1 commit into from
May 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions pycardano/backend/ogmios.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import json
import time
from copy import deepcopy
from datetime import datetime, timezone
from enum import Enum
from typing import Any, Dict, List, Optional, Tuple, Union

import cbor2
import requests
import websocket
from cachetools import Cache, LRUCache, TTLCache
from cachetools import Cache, LRUCache, TTLCache, func

from pycardano.address import Address
from pycardano.backend.base import (
Expand Down Expand Up @@ -214,9 +213,12 @@ def _parse_cost_models(self, ogmios_result: JsonDict) -> Dict[str, Dict[str, int
@property
def genesis_param(self) -> GenesisParameters:
"""Get chain genesis parameters"""
if not self._genesis_param or self._is_chain_tip_updated():
self._genesis_param = self._fetch_genesis_param()
return self._genesis_param

@func.lru_cache(maxsize=10)
def _genesis_param_cache(slot) -> GenesisParameters:
return self._fetch_genesis_param()

return _genesis_param_cache(self.last_block_slot)

def _fetch_genesis_param(self) -> GenesisParameters:
result = self._query_genesis_config()
Expand Down Expand Up @@ -250,10 +252,11 @@ def epoch(self) -> int:
return self._query_current_epoch()

@property
@func.ttl_cache(ttl=1)
def last_block_slot(self) -> int:
"""Slot number of last block"""
result = self._query_chain_tip()
return result["slot"]
slot = result["slot"]
return slot

def _utxos(self, address: str) -> List[UTxO]:
"""Get all UTxOs associated with an address.
Expand All @@ -264,15 +267,16 @@ def _utxos(self, address: str) -> List[UTxO]:
Returns:
List[UTxO]: A list of UTxOs.
"""
if (self.last_block_slot, address) in self._utxo_cache:
return deepcopy(self._utxo_cache[(self.last_block_slot, address)])
key = (self.last_block_slot, address)
if key in self._utxo_cache:
return self._utxo_cache[key]

if self._kupo_url:
utxos = self._utxos_kupo(address)
else:
utxos = self._utxos_ogmios(address)

self._utxo_cache[(self.last_block_slot, address)] = deepcopy(utxos)
self._utxo_cache[key] = utxos

return utxos

Expand All @@ -287,9 +291,7 @@ def _get_datum_from_kupo(self, datum_hash: str) -> Optional[RawCBOR]:
"""
datum = self._datum_cache.get(datum_hash, None)

if datum is not None or (
datum_hash in self._datum_cache and not self._is_chain_tip_updated()
):
if datum is not None:
return datum

if self._kupo_url is None:
Expand Down