From ecd909fd1b92a377e77bb1cac151e0594f968d15 Mon Sep 17 00:00:00 2001 From: Guanqun Lu Date: Thu, 13 Sep 2018 13:08:46 +0800 Subject: [PATCH 1/2] treasury: use 'mutate' and 'retain' to avoid creating a new vec --- srml/treasury/src/lib.rs | 47 ++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/srml/treasury/src/lib.rs b/srml/treasury/src/lib.rs index 51d00224d8fec..716c5394114fb 100644 --- a/srml/treasury/src/lib.rs +++ b/srml/treasury/src/lib.rs @@ -109,7 +109,7 @@ decl_storage! { /// proposal gets these back. A rejected proposal doesn't. ProposalBond get(proposal_bond): required Permill; - /// Minimum amount of funds that should be placed ina deposit for making a proposal. + /// Minimum amount of funds that should be placed in a deposit for making a proposal. ProposalBondMinimum get(proposal_bond_minimum): required T::Balance; /// Period between successive spends. @@ -227,30 +227,31 @@ impl Module { Self::deposit_event(RawEvent::Spending(budget_remaining)); let mut missed_any = false; - let remaining_approvals: Vec<_> = >::get().into_iter().filter(|&index| { - // Should always be true, but shouldn't panic if false or we're screwed. - if let Some(p) = Self::proposals(index) { - if p.value <= budget_remaining { - budget_remaining -= p.value; - >::remove(index); - - // return their deposit. - let _ = >::unreserve(&p.proposer, p.bond); - - // provide the allocation. - >::increase_free_balance_creating(&p.beneficiary, p.value); - - Self::deposit_event(RawEvent::Awarded(index, p.value, p.beneficiary)); - false + >::mutate(|v| { + v.retain(|&index| { + // Should always be true, but shouldn't panic if false or we're screwed. + if let Some(p) = Self::proposals(index) { + if p.value <= budget_remaining { + budget_remaining -= p.value; + >::remove(index); + + // return their deposit. + let _ = >::unreserve(&p.proposer, p.bond); + + // provide the allocation. + >::increase_free_balance_creating(&p.beneficiary, p.value); + + Self::deposit_event(RawEvent::Awarded(index, p.value, p.beneficiary)); + false + } else { + missed_any = true; + true + } } else { - missed_any = true; - true + false } - } else { - false - } - }).collect(); - >::put(remaining_approvals); + }); + }); if !missed_any { // burn some proportion of the remaining budget if we run a surplus. From 0163bb4c0993c18d034daf09f22f925d199412c6 Mon Sep 17 00:00:00 2001 From: Guanqun Lu Date: Thu, 13 Sep 2018 14:23:02 +0800 Subject: [PATCH 2/2] treasury: check the underflow case --- srml/treasury/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/srml/treasury/src/lib.rs b/srml/treasury/src/lib.rs index 716c5394114fb..6026917a26eb4 100644 --- a/srml/treasury/src/lib.rs +++ b/srml/treasury/src/lib.rs @@ -255,7 +255,7 @@ impl Module { if !missed_any { // burn some proportion of the remaining budget if we run a surplus. - let burn = Self::burn().times(budget_remaining); + let burn = Self::burn().times(budget_remaining).min(budget_remaining); budget_remaining -= burn; Self::deposit_event(RawEvent::Burnt(burn)) }