diff --git a/chain-impl-mockchain/benches/tally.rs b/chain-impl-mockchain/benches/tally.rs index a042b21b6..325e8a3ef 100644 --- a/chain-impl-mockchain/benches/tally.rs +++ b/chain-impl-mockchain/benches/tally.rs @@ -1,5 +1,4 @@ use chain_crypto::testing::TestCryptoRng; -use chain_impl_mockchain::testing::scenario::template::WalletTemplateBuilder; use chain_impl_mockchain::{ certificate::{ DecryptedPrivateTally, DecryptedPrivateTallyProposal, EncryptedVoteTally, VoteCast, @@ -41,18 +40,15 @@ fn tally_benchmark( ) { let mut rng = TestCryptoRng::seed_from_u64(0); - // All wallets that are needed to be initialized in the genesis block - // TODO the underlying ledger constructor is not using this &mut. This should be a plain - // Vec, which will greatly simplify this code. - let mut wallets: Vec<&mut WalletTemplateBuilder> = Vec::new(); + let mut wallets = Vec::new(); // Stake pool owner - let mut alice_wallet_builder = wallet(ALICE); - alice_wallet_builder + let alice_wallet_builder = wallet(ALICE) .with(1_000) .owns(STAKE_POOL) .committee_member(); - wallets.push(&mut alice_wallet_builder); + + wallets.push(alice_wallet_builder); // generate the required number of wallets from the distribution let voters_aliases: Vec<_> = (1..=voters_count) @@ -63,17 +59,15 @@ fn tally_benchmark( .take(voters_count) .collect(); let total_votes = voting_powers.iter().sum(); - let mut voters_wallets: Vec<_> = voters_aliases - .iter() - .zip(voting_powers.iter()) - .map(|(alias, voting_power)| { - let mut wallet_builder = WalletTemplateBuilder::new(alias); - wallet_builder.with(*voting_power); - wallet_builder - }) - .collect(); + { + let mut voters_wallets: Vec<_> = voters_aliases + .iter() + .zip(voting_powers.iter()) + .map(|(alias, voting_power)| wallet(alias).with(*voting_power)) + .collect(); - wallets.append(&mut voters_wallets.iter_mut().collect()); + wallets.append(&mut voters_wallets); + } // Prepare committee members keys let members = CommitteeMembersManager::new(&mut rng, CRS_SEED, THRESHOLD, MEMBERS_NO); @@ -84,16 +78,16 @@ fn tally_benchmark( .collect(); // Build the vote plan - let mut vote_plan_builder = vote_plan(VOTE_PLAN); - vote_plan_builder + let mut vote_plan_builder = vote_plan(VOTE_PLAN) .owner(ALICE) .consecutive_epoch_dates() .payload_type(PayloadType::Private) .committee_keys(committee_keys); for _ in 0..n_proposals { - let mut proposal_builder = proposal(VoteTestGen::external_proposal_id()); - proposal_builder.options(3).action_off_chain(); - vote_plan_builder.with_proposal(&mut proposal_builder); + let proposal_builder = proposal(VoteTestGen::external_proposal_id()) + .options(3) + .action_off_chain(); + vote_plan_builder = vote_plan_builder.with_proposal(proposal_builder); } // Initialize ledger @@ -104,7 +98,7 @@ fn tally_benchmark( .with_rewards(Value(1000)), ) .with_initials(wallets) - .with_vote_plans(vec![&mut vote_plan_builder]) + .with_vote_plans(vec![vote_plan_builder]) .build() .unwrap(); diff --git a/chain-impl-mockchain/src/testing/builders/block_builder.rs b/chain-impl-mockchain/src/testing/builders/block_builder.rs index 87d810db2..18e1931ee 100644 --- a/chain-impl-mockchain/src/testing/builders/block_builder.rs +++ b/chain-impl-mockchain/src/testing/builders/block_builder.rs @@ -32,41 +32,39 @@ impl GenesisPraosBlockBuilder { } } - pub fn with_parent(&mut self, parent: &Header) -> &mut Self { - self.with_parent_id(parent.hash()); - self.with_date(parent.block_date()); - self.with_chain_length(parent.chain_length()); - self + pub fn with_parent(self, parent: &Header) -> Self { + self.with_parent_id(parent.hash()) + .with_date(parent.block_date()) + .with_chain_length(parent.chain_length()) } - pub fn with_parent_id(&mut self, parent_id: Hash) -> &mut Self { + pub fn with_parent_id(mut self, parent_id: Hash) -> Self { self.parent_id = Some(parent_id); self } - pub fn with_date(&mut self, date: BlockDate) -> &mut Self { + pub fn with_date(mut self, date: BlockDate) -> Self { self.date = Some(date); self } - pub fn with_chain_length(&mut self, chain_length: ChainLength) -> &mut Self { + pub fn with_chain_length(mut self, chain_length: ChainLength) -> Self { self.chain_length = Some(chain_length); self } - pub fn with_fragment(&mut self, fragment: Fragment) -> &mut Self { + pub fn with_fragment(mut self, fragment: Fragment) -> Self { self.contents_builder.push(fragment); self } - pub fn with_fragments(&mut self, fragments: Vec) -> &mut Self { - for fragment in fragments { - self.with_fragment(fragment); - } - self + pub fn with_fragments(self, fragments: Vec) -> Self { + fragments + .into_iter() + .fold(self, |builder, fragment| builder.with_fragment(fragment)) } - pub fn build(&self, stake_pool: &StakePool, time_era: &TimeEra) -> Block { + pub fn build(self, stake_pool: &StakePool, time_era: &TimeEra) -> Block { if self.date.is_none() || self.chain_length.is_none() || self.parent_id.is_none() { panic!("date,chain_length or hash is not set"); } diff --git a/chain-impl-mockchain/src/testing/builders/stake_pool_builder.rs b/chain-impl-mockchain/src/testing/builders/stake_pool_builder.rs index 1e92512e9..bc9776a08 100644 --- a/chain-impl-mockchain/src/testing/builders/stake_pool_builder.rs +++ b/chain-impl-mockchain/src/testing/builders/stake_pool_builder.rs @@ -45,37 +45,37 @@ impl StakePoolBuilder { } } - pub fn with_owners(&mut self, owners: Vec>) -> &mut Self { + pub fn with_owners(mut self, owners: Vec>) -> Self { self.owners.extend(owners); self } - pub fn with_alias(&mut self, alias: &str) -> &mut Self { + pub fn with_alias(mut self, alias: &str) -> Self { self.alias = alias.to_owned(); self } - pub fn with_operators(&mut self, operators: Vec>) -> &mut Self { + pub fn with_operators(mut self, operators: Vec>) -> Self { self.operators.extend(operators); self } - pub fn with_pool_permissions(&mut self, permissions: PoolPermissions) -> &mut Self { + pub fn with_pool_permissions(mut self, permissions: PoolPermissions) -> Self { self.pool_permissions = Some(permissions); self } - pub fn with_reward_account(&mut self, reward_account: bool) -> &mut Self { + pub fn with_reward_account(mut self, reward_account: bool) -> Self { self.reward_account = reward_account; self } pub fn with_ratio_tax_type( - &mut self, + self, numerator: u64, denominator: u64, max_limit: Option, - ) -> &mut Self { + ) -> Self { self.with_tax_type(TaxType { fixed: Value(0), ratio: Ratio { @@ -86,12 +86,12 @@ impl StakePoolBuilder { }) } - pub fn with_tax_type(&mut self, tax_type: TaxType) -> &mut Self { + pub fn with_tax_type(mut self, tax_type: TaxType) -> Self { self.tax_type = tax_type; self } - pub fn build(&self) -> StakePool { + pub fn build(self) -> StakePool { let mut rng = rand_core::OsRng; let pool_vrf: KeyPair = KeyPair::generate(&mut rng); diff --git a/chain-impl-mockchain/src/testing/builders/update_builder.rs b/chain-impl-mockchain/src/testing/builders/update_builder.rs index 12bb0c788..002979a9a 100644 --- a/chain-impl-mockchain/src/testing/builders/update_builder.rs +++ b/chain-impl-mockchain/src/testing/builders/update_builder.rs @@ -24,19 +24,19 @@ impl ProposalBuilder { config_params: ConfigParams::new(), } } - pub fn with_proposal_changes(&mut self, changes: Vec) -> &mut Self { - for change in changes { - self.with_proposal_change(change); - } - self + + pub fn with_proposal_changes(self, changes: Vec) -> Self { + changes + .into_iter() + .fold(self, |builder, change| builder.with_proposal_change(change)) } - pub fn with_proposal_change(&mut self, change: ConfigParam) -> &mut Self { + pub fn with_proposal_change(mut self, change: ConfigParam) -> Self { self.config_params.push(change); self } - pub fn build(&self) -> UpdateProposal { + pub fn build(self) -> UpdateProposal { let mut update_proposal = UpdateProposal::new(); for config_param in self.config_params.iter().cloned() { update_proposal.changes.push(config_param); @@ -59,17 +59,17 @@ impl SignedProposalBuilder { } } - pub fn with_proposer_id(&mut self, proposer_id: BftLeaderId) -> &mut Self { + pub fn with_proposer_id(mut self, proposer_id: BftLeaderId) -> Self { self.proposer_id = Some(proposer_id); self } - pub fn with_proposal_update(&mut self, update_proposal: UpdateProposal) -> &mut Self { + pub fn with_proposal_update(mut self, update_proposal: UpdateProposal) -> Self { self.update_proposal = Some(update_proposal); self } - pub fn build(&self) -> SignedUpdateProposal { + pub fn build(self) -> SignedUpdateProposal { SignedUpdateProposal { proposal: UpdateProposalWithProposer { proposal: self.update_proposal.clone().unwrap(), @@ -93,17 +93,17 @@ impl UpdateVoteBuilder { } } - pub fn with_proposal_id(&mut self, proposal_id: UpdateProposalId) -> &mut Self { + pub fn with_proposal_id(mut self, proposal_id: UpdateProposalId) -> Self { self.proposal_id = Some(proposal_id); self } - pub fn with_voter_id(&mut self, voter_id: BftLeaderId) -> &mut Self { + pub fn with_voter_id(mut self, voter_id: BftLeaderId) -> Self { self.voter_id = Some(voter_id); self } - pub fn build(&self) -> SignedUpdateVote { + pub fn build(self) -> SignedUpdateVote { let update_vote = UpdateVote { proposal_id: self.proposal_id.unwrap(), voter_id: self.voter_id.clone().unwrap(), diff --git a/chain-impl-mockchain/src/testing/e2e/rewards/tax.rs b/chain-impl-mockchain/src/testing/e2e/rewards/tax.rs index 82392527a..3e844e901 100644 --- a/chain-impl-mockchain/src/testing/e2e/rewards/tax.rs +++ b/chain-impl-mockchain/src/testing/e2e/rewards/tax.rs @@ -90,7 +90,7 @@ pub fn no_tax() { fn verify_distribute_rewards( total_reward: u64, - stake_pool_builder: &mut StakePoolDefBuilder, + stake_pool_builder: StakePoolDefBuilder, expected_stake_pool_reward: u64, ) { let (mut ledger, controller) = prepare_scenario() diff --git a/chain-impl-mockchain/src/testing/scenario/scenario_builder.rs b/chain-impl-mockchain/src/testing/scenario/scenario_builder.rs index c32d75e2e..70b2add42 100644 --- a/chain-impl-mockchain/src/testing/scenario/scenario_builder.rs +++ b/chain-impl-mockchain/src/testing/scenario/scenario_builder.rs @@ -42,7 +42,7 @@ pub struct ScenarioBuilder { config: ConfigBuilder, initials: Option>, stake_pools: Option>, - vote_plans: Vec, + vote_plans: Option>, } pub fn prepare_scenario() -> ScenarioBuilder { @@ -54,32 +54,32 @@ pub fn prepare_scenario() -> ScenarioBuilder { config: default_config_builder, initials: None, stake_pools: None, - vote_plans: Vec::new(), + vote_plans: None, } } impl ScenarioBuilder { - pub fn with_config(&mut self, config: ConfigBuilder) -> &mut Self { + pub fn with_config(mut self, config: ConfigBuilder) -> Self { self.config = config; self } - pub fn with_initials(&mut self, initials: Vec<&mut WalletTemplateBuilder>) -> &mut Self { - self.initials = Some(initials.iter().map(|x| (**x).clone()).collect()); + pub fn with_initials(mut self, initials: Vec) -> Self { + self.initials = Some(initials); self } - pub fn with_vote_plans(&mut self, vote_plans: Vec<&mut VotePlanDefBuilder>) -> &mut Self { - self.vote_plans = vote_plans.iter().map(|x| (**x).clone()).collect(); + pub fn with_vote_plans(mut self, vote_plans: Vec) -> Self { + self.vote_plans = Some(vote_plans); self } - pub fn with_stake_pools(&mut self, stake_pools: Vec<&mut StakePoolDefBuilder>) -> &mut Self { - self.stake_pools = Some(stake_pools.iter().map(|x| (**x).clone()).collect()); + pub fn with_stake_pools(mut self, stake_pools: Vec) -> Self { + self.stake_pools = Some(stake_pools); self } - pub fn build(&self) -> Result<(TestLedger, Controller), ScenarioBuilderError> { + pub fn build(self) -> Result<(TestLedger, Controller), ScenarioBuilderError> { if self.initials.is_none() { return Err(ScenarioBuilderError::UndefinedInitials); } @@ -107,14 +107,17 @@ impl ScenarioBuilder { let faucets: Vec = wallets.iter().cloned().map(|x| x.as_account()).collect(); - let vote_plan_defs: Vec = - self.vote_plans.iter().map(|x| x.clone().build()).collect(); - let vote_plan_fragments: Vec = self + let vote_plan_defs: Vec = self .vote_plans + .iter() + .map(|builders| builders.into_iter()) + .flatten() + .map(|x| x.clone().build()) + .collect(); + let vote_plan_fragments: Vec = vote_plan_defs .iter() .cloned() - .map(|x| { - let vote_plan_def = x.build(); + .map(|vote_plan_def| { let owner = wallets .iter() .cloned() @@ -211,9 +214,9 @@ impl ScenarioBuilder { } fn build_stake_pool(&self, template: StakePoolTemplate) -> StakePool { - let mut builder = StakePoolBuilder::new(); - builder.with_owners(template.owners()); - builder.with_alias(&template.alias()); + let mut builder = StakePoolBuilder::new() + .with_owners(template.owners()) + .with_alias(&template.alias()); if let Some(stake_pools) = &self.stake_pools { let stake_pool_def_opt = stake_pools @@ -224,12 +227,12 @@ impl ScenarioBuilder { if let Some(stake_pool_def) = stake_pool_def_opt { if let Some(pool_permission) = stake_pool_def.pool_permission() { - builder.with_pool_permissions(pool_permission); + builder = builder.with_pool_permissions(pool_permission); } if let Some(tax_type) = stake_pool_def.tax_type { - builder.with_tax_type(tax_type); + builder = builder.with_tax_type(tax_type); } - builder.with_reward_account(stake_pool_def.has_reward_account); + builder = builder.with_reward_account(stake_pool_def.has_reward_account); } } builder.build() diff --git a/chain-impl-mockchain/src/testing/scenario/template/builders.rs b/chain-impl-mockchain/src/testing/scenario/template/builders.rs index 965c723a4..88ed12bb4 100644 --- a/chain-impl-mockchain/src/testing/scenario/template/builders.rs +++ b/chain-impl-mockchain/src/testing/scenario/template/builders.rs @@ -38,32 +38,31 @@ impl WalletTemplateBuilder { } } - pub fn with(&mut self, value: u64) -> &mut Self { + pub fn with(mut self, value: u64) -> Self { self.initial_value = Some(Value(value)); self } - pub fn owns(&mut self, ownership_alias: &str) -> &mut Self { + pub fn owns(mut self, ownership_alias: &str) -> Self { self.ownership_alias = Some(ownership_alias.to_owned()); self } - pub fn delegates_to(&mut self, delegates_to_alias: &str) -> &mut Self { + pub fn delegates_to(mut self, delegates_to_alias: &str) -> Self { self.delagate_alias = Some(delegates_to_alias.to_owned()); self } - pub fn committee_member(&mut self) -> &mut Self { + pub fn committee_member(mut self) -> Self { self.committee_member = true; self } - pub fn owns_and_delegates_to(&mut self, ownership_alias: &str) -> &mut Self { - self.owns(ownership_alias).delegates_to(ownership_alias); - self + pub fn owns_and_delegates_to(self, ownership_alias: &str) -> Self { + self.owns(ownership_alias).delegates_to(ownership_alias) } - pub fn build(&self) -> Result { + pub fn build(self) -> Result { let value = self .initial_value .ok_or(ScenarioBuilderError::UndefinedValueForWallet { @@ -179,17 +178,17 @@ impl StakePoolDefBuilder { } } - pub fn with_permissions_threshold(&mut self, threshold: u8) -> &mut Self { + pub fn with_permissions_threshold(mut self, threshold: u8) -> Self { self.permissions_threshold = threshold; self } - pub fn with_reward_account(&mut self, reward_account: bool) -> &mut Self { + pub fn with_reward_account(mut self, reward_account: bool) -> Self { self.reward_account = reward_account; self } - pub fn tax_ratio(&mut self, numerator: u64, denominator: u64) -> &mut Self { + pub fn tax_ratio(mut self, numerator: u64, denominator: u64) -> Self { self.tax_type = Some(TaxType { fixed: Value(0), ratio: Ratio { @@ -201,7 +200,7 @@ impl StakePoolDefBuilder { self } - pub fn tax_limit(&mut self, limit: u64) -> &mut Self { + pub fn tax_limit(mut self, limit: u64) -> Self { match self.tax_type.as_mut() { Some(tax_type) => tax_type.max_limit = Some(NonZeroU64::new(limit).unwrap()), None => unreachable!("setting tax limit for none TaxType"), @@ -209,7 +208,7 @@ impl StakePoolDefBuilder { self } - pub fn fixed_tax(&mut self, value: u64) -> &mut Self { + pub fn fixed_tax(mut self, value: u64) -> Self { self.tax_type = Some(TaxType { fixed: Value(value), ratio: Ratio::zero(), @@ -218,12 +217,12 @@ impl StakePoolDefBuilder { self } - pub fn no_tax(&mut self) -> &mut Self { + pub fn no_tax(mut self) -> Self { self.tax_type = Some(TaxType::zero()); self } - pub fn build(&self) -> StakePoolDef { + pub fn build(self) -> StakePoolDef { StakePoolDef { alias: self.alias.clone(), permissions_threshold: Some(self.permissions_threshold), @@ -259,22 +258,22 @@ impl VotePlanDefBuilder { } } - pub fn owner(&mut self, owner_alias: &str) -> &mut Self { + pub fn owner(mut self, owner_alias: &str) -> Self { self.owner_alias = Some(owner_alias.to_string()); self } - pub fn payload_type(&mut self, payload_type: PayloadType) -> &mut Self { + pub fn payload_type(mut self, payload_type: PayloadType) -> Self { self.payload_type = payload_type; self } - pub fn committee_keys(&mut self, committee_keys: Vec) -> &mut Self { + pub fn committee_keys(mut self, committee_keys: Vec) -> Self { self.committee_keys = committee_keys; self } - pub fn vote_phases(&mut self, start_epoch: u32, tally_epoch: u32, end_epoch: u32) -> &mut Self { + pub fn vote_phases(mut self, start_epoch: u32, tally_epoch: u32, end_epoch: u32) -> Self { self.vote_date = Some(BlockDate { epoch: start_epoch, slot_id: 0, @@ -290,7 +289,7 @@ impl VotePlanDefBuilder { self } - pub fn consecutive_epoch_dates(&mut self) -> &mut Self { + pub fn consecutive_epoch_dates(mut self) -> Self { self.vote_date = Some(BlockDate { epoch: 0, slot_id: 0, @@ -306,7 +305,7 @@ impl VotePlanDefBuilder { self } - pub fn with_proposal(&mut self, proposal_builder: &mut ProposalDefBuilder) -> &mut Self { + pub fn with_proposal(mut self, proposal_builder: ProposalDefBuilder) -> Self { self.proposals.push(proposal_builder.clone().build()); self } @@ -341,17 +340,17 @@ impl ProposalDefBuilder { } } - pub fn options(&mut self, options: u8) -> &mut Self { + pub fn options(mut self, options: u8) -> Self { self.options = options; self } - pub fn action_off_chain(&mut self) -> &mut Self { + pub fn action_off_chain(mut self) -> Self { self.action_type = VoteAction::OffChain; self } - pub fn action_rewards_add(&mut self, value: u64) -> &mut Self { + pub fn action_rewards_add(mut self, value: u64) -> Self { self.action_type = VoteAction::Treasury { action: TreasuryGovernanceAction::TransferToRewards { value: Value(value), @@ -360,7 +359,7 @@ impl ProposalDefBuilder { self } - pub fn action_transfer_to_rewards(&mut self, value: u64) -> &mut Self { + pub fn action_transfer_to_rewards(mut self, value: u64) -> Self { self.action_type = VoteAction::Parameters { action: ParametersGovernanceAction::RewardAdd { value: Value(value), @@ -369,7 +368,7 @@ impl ProposalDefBuilder { self } - pub fn action_parameters_no_op(&mut self) -> &mut Self { + pub fn action_parameters_no_op(mut self) -> Self { self.action_type = VoteAction::Parameters { action: ParametersGovernanceAction::NoOp, };