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

Commit

Permalink
fix pallet::const_
Browse files Browse the repository at this point in the history
  • Loading branch information
thiolliere committed Sep 15, 2020
1 parent 91eff0b commit 2130f53
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
11 changes: 7 additions & 4 deletions bin/node-template/pallets/template/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ mod mock;
#[cfg(test)]
mod tests;

#[frame_support::pallet(TemplateModule)]
mod pallet {
/// Configure the pallet by specifying the parameters and types on which it depends.
pub trait Trait: frame_system::Trait {
/// Because this pallet emits events, it depends on the runtime's definition of an event.
type Event: From<Event<Self>> + Into<<Self as frame_system::Trait>::Event>;
}
#[pallet::trait_]
pub trait Trait: frame_system::Trait {
/// Because this pallet emits events, it depends on the runtime's definition of an event.
type Event: From<Event<Self>> + Into<<Self as frame_system::Trait>::Event>;
}

// The pallet's runtime storage items.
// https://substrate.dev/docs/en/knowledgebase/runtime/storage
Expand Down
3 changes: 3 additions & 0 deletions frame/example-offchain-worker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,21 @@ mod pallet {
/// To avoid sending too many transactions, we only attempt to send one
/// every `GRACE_PERIOD` blocks. We use Local Storage to coordinate
/// sending between distinct runs of this offchain worker.
#[pallet::const_]
type GracePeriod: Get<Self::BlockNumber>;

/// Number of blocks of cooldown after unsigned transaction is included.
///
/// This ensures that we only accept unsigned transactions once, every `UnsignedInterval`
/// blocks.
#[pallet::const_]
type UnsignedInterval: Get<Self::BlockNumber>;

/// A configuration for base priority of unsigned transactions.
///
/// This is exposed so that it can be tuned for particular runtime, when
/// multiple pallets send unsigned transactions.
#[pallet::const_]
type UnsignedPriority: Get<TransactionPriority>;
}

Expand Down
4 changes: 2 additions & 2 deletions frame/support/procedural/src/pallet/expand/trait_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub fn expand_trait_(def: &mut Def) -> proc_macro2::TokenStream {
)
),
documentation: #scrate::dispatch::DecodeDifferent::Encode(
&[ #( #doc )* ]
&[ #( #doc ),* ]
),
}
})
Expand All @@ -101,7 +101,7 @@ pub fn expand_trait_(def: &mut Def) -> proc_macro2::TokenStream {
pub fn module_constants_metadata()
-> &'static [#scrate::dispatch::ModuleConstantMetadata]
{
&[ #( #consts )* ]
&[ #( #consts ),* ]
}
}
)
Expand Down
35 changes: 31 additions & 4 deletions frame/support/test/tests/pallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,18 @@ pub mod pallet {

#[pallet::trait_]
pub trait Trait: frame_system::Trait {
/// Some comment
/// Some comment
#[pallet::const_]
type MyGetParam: Get<u32>;

/// Some comment
/// Some comment
#[pallet::const_]
type MyGetParam2: Get<u32>;

type Balance: Parameter + Default;

type Event: From<Event<Self>> + IsType<<Self as frame_system::Trait>::Event>;
}

Expand Down Expand Up @@ -68,7 +77,7 @@ pub mod pallet {
impl<T: Trait> Call for Module<T> where T::Balance: From<u64> {
/// Doc comment put in metadata
#[pallet::weight(Weight::from(*_foo))]
fn foo(origin: OriginFor<T>, #[pallet::compact] _foo: u32) -> DispatchResultWithPostInfo {
fn foo(origin: OriginFor<T>, #[pallet::compact] _foo: u32, _bar: u32) -> DispatchResultWithPostInfo {
T::Balance::from(3u64); // Test for where clause
let _ = origin;
Self::deposit_event(Event::Something(3));
Expand Down Expand Up @@ -174,6 +183,7 @@ pub mod pallet {

frame_support::parameter_types!(
pub const MyGetParam: u32= 10;
pub const MyGetParam2: u32= 11;
pub const BlockHashCount: u32 = 250;
pub const MaximumBlockWeight: frame_support::weights::Weight = 1024;
pub const MaximumBlockLength: u32 = 2 * 1024;
Expand Down Expand Up @@ -210,6 +220,7 @@ impl frame_system::Trait for Runtime {
impl pallet::Trait for Runtime {
type Event = Event;
type MyGetParam= MyGetParam;
type MyGetParam2= MyGetParam2;
type Balance = u64;
}

Expand All @@ -230,7 +241,7 @@ frame_support::construct_runtime!(

#[test]
fn call_expand() {
let call_foo = pallet::Call::<Runtime>::foo(3);
let call_foo = pallet::Call::<Runtime>::foo(3, 0);
assert_eq!(
call_foo.get_dispatch_info(),
DispatchInfo {
Expand Down Expand Up @@ -275,7 +286,7 @@ fn instance_expand() {
fn module_expand_deposit_event() {
TestExternalities::default().execute_with(|| {
frame_system::Module::<Runtime>::set_block_number(1);
pallet::Call::<Runtime>::foo(3).dispatch_bypass_filter(None.into()).unwrap();
pallet::Call::<Runtime>::foo(3, 0).dispatch_bypass_filter(None.into()).unwrap();
assert_eq!(
frame_system::Module::<Runtime>::events()[0].event,
Event::pallet(pallet::Event::Something(3)),
Expand Down Expand Up @@ -432,6 +443,10 @@ fn metadata() {
FunctionArgumentMetadata {
name: DecodeDifferent::Decoded("_foo".to_string()),
ty: DecodeDifferent::Decoded("Compact<u32>".to_string()),
},
FunctionArgumentMetadata {
name: DecodeDifferent::Decoded("_bar".to_string()),
ty: DecodeDifferent::Decoded("u32".to_string()),
}
]),
documentation: DecodeDifferent::Decoded(vec![
Expand Down Expand Up @@ -477,7 +492,19 @@ fn metadata() {
name: DecodeDifferent::Decoded("MyGetParam".to_string()),
ty: DecodeDifferent::Decoded("u32".to_string()),
value: DecodeDifferent::Decoded(vec![10, 0, 0, 0]),
documentation: DecodeDifferent::Decoded(vec![]),
documentation: DecodeDifferent::Decoded(vec![
" Some comment".to_string(),
" Some comment".to_string(),
]),
},
ModuleConstantMetadata {
name: DecodeDifferent::Decoded("MyGetParam2".to_string()),
ty: DecodeDifferent::Decoded("u32".to_string()),
value: DecodeDifferent::Decoded(vec![11, 0, 0, 0]),
documentation: DecodeDifferent::Decoded(vec![
" Some comment".to_string(),
" Some comment".to_string(),
]),
},
]),
errors: DecodeDifferent::Decoded(vec![
Expand Down

0 comments on commit 2130f53

Please sign in to comment.