Initial mechanics for 80:20 fee split (#2912)

* Initial mechanics for 80:20 fee split

Also:
- Introduce extra functions for Imbalance manipulation;
- Store treasury pot in an account, letting total issuance account for
  it.

* Fix some tests

* Fix some tests

* Minor cleanups

* Update parity-codec version (#2855)

* Update parity-codec version

* Update grandpa, rhododendron and trie-bench

* Use primitive-types from crates.io

* Bump impl version

* Fix trie-bench version

* Fix lock files

* Fix versions

* Update codec to 4.1

* merge fix

* Revert merge

* More reversions

* Remove accidental code

* Update locks

* Bump runtime

* Update locks

* Tweaks and label TODO

* Update srml/treasury/src/lib.rs

Co-Authored-By: DemiMarie-parity <48690212+DemiMarie-parity@users.noreply.github.com>

* Update issue number

* Update core/sr-primitives/src/traits.rs

Co-Authored-By: Robert Habermeier <rphmeier@gmail.com>

* Fix wasm build

* Fix subkey build
This commit is contained in:
Gavin Wood
2019-07-02 11:41:03 +02:00
committed by GitHub
parent bfe5347724
commit 9c6ebfeccd
14 changed files with 389 additions and 45 deletions
+49 -24
View File
@@ -70,11 +70,14 @@
#[cfg(feature = "std")]
use serde::{Serialize, Deserialize};
use rstd::prelude::*;
use srml_support::{StorageValue, StorageMap, decl_module, decl_storage, decl_event, ensure};
use srml_support::traits::{Currency, ReservableCurrency, OnDilution, OnUnbalanced, Imbalance};
use runtime_primitives::{Permill,
traits::{Zero, EnsureOrigin, StaticLookup, Saturating, CheckedSub, CheckedMul}
use srml_support::{StorageValue, StorageMap, decl_module, decl_storage, decl_event, ensure, print};
use srml_support::traits::{
Currency, ReservableCurrency, OnDilution, OnUnbalanced, Imbalance, WithdrawReason,
ExistenceRequirement
};
use runtime_primitives::{Permill, ModuleId, traits::{
Zero, EnsureOrigin, StaticLookup, CheckedSub, CheckedMul, AccountIdConversion
}};
use parity_codec::{Encode, Decode};
use system::ensure_signed;
@@ -82,6 +85,8 @@ type BalanceOf<T> = <<T as Trait>::Currency as Currency<<T as system::Trait>::Ac
type PositiveImbalanceOf<T> = <<T as Trait>::Currency as Currency<<T as system::Trait>::AccountId>>::PositiveImbalance;
type NegativeImbalanceOf<T> = <<T as Trait>::Currency as Currency<<T as system::Trait>::AccountId>>::NegativeImbalance;
const MODULE_ID: ModuleId = ModuleId(*b"py/trsry");
pub trait Trait: system::Trait {
/// The staking balance.
type Currency: Currency<Self::AccountId> + ReservableCurrency<Self::AccountId>;
@@ -135,12 +140,6 @@ decl_module! {
Self::deposit_event(RawEvent::Proposed(c));
}
/// Set the balance of funds available to spend.
fn set_pot(#[compact] new_pot: BalanceOf<T>) {
// Put the new value into storage.
<Pot<T>>::put(new_pot);
}
/// (Re-)configure this module.
fn configure(
#[compact] proposal_bond: Permill,
@@ -224,9 +223,6 @@ decl_storage! {
// State...
/// Total funds available to this module for spending.
Pot get(pot): BalanceOf<T>;
/// Number of proposals that have been made.
ProposalCount get(proposal_count): ProposalIndex;
@@ -260,6 +256,14 @@ decl_event!(
impl<T: Trait> Module<T> {
// Add public immutables and private mutables.
/// The account ID of the treasury pot.
///
/// This actually does computation. If you need to keep using it, then make sure you cache the
/// value and only call this once.
pub fn account_id() -> T::AccountId {
MODULE_ID.into_account()
}
/// The needed bond for a proposal whose spend is `value`.
fn calculate_bond(value: BalanceOf<T>) -> BalanceOf<T> {
Self::proposal_bond_minimum().max(Self::proposal_bond() * value)
@@ -298,18 +302,36 @@ impl<T: Trait> Module<T> {
});
});
T::MintedForSpending::on_unbalanced(imbalance);
if !missed_any {
// burn some proportion of the remaining budget if we run a surplus.
let burn = (Self::burn() * budget_remaining).min(budget_remaining);
budget_remaining -= burn;
imbalance.subsume(T::Currency::burn(burn));
Self::deposit_event(RawEvent::Burnt(burn))
}
Self::deposit_event(RawEvent::Rollover(budget_remaining));
if let Err(problem) = T::Currency::settle(
&Self::account_id(),
imbalance,
WithdrawReason::Transfer,
ExistenceRequirement::KeepAlive
) {
print("Inconsistent state - couldn't settle imbalance for funds spent by treasury");
// Nothing else to do here.
drop(problem);
}
<Pot<T>>::put(budget_remaining);
Self::deposit_event(RawEvent::Rollover(budget_remaining));
}
fn pot() -> BalanceOf<T> {
T::Currency::free_balance(&Self::account_id())
}
}
impl<T: Trait> OnUnbalanced<NegativeImbalanceOf<T>> for Module<T> {
fn on_unbalanced(amount: NegativeImbalanceOf<T>) {
T::Currency::resolve_creating(&Self::account_id(), amount);
}
}
@@ -322,7 +344,7 @@ impl<T: Trait> OnDilution<BalanceOf<T>> for Module<T> {
if let Some(funding) = total_issuance.checked_sub(&portion) {
let funding = funding / portion;
if let Some(funding) = funding.checked_mul(&minted) {
<Pot<T>>::mutate(|x| *x = x.saturating_add(funding));
Self::on_unbalanced(T::Currency::issue(funding));
}
}
}
@@ -519,6 +541,7 @@ mod tests {
fn accepted_spend_proposal_enacted_on_spend_period() {
with_externalities(&mut new_test_ext(), || {
Treasury::on_dilution(100, 100);
assert_eq!(Treasury::pot(), 100);
assert_ok!(Treasury::propose_spend(Origin::signed(0), 100, 3));
assert_ok!(Treasury::approve_proposal(Origin::ROOT, 0));
@@ -536,25 +559,27 @@ mod tests {
fn on_dilution_quantization_effects() {
with_externalities(&mut new_test_ext(), || {
// minted = 1% of total issuance for all cases
let _ = Treasury::set_pot(0);
assert_eq!(Balances::total_issuance(), 200);
Treasury::on_dilution(2, 66); // portion = 33% of total issuance
assert_eq!(Treasury::pot(), 4); // should increase by 4 (200 - 66) / 66 * 2
Balances::make_free_balance_be(&Treasury::account_id(), 0);
Treasury::on_dilution(2, 67); // portion = 33+eps% of total issuance
assert_eq!(Treasury::pot(), 6); // should increase by 2 (200 - 67) / 67 * 2
assert_eq!(Treasury::pot(), 2); // should increase by 2 (200 - 67) / 67 * 2
Balances::make_free_balance_be(&Treasury::account_id(), 0);
Treasury::on_dilution(2, 100); // portion = 50% of total issuance
assert_eq!(Treasury::pot(), 8); // should increase by 2 (200 - 100) / 100 * 2
assert_eq!(Treasury::pot(), 2); // should increase by 2 (200 - 100) / 100 * 2
Balances::make_free_balance_be(&Treasury::account_id(), 0);
// If any more than 50% of the network is staked (i.e. (2 * portion) > total_issuance)
// then the pot will not increase.
Treasury::on_dilution(2, 101); // portion = 50+eps% of total issuance
assert_eq!(Treasury::pot(), 8); // should increase by 0 (200 - 101) / 101 * 2
assert_eq!(Treasury::pot(), 0); // should increase by 0 (200 - 101) / 101 * 2
Treasury::on_dilution(2, 134); // portion = 67% of total issuance
assert_eq!(Treasury::pot(), 8); // should increase by 0 (200 - 134) / 134 * 2
assert_eq!(Treasury::pot(), 0); // should increase by 0 (200 - 134) / 134 * 2
});
}
@@ -572,7 +597,7 @@ mod tests {
Treasury::on_dilution(100, 100);
<Treasury as OnFinalize<u64>>::on_finalize(4);
assert_eq!(Balances::free_balance(&3), 150);
assert_eq!(Treasury::pot(), 25);
assert_eq!(Treasury::pot(), 75);
});
}
}