mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-09 19:01:08 +00:00
Benchmark Treasury Pallet (#5287)
* Start benchmarks * try_origin or root * More benches * stuck * Custom trait functions for benchmarks * finish benchmarks * Bump impl * More comments * Bump spec * Remove import * Update frame/elections-phragmen/src/lib.rs Co-Authored-By: thiolliere <gui.thiolliere@gmail.com> * Update frame/support/src/traits.rs Co-Authored-By: thiolliere <gui.thiolliere@gmail.com> * Fix merge Co-authored-by: thiolliere <gui.thiolliere@gmail.com>
This commit is contained in:
Generated
+1
@@ -4525,6 +4525,7 @@ dependencies = [
|
||||
name = "pallet-treasury"
|
||||
version = "2.0.0-alpha.4"
|
||||
dependencies = [
|
||||
"frame-benchmarking",
|
||||
"frame-support",
|
||||
"frame-system",
|
||||
"pallet-balances",
|
||||
|
||||
@@ -135,9 +135,14 @@ std = [
|
||||
]
|
||||
runtime-benchmarks = [
|
||||
"frame-benchmarking",
|
||||
"pallet-timestamp/runtime-benchmarks",
|
||||
"pallet-identity/runtime-benchmarks",
|
||||
"frame-support/runtime-benchmarks",
|
||||
"pallet-balances/runtime-benchmarks",
|
||||
"pallet-elections-phragmen/runtime-benchmarks",
|
||||
"pallet-identity/runtime-benchmarks",
|
||||
"pallet-timestamp/runtime-benchmarks",
|
||||
"pallet-treasury/runtime-benchmarks",
|
||||
"pallet-session-benchmarking",
|
||||
"pallet-staking/runtime-benchmarks",
|
||||
"pallet-vesting/runtime-benchmarks",
|
||||
"pallet-session-benchmarking",
|
||||
"pallet-staking/runtime-benchmarks",
|
||||
|
||||
@@ -899,6 +899,13 @@ impl_runtime_apis! {
|
||||
steps,
|
||||
repeat,
|
||||
),
|
||||
b"pallet-treasury" | b"treasury" => Treasury::run_benchmark(
|
||||
extrinsic,
|
||||
lowest_range_values,
|
||||
highest_range_values,
|
||||
steps,
|
||||
repeat,
|
||||
),
|
||||
b"pallet-vesting" | b"vesting" => Vesting::run_benchmark(
|
||||
extrinsic,
|
||||
lowest_range_values,
|
||||
|
||||
@@ -34,3 +34,4 @@ std = [
|
||||
"frame-system/std",
|
||||
"sp-std/std",
|
||||
]
|
||||
runtime-benchmarks = ["frame-support/runtime-benchmarks"]
|
||||
|
||||
@@ -775,6 +775,18 @@ impl<T: Trait> Contains<T::AccountId> for Module<T> {
|
||||
Self::is_member(who)
|
||||
}
|
||||
fn sorted_members() -> Vec<T::AccountId> { Self::members_ids() }
|
||||
|
||||
// A special function to populate members in this pallet for passing Origin
|
||||
// checks in runtime benchmarking.
|
||||
#[cfg(feature = "runtime-benchmarks")]
|
||||
fn add(who: &T::AccountId) {
|
||||
Members::<T>::mutate(|members| {
|
||||
match members.binary_search_by(|(a, _b)| a.cmp(who)) {
|
||||
Ok(_) => (),
|
||||
Err(pos) => members.insert(pos, (who.clone(), BalanceOf::<T>::default())),
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -49,3 +49,4 @@ std = [
|
||||
]
|
||||
nightly = []
|
||||
strict = []
|
||||
runtime-benchmarks = []
|
||||
|
||||
@@ -171,6 +171,13 @@ pub trait Contains<T: Ord> {
|
||||
|
||||
/// Get the number of items in the set.
|
||||
fn count() -> usize { Self::sorted_members().len() }
|
||||
|
||||
/// Add an item that would satisfy `contains`. It does not make sure any other
|
||||
/// state is correctly maintained or generated.
|
||||
///
|
||||
/// **Should be used for benchmarking only!!!**
|
||||
#[cfg(feature = "runtime-benchmarks")]
|
||||
fn add(t: &T);
|
||||
}
|
||||
|
||||
/// Determiner to say whether a given account is unused.
|
||||
|
||||
@@ -17,6 +17,8 @@ frame-support = { version = "2.0.0-alpha.4", default-features = false, path = ".
|
||||
frame-system = { version = "2.0.0-alpha.4", default-features = false, path = "../system" }
|
||||
pallet-balances = { version = "2.0.0-alpha.4", default-features = false, path = "../balances" }
|
||||
|
||||
frame-benchmarking = { version = "2.0.0-alpha.2", default-features = false, path = "../benchmarking", optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
sp-io ={ version = "2.0.0-alpha.4", path = "../../primitives/io" }
|
||||
sp-core = { version = "2.0.0-alpha.4", path = "../../primitives/core" }
|
||||
@@ -32,3 +34,7 @@ std = [
|
||||
"frame-system/std",
|
||||
"pallet-balances/std",
|
||||
]
|
||||
runtime-benchmarks = [
|
||||
"frame-benchmarking",
|
||||
"frame-support/runtime-benchmarks",
|
||||
]
|
||||
|
||||
@@ -0,0 +1,211 @@
|
||||
// Copyright 2020 Parity Technologies (UK) Ltd.
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Substrate is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
|
||||
// Substrate is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
//! Treasury pallet benchmarking.
|
||||
|
||||
use super::*;
|
||||
|
||||
use frame_system::RawOrigin;
|
||||
use frame_benchmarking::{benchmarks, account};
|
||||
use sp_runtime::traits::OnFinalize;
|
||||
|
||||
use crate::Module as Treasury;
|
||||
|
||||
const SEED: u32 = 0;
|
||||
|
||||
// Create the pre-requisite information needed to create a treasury `propose_spend`.
|
||||
fn setup_proposal<T: Trait>(u: u32) -> (
|
||||
T::AccountId,
|
||||
BalanceOf<T>,
|
||||
<T::Lookup as StaticLookup>::Source,
|
||||
) {
|
||||
let caller = account("caller", u, SEED);
|
||||
let value: BalanceOf<T> = T::ProposalBondMinimum::get().saturating_mul(100.into());
|
||||
let _ = T::Currency::make_free_balance_be(&caller, value);
|
||||
let beneficiary = account("beneficiary", u, SEED);
|
||||
let beneficiary_lookup = T::Lookup::unlookup(beneficiary);
|
||||
(caller, value, beneficiary_lookup)
|
||||
}
|
||||
|
||||
// Create the pre-requisite information needed to create a `report_awesome`.
|
||||
fn setup_awesome<T: Trait>(length: u32) -> (T::AccountId, Vec<u8>, T::AccountId) {
|
||||
let caller = account("caller", 0, SEED);
|
||||
let value = T::TipReportDepositBase::get()
|
||||
+ T::TipReportDepositPerByte::get() * length.into()
|
||||
+ T::Currency::minimum_balance();
|
||||
let _ = T::Currency::make_free_balance_be(&caller, value);
|
||||
let reason = vec![0; length as usize];
|
||||
let awesome_person = account("awesome", 0, SEED);
|
||||
(caller, reason, awesome_person)
|
||||
}
|
||||
|
||||
// Create the pre-requisite information needed to call `tip_new`.
|
||||
fn setup_tip<T: Trait>(r: u32, t: u32) ->
|
||||
Result<(T::AccountId, Vec<u8>, T::AccountId, BalanceOf<T>), &'static str>
|
||||
{
|
||||
for i in 0 .. t {
|
||||
let member = account("member", i, SEED);
|
||||
T::Tippers::add(&member);
|
||||
}
|
||||
|
||||
ensure!(T::Tippers::count() == t as usize, "problem creating tippers");
|
||||
let caller = account("member", t - 1, SEED);
|
||||
let reason = vec![0; r as usize];
|
||||
let beneficiary = account("beneficiary", t, SEED);
|
||||
let value = T::Currency::minimum_balance().saturating_mul(100.into());
|
||||
Ok((caller, reason, beneficiary, value))
|
||||
}
|
||||
|
||||
// Create `t` new types for the tip proposal with `hash`.
|
||||
// This function automatically moves forward the block number to a time which
|
||||
// would resolve the tipping process.
|
||||
fn create_tips<T: Trait>(t: u32, hash: T::Hash, value: BalanceOf<T>) -> Result<(), &'static str> {
|
||||
for i in 0 .. t {
|
||||
let caller = account("member", i, SEED);
|
||||
ensure!(T::Tippers::contains(&caller), "caller is not a tipper");
|
||||
Treasury::<T>::tip(RawOrigin::Signed(caller).into(), hash, value)?;
|
||||
}
|
||||
frame_system::Module::<T>::set_block_number(T::TipCountdown::get() * 10.into());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// Create proposals that are approved for use in `on_finalize`.
|
||||
fn create_approved_proposals<T: Trait>(n: u32) -> Result<(), &'static str> {
|
||||
for i in 0 .. n {
|
||||
let (caller, value, lookup) = setup_proposal::<T>(i);
|
||||
Treasury::<T>::propose_spend(
|
||||
RawOrigin::Signed(caller).into(),
|
||||
value,
|
||||
lookup
|
||||
)?;
|
||||
let proposal_id = ProposalCount::get() - 1;
|
||||
Treasury::<T>::approve_proposal(RawOrigin::Root.into(), proposal_id)?;
|
||||
}
|
||||
ensure!(Approvals::get().len() == n as usize, "Not all approved");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
const MAX_BYTES: u32 = 16384;
|
||||
const MAX_TIPPERS: u32 = 100;
|
||||
|
||||
benchmarks! {
|
||||
_ { }
|
||||
|
||||
propose_spend {
|
||||
let u in 0 .. 1000;
|
||||
let (caller, value, beneficiary_lookup) = setup_proposal::<T>(u);
|
||||
}: _(RawOrigin::Signed(caller), value, beneficiary_lookup)
|
||||
|
||||
reject_proposal {
|
||||
let u in 0 .. 1000;
|
||||
let (caller, value, beneficiary_lookup) = setup_proposal::<T>(u);
|
||||
Treasury::<T>::propose_spend(
|
||||
RawOrigin::Signed(caller).into(),
|
||||
value,
|
||||
beneficiary_lookup
|
||||
)?;
|
||||
let proposal_id = ProposalCount::get() - 1;
|
||||
}: _(RawOrigin::Root, proposal_id)
|
||||
|
||||
approve_proposal {
|
||||
let u in 0 .. 1000;
|
||||
let (caller, value, beneficiary_lookup) = setup_proposal::<T>(u);
|
||||
Treasury::<T>::propose_spend(
|
||||
RawOrigin::Signed(caller).into(),
|
||||
value,
|
||||
beneficiary_lookup
|
||||
)?;
|
||||
let proposal_id = ProposalCount::get() - 1;
|
||||
}: _(RawOrigin::Root, proposal_id)
|
||||
|
||||
report_awesome {
|
||||
let r in 0 .. MAX_BYTES;
|
||||
let (caller, reason, awesome_person) = setup_awesome::<T>(r);
|
||||
}: _(RawOrigin::Signed(caller), reason, awesome_person)
|
||||
|
||||
retract_tip {
|
||||
let r in 0 .. MAX_BYTES;
|
||||
let (caller, reason, awesome_person) = setup_awesome::<T>(r);
|
||||
Treasury::<T>::report_awesome(
|
||||
RawOrigin::Signed(caller.clone()).into(),
|
||||
reason.clone(),
|
||||
awesome_person.clone()
|
||||
)?;
|
||||
let reason_hash = T::Hashing::hash(&reason[..]);
|
||||
let hash = T::Hashing::hash_of(&(&reason_hash, &awesome_person));
|
||||
}: _(RawOrigin::Signed(caller), hash)
|
||||
|
||||
tip_new {
|
||||
let r in 0 .. MAX_BYTES;
|
||||
let t in 1 .. MAX_TIPPERS;
|
||||
|
||||
let (caller, reason, beneficiary, value) = setup_tip::<T>(r, t)?;
|
||||
}: _(RawOrigin::Signed(caller), reason, beneficiary, value)
|
||||
|
||||
tip {
|
||||
let t in 1 .. MAX_TIPPERS;
|
||||
let (member, reason, beneficiary, value) = setup_tip::<T>(0, t)?;
|
||||
let value = T::Currency::minimum_balance().saturating_mul(100.into());
|
||||
Treasury::<T>::tip_new(
|
||||
RawOrigin::Signed(member).into(),
|
||||
reason.clone(),
|
||||
beneficiary.clone(),
|
||||
value
|
||||
)?;
|
||||
let reason_hash = T::Hashing::hash(&reason[..]);
|
||||
let hash = T::Hashing::hash_of(&(&reason_hash, &beneficiary));
|
||||
ensure!(Tips::<T>::contains_key(hash), "tip does not exist");
|
||||
create_tips::<T>(t - 1, hash.clone(), value)?;
|
||||
let caller = account("member", t - 1, SEED);
|
||||
}: _(RawOrigin::Signed(caller), hash, value)
|
||||
|
||||
close_tip {
|
||||
let t in 1 .. MAX_TIPPERS;
|
||||
|
||||
// Make sure pot is funded
|
||||
let pot_account = Treasury::<T>::account_id();
|
||||
let value = T::Currency::minimum_balance().saturating_mul(1_000_000_000.into());
|
||||
let _ = T::Currency::make_free_balance_be(&pot_account, value);
|
||||
|
||||
// Set up a new tip proposal
|
||||
let (member, reason, beneficiary, value) = setup_tip::<T>(0, t)?;
|
||||
let value = T::Currency::minimum_balance().saturating_mul(100.into());
|
||||
Treasury::<T>::tip_new(
|
||||
RawOrigin::Signed(member).into(),
|
||||
reason.clone(),
|
||||
beneficiary.clone(),
|
||||
value
|
||||
)?;
|
||||
|
||||
// Create a bunch of tips
|
||||
let reason_hash = T::Hashing::hash(&reason[..]);
|
||||
let hash = T::Hashing::hash_of(&(&reason_hash, &beneficiary));
|
||||
ensure!(Tips::<T>::contains_key(hash), "tip does not exist");
|
||||
create_tips::<T>(t, hash.clone(), value)?;
|
||||
|
||||
let caller = account("caller", t, SEED);
|
||||
}: _(RawOrigin::Signed(caller), hash)
|
||||
|
||||
on_finalize {
|
||||
let p in 0 .. 100;
|
||||
let pot_account = Treasury::<T>::account_id();
|
||||
let value = T::Currency::minimum_balance().saturating_mul(1_000_000_000.into());
|
||||
let _ = T::Currency::make_free_balance_be(&pot_account, value);
|
||||
create_approved_proposals::<T>(p)?;
|
||||
}: {
|
||||
Treasury::<T>::on_finalize(T::BlockNumber::zero());
|
||||
}
|
||||
}
|
||||
@@ -100,7 +100,12 @@ use sp_runtime::{Permill, ModuleId, Percent, RuntimeDebug, traits::{
|
||||
}};
|
||||
use frame_support::{weights::SimpleDispatchInfo, traits::Contains};
|
||||
use codec::{Encode, Decode};
|
||||
use frame_system::{self as system, ensure_signed};
|
||||
use frame_system::{self as system, ensure_signed, ensure_root};
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
#[cfg(feature = "runtime-benchmarks")]
|
||||
mod benchmarking;
|
||||
|
||||
type BalanceOf<T> = <<T as Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::Balance;
|
||||
type PositiveImbalanceOf<T> = <<T as Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::PositiveImbalance;
|
||||
@@ -352,9 +357,11 @@ decl_module! {
|
||||
/// # </weight>
|
||||
#[weight = SimpleDispatchInfo::FixedOperational(100_000)]
|
||||
fn reject_proposal(origin, #[compact] proposal_id: ProposalIndex) {
|
||||
T::RejectOrigin::ensure_origin(origin)?;
|
||||
let proposal = <Proposals<T>>::take(&proposal_id).ok_or(Error::<T>::InvalidProposalIndex)?;
|
||||
T::RejectOrigin::try_origin(origin)
|
||||
.map(|_| ())
|
||||
.or_else(ensure_root)?;
|
||||
|
||||
let proposal = <Proposals<T>>::take(&proposal_id).ok_or(Error::<T>::InvalidProposalIndex)?;
|
||||
let value = proposal.bond;
|
||||
let imbalance = T::Currency::slash_reserved(&proposal.proposer, value).0;
|
||||
T::ProposalRejection::on_unbalanced(imbalance);
|
||||
@@ -372,10 +379,11 @@ decl_module! {
|
||||
/// # </weight>
|
||||
#[weight = SimpleDispatchInfo::FixedOperational(100_000)]
|
||||
fn approve_proposal(origin, #[compact] proposal_id: ProposalIndex) {
|
||||
T::ApproveOrigin::ensure_origin(origin)?;
|
||||
T::ApproveOrigin::try_origin(origin)
|
||||
.map(|_| ())
|
||||
.or_else(ensure_root)?;
|
||||
|
||||
ensure!(<Proposals<T>>::contains_key(proposal_id), Error::<T>::InvalidProposalIndex);
|
||||
|
||||
Approvals::mutate(|v| v.push(proposal_id));
|
||||
}
|
||||
|
||||
@@ -718,456 +726,3 @@ impl<T: Trait> OnUnbalanced<NegativeImbalanceOf<T>> for Module<T> {
|
||||
Self::deposit_event(RawEvent::Deposit(numeric_amount));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
use frame_support::{assert_noop, assert_ok, impl_outer_origin, parameter_types, weights::Weight};
|
||||
use frame_support::traits::Contains;
|
||||
use sp_core::H256;
|
||||
use sp_runtime::{
|
||||
Perbill,
|
||||
testing::Header,
|
||||
traits::{BlakeTwo256, OnFinalize, IdentityLookup, BadOrigin},
|
||||
};
|
||||
|
||||
impl_outer_origin! {
|
||||
pub enum Origin for Test where system = frame_system {}
|
||||
}
|
||||
|
||||
#[derive(Clone, Eq, PartialEq)]
|
||||
pub struct Test;
|
||||
parameter_types! {
|
||||
pub const BlockHashCount: u64 = 250;
|
||||
pub const MaximumBlockWeight: Weight = 1024;
|
||||
pub const MaximumBlockLength: u32 = 2 * 1024;
|
||||
pub const AvailableBlockRatio: Perbill = Perbill::one();
|
||||
}
|
||||
impl frame_system::Trait for Test {
|
||||
type Origin = Origin;
|
||||
type Index = u64;
|
||||
type BlockNumber = u64;
|
||||
type Call = ();
|
||||
type Hash = H256;
|
||||
type Hashing = BlakeTwo256;
|
||||
type AccountId = u64;
|
||||
type Lookup = IdentityLookup<Self::AccountId>;
|
||||
type Header = Header;
|
||||
type Event = ();
|
||||
type BlockHashCount = BlockHashCount;
|
||||
type MaximumBlockWeight = MaximumBlockWeight;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type Version = ();
|
||||
type ModuleToIndex = ();
|
||||
type AccountData = pallet_balances::AccountData<u64>;
|
||||
type OnNewAccount = ();
|
||||
type OnKilledAccount = ();
|
||||
}
|
||||
parameter_types! {
|
||||
pub const ExistentialDeposit: u64 = 1;
|
||||
}
|
||||
impl pallet_balances::Trait for Test {
|
||||
type Balance = u64;
|
||||
type Event = ();
|
||||
type DustRemoval = ();
|
||||
type ExistentialDeposit = ExistentialDeposit;
|
||||
type AccountStore = System;
|
||||
}
|
||||
pub struct TenToFourteen;
|
||||
impl Contains<u64> for TenToFourteen {
|
||||
fn contains(n: &u64) -> bool {
|
||||
*n >= 10 && *n <= 14
|
||||
}
|
||||
fn sorted_members() -> Vec<u64> {
|
||||
vec![10, 11, 12, 13, 14]
|
||||
}
|
||||
}
|
||||
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);
|
||||
pub const TipCountdown: u64 = 1;
|
||||
pub const TipFindersFee: Percent = Percent::from_percent(20);
|
||||
pub const TipReportDepositBase: u64 = 1;
|
||||
pub const TipReportDepositPerByte: u64 = 1;
|
||||
}
|
||||
impl Trait for Test {
|
||||
type Currency = pallet_balances::Module<Test>;
|
||||
type ApproveOrigin = frame_system::EnsureRoot<u64>;
|
||||
type RejectOrigin = frame_system::EnsureRoot<u64>;
|
||||
type Tippers = TenToFourteen;
|
||||
type TipCountdown = TipCountdown;
|
||||
type TipFindersFee = TipFindersFee;
|
||||
type TipReportDepositBase = TipReportDepositBase;
|
||||
type TipReportDepositPerByte = TipReportDepositPerByte;
|
||||
type Event = ();
|
||||
type ProposalRejection = ();
|
||||
type ProposalBond = ProposalBond;
|
||||
type ProposalBondMinimum = ProposalBondMinimum;
|
||||
type SpendPeriod = SpendPeriod;
|
||||
type Burn = Burn;
|
||||
}
|
||||
type System = frame_system::Module<Test>;
|
||||
type Balances = pallet_balances::Module<Test>;
|
||||
type Treasury = Module<Test>;
|
||||
|
||||
fn new_test_ext() -> sp_io::TestExternalities {
|
||||
let mut t = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap();
|
||||
pallet_balances::GenesisConfig::<Test>{
|
||||
// Total issuance will be 200 with treasury account initialized at ED.
|
||||
balances: vec![(0, 100), (1, 98), (2, 1)],
|
||||
}.assimilate_storage(&mut t).unwrap();
|
||||
GenesisConfig::default().assimilate_storage::<Test>(&mut t).unwrap();
|
||||
t.into()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn genesis_config_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_eq!(Treasury::pot(), 0);
|
||||
assert_eq!(Treasury::proposal_count(), 0);
|
||||
});
|
||||
}
|
||||
|
||||
fn tip_hash() -> H256 {
|
||||
BlakeTwo256::hash_of(&(BlakeTwo256::hash(b"awesome.dot"), 3u64))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tip_new_cannot_be_used_twice() {
|
||||
new_test_ext().execute_with(|| {
|
||||
Balances::make_free_balance_be(&Treasury::account_id(), 101);
|
||||
assert_ok!(Treasury::tip_new(Origin::signed(10), b"awesome.dot".to_vec(), 3, 10));
|
||||
assert_noop!(
|
||||
Treasury::tip_new(Origin::signed(11), b"awesome.dot".to_vec(), 3, 10),
|
||||
Error::<Test>::AlreadyKnown
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn report_awesome_and_tip_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
Balances::make_free_balance_be(&Treasury::account_id(), 101);
|
||||
assert_ok!(Treasury::report_awesome(Origin::signed(0), b"awesome.dot".to_vec(), 3));
|
||||
assert_eq!(Balances::reserved_balance(0), 12);
|
||||
assert_eq!(Balances::free_balance(0), 88);
|
||||
|
||||
// other reports don't count.
|
||||
assert_noop!(
|
||||
Treasury::report_awesome(Origin::signed(1), b"awesome.dot".to_vec(), 3),
|
||||
Error::<Test>::AlreadyKnown
|
||||
);
|
||||
|
||||
let h = tip_hash();
|
||||
assert_ok!(Treasury::tip(Origin::signed(10), h.clone(), 10));
|
||||
assert_ok!(Treasury::tip(Origin::signed(11), h.clone(), 10));
|
||||
assert_ok!(Treasury::tip(Origin::signed(12), h.clone(), 10));
|
||||
assert_noop!(Treasury::tip(Origin::signed(9), h.clone(), 10), BadOrigin);
|
||||
System::set_block_number(2);
|
||||
assert_ok!(Treasury::close_tip(Origin::signed(100), h.into()));
|
||||
assert_eq!(Balances::reserved_balance(0), 0);
|
||||
assert_eq!(Balances::free_balance(0), 102);
|
||||
assert_eq!(Balances::free_balance(3), 8);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn report_awesome_from_beneficiary_and_tip_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
Balances::make_free_balance_be(&Treasury::account_id(), 101);
|
||||
assert_ok!(Treasury::report_awesome(Origin::signed(0), b"awesome.dot".to_vec(), 0));
|
||||
assert_eq!(Balances::reserved_balance(0), 12);
|
||||
assert_eq!(Balances::free_balance(0), 88);
|
||||
let h = BlakeTwo256::hash_of(&(BlakeTwo256::hash(b"awesome.dot"), 0u64));
|
||||
assert_ok!(Treasury::tip(Origin::signed(10), h.clone(), 10));
|
||||
assert_ok!(Treasury::tip(Origin::signed(11), h.clone(), 10));
|
||||
assert_ok!(Treasury::tip(Origin::signed(12), h.clone(), 10));
|
||||
System::set_block_number(2);
|
||||
assert_ok!(Treasury::close_tip(Origin::signed(100), h.into()));
|
||||
assert_eq!(Balances::reserved_balance(0), 0);
|
||||
assert_eq!(Balances::free_balance(0), 110);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn close_tip_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
Balances::make_free_balance_be(&Treasury::account_id(), 101);
|
||||
assert_eq!(Treasury::pot(), 100);
|
||||
|
||||
assert_ok!(Treasury::tip_new(Origin::signed(10), b"awesome.dot".to_vec(), 3, 10));
|
||||
let h = tip_hash();
|
||||
assert_ok!(Treasury::tip(Origin::signed(11), h.clone(), 10));
|
||||
assert_noop!(Treasury::close_tip(Origin::signed(0), h.into()), Error::<Test>::StillOpen);
|
||||
|
||||
assert_ok!(Treasury::tip(Origin::signed(12), h.clone(), 10));
|
||||
assert_noop!(Treasury::close_tip(Origin::signed(0), h.into()), Error::<Test>::Premature);
|
||||
|
||||
System::set_block_number(2);
|
||||
assert_noop!(Treasury::close_tip(Origin::NONE, h.into()), BadOrigin);
|
||||
assert_ok!(Treasury::close_tip(Origin::signed(0), h.into()));
|
||||
assert_eq!(Balances::free_balance(3), 10);
|
||||
|
||||
assert_noop!(Treasury::close_tip(Origin::signed(100), h.into()), Error::<Test>::UnknownTip);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn retract_tip_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
Balances::make_free_balance_be(&Treasury::account_id(), 101);
|
||||
assert_ok!(Treasury::report_awesome(Origin::signed(0), b"awesome.dot".to_vec(), 3));
|
||||
let h = tip_hash();
|
||||
assert_ok!(Treasury::tip(Origin::signed(10), h.clone(), 10));
|
||||
assert_ok!(Treasury::tip(Origin::signed(11), h.clone(), 10));
|
||||
assert_ok!(Treasury::tip(Origin::signed(12), h.clone(), 10));
|
||||
assert_noop!(Treasury::retract_tip(Origin::signed(10), h.clone()), Error::<Test>::NotFinder);
|
||||
assert_ok!(Treasury::retract_tip(Origin::signed(0), h.clone()));
|
||||
System::set_block_number(2);
|
||||
assert_noop!(Treasury::close_tip(Origin::signed(0), h.into()), Error::<Test>::UnknownTip);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tip_median_calculation_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
Balances::make_free_balance_be(&Treasury::account_id(), 101);
|
||||
assert_ok!(Treasury::tip_new(Origin::signed(10), b"awesome.dot".to_vec(), 3, 0));
|
||||
let h = tip_hash();
|
||||
assert_ok!(Treasury::tip(Origin::signed(11), h.clone(), 10));
|
||||
assert_ok!(Treasury::tip(Origin::signed(12), h.clone(), 1000000));
|
||||
System::set_block_number(2);
|
||||
assert_ok!(Treasury::close_tip(Origin::signed(0), h.into()));
|
||||
assert_eq!(Balances::free_balance(3), 10);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tip_changing_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
Balances::make_free_balance_be(&Treasury::account_id(), 101);
|
||||
assert_ok!(Treasury::tip_new(Origin::signed(10), b"awesome.dot".to_vec(), 3, 10000));
|
||||
let h = tip_hash();
|
||||
assert_ok!(Treasury::tip(Origin::signed(11), h.clone(), 10000));
|
||||
assert_ok!(Treasury::tip(Origin::signed(12), h.clone(), 10000));
|
||||
assert_ok!(Treasury::tip(Origin::signed(13), h.clone(), 0));
|
||||
assert_ok!(Treasury::tip(Origin::signed(14), h.clone(), 0));
|
||||
assert_ok!(Treasury::tip(Origin::signed(12), h.clone(), 1000));
|
||||
assert_ok!(Treasury::tip(Origin::signed(11), h.clone(), 100));
|
||||
assert_ok!(Treasury::tip(Origin::signed(10), h.clone(), 10));
|
||||
System::set_block_number(2);
|
||||
assert_ok!(Treasury::close_tip(Origin::signed(0), h.into()));
|
||||
assert_eq!(Balances::free_balance(3), 10);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minting_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
// Check that accumulate works when we have Some value in Dummy already.
|
||||
Balances::make_free_balance_be(&Treasury::account_id(), 101);
|
||||
assert_eq!(Treasury::pot(), 100);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn spend_proposal_takes_min_deposit() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_ok!(Treasury::propose_spend(Origin::signed(0), 1, 3));
|
||||
assert_eq!(Balances::free_balance(0), 99);
|
||||
assert_eq!(Balances::reserved_balance(0), 1);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn spend_proposal_takes_proportional_deposit() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_ok!(Treasury::propose_spend(Origin::signed(0), 100, 3));
|
||||
assert_eq!(Balances::free_balance(0), 95);
|
||||
assert_eq!(Balances::reserved_balance(0), 5);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn spend_proposal_fails_when_proposer_poor() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_noop!(
|
||||
Treasury::propose_spend(Origin::signed(2), 100, 3),
|
||||
Error::<Test>::InsufficientProposersBalance,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accepted_spend_proposal_ignored_outside_spend_period() {
|
||||
new_test_ext().execute_with(|| {
|
||||
Balances::make_free_balance_be(&Treasury::account_id(), 101);
|
||||
|
||||
assert_ok!(Treasury::propose_spend(Origin::signed(0), 100, 3));
|
||||
assert_ok!(Treasury::approve_proposal(Origin::ROOT, 0));
|
||||
|
||||
<Treasury as OnFinalize<u64>>::on_finalize(1);
|
||||
assert_eq!(Balances::free_balance(3), 0);
|
||||
assert_eq!(Treasury::pot(), 100);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unused_pot_should_diminish() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let init_total_issuance = Balances::total_issuance();
|
||||
Balances::make_free_balance_be(&Treasury::account_id(), 101);
|
||||
assert_eq!(Balances::total_issuance(), init_total_issuance + 100);
|
||||
|
||||
<Treasury as OnFinalize<u64>>::on_finalize(2);
|
||||
assert_eq!(Treasury::pot(), 50);
|
||||
assert_eq!(Balances::total_issuance(), init_total_issuance + 50);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejected_spend_proposal_ignored_on_spend_period() {
|
||||
new_test_ext().execute_with(|| {
|
||||
Balances::make_free_balance_be(&Treasury::account_id(), 101);
|
||||
|
||||
assert_ok!(Treasury::propose_spend(Origin::signed(0), 100, 3));
|
||||
assert_ok!(Treasury::reject_proposal(Origin::ROOT, 0));
|
||||
|
||||
<Treasury as OnFinalize<u64>>::on_finalize(2);
|
||||
assert_eq!(Balances::free_balance(3), 0);
|
||||
assert_eq!(Treasury::pot(), 50);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reject_already_rejected_spend_proposal_fails() {
|
||||
new_test_ext().execute_with(|| {
|
||||
Balances::make_free_balance_be(&Treasury::account_id(), 101);
|
||||
|
||||
assert_ok!(Treasury::propose_spend(Origin::signed(0), 100, 3));
|
||||
assert_ok!(Treasury::reject_proposal(Origin::ROOT, 0));
|
||||
assert_noop!(Treasury::reject_proposal(Origin::ROOT, 0), Error::<Test>::InvalidProposalIndex);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reject_non_existent_spend_proposal_fails() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_noop!(Treasury::reject_proposal(Origin::ROOT, 0), Error::<Test>::InvalidProposalIndex);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accept_non_existent_spend_proposal_fails() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_noop!(Treasury::approve_proposal(Origin::ROOT, 0), Error::<Test>::InvalidProposalIndex);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accept_already_rejected_spend_proposal_fails() {
|
||||
new_test_ext().execute_with(|| {
|
||||
Balances::make_free_balance_be(&Treasury::account_id(), 101);
|
||||
|
||||
assert_ok!(Treasury::propose_spend(Origin::signed(0), 100, 3));
|
||||
assert_ok!(Treasury::reject_proposal(Origin::ROOT, 0));
|
||||
assert_noop!(Treasury::approve_proposal(Origin::ROOT, 0), Error::<Test>::InvalidProposalIndex);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accepted_spend_proposal_enacted_on_spend_period() {
|
||||
new_test_ext().execute_with(|| {
|
||||
Balances::make_free_balance_be(&Treasury::account_id(), 101);
|
||||
assert_eq!(Treasury::pot(), 100);
|
||||
|
||||
assert_ok!(Treasury::propose_spend(Origin::signed(0), 100, 3));
|
||||
assert_ok!(Treasury::approve_proposal(Origin::ROOT, 0));
|
||||
|
||||
<Treasury as OnFinalize<u64>>::on_finalize(2);
|
||||
assert_eq!(Balances::free_balance(3), 100);
|
||||
assert_eq!(Treasury::pot(), 0);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pot_underflow_should_not_diminish() {
|
||||
new_test_ext().execute_with(|| {
|
||||
Balances::make_free_balance_be(&Treasury::account_id(), 101);
|
||||
assert_eq!(Treasury::pot(), 100);
|
||||
|
||||
assert_ok!(Treasury::propose_spend(Origin::signed(0), 150, 3));
|
||||
assert_ok!(Treasury::approve_proposal(Origin::ROOT, 0));
|
||||
|
||||
<Treasury as OnFinalize<u64>>::on_finalize(2);
|
||||
assert_eq!(Treasury::pot(), 100); // Pot hasn't changed
|
||||
|
||||
let _ = Balances::deposit_into_existing(&Treasury::account_id(), 100).unwrap();
|
||||
<Treasury as OnFinalize<u64>>::on_finalize(4);
|
||||
assert_eq!(Balances::free_balance(3), 150); // Fund has been spent
|
||||
assert_eq!(Treasury::pot(), 25); // Pot has finally changed
|
||||
});
|
||||
}
|
||||
|
||||
// Treasury account doesn't get deleted if amount approved to spend is all its free balance.
|
||||
// i.e. pot should not include existential deposit needed for account survival.
|
||||
#[test]
|
||||
fn treasury_account_doesnt_get_deleted() {
|
||||
new_test_ext().execute_with(|| {
|
||||
Balances::make_free_balance_be(&Treasury::account_id(), 101);
|
||||
assert_eq!(Treasury::pot(), 100);
|
||||
let treasury_balance = Balances::free_balance(&Treasury::account_id());
|
||||
|
||||
assert_ok!(Treasury::propose_spend(Origin::signed(0), treasury_balance, 3));
|
||||
assert_ok!(Treasury::approve_proposal(Origin::ROOT, 0));
|
||||
|
||||
<Treasury as OnFinalize<u64>>::on_finalize(2);
|
||||
assert_eq!(Treasury::pot(), 100); // Pot hasn't changed
|
||||
|
||||
assert_ok!(Treasury::propose_spend(Origin::signed(0), Treasury::pot(), 3));
|
||||
assert_ok!(Treasury::approve_proposal(Origin::ROOT, 1));
|
||||
|
||||
<Treasury as OnFinalize<u64>>::on_finalize(4);
|
||||
assert_eq!(Treasury::pot(), 0); // Pot is emptied
|
||||
assert_eq!(Balances::free_balance(Treasury::account_id()), 1); // but the account is still there
|
||||
});
|
||||
}
|
||||
|
||||
// In case treasury account is not existing then it works fine.
|
||||
// This is useful for chain that will just update runtime.
|
||||
#[test]
|
||||
fn inexistent_account_works() {
|
||||
let mut t = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap();
|
||||
pallet_balances::GenesisConfig::<Test>{
|
||||
balances: vec![(0, 100), (1, 99), (2, 1)],
|
||||
}.assimilate_storage(&mut t).unwrap();
|
||||
// Treasury genesis config is not build thus treasury account does not exist
|
||||
let mut t: sp_io::TestExternalities = t.into();
|
||||
|
||||
t.execute_with(|| {
|
||||
assert_eq!(Balances::free_balance(Treasury::account_id()), 0); // Account does not exist
|
||||
assert_eq!(Treasury::pot(), 0); // Pot is empty
|
||||
|
||||
assert_ok!(Treasury::propose_spend(Origin::signed(0), 99, 3));
|
||||
assert_ok!(Treasury::approve_proposal(Origin::ROOT, 0));
|
||||
assert_ok!(Treasury::propose_spend(Origin::signed(0), 1, 3));
|
||||
assert_ok!(Treasury::approve_proposal(Origin::ROOT, 1));
|
||||
<Treasury as OnFinalize<u64>>::on_finalize(2);
|
||||
assert_eq!(Treasury::pot(), 0); // Pot hasn't changed
|
||||
assert_eq!(Balances::free_balance(3), 0); // Balance of `3` hasn't changed
|
||||
|
||||
Balances::make_free_balance_be(&Treasury::account_id(), 100);
|
||||
assert_eq!(Treasury::pot(), 99); // Pot now contains funds
|
||||
assert_eq!(Balances::free_balance(Treasury::account_id()), 100); // Account does exist
|
||||
|
||||
<Treasury as OnFinalize<u64>>::on_finalize(4);
|
||||
|
||||
assert_eq!(Treasury::pot(), 0); // Pot has changed
|
||||
assert_eq!(Balances::free_balance(3), 99); // Balance of `3` has changed
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,449 @@
|
||||
use super::*;
|
||||
|
||||
use frame_support::{assert_noop, assert_ok, impl_outer_origin, parameter_types, weights::Weight};
|
||||
use frame_support::traits::Contains;
|
||||
use sp_core::H256;
|
||||
use sp_runtime::{
|
||||
Perbill,
|
||||
testing::Header,
|
||||
traits::{BlakeTwo256, OnFinalize, IdentityLookup, BadOrigin},
|
||||
};
|
||||
|
||||
impl_outer_origin! {
|
||||
pub enum Origin for Test where system = frame_system {}
|
||||
}
|
||||
|
||||
#[derive(Clone, Eq, PartialEq)]
|
||||
pub struct Test;
|
||||
parameter_types! {
|
||||
pub const BlockHashCount: u64 = 250;
|
||||
pub const MaximumBlockWeight: Weight = 1024;
|
||||
pub const MaximumBlockLength: u32 = 2 * 1024;
|
||||
pub const AvailableBlockRatio: Perbill = Perbill::one();
|
||||
}
|
||||
impl frame_system::Trait for Test {
|
||||
type Origin = Origin;
|
||||
type Index = u64;
|
||||
type BlockNumber = u64;
|
||||
type Call = ();
|
||||
type Hash = H256;
|
||||
type Hashing = BlakeTwo256;
|
||||
type AccountId = u64;
|
||||
type Lookup = IdentityLookup<Self::AccountId>;
|
||||
type Header = Header;
|
||||
type Event = ();
|
||||
type BlockHashCount = BlockHashCount;
|
||||
type MaximumBlockWeight = MaximumBlockWeight;
|
||||
type AvailableBlockRatio = AvailableBlockRatio;
|
||||
type MaximumBlockLength = MaximumBlockLength;
|
||||
type Version = ();
|
||||
type ModuleToIndex = ();
|
||||
type AccountData = pallet_balances::AccountData<u64>;
|
||||
type OnNewAccount = ();
|
||||
type OnKilledAccount = ();
|
||||
}
|
||||
parameter_types! {
|
||||
pub const ExistentialDeposit: u64 = 1;
|
||||
}
|
||||
impl pallet_balances::Trait for Test {
|
||||
type Balance = u64;
|
||||
type Event = ();
|
||||
type DustRemoval = ();
|
||||
type ExistentialDeposit = ExistentialDeposit;
|
||||
type AccountStore = System;
|
||||
}
|
||||
pub struct TenToFourteen;
|
||||
impl Contains<u64> for TenToFourteen {
|
||||
fn contains(n: &u64) -> bool {
|
||||
*n >= 10 && *n <= 14
|
||||
}
|
||||
fn sorted_members() -> Vec<u64> {
|
||||
vec![10, 11, 12, 13, 14]
|
||||
}
|
||||
}
|
||||
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);
|
||||
pub const TipCountdown: u64 = 1;
|
||||
pub const TipFindersFee: Percent = Percent::from_percent(20);
|
||||
pub const TipReportDepositBase: u64 = 1;
|
||||
pub const TipReportDepositPerByte: u64 = 1;
|
||||
}
|
||||
impl Trait for Test {
|
||||
type Currency = pallet_balances::Module<Test>;
|
||||
type ApproveOrigin = frame_system::EnsureRoot<u64>;
|
||||
type RejectOrigin = frame_system::EnsureRoot<u64>;
|
||||
type Tippers = TenToFourteen;
|
||||
type TipCountdown = TipCountdown;
|
||||
type TipFindersFee = TipFindersFee;
|
||||
type TipReportDepositBase = TipReportDepositBase;
|
||||
type TipReportDepositPerByte = TipReportDepositPerByte;
|
||||
type Event = ();
|
||||
type ProposalRejection = ();
|
||||
type ProposalBond = ProposalBond;
|
||||
type ProposalBondMinimum = ProposalBondMinimum;
|
||||
type SpendPeriod = SpendPeriod;
|
||||
type Burn = Burn;
|
||||
}
|
||||
type System = frame_system::Module<Test>;
|
||||
type Balances = pallet_balances::Module<Test>;
|
||||
type Treasury = Module<Test>;
|
||||
|
||||
fn new_test_ext() -> sp_io::TestExternalities {
|
||||
let mut t = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap();
|
||||
pallet_balances::GenesisConfig::<Test>{
|
||||
// Total issuance will be 200 with treasury account initialized at ED.
|
||||
balances: vec![(0, 100), (1, 98), (2, 1)],
|
||||
}.assimilate_storage(&mut t).unwrap();
|
||||
GenesisConfig::default().assimilate_storage::<Test>(&mut t).unwrap();
|
||||
t.into()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn genesis_config_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_eq!(Treasury::pot(), 0);
|
||||
assert_eq!(Treasury::proposal_count(), 0);
|
||||
});
|
||||
}
|
||||
|
||||
fn tip_hash() -> H256 {
|
||||
BlakeTwo256::hash_of(&(BlakeTwo256::hash(b"awesome.dot"), 3u64))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tip_new_cannot_be_used_twice() {
|
||||
new_test_ext().execute_with(|| {
|
||||
Balances::make_free_balance_be(&Treasury::account_id(), 101);
|
||||
assert_ok!(Treasury::tip_new(Origin::signed(10), b"awesome.dot".to_vec(), 3, 10));
|
||||
assert_noop!(
|
||||
Treasury::tip_new(Origin::signed(11), b"awesome.dot".to_vec(), 3, 10),
|
||||
Error::<Test>::AlreadyKnown
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn report_awesome_and_tip_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
Balances::make_free_balance_be(&Treasury::account_id(), 101);
|
||||
assert_ok!(Treasury::report_awesome(Origin::signed(0), b"awesome.dot".to_vec(), 3));
|
||||
assert_eq!(Balances::reserved_balance(0), 12);
|
||||
assert_eq!(Balances::free_balance(0), 88);
|
||||
|
||||
// other reports don't count.
|
||||
assert_noop!(
|
||||
Treasury::report_awesome(Origin::signed(1), b"awesome.dot".to_vec(), 3),
|
||||
Error::<Test>::AlreadyKnown
|
||||
);
|
||||
|
||||
let h = tip_hash();
|
||||
assert_ok!(Treasury::tip(Origin::signed(10), h.clone(), 10));
|
||||
assert_ok!(Treasury::tip(Origin::signed(11), h.clone(), 10));
|
||||
assert_ok!(Treasury::tip(Origin::signed(12), h.clone(), 10));
|
||||
assert_noop!(Treasury::tip(Origin::signed(9), h.clone(), 10), BadOrigin);
|
||||
System::set_block_number(2);
|
||||
assert_ok!(Treasury::close_tip(Origin::signed(100), h.into()));
|
||||
assert_eq!(Balances::reserved_balance(0), 0);
|
||||
assert_eq!(Balances::free_balance(0), 102);
|
||||
assert_eq!(Balances::free_balance(3), 8);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn report_awesome_from_beneficiary_and_tip_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
Balances::make_free_balance_be(&Treasury::account_id(), 101);
|
||||
assert_ok!(Treasury::report_awesome(Origin::signed(0), b"awesome.dot".to_vec(), 0));
|
||||
assert_eq!(Balances::reserved_balance(0), 12);
|
||||
assert_eq!(Balances::free_balance(0), 88);
|
||||
let h = BlakeTwo256::hash_of(&(BlakeTwo256::hash(b"awesome.dot"), 0u64));
|
||||
assert_ok!(Treasury::tip(Origin::signed(10), h.clone(), 10));
|
||||
assert_ok!(Treasury::tip(Origin::signed(11), h.clone(), 10));
|
||||
assert_ok!(Treasury::tip(Origin::signed(12), h.clone(), 10));
|
||||
System::set_block_number(2);
|
||||
assert_ok!(Treasury::close_tip(Origin::signed(100), h.into()));
|
||||
assert_eq!(Balances::reserved_balance(0), 0);
|
||||
assert_eq!(Balances::free_balance(0), 110);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn close_tip_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
Balances::make_free_balance_be(&Treasury::account_id(), 101);
|
||||
assert_eq!(Treasury::pot(), 100);
|
||||
|
||||
assert_ok!(Treasury::tip_new(Origin::signed(10), b"awesome.dot".to_vec(), 3, 10));
|
||||
let h = tip_hash();
|
||||
assert_ok!(Treasury::tip(Origin::signed(11), h.clone(), 10));
|
||||
assert_noop!(Treasury::close_tip(Origin::signed(0), h.into()), Error::<Test>::StillOpen);
|
||||
|
||||
assert_ok!(Treasury::tip(Origin::signed(12), h.clone(), 10));
|
||||
assert_noop!(Treasury::close_tip(Origin::signed(0), h.into()), Error::<Test>::Premature);
|
||||
|
||||
System::set_block_number(2);
|
||||
assert_noop!(Treasury::close_tip(Origin::NONE, h.into()), BadOrigin);
|
||||
assert_ok!(Treasury::close_tip(Origin::signed(0), h.into()));
|
||||
assert_eq!(Balances::free_balance(3), 10);
|
||||
|
||||
assert_noop!(Treasury::close_tip(Origin::signed(100), h.into()), Error::<Test>::UnknownTip);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn retract_tip_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
Balances::make_free_balance_be(&Treasury::account_id(), 101);
|
||||
assert_ok!(Treasury::report_awesome(Origin::signed(0), b"awesome.dot".to_vec(), 3));
|
||||
let h = tip_hash();
|
||||
assert_ok!(Treasury::tip(Origin::signed(10), h.clone(), 10));
|
||||
assert_ok!(Treasury::tip(Origin::signed(11), h.clone(), 10));
|
||||
assert_ok!(Treasury::tip(Origin::signed(12), h.clone(), 10));
|
||||
assert_noop!(Treasury::retract_tip(Origin::signed(10), h.clone()), Error::<Test>::NotFinder);
|
||||
assert_ok!(Treasury::retract_tip(Origin::signed(0), h.clone()));
|
||||
System::set_block_number(2);
|
||||
assert_noop!(Treasury::close_tip(Origin::signed(0), h.into()), Error::<Test>::UnknownTip);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tip_median_calculation_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
Balances::make_free_balance_be(&Treasury::account_id(), 101);
|
||||
assert_ok!(Treasury::tip_new(Origin::signed(10), b"awesome.dot".to_vec(), 3, 0));
|
||||
let h = tip_hash();
|
||||
assert_ok!(Treasury::tip(Origin::signed(11), h.clone(), 10));
|
||||
assert_ok!(Treasury::tip(Origin::signed(12), h.clone(), 1000000));
|
||||
System::set_block_number(2);
|
||||
assert_ok!(Treasury::close_tip(Origin::signed(0), h.into()));
|
||||
assert_eq!(Balances::free_balance(3), 10);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tip_changing_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
Balances::make_free_balance_be(&Treasury::account_id(), 101);
|
||||
assert_ok!(Treasury::tip_new(Origin::signed(10), b"awesome.dot".to_vec(), 3, 10000));
|
||||
let h = tip_hash();
|
||||
assert_ok!(Treasury::tip(Origin::signed(11), h.clone(), 10000));
|
||||
assert_ok!(Treasury::tip(Origin::signed(12), h.clone(), 10000));
|
||||
assert_ok!(Treasury::tip(Origin::signed(13), h.clone(), 0));
|
||||
assert_ok!(Treasury::tip(Origin::signed(14), h.clone(), 0));
|
||||
assert_ok!(Treasury::tip(Origin::signed(12), h.clone(), 1000));
|
||||
assert_ok!(Treasury::tip(Origin::signed(11), h.clone(), 100));
|
||||
assert_ok!(Treasury::tip(Origin::signed(10), h.clone(), 10));
|
||||
System::set_block_number(2);
|
||||
assert_ok!(Treasury::close_tip(Origin::signed(0), h.into()));
|
||||
assert_eq!(Balances::free_balance(3), 10);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn minting_works() {
|
||||
new_test_ext().execute_with(|| {
|
||||
// Check that accumulate works when we have Some value in Dummy already.
|
||||
Balances::make_free_balance_be(&Treasury::account_id(), 101);
|
||||
assert_eq!(Treasury::pot(), 100);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn spend_proposal_takes_min_deposit() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_ok!(Treasury::propose_spend(Origin::signed(0), 1, 3));
|
||||
assert_eq!(Balances::free_balance(0), 99);
|
||||
assert_eq!(Balances::reserved_balance(0), 1);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn spend_proposal_takes_proportional_deposit() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_ok!(Treasury::propose_spend(Origin::signed(0), 100, 3));
|
||||
assert_eq!(Balances::free_balance(0), 95);
|
||||
assert_eq!(Balances::reserved_balance(0), 5);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn spend_proposal_fails_when_proposer_poor() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_noop!(
|
||||
Treasury::propose_spend(Origin::signed(2), 100, 3),
|
||||
Error::<Test>::InsufficientProposersBalance,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accepted_spend_proposal_ignored_outside_spend_period() {
|
||||
new_test_ext().execute_with(|| {
|
||||
Balances::make_free_balance_be(&Treasury::account_id(), 101);
|
||||
|
||||
assert_ok!(Treasury::propose_spend(Origin::signed(0), 100, 3));
|
||||
assert_ok!(Treasury::approve_proposal(Origin::ROOT, 0));
|
||||
|
||||
<Treasury as OnFinalize<u64>>::on_finalize(1);
|
||||
assert_eq!(Balances::free_balance(3), 0);
|
||||
assert_eq!(Treasury::pot(), 100);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unused_pot_should_diminish() {
|
||||
new_test_ext().execute_with(|| {
|
||||
let init_total_issuance = Balances::total_issuance();
|
||||
Balances::make_free_balance_be(&Treasury::account_id(), 101);
|
||||
assert_eq!(Balances::total_issuance(), init_total_issuance + 100);
|
||||
|
||||
<Treasury as OnFinalize<u64>>::on_finalize(2);
|
||||
assert_eq!(Treasury::pot(), 50);
|
||||
assert_eq!(Balances::total_issuance(), init_total_issuance + 50);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rejected_spend_proposal_ignored_on_spend_period() {
|
||||
new_test_ext().execute_with(|| {
|
||||
Balances::make_free_balance_be(&Treasury::account_id(), 101);
|
||||
|
||||
assert_ok!(Treasury::propose_spend(Origin::signed(0), 100, 3));
|
||||
assert_ok!(Treasury::reject_proposal(Origin::ROOT, 0));
|
||||
|
||||
<Treasury as OnFinalize<u64>>::on_finalize(2);
|
||||
assert_eq!(Balances::free_balance(3), 0);
|
||||
assert_eq!(Treasury::pot(), 50);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reject_already_rejected_spend_proposal_fails() {
|
||||
new_test_ext().execute_with(|| {
|
||||
Balances::make_free_balance_be(&Treasury::account_id(), 101);
|
||||
|
||||
assert_ok!(Treasury::propose_spend(Origin::signed(0), 100, 3));
|
||||
assert_ok!(Treasury::reject_proposal(Origin::ROOT, 0));
|
||||
assert_noop!(Treasury::reject_proposal(Origin::ROOT, 0), Error::<Test>::InvalidProposalIndex);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn reject_non_existent_spend_proposal_fails() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_noop!(Treasury::reject_proposal(Origin::ROOT, 0), Error::<Test>::InvalidProposalIndex);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accept_non_existent_spend_proposal_fails() {
|
||||
new_test_ext().execute_with(|| {
|
||||
assert_noop!(Treasury::approve_proposal(Origin::ROOT, 0), Error::<Test>::InvalidProposalIndex);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accept_already_rejected_spend_proposal_fails() {
|
||||
new_test_ext().execute_with(|| {
|
||||
Balances::make_free_balance_be(&Treasury::account_id(), 101);
|
||||
|
||||
assert_ok!(Treasury::propose_spend(Origin::signed(0), 100, 3));
|
||||
assert_ok!(Treasury::reject_proposal(Origin::ROOT, 0));
|
||||
assert_noop!(Treasury::approve_proposal(Origin::ROOT, 0), Error::<Test>::InvalidProposalIndex);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accepted_spend_proposal_enacted_on_spend_period() {
|
||||
new_test_ext().execute_with(|| {
|
||||
Balances::make_free_balance_be(&Treasury::account_id(), 101);
|
||||
assert_eq!(Treasury::pot(), 100);
|
||||
|
||||
assert_ok!(Treasury::propose_spend(Origin::signed(0), 100, 3));
|
||||
assert_ok!(Treasury::approve_proposal(Origin::ROOT, 0));
|
||||
|
||||
<Treasury as OnFinalize<u64>>::on_finalize(2);
|
||||
assert_eq!(Balances::free_balance(3), 100);
|
||||
assert_eq!(Treasury::pot(), 0);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pot_underflow_should_not_diminish() {
|
||||
new_test_ext().execute_with(|| {
|
||||
Balances::make_free_balance_be(&Treasury::account_id(), 101);
|
||||
assert_eq!(Treasury::pot(), 100);
|
||||
|
||||
assert_ok!(Treasury::propose_spend(Origin::signed(0), 150, 3));
|
||||
assert_ok!(Treasury::approve_proposal(Origin::ROOT, 0));
|
||||
|
||||
<Treasury as OnFinalize<u64>>::on_finalize(2);
|
||||
assert_eq!(Treasury::pot(), 100); // Pot hasn't changed
|
||||
|
||||
let _ = Balances::deposit_into_existing(&Treasury::account_id(), 100).unwrap();
|
||||
<Treasury as OnFinalize<u64>>::on_finalize(4);
|
||||
assert_eq!(Balances::free_balance(3), 150); // Fund has been spent
|
||||
assert_eq!(Treasury::pot(), 25); // Pot has finally changed
|
||||
});
|
||||
}
|
||||
|
||||
// Treasury account doesn't get deleted if amount approved to spend is all its free balance.
|
||||
// i.e. pot should not include existential deposit needed for account survival.
|
||||
#[test]
|
||||
fn treasury_account_doesnt_get_deleted() {
|
||||
new_test_ext().execute_with(|| {
|
||||
Balances::make_free_balance_be(&Treasury::account_id(), 101);
|
||||
assert_eq!(Treasury::pot(), 100);
|
||||
let treasury_balance = Balances::free_balance(&Treasury::account_id());
|
||||
|
||||
assert_ok!(Treasury::propose_spend(Origin::signed(0), treasury_balance, 3));
|
||||
assert_ok!(Treasury::approve_proposal(Origin::ROOT, 0));
|
||||
|
||||
<Treasury as OnFinalize<u64>>::on_finalize(2);
|
||||
assert_eq!(Treasury::pot(), 100); // Pot hasn't changed
|
||||
|
||||
assert_ok!(Treasury::propose_spend(Origin::signed(0), Treasury::pot(), 3));
|
||||
assert_ok!(Treasury::approve_proposal(Origin::ROOT, 1));
|
||||
|
||||
<Treasury as OnFinalize<u64>>::on_finalize(4);
|
||||
assert_eq!(Treasury::pot(), 0); // Pot is emptied
|
||||
assert_eq!(Balances::free_balance(Treasury::account_id()), 1); // but the account is still there
|
||||
});
|
||||
}
|
||||
|
||||
// In case treasury account is not existing then it works fine.
|
||||
// This is useful for chain that will just update runtime.
|
||||
#[test]
|
||||
fn inexistent_account_works() {
|
||||
let mut t = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap();
|
||||
pallet_balances::GenesisConfig::<Test>{
|
||||
balances: vec![(0, 100), (1, 99), (2, 1)],
|
||||
}.assimilate_storage(&mut t).unwrap();
|
||||
// Treasury genesis config is not build thus treasury account does not exist
|
||||
let mut t: sp_io::TestExternalities = t.into();
|
||||
|
||||
t.execute_with(|| {
|
||||
assert_eq!(Balances::free_balance(Treasury::account_id()), 0); // Account does not exist
|
||||
assert_eq!(Treasury::pot(), 0); // Pot is empty
|
||||
|
||||
assert_ok!(Treasury::propose_spend(Origin::signed(0), 99, 3));
|
||||
assert_ok!(Treasury::approve_proposal(Origin::ROOT, 0));
|
||||
assert_ok!(Treasury::propose_spend(Origin::signed(0), 1, 3));
|
||||
assert_ok!(Treasury::approve_proposal(Origin::ROOT, 1));
|
||||
<Treasury as OnFinalize<u64>>::on_finalize(2);
|
||||
assert_eq!(Treasury::pot(), 0); // Pot hasn't changed
|
||||
assert_eq!(Balances::free_balance(3), 0); // Balance of `3` hasn't changed
|
||||
|
||||
Balances::make_free_balance_be(&Treasury::account_id(), 100);
|
||||
assert_eq!(Treasury::pot(), 99); // Pot now contains funds
|
||||
assert_eq!(Balances::free_balance(Treasury::account_id()), 100); // Account does exist
|
||||
|
||||
<Treasury as OnFinalize<u64>>::on_finalize(4);
|
||||
|
||||
assert_eq!(Treasury::pot(), 0); // Pot has changed
|
||||
assert_eq!(Balances::free_balance(3), 99); // Balance of `3` has changed
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user