Skip to content

Check epoch_state table in dbsync #2598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
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 @@ -262,3 +262,5 @@ def __dr(id: str) -> requirements.Req:
db022 = __dr("reward_rest")
db023 = __dr("param_proposal") # new/updated fields
db024 = __dr("epoch_param") # new/updated fields
db025_01 = __dr("int-epoch_state-01") # related to committee
db025_02 = __dr("int-epoch_state-02") # related to constitution
10 changes: 10 additions & 0 deletions cardano_node_tests/tests/tests_conway/test_committee.py
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,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.db025_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.db025_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 @@ -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
Expand Down Expand Up @@ -583,3 +584,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.db025_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.db025_02.success()
23 changes: 23 additions & 0 deletions cardano_node_tests/utils/dbsync_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,15 @@ class DrepDistributionDBRow:
drep_hash_view: str


@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 @@ -1541,3 +1550,17 @@ def query_drep_distr(
with execute(query=query, vars=(drep_hash, epoch_no)) as cur:
while (result := cur.fetchone()) is not None:
yield DrepDistributionDBRow(*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 @@ -1529,3 +1529,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}."
)
8 changes: 8 additions & 0 deletions src_docs/chang_user_stories_template.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1357,6 +1357,12 @@ DB Sync - Conway related tables
- |image-epoch_param|
- The accepted protocol parameters for an epoch.
`→ <https://github.com/IntersectMBO/cardano-db-sync/blob/master/doc/schema.md#epoch_param>`__
-

- |image-epoch_state|
- Table with governance stats per epoch.
`→ <https://github.com/IntersectMBO/cardano-db-sync/blob/master/doc/schema.md#epoch_state>`__


.. |Success Badge| image:: https://img.shields.io/badge/success-green
.. |Failure Badge| image:: https://img.shields.io/badge/failure-red
Expand Down Expand Up @@ -1767,3 +1773,5 @@ DB Sync - Conway related tables
:target: https://github.com/param_proposal-404
.. |image-epoch_param| image:: https://img.shields.io/badge/epoch_param-grey
:target: https://github.com/epoch_param-404
.. |image-epoch_state| image:: https://img.shields.io/badge/epoch_state-grey
:target: https://github.com/epoch_state-404
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"]
}
}
Loading