Implements pallet versioning (#7208)

* Start

* Make macro work

* Rename `ModuleToIndex` to `PalletRuntimeSetup`

Besides the renaming it also adds support getting the name of a pallet
as configured in the runtime.

* Rename it to `PalletInfo`

* Remove accidentally added files

* Some work

* Make everything compile

* Adds a test and fixes some bugs

* Implement ordering for `PalletVersion`

* Apply suggestions from code review

* Review feedback

* Update frame/support/src/dispatch.rs

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

* Update frame/support/src/dispatch.rs

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

* Fix compilation

* Fix test

* Fix doc test

Co-authored-by: Alexander Popiak <alexander.popiak@parity.io>
Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>
This commit is contained in:
Bastian Köcher
2020-10-21 19:05:52 +02:00
committed by GitHub
parent 8cebbd142d
commit ed1d0fa815
30 changed files with 640 additions and 161 deletions
@@ -129,6 +129,7 @@ impl system::Trait for Runtime {
type Event = Event;
type PalletInfo = PalletInfo;
type Call = Call;
type DbWeight = ();
}
frame_support::construct_runtime!(
@@ -22,16 +22,12 @@ mod tests {
use frame_support::metadata::*;
use sp_io::TestExternalities;
use std::marker::PhantomData;
use codec::{Encode, Decode, EncodeLike};
frame_support::decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin, system=self {}
pub struct Module<T: Trait> for enum Call where origin: T::Origin, system=frame_support_test {}
}
pub trait Trait {
type Origin: Encode + Decode + EncodeLike + std::default::Default;
type BlockNumber;
}
pub trait Trait: frame_support_test::Trait {}
frame_support::decl_storage! {
trait Store for Module<T: Trait> as TestStorage {
@@ -74,7 +70,7 @@ mod tests {
pub PUBGETMAPU32MYDEF get(fn pub_map_u32_getter_mydef):
map hasher(blake2_128_concat) u32 => String = "pubmap".into();
COMPLEXTYPE1: ::std::vec::Vec<<T as Trait>::Origin>;
COMPLEXTYPE1: ::std::vec::Vec<T::Origin>;
COMPLEXTYPE2: (Vec<Vec<(u16, Box<()>)>>, u32);
COMPLEXTYPE3: [u32; 25];
}
@@ -85,11 +81,15 @@ mod tests {
struct TraitImpl {}
impl Trait for TraitImpl {
impl frame_support_test::Trait for TraitImpl {
type Origin = u32;
type BlockNumber = u32;
type PalletInfo = ();
type DbWeight = ();
}
impl Trait for TraitImpl {}
const EXPECTED_METADATA: StorageMetadata = StorageMetadata {
prefix: DecodeDifferent::Encode("TestStorage"),
entries: DecodeDifferent::Encode(
@@ -353,7 +353,7 @@ mod tests {
StorageEntryMetadata {
name: DecodeDifferent::Encode("COMPLEXTYPE1"),
modifier: StorageEntryModifier::Default,
ty: StorageEntryType::Plain(DecodeDifferent::Encode("::std::vec::Vec<<T as Trait>::Origin>")),
ty: StorageEntryType::Plain(DecodeDifferent::Encode("::std::vec::Vec<T::Origin>")),
default: DecodeDifferent::Encode(
DefaultByteGetter(&__GetByteStructCOMPLEXTYPE1(PhantomData::<TraitImpl>))
),
@@ -414,13 +414,10 @@ mod tests {
#[cfg(test)]
#[allow(dead_code)]
mod test2 {
pub trait Trait {
type Origin;
type BlockNumber;
}
pub trait Trait: frame_support_test::Trait {}
frame_support::decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin, system=self {}
pub struct Module<T: Trait> for enum Call where origin: T::Origin, system=frame_support_test {}
}
type PairOf<T> = (T, T);
@@ -441,21 +438,23 @@ mod test2 {
struct TraitImpl {}
impl Trait for TraitImpl {
impl frame_support_test::Trait for TraitImpl {
type Origin = u32;
type BlockNumber = u32;
type PalletInfo = ();
type DbWeight = ();
}
impl Trait for TraitImpl {}
}
#[cfg(test)]
#[allow(dead_code)]
mod test3 {
pub trait Trait {
type Origin;
type BlockNumber;
}
pub trait Trait: frame_support_test::Trait {}
frame_support::decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin, system=self {}
pub struct Module<T: Trait> for enum Call where origin: T::Origin, system=frame_support_test {}
}
frame_support::decl_storage! {
trait Store for Module<T: Trait> as Test {
@@ -467,10 +466,14 @@ mod test3 {
struct TraitImpl {}
impl Trait for TraitImpl {
impl frame_support_test::Trait for TraitImpl {
type Origin = u32;
type BlockNumber = u32;
type PalletInfo = ();
type DbWeight = ();
}
impl Trait for TraitImpl {}
}
#[cfg(test)]
@@ -479,13 +482,10 @@ mod test_append_and_len {
use sp_io::TestExternalities;
use codec::{Encode, Decode};
pub trait Trait {
type Origin;
type BlockNumber;
}
pub trait Trait: frame_support_test::Trait {}
frame_support::decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin, system=self {}
pub struct Module<T: Trait> for enum Call where origin: T::Origin, system=frame_support_test {}
}
#[derive(PartialEq, Eq, Clone, Encode, Decode)]
@@ -511,11 +511,15 @@ mod test_append_and_len {
struct Test {}
impl Trait for Test {
impl frame_support_test::Trait for Test {
type Origin = u32;
type BlockNumber = u32;
type PalletInfo = ();
type DbWeight = ();
}
impl Trait for Test {}
#[test]
fn default_for_option() {
TestExternalities::default().execute_with(|| {
@@ -15,13 +15,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.
pub trait Trait {
type Origin;
type BlockNumber: codec::Codec + codec::EncodeLike + Default + Clone;
}
pub trait Trait: frame_support_test::Trait {}
frame_support::decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin, system=self {}
pub struct Module<T: Trait> for enum Call where origin: T::Origin, system=frame_support_test {}
}
frame_support::decl_storage!{
@@ -1,5 +1,5 @@
error: `config()`/`get()` with the same name already defined.
--> $DIR/config_duplicate.rs:30:21
--> $DIR/config_duplicate.rs:27:21
|
30 | pub Value2 config(value): u32;
27 | pub Value2 config(value): u32;
| ^^^^^
@@ -15,13 +15,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.
pub trait Trait {
type Origin;
type BlockNumber: codec::Codec + codec::EncodeLike + Default + Clone;
}
pub trait Trait: frame_support_test::Trait {}
frame_support::decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin, system=self {}
pub struct Module<T: Trait> for enum Call where origin: T::Origin, system=frame_support_test {}
}
frame_support::decl_storage!{
@@ -1,5 +1,5 @@
error: `config()`/`get()` with the same name already defined.
--> $DIR/config_get_duplicate.rs:30:21
--> $DIR/config_get_duplicate.rs:27:21
|
30 | pub Value2 config(value): u32;
27 | pub Value2 config(value): u32;
| ^^^^^
@@ -15,13 +15,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.
pub trait Trait {
type Origin;
type BlockNumber: codec::Codec + codec::EncodeLike + Default + Clone;
}
pub trait Trait: frame_support_test::Trait {}
frame_support::decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin, system=self {}
pub struct Module<T: Trait> for enum Call where origin: T::Origin, system=frame_support_test {}
}
frame_support::decl_storage!{
@@ -1,5 +1,5 @@
error: `config()`/`get()` with the same name already defined.
--> $DIR/get_duplicate.rs:30:21
--> $DIR/get_duplicate.rs:27:21
|
30 | pub Value2 get(fn value) config(): u32;
27 | pub Value2 get(fn value) config(): u32;
| ^^^^^
@@ -21,15 +21,10 @@ use frame_support::{StorageDoubleMap, StorageMap, StorageValue, StoragePrefixedM
use sp_io::{TestExternalities, hashing::{twox_64, twox_128, blake2_128}};
mod no_instance {
use codec::{Encode, Decode, EncodeLike};
pub trait Trait {
type Origin;
type BlockNumber: Encode + Decode + EncodeLike + Default + Clone;
}
pub trait Trait: frame_support_test::Trait {}
frame_support::decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin, system=self {}
pub struct Module<T: Trait> for enum Call where origin: T::Origin, system=frame_support_test {}
}
frame_support::decl_storage!{
@@ -50,13 +45,11 @@ mod no_instance {
}
mod instance {
use super::no_instance;
pub trait Trait<I = DefaultInstance>: super::no_instance::Trait {}
pub trait Trait<I = DefaultInstance>: frame_support_test::Trait {}
frame_support::decl_module! {
pub struct Module<T: Trait<I>, I: Instance = DefaultInstance>
for enum Call where origin: T::Origin, system=no_instance {}
for enum Call where origin: T::Origin, system=frame_support_test {}
}
frame_support::decl_storage!{
@@ -15,13 +15,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.
pub trait Trait {
type BlockNumber: codec::Codec + codec::EncodeLike + Default;
type Origin;
}
pub trait Trait: frame_support_test::Trait {}
frame_support::decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin, system=self {}
pub struct Module<T: Trait> for enum Call where origin: T::Origin, system=frame_support_test {}
}
frame_support::decl_storage! {
@@ -32,11 +29,15 @@ frame_support::decl_storage! {
struct Test;
impl Trait for Test {
impl frame_support_test::Trait for Test {
type BlockNumber = u32;
type Origin = ();
type PalletInfo = ();
type DbWeight = ();
}
impl Trait for Test {}
#[test]
fn init_genesis_config() {
GenesisConfig::<Test> {
@@ -250,6 +250,7 @@ impl system::Trait for Runtime {
type Event = Event;
type PalletInfo = ();
type Call = Call;
type DbWeight = ();
}
frame_support::construct_runtime!(
@@ -166,6 +166,7 @@ impl system::Trait for Runtime {
type Event = Event;
type PalletInfo = ();
type Call = Call;
type DbWeight = ();
}
impl module::Trait for Runtime {}
@@ -0,0 +1,175 @@
// This file is part of Substrate.
// Copyright (C) 2020 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Tests related to the pallet version.
#![recursion_limit="128"]
use codec::{Decode, Encode};
use sp_runtime::{generic, traits::{BlakeTwo256, Block as _, Verify}};
use frame_support::{
traits::{PALLET_VERSION_STORAGE_KEY_POSTFIX, PalletVersion, OnRuntimeUpgrade},
crate_to_pallet_version, weights::Weight,
};
use sp_core::{H256, sr25519};
mod system;
/// A version that we will check for in the tests
const SOME_TEST_VERSION: PalletVersion = PalletVersion { major: 3000, minor: 30, patch: 13 };
/// Checks that `on_runtime_upgrade` sets the latest pallet version when being called without
/// being provided by the user.
mod module1 {
use super::*;
pub trait Trait: system::Trait {}
frame_support::decl_module! {
pub struct Module<T: Trait> for enum Call where
origin: <T as system::Trait>::Origin,
system = system,
{}
}
}
/// Checks that `on_runtime_upgrade` sets the latest pallet version when being called and also
/// being provided by the user.
mod module2 {
use super::*;
pub trait Trait<I=DefaultInstance>: system::Trait {}
frame_support::decl_module! {
pub struct Module<T: Trait<I>, I: Instance=DefaultInstance> for enum Call where
origin: <T as system::Trait>::Origin,
system = system
{
fn on_runtime_upgrade() -> Weight {
assert_eq!(crate_to_pallet_version!(), Self::current_version());
let version_key = PalletVersion::storage_key::<T::PalletInfo, Self>().unwrap();
let version_value = sp_io::storage::get(&version_key);
if version_value.is_some() {
assert_eq!(SOME_TEST_VERSION, Self::storage_version().unwrap());
} else {
// As the storage version does not exist yet, it should be `None`.
assert!(Self::storage_version().is_none());
}
0
}
}
}
frame_support::decl_storage! {
trait Store for Module<T: Trait<I>, I: Instance=DefaultInstance> as Module2 {}
}
}
impl module1::Trait for Runtime {}
impl module2::Trait for Runtime {}
impl module2::Trait<module2::Instance1> for Runtime {}
impl module2::Trait<module2::Instance2> for Runtime {}
pub type Signature = sr25519::Signature;
pub type AccountId = <Signature as Verify>::Signer;
pub type BlockNumber = u64;
pub type Index = u64;
impl system::Trait for Runtime {
type BaseCallFilter= ();
type Hash = H256;
type Origin = Origin;
type BlockNumber = BlockNumber;
type AccountId = AccountId;
type Event = Event;
type PalletInfo = PalletInfo;
type Call = Call;
type DbWeight = ();
}
frame_support::construct_runtime!(
pub enum Runtime where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic
{
System: system::{Module, Call, Event<T>},
Module1: module1::{Module, Call},
Module2: module2::{Module, Call},
Module2_1: module2::<Instance1>::{Module, Call},
Module2_2: module2::<Instance2>::{Module, Call},
}
);
pub type Header = generic::Header<BlockNumber, BlakeTwo256>;
pub type Block = generic::Block<Header, UncheckedExtrinsic>;
pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<u32, Call, Signature, ()>;
/// Returns the storage key for `PalletVersion` for the given `pallet`.
fn get_pallet_version_storage_key_for_pallet(pallet: &str) -> [u8; 32] {
let pallet_name = sp_io::hashing::twox_128(pallet.as_bytes());
let postfix = sp_io::hashing::twox_128(PALLET_VERSION_STORAGE_KEY_POSTFIX);
let mut final_key = [0u8; 32];
final_key[..16].copy_from_slice(&pallet_name);
final_key[16..].copy_from_slice(&postfix);
final_key
}
/// Checks the version of the given `pallet`.
///
/// It is expected that the pallet version can be found in the storage and equals the
/// current crate version.
fn check_pallet_version(pallet: &str) {
let key = get_pallet_version_storage_key_for_pallet(pallet);
let value = sp_io::storage::get(&key).expect("Pallet version exists");
let version = PalletVersion::decode(&mut &value[..])
.expect("Pallet version is encoded correctly");
assert_eq!(crate_to_pallet_version!(), version);
}
#[test]
fn on_runtime_upgrade_sets_the_pallet_versions_in_storage() {
sp_io::TestExternalities::new_empty().execute_with(|| {
AllModules::on_runtime_upgrade();
check_pallet_version("Module1");
check_pallet_version("Module2");
check_pallet_version("Module2_1");
check_pallet_version("Module2_2");
});
}
#[test]
fn on_runtime_upgrade_overwrites_old_version() {
sp_io::TestExternalities::new_empty().execute_with(|| {
let key = get_pallet_version_storage_key_for_pallet("Module2");
sp_io::storage::set(&key, &SOME_TEST_VERSION.encode());
AllModules::on_runtime_upgrade();
check_pallet_version("Module1");
check_pallet_version("Module2");
check_pallet_version("Module2_1");
check_pallet_version("Module2_2");
});
}
@@ -4,10 +4,7 @@ macro_rules! reserved {
mod $reserved {
pub use frame_support::dispatch;
pub trait Trait {
type Origin;
type BlockNumber: Into<u32>;
}
pub trait Trait: frame_support_test::Trait {}
pub mod system {
use frame_support::dispatch;
@@ -18,7 +15,7 @@ macro_rules! reserved {
}
frame_support::decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin, system=self {
pub struct Module<T: Trait> for enum Call where origin: T::Origin, system=frame_support_test {
#[weight = 0]
fn $reserved(_origin) -> dispatch::DispatchResult { unreachable!() }
}
@@ -1,39 +1,39 @@
error: Invalid call fn name: `on_finalize`, name is reserved and doesn't match expected signature, please refer to `decl_module!` documentation to see the appropriate usage, or rename it to an unreserved keyword.
--> $DIR/on_initialize.rs:31:1
--> $DIR/on_initialize.rs:28:1
|
31 | reserved!(on_finalize on_initialize on_runtime_upgrade offchain_worker deposit_event);
28 | reserved!(on_finalize on_initialize on_runtime_upgrade offchain_worker deposit_event);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
error: Invalid call fn name: `on_initialize`, name is reserved and doesn't match expected signature, please refer to `decl_module!` documentation to see the appropriate usage, or rename it to an unreserved keyword.
--> $DIR/on_initialize.rs:31:1
--> $DIR/on_initialize.rs:28:1
|
31 | reserved!(on_finalize on_initialize on_runtime_upgrade offchain_worker deposit_event);
28 | reserved!(on_finalize on_initialize on_runtime_upgrade offchain_worker deposit_event);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
error: Invalid call fn name: `on_runtime_upgrade`, name is reserved and doesn't match expected signature, please refer to `decl_module!` documentation to see the appropriate usage, or rename it to an unreserved keyword.
--> $DIR/on_initialize.rs:31:1
--> $DIR/on_initialize.rs:28:1
|
31 | reserved!(on_finalize on_initialize on_runtime_upgrade offchain_worker deposit_event);
28 | reserved!(on_finalize on_initialize on_runtime_upgrade offchain_worker deposit_event);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
error: Invalid call fn name: `offchain_worker`, name is reserved and doesn't match expected signature, please refer to `decl_module!` documentation to see the appropriate usage, or rename it to an unreserved keyword.
--> $DIR/on_initialize.rs:31:1
--> $DIR/on_initialize.rs:28:1
|
31 | reserved!(on_finalize on_initialize on_runtime_upgrade offchain_worker deposit_event);
28 | reserved!(on_finalize on_initialize on_runtime_upgrade offchain_worker deposit_event);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
error: Invalid call fn name: `deposit_event`, name is reserved and doesn't match expected signature, please refer to `decl_module!` documentation to see the appropriate usage, or rename it to an unreserved keyword.
--> $DIR/on_initialize.rs:31:1
--> $DIR/on_initialize.rs:28:1
|
31 | reserved!(on_finalize on_initialize on_runtime_upgrade offchain_worker deposit_event);
28 | reserved!(on_finalize on_initialize on_runtime_upgrade offchain_worker deposit_event);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
@@ -15,23 +15,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use codec::{Encode, Decode, EncodeLike};
use frame_support::{
assert_ok, assert_noop, transactional,
StorageMap, StorageValue,
dispatch::{DispatchError, DispatchResult},
storage::{with_transaction, TransactionOutcome::*},
assert_ok, assert_noop, transactional, StorageMap, StorageValue,
dispatch::{DispatchError, DispatchResult}, storage::{with_transaction, TransactionOutcome::*},
};
use sp_io::TestExternalities;
use sp_std::result;
pub trait Trait {
type Origin;
type BlockNumber: Encode + Decode + EncodeLike + Default + Clone;
}
pub trait Trait: frame_support_test::Trait {}
frame_support::decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin, system=self {
pub struct Module<T: Trait> for enum Call where origin: T::Origin, system=frame_support_test {
#[weight = 0]
#[transactional]
fn value_commits(_origin, v: u32) {
@@ -55,11 +49,16 @@ frame_support::decl_storage!{
}
struct Runtime;
impl Trait for Runtime {
impl frame_support_test::Trait for Runtime {
type Origin = u32;
type BlockNumber = u32;
type PalletInfo = ();
type DbWeight = ();
}
impl Trait for Runtime {}
#[test]
fn storage_transaction_basic_commit() {
TestExternalities::default().execute_with(|| {
+4 -1
View File
@@ -15,7 +15,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.
use frame_support::codec::{Encode, Decode, EncodeLike};
use frame_support::{
codec::{Encode, Decode, EncodeLike}, traits::Get, weights::RuntimeDbWeight,
};
pub trait Trait: 'static + Eq + Clone {
type Origin: Into<Result<RawOrigin<Self::AccountId>, Self::Origin>>
@@ -28,6 +30,7 @@ pub trait Trait: 'static + Eq + Clone {
type Call;
type Event: From<Event<Self>>;
type PalletInfo: frame_support::traits::PalletInfo;
type DbWeight: Get<RuntimeDbWeight>;
}
frame_support::decl_module! {