Skip to content

Commit 06d488d

Browse files
authored
Merge pull request #269 from input-output-hk/compute_slots_offset
refactor: remove slots_offset and add dynamic calculation
2 parents 0f26571 + dccdae8 commit 06d488d

File tree

5 files changed

+26
-28
lines changed

5 files changed

+26
-28
lines changed

README.md

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,6 @@ The library needs working `cardano-cli` (the command is available on `PATH`, `ca
2929
cluster = clusterlib.ClusterLib(state_dir="path/to/cluster/state_dir")
3030
```
3131

32-
On custom testnets that were started in Byron era, you might need to specify a slots offset between Byron epochs and Shelley epochs.
33-
The "slots_offset" is a difference between number of slots in Byron epochs and in the same number of Shelley epochs.
34-
35-
E.g. for a testnet with parameters
36-
37-
* 100 slots per epoch in Byron era
38-
* 1000 slots per epoch in Shelley era
39-
* two epochs in Byron era before forking to Shelley
40-
41-
The offset will be `2 * (1000 - 100) = 1800`.
42-
43-
```python
44-
cluster = clusterlib.ClusterLib(state_dir="path/to/cluster/state_dir", slots_offset=1800)
45-
```
46-
4732
### Transfer funds
4833

4934
```python

cardano_clusterlib/clusterlib.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from cardano_clusterlib.consts import MAINNET_MAGIC
1313
from cardano_clusterlib.consts import MultiSigTypeArgs
1414
from cardano_clusterlib.consts import MultiSlotTypeArgs
15-
from cardano_clusterlib.consts import SLOTS_OFFSETS
1615
from cardano_clusterlib.consts import ScriptTypes
1716
from cardano_clusterlib.consts import Votes
1817
from cardano_clusterlib.coverage import record_cli_coverage

cardano_clusterlib/clusterlib_helpers.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,3 +333,17 @@ def wait_for_epoch(
333333

334334
LOGGER.debug(f"Expected epoch started; epoch number: {this_epoch}")
335335
return this_epoch
336+
337+
338+
def get_slots_offset(clusterlib_obj: "itp.ClusterLib") -> int:
339+
"""Get offset of slots from Byron era vs current configuration."""
340+
tip = clusterlib_obj.g_query.get_tip()
341+
slot = int(tip["slot"])
342+
slots_ep_end = int(tip["slotsToEpochEnd"])
343+
epoch = int(tip["epoch"])
344+
345+
slots_total = slot + slots_ep_end
346+
slots_shelley = int(clusterlib_obj.epoch_length) * (epoch + 1)
347+
348+
offset = slots_shelley - slots_total
349+
return offset

cardano_clusterlib/clusterlib_klass.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ class ClusterLib:
3636
Attributes:
3737
state_dir: A directory with cluster state files (keys, config files, logs, ...).
3838
protocol: A cluster protocol - full cardano mode by default.
39-
slots_offset: Difference in slots between cluster's start era and current era
40-
(e.g. Byron->Mary)
39+
slots_offset: Difference in slots between cluster's start era and Shelley era
40+
(Byron vs Shelley)
4141
socket_path: A path to socket file for communication with the node. This overrides the
4242
`CARDANO_NODE_SOCKET_PATH` environment variable.
4343
command_era: An era used for CLI commands, by default same as the latest network Era.
@@ -48,7 +48,7 @@ class ClusterLib:
4848
def __init__(
4949
self,
5050
state_dir: itp.FileType,
51-
slots_offset: int = 0,
51+
slots_offset: tp.Optional[int] = None,
5252
socket_path: itp.FileType = "",
5353
command_era: str = consts.CommandEras.LATEST,
5454
):
@@ -95,7 +95,8 @@ def __init__(
9595
else:
9696
self.magic_args = ["--testnet-magic", str(self.network_magic)]
9797

98-
self.slots_offset = slots_offset or consts.SLOTS_OFFSETS.get(self.network_magic) or 0
98+
self._slots_offset = slots_offset if slots_offset is not None else None
99+
99100
self.ttl_length = 1000
100101
# TODO: proper calculation based on `utxoCostPerWord` needed
101102
self._min_change_value = 1800_000
@@ -153,6 +154,13 @@ def cli_version(self) -> version.Version:
153154
self._cli_version = version.parse(version_str)
154155
return self._cli_version
155156

157+
@property
158+
def slots_offset(self) -> int:
159+
"""Get offset of slots from Byron era vs current configuration."""
160+
if self._slots_offset is None:
161+
self._slots_offset = clusterlib_helpers.get_slots_offset(clusterlib_obj=self)
162+
return self._slots_offset
163+
156164
@property
157165
def g_transaction(self) -> transaction_group.TransactionGroup:
158166
"""Transaction group."""

cardano_clusterlib/consts.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,6 @@
44
DEFAULT_COIN: tp.Final[str] = "lovelace"
55
MAINNET_MAGIC: tp.Final[int] = 764824073
66

7-
# offset of slots from Byron configuration vs current era configuration
8-
SLOTS_OFFSETS: tp.Final[tp.Dict[int, int]] = {
9-
764824073: 85363200, # mainnet
10-
1097911063: 30369600, # testnet
11-
1: 1641600, # preprod
12-
}
13-
14-
157
# The SUBCOMMAND_MARK is used to mark the beginning of a subcommand. It is used to differentiate
168
# between options and subcommands. That is needed for CLI coverage recording.
179
# For example, the command `cardano-cli query tx-mempool --cardano-mode info`

0 commit comments

Comments
 (0)