|
| 1 | +// This file is part of Substrate. |
| 2 | + |
| 3 | +// Copyright (C) 2020 Parity Technologies (UK) Ltd. |
| 4 | +// SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | + |
| 18 | +//! Lottery pallet benchmarking. |
| 19 | +
|
| 20 | +#![cfg(feature = "runtime-benchmarks")] |
| 21 | + |
| 22 | +use super::*; |
| 23 | + |
| 24 | +use frame_system::RawOrigin; |
| 25 | +use frame_support::traits::{OnInitialize, UnfilteredDispatchable}; |
| 26 | +use frame_benchmarking::{benchmarks, account, whitelisted_caller}; |
| 27 | +use sp_runtime::traits::{Bounded, Zero}; |
| 28 | + |
| 29 | +use crate::Module as Lottery; |
| 30 | + |
| 31 | +// Set up and start a lottery |
| 32 | +fn setup_lottery<T: Config>(repeat: bool) -> Result<(), &'static str> { |
| 33 | + let price = T::Currency::minimum_balance(); |
| 34 | + let length = 10u32.into(); |
| 35 | + let delay = 5u32.into(); |
| 36 | + // Calls will be maximum length... |
| 37 | + let mut calls = vec![ |
| 38 | + frame_system::Call::<T>::set_code(vec![]).into(); |
| 39 | + T::MaxCalls::get().saturating_sub(1) |
| 40 | + ]; |
| 41 | + // Last call will be the match for worst case scenario. |
| 42 | + calls.push(frame_system::Call::<T>::remark(vec![]).into()); |
| 43 | + let origin = T::ManagerOrigin::successful_origin(); |
| 44 | + Lottery::<T>::set_calls(origin.clone(), calls)?; |
| 45 | + Lottery::<T>::start_lottery(origin, price, length, delay, repeat)?; |
| 46 | + Ok(()) |
| 47 | +} |
| 48 | + |
| 49 | +benchmarks! { |
| 50 | + _ { } |
| 51 | + |
| 52 | + buy_ticket { |
| 53 | + let caller = whitelisted_caller(); |
| 54 | + T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value()); |
| 55 | + setup_lottery::<T>(false)?; |
| 56 | + // force user to have a long vec of calls participating |
| 57 | + let set_code_index: CallIndex = Lottery::<T>::call_to_index( |
| 58 | + &frame_system::Call::<T>::set_code(vec![]).into() |
| 59 | + )?; |
| 60 | + let already_called: (u32, Vec<CallIndex>) = ( |
| 61 | + LotteryIndex::get(), |
| 62 | + vec![ |
| 63 | + set_code_index; |
| 64 | + T::MaxCalls::get().saturating_sub(1) |
| 65 | + ], |
| 66 | + ); |
| 67 | + Participants::<T>::insert(&caller, already_called); |
| 68 | + |
| 69 | + let call = frame_system::Call::<T>::remark(vec![]); |
| 70 | + }: _(RawOrigin::Signed(caller), Box::new(call.into())) |
| 71 | + verify { |
| 72 | + assert_eq!(TicketsCount::get(), 1); |
| 73 | + } |
| 74 | + |
| 75 | + set_calls { |
| 76 | + let n in 0 .. T::MaxCalls::get() as u32; |
| 77 | + let calls = vec![frame_system::Call::<T>::remark(vec![]).into(); n as usize]; |
| 78 | + |
| 79 | + let call = Call::<T>::set_calls(calls); |
| 80 | + let origin = T::ManagerOrigin::successful_origin(); |
| 81 | + assert!(CallIndices::get().is_empty()); |
| 82 | + }: { call.dispatch_bypass_filter(origin)? } |
| 83 | + verify { |
| 84 | + if !n.is_zero() { |
| 85 | + assert!(!CallIndices::get().is_empty()); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + start_lottery { |
| 90 | + let price = BalanceOf::<T>::max_value(); |
| 91 | + let end = 10u32.into(); |
| 92 | + let payout = 5u32.into(); |
| 93 | + |
| 94 | + let call = Call::<T>::start_lottery(price, end, payout, true); |
| 95 | + let origin = T::ManagerOrigin::successful_origin(); |
| 96 | + }: { call.dispatch_bypass_filter(origin)? } |
| 97 | + verify { |
| 98 | + assert!(crate::Lottery::<T>::get().is_some()); |
| 99 | + } |
| 100 | + |
| 101 | + stop_repeat { |
| 102 | + setup_lottery::<T>(true)?; |
| 103 | + assert_eq!(crate::Lottery::<T>::get().unwrap().repeat, true); |
| 104 | + let call = Call::<T>::stop_repeat(); |
| 105 | + let origin = T::ManagerOrigin::successful_origin(); |
| 106 | + }: { call.dispatch_bypass_filter(origin)? } |
| 107 | + verify { |
| 108 | + assert_eq!(crate::Lottery::<T>::get().unwrap().repeat, false); |
| 109 | + } |
| 110 | + |
| 111 | + on_initialize_end { |
| 112 | + setup_lottery::<T>(false)?; |
| 113 | + let winner = account("winner", 0, 0); |
| 114 | + // User needs more than min balance to get ticket |
| 115 | + T::Currency::make_free_balance_be(&winner, T::Currency::minimum_balance() * 10u32.into()); |
| 116 | + // Make sure lottery account has at least min balance too |
| 117 | + let lottery_account = Lottery::<T>::account_id(); |
| 118 | + T::Currency::make_free_balance_be(&lottery_account, T::Currency::minimum_balance() * 10u32.into()); |
| 119 | + // Buy a ticket |
| 120 | + let call = frame_system::Call::<T>::remark(vec![]); |
| 121 | + Lottery::<T>::buy_ticket(RawOrigin::Signed(winner.clone()).into(), Box::new(call.into()))?; |
| 122 | + // Kill user account for worst case |
| 123 | + T::Currency::make_free_balance_be(&winner, 0u32.into()); |
| 124 | + // Assert that lotto is set up for winner |
| 125 | + assert_eq!(TicketsCount::get(), 1); |
| 126 | + assert!(!Lottery::<T>::pot().1.is_zero()); |
| 127 | + }: { |
| 128 | + // Generate `MaxGenerateRandom` numbers for worst case scenario |
| 129 | + for i in 0 .. T::MaxGenerateRandom::get() { |
| 130 | + Lottery::<T>::generate_random_number(i); |
| 131 | + } |
| 132 | + // Start lottery has block 15 configured for payout |
| 133 | + Lottery::<T>::on_initialize(15u32.into()); |
| 134 | + } |
| 135 | + verify { |
| 136 | + assert!(crate::Lottery::<T>::get().is_none()); |
| 137 | + assert_eq!(TicketsCount::get(), 0); |
| 138 | + assert_eq!(Lottery::<T>::pot().1, 0u32.into()); |
| 139 | + assert!(!T::Currency::free_balance(&winner).is_zero()) |
| 140 | + } |
| 141 | + |
| 142 | + on_initialize_repeat { |
| 143 | + setup_lottery::<T>(true)?; |
| 144 | + let winner = account("winner", 0, 0); |
| 145 | + // User needs more than min balance to get ticket |
| 146 | + T::Currency::make_free_balance_be(&winner, T::Currency::minimum_balance() * 10u32.into()); |
| 147 | + // Make sure lottery account has at least min balance too |
| 148 | + let lottery_account = Lottery::<T>::account_id(); |
| 149 | + T::Currency::make_free_balance_be(&lottery_account, T::Currency::minimum_balance() * 10u32.into()); |
| 150 | + // Buy a ticket |
| 151 | + let call = frame_system::Call::<T>::remark(vec![]); |
| 152 | + Lottery::<T>::buy_ticket(RawOrigin::Signed(winner.clone()).into(), Box::new(call.into()))?; |
| 153 | + // Kill user account for worst case |
| 154 | + T::Currency::make_free_balance_be(&winner, 0u32.into()); |
| 155 | + // Assert that lotto is set up for winner |
| 156 | + assert_eq!(TicketsCount::get(), 1); |
| 157 | + assert!(!Lottery::<T>::pot().1.is_zero()); |
| 158 | + }: { |
| 159 | + // Generate `MaxGenerateRandom` numbers for worst case scenario |
| 160 | + for i in 0 .. T::MaxGenerateRandom::get() { |
| 161 | + Lottery::<T>::generate_random_number(i); |
| 162 | + } |
| 163 | + // Start lottery has block 15 configured for payout |
| 164 | + Lottery::<T>::on_initialize(15u32.into()); |
| 165 | + } |
| 166 | + verify { |
| 167 | + assert!(crate::Lottery::<T>::get().is_some()); |
| 168 | + assert_eq!(LotteryIndex::get(), 2); |
| 169 | + assert_eq!(TicketsCount::get(), 0); |
| 170 | + assert_eq!(Lottery::<T>::pot().1, 0u32.into()); |
| 171 | + assert!(!T::Currency::free_balance(&winner).is_zero()) |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +#[cfg(test)] |
| 176 | +mod tests { |
| 177 | + use super::*; |
| 178 | + use crate::mock::{new_test_ext, Test}; |
| 179 | + use frame_support::assert_ok; |
| 180 | + |
| 181 | + #[test] |
| 182 | + fn test_benchmarks() { |
| 183 | + new_test_ext().execute_with(|| { |
| 184 | + assert_ok!(test_benchmark_buy_ticket::<Test>()); |
| 185 | + assert_ok!(test_benchmark_set_calls::<Test>()); |
| 186 | + assert_ok!(test_benchmark_start_lottery::<Test>()); |
| 187 | + assert_ok!(test_benchmark_stop_repeat::<Test>()); |
| 188 | + assert_ok!(test_benchmark_on_initialize_end::<Test>()); |
| 189 | + assert_ok!(test_benchmark_on_initialize_repeat::<Test>()); |
| 190 | + }); |
| 191 | + } |
| 192 | +} |
0 commit comments