From 6725ee8e611d269b1cec0e0732fdbeb97aefa5e6 Mon Sep 17 00:00:00 2001 From: saratomaz Date: Thu, 5 Sep 2024 18:24:05 +0100 Subject: [PATCH] Check epoch_state table in dbsync --- cardano_node_tests/tests/reqs_conway.py | 2 ++ .../tests/tests_conway/test_committee.py | 10 +++++++ .../tests/tests_conway/test_constitution.py | 8 +++++ cardano_node_tests/utils/dbsync_queries.py | 23 ++++++++++++++ cardano_node_tests/utils/dbsync_utils.py | 30 +++++++++++++++++++ src_docs/chang_user_stories_template.rst | 7 +++++ src_docs/requirements_mapping.json | 3 +- 7 files changed, 82 insertions(+), 1 deletion(-) diff --git a/cardano_node_tests/tests/reqs_conway.py b/cardano_node_tests/tests/reqs_conway.py index cc5abaecc..c368a8ee6 100644 --- a/cardano_node_tests/tests/reqs_conway.py +++ b/cardano_node_tests/tests/reqs_conway.py @@ -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 diff --git a/cardano_node_tests/tests/tests_conway/test_committee.py b/cardano_node_tests/tests/tests_conway/test_committee.py index 3695f21b1..342dac0f2 100644 --- a/cardano_node_tests/tests/tests_conway/test_committee.py +++ b/cardano_node_tests/tests/tests_conway/test_committee.py @@ -1023,6 +1023,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=enact_epoch, txid=action_add_txid, change_type="committee" + ) + dbsync_utils.check_epoch_state( + epoch_no=rem_epoch, 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 diff --git a/cardano_node_tests/tests/tests_conway/test_constitution.py b/cardano_node_tests/tests/tests_conway/test_constitution.py index 87ce7d366..7df5e6b37 100644 --- a/cardano_node_tests/tests/tests_conway/test_constitution.py +++ b/cardano_node_tests/tests/tests_conway/test_constitution.py @@ -19,6 +19,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 @@ -554,3 +555,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() diff --git a/cardano_node_tests/utils/dbsync_queries.py b/cardano_node_tests/utils/dbsync_queries.py index 3854e23b2..82a8c6a27 100644 --- a/cardano_node_tests/utils/dbsync_queries.py +++ b/cardano_node_tests/utils/dbsync_queries.py @@ -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 @@ -1514,3 +1523,17 @@ 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) diff --git a/cardano_node_tests/utils/dbsync_utils.py b/cardano_node_tests/utils/dbsync_utils.py index b57368b0e..a7bb6c3c5 100644 --- a/cardano_node_tests/utils/dbsync_utils.py +++ b/cardano_node_tests/utils/dbsync_utils.py @@ -1524,3 +1524,33 @@ def check_off_chain_vote_fetch_error(voting_anchor_id: int) -> None: fetch_error_str = db_off_chain_vote_fetch_error[-1].fetch_error or "" assert "Hash mismatch when fetching metadata" in fetch_error_str + + +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}." + ) diff --git a/src_docs/chang_user_stories_template.rst b/src_docs/chang_user_stories_template.rst index 547e99031..1f25b3501 100644 --- a/src_docs/chang_user_stories_template.rst +++ b/src_docs/chang_user_stories_template.rst @@ -1347,6 +1347,11 @@ DB Sync - Conway related tables - |image-off_chain_vote_fetch_error| - Errors while fetching or validating offchain Voting Anchor metadata. `→ `__ + - + + - |image-epoch_state| + - Table with governance stats per epoch. + `→ `__ .. |Success Badge| image:: https://img.shields.io/badge/success-green .. |Failure Badge| image:: https://img.shields.io/badge/failure-red @@ -1753,3 +1758,5 @@ DB Sync - Conway related tables :target: https://github.com/off_chain_vote_external_update-404 .. |image-off_chain_vote_fetch_error| image:: https://img.shields.io/badge/off_chain_vote_fetch_error-grey :target: https://github.com/off_chain_vote_fetch_error-404 +.. |image-epoch_state| image:: https://img.shields.io/badge/epoch_state-grey + :target: https://github.com/epoch_state-404 diff --git a/src_docs/requirements_mapping.json b/src_docs/requirements_mapping.json index 352db8d65..0c7e9a20c 100644 --- a/src_docs/requirements_mapping.json +++ b/src_docs/requirements_mapping.json @@ -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"] } }