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
+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);
}
}