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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#![cfg(feature = "runtime-benchmarks")]
use super::*;
use frame_benchmarking::{account, benchmarks, impl_benchmark_test_suite, whitelisted_caller};
use frame_support::traits::OnInitialize;
use frame_system::RawOrigin;
use sp_runtime::traits::Bounded;
use crate::Module as Bounties;
use pallet_treasury::Pallet as Treasury;
const SEED: u32 = 0;
fn create_approved_bounties<T: Config>(n: u32) -> Result<(), &'static str> {
for i in 0..n {
let (caller, _curator, _fee, value, reason) = setup_bounty::<T>(i, MAX_BYTES);
Bounties::<T>::propose_bounty(RawOrigin::Signed(caller).into(), value, reason)?;
let bounty_id = BountyCount::get() - 1;
Bounties::<T>::approve_bounty(RawOrigin::Root.into(), bounty_id)?;
}
ensure!(BountyApprovals::get().len() == n as usize, "Not all bounty approved");
Ok(())
}
fn setup_bounty<T: Config>(
u: u32,
d: u32,
) -> (T::AccountId, T::AccountId, BalanceOf<T>, BalanceOf<T>, Vec<u8>) {
let caller = account("caller", u, SEED);
let value: BalanceOf<T> = T::BountyValueMinimum::get().saturating_mul(100u32.into());
let fee = value / 2u32.into();
let deposit = T::BountyDepositBase::get() + T::DataDepositPerByte::get() * MAX_BYTES.into();
let _ = T::Currency::make_free_balance_be(&caller, deposit);
let curator = account("curator", u, SEED);
let _ = T::Currency::make_free_balance_be(&curator, fee / 2u32.into());
let reason = vec![0; d as usize];
(caller, curator, fee, value, reason)
}
fn create_bounty<T: Config>(
) -> Result<(<T::Lookup as StaticLookup>::Source, BountyIndex), &'static str> {
let (caller, curator, fee, value, reason) = setup_bounty::<T>(0, MAX_BYTES);
let curator_lookup = T::Lookup::unlookup(curator.clone());
Bounties::<T>::propose_bounty(RawOrigin::Signed(caller).into(), value, reason)?;
let bounty_id = BountyCount::get() - 1;
Bounties::<T>::approve_bounty(RawOrigin::Root.into(), bounty_id)?;
Treasury::<T>::on_initialize(T::BlockNumber::zero());
Bounties::<T>::propose_curator(RawOrigin::Root.into(), bounty_id, curator_lookup.clone(), fee)?;
Bounties::<T>::accept_curator(RawOrigin::Signed(curator).into(), bounty_id)?;
Ok((curator_lookup, bounty_id))
}
fn setup_pot_account<T: Config>() {
let pot_account = Bounties::<T>::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>(generic_event: <T as Config>::Event) {
frame_system::Pallet::<T>::assert_last_event(generic_event.into());
}
const MAX_BYTES: u32 = 16384;
benchmarks! {
propose_bounty {
let d in 0 .. MAX_BYTES;
let (caller, curator, fee, value, description) = setup_bounty::<T>(0, d);
}: _(RawOrigin::Signed(caller), value, description)
approve_bounty {
let (caller, curator, fee, value, reason) = setup_bounty::<T>(0, MAX_BYTES);
Bounties::<T>::propose_bounty(RawOrigin::Signed(caller).into(), value, reason)?;
let bounty_id = BountyCount::get() - 1;
}: _(RawOrigin::Root, bounty_id)
propose_curator {
setup_pot_account::<T>();
let (caller, curator, fee, value, reason) = setup_bounty::<T>(0, MAX_BYTES);
let curator_lookup = T::Lookup::unlookup(curator.clone());
Bounties::<T>::propose_bounty(RawOrigin::Signed(caller).into(), value, reason)?;
let bounty_id = BountyCount::get() - 1;
Bounties::<T>::approve_bounty(RawOrigin::Root.into(), bounty_id)?;
Bounties::<T>::on_initialize(T::BlockNumber::zero());
}: _(RawOrigin::Root, bounty_id, curator_lookup, fee)
unassign_curator {
setup_pot_account::<T>();
let (curator_lookup, bounty_id) = create_bounty::<T>()?;
Bounties::<T>::on_initialize(T::BlockNumber::zero());
let bounty_id = BountyCount::get() - 1;
frame_system::Pallet::<T>::set_block_number(T::BountyUpdatePeriod::get() + 1u32.into());
let caller = whitelisted_caller();
}: _(RawOrigin::Signed(caller), bounty_id)
accept_curator {
setup_pot_account::<T>();
let (caller, curator, fee, value, reason) = setup_bounty::<T>(0, MAX_BYTES);
let curator_lookup = T::Lookup::unlookup(curator.clone());
Bounties::<T>::propose_bounty(RawOrigin::Signed(caller).into(), value, reason)?;
let bounty_id = BountyCount::get() - 1;
Bounties::<T>::approve_bounty(RawOrigin::Root.into(), bounty_id)?;
Bounties::<T>::on_initialize(T::BlockNumber::zero());
Bounties::<T>::propose_curator(RawOrigin::Root.into(), bounty_id, curator_lookup, fee)?;
}: _(RawOrigin::Signed(curator), bounty_id)
award_bounty {
setup_pot_account::<T>();
let (curator_lookup, bounty_id) = create_bounty::<T>()?;
Bounties::<T>::on_initialize(T::BlockNumber::zero());
let bounty_id = BountyCount::get() - 1;
let curator = T::Lookup::lookup(curator_lookup)?;
let beneficiary = T::Lookup::unlookup(account("beneficiary", 0, SEED));
}: _(RawOrigin::Signed(curator), bounty_id, beneficiary)
claim_bounty {
setup_pot_account::<T>();
let (curator_lookup, bounty_id) = create_bounty::<T>()?;
Bounties::<T>::on_initialize(T::BlockNumber::zero());
let bounty_id = BountyCount::get() - 1;
let curator = T::Lookup::lookup(curator_lookup)?;
let beneficiary_account: T::AccountId = account("beneficiary", 0, SEED);
let beneficiary = T::Lookup::unlookup(beneficiary_account.clone());
Bounties::<T>::award_bounty(RawOrigin::Signed(curator.clone()).into(), bounty_id, beneficiary)?;
frame_system::Pallet::<T>::set_block_number(T::BountyDepositPayoutDelay::get());
ensure!(T::Currency::free_balance(&beneficiary_account).is_zero(), "Beneficiary already has balance");
}: _(RawOrigin::Signed(curator), bounty_id)
verify {
ensure!(!T::Currency::free_balance(&beneficiary_account).is_zero(), "Beneficiary didn't get paid");
}
close_bounty_proposed {
setup_pot_account::<T>();
let (caller, curator, fee, value, reason) = setup_bounty::<T>(0, 0);
Bounties::<T>::propose_bounty(RawOrigin::Signed(caller).into(), value, reason)?;
let bounty_id = BountyCount::get() - 1;
}: close_bounty(RawOrigin::Root, bounty_id)
close_bounty_active {
setup_pot_account::<T>();
let (curator_lookup, bounty_id) = create_bounty::<T>()?;
Bounties::<T>::on_initialize(T::BlockNumber::zero());
let bounty_id = BountyCount::get() - 1;
}: close_bounty(RawOrigin::Root, bounty_id)
verify {
assert_last_event::<T>(RawEvent::BountyCanceled(bounty_id).into())
}
extend_bounty_expiry {
setup_pot_account::<T>();
let (curator_lookup, bounty_id) = create_bounty::<T>()?;
Bounties::<T>::on_initialize(T::BlockNumber::zero());
let bounty_id = BountyCount::get() - 1;
let curator = T::Lookup::lookup(curator_lookup)?;
}: _(RawOrigin::Signed(curator), bounty_id, Vec::new())
verify {
assert_last_event::<T>(RawEvent::BountyExtended(bounty_id).into())
}
spend_funds {
let b in 1 .. 100;
setup_pot_account::<T>();
create_approved_bounties::<T>(b)?;
let mut budget_remaining = BalanceOf::<T>::max_value();
let mut imbalance = PositiveImbalanceOf::<T>::zero();
let mut total_weight = Weight::zero();
let mut missed_any = false;
}: {
<Bounties<T> as pallet_treasury::SpendFunds<T>>::spend_funds(
&mut budget_remaining,
&mut imbalance,
&mut total_weight,
&mut missed_any,
);
}
verify {
ensure!(budget_remaining < BalanceOf::<T>::max_value(), "Budget not used");
ensure!(missed_any == false, "Missed some");
assert_last_event::<T>(RawEvent::BountyBecameActive(b - 1).into())
}
}
impl_benchmark_test_suite!(Bounties, crate::tests::new_test_ext(), crate::tests::Test);