Skip to content

Commit

Permalink
Remove state trie migration from kusama, add it to polkadot (#170)
Browse files Browse the repository at this point in the history
This PR add the state trie migration to V1 to polkadot runtime.
The configuration is the same as the one that was run on kusama.
As in kusama this PR changes the state-trie-version to v1, so the
migration declared in unreleased Have to run with the next upgrade. In
case the migration is not include, the line `state_version: 1` have to
be roll back to `state_version: 0` as warpsync will stop working until
the end of the migration.

<!-- Remember that you can run `/merge` to enable auto-merge in the PR
-->

<!-- Remember to modify the changelog. If you don't need to modify it,
you can check the following box.
Instead, if you have already modified it, simply delete the following
line. -->

- [ ] Does not require a CHANGELOG entry

---------

Co-authored-by: Bastian Köcher <git@kchr.de>
Co-authored-by: Bastian Köcher <info@kchr.de>
  • Loading branch information
3 people authored Mar 17, 2024
1 parent c5f8556 commit 360581f
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 90 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

### Added

- Remove state-trie-migration pallet from kusama, add state trie migration to V1 on polkadot ([polkadot-fellows/runtimes#170](https://github.com/polkadot-fellows/runtimes/pull/170))
- Introduce chain spec generator ([polkadot-fellows/runtimes#127](https://github.com/polkadot-fellows/runtimes/pull/127))
- Add [Encointer](https://encointer.org) system parachain runtime, completing [RFC22](https://github.com/polkadot-fellows/RFCs/blob/main/text/
0022-adopt-encointer-runtime.md) ([polkadot-fellows/runtimes#80](https://github.com/polkadot-fellows/runtimes/pull/80))
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

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

4 changes: 0 additions & 4 deletions relay/kusama/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ pallet-session = { default-features = false , version = "29.0.0" }
pallet-society = { default-features = false, version = "29.0.0" }
frame-support = { default-features = false , features = [ "tuples-96" ] , version = "29.0.0" }
pallet-staking = { default-features = false , version = "29.0.0" }
pallet-state-trie-migration = { default-features = false , version = "30.0.0" }
pallet-staking-runtime-api = { default-features = false , version = "15.0.0" }
frame-system = { default-features = false , version = "29.0.0" }
frame-system-rpc-runtime-api = { default-features = false , version = "27.0.0" }
Expand Down Expand Up @@ -175,7 +174,6 @@ std = [
"pallet-society/std",
"pallet-staking-runtime-api/std",
"pallet-staking/std",
"pallet-state-trie-migration/std",
"pallet-timestamp/std",
"pallet-transaction-payment-rpc-runtime-api/std",
"pallet-transaction-payment/std",
Expand Down Expand Up @@ -246,7 +244,6 @@ runtime-benchmarks = [
"pallet-session-benchmarking/runtime-benchmarks",
"pallet-society/runtime-benchmarks",
"pallet-staking/runtime-benchmarks",
"pallet-state-trie-migration/runtime-benchmarks",
"pallet-timestamp/runtime-benchmarks",
"pallet-treasury/runtime-benchmarks",
"pallet-utility/runtime-benchmarks",
Expand Down Expand Up @@ -300,7 +297,6 @@ try-runtime = [
"pallet-session/try-runtime",
"pallet-society/try-runtime",
"pallet-staking/try-runtime",
"pallet-state-trie-migration/try-runtime",
"pallet-timestamp/try-runtime",
"pallet-transaction-payment/try-runtime",
"pallet-treasury/try-runtime",
Expand Down
86 changes: 2 additions & 84 deletions relay/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1541,27 +1541,6 @@ impl pallet_nomination_pools::Config for Runtime {
type MaxPointsToBalance = MaxPointsToBalance;
}

parameter_types! {
// The deposit configuration for the singed migration. Specially if you want to allow any signed account to do the migration (see `SignedFilter`, these deposits should be high)
pub const MigrationSignedDepositPerItem: Balance = 1 * CENTS;
pub const MigrationSignedDepositBase: Balance = 20 * CENTS * 100;
pub const MigrationMaxKeyLen: u32 = 512;
}

impl pallet_state_trie_migration::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type RuntimeHoldReason = RuntimeHoldReason;
type SignedDepositPerItem = MigrationSignedDepositPerItem;
type SignedDepositBase = MigrationSignedDepositBase;
type ControlOrigin = EnsureRoot<AccountId>;
type SignedFilter = frame_support::traits::NeverEnsureOrigin<AccountId>;

// Use same weights as substrate ones.
type WeightInfo = pallet_state_trie_migration::weights::SubstrateWeight<Runtime>;
type MaxKeyLen = MigrationMaxKeyLen;
}

impl pallet_asset_rate::Config for Runtime {
type WeightInfo = weights::pallet_asset_rate::WeightInfo<Runtime>;
type RuntimeEvent = RuntimeEvent;
Expand Down Expand Up @@ -1739,9 +1718,6 @@ construct_runtime! {
Crowdloan: crowdloan = 73,
Coretime: coretime = 74,

// State trie migration pallet, only temporary.
StateTrieMigration: pallet_state_trie_migration = 98,

// Pallet for sending XCM.
XcmPallet: pallet_xcm = 99,

Expand Down Expand Up @@ -1852,6 +1828,7 @@ pub mod migrations {
}

parameter_types! {
pub const StateTrieMigrationName: &'static str = "StateTrieMigration";
pub const ImOnlinePalletName: &'static str = "ImOnline";
}

Expand Down Expand Up @@ -1968,6 +1945,7 @@ pub mod migrations {

/// Unreleased migrations. Add new ones here:
pub type Unreleased = (
frame_support::migrations::RemovePallet<StateTrieMigrationName, RocksDbWeight>,
pallet_nomination_pools::migration::versioned::V7ToV8<Runtime>,
pallet_staking::migrations::v14::MigrateToV14<Runtime>,
parachains_configuration::migration::v10::MigrateToV10<Runtime>,
Expand Down Expand Up @@ -3064,63 +3042,3 @@ mod remote_tests {
});
}
}

mod init_state_migration {
use super::Runtime;
use frame_support::traits::OnRuntimeUpgrade;
use pallet_state_trie_migration::{AutoLimits, MigrationLimits, MigrationProcess};

/// Initialize an automatic migration process.
pub struct InitMigrate;
impl OnRuntimeUpgrade for InitMigrate {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<sp_std::vec::Vec<u8>, sp_runtime::DispatchError> {
use parity_scale_codec::Encode;
let migration_should_start = AutoLimits::<Runtime>::get().is_none() &&
MigrationProcess::<Runtime>::get() == Default::default();
Ok(migration_should_start.encode())
}

fn on_runtime_upgrade() -> frame_support::weights::Weight {
if AutoLimits::<Runtime>::get().is_some() {
log::warn!("Automatic trie migration already started, not proceeding.");
return <Runtime as frame_system::Config>::DbWeight::get().reads(1)
};

if MigrationProcess::<Runtime>::get() != Default::default() {
log::warn!("MigrationProcess is not Default. Not proceeding.");
return <Runtime as frame_system::Config>::DbWeight::get().reads(2)
};

// Migration is not already running and `MigraitonProcess` is Default. Ready to run
// migrations.
//
// We use limits to target 600ko proofs per block and
// avg 800_000_000_000 of weight per block.
// See spreadsheet 4800_400 in
// https://raw.githubusercontent.com/cheme/substrate/try-runtime-mig/ksm.ods
AutoLimits::<Runtime>::put(Some(MigrationLimits { item: 4_800, size: 204800 * 2 }));
log::info!("Automatic trie migration started.");
<Runtime as frame_system::Config>::DbWeight::get().reads_writes(2, 1)
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(
migration_should_start_bytes: sp_std::vec::Vec<u8>,
) -> Result<(), sp_runtime::DispatchError> {
use parity_scale_codec::Decode;
let migration_should_start: bool =
Decode::decode(&mut migration_should_start_bytes.as_slice())
.expect("failed to decode migration should start");

if migration_should_start {
frame_support::ensure!(
AutoLimits::<Runtime>::get().is_some(),
sp_runtime::DispatchError::Other("Automigration did not start as expected.")
);
}

Ok(())
}
}
}
4 changes: 4 additions & 0 deletions relay/polkadot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pallet-staking = { default-features = false , version = "29.0.0" }
pallet-staking-reward-fn = { default-features = false, version = "20.0.0" }
pallet-staking-reward-curve = { version = "11.0.0" }
pallet-staking-runtime-api = { default-features = false , version = "15.0.0" }
pallet-state-trie-migration = { default-features = false , version = "30.0.0" }
frame-system = { default-features = false , version = "29.0.0" }
frame-system-rpc-runtime-api = { default-features = false , version = "27.0.0" }
polkadot-runtime-constants = { package = "polkadot-runtime-constants", path = "constants", default-features = false }
Expand Down Expand Up @@ -166,6 +167,7 @@ std = [
"pallet-staking-reward-fn/std",
"pallet-staking-runtime-api/std",
"pallet-staking/std",
"pallet-state-trie-migration/std",
"pallet-timestamp/std",
"pallet-transaction-payment-rpc-runtime-api/std",
"pallet-transaction-payment/std",
Expand Down Expand Up @@ -233,6 +235,7 @@ runtime-benchmarks = [
"pallet-scheduler/runtime-benchmarks",
"pallet-session-benchmarking/runtime-benchmarks",
"pallet-staking/runtime-benchmarks",
"pallet-state-trie-migration/runtime-benchmarks",
"pallet-timestamp/runtime-benchmarks",
"pallet-treasury/runtime-benchmarks",
"pallet-utility/runtime-benchmarks",
Expand Down Expand Up @@ -282,6 +285,7 @@ try-runtime = [
"pallet-scheduler/try-runtime",
"pallet-session/try-runtime",
"pallet-staking/try-runtime",
"pallet-state-trie-migration/try-runtime",
"pallet-timestamp/try-runtime",
"pallet-transaction-payment/try-runtime",
"pallet-treasury/try-runtime",
Expand Down
89 changes: 88 additions & 1 deletion relay/polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 25,
state_version: 0,
state_version: 1,
};

/// The BABE epoch configuration at genesis.
Expand Down Expand Up @@ -1541,6 +1541,27 @@ impl frame_support::traits::OnRuntimeUpgrade for InitiateNominationPools {
}
}

parameter_types! {
// The deposit configuration for the singed migration. Specially if you want to allow any signed account to do the migration (see `SignedFilter`, these deposits should be high)
pub const MigrationSignedDepositPerItem: Balance = 1 * CENTS;
pub const MigrationSignedDepositBase: Balance = 20 * CENTS * 100;
pub const MigrationMaxKeyLen: u32 = 512;
}

impl pallet_state_trie_migration::Config for Runtime {
type RuntimeHoldReason = RuntimeHoldReason;
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type SignedDepositPerItem = MigrationSignedDepositPerItem;
type SignedDepositBase = MigrationSignedDepositBase;
type ControlOrigin = EnsureRoot<AccountId>;
type SignedFilter = frame_support::traits::NeverEnsureOrigin<AccountId>;

// Use same weights as substrate ones.
type WeightInfo = pallet_state_trie_migration::weights::SubstrateWeight<Runtime>;
type MaxKeyLen = MigrationMaxKeyLen;
}

impl pallet_asset_rate::Config for Runtime {
type WeightInfo = weights::pallet_asset_rate::WeightInfo<Runtime>;
type RuntimeEvent = RuntimeEvent;
Expand Down Expand Up @@ -1697,6 +1718,9 @@ construct_runtime! {
Auctions: auctions = 72,
Crowdloan: crowdloan = 73,

// State trie migration pallet, only temporary.
StateTrieMigration: pallet_state_trie_migration = 98,

// Pallet for sending XCM.
XcmPallet: pallet_xcm = 99,

Expand Down Expand Up @@ -1843,6 +1867,7 @@ pub mod migrations {

/// Unreleased migrations. Add new ones here:
pub type Unreleased = (
init_state_migration::InitMigrate,
// Upgrade SessionKeys to exclude ImOnline key
UpgradeSessionKeys,
pallet_nomination_pools::migration::versioned::V7ToV8<Runtime>,
Expand Down Expand Up @@ -3153,3 +3178,65 @@ mod remote_tests {
});
}
}

mod init_state_migration {
use super::Runtime;
use frame_support::traits::OnRuntimeUpgrade;
use pallet_state_trie_migration::{AutoLimits, MigrationLimits, MigrationProcess};
#[cfg(not(feature = "std"))]
use sp_std::prelude::*;

/// Initialize an automatic migration process.
pub struct InitMigrate;
impl OnRuntimeUpgrade for InitMigrate {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::DispatchError> {
use parity_scale_codec::Encode;
let migration_should_start = AutoLimits::<Runtime>::get().is_none() &&
MigrationProcess::<Runtime>::get() == Default::default();
Ok(migration_should_start.encode())
}

fn on_runtime_upgrade() -> frame_support::weights::Weight {
if AutoLimits::<Runtime>::get().is_some() {
log::warn!("Automatic trie migration already started, not proceeding.");
return <Runtime as frame_system::Config>::DbWeight::get().reads(1)
};

if MigrationProcess::<Runtime>::get() != Default::default() {
log::warn!("MigrationProcess is not Default. Not proceeding.");
return <Runtime as frame_system::Config>::DbWeight::get().reads(2)
};

// Migration is not already running and `MigraitonProcess` is Default. Ready to run
// migrations.
//
// We use limits to target 600ko proofs per block and
// avg 800_000_000_000 of weight per block.
// See spreadsheet 4800_400 in
// https://raw.githubusercontent.com/cheme/substrate/try-runtime-mig/ksm.ods
AutoLimits::<Runtime>::put(Some(MigrationLimits { item: 4_800, size: 204800 * 2 }));
log::info!("Automatic trie migration started.");
<Runtime as frame_system::Config>::DbWeight::get().reads_writes(2, 1)
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(
migration_should_start_bytes: Vec<u8>,
) -> Result<(), sp_runtime::DispatchError> {
use parity_scale_codec::Decode;
let migration_should_start: bool =
Decode::decode(&mut migration_should_start_bytes.as_slice())
.expect("failed to decode migration should start");

if migration_should_start {
frame_support::ensure!(
AutoLimits::<Runtime>::get().is_some(),
sp_runtime::DispatchError::Other("Automigration did not start as expected.")
);
}

Ok(())
}
}
}

0 comments on commit 360581f

Please sign in to comment.