Skip to content

Commit

Permalink
Merge pull request input-output-hk#269 from input-output-hk/compute_s…
Browse files Browse the repository at this point in the history
…lots_offset

refactor: remove slots_offset and add dynamic calculation
  • Loading branch information
mkoura authored Sep 26, 2024
2 parents 0f26571 + dccdae8 commit 06d488d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 28 deletions.
15 changes: 0 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,6 @@ The library needs working `cardano-cli` (the command is available on `PATH`, `ca
cluster = clusterlib.ClusterLib(state_dir="path/to/cluster/state_dir")
```

On custom testnets that were started in Byron era, you might need to specify a slots offset between Byron epochs and Shelley epochs.
The "slots_offset" is a difference between number of slots in Byron epochs and in the same number of Shelley epochs.

E.g. for a testnet with parameters

* 100 slots per epoch in Byron era
* 1000 slots per epoch in Shelley era
* two epochs in Byron era before forking to Shelley

The offset will be `2 * (1000 - 100) = 1800`.

```python
cluster = clusterlib.ClusterLib(state_dir="path/to/cluster/state_dir", slots_offset=1800)
```

### Transfer funds

```python
Expand Down
1 change: 0 additions & 1 deletion cardano_clusterlib/clusterlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from cardano_clusterlib.consts import MAINNET_MAGIC
from cardano_clusterlib.consts import MultiSigTypeArgs
from cardano_clusterlib.consts import MultiSlotTypeArgs
from cardano_clusterlib.consts import SLOTS_OFFSETS
from cardano_clusterlib.consts import ScriptTypes
from cardano_clusterlib.consts import Votes
from cardano_clusterlib.coverage import record_cli_coverage
Expand Down
14 changes: 14 additions & 0 deletions cardano_clusterlib/clusterlib_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,3 +333,17 @@ def wait_for_epoch(

LOGGER.debug(f"Expected epoch started; epoch number: {this_epoch}")
return this_epoch


def get_slots_offset(clusterlib_obj: "itp.ClusterLib") -> int:
"""Get offset of slots from Byron era vs current configuration."""
tip = clusterlib_obj.g_query.get_tip()
slot = int(tip["slot"])
slots_ep_end = int(tip["slotsToEpochEnd"])
epoch = int(tip["epoch"])

slots_total = slot + slots_ep_end
slots_shelley = int(clusterlib_obj.epoch_length) * (epoch + 1)

offset = slots_shelley - slots_total
return offset
16 changes: 12 additions & 4 deletions cardano_clusterlib/clusterlib_klass.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class ClusterLib:
Attributes:
state_dir: A directory with cluster state files (keys, config files, logs, ...).
protocol: A cluster protocol - full cardano mode by default.
slots_offset: Difference in slots between cluster's start era and current era
(e.g. Byron->Mary)
slots_offset: Difference in slots between cluster's start era and Shelley era
(Byron vs Shelley)
socket_path: A path to socket file for communication with the node. This overrides the
`CARDANO_NODE_SOCKET_PATH` environment variable.
command_era: An era used for CLI commands, by default same as the latest network Era.
Expand All @@ -48,7 +48,7 @@ class ClusterLib:
def __init__(
self,
state_dir: itp.FileType,
slots_offset: int = 0,
slots_offset: tp.Optional[int] = None,
socket_path: itp.FileType = "",
command_era: str = consts.CommandEras.LATEST,
):
Expand Down Expand Up @@ -95,7 +95,8 @@ def __init__(
else:
self.magic_args = ["--testnet-magic", str(self.network_magic)]

self.slots_offset = slots_offset or consts.SLOTS_OFFSETS.get(self.network_magic) or 0
self._slots_offset = slots_offset if slots_offset is not None else None

self.ttl_length = 1000
# TODO: proper calculation based on `utxoCostPerWord` needed
self._min_change_value = 1800_000
Expand Down Expand Up @@ -153,6 +154,13 @@ def cli_version(self) -> version.Version:
self._cli_version = version.parse(version_str)
return self._cli_version

@property
def slots_offset(self) -> int:
"""Get offset of slots from Byron era vs current configuration."""
if self._slots_offset is None:
self._slots_offset = clusterlib_helpers.get_slots_offset(clusterlib_obj=self)
return self._slots_offset

@property
def g_transaction(self) -> transaction_group.TransactionGroup:
"""Transaction group."""
Expand Down
8 changes: 0 additions & 8 deletions cardano_clusterlib/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@
DEFAULT_COIN: tp.Final[str] = "lovelace"
MAINNET_MAGIC: tp.Final[int] = 764824073

# offset of slots from Byron configuration vs current era configuration
SLOTS_OFFSETS: tp.Final[tp.Dict[int, int]] = {
764824073: 85363200, # mainnet
1097911063: 30369600, # testnet
1: 1641600, # preprod
}


# The SUBCOMMAND_MARK is used to mark the beginning of a subcommand. It is used to differentiate
# between options and subcommands. That is needed for CLI coverage recording.
# For example, the command `cardano-cli query tx-mempool --cardano-mode info`
Expand Down

0 comments on commit 06d488d

Please sign in to comment.