Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions cardano_node_tests/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,16 +335,22 @@ def test_ledger_peer_snapshot(self, cluster: clusterlib.ClusterLib):
stake information.

* Execute `cardano-cli query ledger-peer-snapshot` command
* Verify response contains expected keys (bigLedgerPools, slotNo, version)
* Verify response contains expected keys
* Check that bigLedgerPools is a list
* Verify each pool entry contains relativeStake field if pools exist
"""
common.get_test_id(cluster)

peer_snapshot = cluster.g_query.get_ledger_peer_snapshot()
snapshot_keys = set(peer_snapshot)

expected_keys = {"bigLedgerPools", "slotNo", "version"}
missing_keys = expected_keys - set(peer_snapshot)
if "Point" in snapshot_keys:
# In cardano-node 10.7.0+
expected_keys = {"Point", "NetworkMagic", "NodeToClientVersion", "bigLedgerPools"}
else:
expected_keys = {"bigLedgerPools", "slotNo", "version"}

missing_keys = expected_keys - snapshot_keys
assert not missing_keys

pools = peer_snapshot.get("bigLedgerPools", [])
Expand Down
6 changes: 4 additions & 2 deletions cardano_node_tests/tests/test_delegation.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,8 @@ def test_deregister_delegated(
exc_value = str(excinfo.value)
with common.allow_unstable_error_messages():
assert (
"StakeKeyNonZeroAccountBalanceDELEG" in exc_value
"StakeKeyHasNonZeroAccountBalanceDELEG" in exc_value # In cardano-node 10.7.0+
or "StakeKeyNonZeroAccountBalanceDELEG" in exc_value
or "StakeKeyHasNonZeroRewardAccountBalanceDELEG" in exc_value
), exc_value

Expand Down Expand Up @@ -695,7 +696,8 @@ def test_delegate_multisig(
exc_value = str(excinfo.value)
with common.allow_unstable_error_messages():
assert (
"StakeKeyNonZeroAccountBalanceDELEG" in exc_value
"StakeKeyHasNonZeroAccountBalanceDELEG" in exc_value # In cardano-node 10.7.0+
or "StakeKeyNonZeroAccountBalanceDELEG" in exc_value
or "StakeKeyHasNonZeroRewardAccountBalanceDELEG" in exc_value
), exc_value

Expand Down
8 changes: 6 additions & 2 deletions cardano_node_tests/tests/test_ledger_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,17 @@ def test_stake_snapshot(self, cluster: clusterlib.ClusterLib): # noqa: C901
def _get_hashes(snapshot: str) -> dict[str, int]:
hashes = tp.cast(
dict[str, int],
clusterlib_utils.get_snapshot_rec(ledger_snapshot=es_snapshot[snapshot]["stake"]),
clusterlib_utils.get_snapshot_rec(
ledger_snapshot=clusterlib_utils.get_stake_rec(
stake_snapshot=es_snapshot[snapshot]
)
),
)
return hashes

def _get_delegations(snapshot: str) -> dict[str, list[str]]:
delegations: dict = clusterlib_utils.get_snapshot_delegations(
ledger_snapshot=es_snapshot[snapshot]["delegations"]
ledger_snapshot=es_snapshot[snapshot]
)
return delegations

Expand Down
36 changes: 27 additions & 9 deletions cardano_node_tests/tests/test_staking_rewards.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,13 +462,19 @@ def _check_ledger_state(
)

pstake_mark = clusterlib_utils.get_snapshot_rec(
ledger_snapshot=es_snapshot["pstakeMark"]["stake"]
ledger_snapshot=clusterlib_utils.get_stake_rec(
stake_snapshot=es_snapshot["pstakeMark"]
)
)
pstake_set = clusterlib_utils.get_snapshot_rec(
ledger_snapshot=es_snapshot["pstakeSet"]["stake"]
ledger_snapshot=clusterlib_utils.get_stake_rec(
stake_snapshot=es_snapshot["pstakeSet"]
)
)
pstake_go = clusterlib_utils.get_snapshot_rec(
ledger_snapshot=es_snapshot["pstakeGo"]["stake"]
ledger_snapshot=clusterlib_utils.get_stake_rec(
stake_snapshot=es_snapshot["pstakeGo"]
)
)

if this_epoch == init_epoch + 1:
Expand Down Expand Up @@ -716,13 +722,19 @@ def _check_ledger_state(
)

pstake_mark = clusterlib_utils.get_snapshot_rec(
ledger_snapshot=es_snapshot["pstakeMark"]["stake"]
ledger_snapshot=clusterlib_utils.get_stake_rec(
stake_snapshot=es_snapshot["pstakeMark"]
)
)
pstake_set = clusterlib_utils.get_snapshot_rec(
ledger_snapshot=es_snapshot["pstakeSet"]["stake"]
ledger_snapshot=clusterlib_utils.get_stake_rec(
stake_snapshot=es_snapshot["pstakeSet"]
)
)
pstake_go = clusterlib_utils.get_snapshot_rec(
ledger_snapshot=es_snapshot["pstakeGo"]["stake"]
ledger_snapshot=clusterlib_utils.get_stake_rec(
stake_snapshot=es_snapshot["pstakeGo"]
)
)

if this_epoch == init_epoch + 1:
Expand Down Expand Up @@ -1423,13 +1435,19 @@ def _check_ledger_state(
)

pstake_mark = clusterlib_utils.get_snapshot_rec(
ledger_snapshot=es_snapshot["pstakeMark"]["stake"]
ledger_snapshot=clusterlib_utils.get_stake_rec(
stake_snapshot=es_snapshot["pstakeMark"]
)
)
pstake_set = clusterlib_utils.get_snapshot_rec(
ledger_snapshot=es_snapshot["pstakeSet"]["stake"]
ledger_snapshot=clusterlib_utils.get_stake_rec(
stake_snapshot=es_snapshot["pstakeSet"]
)
)
pstake_go = clusterlib_utils.get_snapshot_rec(
ledger_snapshot=es_snapshot["pstakeGo"]["stake"]
ledger_snapshot=clusterlib_utils.get_stake_rec(
stake_snapshot=es_snapshot["pstakeGo"]
)
)

if this_epoch == init_epoch + 1:
Expand Down
84 changes: 66 additions & 18 deletions cardano_node_tests/utils/clusterlib_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,11 +529,11 @@ def wait_for_rewards(*, cluster_obj: clusterlib.ClusterLib) -> None:
def get_chain_account_state(*, ledger_state: dict) -> ChainAccount:
"""Get chain account state from ledger state dict."""
state_before = ledger_state["stateBefore"]
account_state = (
state_before.get("esChainAccountState") # In cardano-node >= 10.6.0
or state_before.get("esAccountState")
)
if account_state is None:
if "esChainAccountState" in state_before:
account_state = state_before["esChainAccountState"] # In cardano-node >= 10.6.0
elif "esAccountState" in state_before:
account_state = state_before["esAccountState"]
else:
err = "Neither 'esChainAccountState' nor 'esAccountState' found in ledger state"
raise KeyError(err)
return ChainAccount(reserves=account_state["reserves"], treasury=account_state["treasury"])
Expand Down Expand Up @@ -1459,32 +1459,80 @@ def get_snapshot_rec(*, ledger_snapshot: dict) -> dict[str, int | list]:
"""Get uniform record for ledger state snapshot."""
hashes: dict[str, int | list] = {}

for rk, r_value in ledger_snapshot.items():
# In node 8.4+ the format is not list of dicts, but a dict like
for rk, rv in ledger_snapshot.items():
# In node 10.7+ the format is a dict like
# "keyHash-7d75e921437e5a19963fc340b08d614dca2d3fe980d321a9682ee4f4": {
# "swdDelegation": "78e6546ed6cc1ecc4bf7fdc726dc6e1a7913f176ca03bf05dca5771e",
# "swdStake": 3480000000000
# }
# In node 8.4+ the format is a dict like
# {'keyHash-12d36d11cd0e570dde3c87360d4fb6074a1925e08a1a55513d7f7641': 1500000,
# 'scriptHash-9c8e9da7f81e3ca90485f32ebefc98137c8ac260a072a00c4aaf142d': 17998926079, ...}
# In older node version, the format is a list of dicts.
r_hash = rk.split("-")[1]
r_amount = rv["swdStake"] if isinstance(rv, dict) and "swdStake" in rv else rv

if r_hash in hashes:
hashes[r_hash] += r_value
hashes[r_hash] += r_amount
else:
hashes[r_hash] = r_value
hashes[r_hash] = r_amount

return hashes


def get_stake_rec(*, stake_snapshot: dict) -> dict:
"""Get uniform record for stake key snapshot."""
stake_rec: dict
if "activeStake" in stake_snapshot:
stake_rec = stake_snapshot["activeStake"] # In cardano-node 10.7.0+
elif "stake" in stake_snapshot:
stake_rec = stake_snapshot["stake"]
else:
err = "Neither 'activeStake' nor 'stake' found in stake snapshot"
raise KeyError(err)
return stake_rec


def get_snapshot_delegations(*, ledger_snapshot: dict) -> dict[str, list[str]]:
"""Get delegations data from ledger state snapshot."""
delegations: dict[str, list[str]] = {}

for rk, r_pool_id in ledger_snapshot.items():
# In node 8.4+ the format is not list of dicts, but dict like
# {'keyHash-12d36d11cd0e570dde3c87360d4fb6074a1925e08a1a55513d7f7641': POOL_ID,
# 'scriptHash-9c8e9da7f81e3ca90485f32ebefc98137c8ac260a072a00c4aaf142d: POOL_ID, ...}
r_hash = rk.split("-")[1]
if r_pool_id in delegations:
delegations[r_pool_id].append(r_hash)
else:
delegations[r_pool_id] = [r_hash]
if "activeStake" in ledger_snapshot:
deleg_rec = ledger_snapshot["activeStake"]

for rk, rv in deleg_rec.items():
# In node 10.7+ the format is a dict like
# "keyHash-7d75e921437e5a19963fc340b08d614dca2d3fe980d321a9682ee4f4": {
# "swdDelegation": "78e6546ed6cc1ecc4bf7fdc726dc6e1a7913f176ca03bf05dca5771e",
# "swdStake": 3480000000000
# }
r_hash = rk.split("-")[1]
r_pool_id = rv["swdDelegation"]

if r_pool_id in delegations:
delegations[r_pool_id].append(r_hash)
else:
delegations[r_pool_id] = [r_hash]

elif "delegations" in ledger_snapshot:
deleg_rec = ledger_snapshot["delegations"]

for rk, rv in deleg_rec.items():
# In node 8.4+ the format is a dict like
# {'keyHash-12d36d11cd0e570dde3c87360d4fb6074a1925e08a1a55513d7f7641': POOL_ID,
# 'scriptHash-9c8e9da7f81e3ca90485f32ebefc98137c8ac260a072a00c4aaf142d: POOL_ID, ...}
# In older node version, the format is a list of dicts.
r_hash = rk.split("-")[1]
r_pool_id = rv

if r_pool_id in delegations:
delegations[r_pool_id].append(r_hash)
else:
delegations[r_pool_id] = [r_hash]

else:
err = "Neither 'stakePoolsSnapShot' nor 'delegations' found in ledger snapshot"
raise KeyError(err)

return delegations

Expand Down
Loading