Skip to content

Per default refetch the chain tip at most every 20 seconds #211

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

Merged
merged 5 commits into from
Apr 18, 2023
Merged
Changes from 1 commit
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
11 changes: 10 additions & 1 deletion pycardano/backend/ogmios.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from datetime import datetime, timezone
from enum import Enum
from typing import Any, Dict, List, Optional, Tuple, Union
import time

import cbor2
import requests
Expand Down Expand Up @@ -45,6 +46,7 @@ class OgmiosChainContext(ChainContext):
_service_name: str
_kupo_url: Optional[str]
_last_known_block_slot: int
_last_chain_tip_fetch: float
_genesis_param: Optional[GenesisParameters]
_protocol_param: Optional[ProtocolParameters]

Expand All @@ -54,12 +56,15 @@ def __init__(
network: Network,
compact_result=True,
kupo_url=None,
refetch_chain_tip_interval=20,
):
self._ws_url = ws_url
self._network = network
self._service_name = "ogmios.v1:compact" if compact_result else "ogmios"
self._kupo_url = kupo_url
self._last_known_block_slot = 0
self._refetch_chain_tip_interval = refetch_chain_tip_interval
self._last_chain_tip_fetch = 0
self._genesis_param = None
self._protocol_param = None

Expand Down Expand Up @@ -110,8 +115,12 @@ def _query_utxos_by_tx_id(self, tx_id: str, index: int) -> List[List[JsonDict]]:
return self._request(OgmiosQueryType.Query, args)

def _is_chain_tip_updated(self):
# fetch at most every twenty seconds!
if time.time() - self._last_chain_tip_fetch < self._refetch_chain_tip_interval:
return False
self._last_chain_tip_fetch = time.time()
slot = self.last_block_slot
if self._last_known_block_slot != slot:
if self._last_known_block_slot < slot:
self._last_known_block_slot = slot
return True
else:
Expand Down