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

revamp locking & reserving #13122

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
23 changes: 20 additions & 3 deletions frame/elections-phragmen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,13 @@ pub mod pallet {
#[pallet::without_storage_info]
pub struct Pallet<T>(PhantomData<T>);

// local lock id.
// `#[pallet::composite]`
enum LockId {
CouncilVoting,
OtherReason,
}

#[pallet::config]
pub trait Config: frame_system::Config {
type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;
Expand All @@ -203,9 +210,15 @@ pub mod pallet {
#[pallet::constant]
type PalletId: Get<LockIdentifier>;

/// This will come from the outer runtime, and it will be the amalgamated lock ids.
type RuntimeLockIds: From<crate::pallet::LockId>;

/// The currency that people are electing with.
type Currency: LockableCurrency<Self::AccountId, Moment = Self::BlockNumber>
+ ReservableCurrency<Self::AccountId>;
type Currency: LockableCurrency<
Self::AccountId,
Moment = Self::BlockNumber,
LockId = Self::RuntimeLockIds,
> + ReservableCurrency<Self::AccountId>;

/// What to do when the members change.
type ChangeMembers: ChangeMembers<Self::AccountId>;
Expand Down Expand Up @@ -361,9 +374,13 @@ pub mod pallet {
},
};

let id: T::RuntimeLockIds = LockId::CouncilVoting.into();

// Amount to be locked up.
let locked_stake = value.min(T::Currency::free_balance(&who));
T::Currency::set_lock(T::PalletId::get(), &who, locked_stake, WithdrawReasons::all());
// TODO: the currency implementations, whatever it might be, will know if this lock/hold
// operation is going to collide with collide with other existing locks or not.
T::Currency::set_lock(id, &who, locked_stake, WithdrawReasons::all());

Voting::<T>::insert(&who, Voter { votes, deposit: new_deposit, stake: locked_stake });
Ok(None::<Weight>.into())
Expand Down
5 changes: 5 additions & 0 deletions frame/support/procedural/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1289,3 +1289,8 @@ pub fn validate_unsigned(_: TokenStream, _: TokenStream) -> TokenStream {
pub fn origin(_: TokenStream, _: TokenStream) -> TokenStream {
pallet_macro_stub()
}

#[proc_macro_attribute]
pub fn composite(_: TokenStream, _: TokenStream) -> TokenStream {
pallet_macro_stub()
}
17 changes: 16 additions & 1 deletion frame/support/procedural/src/pallet/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ pub mod type_value;
pub mod validate_unsigned;

use frame_support_procedural_tools::generate_crate_access_2018;
use syn::spanned::Spanned;
use quote::ToTokens;
use syn::{spanned::Spanned, Item, ItemEnum};

/// Parsed definition of a pallet.
pub struct Def {
Expand All @@ -57,6 +58,7 @@ pub struct Def {
pub validate_unsigned: Option<validate_unsigned::ValidateUnsignedDef>,
pub extra_constants: Option<extra_constants::ExtraConstantsDef>,
pub type_values: Vec<type_value::TypeValueDef>,
pub composites: Vec<ItemEnum>,
pub frame_system: syn::Ident,
pub frame_support: syn::Ident,
pub dev_mode: bool,
Expand Down Expand Up @@ -91,6 +93,7 @@ impl Def {
let mut extra_constants = None;
let mut storages = vec![];
let mut type_values = vec![];
let mut composites = vec![];

for (index, item) in items.iter_mut().enumerate() {
let pallet_attr: Option<PalletAttr> = helper::take_first_item_pallet_attr(item)?;
Expand Down Expand Up @@ -132,6 +135,12 @@ impl Def {
},
Some(PalletAttr::TypeValue(span)) =>
type_values.push(type_value::TypeValueDef::try_from(span, index, item)?),
Some(PalletAttr::Composite(_)) =>
if let Item::Enum(item) = item {
composites.push(item.clone().into());
} else {
syn::parse::<ItemEnum>(item.to_token_stream().into())?;
},
Some(PalletAttr::ExtraConstants(_)) =>
extra_constants =
Some(extra_constants::ExtraConstantsDef::try_from(index, item)?),
Expand Down Expand Up @@ -174,6 +183,7 @@ impl Def {
type_values,
frame_system,
frame_support,
composites,
dev_mode,
};

Expand Down Expand Up @@ -385,6 +395,7 @@ mod keyword {
syn::custom_keyword!(generate_store);
syn::custom_keyword!(Store);
syn::custom_keyword!(extra_constants);
syn::custom_keyword!(composite);
}

/// Parse attributes for item in pallet module
Expand All @@ -404,6 +415,7 @@ enum PalletAttr {
ValidateUnsigned(proc_macro2::Span),
TypeValue(proc_macro2::Span),
ExtraConstants(proc_macro2::Span),
Composite(proc_macro2::Span),
}

impl PalletAttr {
Expand All @@ -423,6 +435,7 @@ impl PalletAttr {
Self::ValidateUnsigned(span) => *span,
Self::TypeValue(span) => *span,
Self::ExtraConstants(span) => *span,
Self::Composite(span) => *span,
}
}
}
Expand Down Expand Up @@ -464,6 +477,8 @@ impl syn::parse::Parse for PalletAttr {
Ok(PalletAttr::TypeValue(content.parse::<keyword::type_value>()?.span()))
} else if lookahead.peek(keyword::extra_constants) {
Ok(PalletAttr::ExtraConstants(content.parse::<keyword::extra_constants>()?.span()))
} else if lookahead.peek(keyword::composite) {
Ok(PalletAttr::Composite(content.parse::<keyword::composite>()?.span()))
} else {
Err(lookahead.error())
}
Expand Down
8 changes: 4 additions & 4 deletions frame/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2738,10 +2738,10 @@ pub use frame_support_procedural::pallet;
/// Contains macro stubs for all of the pallet:: macros
pub mod pallet_macros {
pub use frame_support_procedural::{
call_index, compact, config, constant, disable_frame_system_supertrait_check, error, event,
extra_constants, generate_deposit, generate_storage_info, generate_store, genesis_build,
genesis_config, getter, hooks, inherent, origin, storage, storage_prefix, storage_version,
type_value, unbounded, validate_unsigned, weight, whitelist_storage,
call_index, compact, composite, config, constant, disable_frame_system_supertrait_check,
error, event, extra_constants, generate_deposit, generate_storage_info, generate_store,
genesis_build, genesis_config, getter, hooks, inherent, origin, storage, storage_prefix,
storage_version, type_value, unbounded, validate_unsigned, weight, whitelist_storage,
};
}

Expand Down
8 changes: 6 additions & 2 deletions frame/support/src/traits/tokens/currency/lockable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ pub trait LockableCurrency<AccountId>: Currency<AccountId> {
/// The quantity used to denote time; usually just a `BlockNumber`.
type Moment;

/// Opaque Identifier for lock.
// TODO: can already be ported to master with LockIdentifier used everywhere.
type LockId;

/// The maximum number of locks a user should have on their account.
type MaxLocks: Get<u32>;

Expand All @@ -39,7 +43,7 @@ pub trait LockableCurrency<AccountId>: Currency<AccountId> {
///
/// If the lock `id` already exists, this will update it.
fn set_lock(
id: LockIdentifier,
id: Self::LockId,
who: &AccountId,
amount: Self::Balance,
reasons: WithdrawReasons,
Expand All @@ -54,7 +58,7 @@ pub trait LockableCurrency<AccountId>: Currency<AccountId> {
/// - maximum `amount`
/// - bitwise mask of all `reasons`
fn extend_lock(
id: LockIdentifier,
id: Self::LockId,
who: &AccountId,
amount: Self::Balance,
reasons: WithdrawReasons,
Expand Down