Skip to content

Commit

Permalink
add the pool filter to runtimes
Browse files Browse the repository at this point in the history
  • Loading branch information
Ank4n committed Feb 24, 2025
1 parent d96f148 commit 66bc9b3
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 54 deletions.
3 changes: 2 additions & 1 deletion polkadot/runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use frame_support::{
traits::{
fungible::HoldConsideration, tokens::UnityOrOuterConversion, ConstU32, Contains, EitherOf,
EitherOfDiverse, EnsureOriginWithArg, EverythingBut, FromContains, InstanceFilter,
KeyOwnerProofSystem, LinearStoragePrice, ProcessMessage, ProcessMessageError,
KeyOwnerProofSystem, LinearStoragePrice, Nothing, ProcessMessage, ProcessMessageError,
VariantCountOf, WithdrawReasons,
},
weights::{ConstantMultiplier, WeightMeter, WeightToFee as _},
Expand Down Expand Up @@ -1547,6 +1547,7 @@ impl pallet_nomination_pools::Config for Runtime {
type MaxPointsToBalance = MaxPointsToBalance;
type AdminOrigin = EitherOf<EnsureRoot<AccountId>, StakingAdmin>;
type BlockNumberProvider = System;
type Filter = Nothing;
}

parameter_types! {
Expand Down
1 change: 1 addition & 0 deletions substrate/bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,7 @@ impl pallet_nomination_pools::Config for Runtime {
pallet_collective::EnsureProportionAtLeast<AccountId, CouncilCollective, 3, 4>,
>;
type BlockNumberProvider = System;
type Filter = Nothing;
}

parameter_types! {
Expand Down
10 changes: 0 additions & 10 deletions substrate/frame/delegated-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,6 @@ pub mod pallet {
// Existing `agent` cannot register again and a delegator cannot become an `agent`.
ensure!(!Self::is_agent(&who) && !Self::is_delegator(&who), Error::<T>::NotAllowed);

// They cannot be already a direct staker in the staking pallet.
ensure!(!Self::is_direct_staker(&who), Error::<T>::AlreadyStaking);

// Reward account cannot be same as `agent` account.
ensure!(reward_account != who, Error::<T>::InvalidRewardDestination);

Expand Down Expand Up @@ -407,7 +404,6 @@ pub mod pallet {
// Ensure delegator is sane.
ensure!(!Self::is_agent(&delegator), Error::<T>::NotAllowed);
ensure!(!Self::is_delegator(&delegator), Error::<T>::NotAllowed);
ensure!(!Self::is_direct_staker(&delegator), Error::<T>::AlreadyStaking);

// ensure agent is sane.
ensure!(Self::is_agent(&agent), Error::<T>::NotAgent);
Expand Down Expand Up @@ -442,12 +438,6 @@ pub mod pallet {
Error::<T>::InvalidDelegation
);

// Implementation note: Staking uses deprecated locks (similar to freeze) which are not
// mutually exclusive of holds. This means, if we allow delegating for existing stakers,
// already staked funds might be reused for delegation. We avoid that by just blocking
// this.
ensure!(!Self::is_direct_staker(&delegator), Error::<T>::AlreadyStaking);

// ensure agent is sane.
ensure!(Self::is_agent(&agent), Error::<T>::NotAgent);

Expand Down
1 change: 1 addition & 0 deletions substrate/frame/delegated-staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ impl pallet_nomination_pools::Config for Runtime {
pallet_nomination_pools::adapter::DelegateStake<Self, Staking, DelegatedStaking>;
type AdminOrigin = frame_system::EnsureRoot<Self::AccountId>;
type BlockNumberProvider = System;
type Filter = pallet_staking::AllStakers<Runtime>;
}

frame_support::construct_runtime!(
Expand Down
68 changes: 30 additions & 38 deletions substrate/frame/delegated-staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,31 +65,6 @@ fn cannot_become_agent() {
DelegatedStaking::register_agent(RawOrigin::Signed(100).into(), 100),
Error::<T>::InvalidRewardDestination
);

// an existing validator cannot become agent
assert_noop!(
DelegatedStaking::register_agent(
RawOrigin::Signed(mock::GENESIS_VALIDATOR).into(),
100
),
Error::<T>::AlreadyStaking
);

// an existing direct staker to `CoreStaking` cannot become an agent.
assert_noop!(
DelegatedStaking::register_agent(
RawOrigin::Signed(mock::GENESIS_NOMINATOR_ONE).into(),
100
),
Error::<T>::AlreadyStaking
);
assert_noop!(
DelegatedStaking::register_agent(
RawOrigin::Signed(mock::GENESIS_NOMINATOR_TWO).into(),
100
),
Error::<T>::AlreadyStaking
);
});
}

Expand Down Expand Up @@ -637,18 +612,6 @@ mod staking_integration {
DelegatedStaking::register_agent(RawOrigin::Signed(202).into(), 203),
Error::<T>::NotAllowed
);
// existing staker cannot become a delegate
assert_noop!(
DelegatedStaking::register_agent(
RawOrigin::Signed(GENESIS_NOMINATOR_ONE).into(),
201
),
Error::<T>::AlreadyStaking
);
assert_noop!(
DelegatedStaking::register_agent(RawOrigin::Signed(GENESIS_VALIDATOR).into(), 201),
Error::<T>::AlreadyStaking
);
});
}

Expand Down Expand Up @@ -1390,7 +1353,7 @@ mod pool_integration {
// The delegator cannot add any extra bond to the pool anymore.
assert_noop!(
Pools::bond_extra(RawOrigin::Signed(delegator).into(), BondExtra::FreeBalance(100)),
Error::<T>::AlreadyStaking
PoolsError::<T>::Restricted
);

// But they can unbond
Expand All @@ -1401,6 +1364,35 @@ mod pool_integration {
});
}

#[test]
fn stakers_cannot_join_pool() {
// A pool member is able to stake directly since staking only uses free funds but once a
// staker, they cannot join/add extra bond to the pool. They can still withdraw funds.
ExtBuilder::default().build_and_execute(|| {
start_era(1);
// GIVEN: a pool.
fund(&200, 1000);
let pool_id = create_pool(200, 800);

// WHEN: an account is a staker.
let staker = 100;
fund(&staker, 1000);

assert_ok!(Staking::bond(
RuntimeOrigin::signed(staker),
500,
RewardDestination::Account(101)
));
assert_ok!(Staking::nominate(RuntimeOrigin::signed(staker), vec![GENESIS_VALIDATOR]));

// THEN: they cannot join pool.
assert_noop!(
Pools::join(RawOrigin::Signed(staker).into(), 200, pool_id),
PoolsError::<T>::Restricted
);
});
}

fn create_pool(creator: AccountId, amount: Balance) -> u32 {
fund(&creator, amount * 2);
assert_ok!(Pools::create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ use pallet_staking::{ActiveEra, CurrentEra, ErasStartSessionIndex, StakerStatus}
use parking_lot::RwLock;
use std::sync::Arc;

use frame_support::derive_impl;

use crate::{log, log_current_time};
use frame_support::{derive_impl, traits::Nothing};

pub const INIT_TIMESTAMP: BlockNumber = 30_000;
pub const BLOCK_TIME: BlockNumber = 1000;
Expand Down Expand Up @@ -290,6 +289,7 @@ impl pallet_nomination_pools::Config for Runtime {
type MaxPointsToBalance = frame_support::traits::ConstU8<10>;
type AdminOrigin = frame_system::EnsureRoot<Self::AccountId>;
type BlockNumberProvider = System;
type Filter = Nothing;
}

parameter_types! {
Expand Down
3 changes: 2 additions & 1 deletion substrate/frame/nomination-pools/benchmarking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use frame_support::{
derive_impl,
pallet_prelude::*,
parameter_types,
traits::{ConstU64, VariantCountOf},
traits::{ConstU64, Nothing, VariantCountOf},
PalletId,
};
use sp_runtime::{
Expand Down Expand Up @@ -141,6 +141,7 @@ impl pallet_nomination_pools::Config for Runtime {
type MaxPointsToBalance = MaxPointsToBalance;
type AdminOrigin = frame_system::EnsureRoot<Self::AccountId>;
type BlockNumberProvider = System;
type Filter = Nothing;
}

parameter_types! {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use frame_support::{
assert_ok, derive_impl,
pallet_prelude::*,
parameter_types,
traits::{ConstU64, ConstU8, VariantCountOf},
traits::{ConstU64, ConstU8, Nothing, VariantCountOf},
PalletId,
};
use frame_system::EnsureRoot;
Expand Down Expand Up @@ -270,6 +270,7 @@ impl pallet_nomination_pools::Config for Runtime {
type PalletId = PoolsPalletId;
type AdminOrigin = EnsureRoot<AccountId>;
type BlockNumberProvider = System;
type Filter = Nothing;
}

parameter_types! {
Expand Down
20 changes: 19 additions & 1 deletion substrate/frame/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ use frame_support::{
defensive, defensive_assert,
traits::{
tokens::fungible::{Credit, Debt},
ConstU32, Defensive, DefensiveMax, DefensiveSaturating, Get, LockIdentifier,
ConstU32, Contains, Defensive, DefensiveMax, DefensiveSaturating, Get, LockIdentifier,
},
weights::Weight,
BoundedVec, CloneNoBound, EqNoBound, PartialEqNoBound, RuntimeDebugNoBound, WeakBoundedVec,
Expand Down Expand Up @@ -1335,6 +1335,24 @@ impl<T: Config> EraInfo<T> {
}
}

/// A utility struct that provides a way to check if a given account is a staker.
///
/// This struct implements the `Contains` trait, allowing it to determine whether
/// a particular account is currently staking by checking if the account exists in
/// the staking ledger.
pub struct AllStakers<T: Config>(core::marker::PhantomData<T>);

impl<T: Config> Contains<T::AccountId> for AllStakers<T> {
/// Checks if the given account ID corresponds to a staker.
///
/// # Returns
/// - `true` if the account has an entry in the staking ledger (indicating it is staking).
/// - `false` otherwise.
fn contains(account: &T::AccountId) -> bool {
Ledger::<T>::contains_key(account)
}
}

/// Configurations of the benchmarking of the pallet.
pub trait BenchmarkingConfig {
/// The maximum number of validators to use for snapshot creation.
Expand Down

0 comments on commit 66bc9b3

Please sign in to comment.