/
votes_history.rs
136 lines (120 loc) · 4.37 KB
/
votes_history.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use crate::common::{iapyx_from_secret_key, vitup_setup};
use assert_fs::TempDir;
use chain_impl_mockchain::vote::Choice;
use jormungandr_lib::crypto::hash::Hash;
use jormungandr_lib::interfaces::AccountVotes;
use jormungandr_lib::interfaces::FragmentStatus;
use jormungandr_testing_utils::testing::node::time;
use std::collections::HashMap;
use std::str::FromStr;
use valgrind::{Proposal, Protocol};
use vit_servicing_station_tests::common::data::ArbitraryValidVotingTemplateGenerator;
use vitup::builders::VitBackendSettingsBuilder;
use vitup::config::VoteBlockchainTime;
use vitup::config::{InitialEntry, Initials};
use vitup::scenario::network::setup_network;
const PIN: &str = "1234";
const ALICE: &str = "alice";
#[test]
pub fn votes_history_reflects_casted_votes() {
let testing_directory = TempDir::new().unwrap().into_persistent();
let endpoint = "127.0.0.1:8080";
let version = "2.0";
let batch_size = 1;
let vote_timing = VoteBlockchainTime {
vote_start: 0,
tally_start: 1,
tally_end: 2,
slots_per_epoch: 30,
};
let mut quick_setup = VitBackendSettingsBuilder::new();
quick_setup
.initials(Initials(vec![InitialEntry::Wallet {
name: ALICE.to_string(),
funds: 10_000,
pin: PIN.to_string(),
}]))
.vote_timing(vote_timing.into())
.slot_duration_in_seconds(2)
.proposals_count(300)
.voting_power(31_000)
.private(true);
let mut template_generator = ArbitraryValidVotingTemplateGenerator::new();
let (mut vit_controller, mut controller, vit_parameters, _fund_name) =
vitup_setup(quick_setup, testing_directory.path().to_path_buf());
let (nodes, vit_station, wallet_proxy) = setup_network(
&mut controller,
&mut vit_controller,
vit_parameters,
&mut template_generator,
endpoint.to_string(),
&Protocol::Http,
version.to_owned(),
)
.unwrap();
let secret = testing_directory.path().join("vit_backend/wallet_alice");
let mut alice = iapyx_from_secret_key(secret, &wallet_proxy).unwrap();
let proposals = alice.proposals().unwrap();
let votes_data: Vec<(&Proposal, Choice)> = proposals
.iter()
.take(batch_size)
.map(|proposal| (proposal, Choice::new(0)))
.collect();
let fragment_ids = alice
.votes_batch(votes_data.clone())
.unwrap()
.iter()
.map(|item| item.to_string())
.collect();
time::wait_for_epoch(1, nodes[0].rest());
let fragment_statuses = nodes[0].rest().fragments_statuses(fragment_ids).unwrap();
assert!(fragment_statuses
.iter()
.all(|(_, status)| matches!(status, FragmentStatus::InABlock { .. })));
let mut account_votes: Vec<AccountVotes> = {
let mut votes_registry: HashMap<Hash, Vec<i64>> = nodes[0]
.rest()
.vote_plan_statuses()
.unwrap()
.iter()
.map(|vote_plan| (vote_plan.id, Vec::new()))
.collect();
for (proposal, _choice) in votes_data {
let hash = Hash::from_str(&proposal.chain_voteplan_id).unwrap();
if let Some(registry) = votes_registry.get_mut(&hash) {
registry.push(proposal.chain_proposal_index)
}
}
votes_registry
.into_iter()
.map(|(vote_plan_id, votes)| AccountVotes {
vote_plan_id,
votes: votes.into_iter().map(|x| x as u8).collect(),
})
.collect()
};
let mut votes_history = alice
.votes_history()
.unwrap()
.expect("vote history is empty");
votes_history.sort_by(|x, y| x.vote_plan_id.cmp(&y.vote_plan_id));
account_votes.sort_by(|x, y| x.vote_plan_id.cmp(&y.vote_plan_id));
assert_eq!(votes_history, account_votes);
for account_vote in account_votes {
let actual_ids = alice
.vote_plan_history(account_vote.vote_plan_id)
.unwrap_or_else(|_| {
panic!(
"vote history is empty for vote_plan: {} ",
account_vote.vote_plan_id
)
});
assert_eq!(actual_ids, Some(account_vote.votes.clone()));
}
vit_station.shutdown();
wallet_proxy.shutdown();
for mut node in nodes {
node.shutdown().unwrap();
}
controller.finalize();
}