Skip to content

Commit b8198da

Browse files
chain-impl-mockchain: use more idiomatic builder-style APIs
1 parent 5a9165c commit b8198da

File tree

7 files changed

+92
-99
lines changed

7 files changed

+92
-99
lines changed

chain-impl-mockchain/benches/tally.rs

+7-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use chain_crypto::testing::TestCryptoRng;
2-
use chain_impl_mockchain::testing::scenario::template::WalletTemplateBuilder;
32
use chain_impl_mockchain::{
43
certificate::{
54
DecryptedPrivateTally, DecryptedPrivateTallyProposal, EncryptedVoteTally, VotePlan,
@@ -39,25 +38,19 @@ fn tally_benchmark(voters_count: usize, voting_power_per_voter: u64, c: &mut Cri
3938
const THRESHOLD: usize = 2;
4039
let favorable = Choice::new(1);
4140

42-
let mut wallets: Vec<&mut WalletTemplateBuilder> = Vec::new();
43-
44-
let mut alice_wallet_builder = wallet(ALICE);
45-
alice_wallet_builder
41+
let alice_wallet_builder = wallet(ALICE)
4642
.with(1_000)
4743
.owns(STAKE_POOL)
4844
.committee_member();
49-
wallets.push(&mut alice_wallet_builder);
5045

5146
let voters_aliases = voters_aliases(voters_count);
52-
let mut wallet_builders: Vec<WalletTemplateBuilder> =
53-
voters_aliases.iter().map(|alias| wallet(alias)).collect();
54-
55-
for wallet_builder in wallet_builders.iter_mut() {
56-
wallet_builder.with(voting_power_per_voter);
57-
wallets.push(wallet_builder);
58-
}
47+
let wallets: Vec<_> = voters_aliases
48+
.iter()
49+
.map(|alias| wallet(alias).with(voting_power_per_voter))
50+
.chain(std::iter::once(alice_wallet_builder))
51+
.collect();
5952

60-
let mut rng = TestCryptoRng::from_seed([0u8; 16]);
53+
let mut rng = TestCryptoRng::from_seed([0u8; 32]);
6154
let members = CommitteeMembersManager::new(&mut rng, THRESHOLD, MEMBERS_NO);
6255

6356
let committee_keys = members

chain-impl-mockchain/src/testing/builders/block_builder.rs

+13-15
Original file line numberDiff line numberDiff line change
@@ -32,41 +32,39 @@ impl GenesisPraosBlockBuilder {
3232
}
3333
}
3434

35-
pub fn with_parent(&mut self, parent: &Header) -> &mut Self {
36-
self.with_parent_id(parent.hash());
37-
self.with_date(parent.block_date());
38-
self.with_chain_length(parent.chain_length());
39-
self
35+
pub fn with_parent(self, parent: &Header) -> Self {
36+
self.with_parent_id(parent.hash())
37+
.with_date(parent.block_date())
38+
.with_chain_length(parent.chain_length())
4039
}
4140

42-
pub fn with_parent_id(&mut self, parent_id: Hash) -> &mut Self {
41+
pub fn with_parent_id(mut self, parent_id: Hash) -> Self {
4342
self.parent_id = Some(parent_id);
4443
self
4544
}
4645

47-
pub fn with_date(&mut self, date: BlockDate) -> &mut Self {
46+
pub fn with_date(mut self, date: BlockDate) -> Self {
4847
self.date = Some(date);
4948
self
5049
}
5150

52-
pub fn with_chain_length(&mut self, chain_length: ChainLength) -> &mut Self {
51+
pub fn with_chain_length(mut self, chain_length: ChainLength) -> Self {
5352
self.chain_length = Some(chain_length);
5453
self
5554
}
5655

57-
pub fn with_fragment(&mut self, fragment: Fragment) -> &mut Self {
56+
pub fn with_fragment(mut self, fragment: Fragment) -> Self {
5857
self.contents_builder.push(fragment);
5958
self
6059
}
6160

62-
pub fn with_fragments(&mut self, fragments: Vec<Fragment>) -> &mut Self {
63-
for fragment in fragments {
64-
self.with_fragment(fragment);
65-
}
66-
self
61+
pub fn with_fragments(self, fragments: Vec<Fragment>) -> Self {
62+
fragments
63+
.into_iter()
64+
.fold(self, |builder, fragment| builder.with_fragment(fragment))
6765
}
6866

69-
pub fn build(&self, stake_pool: &StakePool, time_era: &TimeEra) -> Block {
67+
pub fn build(self, stake_pool: &StakePool, time_era: &TimeEra) -> Block {
7068
if self.date.is_none() || self.chain_length.is_none() || self.parent_id.is_none() {
7169
panic!("date,chain_length or hash is not set");
7270
}

chain-impl-mockchain/src/testing/builders/stake_pool_builder.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -45,37 +45,37 @@ impl StakePoolBuilder {
4545
}
4646
}
4747

48-
pub fn with_owners(&mut self, owners: Vec<PublicKey<Ed25519>>) -> &mut Self {
48+
pub fn with_owners(mut self, owners: Vec<PublicKey<Ed25519>>) -> Self {
4949
self.owners.extend(owners);
5050
self
5151
}
5252

53-
pub fn with_alias(&mut self, alias: &str) -> &mut Self {
53+
pub fn with_alias(mut self, alias: &str) -> Self {
5454
self.alias = alias.to_owned();
5555
self
5656
}
5757

58-
pub fn with_operators(&mut self, operators: Vec<PublicKey<Ed25519>>) -> &mut Self {
58+
pub fn with_operators(mut self, operators: Vec<PublicKey<Ed25519>>) -> Self {
5959
self.operators.extend(operators);
6060
self
6161
}
6262

63-
pub fn with_pool_permissions(&mut self, permissions: PoolPermissions) -> &mut Self {
63+
pub fn with_pool_permissions(mut self, permissions: PoolPermissions) -> Self {
6464
self.pool_permissions = Some(permissions);
6565
self
6666
}
6767

68-
pub fn with_reward_account(&mut self, reward_account: bool) -> &mut Self {
68+
pub fn with_reward_account(mut self, reward_account: bool) -> Self {
6969
self.reward_account = reward_account;
7070
self
7171
}
7272

7373
pub fn with_ratio_tax_type(
74-
&mut self,
74+
self,
7575
numerator: u64,
7676
denominator: u64,
7777
max_limit: Option<u64>,
78-
) -> &mut Self {
78+
) -> Self {
7979
self.with_tax_type(TaxType {
8080
fixed: Value(0),
8181
ratio: Ratio {
@@ -86,12 +86,12 @@ impl StakePoolBuilder {
8686
})
8787
}
8888

89-
pub fn with_tax_type(&mut self, tax_type: TaxType) -> &mut Self {
89+
pub fn with_tax_type(mut self, tax_type: TaxType) -> Self {
9090
self.tax_type = tax_type;
9191
self
9292
}
9393

94-
pub fn build(&self) -> StakePool {
94+
pub fn build(self) -> StakePool {
9595
let mut rng = rand_core::OsRng;
9696

9797
let pool_vrf: KeyPair<Curve25519_2HashDH> = KeyPair::generate(&mut rng);

chain-impl-mockchain/src/testing/builders/update_builder.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,19 @@ impl ProposalBuilder {
2424
config_params: ConfigParams::new(),
2525
}
2626
}
27-
pub fn with_proposal_changes(&mut self, changes: Vec<ConfigParam>) -> &mut Self {
28-
for change in changes {
29-
self.with_proposal_change(change);
30-
}
31-
self
27+
28+
pub fn with_proposal_changes(self, changes: Vec<ConfigParam>) -> Self {
29+
changes
30+
.into_iter()
31+
.fold(self, |builder, change| builder.with_proposal_change(change))
3232
}
3333

34-
pub fn with_proposal_change(&mut self, change: ConfigParam) -> &mut Self {
34+
pub fn with_proposal_change(mut self, change: ConfigParam) -> Self {
3535
self.config_params.push(change);
3636
self
3737
}
3838

39-
pub fn build(&self) -> UpdateProposal {
39+
pub fn build(self) -> UpdateProposal {
4040
let mut update_proposal = UpdateProposal::new();
4141
for config_param in self.config_params.iter().cloned() {
4242
update_proposal.changes.push(config_param);
@@ -59,17 +59,17 @@ impl SignedProposalBuilder {
5959
}
6060
}
6161

62-
pub fn with_proposer_id(&mut self, proposer_id: BftLeaderId) -> &mut Self {
62+
pub fn with_proposer_id(mut self, proposer_id: BftLeaderId) -> Self {
6363
self.proposer_id = Some(proposer_id);
6464
self
6565
}
6666

67-
pub fn with_proposal_update(&mut self, update_proposal: UpdateProposal) -> &mut Self {
67+
pub fn with_proposal_update(mut self, update_proposal: UpdateProposal) -> Self {
6868
self.update_proposal = Some(update_proposal);
6969
self
7070
}
7171

72-
pub fn build(&self) -> SignedUpdateProposal {
72+
pub fn build(self) -> SignedUpdateProposal {
7373
SignedUpdateProposal {
7474
proposal: UpdateProposalWithProposer {
7575
proposal: self.update_proposal.clone().unwrap(),
@@ -93,17 +93,17 @@ impl UpdateVoteBuilder {
9393
}
9494
}
9595

96-
pub fn with_proposal_id(&mut self, proposal_id: UpdateProposalId) -> &mut Self {
96+
pub fn with_proposal_id(mut self, proposal_id: UpdateProposalId) -> Self {
9797
self.proposal_id = Some(proposal_id);
9898
self
9999
}
100100

101-
pub fn with_voter_id(&mut self, voter_id: BftLeaderId) -> &mut Self {
101+
pub fn with_voter_id(mut self, voter_id: BftLeaderId) -> Self {
102102
self.voter_id = Some(voter_id);
103103
self
104104
}
105105

106-
pub fn build(&self) -> SignedUpdateVote {
106+
pub fn build(self) -> SignedUpdateVote {
107107
let update_vote = UpdateVote {
108108
proposal_id: self.proposal_id.unwrap(),
109109
voter_id: self.voter_id.clone().unwrap(),

chain-impl-mockchain/src/testing/e2e/rewards/tax.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pub fn no_tax() {
9090

9191
fn verify_distribute_rewards(
9292
total_reward: u64,
93-
stake_pool_builder: &mut StakePoolDefBuilder,
93+
stake_pool_builder: StakePoolDefBuilder,
9494
expected_stake_pool_reward: u64,
9595
) {
9696
let (mut ledger, controller) = prepare_scenario()

chain-impl-mockchain/src/testing/scenario/scenario_builder.rs

+24-21
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub struct ScenarioBuilder {
4242
config: ConfigBuilder,
4343
initials: Option<Vec<WalletTemplateBuilder>>,
4444
stake_pools: Option<Vec<StakePoolDefBuilder>>,
45-
vote_plans: Vec<VotePlanDefBuilder>,
45+
vote_plans: Option<Vec<VotePlanDefBuilder>>,
4646
}
4747

4848
pub fn prepare_scenario() -> ScenarioBuilder {
@@ -54,32 +54,32 @@ pub fn prepare_scenario() -> ScenarioBuilder {
5454
config: default_config_builder,
5555
initials: None,
5656
stake_pools: None,
57-
vote_plans: Vec::new(),
57+
vote_plans: None,
5858
}
5959
}
6060

6161
impl ScenarioBuilder {
62-
pub fn with_config(&mut self, config: ConfigBuilder) -> &mut Self {
62+
pub fn with_config(mut self, config: ConfigBuilder) -> Self {
6363
self.config = config;
6464
self
6565
}
6666

67-
pub fn with_initials(&mut self, initials: Vec<&mut WalletTemplateBuilder>) -> &mut Self {
68-
self.initials = Some(initials.iter().map(|x| (**x).clone()).collect());
67+
pub fn with_initials(mut self, initials: Vec<WalletTemplateBuilder>) -> Self {
68+
self.initials = Some(initials);
6969
self
7070
}
7171

72-
pub fn with_vote_plans(&mut self, vote_plans: Vec<&mut VotePlanDefBuilder>) -> &mut Self {
73-
self.vote_plans = vote_plans.iter().map(|x| (**x).clone()).collect();
72+
pub fn with_vote_plans(mut self, vote_plans: Vec<VotePlanDefBuilder>) -> Self {
73+
self.vote_plans = Some(vote_plans);
7474
self
7575
}
7676

77-
pub fn with_stake_pools(&mut self, stake_pools: Vec<&mut StakePoolDefBuilder>) -> &mut Self {
78-
self.stake_pools = Some(stake_pools.iter().map(|x| (**x).clone()).collect());
77+
pub fn with_stake_pools(mut self, stake_pools: Vec<StakePoolDefBuilder>) -> Self {
78+
self.stake_pools = Some(stake_pools);
7979
self
8080
}
8181

82-
pub fn build(&self) -> Result<(TestLedger, Controller), ScenarioBuilderError> {
82+
pub fn build(self) -> Result<(TestLedger, Controller), ScenarioBuilderError> {
8383
if self.initials.is_none() {
8484
return Err(ScenarioBuilderError::UndefinedInitials);
8585
}
@@ -107,14 +107,17 @@ impl ScenarioBuilder {
107107
let faucets: Vec<AddressDataValue> =
108108
wallets.iter().cloned().map(|x| x.as_account()).collect();
109109

110-
let vote_plan_defs: Vec<VotePlanDef> =
111-
self.vote_plans.iter().map(|x| x.clone().build()).collect();
112-
let vote_plan_fragments: Vec<Fragment> = self
110+
let vote_plan_defs: Vec<VotePlanDef> = self
113111
.vote_plans
112+
.iter()
113+
.map(|builders| builders.into_iter())
114+
.flatten()
115+
.map(|x| x.clone().build())
116+
.collect();
117+
let vote_plan_fragments: Vec<Fragment> = vote_plan_defs
114118
.iter()
115119
.cloned()
116-
.map(|x| {
117-
let vote_plan_def = x.build();
120+
.map(|vote_plan_def| {
118121
let owner = wallets
119122
.iter()
120123
.cloned()
@@ -211,9 +214,9 @@ impl ScenarioBuilder {
211214
}
212215

213216
fn build_stake_pool(&self, template: StakePoolTemplate) -> StakePool {
214-
let mut builder = StakePoolBuilder::new();
215-
builder.with_owners(template.owners());
216-
builder.with_alias(&template.alias());
217+
let mut builder = StakePoolBuilder::new()
218+
.with_owners(template.owners())
219+
.with_alias(&template.alias());
217220

218221
if let Some(stake_pools) = &self.stake_pools {
219222
let stake_pool_def_opt = stake_pools
@@ -224,12 +227,12 @@ impl ScenarioBuilder {
224227

225228
if let Some(stake_pool_def) = stake_pool_def_opt {
226229
if let Some(pool_permission) = stake_pool_def.pool_permission() {
227-
builder.with_pool_permissions(pool_permission);
230+
builder = builder.with_pool_permissions(pool_permission);
228231
}
229232
if let Some(tax_type) = stake_pool_def.tax_type {
230-
builder.with_tax_type(tax_type);
233+
builder = builder.with_tax_type(tax_type);
231234
}
232-
builder.with_reward_account(stake_pool_def.has_reward_account);
235+
builder = builder.with_reward_account(stake_pool_def.has_reward_account);
233236
}
234237
}
235238
builder.build()

0 commit comments

Comments
 (0)