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

Remove old migrations (#1379) #1382

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 1 addition & 24 deletions runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,30 +91,7 @@ macro_rules! decl_common_types {

type Address = sp_runtime::MultiAddress<AccountId, ()>;

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 = "RikiddoSigmoidFeeMarketEma";
pub const SimpleDisputesPalletStr: &'static str = "SimpleDisputes";
}

type RemoveCustomAssets = RemovePallet<CustomAssetsPalletStr, RocksDbWeight>;
type RemoveCampaignAssets = RemovePallet<CampaignAssetsPalletStr, RocksDbWeight>;
type RemoveMarketAssets = RemovePallet<MarketAssetsPalletStr, RocksDbWeight>;
type RemoveLiquidityMining = RemovePallet<LiquidityMiningPalletStr, RocksDbWeight>;
type RemoveRikiddo = RemovePallet<RikiddoPalletStr, RocksDbWeight>;
type RemoveSimpleDisputes = RemovePallet<SimpleDisputesPalletStr, RocksDbWeight>;

type Migrations = (
RemoveCustomAssets,
RemoveCampaignAssets,
RemoveMarketAssets,
RemoveLiquidityMining,
RemoveRikiddo,
RemoveSimpleDisputes,
);
type Migrations = ();

pub type Executive = frame_executive::Executive<
Runtime,
Expand Down
248 changes: 0 additions & 248 deletions zrml/neo-swaps/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,251 +14,3 @@
//
// You should have received a copy of the GNU General Public License
// along with Zeitgeist. If not, see <https://www.gnu.org/licenses/>.

use crate::{
traits::LiquiditySharesManager, types::Pool, AssetOf, BalanceOf, Config, LiquidityTreeOf,
Pallet, Pools,
};
use alloc::collections::BTreeMap;
use core::marker::PhantomData;
use frame_support::{
traits::{Get, OnRuntimeUpgrade, StorageVersion},
weights::Weight,
};
use log;
use parity_scale_codec::{Decode, Encode};
use scale_info::TypeInfo;
use sp_runtime::{RuntimeDebug, Saturating};

cfg_if::cfg_if! {
if #[cfg(feature = "try-runtime")] {
use crate::{MarketIdOf};
use alloc::{format, vec::Vec};
use frame_support::{migration::storage_key_iter, pallet_prelude::Twox64Concat};
use sp_runtime::DispatchError;
}
}

cfg_if::cfg_if! {
if #[cfg(any(feature = "try-runtime", test))] {
const NEO_SWAPS: &[u8] = b"NeoSwaps";
const POOLS: &[u8] = b"Pools";
}
}

const NEO_SWAPS_REQUIRED_STORAGE_VERSION: u16 = 1;
const NEO_SWAPS_NEXT_STORAGE_VERSION: u16 = NEO_SWAPS_REQUIRED_STORAGE_VERSION + 1;

#[derive(Clone, Decode, Encode, Eq, PartialEq, RuntimeDebug, TypeInfo)]
#[scale_info(skip_type_params(T))]
pub struct OldPool<T, LSM>
where
T: Config,
LSM: LiquiditySharesManager<T>,
{
pub account_id: T::AccountId,
pub reserves: BTreeMap<AssetOf<T>, BalanceOf<T>>,
pub collateral: AssetOf<T>,
pub liquidity_parameter: BalanceOf<T>,
pub liquidity_shares_manager: LSM,
pub swap_fee: BalanceOf<T>,
}

type OldPoolOf<T> = OldPool<T, LiquidityTreeOf<T>>;

pub struct MigratePoolReservesToBoundedBTreeMap<T>(PhantomData<T>);

impl<T> OnRuntimeUpgrade for MigratePoolReservesToBoundedBTreeMap<T>
where
T: Config,
{
fn on_runtime_upgrade() -> Weight {
let mut total_weight = T::DbWeight::get().reads(1);
let neo_swaps_version = StorageVersion::get::<Pallet<T>>();
if neo_swaps_version != NEO_SWAPS_REQUIRED_STORAGE_VERSION {
log::info!(
"MigratePoolReservesToBoundedBTreeMap: neo-swaps version is {:?}, but {:?} is \
required",
neo_swaps_version,
NEO_SWAPS_REQUIRED_STORAGE_VERSION,
);
return total_weight;
}
log::info!("MigratePoolReservesToBoundedBTreeMap: Starting...");
let mut translated = 0u64;
Pools::<T>::translate::<OldPoolOf<T>, _>(|_, pool| {
// Can't fail unless `MaxAssets` is misconfigured. If it fails after all, we delete the
// pool. This may seem drastic, but is actually cleaner than trying some half-baked
// recovery and allows us to do a manual recovery of funds.
let reserves = pool.reserves.try_into().ok()?;
translated.saturating_inc();
Some(Pool {
account_id: pool.account_id,
reserves,
collateral: pool.collateral,
liquidity_parameter: pool.liquidity_parameter,
liquidity_shares_manager: pool.liquidity_shares_manager,
swap_fee: pool.swap_fee,
})
});
log::info!("MigratePoolReservesToBoundedBTreeMap: Upgraded {} pools.", translated);
total_weight =
total_weight.saturating_add(T::DbWeight::get().reads_writes(translated, translated));
StorageVersion::new(NEO_SWAPS_NEXT_STORAGE_VERSION).put::<Pallet<T>>();
total_weight = total_weight.saturating_add(T::DbWeight::get().writes(1));
log::info!("MigratePoolReservesToBoundedBTreeMap: Done!");
total_weight
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, DispatchError> {
let old_pools =
storage_key_iter::<MarketIdOf<T>, OldPoolOf<T>, Twox64Concat>(NEO_SWAPS, POOLS)
.collect::<BTreeMap<_, _>>();
Ok(old_pools.encode())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(previous_state: Vec<u8>) -> Result<(), DispatchError> {
let old_pools: BTreeMap<MarketIdOf<T>, OldPoolOf<T>> =
Decode::decode(&mut &previous_state[..])
.map_err(|_| "Failed to decode state: Invalid state")?;
let new_pool_count = Pools::<T>::iter().count();
assert_eq!(old_pools.len(), new_pool_count);
for (market_id, new_pool) in Pools::<T>::iter() {
let old_pool =
old_pools.get(&market_id).expect(&format!("Pool {:?} not found", market_id)[..]);
assert_eq!(new_pool.account_id, old_pool.account_id);
assert_eq!(new_pool.reserves.into_inner(), old_pool.reserves);
assert_eq!(new_pool.collateral, old_pool.collateral);
assert_eq!(new_pool.liquidity_parameter, old_pool.liquidity_parameter);
assert_eq!(new_pool.liquidity_shares_manager, old_pool.liquidity_shares_manager);
assert_eq!(new_pool.swap_fee, old_pool.swap_fee);
}
log::info!(
"MigratePoolReservesToBoundedBTreeMap: Post-upgrade pool count is {}!",
new_pool_count
);
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::{
liquidity_tree::types::LiquidityTree,
mock::{ExtBuilder, Runtime},
MarketIdOf, PoolOf, Pools,
};
use alloc::collections::BTreeMap;
use core::fmt::Debug;
use frame_support::{migration::put_storage_value, StorageHasher, Twox64Concat};
use parity_scale_codec::Encode;
use sp_io::storage::root as storage_root;
use sp_runtime::StateVersion;
use zeitgeist_primitives::types::Asset;

#[test]
fn on_runtime_upgrade_increments_the_storage_version() {
ExtBuilder::default().build().execute_with(|| {
set_up_version();
MigratePoolReservesToBoundedBTreeMap::<Runtime>::on_runtime_upgrade();
assert_eq!(StorageVersion::get::<Pallet<Runtime>>(), NEO_SWAPS_NEXT_STORAGE_VERSION);
});
}

#[test]
fn on_runtime_upgrade_is_noop_if_versions_are_not_correct() {
ExtBuilder::default().build().execute_with(|| {
StorageVersion::new(NEO_SWAPS_NEXT_STORAGE_VERSION).put::<Pallet<Runtime>>();
let (_, new_pools) = construct_old_new_tuple();
populate_test_data::<Twox64Concat, MarketIdOf<Runtime>, PoolOf<Runtime>>(
NEO_SWAPS, POOLS, new_pools,
);
let tmp = storage_root(StateVersion::V1);
MigratePoolReservesToBoundedBTreeMap::<Runtime>::on_runtime_upgrade();
assert_eq!(tmp, storage_root(StateVersion::V1));
});
}

#[test]
fn on_runtime_upgrade_correctly_updates_markets() {
ExtBuilder::default().build().execute_with(|| {
set_up_version();
let (old_pools, new_pools) = construct_old_new_tuple();
populate_test_data::<Twox64Concat, MarketIdOf<Runtime>, OldPoolOf<Runtime>>(
NEO_SWAPS, POOLS, old_pools,
);
MigratePoolReservesToBoundedBTreeMap::<Runtime>::on_runtime_upgrade();
let actual = Pools::get(0u128).unwrap();
assert_eq!(actual, new_pools[0]);
});
}

fn set_up_version() {
StorageVersion::new(NEO_SWAPS_REQUIRED_STORAGE_VERSION).put::<Pallet<Runtime>>();
}

fn construct_old_new_tuple() -> (Vec<OldPoolOf<Runtime>>, Vec<PoolOf<Runtime>>) {
let account_id = 1;
let mut old_reserves = BTreeMap::new();
old_reserves.insert(Asset::CategoricalOutcome(2, 3), 4);
let new_reserves = old_reserves.clone().try_into().unwrap();
let collateral = Asset::Ztg;
let liquidity_parameter = 5;
let swap_fee = 6;
let total_shares = 7;
let fees = 8;

let mut liquidity_shares_manager = LiquidityTree::new(account_id, total_shares).unwrap();
liquidity_shares_manager.nodes.get_mut(0).unwrap().fees = fees;

let old_pool = OldPoolOf {
account_id,
reserves: old_reserves,
collateral,
liquidity_parameter,
liquidity_shares_manager: liquidity_shares_manager.clone(),
swap_fee,
};
let new_pool = Pool {
account_id,
reserves: new_reserves,
collateral,
liquidity_parameter,
liquidity_shares_manager,
swap_fee,
};
(vec![old_pool], vec![new_pool])
}

#[allow(unused)]
fn populate_test_data<H, K, V>(pallet: &[u8], prefix: &[u8], data: Vec<V>)
where
H: StorageHasher,
K: TryFrom<usize> + Encode,
V: Encode + Clone,
<K as TryFrom<usize>>::Error: Debug,
{
for (key, value) in data.iter().enumerate() {
let storage_hash = utility::key_to_hash::<H, K>(K::try_from(key).unwrap());
put_storage_value::<V>(pallet, prefix, &storage_hash, (*value).clone());
}
}
}

mod utility {
use alloc::vec::Vec;
use frame_support::StorageHasher;
use parity_scale_codec::Encode;

#[allow(unused)]
pub fn key_to_hash<H, K>(key: K) -> Vec<u8>
where
H: StorageHasher,
K: Encode,
{
key.using_encoded(H::hash).as_ref().to_vec()
}
}
3 changes: 2 additions & 1 deletion zrml/neo-swaps/src/traits/liquidity_shares_manager.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 Forecasting Technologies LTD.
// Copyright 2023-2024 Forecasting Technologies LTD.
//
// This file is part of Zeitgeist.
//
Expand Down Expand Up @@ -34,6 +34,7 @@ pub trait LiquiditySharesManager<T: Config> {
fn exit(&mut self, who: &T::AccountId, amount: BalanceOf<T>) -> DispatchResult;

/// Transfer `amount` units of pool shares from `sender` to `receiver`.
#[allow(unused)]
fn split(
&mut self,
sender: &T::AccountId,
Expand Down
Loading