Allow pallet in construct_runtime to have fixed index (#6969)

* implement index for pallet + some tests

* add test and doc

* remove deprecated and document behavior

* update internal doc

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* address review

* use index for all module, break construct_runtime

* fix line length

* implement migration helper funciton in scheduler

* fix start at index 0

* Update frame/scheduler/src/lib.rs

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>

* Update frame/support/procedural/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* bump frame-metadata crate

* factorize

* avoid some unwrap and remove nightly join

* Update frame/support/src/event.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* fix test

* add test and improve error message

* factorize test

* keep iterator, and use slice instead of vec

* refactor to avoid to have expects

* small refactor

* Test something

* Make sure we update the `Cargo.lock`

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* return 2 error

* Apply suggestions from code review

Co-authored-by: Alexander Popiak <alexander.popiak@parity.io>

* Update frame/scheduler/src/lib.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* fix typo

* Revert "fix typo"

This reverts commit f2de8f2db34e8ac72bc9c34437c60dca3fa4ac22.

* Revert "Update frame/scheduler/src/lib.rs"

This reverts commit 6feb4605c6f784b64591e229de7a6fec6dbffb4b.

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
Co-authored-by: Bastian Köcher <git@kchr.de>
Co-authored-by: Alexander Popiak <alexander.popiak@parity.io>
Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
This commit is contained in:
Guillaume Thiolliere
2020-09-22 16:54:03 +02:00
committed by GitHub
parent 86594727d9
commit 98951b33a9
21 changed files with 876 additions and 128 deletions
+2 -1
View File
@@ -1911,7 +1911,7 @@ macro_rules! impl_outer_dispatch {
$(#[$attr:meta])*
pub enum $call_type:ident for $runtime:ident where origin: $origin:ty {
$(
$module:ident::$camelcase:ident,
$( #[codec(index = $index:tt)] )? $module:ident::$camelcase:ident,
)*
}
) => {
@@ -1924,6 +1924,7 @@ macro_rules! impl_outer_dispatch {
)]
pub enum $call_type {
$(
$( #[codec(index = $index)] )?
$camelcase ( $crate::dispatch::CallableCallFor<$camelcase, $runtime> )
,)*
}
+41 -15
View File
@@ -346,7 +346,7 @@ macro_rules! impl_outer_event {
$name;
$runtime;
Modules { $( $rest_events )* };
;
{};
);
};
// Generic + Instance
@@ -355,17 +355,17 @@ macro_rules! impl_outer_event {
$name:ident;
$runtime:ident;
Modules {
$module:ident $instance:ident<T>,
$( #[codec(index = $index:tt)] )? $module:ident $instance:ident<T>,
$( $rest_event_generic_instance:tt )*
};
$( $module_name:ident::Event $( <$generic_param:ident> )? $( { $generic_instance:ident } )?, )*;
{ $( $parsed:tt )* };
) => {
$crate::impl_outer_event!(
$( #[$attr] )*;
$name;
$runtime;
Modules { $( $rest_event_generic_instance )* };
$( $module_name::Event $( <$generic_param> )? $( { $generic_instance } )?, )* $module::Event<$runtime>{ $instance },;
{ $( $parsed )* $module::Event<$runtime>{ $instance } index { $( $index )? }, };
);
};
// Instance
@@ -374,17 +374,17 @@ macro_rules! impl_outer_event {
$name:ident;
$runtime:ident;
Modules {
$module:ident $instance:ident,
$( #[codec(index = $index:tt)] )? $module:ident $instance:ident,
$( $rest_event_instance:tt )*
};
$( $module_name:ident::Event $( <$generic_param:ident> )? $( { $generic_instance:ident } )?, )*;
{ $( $parsed:tt )* };
) => {
$crate::impl_outer_event!(
$( #[$attr] )*;
$name;
$runtime;
Modules { $( $rest_event_instance )* };
$( $module_name::Event $( <$generic_param> )* $( { $generic_instance } )?, )* $module::Event { $instance },;
{ $( $parsed )* $module::Event { $instance } index { $( $index )? }, };
);
};
// Generic
@@ -393,17 +393,17 @@ macro_rules! impl_outer_event {
$name:ident;
$runtime:ident;
Modules {
$module:ident<T>,
$( #[codec(index = $index:tt)] )? $module:ident<T>,
$( $rest_event_generic:tt )*
};
$( $module_name:ident::Event $( <$generic_param:ident> )? $( { $generic_instance:ident } )?, )*;
{ $( $parsed:tt )* };
) => {
$crate::impl_outer_event!(
$( #[$attr] )*;
$name;
$runtime;
Modules { $( $rest_event_generic )* };
$( $module_name::Event $( <$generic_param> )? $( { $generic_instance } )?, )* $module::Event<$runtime>,;
{ $( $parsed )* $module::Event<$runtime> index { $( $index )? }, };
);
};
// No Generic and no Instance
@@ -412,17 +412,17 @@ macro_rules! impl_outer_event {
$name:ident;
$runtime:ident;
Modules {
$module:ident,
$( #[codec(index = $index:tt)] )? $module:ident,
$( $rest_event_no_generic_no_instance:tt )*
};
$( $module_name:ident::Event $( <$generic_param:ident> )? $( { $generic_instance:ident } )?, )*;
{ $( $parsed:tt )* };
) => {
$crate::impl_outer_event!(
$( #[$attr] )*;
$name;
$runtime;
Modules { $( $rest_event_no_generic_no_instance )* };
$( $module_name::Event $( <$generic_param> )? $( { $generic_instance } )?, )* $module::Event,;
{ $( $parsed )* $module::Event index { $( $index )? }, };
);
};
@@ -432,7 +432,14 @@ macro_rules! impl_outer_event {
$name:ident;
$runtime:ident;
Modules {};
$( $module_name:ident::Event $( <$generic_param:ident> )? $( { $generic_instance:ident } )?, )*;
{
$(
$module_name:ident::Event
$( <$generic_param:ident> )?
$( { $generic_instance:ident } )?
index { $( $index:tt )? },
)*
};
) => {
$crate::paste::item! {
#[derive(
@@ -445,6 +452,7 @@ macro_rules! impl_outer_event {
#[allow(non_camel_case_types)]
pub enum $name {
$(
$( #[codec(index = $index)] )?
[< $module_name $(_ $generic_instance )? >](
$module_name::Event < $( $generic_param )? $(, $module_name::$generic_instance )? >
),
@@ -697,7 +705,7 @@ mod tests {
pub enum TestEventSystemRenamed for TestRuntime2 {
system_renamed,
event_module<T>,
event_module2<T>,
#[codec(index = "5")] event_module2<T>,
event_module3,
}
}
@@ -796,4 +804,22 @@ mod tests {
fn outer_event_metadata() {
assert_eq!(EXPECTED_METADATA, TestRuntime::outer_event_metadata());
}
#[test]
fn test_codec() {
let runtime_1_event_module_2 = TestEvent::event_module2(
event_module2::Event::<TestRuntime>::TestEvent(3)
);
assert_eq!(runtime_1_event_module_2.encode()[0], 2);
let runtime_2_event_module_2 = TestEventSystemRenamed::event_module2(
event_module2::Event::<TestRuntime2>::TestEvent(3)
);
assert_eq!(runtime_2_event_module_2.encode()[0], 5);
let runtime_2_event_module_3 = TestEventSystemRenamed::event_module3(
event_module3::Event::HiEvent
);
assert_eq!(runtime_2_event_module_3.encode()[0], 3);
}
}
+14 -7
View File
@@ -51,9 +51,9 @@ pub use frame_metadata::{
/// struct Runtime;
/// frame_support::impl_runtime_metadata! {
/// for Runtime with modules where Extrinsic = UncheckedExtrinsic
/// module0::Module as Module0 with,
/// module1::Module as Module1 with,
/// module2::Module as Module2 with Storage,
/// module0::Module as Module0 { index 0 } with,
/// module1::Module as Module1 { index 1 } with,
/// module2::Module as Module2 { index 2 } with Storage,
/// };
/// ```
///
@@ -91,13 +91,17 @@ macro_rules! __runtime_modules_to_metadata {
(
$runtime: ident;
$( $metadata:expr ),*;
$mod:ident::$module:ident $( < $instance:ident > )? as $name:ident $(with)+ $($kw:ident)*,
$mod:ident::$module:ident $( < $instance:ident > )? as $name:ident
{ index $index:tt }
$(with)+ $($kw:ident)*
,
$( $rest:tt )*
) => {
$crate::__runtime_modules_to_metadata!(
$runtime;
$( $metadata, )* $crate::metadata::ModuleMetadata {
name: $crate::metadata::DecodeDifferent::Encode(stringify!($name)),
index: $index,
storage: $crate::__runtime_modules_to_metadata_calls_storage!(
$mod, $module $( <$instance> )?, $runtime, $(with $kw)*
),
@@ -449,9 +453,9 @@ mod tests {
impl_runtime_metadata!(
for TestRuntime with modules where Extrinsic = TestExtrinsic
system::Module as System with Event,
event_module::Module as Module with Event Call,
event_module2::Module as Module2 with Event Storage Call,
system::Module as System { index 0 } with Event,
event_module::Module as Module { index 1 } with Event Call,
event_module2::Module as Module2 { index 2 } with Event Storage Call,
);
struct ConstantBlockNumberByteGetter;
@@ -481,6 +485,7 @@ mod tests {
modules: DecodeDifferent::Encode(&[
ModuleMetadata {
name: DecodeDifferent::Encode("System"),
index: 0,
storage: None,
calls: None,
event: Some(DecodeDifferent::Encode(
@@ -524,6 +529,7 @@ mod tests {
},
ModuleMetadata {
name: DecodeDifferent::Encode("Module"),
index: 1,
storage: None,
calls: Some(
DecodeDifferent::Encode(FnEncode(|| &[
@@ -559,6 +565,7 @@ mod tests {
},
ModuleMetadata {
name: DecodeDifferent::Encode("Module2"),
index: 2,
storage: Some(DecodeDifferent::Encode(
FnEncode(|| StorageMetadata {
prefix: DecodeDifferent::Encode("TestStorage"),
+53 -10
View File
@@ -41,7 +41,10 @@ macro_rules! impl_outer_origin {
(
$(#[$attr:meta])*
pub enum $name:ident for $runtime:ident where system = $system:ident {
pub enum $name:ident for $runtime:ident where
system = $system:ident
$(, system_index = $system_index:tt)?
{
$( $rest_with_system:tt )*
}
) => {
@@ -52,6 +55,7 @@ macro_rules! impl_outer_origin {
[< $name Caller >];
$runtime;
$system;
system_index { $( $system_index )? };
Modules { $( $rest_with_system )* };
);
}
@@ -64,8 +68,9 @@ macro_rules! impl_outer_origin {
$caller_name:ident;
$runtime:ident;
$system:ident;
system_index { $( $system_index:tt )? };
Modules {
$module:ident $instance:ident <T>
$( #[codec(index = $index:tt)] )? $module:ident $instance:ident <T>
$(, $( $rest_module:tt )* )?
};
$( $parsed:tt )*
@@ -76,8 +81,9 @@ macro_rules! impl_outer_origin {
$caller_name;
$runtime;
$system;
system_index { $( $system_index )? };
Modules { $( $( $rest_module )* )? };
$( $parsed )* $module <$runtime> { $instance },
$( $parsed )* $module <$runtime> { $instance } index { $( $index )? },
);
};
@@ -88,8 +94,9 @@ macro_rules! impl_outer_origin {
$caller_name:ident;
$runtime:ident;
$system:ident;
system_index { $( $system_index:tt )? };
Modules {
$module:ident $instance:ident
$( #[codec(index = $index:tt )] )? $module:ident $instance:ident
$(, $rest_module:tt )*
};
$( $parsed:tt )*
@@ -100,8 +107,9 @@ macro_rules! impl_outer_origin {
$caller_name;
$runtime;
$system;
system_index { $( $system_index )? };
Modules { $( $rest_module )* };
$( $parsed )* $module { $instance },
$( $parsed )* $module { $instance } index { $( $index )? },
);
};
@@ -112,8 +120,9 @@ macro_rules! impl_outer_origin {
$caller_name:ident;
$runtime:ident;
$system:ident;
system_index { $( $system_index:tt )? };
Modules {
$module:ident <T>
$( #[codec(index = $index:tt )] )? $module:ident <T>
$(, $( $rest_module:tt )* )?
};
$( $parsed:tt )*
@@ -124,8 +133,9 @@ macro_rules! impl_outer_origin {
$caller_name;
$runtime;
$system;
system_index { $( $system_index )? };
Modules { $( $( $rest_module )* )? };
$( $parsed )* $module <$runtime>,
$( $parsed )* $module <$runtime> index { $( $index )? },
);
};
@@ -136,8 +146,9 @@ macro_rules! impl_outer_origin {
$caller_name:ident;
$runtime:ident;
$system:ident;
system_index { $( $system_index:tt )? };
Modules {
$module:ident
$( #[codec(index = $index:tt )] )? $module:ident
$(, $( $rest_module:tt )* )?
};
$( $parsed:tt )*
@@ -148,8 +159,9 @@ macro_rules! impl_outer_origin {
$caller_name;
$runtime;
$system;
system_index { $( $system_index )? };
Modules { $( $( $rest_module )* )? };
$( $parsed )* $module,
$( $parsed )* $module index { $( $index )? },
);
};
@@ -160,8 +172,14 @@ macro_rules! impl_outer_origin {
$caller_name:ident;
$runtime:ident;
$system:ident;
system_index { $( $system_index:tt )? };
Modules { };
$( $module:ident $( < $generic:ident > )? $( { $generic_instance:ident } )? ,)*
$(
$module:ident
$( < $generic:ident > )?
$( { $generic_instance:ident } )?
index { $( $index:tt )? },
)*
) => {
// WARNING: All instance must hold the filter `frame_system::Trait::BaseCallFilter`, except
// when caller is system Root. One can use `OriginTrait::reset_filter` to do so.
@@ -233,8 +251,10 @@ macro_rules! impl_outer_origin {
$(#[$attr])*
#[allow(non_camel_case_types)]
pub enum $caller_name {
$( #[codec(index = $system_index)] )?
system($system::Origin<$runtime>),
$(
$( #[codec(index = $index)] )?
[< $module $( _ $generic_instance )? >]
($module::Origin < $( $generic, )? $( $module::$generic_instance )? > ),
)*
@@ -442,6 +462,13 @@ mod tests {
pub enum OriginEmpty for TestRuntime where system = frame_system {}
);
impl_outer_origin!(
pub enum OriginIndices for TestRuntime where system = frame_system, system_index = "11" {
origin_with_generic<T>,
#[codec(index = "10")] origin_without_generic,
}
);
#[test]
fn test_default_filter() {
assert_eq!(OriginWithSystem::root().filter_call(&0), true);
@@ -472,4 +499,20 @@ mod tests {
assert_eq!(origin.filter_call(&0), true);
assert_eq!(origin.filter_call(&1), false);
}
#[test]
fn test_codec() {
use codec::Encode;
assert_eq!(OriginIndices::root().caller.encode()[0], 11);
let without_generic_variant = OriginIndicesCaller::origin_without_generic(
origin_without_generic::Origin
);
assert_eq!(without_generic_variant.encode()[0], 10);
assert_eq!(OriginWithoutSystem::root().caller.encode()[0], 0);
let without_generic_variant = OriginWithoutSystemCaller::origin_without_generic(
origin_without_generic::Origin
);
assert_eq!(without_generic_variant.encode()[0], 1);
}
}