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
#![cfg(feature = "runtime-benchmarks")]
use super::*;
use frame_benchmarking::{account, benchmarks, impl_benchmark_test_suite, whitelisted_caller};
use frame_support::traits::{EnsureOrigin, OnInitialize, UnfilteredDispatchable};
use frame_system::RawOrigin;
use sp_runtime::traits::{Bounded, Zero};
use crate::Pallet as Lottery;
fn setup_lottery<T: Config>(repeat: bool) -> Result<(), &'static str> {
let price = T::Currency::minimum_balance();
let length = 10u32.into();
let delay = 5u32.into();
let mut calls = vec![
frame_system::Call::<T>::set_code(vec![]).into();
T::MaxCalls::get().saturating_sub(1) as usize
];
calls.push(frame_system::Call::<T>::remark(vec![]).into());
let origin = T::ManagerOrigin::successful_origin();
Lottery::<T>::set_calls(origin.clone(), calls)?;
Lottery::<T>::start_lottery(origin, price, length, delay, repeat)?;
Ok(())
}
benchmarks! {
buy_ticket {
let caller = whitelisted_caller();
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
setup_lottery::<T>(false)?;
let set_code_index: CallIndex = Lottery::<T>::call_to_index(
&frame_system::Call::<T>::set_code(vec![]).into()
)?;
let already_called: (u32, Vec<CallIndex>) = (
LotteryIndex::<T>::get(),
vec![
set_code_index;
T::MaxCalls::get().saturating_sub(1) as usize
],
);
Participants::<T>::insert(&caller, already_called);
let call = frame_system::Call::<T>::remark(vec![]);
}: _(RawOrigin::Signed(caller), Box::new(call.into()))
verify {
assert_eq!(TicketsCount::<T>::get(), 1);
}
set_calls {
let n in 0 .. T::MaxCalls::get() as u32;
let calls = vec![frame_system::Call::<T>::remark(vec![]).into(); n as usize];
let call = Call::<T>::set_calls(calls);
let origin = T::ManagerOrigin::successful_origin();
assert!(CallIndices::<T>::get().is_empty());
}: { call.dispatch_bypass_filter(origin)? }
verify {
if !n.is_zero() {
assert!(!CallIndices::<T>::get().is_empty());
}
}
start_lottery {
let price = BalanceOf::<T>::max_value();
let end = 10u32.into();
let payout = 5u32.into();
let call = Call::<T>::start_lottery(price, end, payout, true);
let origin = T::ManagerOrigin::successful_origin();
}: { call.dispatch_bypass_filter(origin)? }
verify {
assert!(crate::Lottery::<T>::get().is_some());
}
stop_repeat {
setup_lottery::<T>(true)?;
assert_eq!(crate::Lottery::<T>::get().unwrap().repeat, true);
let call = Call::<T>::stop_repeat();
let origin = T::ManagerOrigin::successful_origin();
}: { call.dispatch_bypass_filter(origin)? }
verify {
assert_eq!(crate::Lottery::<T>::get().unwrap().repeat, false);
}
on_initialize_end {
setup_lottery::<T>(false)?;
let winner = account("winner", 0, 0);
T::Currency::make_free_balance_be(&winner, T::Currency::minimum_balance() * 10u32.into());
let lottery_account = Lottery::<T>::account_id();
T::Currency::make_free_balance_be(&lottery_account, T::Currency::minimum_balance() * 10u32.into());
let call = frame_system::Call::<T>::remark(vec![]);
Lottery::<T>::buy_ticket(RawOrigin::Signed(winner.clone()).into(), Box::new(call.into()))?;
T::Currency::make_free_balance_be(&winner, 0u32.into());
assert_eq!(TicketsCount::<T>::get(), 1);
assert!(!Lottery::<T>::pot().1.is_zero());
}: {
for i in 0 .. T::MaxGenerateRandom::get() {
Lottery::<T>::generate_random_number(i);
}
Lottery::<T>::on_initialize(15u32.into());
}
verify {
assert!(crate::Lottery::<T>::get().is_none());
assert_eq!(TicketsCount::<T>::get(), 0);
assert_eq!(Lottery::<T>::pot().1, 0u32.into());
assert!(!T::Currency::free_balance(&winner).is_zero())
}
on_initialize_repeat {
setup_lottery::<T>(true)?;
let winner = account("winner", 0, 0);
T::Currency::make_free_balance_be(&winner, T::Currency::minimum_balance() * 10u32.into());
let lottery_account = Lottery::<T>::account_id();
T::Currency::make_free_balance_be(&lottery_account, T::Currency::minimum_balance() * 10u32.into());
let call = frame_system::Call::<T>::remark(vec![]);
Lottery::<T>::buy_ticket(RawOrigin::Signed(winner.clone()).into(), Box::new(call.into()))?;
T::Currency::make_free_balance_be(&winner, 0u32.into());
assert_eq!(TicketsCount::<T>::get(), 1);
assert!(!Lottery::<T>::pot().1.is_zero());
}: {
for i in 0 .. T::MaxGenerateRandom::get() {
Lottery::<T>::generate_random_number(i);
}
Lottery::<T>::on_initialize(15u32.into());
}
verify {
assert!(crate::Lottery::<T>::get().is_some());
assert_eq!(LotteryIndex::<T>::get(), 2);
assert_eq!(TicketsCount::<T>::get(), 0);
assert_eq!(Lottery::<T>::pot().1, 0u32.into());
assert!(!T::Currency::free_balance(&winner).is_zero())
}
}
impl_benchmark_test_suite!(Lottery, crate::mock::new_test_ext(), crate::mock::Test);