Skip to content

Commit 8b8269a

Browse files
committed
Per default refetch the chain tip at most every 20 seconds
At the current state, every access to protocol params causes at least one chain tip fetch. Due to the potential delay of this request, often another protocol params fetch is triggered as well. This significantly (and mostly unnecessarily) slows down transaction building. This fix caches the chain tip for a default of 20 seconds (the average slot time), making sure that subsequent calls to _is_chain_tip_updated return without any delay, drastically speeding up tx building.
1 parent 6ec8623 commit 8b8269a

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

pycardano/backend/ogmios.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from datetime import datetime, timezone
33
from enum import Enum
44
from typing import Any, Dict, List, Optional, Tuple, Union
5+
import time
56

67
import cbor2
78
import requests
@@ -45,6 +46,7 @@ class OgmiosChainContext(ChainContext):
4546
_service_name: str
4647
_kupo_url: Optional[str]
4748
_last_known_block_slot: int
49+
_last_chain_tip_fetch: float
4850
_genesis_param: Optional[GenesisParameters]
4951
_protocol_param: Optional[ProtocolParameters]
5052

@@ -54,12 +56,15 @@ def __init__(
5456
network: Network,
5557
compact_result=True,
5658
kupo_url=None,
59+
refetch_chain_tip_interval=20,
5760
):
5861
self._ws_url = ws_url
5962
self._network = network
6063
self._service_name = "ogmios.v1:compact" if compact_result else "ogmios"
6164
self._kupo_url = kupo_url
6265
self._last_known_block_slot = 0
66+
self._refetch_chain_tip_interval = refetch_chain_tip_interval
67+
self._last_chain_tip_fetch = 0
6368
self._genesis_param = None
6469
self._protocol_param = None
6570

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

112117
def _is_chain_tip_updated(self):
118+
# fetch at most every twenty seconds!
119+
if time.time() - self._last_chain_tip_fetch < self._refetch_chain_tip_interval:
120+
return False
121+
self._last_chain_tip_fetch = time.time()
113122
slot = self.last_block_slot
114-
if self._last_known_block_slot != slot:
123+
if self._last_known_block_slot < slot:
115124
self._last_known_block_slot = slot
116125
return True
117126
else:

0 commit comments

Comments
 (0)