1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#![cfg(feature = "runtime-benchmarks")]
use super::{Pallet as Treasury, *};
use frame_benchmarking::{account, benchmarks_instance_pallet};
use frame_support::{
dispatch::UnfilteredDispatchable,
ensure,
traits::{EnsureOrigin, OnInitialize},
};
use frame_system::RawOrigin;
const SEED: u32 = 0;
fn setup_proposal<T: Config<I>, I: 'static>(
u: u32,
) -> (T::AccountId, BalanceOf<T, I>, AccountIdLookupOf<T>) {
let caller = account("caller", u, SEED);
let value: BalanceOf<T, I> = T::ProposalBondMinimum::get().saturating_mul(100u32.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)
}
fn create_approved_proposals<T: Config<I>, I: 'static>(n: u32) -> Result<(), &'static str> {
for i in 0..n {
let (caller, value, lookup) = setup_proposal::<T, I>(i);
Treasury::<T, I>::propose_spend(RawOrigin::Signed(caller).into(), value, lookup)?;
let proposal_id = <ProposalCount<T, I>>::get() - 1;
Treasury::<T, I>::approve_proposal(RawOrigin::Root.into(), proposal_id)?;
}
ensure!(<Approvals<T, I>>::get().len() == n as usize, "Not all approved");
Ok(())
}
fn setup_pot_account<T: Config<I>, I: 'static>() {
let pot_account = Treasury::<T, I>::account_id();
let value = T::Currency::minimum_balance().saturating_mul(1_000_000_000u32.into());
let _ = T::Currency::make_free_balance_be(&pot_account, value);
}
fn assert_last_event<T: Config<I>, I: 'static>(generic_event: <T as Config<I>>::Event) {
frame_system::Pallet::<T>::assert_last_event(generic_event.into());
}
benchmarks_instance_pallet! {
spend {
let (_, value, beneficiary_lookup) = setup_proposal::<T, _>(SEED);
let origin = T::SpendOrigin::try_successful_origin();
let beneficiary = T::Lookup::lookup(beneficiary_lookup.clone()).unwrap();
let call = Call::<T, I>::spend { amount: value, beneficiary: beneficiary_lookup };
}: {
if let Ok(origin) = origin.clone() {
call.dispatch_bypass_filter(origin)?;
}
}
verify {
if origin.is_ok() {
assert_last_event::<T, I>(Event::SpendApproved { proposal_index: 0, amount: value, beneficiary }.into())
}
}
propose_spend {
let (caller, value, beneficiary_lookup) = setup_proposal::<T, _>(SEED);
let caller_key = frame_system::Account::<T>::hashed_key_for(&caller);
frame_benchmarking::benchmarking::add_to_whitelist(caller_key.into());
}: _(RawOrigin::Signed(caller), value, beneficiary_lookup)
reject_proposal {
let (caller, value, beneficiary_lookup) = setup_proposal::<T, _>(SEED);
Treasury::<T, _>::propose_spend(
RawOrigin::Signed(caller).into(),
value,
beneficiary_lookup
)?;
let proposal_id = Treasury::<T, _>::proposal_count() - 1;
let reject_origin = T::RejectOrigin::successful_origin();
}: _<T::Origin>(reject_origin, proposal_id)
approve_proposal {
let p in 0 .. T::MaxApprovals::get() - 1;
create_approved_proposals::<T, _>(p)?;
let (caller, value, beneficiary_lookup) = setup_proposal::<T, _>(SEED);
Treasury::<T, _>::propose_spend(
RawOrigin::Signed(caller).into(),
value,
beneficiary_lookup
)?;
let proposal_id = Treasury::<T, _>::proposal_count() - 1;
let approve_origin = T::ApproveOrigin::successful_origin();
}: _<T::Origin>(approve_origin, proposal_id)
remove_approval {
let (caller, value, beneficiary_lookup) = setup_proposal::<T, _>(SEED);
Treasury::<T, _>::propose_spend(
RawOrigin::Signed(caller).into(),
value,
beneficiary_lookup
)?;
let proposal_id = Treasury::<T, _>::proposal_count() - 1;
Treasury::<T, I>::approve_proposal(RawOrigin::Root.into(), proposal_id)?;
let reject_origin = T::RejectOrigin::successful_origin();
}: _<T::Origin>(reject_origin, proposal_id)
on_initialize_proposals {
let p in 0 .. T::MaxApprovals::get();
setup_pot_account::<T, _>();
create_approved_proposals::<T, _>(p)?;
}: {
Treasury::<T, _>::on_initialize(T::BlockNumber::zero());
}
impl_benchmark_test_suite!(Treasury, crate::tests::new_test_ext(), crate::tests::Test);
}