This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Participation Lottery Pallet (#7221)
* Basic design * start adding tests * finish tests * clean up crates * use call index for match * finish benchmarks * add to runtime * fix * cargo run --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_lottery --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/lottery/src/weights.rs --template=./.maintain/frame-weight-template.hbs * more efficient storage * cargo run --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_lottery --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/lottery/src/weights.rs --template=./.maintain/frame-weight-template.hbs * Update lib.rs * Update bin/node/runtime/src/lib.rs * trait -> config * add repeating lottery * new benchmarks * fix build * move trait for warning * feedback from @xlc * add stop_repeat * fix * cargo run --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_lottery --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/lottery/src/weights.rs --template=./.maintain/frame-weight-template.hbs * Support static calls * cargo run --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_lottery --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/lottery/src/weights.rs --template=./.maintain/frame-weight-template.hbs * fix test * add loop to mitigate modulo bias * Update weights for worst case scenario loop * Initialize pot with ED * cargo run --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_lottery --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/lottery/src/weights.rs --template=./.maintain/frame-weight-template.hbs Co-authored-by: Parity Benchmarking Bot <admin@parity.io>
- Loading branch information
1 parent
0091c09
commit 1b840aa
Showing
10 changed files
with
1,250 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
[package] | ||
name = "pallet-lottery" | ||
version = "2.0.0" | ||
authors = ["Parity Technologies <admin@parity.io>"] | ||
edition = "2018" | ||
license = "Apache-2.0" | ||
homepage = "https://substrate.dev" | ||
repository = "https://github.com/paritytech/substrate/" | ||
description = "FRAME Participation Lottery Pallet" | ||
readme = "README.md" | ||
|
||
[package.metadata.docs.rs] | ||
targets = ["x86_64-unknown-linux-gnu"] | ||
|
||
[dependencies] | ||
codec = { package = "parity-scale-codec", version = "1.3.4", default-features = false, features = ["derive"] } | ||
sp-std = { version = "2.0.0", default-features = false, path = "../../primitives/std" } | ||
sp-runtime = { version = "2.0.0", default-features = false, path = "../../primitives/runtime" } | ||
frame-support = { version = "2.0.0", default-features = false, path = "../support" } | ||
frame-system = { version = "2.0.0", default-features = false, path = "../system" } | ||
|
||
frame-benchmarking = { version = "2.0.0", default-features = false, path = "../benchmarking", optional = true } | ||
|
||
[dev-dependencies] | ||
pallet-balances = { version = "2.0.0", path = "../balances" } | ||
sp-core = { version = "2.0.0", path = "../../primitives/core" } | ||
sp-io = { version = "2.0.0", path = "../../primitives/io" } | ||
|
||
[features] | ||
default = ["std"] | ||
std = [ | ||
"codec/std", | ||
"sp-std/std", | ||
"frame-support/std", | ||
"sp-runtime/std", | ||
"frame-system/std", | ||
] | ||
runtime-benchmarks = [ | ||
"frame-benchmarking", | ||
"frame-system/runtime-benchmarks", | ||
"frame-support/runtime-benchmarks", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
// This file is part of Substrate. | ||
|
||
// Copyright (C) 2020 Parity Technologies (UK) Ltd. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//! Lottery pallet benchmarking. | ||
#![cfg(feature = "runtime-benchmarks")] | ||
|
||
use super::*; | ||
|
||
use frame_system::RawOrigin; | ||
use frame_support::traits::{OnInitialize, UnfilteredDispatchable}; | ||
use frame_benchmarking::{benchmarks, account, whitelisted_caller}; | ||
use sp_runtime::traits::{Bounded, Zero}; | ||
|
||
use crate::Module as Lottery; | ||
|
||
// Set up and start a 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(); | ||
// Calls will be maximum length... | ||
let mut calls = vec![ | ||
frame_system::Call::<T>::set_code(vec![]).into(); | ||
T::MaxCalls::get().saturating_sub(1) | ||
]; | ||
// Last call will be the match for worst case scenario. | ||
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)?; | ||
// force user to have a long vec of calls participating | ||
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::get(), | ||
vec![ | ||
set_code_index; | ||
T::MaxCalls::get().saturating_sub(1) | ||
], | ||
); | ||
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); | ||
} | ||
|
||
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::get().is_empty()); | ||
}: { call.dispatch_bypass_filter(origin)? } | ||
verify { | ||
if !n.is_zero() { | ||
assert!(!CallIndices::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); | ||
// User needs more than min balance to get ticket | ||
T::Currency::make_free_balance_be(&winner, T::Currency::minimum_balance() * 10u32.into()); | ||
// Make sure lottery account has at least min balance too | ||
let lottery_account = Lottery::<T>::account_id(); | ||
T::Currency::make_free_balance_be(&lottery_account, T::Currency::minimum_balance() * 10u32.into()); | ||
// Buy a ticket | ||
let call = frame_system::Call::<T>::remark(vec![]); | ||
Lottery::<T>::buy_ticket(RawOrigin::Signed(winner.clone()).into(), Box::new(call.into()))?; | ||
// 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!(!Lottery::<T>::pot().1.is_zero()); | ||
}: { | ||
// Generate `MaxGenerateRandom` numbers for worst case scenario | ||
for i in 0 .. T::MaxGenerateRandom::get() { | ||
Lottery::<T>::generate_random_number(i); | ||
} | ||
// Start lottery has block 15 configured for payout | ||
Lottery::<T>::on_initialize(15u32.into()); | ||
} | ||
verify { | ||
assert!(crate::Lottery::<T>::get().is_none()); | ||
assert_eq!(TicketsCount::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); | ||
// User needs more than min balance to get ticket | ||
T::Currency::make_free_balance_be(&winner, T::Currency::minimum_balance() * 10u32.into()); | ||
// Make sure lottery account has at least min balance too | ||
let lottery_account = Lottery::<T>::account_id(); | ||
T::Currency::make_free_balance_be(&lottery_account, T::Currency::minimum_balance() * 10u32.into()); | ||
// Buy a ticket | ||
let call = frame_system::Call::<T>::remark(vec![]); | ||
Lottery::<T>::buy_ticket(RawOrigin::Signed(winner.clone()).into(), Box::new(call.into()))?; | ||
// 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!(!Lottery::<T>::pot().1.is_zero()); | ||
}: { | ||
// Generate `MaxGenerateRandom` numbers for worst case scenario | ||
for i in 0 .. T::MaxGenerateRandom::get() { | ||
Lottery::<T>::generate_random_number(i); | ||
} | ||
// Start lottery has block 15 configured for payout | ||
Lottery::<T>::on_initialize(15u32.into()); | ||
} | ||
verify { | ||
assert!(crate::Lottery::<T>::get().is_some()); | ||
assert_eq!(LotteryIndex::get(), 2); | ||
assert_eq!(TicketsCount::get(), 0); | ||
assert_eq!(Lottery::<T>::pot().1, 0u32.into()); | ||
assert!(!T::Currency::free_balance(&winner).is_zero()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::mock::{new_test_ext, Test}; | ||
use frame_support::assert_ok; | ||
|
||
#[test] | ||
fn test_benchmarks() { | ||
new_test_ext().execute_with(|| { | ||
assert_ok!(test_benchmark_buy_ticket::<Test>()); | ||
assert_ok!(test_benchmark_set_calls::<Test>()); | ||
assert_ok!(test_benchmark_start_lottery::<Test>()); | ||
assert_ok!(test_benchmark_stop_repeat::<Test>()); | ||
assert_ok!(test_benchmark_on_initialize_end::<Test>()); | ||
assert_ok!(test_benchmark_on_initialize_repeat::<Test>()); | ||
}); | ||
} | ||
} |
Oops, something went wrong.