Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support STORAGE_VERSION for our pallets #726

Merged
merged 12 commits into from
Aug 6, 2022
10 changes: 8 additions & 2 deletions pallets/asset-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ mod benchmarking;
pub mod migrations;
pub mod weights;
pub use crate::weights::WeightInfo;

pub use pallet::*;

#[cfg(test)]
Expand All @@ -44,7 +43,11 @@ mod tests;
pub mod pallet {

use crate::weights::WeightInfo;
use frame_support::{pallet_prelude::*, traits::Contains, transactional, PalletId};
use frame_support::{
pallet_prelude::*,
traits::{Contains, StorageVersion},
transactional, PalletId,
};
use frame_system::pallet_prelude::*;
use manta_primitives::{
assets::{
Expand All @@ -60,13 +63,16 @@ pub mod pallet {
};
use xcm::latest::prelude::*;

const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);

/// Alias for the junction Parachain(#[codec(compact)] u32),
pub(crate) type ParaId = u32;
pub(crate) type AssetCount = u32;

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::without_storage_info]
#[pallet::storage_version(STORAGE_VERSION)]
pub struct Pallet<T>(_);

/// Convert AssetId and AssetLocation
Expand Down
5 changes: 4 additions & 1 deletion pallets/manta-pay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,16 @@ pub mod runtime;
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_support::{pallet_prelude::*, traits::StorageVersion};
use frame_system::pallet_prelude::*;
use sp_runtime::traits::AccountIdConversion;

const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);
flame4 marked this conversation as resolved.
Show resolved Hide resolved

/// Pallet
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::storage_version(STORAGE_VERSION)]
pub struct Pallet<T>(_);

/// The module configuration trait.
Expand Down
4 changes: 4 additions & 0 deletions pallets/tx-pause/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ pub use weights::WeightInfo;
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::traits::StorageVersion;

const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);

#[pallet::config]
pub trait Config: frame_system::Config {
Expand Down Expand Up @@ -82,6 +85,7 @@ pub mod pallet {

#[pallet::pallet]
#[pallet::without_storage_info]
#[pallet::storage_version(STORAGE_VERSION)]
pub struct Pallet<T>(_);

#[pallet::hooks]
Expand Down
4 changes: 4 additions & 0 deletions pallets/vesting/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ const VESTING_ID: LockIdentifier = *b"calamvst";
#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::traits::StorageVersion;

const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);

#[pallet::config]
pub trait Config: frame_system::Config {
Expand Down Expand Up @@ -111,6 +114,7 @@ pub mod pallet {
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::without_storage_info]
#[pallet::storage_version(STORAGE_VERSION)]
pub struct Pallet<T>(_);

#[pallet::event]
Expand Down
1 change: 1 addition & 0 deletions runtime/calamari/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ try-runtime = [
'pallet-assets/try-runtime',
'pallet-tx-pause/try-runtime',
'pallet-asset-manager/try-runtime',
'runtime-common/try-runtime',
]
# Set timing constants (e.g. session period) to faster versions to speed up testing.
fast-runtime = []
Expand Down
14 changes: 13 additions & 1 deletion runtime/calamari/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub mod xcm_config;
use currency::*;
use fee::WeightToFee;
use impls::DealWithFees;
use runtime_common::migration::MigratePalletPv2Sv;

pub type NegativeImbalance = <Balances as Currency<AccountId>>::NegativeImbalance;

Expand Down Expand Up @@ -775,14 +776,25 @@ pub type SignedExtra = (
pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<Address, Call, Signature, SignedExtra>;
/// Extrinsic type that has already been checked.
pub type CheckedExtrinsic = generic::CheckedExtrinsic<AccountId, Call, SignedExtra>;

/// Types for runtime upgrading.
/// Each type should implement trait `OnRuntimeUpgrade`.
pub type OnRuntimeUpgradeHooks = (
UpgradeSessionKeys,
MigratePalletPv2Sv<pallet_asset_manager::Pallet<Runtime>>,
MigratePalletPv2Sv<pallet_tx_pause::Pallet<Runtime>>,
MigratePalletPv2Sv<manta_collator_selection::Pallet<Runtime>>,
MigratePalletPv2Sv<calamari_vesting::Pallet<Runtime>>,
);

/// Executive: handles dispatch to the various modules.
pub type Executive = frame_executive::Executive<
Runtime,
Block,
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsReversedWithSystemFirst,
UpgradeSessionKeys,
OnRuntimeUpgradeHooks,
>;
// When this is removed, should also remove `OldSessionKeys`.
pub struct UpgradeSessionKeys;
Expand Down
5 changes: 5 additions & 0 deletions runtime/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ version = '3.2.1'
frame-support = { git = 'https://github.com/paritytech/substrate.git', branch = "polkadot-v0.9.26", default-features = false }
pallet-transaction-payment = { git = 'https://github.com/paritytech/substrate.git', branch = "polkadot-v0.9.26", default-features = false }
sp-runtime = { git = 'https://github.com/paritytech/substrate.git', branch = "polkadot-v0.9.26", default-features = false }
sp-std = { git = 'https://github.com/paritytech/substrate.git', branch = "polkadot-v0.9.26", default-features = false }
flame4 marked this conversation as resolved.
Show resolved Hide resolved

# Self dependencies
manta-primitives = { path = '../../primitives/manta', default-features = false }
Expand Down Expand Up @@ -63,4 +64,8 @@ std = [
"manta-primitives/std",
"pallet-transaction-payment/std",
"sp-runtime/std",
"sp-std/std",
]
try-runtime = [
"frame-support/try-runtime",
]
2 changes: 2 additions & 0 deletions runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#![allow(clippy::upper_case_acronyms)]
#![cfg_attr(not(feature = "std"), no_std)]

pub mod migration;

use frame_support::parameter_types;
use manta_primitives::types::BlockNumber;
use pallet_transaction_payment::{Multiplier, TargetedFeeAdjustment};
Expand Down
156 changes: 156 additions & 0 deletions runtime/common/src/migration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// Copyright 2020-2022 Manta Network.
// This file is part of Manta.
//
// Manta is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Manta is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Manta. If not, see <http://www.gnu.org/licenses/>.

//! Migration creates some helper function to make storage migration more convenient.

#![cfg_attr(not(feature = "std"), no_std)]

use frame_support::{
dispatch::Weight,
ensure,
migrations::migrate_from_pallet_version_to_storage_version,
traits::{GetStorageVersion, OnRuntimeUpgrade, PalletInfoAccess, StorageVersion},
weights::constants::RocksDbWeight,
};

use sp_std::marker::PhantomData;

/// MigratePalletPv2Sv means a wrapped handler to automatically upgrade our pallet
/// from PalletVersion(Pv) to StorageVersion(Sv).
///
/// It's actually a simple rewriting about storage flag: delete [pallet_name] + '__STORAGE_VERSION__' key
/// and reset [pallet_name] + '__PALLET_VERSION__' key.
/// So It's a one-time job, and should be removed soon to minimize runtime size.
pub struct MigratePalletPv2Sv<T>(PhantomData<T>);

impl<T> OnRuntimeUpgrade for MigratePalletPv2Sv<T>
where
T: GetStorageVersion + PalletInfoAccess,
{
fn on_runtime_upgrade() -> Weight {
let db_weight = RocksDbWeight::get();
Garandor marked this conversation as resolved.
Show resolved Hide resolved
migrate_from_pallet_version_to_storage_version::<T>(&db_weight)
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<(), &'static str> {
let storage_version = StorageVersion::get::<T>();
frame_support::debug(&"----PreUpgrade----");
frame_support::debug(&T::module_name());
frame_support::debug(&T::name());
frame_support::debug(&storage_version);
flame4 marked this conversation as resolved.
Show resolved Hide resolved
Ok(())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade() -> Result<(), &'static str> {
let storage_version = StorageVersion::get::<T>();
frame_support::debug(&"----PostUpgrade----");
frame_support::debug(&T::module_name());
frame_support::debug(&T::name());
frame_support::debug(&storage_version);
ensure!(storage_version == StorageVersion::new(1), T::module_name());
Ok(())
}
}

#[cfg(test)]
mod test {
use super::*;
use frame_support::traits::{CrateVersion, StorageInstance, StorageVersion};
use sp_io::TestExternalities;

pub struct DummyPrefix;

impl StorageInstance for DummyPrefix {
fn pallet_prefix() -> &'static str {
"test_pv2sv"
}

const STORAGE_PREFIX: &'static str = "foo";
}

// just used for below migration test.
// avoiding declare a huge Runtime part.
struct MockForMigrationTesting {}

impl GetStorageVersion for MockForMigrationTesting {
fn current_storage_version() -> StorageVersion {
StorageVersion::new(10)
}

fn on_chain_storage_version() -> StorageVersion {
StorageVersion::get::<Self>()
}
}

impl PalletInfoAccess for MockForMigrationTesting {
fn index() -> usize {
0
}

fn name() -> &'static str {
"test_pv_2_sv"
}

fn module_name() -> &'static str {
"test_module_name"
}

fn crate_version() -> CrateVersion {
CrateVersion {
major: 4,
minor: 0,
patch: 0,
}
}
}

#[test]
fn test_pv_2_sv_works() {
// 1. write old pallet version into storage.
// 2. call utility
// 3. test whether it works.
const PALLET_VERSION_STORAGE_KEY_POSTFIX: &[u8] = b":__PALLET_VERSION__:";
fn pallet_version_key(name: &str) -> [u8; 32] {
frame_support::storage::storage_prefix(
name.as_bytes(),
PALLET_VERSION_STORAGE_KEY_POSTFIX,
)
}

let mut db = TestExternalities::default();
db.execute_with(|| {
sp_io::storage::set(
&pallet_version_key(MockForMigrationTesting::name()),
&[1, 0, 0],
);
assert_eq!(
MockForMigrationTesting::on_chain_storage_version(),
StorageVersion::new(0)
);
let weight = MigratePalletPv2Sv::<MockForMigrationTesting>::on_runtime_upgrade();
assert_eq!(100_000 * 1000 * 2, weight);
assert!(
sp_io::storage::get(&pallet_version_key(MockForMigrationTesting::name())).is_none()
);
assert_eq!(
MockForMigrationTesting::on_chain_storage_version(),
StorageVersion::new(10)
);
})
}
}
14 changes: 13 additions & 1 deletion runtime/dolphin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ pub mod xcm_config;
use currency::*;
use fee::WeightToFee;
use impls::DealWithFees;
use runtime_common::migration::MigratePalletPv2Sv;

pub type NegativeImbalance = <Balances as Currency<AccountId>>::NegativeImbalance;

Expand Down Expand Up @@ -762,14 +763,25 @@ pub type SignedExtra = (
pub type UncheckedExtrinsic = generic::UncheckedExtrinsic<Address, Call, Signature, SignedExtra>;
/// Extrinsic type that has already been checked.
pub type CheckedExtrinsic = generic::CheckedExtrinsic<AccountId, Call, SignedExtra>;

/// Types for runtime upgrading.
/// Each type should implement trait `OnRuntimeUpgrade`.
pub type OnRuntimeUpgradeHooks = (
UpgradeSessionKeys,
MigratePalletPv2Sv<pallet_asset_manager::Pallet<Runtime>>,
MigratePalletPv2Sv<pallet_tx_pause::Pallet<Runtime>>,
MigratePalletPv2Sv<manta_collator_selection::Pallet<Runtime>>,
MigratePalletPv2Sv<pallet_manta_pay::Pallet<Runtime>>,
);

/// Executive: handles dispatch to the various modules.
pub type Executive = frame_executive::Executive<
Runtime,
Block,
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsReversedWithSystemFirst,
UpgradeSessionKeys,
OnRuntimeUpgradeHooks,
>;
// When this is removed, should also remove `OldSessionKeys`.
pub struct UpgradeSessionKeys;
Expand Down