Skip to content

Commit

Permalink
Allow pallet in construct_runtime to have fixed index (#6969)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* 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 <[email protected]>

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

Co-authored-by: Bastian Köcher <[email protected]>

* bump frame-metadata crate

* factorize

* avoid some unwrap and remove nightly join

* Update frame/support/src/event.rs

Co-authored-by: Bastian Köcher <[email protected]>

* 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 <[email protected]>

* return 2 error

* Apply suggestions from code review

Co-authored-by: Alexander Popiak <[email protected]>

* Update frame/scheduler/src/lib.rs

Co-authored-by: Kian Paimani <[email protected]>

* 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 <[email protected]>
Co-authored-by: Shawn Tabrizi <[email protected]>
Co-authored-by: Bastian Köcher <[email protected]>
Co-authored-by: Alexander Popiak <[email protected]>
Co-authored-by: Kian Paimani <[email protected]>
  • Loading branch information
6 people authored Sep 22, 2020
1 parent 5ca3777 commit c1e5905
Show file tree
Hide file tree
Showing 21 changed files with 874 additions and 126 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion frame/metadata/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "frame-metadata"
version = "11.0.0-rc6"
version = "12.0.0-rc6"
authors = ["Parity Technologies <[email protected]>"]
edition = "2018"
license = "Apache-2.0"
Expand Down
15 changes: 10 additions & 5 deletions frame/metadata/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,8 +362,10 @@ pub enum RuntimeMetadata {
V9(RuntimeMetadataDeprecated),
/// Version 10 for runtime metadata. No longer used.
V10(RuntimeMetadataDeprecated),
/// Version 11 for runtime metadata.
V11(RuntimeMetadataV11),
/// Version 11 for runtime metadata. No longer used.
V11(RuntimeMetadataDeprecated),
/// Version 12 for runtime metadata.
V12(RuntimeMetadataV12),
}

/// Enum that should fail.
Expand All @@ -387,15 +389,15 @@ impl Decode for RuntimeMetadataDeprecated {
/// The metadata of a runtime.
#[derive(Eq, Encode, PartialEq, RuntimeDebug)]
#[cfg_attr(feature = "std", derive(Decode, Serialize))]
pub struct RuntimeMetadataV11 {
pub struct RuntimeMetadataV12 {
/// Metadata of all the modules.
pub modules: DecodeDifferentArray<ModuleMetadata>,
/// Metadata of the extrinsic.
pub extrinsic: ExtrinsicMetadata,
}

/// The latest version of the metadata.
pub type RuntimeMetadataLastVersion = RuntimeMetadataV11;
pub type RuntimeMetadataLastVersion = RuntimeMetadataV12;

/// All metadata about an runtime module.
#[derive(Clone, PartialEq, Eq, Encode, RuntimeDebug)]
Expand All @@ -407,6 +409,9 @@ pub struct ModuleMetadata {
pub event: ODFnA<EventMetadata>,
pub constants: DFnA<ModuleConstantMetadata>,
pub errors: DFnA<ErrorMetadata>,
/// Define the index of the module, this index will be used for the encoding of module event,
/// call and origin variants.
pub index: u8,
}

type ODFnA<T> = Option<DFnA<T>>;
Expand All @@ -420,6 +425,6 @@ impl Into<sp_core::OpaqueMetadata> for RuntimeMetadataPrefixed {

impl Into<RuntimeMetadataPrefixed> for RuntimeMetadataLastVersion {
fn into(self) -> RuntimeMetadataPrefixed {
RuntimeMetadataPrefixed(META_RESERVED, RuntimeMetadata::V11(self))
RuntimeMetadataPrefixed(META_RESERVED, RuntimeMetadata::V12(self))
}
}
136 changes: 134 additions & 2 deletions frame/scheduler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,25 @@ impl<T: Trait> Module<T> {
}
}

/// Helper to migrate scheduler when the pallet origin type has changed.
pub fn migrate_origin<OldOrigin: Into<T::PalletsOrigin> + codec::Decode>() {
Agenda::<T>::translate::<
Vec<Option<Scheduled<<T as Trait>::Call, T::BlockNumber, OldOrigin, T::AccountId>>>, _
>(|_, agenda| Some(
agenda
.into_iter()
.map(|schedule| schedule.map(|schedule| Scheduled {
maybe_id: schedule.maybe_id,
priority: schedule.priority,
call: schedule.call,
maybe_periodic: schedule.maybe_periodic,
origin: schedule.origin.into(),
_phantom: Default::default(),
}))
.collect::<Vec<_>>()
));
}

fn do_schedule(
when: DispatchTime<T::BlockNumber>,
maybe_periodic: Option<schedule::Period<T::BlockNumber>>,
Expand Down Expand Up @@ -632,6 +651,7 @@ mod tests {
traits::{BlakeTwo256, IdentityLookup},
};
use frame_system::{EnsureOneOf, EnsureRoot, EnsureSignedBy};
use substrate_test_utils::assert_eq_uvec;
use crate as scheduler;

mod logger {
Expand Down Expand Up @@ -1161,8 +1181,6 @@ mod tests {

#[test]
fn migration_to_v2_works() {
use substrate_test_utils::assert_eq_uvec;

new_test_ext().execute_with(|| {
for i in 0..3u64 {
let k = i.twox_64_concat();
Expand Down Expand Up @@ -1264,4 +1282,118 @@ mod tests {
assert_eq!(StorageVersion::get(), Releases::V2);
});
}

#[test]
fn test_migrate_origin() {
new_test_ext().execute_with(|| {
for i in 0..3u64 {
let k = i.twox_64_concat();
let old: Vec<Option<Scheduled<_, _, u32, u64>>> = vec![
Some(Scheduled {
maybe_id: None,
priority: i as u8 + 10,
call: Call::Logger(logger::Call::log(96, 100)),
origin: 3u32,
maybe_periodic: None,
_phantom: Default::default(),
}),
None,
Some(Scheduled {
maybe_id: Some(b"test".to_vec()),
priority: 123,
origin: 2u32,
call: Call::Logger(logger::Call::log(69, 1000)),
maybe_periodic: Some((456u64, 10)),
_phantom: Default::default(),
}),
];
frame_support::migration::put_storage_value(
b"Scheduler",
b"Agenda",
&k,
old,
);
}

impl Into<OriginCaller> for u32 {
fn into(self) -> OriginCaller {
match self {
3u32 => system::RawOrigin::Root.into(),
2u32 => system::RawOrigin::None.into(),
_ => unreachable!("test make no use of it"),
}
}
}

Scheduler::migrate_origin::<u32>();

assert_eq_uvec!(Agenda::<Test>::iter().collect::<Vec<_>>(), vec![
(
0,
vec![
Some(ScheduledV2::<_, _, OriginCaller, u64> {
maybe_id: None,
priority: 10,
call: Call::Logger(logger::Call::log(96, 100)),
maybe_periodic: None,
origin: system::RawOrigin::Root.into(),
_phantom: PhantomData::<u64>::default(),
}),
None,
Some(ScheduledV2 {
maybe_id: Some(b"test".to_vec()),
priority: 123,
call: Call::Logger(logger::Call::log(69, 1000)),
maybe_periodic: Some((456u64, 10)),
origin: system::RawOrigin::None.into(),
_phantom: PhantomData::<u64>::default(),
}),
]),
(
1,
vec![
Some(ScheduledV2 {
maybe_id: None,
priority: 11,
call: Call::Logger(logger::Call::log(96, 100)),
maybe_periodic: None,
origin: system::RawOrigin::Root.into(),
_phantom: PhantomData::<u64>::default(),
}),
None,
Some(ScheduledV2 {
maybe_id: Some(b"test".to_vec()),
priority: 123,
call: Call::Logger(logger::Call::log(69, 1000)),
maybe_periodic: Some((456u64, 10)),
origin: system::RawOrigin::None.into(),
_phantom: PhantomData::<u64>::default(),
}),
]
),
(
2,
vec![
Some(ScheduledV2 {
maybe_id: None,
priority: 12,
call: Call::Logger(logger::Call::log(96, 100)),
maybe_periodic: None,
origin: system::RawOrigin::Root.into(),
_phantom: PhantomData::<u64>::default(),
}),
None,
Some(ScheduledV2 {
maybe_id: Some(b"test".to_vec()),
priority: 123,
call: Call::Logger(logger::Call::log(69, 1000)),
maybe_periodic: Some((456u64, 10)),
origin: system::RawOrigin::None.into(),
_phantom: PhantomData::<u64>::default(),
}),
]
)
]);
});
}
}
2 changes: 1 addition & 1 deletion frame/support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ targets = ["x86_64-unknown-linux-gnu"]
log = "0.4"
serde = { version = "1.0.101", optional = true, features = ["derive"] }
codec = { package = "parity-scale-codec", version = "1.3.1", default-features = false, features = ["derive"] }
frame-metadata = { version = "11.0.0-rc6", default-features = false, path = "../metadata" }
frame-metadata = { version = "12.0.0-rc6", default-features = false, path = "../metadata" }
sp-std = { version = "2.0.0-rc6", default-features = false, path = "../../primitives/std" }
sp-io = { version = "2.0.0-rc6", default-features = false, path = "../../primitives/io" }
sp-runtime = { version = "2.0.0-rc6", default-features = false, path = "../../primitives/runtime" }
Expand Down
Loading

0 comments on commit c1e5905

Please sign in to comment.