Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/bko-bump-to-1.5' into bko-bump-t…
Browse files Browse the repository at this point in the history
…o-1.6
  • Loading branch information
bkontur committed Feb 14, 2024
2 parents 1ba0c11 + e7dffea commit 35f0e51
Show file tree
Hide file tree
Showing 27 changed files with 219 additions and 901 deletions.
216 changes: 108 additions & 108 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion integration-tests/emulated/chains/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ polkadot-parachain-primitives = { version = "6.0.0" }
parachains-common = { version = "7.0.0" }
cumulus-primitives-core = { version = "0.7.0" }
xcm-emulator = { version = "0.5.0" }
penpal-runtime = { version = "0.14.0" }
penpal-runtime = { version = "0.14.3" }

# Local runtimes
kusama-runtime = { package = "staging-kusama-runtime", path = "../../../relay/kusama" }
Expand Down
8 changes: 4 additions & 4 deletions relay/kusama/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pallet-membership = { default-features = false , version = "28.0.0" }
pallet-message-queue = { default-features = false , version = "31.0.0" }
pallet-mmr = { default-features = false , version = "27.0.0" }
pallet-multisig = { default-features = false , version = "28.0.0" }
pallet-nomination-pools = { default-features = false , version = "25.0.0" }
pallet-nomination-pools = { default-features = false , version = "25.0.3" }
pallet-offences = { default-features = false , version = "27.0.0" }
pallet-preimage = { default-features = false , version = "28.0.0" }
pallet-proxy = { default-features = false , version = "28.0.0" }
Expand All @@ -94,7 +94,7 @@ pallet-utility = { default-features = false , version = "28.0.0" }
pallet-vesting = { default-features = false , version = "28.0.0" }
pallet-whitelist = { default-features = false , version = "27.0.0" }
pallet-xcm = { default-features = false , version = "7.0.0" }
pallet-xcm-benchmarks = { default-features = false, optional = true , version = "7.0.0" }
pallet-xcm-benchmarks = { default-features = false, optional = true , version = "7.0.3" }
frame-election-provider-support = { default-features = false , version = "28.0.0" }

frame-benchmarking = { default-features = false, optional = true , version = "28.0.0" }
Expand All @@ -111,8 +111,8 @@ runtime-parachains = { package = "polkadot-runtime-parachains", default-features
primitives = { package = "polkadot-primitives", default-features = false , version = "7.0.0" }

xcm = { package = "staging-xcm", default-features = false , version = "7.0.0" }
xcm-executor = { package = "staging-xcm-executor", default-features = false , version = "7.0.0" }
xcm-builder = { package = "staging-xcm-builder", default-features = false , version = "7.0.0" }
xcm-executor = { package = "staging-xcm-executor", default-features = false , version = "7.0.3" }
xcm-builder = { package = "staging-xcm-builder", default-features = false , version = "7.0.3" }

sp-debug-derive = { default-features = false, version = "14.0.0" }

Expand Down
321 changes: 8 additions & 313 deletions relay/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1691,12 +1691,8 @@ pub mod migrations {
/// Unreleased migrations. Add new ones here:
pub type Unreleased = (
pallet_nomination_pools::migration::versioned::V5toV6<Runtime>,
// TODO:(PR#137) - replace with fixed/released version
crate::test_oliverfix_migration::V6ToV7<Runtime>,
// pallet_nomination_pools::migration::versioned::V6ToV7<Runtime>,
// TODO:(PR#137) - replace with fixed/released version
crate::test_oliverfix_migration::V7ToV8<Runtime>,
// pallet_nomination_pools::migration::versioned::V7ToV8<Runtime>,
pallet_nomination_pools::migration::versioned::V6ToV7<Runtime>,
pallet_nomination_pools::migration::versioned::V7ToV8<Runtime>,
pallet_staking::migrations::v14::MigrateToV14<Runtime>,
parachains_configuration::migration::v10::MigrateToV10<Runtime>,
parachains_configuration::migration::v11::MigrateToV11<Runtime>,
Expand Down Expand Up @@ -2483,13 +2479,12 @@ sp_api::impl_runtime_apis! {
Ok((origin, ticket, assets))
}

// TODO:(PR#137) - revert back with `polkadot-sdk@1.6.0` or `polkadot-sdk@1.7.0`
// fn fee_asset() -> Result<MultiAsset, BenchmarkError> {
// Ok(MultiAsset {
// id: Concrete(TokenLocation::get()),
// fun: Fungible(1_000_000 * UNITS),
// })
// }
fn fee_asset() -> Result<MultiAsset, BenchmarkError> {
Ok(MultiAsset {
id: Concrete(TokenLocation::get()),
fun: Fungible(1_000_000 * UNITS),
})
}

fn unlockable_asset() -> Result<(MultiLocation, MultiLocation, MultiAsset), BenchmarkError> {
// Kusama doesn't support asset locking
Expand Down Expand Up @@ -2805,303 +2800,3 @@ mod init_state_migration {
}
}
}

// TODO:(PR#137) - replace with fixed/released version
mod test_oliverfix_migration {
use super::*;
use frame_support::{
traits::OnRuntimeUpgrade, DebugNoBound, RuntimeDebugNoBound, Twox64Concat,
};
use frame_system::pallet_prelude::BlockNumberFor;
use pallet_nomination_pools::*;
use sp_runtime::Saturating;

pub type V7ToV8<T> = frame_support::migrations::VersionedMigration<
7,
8,
v8::VersionUncheckedMigrateV7ToV8<T>,
pallet_nomination_pools::pallet::Pallet<T>,
<T as frame_system::Config>::DbWeight,
>;

pub type V6ToV7<T> = frame_support::migrations::VersionedMigration<
6,
7,
v7::VersionUncheckedMigrateV6ToV7<T>,
pallet_nomination_pools::pallet::Pallet<T>,
<T as frame_system::Config>::DbWeight,
>;

pub mod v8 {
use super::{v7::V7BondedPoolInner, *};

impl<T: Config> V7BondedPoolInner<T> {
fn migrate_to_v8(self) -> BondedPoolInner<T> {
BondedPoolInner {
commission: Commission {
current: self.commission.current,
max: self.commission.max,
change_rate: self.commission.change_rate,
throttle_from: self.commission.throttle_from,
// `claim_permission` is a new field.
claim_permission: None,
},
member_counter: self.member_counter,
points: self.points,
roles: self.roles,
state: self.state,
}
}
}

pub struct VersionUncheckedMigrateV7ToV8<T>(sp_std::marker::PhantomData<T>);
impl<T: Config> OnRuntimeUpgrade for VersionUncheckedMigrateV7ToV8<T> {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::TryRuntimeError> {
Ok(Vec::new())
}

fn on_runtime_upgrade() -> Weight {
let mut translated = 0u64;
BondedPools::<T>::translate::<V7BondedPoolInner<T>, _>(|_key, old_value| {
translated.saturating_inc();
Some(old_value.migrate_to_v8())
});
T::DbWeight::get().reads_writes(translated, translated + 1)
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_: Vec<u8>) -> Result<(), sp_runtime::TryRuntimeError> {
// Check new `claim_permission` field is present.
frame_support::ensure!(
BondedPools::<T>::iter()
.all(|(_, inner)| inner.commission.claim_permission.is_none()),
"`claim_permission` value has not been set correctly."
);
use frame_support::traits::GetStorageVersion;
frame_support::ensure!(
Pallet::<T>::on_chain_storage_version() >= 8,
"nomination-pools::migration::v8: wrong storage version"
);
Ok(())
}
}
}

/// This migration accumulates and initializes the [`TotalValueLocked`] for all pools.
///
/// WARNING: This migration works under the assumption that the [`BondedPools`] cannot be
/// inflated arbitrarily. Otherwise this migration could fail due to too high weight.
pub(crate) mod v7 {
use super::*;
use frame_support::{BoundedBTreeMap, DefaultNoBound};
use sp_runtime::traits::{Convert, Zero};
use sp_staking::EraIndex;

#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, DebugNoBound, PartialEq, Clone)]
#[codec(mel_bound(T: Config))]
#[scale_info(skip_type_params(T))]
pub struct V7Commission<T: Config> {
pub current: Option<(Perbill, T::AccountId)>,
pub max: Option<Perbill>,
pub change_rate: Option<CommissionChangeRate<BlockNumberFor<T>>>,
pub throttle_from: Option<BlockNumberFor<T>>,
}

#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, DebugNoBound, PartialEq, Clone)]
#[codec(mel_bound(T: Config))]
#[scale_info(skip_type_params(T))]
pub struct V7BondedPoolInner<T: Config> {
pub commission: V7Commission<T>,
pub member_counter: u32,
pub points: BalanceOf<T>,
pub roles: PoolRoles<T::AccountId>,
pub state: PoolState,
}

#[allow(dead_code)]
#[derive(RuntimeDebugNoBound)]
#[cfg_attr(feature = "std", derive(Clone, PartialEq))]
pub struct V7BondedPool<T: Config> {
/// The identifier of the pool.
id: PoolId,
/// The inner fields.
inner: V7BondedPoolInner<T>,
}

impl<T: Config> V7BondedPool<T> {
fn bonded_account(&self) -> T::AccountId {
Pallet::<T>::create_bonded_account(self.id)
}
fn points_to_balance(&self, points: BalanceOf<T>) -> BalanceOf<T> {
use sp_staking::StakingInterface;
let bonded_balance =
T::Staking::active_stake(&self.bonded_account()).unwrap_or(Zero::zero());
point_to_balance::<T>(bonded_balance, self.inner.points, points)
}
}

// NOTE: We cannot put a V7 prefix here since that would change the storage key.
#[frame_support::storage_alias]
pub type BondedPools<T: Config> =
CountedStorageMap<Pallet<T>, Twox64Concat, PoolId, V7BondedPoolInner<T>>;

pub struct VersionUncheckedMigrateV6ToV7<T>(sp_std::marker::PhantomData<T>);
impl<T: Config> VersionUncheckedMigrateV6ToV7<T> {
fn calculate_tvl_by_total_stake() -> BalanceOf<T> {
use sp_staking::StakingInterface;
BondedPools::<T>::iter()
.map(|(id, inner)| {
T::Staking::total_stake(
&V7BondedPool { id, inner: inner.clone() }.bonded_account(),
)
.unwrap_or_default()
})
.reduce(|acc, total_balance| acc + total_balance)
.unwrap_or_default()
}
}

impl<T: Config> OnRuntimeUpgrade for VersionUncheckedMigrateV6ToV7<T> {
fn on_runtime_upgrade() -> Weight {
let migrated = BondedPools::<T>::count();
// The TVL should be the sum of all the funds that are actively staked and in the
// unbonding process of the account of each pool.
let tvl: BalanceOf<T> = Self::calculate_tvl_by_total_stake();

TotalValueLocked::<T>::set(tvl);

log!(info, "Upgraded {} pools with a TVL of {:?}", migrated, tvl);

// reads: migrated * (BondedPools + Staking::total_stake) + count + onchain
// version
//
// writes: current version + TVL
T::DbWeight::get()
.reads_writes(migrated.saturating_mul(2).saturating_add(2).into(), 2)
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::TryRuntimeError> {
Ok(Vec::new())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_data: Vec<u8>) -> Result<(), sp_runtime::TryRuntimeError> {
// check that the `TotalValueLocked` written is actually the sum of `total_stake` of
// the `BondedPools``
let tvl: BalanceOf<T> = Self::calculate_tvl_by_total_stake();
frame_support::ensure!(
TotalValueLocked::<T>::get() == tvl,
"TVL written is not equal to `Staking::total_stake` of all `BondedPools`."
);

// calculate the sum of `total_balance` of all `PoolMember` as the upper bound for
// the `TotalValueLocked`.
let total_balance_members: BalanceOf<T> = PoolMembers::<T>::iter()
.map(|(_, member)| total_balance(&member))
.reduce(|acc, total_balance| acc + total_balance)
.unwrap_or_default();

frame_support::ensure!(
TotalValueLocked::<T>::get() <= total_balance_members,
"TVL is greater than the balance of all PoolMembers."
);

use frame_support::traits::GetStorageVersion;
frame_support::ensure!(
Pallet::<T>::on_chain_storage_version() >= 7,
"nomination-pools::migration::v7: wrong storage version"
);

Ok(())
}
}

#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, DefaultNoBound, RuntimeDebugNoBound)]
#[cfg_attr(feature = "std", derive(Clone, PartialEq, Eq))]
#[codec(mel_bound(T: Config))]
#[scale_info(skip_type_params(T))]
pub struct UnbondPool<T: Config> {
/// The points in this pool.
points: BalanceOf<T>,
/// The funds in the pool.
balance: BalanceOf<T>,
}

impl<T: Config> UnbondPool<T> {
fn point_to_balance(&self, points: BalanceOf<T>) -> BalanceOf<T> {
point_to_balance::<T>(self.balance, self.points, points)
}
}

#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, DefaultNoBound, RuntimeDebugNoBound)]
#[cfg_attr(feature = "std", derive(Clone, PartialEq))]
#[codec(mel_bound(T: Config))]
#[scale_info(skip_type_params(T))]
pub struct SubPools<T: Config> {
/// A general, era agnostic pool of funds that have fully unbonded. The pools
/// of `Self::with_era` will lazily be merged into into this pool if they are
/// older then `current_era - TotalUnbondingPools`.
no_era: UnbondPool<T>,
/// Map of era in which a pool becomes unbonded in => unbond pools.
with_era: BoundedBTreeMap<EraIndex, UnbondPool<T>, TotalUnbondingPools<T>>,
}

#[frame_support::storage_alias]
pub type SubPoolsStorage<T: Config> =
CountedStorageMap<Pallet<T>, Twox64Concat, PoolId, SubPools<T>>;

// TODO:(PR#137) - hacky copy of pallet-nomination-pools, because lots of this stuff is
// private there.
fn total_balance<T: Config>(self_as_member: &PoolMember<T>) -> BalanceOf<T> {
// let pool = V7BondedPool::<T>::get(self_as_member.pool_id).unwrap();
let id = self_as_member.pool_id;
let pool = BondedPools::<T>::try_get(id)
.ok()
.map(|inner| V7BondedPool { id, inner })
.unwrap();
let active_balance = pool.points_to_balance(self_as_member.points);

let sub_pools = match SubPoolsStorage::<T>::get(self_as_member.pool_id) {
Some(sub_pools) => sub_pools,
None => return active_balance,
};

let unbonding_balance = self_as_member.unbonding_eras.iter().fold(
BalanceOf::<T>::zero(),
|accumulator, (era, unlocked_points)| {
// if the `SubPools::with_era` has already been merged into the
// `SubPools::no_era` use this pool instead.
let era_pool = sub_pools.with_era.get(era).unwrap_or(&sub_pools.no_era);
accumulator + (era_pool.point_to_balance(*unlocked_points))
},
);

active_balance + unbonding_balance
}

fn point_to_balance<T: Config>(
current_balance: BalanceOf<T>,
current_points: BalanceOf<T>,
points: BalanceOf<T>,
) -> BalanceOf<T> {
let u256 = T::BalanceToU256::convert;
let balance = T::U256ToBalance::convert;
if current_balance.is_zero() || current_points.is_zero() || points.is_zero() {
// There is nothing to unbond
return Zero::zero()
}

use sp_std::ops::Div;

// Equivalent of (current_balance / current_points) * points
balance(
u256(current_balance)
.saturating_mul(u256(points))
// We check for zero above
.div(u256(current_points)),
)
}
}
}
7 changes: 2 additions & 5 deletions relay/kusama/src/xcm_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ use xcm_builder::CurrencyAdapter as XcmCurrencyAdapter;
use xcm_builder::{
AccountId32Aliases, AllowExplicitUnpaidExecutionFrom, AllowKnownQueryResponses,
AllowSubscriptionsFrom, AllowTopLevelPaidExecutionFrom, ChildParachainAsNative,
ChildParachainConvertsVia, DescribeAllTerminal, DescribeFamily,
/* TODO:(PR#137) - wait for xcm-executor patch or `polkadot-sdk@1.7.0`
* FrameTransactionalProcessor, */
ChildParachainConvertsVia, DescribeAllTerminal, DescribeFamily, FrameTransactionalProcessor,
HashedDescription, IsChildSystemParachain, IsConcrete, MintLocation, OriginToPluralityVoice,
SignedAccountId32AsNative, SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit,
TrailingSetTopicAsId, UsingComponents, WeightInfoBounds, WithComputedOrigin, WithUniqueTopic,
Expand Down Expand Up @@ -217,8 +215,7 @@ impl xcm_executor::Config for XcmConfig {
type CallDispatcher = RuntimeCall;
type SafeCallFilter = Everything;
type Aliasers = Nothing;
// TODO:(PR#137) - wait for xcm-executor patch or `polkadot-sdk@1.7.0`
// type TransactionalProcessor = FrameTransactionalProcessor;
type TransactionalProcessor = FrameTransactionalProcessor;
}

parameter_types! {
Expand Down
Loading

0 comments on commit 35f0e51

Please sign in to comment.