Skip to content

Commit

Permalink
Check epoch_state table in dbsync
Browse files Browse the repository at this point in the history
  • Loading branch information
saratomaz committed Sep 5, 2024
1 parent df015aa commit 4d82c5b
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 10 deletions.
2 changes: 2 additions & 0 deletions cardano_node_tests/tests/reqs_conway.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,5 @@ def __dr(id: str) -> requirements.Req:
db020 = __dr("off_chain_vote_external_update")
db021 = __dr("off_chain_vote_fetch_error")
db022 = __dr("reward_rest")
db023_01 = __dr("int-epoch_state-01") # related to committee
db023_02 = __dr("int-epoch_state-02") # related to constitution
34 changes: 25 additions & 9 deletions cardano_node_tests/tests/tests_conway/test_committee.py
Original file line number Diff line number Diff line change
Expand Up @@ -852,10 +852,11 @@ def _check_resign_dbsync(res_member: clusterlib.CCMember) -> None:
reqc.cip003.success()

# Check enactment of add action
_cur_epoch = cluster.wait_for_new_epoch(padding_seconds=5)
_cur_epoch_add_action = cluster.wait_for_new_epoch(padding_seconds=5)
enact_add_gov_state = cluster.g_conway_governance.query.gov_state()
conway_common.save_gov_state(
gov_state=enact_add_gov_state, name_template=f"{temp_template}_enact_add_{_cur_epoch}"
gov_state=enact_add_gov_state,
name_template=f"{temp_template}_enact_add_{_cur_epoch_add_action}",
)

reqc.cip073_03.start(url=helpers.get_vcs_link())
Expand All @@ -870,9 +871,11 @@ def _check_resign_dbsync(res_member: clusterlib.CCMember) -> None:
enact_add_committee_state = cluster.g_conway_governance.query.committee_state()
conway_common.save_committee_state(
committee_state=enact_add_committee_state,
name_template=f"{temp_template}_enact_add_{_cur_epoch}",
name_template=f"{temp_template}_enact_add_{_cur_epoch_add_action}",
)
_check_cc_member1_expired(
committee_state=enact_add_committee_state, curr_epoch=_cur_epoch_add_action
)
_check_cc_member1_expired(committee_state=enact_add_committee_state, curr_epoch=_cur_epoch)

_url = helpers.get_vcs_link()
[r.start(url=_url) for r in (reqc.cip009, reqc.cip010)]
Expand Down Expand Up @@ -913,7 +916,7 @@ def _check_resign_dbsync(res_member: clusterlib.CCMember) -> None:
reqc.cip038_01.success()

# The proposal was enacted, but it is already expired
assert _cur_epoch == actions_epoch + 3, "Unexpected epoch"
assert _cur_epoch_add_action == actions_epoch + 3, "Unexpected epoch"
with pytest.raises(clusterlib.CLIError) as excinfo:
conway_common.cast_vote(
cluster_obj=cluster,
Expand All @@ -939,23 +942,26 @@ def _check_resign_dbsync(res_member: clusterlib.CCMember) -> None:
), "CC Member is not marked for removal"

# Check enactment of removal action
_cur_epoch = cluster.wait_for_new_epoch(padding_seconds=5)
_cur_epoch_rem_action = cluster.wait_for_new_epoch(padding_seconds=5)
enact_rem_gov_state = cluster.g_conway_governance.query.gov_state()
conway_common.save_gov_state(
gov_state=enact_rem_gov_state, name_template=f"{temp_template}_enact_rem_{_cur_epoch}"
gov_state=enact_rem_gov_state,
name_template=f"{temp_template}_enact_rem_{_cur_epoch_rem_action}",
)
_check_cc_member2_removed(gov_state=enact_rem_gov_state)

# Check committee state after enactment of removal action
enact_rem_committee_state = cluster.g_conway_governance.query.committee_state()
conway_common.save_committee_state(
committee_state=enact_rem_committee_state,
name_template=f"{temp_template}_enact_rem_{_cur_epoch}",
name_template=f"{temp_template}_enact_rem_{_cur_epoch_rem_action}",
)
enact_rem_member_rec = enact_rem_committee_state["committee"].get(cc_member2_key)
assert not enact_rem_member_rec, "Removed committee member still present"

_check_cc_member1_expired(committee_state=enact_rem_committee_state, curr_epoch=_cur_epoch)
_check_cc_member1_expired(
committee_state=enact_rem_committee_state, curr_epoch=_cur_epoch_rem_action
)
reqc.cip011.success()

# Try to vote on enacted removal action
Expand Down Expand Up @@ -1020,6 +1026,16 @@ def _check_resign_dbsync(res_member: clusterlib.CCMember) -> None:
_msg = f"db-sync error: {dbsync_resign_err}"
raise AssertionError(_msg)

# Check epoch state in dbsync
reqc.db023_01.start(url=helpers.get_vcs_link())
dbsync_utils.check_epoch_state(
epoch_no=_cur_epoch_add_action, txid=action_add_txid, change_type="committee"
)
dbsync_utils.check_epoch_state(
epoch_no=_cur_epoch_rem_action, txid=action_rem_txid, change_type="committee"
)
reqc.db023_01.success()

@allure.link(helpers.get_vcs_link())
@pytest.mark.skipif(not configuration.HAS_CC, reason="Runs only on setup with CC")
@pytest.mark.long
Expand Down
8 changes: 8 additions & 0 deletions cardano_node_tests/tests/tests_conway/test_constitution.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from cardano_node_tests.utils import clusterlib_utils
from cardano_node_tests.utils import configuration
from cardano_node_tests.utils import dbsync_queries
from cardano_node_tests.utils import dbsync_utils
from cardano_node_tests.utils import governance_setup
from cardano_node_tests.utils import governance_utils
from cardano_node_tests.utils import helpers
Expand Down Expand Up @@ -568,3 +569,10 @@ def _check_cli_query():
assert constitution_db, "No new constitution proposal found in dbsync"
assert constitution_db[0].gov_action_type == "NewConstitution"
reqc.db012.success()

# Check epoch state in dbsync
reqc.db023_02.start(url=helpers.get_vcs_link())
dbsync_utils.check_epoch_state(
epoch_no=cluster.g_query.get_epoch(), txid=action_txid, change_type="constitution"
)
reqc.db023_02.success()
24 changes: 24 additions & 0 deletions cardano_node_tests/utils/dbsync_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,15 @@ class NewConstitutionInfoDBRow:
action_ix: int


@pydantic.dataclasses.dataclass(frozen=True)
class EpochStateDBRow:
id: int
committee_id: int
no_confidence_id: tp.Optional[int]
constitution_id: int
epoch_no: int


@contextlib.contextmanager
def execute(query: str, vars: tp.Sequence = ()) -> tp.Iterator[psycopg2.extensions.cursor]:
# pylint: disable=redefined-builtin
Expand Down Expand Up @@ -1514,3 +1523,18 @@ def query_new_constitution(txhash: str) -> tp.Generator[NewConstitutionInfoDBRow
with execute(query=query, vars=(rf"\x{txhash}",)) as cur:
while (result := cur.fetchone()) is not None:
yield NewConstitutionInfoDBRow(*result)


def query_epoch_state(epoch_no: int) -> tp.Generator[EpochStateDBRow, None, None]:
"""Query epoch_state table in db-sync."""
query = (
"SELECT "
" id, committee_id, no_confidence_id, constitution_id, epoch_no "
"FROM epoch_state "
"WHERE epoch_no = %s "
)

with execute(query=query, vars=(epoch_no,)) as cur:
while (result := cur.fetchone()) is not None:
yield EpochStateDBRow(*result)

30 changes: 30 additions & 0 deletions cardano_node_tests/utils/dbsync_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1507,3 +1507,33 @@ def check_delegation_vote(txhash: str, stake_address: str, drep: str) -> None:
assert delegation_vote_data.stake_address_hash_view == stake_address, (
"Incorrect delegation DRep: " f"{delegation_vote_data.drep_hash_view} vs {drep}"
)


def check_epoch_state(epoch_no: int, txid: str, change_type: str = "") -> None:
"""Check governance stats per epoch in dbsync."""
if not configuration.HAS_DBSYNC:
return

epoch_state_data = list(dbsync_queries.query_epoch_state(epoch_no=epoch_no))

if not epoch_state_data:
msg = f"No information about epoch state in dbsync for epoch: {epoch_no}"
raise AssertionError(msg)

if change_type == "committee":
dbsync_committee_info = list(dbsync_queries.query_new_committee_info(txhash=txid))[-1]
es_committee_id = epoch_state_data[0].committee_id
tx_committee_id = dbsync_committee_info.id
assert es_committee_id == tx_committee_id, (
f"Committee id mismatch between epoch_state {es_committee_id} "
f"and committee table {tx_committee_id}."
)

if change_type == "constitution":
dbsync_constitution_info = list(dbsync_queries.query_new_constitution(txhash=txid))[-1]
es_constitution_id = epoch_state_data[0].constitution_id
tx_constitution_id = dbsync_constitution_info.id
assert es_constitution_id == tx_constitution_id, (
f"Committee id mismatch between epoch_state {es_constitution_id} "
f"and committee table {tx_constitution_id}."
)
3 changes: 2 additions & 1 deletion src_docs/requirements_mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"CIP062": ["intCIP062-01", "intCIP062-02"],
"CIP064": ["intCIP064-01", "intCIP064-02", "intCIP064-03", "intCIP064-04"],
"CIP069": ["intCIP069en", "intCIP069ex"],
"CIP073": ["intCIP073-01", "intCIP073-02", "intCIP073-03", "intCIP073-04"]
"CIP073": ["intCIP073-01", "intCIP073-02", "intCIP073-03", "intCIP073-04"],
"epoch_state": ["int-epoch_state-01", "int-epoch_state-02"]
}
}

0 comments on commit 4d82c5b

Please sign in to comment.