Skip to content

Commit

Permalink
Merge pull request #1951 from input-output-hk/new_ledger_state_json
Browse files Browse the repository at this point in the history
Support the new ledger state JSON output
  • Loading branch information
mkoura authored Sep 19, 2023
2 parents 48e3958 + 6c0849b commit 8d3a047
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 148 deletions.
23 changes: 6 additions & 17 deletions cardano_node_tests/tests/test_ledger_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,15 @@ def test_stake_snapshot(self, cluster: clusterlib.ClusterLib): # noqa: C901
es_snapshot: dict = ledger_state["stateBefore"]["esSnapshots"]

def _get_hashes(snapshot: str) -> tp.Dict[str, int]:
hashes: tp.Dict[str, int] = {}
for r in es_snapshot[snapshot]["stake"]:
r_hash_rec = r[0]
r_hash = r_hash_rec.get("script hash") or r_hash_rec.get("key hash")
if r_hash in hashes:
hashes[r_hash] += r[1]
else:
hashes[r_hash] = r[1]
hashes: dict = clusterlib_utils.get_snapshot_rec(
ledger_snapshot=es_snapshot[snapshot]["stake"]
)
return hashes

def _get_delegations(snapshot: str) -> tp.Dict[str, tp.List[str]]:
delegations: tp.Dict[str, tp.List[str]] = {}
for r in es_snapshot[snapshot]["delegations"]:
r_hash_rec = r[0]
r_hash = r_hash_rec.get("script hash") or r_hash_rec.get("key hash")
r_pool_id = r[1]
if r_pool_id in delegations:
delegations[r_pool_id].append(r_hash)
else:
delegations[r_pool_id] = [r_hash]
delegations: dict = clusterlib_utils.get_snapshot_delegations(
ledger_snapshot=es_snapshot[snapshot]["delegations"]
)
return delegations

errors = []
Expand Down
198 changes: 67 additions & 131 deletions cardano_node_tests/tests/test_staking_rewards.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,46 +213,21 @@ def _dbsync_check_rewards(
return reward_db_record


def _get_rec_hash(rec: tp.List[dict]):
"""Get credential hash (key or script) from record."""
# make it fail when neither key nor script hash is present
return rec[0].get("key hash") or rec[0]["script hash"]


def _get_cred_hashes(rec: dict) -> tp.List[str]:
"""Get credential hashes in ledger state snapshot record."""
return [_get_rec_hash(r) for r in rec]


def _get_val_for_cred_hash(key_hash: str, rec: list) -> tp.Any:
"""Get value for credential hash in ledger state snapshot record."""
for r in rec:
if _get_rec_hash(r) == key_hash:
return r[1]
return None


def _get_rew_amount_for_cred_hash(key_hash: str, rec: list) -> int:
def _get_rew_amount_for_cred_hash(key_hash: str, rec: tp.Dict[str, tp.List[dict]]) -> int:
"""Get reward amount for credential hash in ledger state snapshot record."""
for r in rec:
if _get_rec_hash(r) != key_hash:
continue
rew_amount = 0
for sr in r[1]:
rew_amount += sr["rewardAmount"]
return rew_amount
return 0
r = rec.get(key_hash) or []
rew_amount = 0
for sr in r:
rew_amount += sr["rewardAmount"]
return rew_amount


def _get_rew_type_for_cred_hash(key_hash: str, rec: list) -> tp.List[str]:
def _get_rew_type_for_cred_hash(key_hash: str, rec: tp.Dict[str, tp.List[dict]]) -> tp.List[str]:
"""Get reward types for credential hash in ledger state snapshot record."""
rew_types = []
for r in rec:
if _get_rec_hash(r) != key_hash:
continue
for sr in r[1]:
rew_types.append(sr["rewardType"])
return rew_types
r = rec.get(key_hash) or []
for sr in r[1]:
rew_types.append(sr["rewardType"])
return rew_types


Expand Down Expand Up @@ -452,7 +427,9 @@ def _check_ledger_state(
ledger_state=ledger_state,
)
es_snapshot: dict = ledger_state["stateBefore"]["esSnapshots"]
rs_record: list = ledger_state["possibleRewardUpdate"]["rs"]
rs_record = clusterlib_utils.get_snapshot_rec(
ledger_snapshot=ledger_state["possibleRewardUpdate"]["rs"]
)
rs_records[this_epoch] = rs_record

# Make sure reward amount corresponds with ledger state.
Expand All @@ -469,9 +446,15 @@ def _check_ledger_state(
pool_reward_addr_dec, prev_rs_record
)

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

if this_epoch == init_epoch + 1:
assert pool_stake_addr_dec in pstake_mark
Expand All @@ -482,24 +465,15 @@ def _check_ledger_state(
assert user_stake_addr_dec not in pstake_go

# make sure ledger state and actual stake correspond
assert (
_get_val_for_cred_hash(user_stake_addr_dec, es_snapshot["pstakeMark"]["stake"])
== user_rewards[-1].stake_total
)
assert pstake_mark[user_stake_addr_dec] == user_rewards[-1].stake_total

if this_epoch == init_epoch + 2:
assert user_stake_addr_dec in pstake_mark
assert user_stake_addr_dec in pstake_set
assert user_stake_addr_dec not in pstake_go

assert (
_get_val_for_cred_hash(user_stake_addr_dec, es_snapshot["pstakeMark"]["stake"])
== user_rewards[-1].stake_total
)
assert (
_get_val_for_cred_hash(user_stake_addr_dec, es_snapshot["pstakeSet"]["stake"])
== user_rewards[-2].stake_total
)
assert pstake_mark[user_stake_addr_dec] == user_rewards[-1].stake_total
assert pstake_set[user_stake_addr_dec] == user_rewards[-2].stake_total

if this_epoch >= init_epoch + 2:
assert pool_stake_addr_dec in pstake_mark
Expand All @@ -511,18 +485,9 @@ def _check_ledger_state(
assert user_stake_addr_dec in pstake_set
assert user_stake_addr_dec in pstake_go

assert (
_get_val_for_cred_hash(user_stake_addr_dec, es_snapshot["pstakeMark"]["stake"])
== user_rewards[-1].stake_total
)
assert (
_get_val_for_cred_hash(user_stake_addr_dec, es_snapshot["pstakeSet"]["stake"])
== user_rewards[-2].stake_total
)
assert (
_get_val_for_cred_hash(user_stake_addr_dec, es_snapshot["pstakeGo"]["stake"])
== user_rewards[-3].stake_total
)
assert pstake_mark[user_stake_addr_dec] == user_rewards[-1].stake_total
assert pstake_set[user_stake_addr_dec] == user_rewards[-2].stake_total
assert pstake_go[user_stake_addr_dec] == user_rewards[-3].stake_total

LOGGER.info("Checking rewards for 9 epochs.")
for __ in range(9):
Expand Down Expand Up @@ -724,7 +689,9 @@ def _check_ledger_state(
ledger_state=ledger_state,
)
es_snapshot: dict = ledger_state["stateBefore"]["esSnapshots"]
rs_record: list = ledger_state["possibleRewardUpdate"]["rs"]
rs_record: tp.Dict[str, tp.Any] = clusterlib_utils.get_snapshot_rec(
ledger_snapshot=ledger_state["possibleRewardUpdate"]["rs"]
)
rs_records[this_epoch] = rs_record

# Make sure reward amount corresponds with ledger state.
Expand All @@ -740,76 +707,55 @@ def _check_ledger_state(
prev_recorded_reward + mir_reward,
)

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

if this_epoch == init_epoch + 1:
assert reward_addr_dec in pstake_mark
assert reward_addr_dec not in pstake_set
assert reward_addr_dec not in pstake_go

# make sure ledger state and actual stake correspond
assert (
_get_val_for_cred_hash(reward_addr_dec, es_snapshot["pstakeMark"]["stake"])
== reward_records[-1].reward_total
)
assert pstake_mark[reward_addr_dec] == reward_records[-1].reward_total

if this_epoch == init_epoch + 2:
assert reward_addr_dec in pstake_mark
assert reward_addr_dec in pstake_set
assert reward_addr_dec not in pstake_go

assert (
_get_val_for_cred_hash(reward_addr_dec, es_snapshot["pstakeMark"]["stake"])
== reward_records[-1].reward_total
)
assert (
_get_val_for_cred_hash(reward_addr_dec, es_snapshot["pstakeSet"]["stake"])
== reward_records[-2].reward_total
)
assert pstake_mark[reward_addr_dec] == reward_records[-1].reward_total
assert pstake_mark[reward_addr_dec] == reward_records[-2].reward_total

if init_epoch + 3 <= this_epoch <= init_epoch + 5:
assert reward_addr_dec in pstake_mark
assert reward_addr_dec in pstake_set
assert reward_addr_dec in pstake_go

assert (
_get_val_for_cred_hash(reward_addr_dec, es_snapshot["pstakeMark"]["stake"])
== reward_records[-1].reward_total
)
assert (
_get_val_for_cred_hash(reward_addr_dec, es_snapshot["pstakeSet"]["stake"])
== reward_records[-2].reward_total
)
assert (
_get_val_for_cred_hash(reward_addr_dec, es_snapshot["pstakeGo"]["stake"])
== reward_records[-3].reward_total
)
assert pstake_mark[reward_addr_dec] == reward_records[-1].reward_total
assert pstake_set[reward_addr_dec] == reward_records[-2].reward_total
assert pstake_go[reward_addr_dec] == reward_records[-3].reward_total

if this_epoch == init_epoch + 6:
assert reward_addr_dec not in pstake_mark
assert reward_addr_dec in pstake_set
assert reward_addr_dec in pstake_go

assert (
_get_val_for_cred_hash(reward_addr_dec, es_snapshot["pstakeSet"]["stake"])
== reward_records[-2].reward_total
)
assert (
_get_val_for_cred_hash(reward_addr_dec, es_snapshot["pstakeGo"]["stake"])
== reward_records[-3].reward_total
)
assert pstake_set[reward_addr_dec] == reward_records[-2].reward_total
assert pstake_go[reward_addr_dec] == reward_records[-3].reward_total

if this_epoch == init_epoch + 7:
assert reward_addr_dec not in pstake_mark
assert reward_addr_dec not in pstake_set
assert reward_addr_dec in pstake_go

assert (
_get_val_for_cred_hash(reward_addr_dec, es_snapshot["pstakeGo"]["stake"])
== reward_records[-3].reward_total
)
assert pstake_go[reward_addr_dec] == reward_records[-3].reward_total

if this_epoch > init_epoch + 7:
assert reward_addr_dec not in pstake_mark
Expand Down Expand Up @@ -1503,7 +1449,9 @@ def _check_ledger_state(
ledger_state=ledger_state,
)
es_snapshot: dict = ledger_state["stateBefore"]["esSnapshots"]
rs_record: list = ledger_state["possibleRewardUpdate"]["rs"]
rs_record = clusterlib_utils.get_snapshot_rec(
ledger_snapshot=ledger_state["possibleRewardUpdate"]["rs"]
)
rs_records[this_epoch] = rs_record

# Make sure reward amount corresponds with ledger state.
Expand All @@ -1515,52 +1463,40 @@ def _check_ledger_state(
stake_addr_dec, prev_rs_record
)

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

if this_epoch == init_epoch + 1:
assert stake_addr_dec in pstake_mark
assert stake_addr_dec not in pstake_set
assert stake_addr_dec not in pstake_go

# make sure ledger state and actual stake correspond
assert (
_get_val_for_cred_hash(stake_addr_dec, es_snapshot["pstakeMark"]["stake"])
== reward_records[-1].stake_total
)
assert pstake_mark[stake_addr_dec] == reward_records[-1].stake_total

if this_epoch == init_epoch + 2:
assert stake_addr_dec in pstake_mark
assert stake_addr_dec in pstake_set
assert stake_addr_dec not in pstake_go

assert (
_get_val_for_cred_hash(stake_addr_dec, es_snapshot["pstakeMark"]["stake"])
== reward_records[-1].stake_total
)
assert (
_get_val_for_cred_hash(stake_addr_dec, es_snapshot["pstakeSet"]["stake"])
== reward_records[-2].stake_total
)
assert pstake_mark[stake_addr_dec] == reward_records[-1].stake_total
assert pstake_set[stake_addr_dec] == reward_records[-2].stake_total

if this_epoch >= init_epoch + 3:
assert stake_addr_dec in pstake_mark
assert stake_addr_dec in pstake_set
assert stake_addr_dec in pstake_go

assert (
_get_val_for_cred_hash(stake_addr_dec, es_snapshot["pstakeMark"]["stake"])
== reward_records[-1].stake_total
)
assert (
_get_val_for_cred_hash(stake_addr_dec, es_snapshot["pstakeSet"]["stake"])
== reward_records[-2].stake_total
)
assert (
_get_val_for_cred_hash(stake_addr_dec, es_snapshot["pstakeGo"]["stake"])
== reward_records[-3].stake_total
)
assert pstake_mark[stake_addr_dec] == reward_records[-1].stake_total
assert pstake_set[stake_addr_dec] == reward_records[-2].stake_total
assert pstake_go[stake_addr_dec] == reward_records[-3].stake_total

LOGGER.info("Checking rewards for 8 epochs.")
withdrawal_past_epoch = False
Expand Down
Loading

0 comments on commit 8d3a047

Please sign in to comment.