Skip to content

Commit bae4341

Browse files
REFACTOR. pull out genesis and protocol param fetching logic to be independently testable
REFACTOR. renaming chain tip status checking helper method
1 parent 6f85d49 commit bae4341

File tree

1 file changed

+70
-67
lines changed

1 file changed

+70
-67
lines changed

pycardano/backend/ogmios.py

+70-67
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def _query_utxos_by_tx_id(self, tx_id: str, index: int) -> List[List[JSON]]:
112112
args = {"query": {"utxo": [{"txId": tx_id, "index": index}]}}
113113
return self._request(OgmiosQueryType.Query, args)
114114

115-
def _check_chain_tip_and_update(self):
115+
def _is_chain_tip_updated(self):
116116
slot = self.last_block_slot
117117
if self._last_known_block_slot != slot:
118118
self._last_known_block_slot = slot
@@ -128,81 +128,84 @@ def _fraction_parser(fraction: str) -> float:
128128
@property
129129
def protocol_param(self) -> ProtocolParameters:
130130
"""Get current protocol parameters"""
131-
if not self._protocol_param or self._check_chain_tip_and_update():
132-
result = self._query_current_protocol_params()
133-
param = ProtocolParameters(
134-
min_fee_constant=result["minFeeConstant"],
135-
min_fee_coefficient=result["minFeeCoefficient"],
136-
max_block_size=result["maxBlockBodySize"],
137-
max_tx_size=result["maxTxSize"],
138-
max_block_header_size=result["maxBlockHeaderSize"],
139-
key_deposit=result["stakeKeyDeposit"],
140-
pool_deposit=result["poolDeposit"],
141-
pool_influence=self._fraction_parser(result["poolInfluence"]),
142-
monetary_expansion=self._fraction_parser(result["monetaryExpansion"]),
143-
treasury_expansion=self._fraction_parser(result["treasuryExpansion"]),
144-
decentralization_param=self._fraction_parser(
145-
result.get("decentralizationParameter", "0/1")
146-
),
147-
extra_entropy=result.get("extraEntropy", ""),
148-
protocol_major_version=result["protocolVersion"]["major"],
149-
protocol_minor_version=result["protocolVersion"]["minor"],
150-
min_pool_cost=result["minPoolCost"],
151-
price_mem=self._fraction_parser(result["prices"]["memory"]),
152-
price_step=self._fraction_parser(result["prices"]["steps"]),
153-
max_tx_ex_mem=result["maxExecutionUnitsPerTransaction"]["memory"],
154-
max_tx_ex_steps=result["maxExecutionUnitsPerTransaction"]["steps"],
155-
max_block_ex_mem=result["maxExecutionUnitsPerBlock"]["memory"],
156-
max_block_ex_steps=result["maxExecutionUnitsPerBlock"]["steps"],
157-
max_val_size=result["maxValueSize"],
158-
collateral_percent=result["collateralPercentage"],
159-
max_collateral_inputs=result["maxCollateralInputs"],
160-
coins_per_utxo_word=result.get(
161-
"coinsPerUtxoWord", ALONZO_COINS_PER_UTXO_WORD
162-
),
163-
coins_per_utxo_byte=result.get("coinsPerUtxoByte", 0),
164-
cost_models=result.get("costModels", {}),
165-
)
131+
if not self._protocol_param or self._is_chain_tip_updated():
132+
self._protocol_param = self._fetch_protocol_param()
133+
return self._protocol_param
166134

167-
if "plutus:v1" in param.cost_models:
168-
param.cost_models["PlutusV1"] = param.cost_models.pop("plutus:v1")
169-
if "plutus:v2" in param.cost_models:
170-
param.cost_models["PlutusV2"] = param.cost_models.pop("plutus:v2")
135+
def _fetch_protocol_param(self) -> ProtocolParameters:
136+
result = self._query_current_protocol_params()
137+
param = ProtocolParameters(
138+
min_fee_constant=result["minFeeConstant"],
139+
min_fee_coefficient=result["minFeeCoefficient"],
140+
max_block_size=result["maxBlockBodySize"],
141+
max_tx_size=result["maxTxSize"],
142+
max_block_header_size=result["maxBlockHeaderSize"],
143+
key_deposit=result["stakeKeyDeposit"],
144+
pool_deposit=result["poolDeposit"],
145+
pool_influence=self._fraction_parser(result["poolInfluence"]),
146+
monetary_expansion=self._fraction_parser(result["monetaryExpansion"]),
147+
treasury_expansion=self._fraction_parser(result["treasuryExpansion"]),
148+
decentralization_param=self._fraction_parser(
149+
result.get("decentralizationParameter", "0/1")
150+
),
151+
extra_entropy=result.get("extraEntropy", ""),
152+
protocol_major_version=result["protocolVersion"]["major"],
153+
protocol_minor_version=result["protocolVersion"]["minor"],
154+
min_pool_cost=result["minPoolCost"],
155+
price_mem=self._fraction_parser(result["prices"]["memory"]),
156+
price_step=self._fraction_parser(result["prices"]["steps"]),
157+
max_tx_ex_mem=result["maxExecutionUnitsPerTransaction"]["memory"],
158+
max_tx_ex_steps=result["maxExecutionUnitsPerTransaction"]["steps"],
159+
max_block_ex_mem=result["maxExecutionUnitsPerBlock"]["memory"],
160+
max_block_ex_steps=result["maxExecutionUnitsPerBlock"]["steps"],
161+
max_val_size=result["maxValueSize"],
162+
collateral_percent=result["collateralPercentage"],
163+
max_collateral_inputs=result["maxCollateralInputs"],
164+
coins_per_utxo_word=result.get(
165+
"coinsPerUtxoWord", ALONZO_COINS_PER_UTXO_WORD
166+
),
167+
coins_per_utxo_byte=result.get("coinsPerUtxoByte", 0),
168+
cost_models=result.get("costModels", {}),
169+
)
171170

172-
result = self._query_genesis_config()
173-
param.min_utxo = result["protocolParameters"]["minUtxoValue"]
171+
if "plutus:v1" in param.cost_models:
172+
param.cost_models["PlutusV1"] = param.cost_models.pop("plutus:v1")
173+
if "plutus:v2" in param.cost_models:
174+
param.cost_models["PlutusV2"] = param.cost_models.pop("plutus:v2")
174175

175-
self._protocol_param = param
176-
return self._protocol_param
176+
result = self._query_genesis_config()
177+
param.min_utxo = result["protocolParameters"]["minUtxoValue"]
178+
return param
177179

178180
@property
179181
def genesis_param(self) -> GenesisParameters:
180182
"""Get chain genesis parameters"""
181-
if not self._genesis_param or self._check_chain_tip_and_update():
182-
result = self._query_genesis_config()
183-
system_start_unix = int(
184-
calendar.timegm(
185-
time.strptime(
186-
result["systemStart"].split(".")[0], "%Y-%m-%dT%H:%M:%S"
187-
),
188-
)
189-
)
190-
self._genesis_param = GenesisParameters(
191-
active_slots_coefficient=self._fraction_parser(
192-
result["activeSlotsCoefficient"]
193-
),
194-
update_quorum=result["updateQuorum"],
195-
max_lovelace_supply=result["maxLovelaceSupply"],
196-
network_magic=result["networkMagic"],
197-
epoch_length=result["epochLength"],
198-
system_start=system_start_unix,
199-
slots_per_kes_period=result["slotsPerKesPeriod"],
200-
slot_length=result["slotLength"],
201-
max_kes_evolutions=result["maxKesEvolutions"],
202-
security_param=result["securityParameter"],
203-
)
183+
if not self._genesis_param or self._is_chain_tip_updated():
184+
self._genesis_param = self._fetch_genesis_param()
204185
return self._genesis_param
205186

187+
def _fetch_genesis_param(self) -> GenesisParameters:
188+
result = self._query_genesis_config()
189+
system_start_unix = int(
190+
calendar.timegm(
191+
time.strptime(result["systemStart"].split(".")[0], "%Y-%m-%dT%H:%M:%S"),
192+
)
193+
)
194+
return GenesisParameters(
195+
active_slots_coefficient=self._fraction_parser(
196+
result["activeSlotsCoefficient"]
197+
),
198+
update_quorum=result["updateQuorum"],
199+
max_lovelace_supply=result["maxLovelaceSupply"],
200+
network_magic=result["networkMagic"],
201+
epoch_length=result["epochLength"],
202+
system_start=system_start_unix,
203+
slots_per_kes_period=result["slotsPerKesPeriod"],
204+
slot_length=result["slotLength"],
205+
max_kes_evolutions=result["maxKesEvolutions"],
206+
security_param=result["securityParameter"],
207+
)
208+
206209
@property
207210
def network(self) -> Network:
208211
"""Get current network"""

0 commit comments

Comments
 (0)