Skip to content

Commit

Permalink
set voting challenges in voting generator
Browse files Browse the repository at this point in the history
  • Loading branch information
dkijania committed Jan 22, 2021
1 parent 3e14604 commit e6f09cc
Showing 1 changed file with 33 additions and 14 deletions.
47 changes: 33 additions & 14 deletions vit-servicing-station-tests/src/common/data/generator/voting.rs
Expand Up @@ -2,9 +2,10 @@ use super::{ArbitraryGenerator, Snapshot};
use chain_impl_mockchain::certificate::VotePlan;
use chain_impl_mockchain::testing::scenario::template::VotePlanDef;
use vit_servicing_station_lib::db::models::{
challenges::Challenge, funds::Fund, proposals::Proposal, vote_options::VoteOptions,
funds::Fund, proposals::Proposal, vote_options::VoteOptions,
voteplans::Voteplan,
};
use rand::{rngs::OsRng, RngCore};

use fake::{
faker::company::en::{Buzzword, CatchPhase},
Expand All @@ -20,6 +21,8 @@ pub struct ValidVotePlanParameters {
pub voting_tally_end: Option<i64>,
pub next_fund_start_time: Option<i64>,
pub vote_encryption_key: Option<String>,
pub vote_options: VoteOptions,
pub challenges_count: usize
}

impl ValidVotePlanParameters {
Expand All @@ -32,6 +35,8 @@ impl ValidVotePlanParameters {
voting_tally_end: None,
next_fund_start_time: None,
vote_encryption_key: None,
vote_options: VoteOptions::parse_coma_separated_value("blank,yes,no"),
challenges_count: 4
}
}

Expand All @@ -58,6 +63,14 @@ impl ValidVotePlanParameters {
pub fn set_next_fund_start_time(&mut self, next_fund_start_time: i64) {
self.next_fund_start_time = Some(next_fund_start_time);
}

pub fn set_challenges_count(&mut self, challenges_count: usize) {
self.challenges_count = challenges_count;
}

pub fn set_vote_options(&mut self, vote_options: VoteOptions) {
self.vote_options = vote_options
}
}

pub struct ValidVotePlanGenerator {
Expand All @@ -74,6 +87,7 @@ impl ValidVotePlanGenerator {
}

pub fn build(self) -> Snapshot {

let mut generator = ArbitraryGenerator::new();
let vote_plan = Self::convert_to_vote_plan(&self.parameters.vote_plan);
let chain_vote_plan_id = vote_plan.to_id().to_string();
Expand Down Expand Up @@ -103,15 +117,9 @@ impl ValidVotePlanGenerator {
fund_id,
};

let challenge = Challenge {
id: generator.id(),
title: "up for a challenge?".to_string(),
description: "hey hey hey it's awesome".to_string(),
rewards_total: 100500,
fund_id,
};
let challenges = std::iter::from_fn(|| Some(generator.challenge_with_fund_id(fund_id))).take(self.parameters.challenges_count).collect();

let fund = Fund {
let mut fund = Fund {
id: fund_id,
fund_name: self.parameters.vote_plan.alias(),
fund_goal: "How will we encourage developers and entrepreneurs to build Dapps and businesses on top of Cardano in the next 6 months?".to_string(),
Expand All @@ -122,12 +130,21 @@ impl ValidVotePlanGenerator {
fund_end_time: voting_tally_end,
next_fund_start_time,
chain_vote_plans: vec![vote_plan.clone()],
challenges: vec![challenge.clone()],
challenges,
};

let mut proposals = vec![];
let mut rng = OsRng;

for (index, proposal) in self.parameters.vote_plan.proposals().iter().enumerate() {

let challenge_idx = rng.next_u32() as usize % self.parameters.challenges_count;
let mut challenge = fund.challenges.get_mut(challenge_idx).unwrap();

let proposal_funds = generator.proposal_fund();

challenge.rewards_total += proposal_funds;

let proposal_url = generator.gen_http_address();
let proposal = Proposal {
internal_id: index as i32,
Expand All @@ -138,31 +155,33 @@ impl ValidVotePlanGenerator {
proposal_problem: Buzzword().fake::<String>(),
proposal_solution: CatchPhase().fake::<String>(),
proposal_public_key: generator.hash(),
proposal_funds: generator.proposal_fund(),
proposal_funds,
proposal_url: proposal_url.to_string(),
proposal_impact_score: generator.impact_score(),
proposal_files_url: format!("{}/files", proposal_url),
proposer: generator.proposer(),
chain_proposal_id: proposal.id().to_string().as_bytes().to_vec(),
chain_proposal_index: index as i64,
chain_vote_options: VoteOptions::parse_coma_separated_value("blank,yes,no"),
chain_vote_options: self.parameters.vote_options.clone(),
chain_voteplan_id: chain_vote_plan_id.clone(),
chain_vote_start_time: vote_plan.chain_vote_start_time,
chain_vote_end_time: vote_plan.chain_vote_end_time,
chain_committee_end_time: vote_plan.chain_committee_end_time,
chain_voteplan_payload: vote_plan.chain_voteplan_payload.clone(),
chain_vote_encryption_key: vote_plan.chain_vote_encryption_key.clone(),
fund_id: fund.id,
challenge_id: fund.challenges.first().unwrap().id,
challenge_id: challenge.id,
};

proposals.push(proposal);
}

let challenges = fund.challenges.clone();

Snapshot::new(
vec![fund],
proposals,
vec![challenge],
challenges,
generator.tokens(),
vec![vote_plan],
)
Expand Down

0 comments on commit e6f09cc

Please sign in to comment.