Skip to content

Commit

Permalink
NPG 3275 explorer vote plan test by id (#4090)
Browse files Browse the repository at this point in the history
* Create vote_plan_by_id.graphql

* rename vote_plan_by_id file

* adding vote plan query infra

* adding vote plan test

* update vote plan by id test

* adding api call

* adding vote plan not existing test

* adding private vote test

* clean up private testing test

* add comment

* adding vote_plan verifier

* adding verifies to vote plan test

* fix clippy
  • Loading branch information
kukkok3 authored Sep 22, 2022
1 parent e037228 commit 983a37c
Show file tree
Hide file tree
Showing 7 changed files with 919 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
query VotePlanById($id: String!){
votePlan(id: $id) {
id
voteStart {...blockDate}
voteEnd {...blockDate}
committeeEnd {...blockDate}
payloadType
proposals {
proposalId
options {start end}
tally {
__typename
... on TallyPublicStatus {results
options{start end} }
... on TallyPrivateStatus {results
options{start end} }
}
votes{
edges{
node{
address{
id
#delegation{id}
}
payload{
__typename
... on VotePayloadPublicStatus {choice}
... on VotePayloadPrivateStatus {proof encryptedVote}
}
}
}
totalCount
}
}
}
}
fragment blockDate on BlockDate{
epoch{id}
slot
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ impl ExplorerParams {

#[allow(clippy::derivable_impls)]
impl Default for ExplorerParams {
//Passing None we use the default values of the explorer
//DEFAULT_QUERY_DEPTH_LIMIT= 15
//DEFAULT_QUERY_COMPLEXITY_LIMIT= 40
//address_bech32_prefix= addr
fn default() -> Self {
ExplorerParams {
query_complexity_limit: None,
Expand Down
10 changes: 9 additions & 1 deletion testing/jormungandr-automation/src/jormungandr/explorer/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub type NonZero = String;
pub type TimeOffsetSeconds = String;
pub type PublicKey = String;
pub type ExternalProposalId = String;

pub type Weight = String;
use graphql_client::GraphQLQuery;

#[derive(GraphQLQuery)]
Expand Down Expand Up @@ -115,3 +115,11 @@ pub struct TransactionByIdCertificates;
response_derives = "Debug"
)]
pub struct AllVotePlans;

#[derive(GraphQLQuery)]
#[graphql(
query_path = "resources/explorer/graphql/voteplan_by_id.graphql",
schema_path = "resources/explorer/graphql/schema.graphql",
response_derives = "Debug,Clone"
)]
pub struct VotePlanById;
20 changes: 16 additions & 4 deletions testing/jormungandr-automation/src/jormungandr/explorer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use self::{
data::{
address, all_blocks, all_stake_pools, all_vote_plans, block, blocks_by_chain_length, epoch,
last_block, settings, stake_pool, transaction_by_id, transaction_by_id_certificates,
transactions_by_address, Address, AllBlocks, AllStakePools, AllVotePlans, Block,
BlocksByChainLength, Epoch, LastBlock, Settings, StakePool, TransactionById,
TransactionByIdCertificates, TransactionsByAddress,
transactions_by_address, vote_plan_by_id, Address, AllBlocks, AllStakePools, AllVotePlans,
Block, BlocksByChainLength, Epoch, LastBlock, Settings, StakePool, TransactionById,
TransactionByIdCertificates, TransactionsByAddress, VotePlanById,
},
};
use crate::testing::configuration::get_explorer_app;
Expand All @@ -19,7 +19,7 @@ use std::{
};
mod client;
pub mod configuration;
mod data;
pub mod data;
pub mod verifier;
mod wrappers;

Expand Down Expand Up @@ -307,6 +307,18 @@ impl Explorer {
Ok(response_body)
}

pub fn vote_plan(
&self,
id: String,
) -> Result<Response<vote_plan_by_id::ResponseData>, ExplorerError> {
let query = VotePlanById::build_query(vote_plan_by_id::Variables { id });
self.print_request(&query);
let response = self.client.run(query).map_err(ExplorerError::ClientError)?;
let response_body: Response<vote_plan_by_id::ResponseData> = response.json()?;
self.print_log(&response_body);
Ok(response_body)
}

pub fn transaction(
&self,
hash: Hash,
Expand Down
166 changes: 163 additions & 3 deletions testing/jormungandr-automation/src/jormungandr/explorer/verifier.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use super::data::{
address::AddressAddress, settings::SettingsSettingsFees, transaction_by_id_certificates::*,
transactions_by_address::TransactionsByAddressTipTransactionsByAddress,
vote_plan_by_id::VotePlanByIdVotePlan,
};
use crate::jormungandr::explorer::{
data::{transaction_by_id_certificates::PayloadType as expPayloadType, vote_plan_by_id},
vote_plan_by_id::VotePlanByIdVotePlanProposalsTally::*,
};
use crate::jormungandr::explorer::data::transaction_by_id_certificates::PayloadType as expPayloadType;
use bech32::FromBase32;
use chain_addr::AddressReadable;
use chain_crypto::{Ed25519, PublicKey};
Expand All @@ -13,12 +17,17 @@ use chain_impl_mockchain::{
config::{ConfigParam::*, RewardParams},
fee::LinearFee,
fragment::Fragment,
testing::data::Wallet,
transaction::{AccountIdentifier, InputEnum, Transaction},
vote::PayloadType,
vote,
vote::{Choice, PayloadType},
};
use jormungandr_lib::interfaces::{
Address, FragmentStatus, PrivateTallyState, Tally, VotePlanStatus,
};
use jormungandr_lib::interfaces::{Address, FragmentStatus};
use std::{collections::HashMap, num::NonZeroU64};
use thiserror::Error;
use vote_plan_by_id::VotePlanByIdVotePlanProposalsVotesEdgesNodePayload::*;

#[derive(Debug, Error)]
pub enum VerifierError {
Expand Down Expand Up @@ -958,6 +967,157 @@ impl ExplorerVerifier {
}
}

pub fn assert_vote_plan_by_id(
explorer_vote_plan: VotePlanByIdVotePlan,
vote_plan_status: VotePlanStatus,
proposal_votes: HashMap<String, Vec<(Wallet, Choice)>>,
) {
assert_eq!(explorer_vote_plan.id, vote_plan_status.id.to_string());
assert_eq!(
explorer_vote_plan.vote_start.epoch.id,
vote_plan_status.vote_start.epoch().to_string()
);
assert_eq!(
explorer_vote_plan.vote_start.slot,
vote_plan_status.vote_start.slot().to_string()
);
assert_eq!(
explorer_vote_plan.vote_end.epoch.id,
vote_plan_status.vote_end.epoch().to_string()
);
assert_eq!(
explorer_vote_plan.vote_end.slot,
vote_plan_status.vote_end.slot().to_string()
);
assert_eq!(
explorer_vote_plan.committee_end.epoch.id,
vote_plan_status.committee_end.epoch().to_string()
);
assert_eq!(
explorer_vote_plan.committee_end.slot,
vote_plan_status.committee_end.slot().to_string()
);
match explorer_vote_plan.payload_type {
vote_plan_by_id::PayloadType::PUBLIC => assert!(matches!(
vote_plan_status.payload,
vote::PayloadType::Public
)),
vote_plan_by_id::PayloadType::PRIVATE => assert!(matches!(
vote_plan_status.payload,
vote::PayloadType::Private
)),
vote_plan_by_id::PayloadType::Other(_) => panic!("Wrong payload type"),
}

assert_eq!(
explorer_vote_plan.proposals.len(),
vote_plan_status.proposals.len()
);
for explorer_proposal in explorer_vote_plan.proposals {
let vote_proposal_status =
match vote_plan_status.proposals.iter().position(|proposal| {
explorer_proposal.proposal_id == proposal.proposal_id.to_string()
}) {
Some(index) => vote_plan_status.proposals[index].clone(),
None => panic!("Proposal id not found"),
};
assert_eq!(
vote_proposal_status.options.start,
explorer_proposal.options.start as u8
);
assert_eq!(
vote_proposal_status.options.end,
explorer_proposal.options.end as u8
);
match &vote_proposal_status.tally {
Tally::Public { result } => {
if let TallyPublicStatus(explorer_tally_status) =
explorer_proposal.tally.unwrap()
{
assert_eq!(result.results.len(), explorer_tally_status.results.len());
let matching_results = result
.results
.iter()
.zip(explorer_tally_status.results.iter())
.filter(|&(a, b)| &a.to_string() == b)
.count();
assert_eq!(matching_results, result.results.len());
assert_eq!(result.options.len(), explorer_tally_status.results.len());
assert_eq!(
result.options.start,
explorer_tally_status.options.start as u8
);
assert_eq!(result.options.end, explorer_tally_status.options.end as u8);
} else {
panic!("Wrong tally status. Expected Public")
}
}
Tally::Private { state } => {
assert!(explorer_proposal.tally.is_some());
if let TallyPrivateStatus(explorer_tally_status) =
explorer_proposal.tally.unwrap()
{
match state {
PrivateTallyState::Encrypted { encrypted_tally: _ } => assert!(
explorer_tally_status.results.is_none(),
"BUG NPG-3369 fixed"
),
PrivateTallyState::Decrypted { result } => {
let explorer_tally_result = explorer_tally_status.results.unwrap();
assert_eq!(result.results.len(), explorer_tally_result.len());
let matching_results = result
.results
.iter()
.zip(explorer_tally_result.iter())
.filter(|&(a, b)| &a.to_string() == b)
.count();
assert_eq!(matching_results, result.results.len());
assert_eq!(result.options.len(), explorer_tally_result.len());
assert_eq!(
result.options.start,
explorer_tally_status.options.start as u8
);
assert_eq!(
result.options.end,
explorer_tally_status.options.end as u8
);
}
}
} else {
panic!("Wrong tally status. Expected Private")
}
}
}
assert_eq!(
vote_proposal_status.votes_cast,
explorer_proposal.votes.total_count as usize
);
if vote_proposal_status.votes_cast == 0 {
assert!(explorer_proposal.votes.edges.unwrap().is_empty());
} else {
let explorer_votes = explorer_proposal.votes.edges.unwrap();
assert_eq!(explorer_votes.len(), vote_proposal_status.votes_cast);
let votes = proposal_votes
.get(&vote_proposal_status.proposal_id.to_string())
.unwrap();
for vote in votes {
for explorer_vote in &explorer_votes {
if vote.0.public_key().to_string()
== explorer_vote.as_ref().unwrap().node.address.id
{
match &explorer_vote.as_ref().unwrap().node.payload {
VotePayloadPublicStatus(choice) => {
assert_eq!(choice.choice as u8, vote.1.as_byte())
}
VotePayloadPrivateStatus(_) => todo!(),
}
}
}
}
}
}
}

fn decode_bech32_pk(bech32_public_key: &str) -> PublicKey<Ed25519> {
let (_, data, _variant) = bech32::decode(bech32_public_key).unwrap();
let dat = Vec::from_base32(&data).unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pub mod certificates;
pub mod explorer_sanity;
pub mod settings;
pub mod transaction;
pub mod vote_plan;
Loading

0 comments on commit 983a37c

Please sign in to comment.