Sr treasury fix (#739)

* treasury: use 'mutate' and 'retain' to avoid creating a new vec

* treasury: check the underflow case
This commit is contained in:
Guanqun Lu
2018-09-14 15:50:22 +08:00
committed by Gav Wood
parent de4db765f5
commit b76660afd4
+22 -21
View File
@@ -109,7 +109,7 @@ decl_storage! {
/// proposal gets these back. A rejected proposal doesn't. /// proposal gets these back. A rejected proposal doesn't.
ProposalBond get(proposal_bond): required Permill; 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; ProposalBondMinimum get(proposal_bond_minimum): required T::Balance;
/// Period between successive spends. /// Period between successive spends.
@@ -227,34 +227,35 @@ impl<T: Trait> Module<T> {
Self::deposit_event(RawEvent::Spending(budget_remaining)); Self::deposit_event(RawEvent::Spending(budget_remaining));
let mut missed_any = false; let mut missed_any = false;
let remaining_approvals: Vec<_> = <Approvals<T>>::get().into_iter().filter(|&index| { <Approvals<T>>::mutate(|v| {
// Should always be true, but shouldn't panic if false or we're screwed. v.retain(|&index| {
if let Some(p) = Self::proposals(index) { // Should always be true, but shouldn't panic if false or we're screwed.
if p.value <= budget_remaining { if let Some(p) = Self::proposals(index) {
budget_remaining -= p.value; if p.value <= budget_remaining {
<Proposals<T>>::remove(index); budget_remaining -= p.value;
<Proposals<T>>::remove(index);
// return their deposit. // return their deposit.
let _ = <balances::Module<T>>::unreserve(&p.proposer, p.bond); let _ = <balances::Module<T>>::unreserve(&p.proposer, p.bond);
// provide the allocation. // provide the allocation.
<balances::Module<T>>::increase_free_balance_creating(&p.beneficiary, p.value); <balances::Module<T>>::increase_free_balance_creating(&p.beneficiary, p.value);
Self::deposit_event(RawEvent::Awarded(index, p.value, p.beneficiary)); Self::deposit_event(RawEvent::Awarded(index, p.value, p.beneficiary));
false false
} else {
missed_any = true;
true
}
} else { } else {
missed_any = true; false
true
} }
} else { });
false });
}
}).collect();
<Approvals<T>>::put(remaining_approvals);
if !missed_any { if !missed_any {
// burn some proportion of the remaining budget if we run a surplus. // 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; budget_remaining -= burn;
Self::deposit_event(RawEvent::Burnt(burn)) Self::deposit_event(RawEvent::Burnt(burn))
} }