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
#![cfg(feature = "runtime-benchmarks")]
use super::*;
use frame_benchmarking::{account, benchmarks, impl_benchmark_test_suite, whitelisted_caller};
use frame_system::RawOrigin;
use sp_runtime::traits::Saturating;
use crate::Module as TipsMod;
const SEED: u32 = 0;
fn setup_awesome<T: Config>(length: u32) -> (T::AccountId, Vec<u8>, T::AccountId) {
let caller = whitelisted_caller();
let value = T::TipReportDepositBase::get() +
T::DataDepositPerByte::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)
}
fn setup_tip<T: Config>(
r: u32,
t: u32,
) -> Result<(T::AccountId, Vec<u8>, T::AccountId, BalanceOf<T>), &'static str> {
let tippers_count = T::Tippers::count();
for i in 0..t {
let member = account("member", i, SEED);
T::Tippers::add(&member);
ensure!(T::Tippers::contains(&member), "failed to add tipper");
}
ensure!(T::Tippers::count() == 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(100u32.into());
Ok((caller, reason, beneficiary, value))
}
fn create_tips<T: Config>(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");
TipsMod::<T>::tip(RawOrigin::Signed(caller).into(), hash, value)?;
}
Tips::<T>::mutate(hash, |maybe_tip| {
if let Some(open_tip) = maybe_tip {
open_tip.closes = Some(T::BlockNumber::zero());
}
});
Ok(())
}
fn setup_pot_account<T: Config>() {
let pot_account = TipsMod::<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);
}
const MAX_BYTES: u32 = 16384;
const MAX_TIPPERS: u32 = 100;
benchmarks! {
report_awesome {
let r in 0 .. MAX_BYTES;
let (caller, reason, awesome_person) = setup_awesome::<T>(r);
let caller_key = frame_system::Account::<T>::hashed_key_for(&caller);
frame_benchmarking::benchmarking::add_to_whitelist(caller_key.into());
}: _(RawOrigin::Signed(caller), reason, awesome_person)
retract_tip {
let r = MAX_BYTES;
let (caller, reason, awesome_person) = setup_awesome::<T>(r);
TipsMod::<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));
let caller_key = frame_system::Account::<T>::hashed_key_for(&caller);
frame_benchmarking::benchmarking::add_to_whitelist(caller_key.into());
}: _(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)?;
let caller_key = frame_system::Account::<T>::hashed_key_for(&caller);
frame_benchmarking::benchmarking::add_to_whitelist(caller_key.into());
}: _(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(100u32.into());
TipsMod::<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);
let caller_key = frame_system::Account::<T>::hashed_key_for(&caller);
frame_benchmarking::benchmarking::add_to_whitelist(caller_key.into());
}: _(RawOrigin::Signed(caller), hash, value)
close_tip {
let t in 1 .. MAX_TIPPERS;
setup_pot_account::<T>();
let (member, reason, beneficiary, value) = setup_tip::<T>(0, t)?;
let value = T::Currency::minimum_balance().saturating_mul(100u32.into());
TipsMod::<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, hash.clone(), value)?;
let caller = account("caller", 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), hash)
slash_tip {
let t in 1 .. MAX_TIPPERS;
setup_pot_account::<T>();
let (member, reason, beneficiary, value) = setup_tip::<T>(0, t)?;
let value = T::Currency::minimum_balance().saturating_mul(100u32.into());
TipsMod::<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");
}: _(RawOrigin::Root, hash)
}
impl_benchmark_test_suite!(TipsMod, crate::tests::new_test_ext(), crate::tests::Test);