Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Migrate pallet-lottery to pallet attribute macro (#8762)
Browse files Browse the repository at this point in the history
* Migrate pallet-lottery to pallet attribute macro.

* Fix metadata inconsistency.

* fix

* Use DispatchResult in call returns.
  • Loading branch information
shaunxw authored May 12, 2021
1 parent 972e493 commit 5be1f40
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 176 deletions.
2 changes: 1 addition & 1 deletion bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,7 @@ impl pallet_mmr::Config for Runtime {

parameter_types! {
pub const LotteryPalletId: PalletId = PalletId(*b"py/lotto");
pub const MaxCalls: usize = 10;
pub const MaxCalls: u32 = 10;
pub const MaxGenerateRandom: u32 = 10;
}

Expand Down
26 changes: 13 additions & 13 deletions frame/lottery/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
use super::*;

use frame_system::RawOrigin;
use frame_support::traits::{OnInitialize, UnfilteredDispatchable};
use frame_support::traits::{EnsureOrigin, OnInitialize, UnfilteredDispatchable};
use frame_benchmarking::{benchmarks, account, whitelisted_caller, impl_benchmark_test_suite};
use sp_runtime::traits::{Bounded, Zero};

use crate::Module as Lottery;
use crate::Pallet as Lottery;

// Set up and start a lottery
fn setup_lottery<T: Config>(repeat: bool) -> Result<(), &'static str> {
Expand All @@ -36,7 +36,7 @@ fn setup_lottery<T: Config>(repeat: bool) -> Result<(), &'static str> {
// Calls will be maximum length...
let mut calls = vec![
frame_system::Call::<T>::set_code(vec![]).into();
T::MaxCalls::get().saturating_sub(1)
T::MaxCalls::get().saturating_sub(1) as usize
];
// Last call will be the match for worst case scenario.
calls.push(frame_system::Call::<T>::remark(vec![]).into());
Expand All @@ -56,18 +56,18 @@ benchmarks! {
&frame_system::Call::<T>::set_code(vec![]).into()
)?;
let already_called: (u32, Vec<CallIndex>) = (
LotteryIndex::get(),
LotteryIndex::<T>::get(),
vec![
set_code_index;
T::MaxCalls::get().saturating_sub(1)
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::get(), 1);
assert_eq!(TicketsCount::<T>::get(), 1);
}

set_calls {
Expand All @@ -76,11 +76,11 @@ benchmarks! {

let call = Call::<T>::set_calls(calls);
let origin = T::ManagerOrigin::successful_origin();
assert!(CallIndices::get().is_empty());
assert!(CallIndices::<T>::get().is_empty());
}: { call.dispatch_bypass_filter(origin)? }
verify {
if !n.is_zero() {
assert!(!CallIndices::get().is_empty());
assert!(!CallIndices::<T>::get().is_empty());
}
}

Expand Down Expand Up @@ -120,7 +120,7 @@ benchmarks! {
// Kill user account for worst case
T::Currency::make_free_balance_be(&winner, 0u32.into());
// Assert that lotto is set up for winner
assert_eq!(TicketsCount::get(), 1);
assert_eq!(TicketsCount::<T>::get(), 1);
assert!(!Lottery::<T>::pot().1.is_zero());
}: {
// Generate `MaxGenerateRandom` numbers for worst case scenario
Expand All @@ -132,7 +132,7 @@ benchmarks! {
}
verify {
assert!(crate::Lottery::<T>::get().is_none());
assert_eq!(TicketsCount::get(), 0);
assert_eq!(TicketsCount::<T>::get(), 0);
assert_eq!(Lottery::<T>::pot().1, 0u32.into());
assert!(!T::Currency::free_balance(&winner).is_zero())
}
Expand All @@ -151,7 +151,7 @@ benchmarks! {
// Kill user account for worst case
T::Currency::make_free_balance_be(&winner, 0u32.into());
// Assert that lotto is set up for winner
assert_eq!(TicketsCount::get(), 1);
assert_eq!(TicketsCount::<T>::get(), 1);
assert!(!Lottery::<T>::pot().1.is_zero());
}: {
// Generate `MaxGenerateRandom` numbers for worst case scenario
Expand All @@ -163,8 +163,8 @@ benchmarks! {
}
verify {
assert!(crate::Lottery::<T>::get().is_some());
assert_eq!(LotteryIndex::get(), 2);
assert_eq!(TicketsCount::get(), 0);
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())
}
Expand Down
Loading

0 comments on commit 5be1f40

Please sign in to comment.