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

Allow pallet in construct_runtime to have fixed index #6969

Merged
37 commits merged into from
Sep 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
3556df3
implement index for pallet + some tests
gui1117 Aug 26, 2020
766f510
add test and doc
gui1117 Sep 1, 2020
d246bdf
remove deprecated and document behavior
gui1117 Sep 1, 2020
11a3af2
update internal doc
gui1117 Sep 1, 2020
ce2d1bd
Apply suggestions from code review
gui1117 Sep 7, 2020
b56cbd7
address review
gui1117 Sep 7, 2020
da702ae
use index for all module, break construct_runtime
gui1117 Sep 8, 2020
0290c47
fix line length
gui1117 Sep 8, 2020
82e36e4
Merge remote-tracking branch 'origin/master' into gui-construct-runti…
gui1117 Sep 9, 2020
6c8e80a
implement migration helper funciton in scheduler
gui1117 Sep 9, 2020
4e5b4f8
fix start at index 0
gui1117 Sep 9, 2020
95a1eb7
Update frame/scheduler/src/lib.rs
gui1117 Sep 10, 2020
9518e5b
Merge remote-tracking branch 'origin/master' into gui-construct-runti…
gui1117 Sep 10, 2020
a830e2f
Update frame/support/procedural/src/lib.rs
gui1117 Sep 16, 2020
9812da2
bump frame-metadata crate
gui1117 Sep 16, 2020
5d86201
factorize
gui1117 Sep 16, 2020
078a543
avoid some unwrap and remove nightly join
gui1117 Sep 16, 2020
4bba712
Update frame/support/src/event.rs
gui1117 Sep 16, 2020
c6f073b
fix test
gui1117 Sep 16, 2020
43856fd
add test and improve error message
gui1117 Sep 16, 2020
7da2738
factorize test
gui1117 Sep 16, 2020
b9fca7e
keep iterator, and use slice instead of vec
gui1117 Sep 16, 2020
61019e7
refactor to avoid to have expects
gui1117 Sep 16, 2020
a9d454a
Merge remote-tracking branch 'origin/master' into gui-construct-runti…
gui1117 Sep 16, 2020
ae0f859
small refactor
gui1117 Sep 16, 2020
85a65f2
Test something
bkchr Sep 17, 2020
ab0c998
Make sure we update the `Cargo.lock`
bkchr Sep 17, 2020
fda8a1a
Apply suggestions from code review
gui1117 Sep 18, 2020
95ef421
return 2 error
gui1117 Sep 18, 2020
68b472e
Apply suggestions from code review
gui1117 Sep 21, 2020
21510d4
Merge remote-tracking branch 'origin/master' into gui-construct-runti…
gui1117 Sep 21, 2020
6feb460
Update frame/scheduler/src/lib.rs
gui1117 Sep 21, 2020
f2de8f2
fix typo
gui1117 Sep 21, 2020
3f38e1a
Revert "fix typo"
gui1117 Sep 21, 2020
492a83f
Revert "Update frame/scheduler/src/lib.rs"
gui1117 Sep 21, 2020
d04f8cd
Merge remote-tracking branch 'origin/master' into gui-construct-runti…
gui1117 Sep 22, 2020
986155b
Merge remote-tracking branch 'origin/master' into gui-construct-runti…
gui1117 Sep 22, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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