Skip to content

Commit

Permalink
Test voting_procedure dbsync table
Browse files Browse the repository at this point in the history
  • Loading branch information
saratomaz committed May 9, 2024
1 parent fd03bfa commit 8f67b9d
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 12 deletions.
11 changes: 2 additions & 9 deletions cardano_node_tests/tests/tests_conway/conway_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@
LOGGER = logging.getLogger(__name__)


@dataclasses.dataclass(frozen=True, order=True)
class VotedVotes:
cc: tp.List[clusterlib.VoteCC] # pylint: disable=invalid-name
drep: tp.List[clusterlib.VoteDrep]
spo: tp.List[clusterlib.VoteSPO]


@dataclasses.dataclass(frozen=True)
class PParamPropRec:
proposals: tp.List[clusterlib_utils.UpdateProposal]
Expand Down Expand Up @@ -205,7 +198,7 @@ def cast_vote(
cc_skip_votes: bool = False,
drep_skip_votes: bool = False,
spo_skip_votes: bool = False,
) -> VotedVotes:
) -> governance_utils.VotedVotes:
"""Cast a vote."""
# pylint: disable=too-many-arguments
votes_cc = []
Expand Down Expand Up @@ -294,7 +287,7 @@ def cast_vote(
assert not votes_drep or prop_vote["dRepVotes"], "No DRep votes"
assert not votes_spo or prop_vote["stakePoolVotes"], "No stake pool votes"

return VotedVotes(cc=votes_cc, drep=votes_drep, spo=votes_spo)
return governance_utils.VotedVotes(cc=votes_cc, drep=votes_drep, spo=votes_spo)


def resign_ccs(
Expand Down
11 changes: 10 additions & 1 deletion cardano_node_tests/tests/tests_conway/test_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from cardano_node_tests.tests.tests_conway import conway_common
from cardano_node_tests.utils import clusterlib_utils
from cardano_node_tests.utils import configuration
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 @@ -175,7 +176,7 @@ def test_info(
)

reqc.cli024.start(url=helpers.get_vcs_link())
conway_common.submit_vote(
vote_tx_output = conway_common.submit_vote(
cluster_obj=cluster,
name_template=temp_template,
payment_addr=pool_user_ug.payment,
Expand All @@ -185,6 +186,8 @@ def test_info(
)
reqc.cli024.success()

vote_txid = cluster.g_transaction.get_txid(tx_body_file=vote_tx_output.out_file)

vote_gov_state = cluster.g_conway_governance.query.gov_state()
_cur_epoch = cluster.g_query.get_epoch()
conway_common.save_gov_state(
Expand Down Expand Up @@ -227,3 +230,9 @@ def test_info(
governance_utils.check_vote_view(cluster_obj=cluster, vote_data=votes_drep[0])
governance_utils.check_vote_view(cluster_obj=cluster, vote_data=votes_spo[0])
reqc.cli022.success()

# Check dbsync
dbsync_utils.check_votes(
votes=governance_utils.VotedVotes(cc=votes_cc, drep=votes_drep, spo=votes_spo),
txhash=vote_txid,
)
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def test_treasury_withdrawals( # noqa: C901

def _cast_vote(
approve: bool, vote_id: str, add_spo_votes: bool = False
) -> conway_common.VotedVotes:
) -> governance_utils.VotedVotes:
votes_cc = []
votes_drep = []
votes_spo = []
Expand Down Expand Up @@ -243,7 +243,7 @@ def _cast_vote(
assert prop_vote["dRepVotes"], "No DRep votes"
assert not votes_spo or prop_vote["stakePoolVotes"], "Unexpected stake pool votes"

return conway_common.VotedVotes(cc=votes_cc, drep=votes_drep, spo=votes_spo)
return governance_utils.VotedVotes(cc=votes_cc, drep=votes_drep, spo=votes_spo)

# Check that SPOs cannot vote on treasury withdrawal action
with pytest.raises(clusterlib.CLIError) as excinfo:
Expand Down
26 changes: 26 additions & 0 deletions cardano_node_tests/utils/dbsync_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,17 @@ class GovActionProposalDBRow:
expired_epoch: int


@dataclasses.dataclass(frozen=True)
class VotingProcedureDBRow:
# pylint: disable-next=invalid-name
id: int
voter_role: str
committee_voter: int
drep_voter: int
pool_voter: int
vote: str


@contextlib.contextmanager
def execute(query: str, vars: tp.Sequence = ()) -> tp.Iterator[psycopg2.extensions.cursor]:
# pylint: disable=redefined-builtin
Expand Down Expand Up @@ -1125,3 +1136,18 @@ def query_gov_action_proposal(
with execute(query=query, vars=(query_var,)) as cur:
while (result := cur.fetchone()) is not None:
yield GovActionProposalDBRow(*result)


def query_voting_procedure(txhash: str) -> tp.Generator[VotingProcedureDBRow, None, None]:
"""Query voting_procedure table in db-sync."""
query = (
"SELECT"
" vp.id, vp.voter_role, vp.committee_voter, vp.drep_voter, vp.pool_voter, vp.vote "
"FROM voting_procedure as vp "
"INNER JOIN tx ON tx.id = vp.tx_id "
"WHERE tx.hash = %s;"
)

with execute(query=query, vars=(rf"\x{txhash}",)) as cur:
while (result := cur.fetchone()) is not None:
yield VotingProcedureDBRow(*result)
22 changes: 22 additions & 0 deletions cardano_node_tests/utils/dbsync_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -985,3 +985,25 @@ def check_drep_deregistration(
), f"Wrong deposit value for deregistered DRep {drep.drep_id} in db-sync"

return drep_data


def check_votes(votes: governance_utils.VotedVotes, txhash: str) -> None:
"""Check votes in db-sync."""
expected_votes_by_role = {
"ConstitutionalCommittee": [v.vote.name for v in votes.cc],
"DRep": [v.vote.name for v in votes.drep],
"SPO": [v.vote.name for v in votes.spo],
}

dbsync_votes = list(dbsync_queries.query_voting_procedure(txhash=txhash))

dbsync_votes_by_role: dict = {
"ConstitutionalCommittee": [],
"DRep": [],
"SPO": [],
}

for d_vote in dbsync_votes:
dbsync_votes_by_role[d_vote.voter_role].append(d_vote.vote.upper())

assert expected_votes_by_role == dbsync_votes_by_role, "Votes didn't match in dbsync"
7 changes: 7 additions & 0 deletions cardano_node_tests/utils/governance_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ class StakeDelegation:
total_lovelace: int


@dataclasses.dataclass(frozen=True, order=True)
class VotedVotes:
cc: tp.List[clusterlib.VoteCC] # pylint: disable=invalid-name
drep: tp.List[clusterlib.VoteDrep]
spo: tp.List[clusterlib.VoteSPO]


class PrevGovActionIds(enum.Enum):
COMMITTEE = "Committee"
CONSTITUTION = "Constitution"
Expand Down

0 comments on commit 8f67b9d

Please sign in to comment.