|
| 1 | +use crate::Vote; |
| 2 | + |
| 3 | +use crate::common::iapyx_from_mainnet; |
| 4 | +use crate::common::MainnetWallet; |
| 5 | +use assert_fs::TempDir; |
| 6 | +use catalyst_toolbox::rewards::voters::calc_voter_rewards; |
| 7 | +use catalyst_toolbox::snapshot::RawSnapshot; |
| 8 | +use catalyst_toolbox::snapshot::Snapshot; |
| 9 | +use chain_impl_mockchain::block::BlockDate; |
| 10 | +use jormungandr_automation::testing::time; |
| 11 | +use jormungandr_lib::crypto::account::Identifier; |
| 12 | +use vit_servicing_station_tests::common::data::ArbitraryValidVotingTemplateGenerator; |
| 13 | +use vitup::config::VoteBlockchainTime; |
| 14 | +use vitup::config::{Block0Initials, ConfigBuilder}; |
| 15 | +use vitup::testing::spawn_network; |
| 16 | +use vitup::testing::vitup_setup; |
| 17 | +use snapshot_lib::VotingGroup; |
| 18 | + |
| 19 | +#[test] |
| 20 | +pub fn voters_with_at_least_one_vote() { |
| 21 | + let stake = 10_000; |
| 22 | + |
| 23 | + let alice_wallet = MainnetWallet::new(stake); |
| 24 | + let bob_wallet = MainnetWallet::new(stake); |
| 25 | + let clarice_wallet = MainnetWallet::new(stake); |
| 26 | + |
| 27 | + let raw_snapshot = vec![ |
| 28 | + alice_wallet.as_voting_registration(), |
| 29 | + bob_wallet.as_voting_registration(), |
| 30 | + clarice_wallet.as_voting_registration(), |
| 31 | + ]; |
| 32 | + |
| 33 | + let assigner = |_: &Identifier| VotingGroup::new(); |
| 34 | + |
| 35 | + let snapshot = Snapshot::from_raw_snapshot( |
| 36 | + RawSnapshot::from(raw_snapshot), |
| 37 | + 450.into(), |
| 38 | + 1.into(), |
| 39 | + &assigner, |
| 40 | + ) |
| 41 | + .unwrap(); |
| 42 | + let testing_directory = TempDir::new().unwrap().into_persistent(); |
| 43 | + |
| 44 | + let vote_timing = VoteBlockchainTime { |
| 45 | + vote_start: 0, |
| 46 | + tally_start: 1, |
| 47 | + tally_end: 2, |
| 48 | + slots_per_epoch: 30, |
| 49 | + }; |
| 50 | + let config = ConfigBuilder::default() |
| 51 | + .block0_initials(Block0Initials(vec![ |
| 52 | + alice_wallet.as_initial_entry(), |
| 53 | + bob_wallet.as_initial_entry(), |
| 54 | + clarice_wallet.as_initial_entry(), |
| 55 | + ])) |
| 56 | + .vote_timing(vote_timing.into()) |
| 57 | + .slot_duration_in_seconds(2) |
| 58 | + .proposals_count(3) |
| 59 | + .voting_power(100) |
| 60 | + .private(false) |
| 61 | + .build(); |
| 62 | + |
| 63 | + let mut template_generator = ArbitraryValidVotingTemplateGenerator::new(); |
| 64 | + let (mut controller, vit_parameters, network_params) = |
| 65 | + vitup_setup(&config, testing_directory.path().to_path_buf()).unwrap(); |
| 66 | + |
| 67 | + let (nodes, _vit_station, wallet_proxy) = spawn_network( |
| 68 | + &mut controller, |
| 69 | + vit_parameters, |
| 70 | + network_params, |
| 71 | + &mut template_generator, |
| 72 | + ) |
| 73 | + .unwrap(); |
| 74 | + |
| 75 | + let mut alice = iapyx_from_mainnet(&alice_wallet, &wallet_proxy).unwrap(); |
| 76 | + let mut bob = iapyx_from_mainnet(&bob_wallet, &wallet_proxy).unwrap(); |
| 77 | + |
| 78 | + let fund1_vote_plan = &controller.defined_vote_plans()[0]; |
| 79 | + |
| 80 | + alice |
| 81 | + .vote_for(fund1_vote_plan.id(), 0, Vote::Yes as u8) |
| 82 | + .unwrap(); |
| 83 | + |
| 84 | + bob.vote_for(fund1_vote_plan.id(), 1, Vote::Yes as u8) |
| 85 | + .unwrap(); |
| 86 | + |
| 87 | + bob.vote_for(fund1_vote_plan.id(), 0, Vote::Yes as u8) |
| 88 | + .unwrap(); |
| 89 | + |
| 90 | + let target_date = BlockDate { |
| 91 | + epoch: 1, |
| 92 | + slot_id: 0, |
| 93 | + }; |
| 94 | + time::wait_for_date(target_date.into(), nodes[0].rest()); |
| 95 | + |
| 96 | + let _block0 = &controller.settings().block0; |
| 97 | + let records = calc_voter_rewards( |
| 98 | + nodes[0].rest().account_votes_count().unwrap(), |
| 99 | + 1, |
| 100 | + snapshot.to_full_snapshot_info(), |
| 101 | + 100u32.into(), |
| 102 | + ) |
| 103 | + .unwrap(); |
| 104 | + |
| 105 | + assert_eq!( |
| 106 | + records |
| 107 | + .iter() |
| 108 | + .find(|(x, _y)| **x == alice_wallet.reward_address()) |
| 109 | + .unwrap() |
| 110 | + .1, |
| 111 | + &50u32.into() |
| 112 | + ); |
| 113 | + |
| 114 | + assert_eq!( |
| 115 | + records |
| 116 | + .iter() |
| 117 | + .find(|(x, _y)| **x == bob_wallet.reward_address()) |
| 118 | + .unwrap() |
| 119 | + .1, |
| 120 | + &50u32.into() |
| 121 | + ); |
| 122 | +} |
0 commit comments