Skip to content

Commit

Permalink
Multi-block migrator (#1322)
Browse files Browse the repository at this point in the history
* add multi-block migrator

* remove dep

* update config

* setup bench

* rename

* unfreeze chain

* reduce mbm service weight
  • Loading branch information
ermalkaleci authored Aug 27, 2024
1 parent 829355b commit d2714e5
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 27 deletions.
29 changes: 25 additions & 4 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ pallet-contracts = { git = "https://github.com/paritytech/polkadot-sdk", branch
pallet-contracts-uapi = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.11.0", default-features = false }
pallet-identity = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.11.0", default-features = false }
pallet-multisig = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.11.0", default-features = false }
pallet-migrations = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.11.0", default-features = false }
pallet-insecure-randomness-collective-flip = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.11.0", default-features = false }
pallet-session = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.11.0", default-features = false, features = ["historical"] }
pallet-sudo = { git = "https://github.com/paritytech/polkadot-sdk", branch = "release-polkadot-v1.11.0", default-features = false }
Expand Down
12 changes: 12 additions & 0 deletions primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub mod governance;
#[cfg(feature = "runtime-benchmarks")]
pub mod benchmarks;

use frame_support::migrations::{FailedMigrationHandler, FailedMigrationHandling};
use sp_runtime::{
generic,
traits::{BlakeTwo256, IdentifyAccount, Verify},
Expand Down Expand Up @@ -81,3 +82,14 @@ pub type AssetId = u128;
pub type Block = sp_runtime::generic::Block<Header, sp_runtime::OpaqueExtrinsic>;
/// Index of a transaction in the chain.
pub type Nonce = u32;

/// Unfreeze chain on failed migration and continue with extrinsic execution.
/// Migration must be tested and make sure it doesn't fail. If it happens, we don't have other
/// choices but unfreeze chain and continue with extrinsic execution.
pub struct UnfreezeChainOnFailedMigration;
impl FailedMigrationHandler for UnfreezeChainOnFailedMigration {
fn failed(migration: Option<u32>) -> FailedMigrationHandling {
log::error!(target: "mbm", "Migration failed at cursor: {migration:?}");
FailedMigrationHandling::ForceUnstuck
}
}
4 changes: 4 additions & 0 deletions runtime/astar/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pallet-evm-precompile-simple = { workspace = true }
pallet-identity = { workspace = true }
pallet-membership = { workspace = true }
pallet-message-queue = { workspace = true }
pallet-migrations = { workspace = true }
pallet-multisig = { workspace = true }
pallet-proxy = { workspace = true }
pallet-session = { workspace = true, features = ["historical"] }
Expand Down Expand Up @@ -188,6 +189,7 @@ std = [
"pallet-evm-precompile-sha3fips/std",
"pallet-identity/std",
"pallet-multisig/std",
"pallet-migrations/std",
"pallet-session/std",
"pallet-utility/std",
"pallet-timestamp/std",
Expand Down Expand Up @@ -271,6 +273,7 @@ runtime-benchmarks = [
"pallet-evm-precompile-xcm/runtime-benchmarks",
"pallet-identity/runtime-benchmarks",
"pallet-multisig/runtime-benchmarks",
"pallet-migrations/runtime-benchmarks",
"pallet-sudo/runtime-benchmarks",
"pallet-utility/runtime-benchmarks",
"pallet-vesting/runtime-benchmarks",
Expand Down Expand Up @@ -311,6 +314,7 @@ try-runtime = [
"pallet-xcm/try-runtime",
"pallet-identity/try-runtime",
"pallet-multisig/try-runtime",
"pallet-migrations/try-runtime",
"cumulus-pallet-aura-ext/try-runtime",
"cumulus-pallet-parachain-system/try-runtime",
"cumulus-pallet-xcm/try-runtime",
Expand Down
43 changes: 35 additions & 8 deletions runtime/astar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ use astar_primitives::{
evm::EvmRevertCodeHandler,
oracle::{CurrencyId, DummyCombineData, Price},
xcm::AssetLocationIdConverter,
Address, AssetId, BlockNumber, Hash, Header, Nonce,
Address, AssetId, BlockNumber, Hash, Header, Nonce, UnfreezeChainOnFailedMigration,
};
pub use astar_primitives::{governance::OracleMembershipInst, AccountId, Balance, Signature};

Expand Down Expand Up @@ -289,7 +289,7 @@ impl frame_system::Config for Runtime {
type OnSetCode = cumulus_pallet_parachain_system::ParachainSetCode<Self>;
type MaxConsumers = frame_support::traits::ConstU32<16>;
type SingleBlockMigrations = ();
type MultiBlockMigrator = ();
type MultiBlockMigrator = MultiBlockMigrations;
type PreInherents = ();
type PostInherents = ();
type PostTransactions = ();
Expand Down Expand Up @@ -1189,6 +1189,25 @@ impl frame_support::traits::SortedMembers<AccountId> for OracleMembershipWrapper
}
}

parameter_types! {
pub MbmServiceWeight: Weight = Perbill::from_percent(50) * RuntimeBlockWeights::get().max_block;
}

impl pallet_migrations::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
#[cfg(not(feature = "runtime-benchmarks"))]
type Migrations = ();
// Benchmarks need mocked migrations to guarantee that they succeed.
#[cfg(feature = "runtime-benchmarks")]
type Migrations = pallet_migrations::mock_helpers::MockedMigrations;
type CursorMaxLen = ConstU32<65_536>;
type IdentifierMaxLen = ConstU32<256>;
type MigrationStatusHandler = ();
type FailedMigrationHandler = UnfreezeChainOnFailedMigration;
type MaxServiceWeight = MbmServiceWeight;
type WeightInfo = pallet_migrations::weights::SubstrateWeight<Runtime>;
}

construct_runtime!(
pub struct Runtime
{
Expand Down Expand Up @@ -1225,6 +1244,7 @@ construct_runtime!(
XcmpQueue: cumulus_pallet_xcmp_queue = 50,
PolkadotXcm: pallet_xcm = 51,
CumulusXcm: cumulus_pallet_xcm = 52,
// skip 53 - cumulus_pallet_dmp_queue previously
XcAssetConfig: pallet_xc_asset_config = 54,
XTokens: orml_xtokens = 55,
MessageQueue: pallet_message_queue = 56,
Expand All @@ -1236,6 +1256,8 @@ construct_runtime!(
Contracts: pallet_contracts = 70,

Sudo: pallet_sudo = 99,

MultiBlockMigrations: pallet_migrations = 120,
}
);

Expand Down Expand Up @@ -1277,7 +1299,7 @@ pub type Executive = frame_executive::Executive<
parameter_types! {
// Threshold amount variation allowed for this migration - 10%
pub const ThresholdVariationPercentage: u32 = 10;
// percentages below are calulated based on total issuance at the time when dApp staking v3 was launched (8.4B)
// percentages below are calculated based on total issuance at the time when dApp staking v3 was launched (8.4B)
pub const TierThresholds: [TierThreshold; 4] = [
TierThreshold::DynamicPercentage {
percentage: Perbill::from_parts(35_700_000), // 3.57%
Expand All @@ -1303,11 +1325,12 @@ parameter_types! {

/// All migrations that will run on the next runtime upgrade.
///
/// Once done, migrations should be removed from the tuple.
pub type Migrations = (
// permanent migration, do not remove
pallet_xcm::migration::MigrateToLatestXcmVersion<Runtime>,
// dapp-staking dyn tier threshold migrations
/// __NOTE:__ THE ORDER IS IMPORTANT.
pub type Migrations = (Unreleased, Permanent);

/// Unreleased migrations. Add new ones here:
pub type Unreleased = (
// dApp-staking dyn tier threshold migrations
pallet_dapp_staking_v3::migration::versioned_migrations::V7ToV8<
Runtime,
TierThresholds,
Expand All @@ -1320,6 +1343,9 @@ pub type Migrations = (
pallet_contracts::Migration<Runtime>,
);

/// Migrations/checks that do not need to be versioned and can run on every upgrade.
pub type Permanent = (pallet_xcm::migration::MigrateToLatestXcmVersion<Runtime>,);

type EventRecord = frame_system::EventRecord<
<Runtime as frame_system::Config>::RuntimeEvent,
<Runtime as frame_system::Config>::Hash,
Expand Down Expand Up @@ -1397,6 +1423,7 @@ mod benches {
[pallet_timestamp, Timestamp]
[pallet_dapp_staking_v3, DappStaking]
[pallet_inflation, Inflation]
[pallet_migrations, MultiBlockMigrations]
[pallet_xc_asset_config, XcAssetConfig]
[pallet_collator_selection, CollatorSelection]
[pallet_xcm, PalletXcmExtrinsicsBenchmark::<Runtime>]
Expand Down
4 changes: 4 additions & 0 deletions runtime/shibuya/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pallet-identity = { workspace = true }
pallet-insecure-randomness-collective-flip = { workspace = true }
pallet-membership = { workspace = true }
pallet-message-queue = { workspace = true }
pallet-migrations = { workspace = true }
pallet-multisig = { workspace = true }
pallet-preimage = { workspace = true }
pallet-proxy = { workspace = true }
Expand Down Expand Up @@ -208,6 +209,7 @@ std = [
"pallet-price-aggregator/std",
"pallet-identity/std",
"pallet-multisig/std",
"pallet-migrations/std",
"pallet-insecure-randomness-collective-flip/std",
"pallet-session/std",
"pallet-utility/std",
Expand Down Expand Up @@ -302,6 +304,7 @@ runtime-benchmarks = [
"pallet-evm-precompile-xcm/runtime-benchmarks",
"pallet-identity/runtime-benchmarks",
"pallet-multisig/runtime-benchmarks",
"pallet-migrations/runtime-benchmarks",
"pallet-proxy/runtime-benchmarks",
"pallet-scheduler/runtime-benchmarks",
"pallet-sudo/runtime-benchmarks",
Expand Down Expand Up @@ -347,6 +350,7 @@ try-runtime = [
"pallet-xcm/try-runtime",
"pallet-identity/try-runtime",
"pallet-multisig/try-runtime",
"pallet-migrations/try-runtime",
"pallet-insecure-randomness-collective-flip/try-runtime",
"pallet-scheduler/try-runtime",
"pallet-proxy/try-runtime",
Expand Down
43 changes: 35 additions & 8 deletions runtime/shibuya/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ use astar_primitives::{
},
oracle::{CurrencyId, DummyCombineData, Price},
xcm::AssetLocationIdConverter,
Address, AssetId, BlockNumber, Hash, Header, Nonce,
Address, AssetId, BlockNumber, Hash, Header, Nonce, UnfreezeChainOnFailedMigration,
};
pub use astar_primitives::{AccountId, Balance, Signature};

Expand Down Expand Up @@ -326,7 +326,7 @@ impl frame_system::Config for Runtime {
type OnSetCode = cumulus_pallet_parachain_system::ParachainSetCode<Self>;
type MaxConsumers = frame_support::traits::ConstU32<16>;
type SingleBlockMigrations = ();
type MultiBlockMigrator = ();
type MultiBlockMigrator = MultiBlockMigrations;
type PreInherents = ();
type PostInherents = ();
type PostTransactions = ();
Expand Down Expand Up @@ -1514,6 +1514,25 @@ impl pallet_collective_proxy::Config for Runtime {
type WeightInfo = pallet_collective_proxy::weights::SubstrateWeight<Runtime>;
}

parameter_types! {
pub MbmServiceWeight: Weight = Perbill::from_percent(50) * RuntimeBlockWeights::get().max_block;
}

impl pallet_migrations::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
#[cfg(not(feature = "runtime-benchmarks"))]
type Migrations = ();
// Benchmarks need mocked migrations to guarantee that they succeed.
#[cfg(feature = "runtime-benchmarks")]
type Migrations = pallet_migrations::mock_helpers::MockedMigrations;
type CursorMaxLen = ConstU32<65_536>;
type IdentifierMaxLen = ConstU32<256>;
type MigrationStatusHandler = ();
type FailedMigrationHandler = UnfreezeChainOnFailedMigration;
type MaxServiceWeight = MbmServiceWeight;
type WeightInfo = pallet_migrations::weights::SubstrateWeight<Runtime>;
}

construct_runtime!(
pub struct Runtime
{
Expand Down Expand Up @@ -1548,6 +1567,7 @@ construct_runtime!(
XcmpQueue: cumulus_pallet_xcmp_queue = 50,
PolkadotXcm: pallet_xcm = 51,
CumulusXcm: cumulus_pallet_xcm = 52,
// skip 53 - cumulus_pallet_dmp_queue previously
XcAssetConfig: pallet_xc_asset_config = 54,
XTokens: orml_xtokens = 55,
MessageQueue: pallet_message_queue = 56,
Expand Down Expand Up @@ -1577,6 +1597,8 @@ construct_runtime!(
Treasury: pallet_treasury::<Instance1> = 107,
CommunityTreasury: pallet_treasury::<Instance2> = 108,
CollectiveProxy: pallet_collective_proxy = 109,

MultiBlockMigrations: pallet_migrations = 120,
}
);

Expand Down Expand Up @@ -1618,7 +1640,7 @@ pub type Executive = frame_executive::Executive<
parameter_types! {
// Threshold amount variation allowed for this migration - 150%
pub const ThresholdVariationPercentage: u32 = 150;
// percentages below are calulated based on a total issuance at the time when dApp staking v3 was launched (147M)
// percentages below are calculated based on a total issuance at the time when dApp staking v3 was launched (147M)
pub const TierThresholds: [TierThreshold; 4] = [
TierThreshold::DynamicPercentage {
percentage: Perbill::from_parts(20_000), // 0.0020%
Expand All @@ -1644,11 +1666,12 @@ parameter_types! {

/// All migrations that will run on the next runtime upgrade.
///
/// Once done, migrations should be removed from the tuple.
pub type Migrations = (
// permanent migration, do not remove
pallet_xcm::migration::MigrateToLatestXcmVersion<Runtime>,
// dapp-staking dyn tier threshold migrations
/// __NOTE:__ THE ORDER IS IMPORTANT.
pub type Migrations = (Unreleased, Permanent);

/// Unreleased migrations. Add new ones here:
pub type Unreleased = (
// dApp-staking dyn tier threshold migrations
pallet_dapp_staking_v3::migration::versioned_migrations::V7ToV8<
Runtime,
TierThresholds,
Expand All @@ -1661,6 +1684,9 @@ pub type Migrations = (
pallet_contracts::Migration<Runtime>,
);

/// Migrations/checks that do not need to be versioned and can run on every upgrade.
pub type Permanent = (pallet_xcm::migration::MigrateToLatestXcmVersion<Runtime>,);

type EventRecord = frame_system::EventRecord<
<Runtime as frame_system::Config>::RuntimeEvent,
<Runtime as frame_system::Config>::Hash,
Expand Down Expand Up @@ -1738,6 +1764,7 @@ mod benches {
[pallet_timestamp, Timestamp]
[pallet_dapp_staking_v3, DappStaking]
[pallet_inflation, Inflation]
[pallet_migrations, MultiBlockMigrations]
[pallet_xc_asset_config, XcAssetConfig]
[pallet_collator_selection, CollatorSelection]
[pallet_xcm, PalletXcmExtrinsicsBenchmark::<Runtime>]
Expand Down
Loading

0 comments on commit d2714e5

Please sign in to comment.