Skip to content

Commit

Permalink
fix: add set last pool id and assets migration (#879)
Browse files Browse the repository at this point in the history
* add set last pool id and assets migration

* fix trait
  • Loading branch information
1xstj authored Jan 13, 2025
1 parent c5844eb commit 7765025
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 3 deletions.
14 changes: 13 additions & 1 deletion pallets/tangle-lst/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,8 @@ pub mod pallet {

/// Ever increasing number of all pools created so far.
#[pallet::storage]
pub type LastPoolId<T: Config> = StorageValue<_, u32, ValueQuery>;
#[pallet::getter(fn last_pool_id)]
pub type LastPoolId<T: Config> = StorageValue<_, PoolId, ValueQuery>;

/// Unbonding members.
///
Expand Down Expand Up @@ -475,6 +476,8 @@ pub mod pallet {
MinBalanceDeficitAdjusted { pool_id: PoolId, amount: BalanceOf<T> },
/// Claimed excess frozen ED of the reward pool.
MinBalanceExcessAdjusted { pool_id: PoolId, amount: BalanceOf<T> },
/// The last PoolId is updated
LastPoolIdUpdated { pool_id: PoolId },
}

#[pallet::error]
Expand Down Expand Up @@ -1547,6 +1550,15 @@ pub mod pallet {

Ok(())
}

#[pallet::call_index(23)]
#[pallet::weight(T::WeightInfo::set_last_pool_id())]
pub fn set_last_pool_id(origin: OriginFor<T>, pool_id: PoolId) -> DispatchResult {
let _who = T::ForceOrigin::ensure_origin(origin)?;
LastPoolId::<T>::put(pool_id);
Self::deposit_event(Event::<T>::LastPoolIdUpdated { pool_id });
Ok(())
}
}

#[pallet::hooks]
Expand Down
22 changes: 22 additions & 0 deletions pallets/tangle-lst/src/tests/update_roles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,28 @@ fn update_roles_works() {
})
}

#[test]
fn set_last_pool_id_works() {
ExtBuilder::default().build().execute_with(|| {
// Non-root account should not be able to set last pool id
assert_noop!(Lst::set_last_pool_id(RuntimeOrigin::signed(1), 5), DispatchError::BadOrigin);

// Root should be able to set last pool id
assert_ok!(Lst::set_last_pool_id(RuntimeOrigin::root(), 5));

// Verify the storage is updated
assert_eq!(Lst::last_pool_id(), 5);

// Verify event is emitted
System::assert_has_event(Event::LastPoolIdUpdated { pool_id: 5 }.into());

// Root should be able to update it again
assert_ok!(Lst::set_last_pool_id(RuntimeOrigin::root(), 10));
assert_eq!(Lst::last_pool_id(), 10);
System::assert_has_event(Event::LastPoolIdUpdated { pool_id: 10 }.into());
});
}

const DOT: Balance = 10u128.pow(10u32);
const POLKADOT_TOTAL_ISSUANCE_GENESIS: Balance = DOT * 10u128.pow(9u32);

Expand Down
24 changes: 24 additions & 0 deletions pallets/tangle-lst/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub trait WeightInfo {
fn set_claim_permission() -> Weight;
fn claim_commission() -> Weight;
fn adjust_pool_deposit() -> Weight;
fn set_last_pool_id() -> Weight;
}

/// Weights for `pallet_nomination_pools` using the Substrate node and recommended hardware.
Expand Down Expand Up @@ -633,6 +634,17 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
.saturating_add(T::DbWeight::get().reads(4_u64))
.saturating_add(T::DbWeight::get().writes(2_u64))
}
/// Storage: `NominationPools::LastPoolId` (r:1 w:1)
/// Proof: `NominationPools::LastPoolId` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
fn set_last_pool_id() -> Weight {
// Proof Size summary in bytes:
// Measured: `901`
// Estimated: `4764`
// Minimum execution time: 65_462_000 picoseconds.
Weight::from_parts(67_250_000, 4764)
.saturating_add(T::DbWeight::get().reads(1_u64))
.saturating_add(T::DbWeight::get().writes(1_u64))
}
}

// For backwards compatibility and tests.
Expand Down Expand Up @@ -1207,4 +1219,16 @@ impl WeightInfo for () {
.saturating_add(RocksDbWeight::get().reads(4_u64))
.saturating_add(RocksDbWeight::get().writes(2_u64))
}

/// Storage: `NominationPools::LastPoolId` (r:1 w:1)
/// Proof: `NominationPools::LastPoolId` (`max_values`: Some(1), `max_size`: Some(4), added: 499, mode: `MaxEncodedLen`)
fn set_last_pool_id() -> Weight {
// Proof Size summary in bytes:
// Measured: `901`
// Estimated: `4764`
// Minimum execution time: 65_462_000 picoseconds.
Weight::from_parts(67_250_000, 4764)
.saturating_add(RocksDbWeight::get().reads(1_u64))
.saturating_add(RocksDbWeight::get().writes(1_u64))
}
}
10 changes: 9 additions & 1 deletion runtime/mainnet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1445,7 +1445,15 @@ pub type Executive = frame_executive::Executive<
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
migrations::MigrateSessionKeys<Runtime>,
(
migrations::MigrateSessionKeys<Runtime>,
// AssetId limits
// 0 - 1000 (reserved for future use)
// 1000 - 50000 (reserved for LST pools)
// 50000 - 1000000 (reserved for native assets)
// set user start at 50_000, everything below is reserved for system use
migrations::SetNextAssetId<ConstU128<50_000>, Runtime>,
),
>;

impl fp_self_contained::SelfContainedCall for RuntimeCall {
Expand Down
19 changes: 19 additions & 0 deletions runtime/mainnet/src/migrations.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use super::*;
use frame_support::traits::Incrementable;
use frame_support::traits::OnRuntimeUpgrade;
use pallet_assets::NextAssetId;
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use sp_runtime::{BoundToRuntimeAppPublic, RuntimeAppPublic, RuntimeDebug};

Expand Down Expand Up @@ -69,3 +71,20 @@ impl<T: pallet_session::Config> OnRuntimeUpgrade for MigrateSessionKeys<T> {
T::DbWeight::get().reads_writes(10, 10)
}
}

/// Set [`NextAssetId`] to the value of `ID` if [`NextAssetId`] does not exist yet.
pub struct SetNextAssetId<ID, T: pallet_assets::Config>(core::marker::PhantomData<(ID, T)>);
impl<ID, T: pallet_assets::Config> OnRuntimeUpgrade for SetNextAssetId<ID, T>
where
T::AssetId: Incrementable,
ID: Get<T::AssetId>,
{
fn on_runtime_upgrade() -> frame_support::weights::Weight {
if !NextAssetId::<T>::exists() {
NextAssetId::<T>::put(ID::get());
T::DbWeight::get().reads_writes(1, 1)
} else {
T::DbWeight::get().reads(1)
}
}
}
10 changes: 9 additions & 1 deletion runtime/testnet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1356,7 +1356,15 @@ pub type Executive = frame_executive::Executive<
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
migrations::MigrateSessionKeys<Runtime>,
(
migrations::MigrateSessionKeys<Runtime>,
// AssetId limits
// 0 - 1000 (reserved for future use)
// 1000 - 50000 (reserved for LST pools)
// 50000 - 1000000 (reserved for native assets)
// set user start at 50_000, everything below is reserved for system use
migrations::SetNextAssetId<ConstU128<50_000>, Runtime>,
),
>;

impl fp_self_contained::SelfContainedCall for RuntimeCall {
Expand Down
19 changes: 19 additions & 0 deletions runtime/testnet/src/migrations.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use super::*;
use frame_support::traits::Incrementable;
use frame_support::traits::OnRuntimeUpgrade;
use pallet_assets::NextAssetId;
use parity_scale_codec::{Decode, Encode, MaxEncodedLen};
use sp_runtime::{BoundToRuntimeAppPublic, RuntimeAppPublic, RuntimeDebug};

Expand Down Expand Up @@ -69,3 +71,20 @@ impl<T: pallet_session::Config> OnRuntimeUpgrade for MigrateSessionKeys<T> {
T::DbWeight::get().reads_writes(10, 10)
}
}

/// Set [`NextAssetId`] to the value of `ID` if [`NextAssetId`] does not exist yet.
pub struct SetNextAssetId<ID, T: pallet_assets::Config>(core::marker::PhantomData<(ID, T)>);
impl<ID, T: pallet_assets::Config> OnRuntimeUpgrade for SetNextAssetId<ID, T>
where
T::AssetId: Incrementable,
ID: Get<T::AssetId>,
{
fn on_runtime_upgrade() -> frame_support::weights::Weight {
if !NextAssetId::<T>::exists() {
NextAssetId::<T>::put(ID::get());
T::DbWeight::get().reads_writes(1, 1)
} else {
T::DbWeight::get().reads(1)
}
}
}

0 comments on commit 7765025

Please sign in to comment.