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

pallet-lottery: add generate_storage_info #10594

Merged
merged 14 commits into from
Jan 11, 2022
Merged
33 changes: 23 additions & 10 deletions frame/lottery/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
//! users to make those calls on your network. An example of how this could be
//! used is to set validator nominations as a valid lottery call. If the lottery
//! is set to repeat every month, then users would be encouraged to re-nominate
//! validators every month. A user can ony purchase one ticket per valid call
//! validators every month. A user can only purchase one ticket per valid call
//! per lottery.
//!
//! This pallet can be configured to use dynamically set calls or statically set
Expand Down Expand Up @@ -58,6 +58,8 @@ use codec::{Decode, Encode};
use frame_support::{
dispatch::{DispatchResult, Dispatchable, GetDispatchInfo},
ensure,
pallet_prelude::MaxEncodedLen,
storage::bounded_vec::BoundedVec,
traits::{Currency, ExistenceRequirement::KeepAlive, Get, Randomness, ReservableCurrency},
PalletId, RuntimeDebug,
};
Expand All @@ -76,7 +78,9 @@ type BalanceOf<T> =
// We use this to uniquely match someone's incoming call with the calls configured for the lottery.
type CallIndex = (u8, u8);

#[derive(Encode, Decode, Default, Eq, PartialEq, RuntimeDebug, scale_info::TypeInfo)]
#[derive(
Encode, Decode, Default, Eq, PartialEq, RuntimeDebug, scale_info::TypeInfo, MaxEncodedLen,
)]
pub struct LotteryConfig<BlockNumber, Balance> {
/// Price per entry.
price: Balance,
Expand Down Expand Up @@ -120,6 +124,7 @@ pub mod pallet {

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::generate_storage_info]
pub struct Pallet<T>(_);

/// The pallet's config trait.
Expand Down Expand Up @@ -209,8 +214,13 @@ pub mod pallet {

/// Users who have purchased a ticket. (Lottery Index, Tickets Purchased)
#[pallet::storage]
pub(crate) type Participants<T: Config> =
StorageMap<_, Twox64Concat, T::AccountId, (u32, Vec<CallIndex>), ValueQuery>;
pub(crate) type Participants<T: Config> = StorageMap<
_,
Twox64Concat,
T::AccountId,
(u32, BoundedVec<CallIndex, T::MaxCalls>),
ValueQuery,
>;

/// Total number of tickets sold.
#[pallet::storage]
Expand All @@ -226,7 +236,8 @@ pub mod pallet {
/// The calls stored in this pallet to be used in an active lottery if configured
/// by `Config::ValidateCall`.
#[pallet::storage]
pub(crate) type CallIndices<T> = StorageValue<_, Vec<CallIndex>, ValueQuery>;
pub(crate) type CallIndices<T: Config> =
StorageValue<_, BoundedVec<CallIndex, T::MaxCalls>, ValueQuery>;

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
Expand Down Expand Up @@ -395,11 +406,13 @@ impl<T: Config> Pallet<T> {
}

// Converts a vector of calls into a vector of call indices.
fn calls_to_indices(calls: &[<T as Config>::Call]) -> Result<Vec<CallIndex>, DispatchError> {
let mut indices = Vec::with_capacity(calls.len());
fn calls_to_indices(
calls: &[<T as Config>::Call],
) -> Result<BoundedVec<CallIndex, T::MaxCalls>, DispatchError> {
let mut indices = BoundedVec::<CallIndex, T::MaxCalls>::default();
ggwpez marked this conversation as resolved.
Show resolved Hide resolved
for c in calls.iter() {
let index = Self::call_to_index(c)?;
indices.push(index)
indices.try_push(index).map_err(|_| Error::<T>::TooManyCalls)?;
}
Ok(indices)
}
Expand Down Expand Up @@ -433,7 +446,7 @@ impl<T: Config> Pallet<T> {
let index = LotteryIndex::<T>::get();
// If lottery index doesn't match, then reset participating calls and index.
if *lottery_index != index {
*participating_calls = Vec::new();
*participating_calls = Default::default();
*lottery_index = index;
} else {
// Check that user is not already participating under this call.
Expand All @@ -447,7 +460,7 @@ impl<T: Config> Pallet<T> {
// Create a new ticket.
TicketsCount::<T>::put(new_ticket_count);
Tickets::<T>::insert(ticket_count, caller.clone());
participating_calls.push(call_index);
shawntabrizi marked this conversation as resolved.
Show resolved Hide resolved
participating_calls.try_push(call_index).map_err(|_| Error::<T>::TooManyCalls)?;
Ok(())
},
)?;
Expand Down
2 changes: 1 addition & 1 deletion frame/lottery/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn initial_state() {
new_test_ext().execute_with(|| {
assert_eq!(Balances::free_balance(Lottery::account_id()), 0);
assert!(crate::Lottery::<Test>::get().is_none());
assert_eq!(Participants::<Test>::get(&1), (0, vec![]));
shawntabrizi marked this conversation as resolved.
Show resolved Hide resolved
assert_eq!(Participants::<Test>::get(&1), (0, Default::default()));
assert_eq!(TicketsCount::<Test>::get(), 0);
assert!(Tickets::<Test>::get(0).is_none());
});
Expand Down