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 13, 2024
1 parent f3a9f59 commit 6725ee8
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 1 deletion.
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
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 @@ -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
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 @@ -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()
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 @@ -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,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)
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 @@ -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}."
)
7 changes: 7 additions & 0 deletions src_docs/chang_user_stories_template.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1347,6 +1347,11 @@ DB Sync - Conway related tables
- |image-off_chain_vote_fetch_error|
- Errors while fetching or validating offchain Voting Anchor metadata.
`<https://github.com/IntersectMBO/cardano-db-sync/blob/master/doc/schema.md#off_chain_vote_fetch_error>`__
-

- |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 @@ -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
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 6725ee8

Please sign in to comment.