From 2130f534b3c5a9bdae1fa2ce91822b834b8a77b4 Mon Sep 17 00:00:00 2001 From: thiolliere Date: Tue, 15 Sep 2020 14:09:15 +0200 Subject: [PATCH] fix pallet::const_ --- bin/node-template/pallets/template/src/lib.rs | 11 +++--- frame/example-offchain-worker/src/lib.rs | 3 ++ .../procedural/src/pallet/expand/trait_.rs | 4 +-- frame/support/test/tests/pallet.rs | 35 ++++++++++++++++--- 4 files changed, 43 insertions(+), 10 deletions(-) diff --git a/bin/node-template/pallets/template/src/lib.rs b/bin/node-template/pallets/template/src/lib.rs index 729a71278aa9f..28aba2cb6dca2 100644 --- a/bin/node-template/pallets/template/src/lib.rs +++ b/bin/node-template/pallets/template/src/lib.rs @@ -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> + Into<::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> + Into<::Event>; + } // The pallet's runtime storage items. // https://substrate.dev/docs/en/knowledgebase/runtime/storage diff --git a/frame/example-offchain-worker/src/lib.rs b/frame/example-offchain-worker/src/lib.rs index 8b95769b901ac..547deb4b8bb4c 100644 --- a/frame/example-offchain-worker/src/lib.rs +++ b/frame/example-offchain-worker/src/lib.rs @@ -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; /// 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; /// 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; } diff --git a/frame/support/procedural/src/pallet/expand/trait_.rs b/frame/support/procedural/src/pallet/expand/trait_.rs index d58ec04d4ff73..e243ff6046373 100644 --- a/frame/support/procedural/src/pallet/expand/trait_.rs +++ b/frame/support/procedural/src/pallet/expand/trait_.rs @@ -88,7 +88,7 @@ pub fn expand_trait_(def: &mut Def) -> proc_macro2::TokenStream { ) ), documentation: #scrate::dispatch::DecodeDifferent::Encode( - &[ #( #doc )* ] + &[ #( #doc ),* ] ), } }) @@ -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 ),* ] } } ) diff --git a/frame/support/test/tests/pallet.rs b/frame/support/test/tests/pallet.rs index 2b9618eb113e8..6242510eb0ba8 100644 --- a/frame/support/test/tests/pallet.rs +++ b/frame/support/test/tests/pallet.rs @@ -33,9 +33,18 @@ pub mod pallet { #[pallet::trait_] pub trait Trait: frame_system::Trait { + /// Some comment + /// Some comment #[pallet::const_] type MyGetParam: Get; + + /// Some comment + /// Some comment + #[pallet::const_] + type MyGetParam2: Get; + type Balance: Parameter + Default; + type Event: From> + IsType<::Event>; } @@ -68,7 +77,7 @@ pub mod pallet { impl Call for Module where T::Balance: From { /// Doc comment put in metadata #[pallet::weight(Weight::from(*_foo))] - fn foo(origin: OriginFor, #[pallet::compact] _foo: u32) -> DispatchResultWithPostInfo { + fn foo(origin: OriginFor, #[pallet::compact] _foo: u32, _bar: u32) -> DispatchResultWithPostInfo { T::Balance::from(3u64); // Test for where clause let _ = origin; Self::deposit_event(Event::Something(3)); @@ -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; @@ -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; } @@ -230,7 +241,7 @@ frame_support::construct_runtime!( #[test] fn call_expand() { - let call_foo = pallet::Call::::foo(3); + let call_foo = pallet::Call::::foo(3, 0); assert_eq!( call_foo.get_dispatch_info(), DispatchInfo { @@ -275,7 +286,7 @@ fn instance_expand() { fn module_expand_deposit_event() { TestExternalities::default().execute_with(|| { frame_system::Module::::set_block_number(1); - pallet::Call::::foo(3).dispatch_bypass_filter(None.into()).unwrap(); + pallet::Call::::foo(3, 0).dispatch_bypass_filter(None.into()).unwrap(); assert_eq!( frame_system::Module::::events()[0].event, Event::pallet(pallet::Event::Something(3)), @@ -432,6 +443,10 @@ fn metadata() { FunctionArgumentMetadata { name: DecodeDifferent::Decoded("_foo".to_string()), ty: DecodeDifferent::Decoded("Compact".to_string()), + }, + FunctionArgumentMetadata { + name: DecodeDifferent::Decoded("_bar".to_string()), + ty: DecodeDifferent::Decoded("u32".to_string()), } ]), documentation: DecodeDifferent::Decoded(vec![ @@ -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![