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

Participation Lottery Pallet #7221

Merged
40 commits merged into from
Jan 5, 2021
Merged
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
a1262a2
Basic design
shawntabrizi Sep 26, 2020
9380a57
Merge branch 'master' into shawntabrizi-lottery
shawntabrizi Nov 24, 2020
8ce5540
start adding tests
shawntabrizi Nov 25, 2020
fc12be9
finish tests
shawntabrizi Nov 25, 2020
d8f0598
clean up crates
shawntabrizi Nov 25, 2020
1289a8e
use call index for match
shawntabrizi Nov 25, 2020
82da7df
finish benchmarks
shawntabrizi Nov 25, 2020
14a5c44
add to runtime
shawntabrizi Nov 25, 2020
ed7c11e
Merge remote-tracking branch 'origin/master' into shawntabrizi-lottery
Nov 25, 2020
f67b119
fix
shawntabrizi Nov 25, 2020
48725b5
Merge branch 'shawntabrizi-lottery' of https://github.com/paritytech/…
shawntabrizi Nov 25, 2020
12a87b2
Merge remote-tracking branch 'origin/master' into shawntabrizi-lottery
Nov 25, 2020
90a3335
cargo run --release --features=runtime-benchmarks --manifest-path=bin…
Nov 25, 2020
844646b
more efficient storage
shawntabrizi Nov 25, 2020
23647d1
Merge remote-tracking branch 'origin/master' into shawntabrizi-lottery
Nov 25, 2020
a121177
cargo run --release --features=runtime-benchmarks --manifest-path=bin…
Nov 25, 2020
6b14824
Update lib.rs
shawntabrizi Nov 25, 2020
e9e0b5a
Update bin/node/runtime/src/lib.rs
shawntabrizi Nov 25, 2020
0ced83e
Merge branch 'master' into shawntabrizi-lottery
shawntabrizi Dec 1, 2020
eee7412
trait -> config
shawntabrizi Dec 1, 2020
47b1860
add repeating lottery
shawntabrizi Dec 1, 2020
05c97ef
new benchmarks
shawntabrizi Dec 1, 2020
c78726f
fix build
shawntabrizi Dec 1, 2020
5585006
move trait for warning
shawntabrizi Dec 1, 2020
5ded45d
feedback from @xlc
shawntabrizi Dec 1, 2020
9739b40
add stop_repeat
shawntabrizi Dec 1, 2020
ef67c0e
fix
shawntabrizi Dec 1, 2020
649cfa7
cargo run --release --features=runtime-benchmarks --manifest-path=bin…
Dec 1, 2020
d50bf84
Support static calls
shawntabrizi Dec 1, 2020
280afe6
Merge remote-tracking branch 'origin/master' into shawntabrizi-lottery
Dec 1, 2020
5460912
cargo run --release --features=runtime-benchmarks --manifest-path=bin…
Dec 1, 2020
171188a
Merge branch 'master' into shawntabrizi-lottery
shawntabrizi Dec 4, 2020
3164e13
Merge branch 'master' into shawntabrizi-lottery
shawntabrizi Dec 5, 2020
9707522
Merge branch 'master' into shawntabrizi-lottery
shawntabrizi Jan 1, 2021
c343e03
fix test
shawntabrizi Jan 1, 2021
8fc534c
add loop to mitigate modulo bias
shawntabrizi Jan 1, 2021
34e9c43
Update weights for worst case scenario loop
shawntabrizi Jan 1, 2021
e3c6a16
Initialize pot with ED
shawntabrizi Jan 5, 2021
7b4ee4a
Merge remote-tracking branch 'origin/master' into shawntabrizi-lottery
Jan 5, 2021
3de5a8b
cargo run --release --features=runtime-benchmarks --manifest-path=bin…
Jan 5, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add stop_repeat
shawntabrizi committed Dec 1, 2020
commit 9739b404398e46dd5c2250bc7a1948e40694f000
40 changes: 25 additions & 15 deletions frame/lottery/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -48,20 +48,6 @@ fn setup_lottery<T: Config>(repeat: bool) -> Result<(), &'static str> {
benchmarks! {
_ { }

start_lottery {
let n in 0 .. T::MaxCalls::get() as u32;
let price = BalanceOf::<T>::max_value();
let end = 10u32.into();
let payout = 5u32.into();
let calls = vec![frame_system::Call::<T>::remark(vec![]).into(); n as usize];

let call = Call::<T>::start_lottery(price, end, payout, calls, true);
let origin = T::ManagerOrigin::successful_origin();
}: { call.dispatch_bypass_filter(origin)? }
verify {
assert!(crate::Lottery::<T>::get().is_some());
}

buy_ticket {
let caller = whitelisted_caller();
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
@@ -70,7 +56,7 @@ benchmarks! {
let set_code_index: CallIndex = Lottery::<T>::call_to_index(
&frame_system::Call::<T>::set_code(vec![]).into()
)?;
let already_called: (Index, Vec<CallIndex>) = (
let already_called: (u32, Vec<CallIndex>) = (
LotteryIndex::get(),
vec![
set_code_index;
@@ -85,6 +71,30 @@ benchmarks! {
assert_eq!(TicketsCount::get(), 1);
}

start_lottery {
let n in 0 .. T::MaxCalls::get() as u32;
let price = BalanceOf::<T>::max_value();
let end = 10u32.into();
let payout = 5u32.into();
let calls = vec![frame_system::Call::<T>::remark(vec![]).into(); n as usize];

let call = Call::<T>::start_lottery(price, end, payout, calls, 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!(crate::Lottery::<T>::get().unwrap().repeat, false);
}

on_initialize_end {
setup_lottery::<T>(false)?;
let winner = account("winner", 0, 0);
37 changes: 37 additions & 0 deletions frame/lottery/src/lib.rs
Original file line number Diff line number Diff line change
@@ -148,6 +148,18 @@ decl_module! {

fn deposit_event() = default;

/// Start a lottery using the provided configuration.
///
/// This extrinsic must be called by the `ManagerOrigin`.
///
/// Parameters:
///
/// * `price`: The cost of a single ticket.
/// * `length`: How long the lottery should run for starting at the current block.
/// * `delay`: How long after the lottery end we should wait before picking a winner.
/// * `calls`: The calls allowed for purchasing a lottery ticket. **Important**: Only
/// the call index is used to determine if the ticket purchase is valid.
/// * `repeat`: If the lottery should repeat when completed.
#[weight = T::WeightInfo::start_lottery(calls.len() as u32)]
fn start_lottery(origin,
price: BalanceOf<T>,
@@ -179,6 +191,17 @@ decl_module! {
Self::deposit_event(RawEvent::LotteryStarted);
}

/// Buy a ticket to enter the lottery.
///
/// This extrinsic acts as a passthrough function for `call`. In all
/// situations where `call` alone would succeed, this extrinsic should
/// succeed.
///
/// If `call` is successful, then we will attempt to purchase a ticket,
/// which may fail silently. To detect success of a ticket purchase, you
/// should listen for the `TicketBought` event.
///
/// This extrinsic must be called by a signed origin.
#[weight =
T::WeightInfo::buy_ticket()
.saturating_add(call.get_dispatch_info().weight)
@@ -196,6 +219,20 @@ decl_module! {
let _ = Self::do_buy_ticket(&caller, &call_index);
}

/// If a lottery is repeating, you can use this to stop the repeat.
/// The lottery will continue to run to completion.
///
/// This extrinsic must be called by the `ManagerOrigin`.
#[weight = T::WeightInfo::stop_repeat()]
fn stop_repeat(origin) {
T::ManagerOrigin::ensure_origin(origin)?;
Lottery::<T>::mutate(|mut lottery| {
if let Some(config) = &mut lottery {
config.repeat = false
}
});
}

fn on_initialize(n: T::BlockNumber) -> Weight {
Lottery::<T>::mutate(|mut lottery| -> Weight {
if let Some(config) = &mut lottery {
3 changes: 2 additions & 1 deletion frame/lottery/src/tests.rs
Original file line number Diff line number Diff line change
@@ -48,7 +48,7 @@ fn basic_end_to_end_works() {
Call::Balances(BalancesCall::transfer(0, 0)),
];

// Start lottery
// Start lottery, it repeats
assert_ok!(Lottery::start_lottery(Origin::root(), price, length, delay, calls.clone(), true));
assert!(crate::Lottery::<Test>::get().is_some());

@@ -216,5 +216,6 @@ fn buy_ticket_works() {
run_to_block(25);
assert_ok!(Lottery::buy_ticket(Origin::signed(2), call.clone()));
assert_eq!(TicketsCount::get(), 0);
assert_eq!(LotteryIndex::get(), 1);
});
}
11 changes: 11 additions & 0 deletions frame/lottery/src/weights.rs
Original file line number Diff line number Diff line change
@@ -46,6 +46,7 @@ use sp_std::marker::PhantomData;
pub trait WeightInfo {
fn start_lottery(n: u32, ) -> Weight;
fn buy_ticket() -> Weight;
fn stop_repeat() -> Weight;
fn on_initialize_repeat() -> Weight;
fn on_initialize_end() -> Weight;
}
@@ -64,6 +65,11 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
.saturating_add(T::DbWeight::get().reads(5 as Weight))
.saturating_add(T::DbWeight::get().writes(4 as Weight))
}
fn stop_repeat() -> Weight {
(111_483_000 as Weight)
.saturating_add(T::DbWeight::get().reads(6 as Weight))
.saturating_add(T::DbWeight::get().writes(4 as Weight))
}
fn on_initialize_repeat() -> Weight {
(111_483_000 as Weight)
.saturating_add(T::DbWeight::get().reads(6 as Weight))
@@ -89,6 +95,11 @@ impl WeightInfo for () {
.saturating_add(RocksDbWeight::get().reads(5 as Weight))
.saturating_add(RocksDbWeight::get().writes(4 as Weight))
}
fn stop_repeat() -> Weight {
(116_213_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(5 as Weight))
.saturating_add(RocksDbWeight::get().writes(4 as Weight))
}
fn on_initialize_repeat() -> Weight {
(111_483_000 as Weight)
.saturating_add(RocksDbWeight::get().reads(6 as Weight))