Turn storage items into parameters (#2883)

* balances: Turn storage items into parameters.

* contract: Turn storage items into parameters.

* council: Turn storage items into parameters.

* finality-tracker: Turn storage items into parameters.

* treasury: Turn storage items into parameters.

* democracy: Fix tests.

* example: Fix tests.

* executive: Fix tests.

* staking: Fix tests.

* Update runtime.

* Update template-node.

* Update runtime version.

* Fix executor tests.

* Fix node cli tests.

* Address grumbles.

* Add removed default values to docs.

* Make gas price a storage item.

* Set associated consts must be callable outside of build.

* Fix not enough gas to pay for transfer fee.

* Fix build.

* Emit metadata.

* Fix build.

* Add default values for all parameter types.

* Fix build.

* Fix build.

* Fix build.

* Fix build.
This commit is contained in:
David Craven
2019-07-02 12:52:58 +02:00
committed by GitHub
parent b2622b611e
commit cf036f685e
23 changed files with 943 additions and 597 deletions
+62 -53
View File
@@ -72,12 +72,13 @@ use serde::{Serialize, Deserialize};
use rstd::prelude::*;
use srml_support::{StorageValue, StorageMap, decl_module, decl_storage, decl_event, ensure, print};
use srml_support::traits::{
Currency, ReservableCurrency, OnDilution, OnUnbalanced, Imbalance, WithdrawReason,
ExistenceRequirement
Currency, ExistenceRequirement, Get, Imbalance, OnDilution, OnUnbalanced,
ReservableCurrency, WithdrawReason
};
use runtime_primitives::{Permill, ModuleId, traits::{
use runtime_primitives::{Permill, ModuleId};
use runtime_primitives::traits::{
Zero, EnsureOrigin, StaticLookup, CheckedSub, CheckedMul, AccountIdConversion
}};
};
use parity_codec::{Encode, Decode};
use system::ensure_signed;
@@ -87,6 +88,11 @@ type NegativeImbalanceOf<T> = <<T as Trait>::Currency as Currency<<T as system::
const MODULE_ID: ModuleId = ModuleId(*b"py/trsry");
pub const DEFAULT_PROPOSAL_BOND: u32 = 0;
pub const DEFAULT_PROPOSAL_BOND_MINIMUM: u32 = 0;
pub const DEFAULT_SPEND_PERIOD: u32 = 0;
pub const DEFAULT_BURN: u32 = 0;
pub trait Trait: system::Trait {
/// The staking balance.
type Currency: Currency<Self::AccountId> + ReservableCurrency<Self::AccountId>;
@@ -105,12 +111,38 @@ pub trait Trait: system::Trait {
/// Handler for the unbalanced decrease when slashing for a rejected proposal.
type ProposalRejection: OnUnbalanced<NegativeImbalanceOf<Self>>;
/// Fraction of a proposal's value that should be bonded in order to place the proposal.
/// An accepted proposal gets these back. A rejected proposal does not.
type ProposalBond: Get<Permill>;
/// Minimum amount of funds that should be placed in a deposit for making a proposal.
type ProposalBondMinimum: Get<BalanceOf<Self>>;
/// Period between successive spends.
type SpendPeriod: Get<Self::BlockNumber>;
/// Percentage of spare funds (if any) that are burnt per spend period.
type Burn: Get<Permill>;
}
type ProposalIndex = u32;
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
/// Fraction of a proposal's value that should be bonded in order to place the proposal.
/// An accepted proposal gets these back. A rejected proposal does not.
const ProposalBond: Permill = T::ProposalBond::get();
/// Minimum amount of funds that should be placed in a deposit for making a proposal.
const ProposalBondMinimum: BalanceOf<T> = T::ProposalBondMinimum::get();
/// Period between successive spends.
const SpendPeriod: T::BlockNumber = T::SpendPeriod::get();
/// Percentage of spare funds (if any) that are burnt per spend period.
const Burn: Permill = T::Burn::get();
fn deposit_event<T>() = default;
/// Put forward a suggestion for spending. A deposit proportional to the value
/// is reserved and slashed if the proposal is rejected. It is returned once the
@@ -140,19 +172,6 @@ decl_module! {
Self::deposit_event(RawEvent::Proposed(c));
}
/// (Re-)configure this module.
fn configure(
#[compact] proposal_bond: Permill,
#[compact] proposal_bond_minimum: BalanceOf<T>,
#[compact] spend_period: T::BlockNumber,
#[compact] burn: Permill
) {
ProposalBond::put(proposal_bond);
<ProposalBondMinimum<T>>::put(proposal_bond_minimum);
<SpendPeriod<T>>::put(spend_period);
Burn::put(burn);
}
/// Reject a proposed spend. The original deposit will be slashed.
///
/// # <weight>
@@ -187,7 +206,7 @@ decl_module! {
fn on_finalize(n: T::BlockNumber) {
// Check to see if we should spend some funds!
if (n % Self::spend_period()).is_zero() {
if (n % T::SpendPeriod::get()).is_zero() {
Self::spend_funds();
}
}
@@ -206,23 +225,6 @@ pub struct Proposal<AccountId, Balance> {
decl_storage! {
trait Store for Module<T: Trait> as Treasury {
// Config...
/// Fraction of a proposal's value that should be bonded in order to place the proposal.
/// An accepted proposal gets these back. A rejected proposal does not.
ProposalBond get(proposal_bond) config(): Permill;
/// Minimum amount of funds that should be placed in a deposit for making a proposal.
ProposalBondMinimum get(proposal_bond_minimum) config(): BalanceOf<T>;
/// Period between successive spends.
SpendPeriod get(spend_period) config(): T::BlockNumber = runtime_primitives::traits::One::one();
/// Percentage of spare funds (if any) that are burnt per spend period.
Burn get(burn) config(): Permill;
// State...
/// Number of proposals that have been made.
ProposalCount get(proposal_count): ProposalIndex;
@@ -266,7 +268,7 @@ impl<T: Trait> Module<T> {
/// 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)
T::ProposalBondMinimum::get().max(T::ProposalBond::get() * value)
}
// Spend some money!
@@ -304,7 +306,7 @@ impl<T: Trait> Module<T> {
if !missed_any {
// burn some proportion of the remaining budget if we run a surplus.
let burn = (Self::burn() * budget_remaining).min(budget_remaining);
let burn = (T::Burn::get() * budget_remaining).min(budget_remaining);
budget_remaining -= burn;
imbalance.subsume(T::Currency::burn(burn));
Self::deposit_event(RawEvent::Burnt(burn))
@@ -356,7 +358,7 @@ mod tests {
use super::*;
use runtime_io::with_externalities;
use srml_support::{impl_outer_origin, assert_ok, assert_noop};
use srml_support::{assert_noop, assert_ok, impl_outer_origin, parameter_types};
use substrate_primitives::{H256, Blake2Hasher};
use runtime_primitives::{traits::{BlakeTwo256, OnFinalize, IdentityLookup}, testing::Header};
@@ -377,6 +379,13 @@ mod tests {
type Header = Header;
type Event = ();
}
parameter_types! {
pub const ExistentialDeposit: u64 = 0;
pub const TransferFee: u64 = 0;
pub const CreationFee: u64 = 0;
pub const TransactionBaseFee: u64 = 0;
pub const TransactionByteFee: u64 = 0;
}
impl balances::Trait for Test {
type Balance = u64;
type OnNewAccount = ();
@@ -385,6 +394,17 @@ mod tests {
type TransactionPayment = ();
type TransferPayment = ();
type DustRemoval = ();
type ExistentialDeposit = ExistentialDeposit;
type TransferFee = TransferFee;
type CreationFee = CreationFee;
type TransactionBaseFee = TransactionBaseFee;
type TransactionByteFee = TransactionByteFee;
}
parameter_types! {
pub const ProposalBond: Permill = Permill::from_percent(5);
pub const ProposalBondMinimum: u64 = 1;
pub const SpendPeriod: u64 = 2;
pub const Burn: Permill = Permill::from_percent(50);
}
impl Trait for Test {
type Currency = balances::Module<Test>;
@@ -393,6 +413,10 @@ mod tests {
type Event = ();
type MintedForSpending = ();
type ProposalRejection = ();
type ProposalBond = ProposalBond;
type ProposalBondMinimum = ProposalBondMinimum;
type SpendPeriod = SpendPeriod;
type Burn = Burn;
}
type Balances = balances::Module<Test>;
type Treasury = Module<Test>;
@@ -401,29 +425,14 @@ mod tests {
let mut t = system::GenesisConfig::default().build_storage::<Test>().unwrap().0;
t.extend(balances::GenesisConfig::<Test>{
balances: vec![(0, 100), (1, 99), (2, 1)],
transaction_base_fee: 0,
transaction_byte_fee: 0,
transfer_fee: 0,
creation_fee: 0,
existential_deposit: 0,
vesting: vec![],
}.build_storage().unwrap().0);
t.extend(GenesisConfig::<Test>{
proposal_bond: Permill::from_percent(5),
proposal_bond_minimum: 1,
spend_period: 2,
burn: Permill::from_percent(50),
}.build_storage().unwrap().0);
t.into()
}
#[test]
fn genesis_config_works() {
with_externalities(&mut new_test_ext(), || {
assert_eq!(Treasury::proposal_bond(), Permill::from_percent(5));
assert_eq!(Treasury::proposal_bond_minimum(), 1);
assert_eq!(Treasury::spend_period(), 2);
assert_eq!(Treasury::burn(), Permill::from_percent(50));
assert_eq!(Treasury::pot(), 0);
assert_eq!(Treasury::proposal_count(), 0);
});