Skip to content

Commit f635809

Browse files
authored
update add_account function interface (#794)
1 parent da2bbc4 commit f635809

File tree

5 files changed

+33
-28
lines changed

5 files changed

+33
-28
lines changed

chain-impl-mockchain/src/accounting/account/mod.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,12 @@ impl<ID: Clone + Eq + Hash, Extra: Clone> Ledger<ID, Extra> {
7575
/// If the identifier is already present, error out.
7676
pub fn add_account(
7777
&self,
78-
identifier: &ID,
78+
identifier: ID,
7979
initial_value: Value,
8080
extra: Extra,
8181
) -> Result<Self, LedgerError> {
8282
self.0
83-
.insert(identifier.clone(), AccountState::new(initial_value, extra))
83+
.insert(identifier, AccountState::new(initial_value, extra))
8484
.map(Ledger)
8585
.map_err(|e| e.into())
8686
}
@@ -296,7 +296,7 @@ mod tests {
296296
// Add all arbitrary accounts
297297
for account_id in arbitrary_accounts_ids.iter() {
298298
ledger = ledger
299-
.add_account(account_id, AverageValue::arbitrary(gen).into(), ())
299+
.add_account(account_id.clone(), AverageValue::arbitrary(gen).into(), ())
300300
.unwrap();
301301

302302
for token in &arbitrary_voting_tokens {
@@ -340,7 +340,7 @@ mod tests {
340340
let initial_total_value = ledger.get_total_value().unwrap();
341341

342342
// add new account
343-
ledger = match ledger.add_account(&account_id, value, ()) {
343+
ledger = match ledger.add_account(account_id.clone(), value, ()) {
344344
Ok(ledger) => ledger,
345345
Err(err) => {
346346
return TestResult::error(format!(
@@ -351,7 +351,7 @@ mod tests {
351351
};
352352

353353
// add account again should throw an error
354-
if ledger.add_account(&account_id, value, ()).is_ok() {
354+
if ledger.add_account(account_id.clone(), value, ()).is_ok() {
355355
return TestResult::error(format!(
356356
"Account with id {} again should should",
357357
account_id
@@ -559,7 +559,9 @@ mod tests {
559559
value_to_remove: Value,
560560
) -> TestResult {
561561
let mut ledger = Ledger::new();
562-
ledger = ledger.add_account(&id, account_state.value(), ()).unwrap();
562+
ledger = ledger
563+
.add_account(id.clone(), account_state.value(), ())
564+
.unwrap();
563565
let result = ledger.remove_value(&id, SpendingCounter::zero(), value_to_remove);
564566
let expected_result = account_state.value() - value_to_remove;
565567
match (result, expected_result) {
@@ -588,7 +590,9 @@ mod tests {
588590
account_state: AccountState<()>,
589591
) -> TestResult {
590592
let mut ledger = Ledger::new();
591-
ledger = ledger.add_account(&id, account_state.value(), ()).unwrap();
593+
ledger = ledger
594+
.add_account(id.clone(), account_state.value(), ())
595+
.unwrap();
592596
let result = ledger.remove_account(&id);
593597
let expected_zero = account_state.value() == Value::zero();
594598
match (result, expected_zero) {

chain-impl-mockchain/src/ledger/ledger.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,15 +1568,14 @@ impl Ledger {
15681568
let account_id = account_id.clone().into();
15691569
// TODO: probably faster to just call add_account and check for already exists error
15701570
if !self.accounts.exists(&account_id) {
1571-
self.accounts =
1572-
self.accounts.add_account(&account_id, Value::zero(), ())?;
1571+
self.accounts = self.accounts.add_account(account_id, Value::zero(), ())?;
15731572
}
15741573
new_utxos.push((index as u8, output.clone()));
15751574
}
15761575
Kind::Account(identifier) => {
15771576
// don't have a way to make a newtype ref from the ref so .clone()
15781577
let account = identifier.clone().into();
1579-
self.add_value_or_create_account(&account, output.value)?;
1578+
self.add_value_or_create_account(account, output.value)?;
15801579
}
15811580
Kind::Multisig(identifier) => {
15821581
let identifier = multisig::Identifier::from(*identifier);
@@ -1596,10 +1595,10 @@ impl Ledger {
15961595

15971596
fn add_value_or_create_account(
15981597
&mut self,
1599-
account: &account::Identifier,
1598+
account: account::Identifier,
16001599
value: Value,
16011600
) -> Result<(), Error> {
1602-
self.accounts = match self.accounts.add_value(account, value) {
1601+
self.accounts = match self.accounts.add_value(&account, value) {
16031602
Ok(accounts) => accounts,
16041603
Err(account::LedgerError::NonExistent) => {
16051604
self.accounts.add_account(account, value, ())?
@@ -2043,7 +2042,7 @@ mod tests {
20432042
) -> TestResult {
20442043
let mut account_ledger = account::Ledger::new();
20452044
account_ledger = account_ledger
2046-
.add_account(&id, account_state.value(), ())
2045+
.add_account(id.clone(), account_state.value(), ())
20472046
.unwrap();
20482047
let result = super::input_single_account_verify(
20492048
account_ledger,
@@ -2089,7 +2088,9 @@ mod tests {
20892088
fn account_ledger_with_initials(initials: &[(Identifier, Value)]) -> account::Ledger {
20902089
let mut account_ledger = account::Ledger::new();
20912090
for (id, initial_value) in initials {
2092-
account_ledger = account_ledger.add_account(id, *initial_value, ()).unwrap();
2091+
account_ledger = account_ledger
2092+
.add_account(id.clone(), *initial_value, ())
2093+
.unwrap();
20932094
}
20942095
account_ledger
20952096
}
@@ -2397,7 +2398,7 @@ mod tests {
23972398

23982399
let account = AddressData::account(Discrimination::Test);
23992400
accounts = accounts
2400-
.add_account(&account.to_id(), Value(100), ())
2401+
.add_account(account.to_id(), Value(100), ())
24012402
.unwrap();
24022403

24032404
let delegation = AddressData::delegation_for(&account);
@@ -2471,7 +2472,7 @@ mod tests {
24712472

24722473
let account = AddressData::account(Discrimination::Test);
24732474
accounts = accounts
2474-
.add_account(&account.to_id(), Value(100), ())
2475+
.add_account(account.to_id(), Value(100), ())
24752476
.unwrap();
24762477

24772478
let ledger = build_ledger(utxos, accounts, multisig_ledger, params.static_params());

chain-impl-mockchain/src/multisig/ledger.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ impl Ledger {
7474
let new_decls = self
7575
.declarations
7676
.insert(identifier.clone(), declaration.clone())?;
77-
let new_accts = self.accounts.add_account(&identifier, Value::zero(), ())?;
77+
let new_accts = self.accounts.add_account(identifier, Value::zero(), ())?;
7878
Ok(Self {
7979
accounts: new_accts,
8080
declarations: new_decls,

chain-impl-mockchain/src/stake/distribution.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ mod tests {
421421
};
422422
accounts = accounts
423423
.add_account(
424-
&Identifier::from(account_public_key.clone()),
424+
Identifier::from(account_public_key.clone()),
425425
Value::zero(),
426426
(),
427427
)
@@ -445,12 +445,12 @@ mod tests {
445445

446446
// add accounts without delegation
447447
for (id, value) in stake_distribution_data.unassigned_accounts.iter().cloned() {
448-
accounts = accounts.add_account(&id, value, ()).unwrap();
448+
accounts = accounts.add_account(id, value, ()).unwrap();
449449
}
450450

451451
// add accounts with delegation
452452
for (id, value) in stake_distribution_data.assigned_accounts.iter().cloned() {
453-
accounts = accounts.add_account(&id, value, ()).unwrap();
453+
accounts = accounts.add_account(id.clone(), value, ()).unwrap();
454454
accounts = accounts
455455
.set_delegation(&id, &DelegationType::Full(id_active_pool.clone()))
456456
.unwrap();
@@ -459,15 +459,15 @@ mod tests {
459459
// add accounts with delegation as a target for delegation addresses
460460
let single_account = stake_distribution_data.single_account.clone();
461461
accounts = accounts
462-
.add_account(&single_account.0, single_account.1, ())
462+
.add_account(single_account.0.clone(), single_account.1, ())
463463
.unwrap();
464464
accounts = accounts
465465
.set_delegation(&single_account.0, &DelegationType::Full(id_active_pool))
466466
.unwrap();
467467

468468
// add accounts with retired stake pool
469469
for (id, value) in stake_distribution_data.dangling_accounts.iter().cloned() {
470-
accounts = accounts.add_account(&id, value, ()).unwrap();
470+
accounts = accounts.add_account(id.clone(), value, ()).unwrap();
471471
accounts = accounts
472472
.set_delegation(&id, &DelegationType::Full(id_retired_pool.clone()))
473473
.unwrap();

chain-impl-mockchain/src/vote/manager.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,7 @@ mod tests {
911911
.unwrap();
912912

913913
let account_ledger = account::Ledger::default()
914-
.add_account(&identifier, Value(0), ())
914+
.add_account(identifier.clone(), Value(0), ())
915915
.unwrap()
916916
.token_add(&identifier, vote_plan.voting_token().clone(), Value(100))
917917
.unwrap();
@@ -1586,7 +1586,7 @@ mod tests {
15861586
};
15871587
let value = Value(51);
15881588
let account_ledger = account::Ledger::new()
1589-
.add_account(&wallet.clone().into(), Value(0), ())
1589+
.add_account(wallet.clone().into(), Value(0), ())
15901590
.unwrap()
15911591
.token_add(&wallet.into(), token.clone(), value)
15921592
.unwrap();
@@ -1618,7 +1618,7 @@ mod tests {
16181618
.unwrap();
16191619

16201620
let account_ledger = account::Ledger::default()
1621-
.add_account(&identifier, Value(0), ())
1621+
.add_account(identifier.clone(), Value(0), ())
16221622
.unwrap()
16231623
.token_add(&identifier, vote_plan.voting_token().clone(), Value(100))
16241624
.unwrap();
@@ -1687,7 +1687,7 @@ mod tests {
16871687
.unwrap();
16881688

16891689
let account_ledger = account::Ledger::default()
1690-
.add_account(&identifier, Value(0), ())
1690+
.add_account(identifier.clone(), Value(0), ())
16911691
.unwrap()
16921692
.token_add(&identifier, vote_plan.voting_token().clone(), Value(100))
16931693
.unwrap();
@@ -1870,7 +1870,7 @@ mod tests {
18701870
let account = TestGen::identifier();
18711871
let token_totals = Default::default();
18721872
let account_ledger = account::Ledger::default()
1873-
.add_account(&account, Value(1_000), ())
1873+
.add_account(account.clone(), Value(1_000), ())
18741874
.unwrap()
18751875
.token_add(&account, token_id, Value(1_000))
18761876
.unwrap();
@@ -1909,7 +1909,7 @@ mod tests {
19091909
let account = TestGen::identifier();
19101910
let token_totals = Default::default();
19111911
let account_ledger = account::Ledger::default()
1912-
.add_account(&account, Value(1_000), ())
1912+
.add_account(account.clone(), Value(1_000), ())
19131913
.unwrap();
19141914

19151915
let token_distribution = TokenDistribution::new(&token_totals, &account_ledger);

0 commit comments

Comments
 (0)