Skip to content

Commit

Permalink
Use Fraction for protocol parameters (#314)
Browse files Browse the repository at this point in the history
* Use Fraction for protocol parameters

* Fix testcase
  • Loading branch information
nielstron authored Feb 20, 2024
1 parent 4b1c1d4 commit 997d3d2
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 25 deletions.
15 changes: 8 additions & 7 deletions pycardano/backend/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Defines interfaces for client codes to interact (read/write) with the blockchain."""

from dataclasses import dataclass
from fractions import Fraction
from typing import Dict, List, Union

from pycardano.address import Address
Expand All @@ -24,7 +25,7 @@
class GenesisParameters:
"""Cardano genesis parameters"""

active_slots_coefficient: float
active_slots_coefficient: Fraction

update_quorum: int

Expand Down Expand Up @@ -63,13 +64,13 @@ class ProtocolParameters:

pool_deposit: int

pool_influence: float
pool_influence: Fraction

monetary_expansion: float
monetary_expansion: Fraction

treasury_expansion: float
treasury_expansion: Fraction

decentralization_param: float
decentralization_param: Fraction

extra_entropy: str

Expand All @@ -81,9 +82,9 @@ class ProtocolParameters:

min_pool_cost: int

price_mem: float
price_mem: Fraction

price_step: float
price_step: Fraction

max_tx_ex_mem: int

Expand Down
13 changes: 7 additions & 6 deletions pycardano/backend/blockfrost.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import tempfile
import time
import warnings
from fractions import Fraction
from typing import Dict, List, Optional, Union

import cbor2
Expand Down Expand Up @@ -133,17 +134,17 @@ def protocol_param(self) -> ProtocolParameters:
max_block_header_size=int(params.max_block_header_size),
key_deposit=int(params.key_deposit),
pool_deposit=int(params.pool_deposit),
pool_influence=float(params.a0),
monetary_expansion=float(params.rho),
treasury_expansion=float(params.tau),
decentralization_param=float(params.decentralisation_param),
pool_influence=Fraction(params.a0),
monetary_expansion=Fraction(params.rho),
treasury_expansion=Fraction(params.tau),
decentralization_param=Fraction(params.decentralisation_param),
extra_entropy=params.extra_entropy,
protocol_major_version=int(params.protocol_major_ver),
protocol_minor_version=int(params.protocol_minor_ver),
min_utxo=int(params.min_utxo),
min_pool_cost=int(params.min_pool_cost),
price_mem=float(params.price_mem),
price_step=float(params.price_step),
price_mem=Fraction(params.price_mem),
price_step=Fraction(params.price_step),
max_tx_ex_mem=int(params.max_tx_ex_mem),
max_tx_ex_steps=int(params.max_tx_ex_steps),
max_block_ex_mem=int(params.max_block_ex_mem),
Expand Down
2 changes: 1 addition & 1 deletion pycardano/backend/cardano_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def __init__(
self._genesis_param = None
self._protocol_param = None
if refetch_chain_tip_interval is None:
self._refetch_chain_tip_interval = (
self._refetch_chain_tip_interval = float(
self.genesis_param.slot_length
/ self.genesis_param.active_slots_coefficient
)
Expand Down
8 changes: 4 additions & 4 deletions pycardano/backend/ogmios.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import time
from datetime import datetime, timezone
from enum import Enum
from fractions import Fraction
from typing import Any, Dict, List, Optional, Tuple, Union

import requests
Expand Down Expand Up @@ -77,7 +78,7 @@ def __init__(
self._genesis_param = None
self._protocol_param = None
if refetch_chain_tip_interval is None:
self._refetch_chain_tip_interval = (
self._refetch_chain_tip_interval = float(
self.genesis_param.slot_length
/ self.genesis_param.active_slots_coefficient
)
Expand Down Expand Up @@ -146,9 +147,8 @@ def _is_chain_tip_updated(self):
return False

@staticmethod
def _fraction_parser(fraction: str) -> float:
x, y = fraction.split("/")
return int(x) / int(y)
def _fraction_parser(fraction: str) -> Fraction:
return Fraction(fraction)

@property
def protocol_param(self) -> ProtocolParameters:
Expand Down
15 changes: 8 additions & 7 deletions test/pycardano/backend/test_ogmios.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from fractions import Fraction
from unittest.mock import patch

import pytest
Expand Down Expand Up @@ -130,17 +131,17 @@ def test_protocol_param(self, chain_context):
max_block_header_size=1100,
key_deposit=0,
pool_deposit=0,
pool_influence=0.0,
monetary_expansion=0.1,
treasury_expansion=0.1,
decentralization_param=1.0,
pool_influence=Fraction("0"),
monetary_expansion=Fraction("1/10"),
treasury_expansion=Fraction("1/10"),
decentralization_param=Fraction("1"),
extra_entropy="neutral",
protocol_major_version=5,
protocol_minor_version=0,
min_utxo=1000000,
min_pool_cost=0,
price_mem=0.1,
price_step=0.1,
price_mem=Fraction("1/10"),
price_step=Fraction("1/10"),
max_tx_ex_mem=500000000000,
max_tx_ex_steps=500000000000,
max_block_ex_mem=500000000000,
Expand All @@ -158,7 +159,7 @@ def test_protocol_param(self, chain_context):
def test_genesis(self, chain_context):
assert (
GenesisParameters(
active_slots_coefficient=0.1,
active_slots_coefficient=Fraction("1/10"),
update_quorum=2,
max_lovelace_supply=1000000000000,
network_magic=42,
Expand Down

0 comments on commit 997d3d2

Please sign in to comment.