Skip to content

Commit

Permalink
adjustment after removal of fragment builder in wallet, but rather ex…
Browse files Browse the repository at this point in the history
…posing it as independent struct
  • Loading branch information
dkijania committed Jan 26, 2022
1 parent 9d04343 commit c054d42
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 272 deletions.
273 changes: 140 additions & 133 deletions Cargo.lock

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions Cargo.toml
Expand Up @@ -26,10 +26,11 @@ chain-storage = { git = "https://github.com/input-output-hk/chain-libs.git", bra
chain-time = { git = "https://github.com/input-output-hk/chain-libs.git", branch = "master" }
chain-impl-mockchain = { git = "https://github.com/input-output-hk/chain-libs.git", branch = "master" }
chrono = "0.4"
jcli = { git = "https://github.com/input-output-hk/jormungandr.git", branch = "master" }
jormungandr-lib = { git = "https://github.com/input-output-hk/jormungandr.git", branch = "master" }
jormungandr-integration-tests = { git = "https://github.com/input-output-hk/jormungandr.git", branch = "master" }
jormungandr-automation = { git = "https://github.com/input-output-hk/jormungandr.git", branch = "master" }
jcli = { git = "https://github.com/input-output-hk/jormungandr.git", branch = "port_update_proposals_test"}
jormungandr-lib = { git = "https://github.com/input-output-hk/jormungandr.git", branch = "port_update_proposals_test" }
jormungandr-integration-tests = { git = "https://github.com/input-output-hk/jormungandr.git", branch = "port_update_proposals_test" }
jormungandr-automation = { git = "https://github.com/input-output-hk/jormungandr.git", branch = "port_update_proposals_test" }
thor = { git = "https://github.com/input-output-hk/jormungandr.git", branch = "port_update_proposals_test" }
jortestkit = { git = "https://github.com/input-output-hk/jortestkit.git", branch = "master" }
# TODO: Remove fixed and migrate to rust_decimal
fixed = { version = "1.7", features = ["serde"] }
Expand Down
1 change: 0 additions & 1 deletion src/bin/cli/rewards/voters.rs
Expand Up @@ -3,7 +3,6 @@ use catalyst_toolbox::rewards::voters::{
calculate_reward_share, calculate_stake, reward_from_share, vote_count_with_addresses,
AddressesVoteCount, Rewards, VoteCount, ADA_TO_LOVELACE_FACTOR,
};

use chain_addr::{Discrimination, Kind};
use chain_impl_mockchain::vote::CommitteeId;
use jcli_lib::jcli_lib::block::Common;
Expand Down
75 changes: 27 additions & 48 deletions src/recovery/tally.rs
Expand Up @@ -597,16 +597,17 @@ mod test {
use chain_impl_mockchain::chaintypes::ConsensusType;
use chain_impl_mockchain::vote::Choice;
use chain_impl_mockchain::vote::Tally;
use jormungandr_automation::jormungandr::ConfigurationBuilder;
use jormungandr_automation::testing::VotePlanBuilder;
use jormungandr_lib::interfaces::load_persistent_fragments_logs_from_folder_path;
use jormungandr_lib::interfaces::KesUpdateSpeed;
use jormungandr_lib::interfaces::PersistentFragmentLog;
use jormungandr_lib::time::SecondsSinceUnixEpoch;
use jormungandr_testing_utils::testing::fragments::write_into_persistent_log;
use jormungandr_testing_utils::testing::jormungandr::ConfigurationBuilder;
use jormungandr_testing_utils::testing::vote_plan_cert;
use jormungandr_testing_utils::testing::VotePlanBuilder;
use jormungandr_testing_utils::wallet::Wallet as TestWallet;
use rand::rngs::OsRng;
use thor::vote_plan_cert;
use thor::write_into_persistent_log;
use thor::FragmentBuilder;
use thor::Wallet as TestWallet;

#[test]
fn test_vote_flow() {
Expand All @@ -616,11 +617,10 @@ mod test {
let slots_per_epoch = 10;

let mut rng = OsRng;
let mut alice =
let alice =
TestWallet::new_account_with_discrimination(&mut rng, Discrimination::Production);
let mut bob =
TestWallet::new_account_with_discrimination(&mut rng, Discrimination::Production);
let mut clarice =
let bob = TestWallet::new_account_with_discrimination(&mut rng, Discrimination::Production);
let clarice =
TestWallet::new_account_with_discrimination(&mut rng, Discrimination::Production);

let vote_plan = VotePlanBuilder::new().proposals_count(3).public().build();
Expand All @@ -646,7 +646,7 @@ mod test {
.with_block0_consensus(ConsensusType::Bft)
.with_kes_update_speed(KesUpdateSpeed::new(43200).unwrap())
.with_discrimination(Discrimination::Production)
.with_committees(&[&alice])
.with_committees(&[alice.to_committee_id()])
.with_slot_duration(slot_duration)
.with_slots_per_epoch(slots_per_epoch)
.build_block0();
Expand All @@ -656,37 +656,18 @@ mod test {
slot_id: 0,
};
let block0 = block0_configuration.to_block();

let fragment_builder = FragmentBuilder::new(
&block0.id().into(),
&block0_configuration.blockchain_configuration.linear_fees,
valid_until,
);

//vote fragments
let mut fragments: Vec<PersistentFragmentLog> = vec![
alice
.issue_vote_cast_cert(
&block0.id().into(),
&block0_configuration.blockchain_configuration.linear_fees,
valid_until,
&vote_plan,
0,
&Choice::new(1),
)
.unwrap(),
bob.issue_vote_cast_cert(
&block0.id().into(),
&block0_configuration.blockchain_configuration.linear_fees,
valid_until,
&vote_plan,
1,
&Choice::new(1),
)
.unwrap(),
clarice
.issue_vote_cast_cert(
&block0.id().into(),
&block0_configuration.blockchain_configuration.linear_fees,
valid_until,
&vote_plan,
2,
&Choice::new(1),
)
.unwrap(),
fragment_builder.vote_cast(&alice, &vote_plan, 0, &Choice::new(1)),
fragment_builder.vote_cast(&bob, &vote_plan, 1, &Choice::new(1)),
fragment_builder.vote_cast(&clarice, &vote_plan, 2, &Choice::new(1)),
]
.into_iter()
.map(|fragment| PersistentFragmentLog {
Expand All @@ -700,6 +681,12 @@ mod test {
slot_id: 0,
};

let fragment_builder = FragmentBuilder::new(
&block0.id().into(),
&block0_configuration.blockchain_configuration.linear_fees,
valid_until,
);

//calculate tally period
let new_secs = block0_configuration
.blockchain_configuration
Expand All @@ -710,15 +697,7 @@ mod test {
//push tally fragment
fragments.push(PersistentFragmentLog {
time: SecondsSinceUnixEpoch::from_secs(new_secs),
fragment: alice
.issue_vote_tally_cert(
&block0.id().into(),
&block0_configuration.blockchain_configuration.linear_fees,
valid_until,
&vote_plan,
VoteTallyPayload::Public,
)
.unwrap(),
fragment: fragment_builder.vote_tally(&alice, &vote_plan, VoteTallyPayload::Public),
});

let persistent_fragment_log_output = temp_dir.child("fragments");
Expand Down
19 changes: 8 additions & 11 deletions src/rewards/voters.rs
Expand Up @@ -102,22 +102,20 @@ fn account_hex_to_address(
#[cfg(test)]
mod test {
use super::*;
use jormungandr_testing_utils::testing::jormungandr::ConfigurationBuilder;
use jormungandr_testing_utils::testing::startup;
use jormungandr_testing_utils::wallet::Wallet;
use jormungandr_automation::jormungandr::ConfigurationBuilder;
use quickcheck::TestResult;
use quickcheck_macros::quickcheck;
use thor::Wallet;

#[quickcheck]
pub fn test_calculate_stake(wallet_count: usize, committe_count: usize) -> TestResult {
if committe_count >= wallet_count {
return TestResult::discard();
}

let wallets: Vec<Wallet> =
std::iter::from_fn(|| Some(startup::create_new_account_address()))
.take(wallet_count)
.collect();
let wallets: Vec<Wallet> = std::iter::from_fn(|| Some(Default::default()))
.take(wallet_count)
.collect();
let wallet_initial_funds = 10;

let block0 = ConfigurationBuilder::new()
Expand Down Expand Up @@ -149,10 +147,9 @@ mod test {
pub fn test_calculate_rewards() {
let wallet_count = 10usize;

let wallets: Vec<Wallet> =
std::iter::from_fn(|| Some(startup::create_new_account_address()))
.take(wallet_count)
.collect();
let wallets: Vec<Wallet> = std::iter::from_fn(|| Some(Default::default()))
.take(wallet_count)
.collect();
let wallet_initial_funds = 1;

let addresses: AddressesVoteCount = wallets
Expand Down
13 changes: 9 additions & 4 deletions tests/tally/blockchain.rs
Expand Up @@ -12,12 +12,12 @@ use chain_impl_mockchain::{
vote::PayloadType,
};
use jormungandr_lib::interfaces::{
try_initials_vec_from_messages, Block0Configuration, BlockchainConfiguration, Initial,
try_initial_fragment_from_message, Block0Configuration, BlockchainConfiguration, Initial,
};

use jormungandr_testing_utils::wallet::Wallet;
use rand::{CryptoRng, Rng};
use std::collections::HashMap;
use thor::Wallet;

// rather arbitrary at this point
const DEFAULT_WALLETS: u32 = 10;
Expand Down Expand Up @@ -129,11 +129,16 @@ impl TestBlockchainBuilder {
})
.collect::<Vec<VotePlan>>();

initial.extend(try_initials_vec_from_messages(vote_plans_fragments.iter()).unwrap());
let discrimination = Discrimination::Production;
initial.extend(
vote_plans_fragments
.iter()
.map(|message| try_initial_fragment_from_message(discrimination, message).unwrap()),
);

let mut config = Block0Configuration {
blockchain_configuration: BlockchainConfiguration::new(
Discrimination::Production,
discrimination,
ConsensusVersion::Bft,
LinearFee::new(0, 0, 0), // it is much easier not to account for feers in the tests verification alg
),
Expand Down
53 changes: 21 additions & 32 deletions tests/tally/generator.rs
Expand Up @@ -17,10 +17,11 @@ use chain_impl_mockchain::{
vote::{Choice, PayloadType, VotePlanManager, VotePlanStatus},
};
use jormungandr_lib::{crypto::hash::Hash, interfaces::Block0Configuration};
use jormungandr_testing_utils::wallet::Wallet;
use rand::prelude::*;
use rand_chacha::ChaChaRng;
use std::collections::HashMap;
use thor::FragmentBuilder;
use thor::Wallet;

pub struct VoteRoundGenerator {
block0: Block0Configuration,
Expand Down Expand Up @@ -150,7 +151,7 @@ impl VoteRoundGenerator {
.expect("vote plan not found")
.plan()
.vote_start(),
address,
address.to_single_account().unwrap(),
vote_cast,
);
if let Ok(update_voteplan) = update_voteplan {
Expand Down Expand Up @@ -187,7 +188,7 @@ impl VoteRoundGenerator {
let mut manager = manager
.start_private_tally(
vote_end,
&stake_control,
tmp_ledger.accounts(),
self.block0.blockchain_configuration.committees[0].into(),
)
.unwrap();
Expand All @@ -213,7 +214,7 @@ impl VoteRoundGenerator {
let partial_res = encrypted_tally
.validate_partial_decryptions(&member_keys, &sh)
.unwrap()
.decrypt_tally((*total_stake).into(), &table)
.decrypt_tally((*total_stake).0, &table)
.unwrap();
results.push(partial_res.votes.into_boxed_slice());
shares.push(sh);
Expand Down Expand Up @@ -250,7 +251,7 @@ impl VoteRoundGenerator {
let manager = manager
.public_tally(
vote_end,
&stake_control.clone(),
&tmp_ledger.accounts().clone(),
&Default::default(),
self.block0.blockchain_configuration.committees[0].into(),
|_| (),
Expand Down Expand Up @@ -278,28 +279,20 @@ impl VoteRoundGenerator {
let committee_end = voteplan.committee_end();
let committee_member = self.committee_wallets.values_mut().next().unwrap();

let fragment_builder = FragmentBuilder::new(
&self.block0_hash,
&self.block0.blockchain_configuration.linear_fees,
committee_end,
);

if let VoteTallyPayload::Private { .. } = payload {
let encrypted_tally_fragment = committee_member
.issue_encrypted_tally_cert(
&self.block0_hash,
&self.block0.blockchain_configuration.linear_fees,
committee_end,
voteplan,
)
.unwrap();
let encrypted_tally_fragment =
fragment_builder.encrypted_tally(committee_member, voteplan);
committee_member.confirm_transaction();
res.push(encrypted_tally_fragment);
}

let tally_fragment = committee_member
.issue_vote_tally_cert(
&self.block0_hash,
&self.block0.blockchain_configuration.linear_fees,
committee_end,
voteplan,
payload,
)
.unwrap();
let tally_fragment = fragment_builder.vote_tally(committee_member, voteplan, payload);
committee_member.confirm_transaction();
res.push(tally_fragment);

Expand Down Expand Up @@ -349,16 +342,12 @@ impl GenerationStrategy for TestStrategy {
let proposal_index = rng.gen_range(0..voteplan.proposals().len());
let choice = Choice::new(rng.gen_bool(0.5) as u8 + 1); // app votes ares 1-based
let valid_until = voteplan.vote_start().next_epoch();
let fragment = wallet
.issue_vote_cast_cert(
block0_hash,
fees,
valid_until,
voteplan,
proposal_index as u8,
&choice,
)
.unwrap();
let fragment = FragmentBuilder::new(block0_hash, fees, valid_until).vote_cast(
wallet,
voteplan,
proposal_index as u8,
&choice,
);
wallet.confirm_transaction();

fragments.push(fragment);
Expand Down

0 comments on commit c054d42

Please sign in to comment.