Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Improve storage_alias and make UnlockAndUnreserveAllFunds independent of the pallet #14773

Merged
merged 8 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions Cargo.lock

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

101 changes: 85 additions & 16 deletions frame/democracy/src/migrations/unlock_and_unreserve_all_funds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,66 @@
//! A migration that unreserves all deposit and unlocks all stake held in the context of this
//! pallet.

use crate::{BalanceOf, DEMOCRACY_ID};
use crate::{PropIndex, Voting, DEMOCRACY_ID};
use core::iter::Sum;
use frame_support::traits::{LockableCurrency, OnRuntimeUpgrade, ReservableCurrency};
use frame_support::{
pallet_prelude::ValueQuery,
storage_alias,
traits::{Currency, LockableCurrency, OnRuntimeUpgrade, ReservableCurrency},
weights::RuntimeDbWeight,
Parameter, Twox64Concat,
};
use sp_core::Get;
use sp_runtime::{traits::Zero, Saturating};
use sp_runtime::{traits::Zero, BoundedVec, Saturating};
use sp_std::{collections::btree_map::BTreeMap, vec::Vec};

const LOG_TARGET: &str = "runtime::democracy::migrations::unlock_and_unreserve_all_funds";

type BalanceOf<T> =
<<T as UnlockConfig>::Currency as Currency<<T as UnlockConfig>::AccountId>>::Balance;

/// The configuration for [`UnlockAndUnreserveAllFunds`].
pub trait UnlockConfig: 'static {
/// The account id as being used in the runtime.
bkchr marked this conversation as resolved.
Show resolved Hide resolved
type AccountId: Parameter + Ord;
/// The currency type being used in the runtime.
bkchr marked this conversation as resolved.
Show resolved Hide resolved
///
/// Should be the same as previously used for the pallet.
bkchr marked this conversation as resolved.
Show resolved Hide resolved
type Currency: LockableCurrency<Self::AccountId> + ReservableCurrency<Self::AccountId>;
/// The name of the pallet as previously configured in `construct_runtime!`.
bkchr marked this conversation as resolved.
Show resolved Hide resolved
bkchr marked this conversation as resolved.
Show resolved Hide resolved
type PalletName: Get<&'static str>;
/// The maximum number of votes as configured previously in the runtime.
type MaxVotes: Get<u32>;
/// The maximum deposit as configured previously in the runtime.
type MaxDeposits: Get<u32>;
/// The DB weight as configured in the runtime to calculate the correct weight.
type DbWeight: Get<RuntimeDbWeight>;
/// The block number as configured in the runtime.
type BlockNumber: Parameter + Zero + Copy + Ord;
}

#[storage_alias(dynamic)]
type DepositOf<T: UnlockConfig> = StorageMap<
<T as UnlockConfig>::PalletName,
Twox64Concat,
PropIndex,
(BoundedVec<<T as UnlockConfig>::AccountId, <T as UnlockConfig>::MaxDeposits>, BalanceOf<T>),
>;

#[storage_alias(dynamic)]
type VotingOf<T: UnlockConfig> = StorageMap<
<T as UnlockConfig>::PalletName,
Twox64Concat,
<T as UnlockConfig>::AccountId,
Voting<
BalanceOf<T>,
<T as UnlockConfig>::AccountId,
<T as UnlockConfig>::BlockNumber,
<T as UnlockConfig>::MaxVotes,
>,
ValueQuery,
>;

/// A migration that unreserves all deposit and unlocks all stake held in the context of this
/// pallet.
///
Expand All @@ -35,9 +86,9 @@ const LOG_TARGET: &str = "runtime::democracy::migrations::unlock_and_unreserve_a
/// The pallet should be made inoperable before this migration is run.
///
/// (See also [`RemovePallet`][frame_support::migrations::RemovePallet])
pub struct UnlockAndUnreserveAllFunds<T: crate::Config>(sp_std::marker::PhantomData<T>);
pub struct UnlockAndUnreserveAllFunds<T: UnlockConfig>(sp_std::marker::PhantomData<T>);

impl<T: crate::Config> UnlockAndUnreserveAllFunds<T> {
impl<T: UnlockConfig> UnlockAndUnreserveAllFunds<T> {
/// Calculates and returns the total amounts reserved by each account by this pallet, and all
/// accounts with locks in the context of this pallet.
///
Expand All @@ -63,7 +114,7 @@ impl<T: crate::Config> UnlockAndUnreserveAllFunds<T> {

// Get all deposits (reserved).
let mut total_voting_vec_entries: u64 = 0;
let account_deposits: BTreeMap<T::AccountId, BalanceOf<T>> = crate::DepositOf::<T>::iter()
let account_deposits: BTreeMap<T::AccountId, BalanceOf<T>> = DepositOf::<T>::iter()
.flat_map(|(_prop_index, (accounts, balance))| {
// Count the number of deposits
deposit_of_len.saturating_inc();
Expand All @@ -81,7 +132,7 @@ impl<T: crate::Config> UnlockAndUnreserveAllFunds<T> {
});

// Voter accounts have amounts locked.
let account_stakes: BTreeMap<T::AccountId, BalanceOf<T>> = crate::VotingOf::<T>::iter()
let account_stakes: BTreeMap<T::AccountId, BalanceOf<T>> = VotingOf::<T>::iter()
.map(|(account_id, voting)| (account_id, voting.locked_balance()))
.collect();
let voting_of_len = account_stakes.len() as u64;
Expand All @@ -100,7 +151,7 @@ impl<T: crate::Config> UnlockAndUnreserveAllFunds<T> {
}
}

impl<T: crate::Config> OnRuntimeUpgrade for UnlockAndUnreserveAllFunds<T>
impl<T: UnlockConfig> OnRuntimeUpgrade for UnlockAndUnreserveAllFunds<T>
where
BalanceOf<T>: Sum,
{
Expand Down Expand Up @@ -241,14 +292,32 @@ where
mod test {
use super::*;
use crate::{
tests::{new_test_ext, Test},
tests::{new_test_ext, Balances, Test},
DepositOf, Voting, VotingOf,
};
use frame_support::{
assert_ok,
assert_ok, parameter_types,
traits::{Currency, OnRuntimeUpgrade, ReservableCurrency, WithdrawReasons},
BoundedVec,
};
use frame_system::pallet_prelude::BlockNumberFor;
use sp_core::ConstU32;

parameter_types! {
const PalletName: &'static str = "Democracy";
}

struct UnlockConfigImpl;

impl super::UnlockConfig for UnlockConfigImpl {
type Currency = Balances;
type MaxVotes = ConstU32<100>;
type MaxDeposits = ConstU32<1000>;
type AccountId = u64;
type BlockNumber = BlockNumberFor<Test>;
type DbWeight = ();
type PalletName = PalletName;
}

#[test]
fn unreserve_works_for_depositer() {
Expand Down Expand Up @@ -288,10 +357,10 @@ mod test {
);

// Run the migration.
let bytes = UnlockAndUnreserveAllFunds::<Test>::pre_upgrade()
let bytes = UnlockAndUnreserveAllFunds::<UnlockConfigImpl>::pre_upgrade()
.unwrap_or_else(|e| panic!("pre_upgrade failed: {:?}", e));
UnlockAndUnreserveAllFunds::<Test>::on_runtime_upgrade();
assert_ok!(UnlockAndUnreserveAllFunds::<Test>::post_upgrade(bytes));
UnlockAndUnreserveAllFunds::<UnlockConfigImpl>::on_runtime_upgrade();
assert_ok!(UnlockAndUnreserveAllFunds::<UnlockConfigImpl>::post_upgrade(bytes));

// Assert the reserved balance was reduced by the expected amount.
assert_eq!(
Expand Down Expand Up @@ -342,10 +411,10 @@ mod test {
);

// Run the migration.
let bytes = UnlockAndUnreserveAllFunds::<Test>::pre_upgrade()
let bytes = UnlockAndUnreserveAllFunds::<UnlockConfigImpl>::pre_upgrade()
.unwrap_or_else(|e| panic!("pre_upgrade failed: {:?}", e));
UnlockAndUnreserveAllFunds::<Test>::on_runtime_upgrade();
assert_ok!(UnlockAndUnreserveAllFunds::<Test>::post_upgrade(bytes));
UnlockAndUnreserveAllFunds::<UnlockConfigImpl>::on_runtime_upgrade();
assert_ok!(UnlockAndUnreserveAllFunds::<UnlockConfigImpl>::post_upgrade(bytes));

// Assert the voter lock was removed
assert_eq!(
Expand Down
1 change: 1 addition & 0 deletions frame/support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ k256 = { version = "0.13.1", default-features = false, features = ["ecdsa"] }
environmental = { version = "1.1.4", default-features = false }
sp-genesis-builder = { version = "0.1.0", default-features=false, path = "../../primitives/genesis-builder" }
serde_json = { version = "1.0.85", default-features = false, features = ["alloc"] }
docify = "0.2.1"

aquamarine = { version = "0.3.2" }

Expand Down
4 changes: 2 additions & 2 deletions frame/support/procedural/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,8 +547,8 @@ pub fn __create_tt_macro(input: TokenStream) -> TokenStream {
}

#[proc_macro_attribute]
pub fn storage_alias(_: TokenStream, input: TokenStream) -> TokenStream {
storage_alias::storage_alias(input.into())
pub fn storage_alias(attributes: TokenStream, input: TokenStream) -> TokenStream {
storage_alias::storage_alias(attributes.into(), input.into())
.unwrap_or_else(|r| r.into_compile_error())
.into()
}
Expand Down
Loading