Skip to content

Commit

Permalink
Merge pull request #2609 from saratomaz/check_drep_distr
Browse files Browse the repository at this point in the history
Check drep_distr in dbsync
  • Loading branch information
mkoura committed Sep 13, 2024
2 parents f3a9f59 + df7554a commit 5584d1f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
32 changes: 29 additions & 3 deletions cardano_node_tests/tests/tests_conway/test_drep.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
from cardano_node_tests.utils import blockers
from cardano_node_tests.utils import cluster_nodes
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_utils
from cardano_node_tests.utils import helpers
Expand Down Expand Up @@ -1152,13 +1154,15 @@ def _deregister():
# This takes one epoch, so test this only for selected combinations of build command
# and submit method, only when we are running on local testnet, and only if we are not
# running smoke tests.
if (
check_delegation = (
use_build_cmd
and submit_method == submit_utils.SubmitMethods.CLI
and cluster_nodes.get_cluster_type().type == cluster_nodes.ClusterType.LOCAL
and "smoke" not in request.config.getoption("-m")
):
cluster.wait_for_epoch(epoch_no=init_epoch + 1, padding_seconds=5)
)

if check_delegation:
deleg_epoch = cluster.wait_for_epoch(epoch_no=init_epoch + 1, padding_seconds=5)
deleg_state = clusterlib_utils.get_delegation_state(cluster_obj=cluster)
stake_addr_hash = cluster.g_stake_address.get_stake_vkey_hash(
stake_vkey_file=pool_user_rewards.stake.vkey_file
Expand Down Expand Up @@ -1210,6 +1214,28 @@ def _deregister():
)
reqc.db003.success()

# Check DRep distribution in dbsync.
# A new entry is created for each DRep for each epoch in drep_distr table.
if check_delegation and configuration.HAS_DBSYNC:
reqc.db014.start(url=helpers.get_vcs_link())
cluster.wait_for_new_epoch()
if drep == "custom":
drep_id_bech32 = helpers.encode_bech32(prefix="drep", data=drep_id)
else:
drep_id_bech32 = f"drep_{drep_id}"
db_drep_distr = list(
dbsync_queries.query_drep_distr(drep_hash=drep_id_bech32, epoch_no=deleg_epoch)
)

assert (
db_drep_distr
), f"No Drep distribution found for Drep {drep_id_bech32} and epoch {deleg_epoch}"
assert (
db_drep_distr[0].amount >= deleg_amount
), f"Unexpected delegated amount in dbsync: {db_drep_distr[0].amount} < {deleg_amount}"

reqc.db014.success()

@allure.link(helpers.get_vcs_link())
@submit_utils.PARAM_SUBMIT_METHOD
@common.PARAM_USE_BUILD_CMD
Expand Down
27 changes: 27 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 DrepDistributionDBRow:
id: int
hash_id: int
amount: int
epoch_no: int
drep_hash_view: str


@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,21 @@ 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_drep_distr(
drep_hash: str, epoch_no: int
) -> tp.Generator[DrepDistributionDBRow, None, None]:
"""Query drep voting power in db-sync."""
query = (
"SELECT"
" drep_distr.id, drep_distr.hash_id, drep_distr.amount, drep_distr.epoch_no,"
" drep_hash.view "
"FROM drep_distr "
"INNER JOIN drep_hash ON drep_hash.id = drep_distr.hash_id "
"WHERE drep_hash.view = %s AND drep_distr.epoch_no = %s "
)

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

0 comments on commit 5584d1f

Please sign in to comment.