From 796596fb1faf928c2af6020d18c87de276ecf87d Mon Sep 17 00:00:00 2001 From: Malte Kliemann Date: Mon, 12 Aug 2024 14:06:54 +0200 Subject: [PATCH] Remove old migrations and clear pallet storage --- runtime/common/src/lib.rs | 29 +- zrml/market-commons/src/migrations.rs | 387 -------------------------- 2 files changed, 24 insertions(+), 392 deletions(-) diff --git a/runtime/common/src/lib.rs b/runtime/common/src/lib.rs index d64d0c01b..a5ea7e94c 100644 --- a/runtime/common/src/lib.rs +++ b/runtime/common/src/lib.rs @@ -86,13 +86,35 @@ macro_rules! decl_common_types { generic, DispatchError, DispatchResult, RuntimeDebug, SaturatedConversion, }; use zeitgeist_primitives::traits::{DeployPoolApi, DistributeFees, MarketCommonsPalletApi}; - use zrml_market_commons::migrations::MigrateDisputeMechanism; pub type Block = generic::Block; type Address = sp_runtime::MultiAddress; - type Migrations = (MigrateDisputeMechanism); + parameter_types! { + pub const CampaignAssetsPalletStr: &'static str = "CampaignAssets"; + pub const CustomAssetsPalletStr: &'static str = "CustomAssets"; + pub const MarketAssetsPalletStr: &'static str = "MarketAssets"; + pub const LiquidityMiningPalletStr: &'static str = "LiquidityMining"; + pub const RikiddoPalletStr: &'static str = "Rikiddo"; + pub const SimpleDisputesPalletStr: &'static str = "SimpleDisputes"; + } + + type RemoveCustomAssets = RemovePallet; + type RemoveCampaignAssets = RemovePallet; + type RemoveMarketAssets = RemovePallet; + type RemoveLiquidityMining = RemovePallet; + type RemoveRikiddo = RemovePallet; + type RemoveSimpleDisputes = RemovePallet; + + type Migrations = ( + RemoveCustomAssets, + RemoveCampaignAssets, + RemoveMarketAssets, + RemoveLiquidityMining, + RemoveRikiddo, + RemoveSimpleDisputes, + ); pub type Executive = frame_executive::Executive< Runtime, @@ -751,10 +773,7 @@ macro_rules! impl_config_traits { type MaxDebugBufferLen = ContractsMaxDebugBufferLen; type MaxDelegateDependencies = ContractsMaxDelegateDependencies; type MaxStorageKeyLen = ContractsMaxStorageKeyLen; - #[cfg(not(feature = "runtime-benchmarks"))] type Migrations = (); - #[cfg(feature = "runtime-benchmarks")] - type Migrations = pallet_contracts::migration::codegen::BenchMigrations; type Randomness = RandomnessCollectiveFlip; type RuntimeCall = RuntimeCall; type RuntimeEvent = RuntimeEvent; diff --git a/zrml/market-commons/src/migrations.rs b/zrml/market-commons/src/migrations.rs index adf04862f..7bcc6e2e5 100644 --- a/zrml/market-commons/src/migrations.rs +++ b/zrml/market-commons/src/migrations.rs @@ -15,390 +15,3 @@ // // You should have received a copy of the GNU General Public License // along with Zeitgeist. If not, see . - -use crate::{AccountIdOf, BalanceOf, Config, MarketIdOf, MomentOf, Pallet as MarketCommons}; -use alloc::vec::Vec; -use core::marker::PhantomData; -use frame_support::{ - pallet_prelude::Weight, - traits::{Get, OnRuntimeUpgrade, StorageVersion}, -}; -use frame_system::pallet_prelude::BlockNumberFor; -use parity_scale_codec::{Decode, Encode, MaxEncodedLen}; -use scale_info::TypeInfo; -use sp_runtime::{Perbill, RuntimeDebug, Saturating}; -use zeitgeist_primitives::types::{ - Asset, Deadlines, EarlyClose, Market, MarketBonds, MarketCreation, MarketDisputeMechanism, - MarketPeriod, MarketStatus, MarketType, OutcomeReport, Report, ScoringRule, -}; - -#[cfg(feature = "try-runtime")] -use { - alloc::{collections::BTreeMap, format}, - frame_support::migration::storage_key_iter, - sp_runtime::DispatchError, -}; - -#[cfg(any(feature = "try-runtime", feature = "test"))] -use frame_support::Blake2_128Concat; - -#[cfg(any(feature = "try-runtime", test))] -const MARKET_COMMONS: &[u8] = b"MarketCommons"; -#[cfg(any(feature = "try-runtime", test))] -const MARKETS: &[u8] = b"Markets"; - -#[derive(Clone, Decode, Encode, Eq, PartialEq, RuntimeDebug, TypeInfo)] -pub struct OldMarket { - pub market_id: MarketId, - /// Base asset of the market. - pub base_asset: Asset, - /// Creator of this market. - pub creator: AccountId, - /// Creation type. - pub creation: MarketCreation, - /// A fee that is charged each trade and given to the market creator. - pub creator_fee: Perbill, - /// Oracle that reports the outcome of this market. - pub oracle: AccountId, - /// Metadata for the market, usually a content address of IPFS - /// hosted JSON. Currently limited to 66 bytes (see `MaxEncodedLen` implementation) - pub metadata: Vec, - /// The type of the market. - pub market_type: MarketType, - /// Market start and end - pub period: MarketPeriod, - /// Market deadlines. - pub deadlines: Deadlines, - /// The scoring rule used for the market. - pub scoring_rule: ScoringRule, - /// The current status of the market. - pub status: MarketStatus, - /// The report of the market. Only `Some` if it has been reported. - pub report: Option>, - /// The resolved outcome. - pub resolved_outcome: Option, - /// See [`MarketDisputeMechanism`]. - pub dispute_mechanism: Option, - /// The bonds reserved for this market. - pub bonds: MarketBonds, - /// The time at which the market was closed early. - pub early_close: Option>, -} - -type OldMarketOf = - OldMarket, BalanceOf, BlockNumberFor, MomentOf, MarketIdOf>; - -#[derive(TypeInfo, Clone, Copy, Encode, Eq, Decode, MaxEncodedLen, PartialEq, RuntimeDebug)] -pub enum OldMarketDisputeMechanism { - Authorized, - Court, - SimpleDisputes, -} - -const MARKET_COMMONS_REQUIRED_STORAGE_VERSION: u16 = 11; -const MARKET_COMMONS_NEXT_STORAGE_VERSION: u16 = 12; - -#[cfg(feature = "try-runtime")] -#[frame_support::storage_alias] -pub(crate) type Markets = - StorageMap, Blake2_128Concat, MarketIdOf, OldMarketOf>; - -pub struct MigrateDisputeMechanism(PhantomData); - -/// Removes the `SimpleDisputes` MDM by switching markets that use `SimpleDisputes` to `Authorized`. -/// Note that this migration does not unreserve any funds bonded with zrml-simple-dispute's reserve -/// ID. -impl OnRuntimeUpgrade for MigrateDisputeMechanism -where - T: Config, -{ - fn on_runtime_upgrade() -> Weight { - let mut total_weight = T::DbWeight::get().reads(1); - let market_commons_version = StorageVersion::get::>(); - if market_commons_version != MARKET_COMMONS_REQUIRED_STORAGE_VERSION { - log::info!( - "MigrateDisputeMechanism: market-commons version is {:?}, but {:?} is required", - market_commons_version, - MARKET_COMMONS_REQUIRED_STORAGE_VERSION, - ); - return total_weight; - } - log::info!("MigrateDisputeMechanism: Starting..."); - - let mut translated = 0u64; - crate::Markets::::translate::, _>(|_, old_market| { - translated.saturating_inc(); - let dispute_mechanism = match old_market.dispute_mechanism { - Some(OldMarketDisputeMechanism::Court) => Some(MarketDisputeMechanism::Court), - Some(_) => Some(MarketDisputeMechanism::Authorized), - None => None, - }; - let new_market = Market { - market_id: old_market.market_id, - base_asset: old_market.base_asset, - creator: old_market.creator, - creation: old_market.creation, - creator_fee: old_market.creator_fee, - oracle: old_market.oracle, - metadata: old_market.metadata, - market_type: old_market.market_type, - period: old_market.period, - deadlines: old_market.deadlines, - scoring_rule: old_market.scoring_rule, - status: old_market.status, - report: old_market.report, - resolved_outcome: old_market.resolved_outcome, - dispute_mechanism, - bonds: old_market.bonds, - early_close: old_market.early_close, - }; - Some(new_market) - }); - log::info!("MigrateDisputeMechanism: Upgraded {} markets.", translated); - total_weight = - total_weight.saturating_add(T::DbWeight::get().reads_writes(translated, translated)); - - StorageVersion::new(MARKET_COMMONS_NEXT_STORAGE_VERSION).put::>(); - total_weight = total_weight.saturating_add(T::DbWeight::get().writes(1)); - log::info!("MigrateDisputeMechanism: Done!"); - total_weight - } - - #[cfg(feature = "try-runtime")] - fn pre_upgrade() -> Result, DispatchError> { - let old_markets = storage_key_iter::, OldMarketOf, Blake2_128Concat>( - MARKET_COMMONS, - MARKETS, - ) - .collect::>(); - let markets = Markets::::iter_keys().count(); - let decodable_markets = Markets::::iter_values().count(); - if markets == decodable_markets { - log::info!("All {} markets could successfully be decoded.", markets); - } else { - log::error!( - "Can only decode {} of {} markets - others will be dropped.", - decodable_markets, - markets - ); - } - - Ok(old_markets.encode()) - } - - #[cfg(feature = "try-runtime")] - fn post_upgrade(previous_state: Vec) -> Result<(), DispatchError> { - let old_markets: BTreeMap, OldMarketOf> = - Decode::decode(&mut &previous_state[..]).unwrap(); - let old_market_count = old_markets.len(); - let new_market_count = crate::Markets::::iter().count(); - assert_eq!(old_market_count, new_market_count); - for (market_id, new_market) in crate::Markets::::iter() { - let old_market = old_markets - .get(&market_id) - .expect(&format!("Market {:?} not found", market_id)[..]); - assert_eq!(old_market.market_id, new_market.market_id); - assert_eq!(old_market.base_asset, new_market.base_asset); - assert_eq!(old_market.creator, new_market.creator); - assert_eq!(old_market.creation, new_market.creation); - assert_eq!(old_market.creator_fee, new_market.creator_fee); - assert_eq!(old_market.oracle, new_market.oracle); - assert_eq!(old_market.metadata, new_market.metadata); - assert_eq!(old_market.market_type, new_market.market_type); - assert_eq!(old_market.period, new_market.period); - assert_eq!(old_market.deadlines, new_market.deadlines); - assert_eq!(old_market.status, new_market.status); - assert_eq!(old_market.report, new_market.report); - assert_eq!(old_market.resolved_outcome, new_market.resolved_outcome); - assert_eq!(old_market.bonds, new_market.bonds); - assert_eq!(old_market.early_close, new_market.early_close); - } - log::info!("MigrateDisputeMechanism: Post-upgrade market count is {}!", new_market_count); - Ok(()) - } -} - -#[cfg(test)] -mod tests { - use super::*; - use crate::{ - mock::{ExtBuilder, Runtime}, - MarketOf, - }; - use alloc::fmt::Debug; - use frame_support::{migration::put_storage_value, Blake2_128Concat, StorageHasher}; - use parity_scale_codec::Encode; - use sp_io::storage::root as storage_root; - use sp_runtime::{Perbill, StateVersion}; - use test_case::test_case; - use zeitgeist_primitives::types::{Bond, EarlyCloseState, MarketId}; - - #[test] - fn on_runtime_upgrade_increments_the_storage_version() { - ExtBuilder::default().build().execute_with(|| { - set_up_version(); - MigrateDisputeMechanism::::on_runtime_upgrade(); - assert_eq!( - StorageVersion::get::>(), - MARKET_COMMONS_NEXT_STORAGE_VERSION - ); - }); - } - - #[test_case(None, None; "none")] - #[test_case( - Some(OldMarketDisputeMechanism::Authorized), - Some(MarketDisputeMechanism::Authorized) - )] - #[test_case(Some(OldMarketDisputeMechanism::Court), Some(MarketDisputeMechanism::Court))] - #[test_case( - Some(OldMarketDisputeMechanism::SimpleDisputes), - Some(MarketDisputeMechanism::Authorized) - )] - fn on_runtime_upgrade_works_as_expected( - old_scoring_rule: Option, - new_scoring_rule: Option, - ) { - ExtBuilder::default().build().execute_with(|| { - set_up_version(); - let (old_market, new_market) = - construct_old_new_tuple(old_scoring_rule, new_scoring_rule); - populate_test_data::>( - MARKET_COMMONS, - MARKETS, - vec![old_market], - ); - MigrateDisputeMechanism::::on_runtime_upgrade(); - assert_eq!(crate::Markets::::get(0).unwrap(), new_market); - }); - } - - #[test] - fn on_runtime_upgrade_is_noop_if_versions_are_not_correct() { - ExtBuilder::default().build().execute_with(|| { - StorageVersion::new(MARKET_COMMONS_NEXT_STORAGE_VERSION) - .put::>(); - let market = Market { - market_id: 7, - base_asset: Asset::Ztg, - creator: 1, - creation: MarketCreation::Permissionless, - creator_fee: Perbill::from_rational(2u32, 3u32), - oracle: 4, - metadata: vec![0x05; 50], - market_type: MarketType::Categorical(999), - period: MarketPeriod::, MomentOf>::Block(6..7), - deadlines: Deadlines { grace_period: 7, oracle_duration: 8, dispute_duration: 9 }, - scoring_rule: ScoringRule::AmmCdaHybrid, - status: MarketStatus::Active, - report: Some(Report { at: 13, by: 14, outcome: OutcomeReport::Categorical(10) }), - resolved_outcome: None, - dispute_mechanism: Some(MarketDisputeMechanism::Court), - bonds: MarketBonds { - creation: Some(Bond::new(11, 12)), - oracle: None, - outsider: None, - dispute: None, - close_dispute: None, - close_request: None, - }, - early_close: None, - }; - crate::Markets::::insert(333, market); - let tmp = storage_root(StateVersion::V1); - MigrateDisputeMechanism::::on_runtime_upgrade(); - assert_eq!(tmp, storage_root(StateVersion::V1)); - }); - } - - fn set_up_version() { - StorageVersion::new(MARKET_COMMONS_REQUIRED_STORAGE_VERSION) - .put::>(); - } - - fn construct_old_new_tuple( - old_dispute_mechanism: Option, - new_dispute_mechanism: Option, - ) -> (OldMarketOf, MarketOf) { - let market_id = 0; - let base_asset = Asset::Ztg; - let creator = 1; - let creation = MarketCreation::Advised; - let creator_fee = Perbill::from_rational(2u32, 3u32); - let oracle = 4; - let metadata = vec![5; 50]; - let market_type = MarketType::Scalar(6..=7); - let period = MarketPeriod::Block(8..9); - let deadlines = Deadlines { grace_period: 10, oracle_duration: 11, dispute_duration: 12 }; - let scoring_rule = ScoringRule::AmmCdaHybrid; - let status = MarketStatus::Resolved; - let report = Some(Report { at: 13, by: 14, outcome: OutcomeReport::Categorical(15) }); - let resolved_outcome = Some(OutcomeReport::Categorical(16)); - let bonds = MarketBonds { - creation: Some(Bond { who: 17, value: 18, is_settled: true }), - oracle: Some(Bond { who: 19, value: 20, is_settled: true }), - outsider: Some(Bond { who: 21, value: 22, is_settled: true }), - dispute: Some(Bond { who: 23, value: 24, is_settled: true }), - close_request: Some(Bond { who: 25, value: 26, is_settled: true }), - close_dispute: Some(Bond { who: 27, value: 28, is_settled: true }), - }; - let early_close = Some(EarlyClose { - old: MarketPeriod::Block(29..30), - new: MarketPeriod::Block(31..32), - state: EarlyCloseState::Disputed, - }); - let old_markets = OldMarket { - market_id, - base_asset, - creator, - creation: creation.clone(), - creator_fee, - oracle, - metadata: metadata.clone(), - market_type: market_type.clone(), - period: period.clone(), - deadlines, - scoring_rule, - status, - report: report.clone(), - resolved_outcome: resolved_outcome.clone(), - dispute_mechanism: old_dispute_mechanism, - bonds: bonds.clone(), - early_close: early_close.clone(), - }; - let new_market = Market { - market_id, - base_asset, - creator, - creation: creation.clone(), - creator_fee, - oracle, - metadata: metadata.clone(), - market_type: market_type.clone(), - period: period.clone(), - deadlines, - scoring_rule, - status, - report: report.clone(), - resolved_outcome: resolved_outcome.clone(), - dispute_mechanism: new_dispute_mechanism, - bonds: bonds.clone(), - early_close: early_close.clone(), - }; - (old_markets, new_market) - } - - #[allow(unused)] - fn populate_test_data(pallet: &[u8], prefix: &[u8], data: Vec) - where - H: StorageHasher, - K: TryFrom + Encode, - V: Encode + Clone, - >::Error: Debug, - { - for (key, value) in data.iter().enumerate() { - let storage_hash = K::try_from(key).unwrap().using_encoded(H::hash).as_ref().to_vec(); - put_storage_value::(pallet, prefix, &storage_hash, (*value).clone()); - } - } -}