Skip to content

Commit 1bcf1f1

Browse files
authored
Merge pull request #211 from OpShin/feat/cache_chain_tip
Per default refetch the chain tip at most every 20 seconds
2 parents dda6da2 + b90d7a4 commit 1bcf1f1

File tree

3 files changed

+45
-24
lines changed

3 files changed

+45
-24
lines changed

pycardano/backend/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from pycardano.address import Address
99
from pycardano.network import Network
1010
from pycardano.plutus import ExecutionUnits
11-
from pycardano.transaction import UTxO, Transaction
11+
from pycardano.transaction import Transaction, UTxO
1212

1313
__all__ = [
1414
"GenesisParameters",

pycardano/backend/ogmios.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import time
23
from datetime import datetime, timezone
34
from enum import Enum
45
from typing import Any, Dict, List, Optional, Tuple, Union
@@ -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,14 +56,26 @@ def __init__(
5456
network: Network,
5557
compact_result=True,
5658
kupo_url=None,
59+
refetch_chain_tip_interval: Optional[float] = None,
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 = (
67+
refetch_chain_tip_interval
68+
if refetch_chain_tip_interval is not None
69+
else 1000
70+
)
71+
self._last_chain_tip_fetch = 0
6372
self._genesis_param = None
6473
self._protocol_param = None
74+
if refetch_chain_tip_interval is None:
75+
self._refetch_chain_tip_interval = (
76+
self.genesis_param.slot_length
77+
/ self.genesis_param.active_slots_coefficient
78+
)
6579

6680
def _request(self, method: OgmiosQueryType, args: JsonDict) -> Any:
6781
ws = websocket.WebSocket()
@@ -110,8 +124,12 @@ def _query_utxos_by_tx_id(self, tx_id: str, index: int) -> List[List[JsonDict]]:
110124
return self._request(OgmiosQueryType.Query, args)
111125

112126
def _is_chain_tip_updated(self):
127+
# fetch at most every twenty seconds!
128+
if time.time() - self._last_chain_tip_fetch < self._refetch_chain_tip_interval:
129+
return False
130+
self._last_chain_tip_fetch = time.time()
113131
slot = self.last_block_slot
114-
if self._last_known_block_slot != slot:
132+
if self._last_known_block_slot < slot:
115133
self._last_known_block_slot = slot
116134
return True
117135
else:

test/pycardano/backend/test_ogmios.py

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from unittest.mock import patch
2+
13
from pycardano.backend.base import GenesisParameters, ProtocolParameters
24
from pycardano.backend.ogmios import OgmiosChainContext
35
from pycardano.network import Network
@@ -80,30 +82,31 @@
8082
]
8183

8284

83-
class TestOgmiosChainContext:
84-
chain_context = OgmiosChainContext("", Network.TESTNET)
85-
86-
def override_request(method, args):
87-
if args["query"] == "currentProtocolParameters":
88-
return PROTOCOL_RESULT
89-
elif args["query"] == "genesisConfig":
90-
return GENESIS_RESULT
91-
elif "utxo" in args["query"]:
92-
query = args["query"]["utxo"][0]
93-
if isinstance(query, dict):
94-
for utxo in UTXOS:
95-
if (
96-
utxo[0]["txId"] == query["txId"]
97-
and utxo[0]["index"] == query["index"]
98-
):
99-
return [utxo]
100-
return []
101-
else:
102-
return UTXOS
85+
def override_request(method, args):
86+
if args["query"] == "currentProtocolParameters":
87+
return PROTOCOL_RESULT
88+
elif args["query"] == "genesisConfig":
89+
return GENESIS_RESULT
90+
elif "utxo" in args["query"]:
91+
query = args["query"]["utxo"][0]
92+
if isinstance(query, dict):
93+
for utxo in UTXOS:
94+
if (
95+
utxo[0]["txId"] == query["txId"]
96+
and utxo[0]["index"] == query["index"]
97+
):
98+
return [utxo]
99+
return []
103100
else:
104-
return None
101+
return UTXOS
102+
else:
103+
return None
105104

106-
chain_context._request = override_request
105+
106+
class TestOgmiosChainContext:
107+
def __init__(self):
108+
with patch("OgmiosChainContext._request", return_value=override_request):
109+
self.chain_context = OgmiosChainContext("", Network.TESTNET)
107110

108111
def test_protocol_param(self):
109112
assert (

0 commit comments

Comments
 (0)