Skip to content

Commit

Permalink
use staking default config in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ank4n committed Jun 9, 2024
1 parent 6bbfbdc commit 8e3baa5
Show file tree
Hide file tree
Showing 12 changed files with 24 additions and 77 deletions.
19 changes: 1 addition & 18 deletions substrate/frame/babe/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ use frame_support::{
traits::{ConstU128, ConstU32, ConstU64, KeyOwnerProofSystem, OnInitialize},
};
use pallet_session::historical as pallet_session_historical;
use pallet_staking::FixedNominationsQuota;
use sp_consensus_babe::{AuthorityId, AuthorityPair, Randomness, Slot, VrfSignature};
use sp_core::{
crypto::{KeyTypeId, Pair, VrfSecret},
Expand Down Expand Up @@ -142,7 +141,6 @@ pallet_staking_reward_curve::build! {
parameter_types! {
pub const SessionsPerEra: SessionIndex = 3;
pub const BondingDuration: EraIndex = 3;
pub const SlashDeferDuration: EraIndex = 0;
pub const RewardCurve: &'static PiecewiseLinear<'static> = &REWARD_CURVE;
pub static ElectionsBounds: ElectionBounds = ElectionBoundsBuilder::default().build();
}
Expand All @@ -157,35 +155,20 @@ impl onchain::Config for OnChainSeqPhragmen {
type Bounds = ElectionsBounds;
}

#[derive_impl(pallet_staking::config_preludes::TestDefaultConfig)]
impl pallet_staking::Config for Test {
type RewardRemainder = ();
type CurrencyToVote = ();
type RuntimeEvent = RuntimeEvent;
type Currency = Balances;
type CurrencyBalance = <Self as pallet_balances::Config>::Balance;
type Slash = ();
type Reward = ();
type SessionsPerEra = SessionsPerEra;
type BondingDuration = BondingDuration;
type SlashDeferDuration = SlashDeferDuration;
type AdminOrigin = frame_system::EnsureRoot<Self::AccountId>;
type SessionInterface = Self;
type UnixTime = pallet_timestamp::Pallet<Test>;
type EraPayout = pallet_staking::ConvertCurve<RewardCurve>;
type MaxExposurePageSize = ConstU32<64>;
type NextNewSession = Session;
type ElectionProvider = onchain::OnChainExecution<OnChainSeqPhragmen>;
type GenesisElectionProvider = Self::ElectionProvider;
type VoterList = pallet_staking::UseNominatorsAndValidatorsMap<Self>;
type TargetList = pallet_staking::UseValidatorsMap<Self>;
type NominationsQuota = FixedNominationsQuota<16>;
type MaxUnlockingChunks = ConstU32<32>;
type MaxControllersInDeprecationBatch = ConstU32<100>;
type HistoryDepth = ConstU32<84>;
type EventListeners = ();
type BenchmarkingConfig = pallet_staking::TestBenchmarkingConfig;
type WeightInfo = ();
type DisablingStrategy = pallet_staking::UpToLimitDisablingStrategy;
}

impl pallet_offences::Config for Test {
Expand Down
20 changes: 1 addition & 19 deletions substrate/frame/delegated-staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ pallet_staking_reward_curve::build! {

parameter_types! {
pub const RewardCurve: &'static sp_runtime::curve::PiecewiseLinear<'static> = &I_NPOS;
pub static BondingDuration: u32 = 3;
pub static ElectionsBoundsOnChain: ElectionBounds = ElectionBoundsBuilder::default().build();
}
pub struct OnChainSeqPhragmen;
Expand All @@ -106,35 +105,18 @@ impl onchain::Config for OnChainSeqPhragmen {
type Bounds = ElectionsBoundsOnChain;
}

#[derive_impl(pallet_staking::config_preludes::TestDefaultConfig)]
impl pallet_staking::Config for Runtime {
type Currency = Balances;
type CurrencyBalance = Balance;
type UnixTime = pallet_timestamp::Pallet<Self>;
type CurrencyToVote = ();
type RewardRemainder = ();
type RuntimeEvent = RuntimeEvent;
type Slash = ();
type Reward = ();
type SessionsPerEra = ConstU32<1>;
type SlashDeferDuration = ();
type AdminOrigin = frame_system::EnsureRoot<Self::AccountId>;
type BondingDuration = BondingDuration;
type SessionInterface = ();
type EraPayout = pallet_staking::ConvertCurve<RewardCurve>;
type NextNewSession = ();
type HistoryDepth = ConstU32<84>;
type MaxExposurePageSize = ConstU32<64>;
type ElectionProvider = onchain::OnChainExecution<OnChainSeqPhragmen>;
type GenesisElectionProvider = Self::ElectionProvider;
type VoterList = pallet_staking::UseNominatorsAndValidatorsMap<Self>;
type TargetList = pallet_staking::UseValidatorsMap<Self>;
type NominationsQuota = pallet_staking::FixedNominationsQuota<16>;
type MaxUnlockingChunks = ConstU32<10>;
type MaxControllersInDeprecationBatch = ConstU32<100>;
type EventListeners = (Pools, DelegatedStaking);
type BenchmarkingConfig = pallet_staking::TestBenchmarkingConfig;
type WeightInfo = ();
type DisablingStrategy = pallet_staking::UpToLimitDisablingStrategy;
}

parameter_types! {
Expand Down
26 changes: 13 additions & 13 deletions substrate/frame/delegated-staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,53 +501,53 @@ mod staking_integration {
ExtBuilder::default().build_and_execute(|| {
start_era(1);
let agent = 200;
setup_delegation_stake(agent, 201, (300..350).collect(), 100, 0);
setup_delegation_stake(agent, 201, (300..350).collect(), 320, 0);

// verify withdraw not possible yet
assert_noop!(
DelegatedStaking::release_delegation(RawOrigin::Signed(agent).into(), 300, 100, 0),
DelegatedStaking::release_delegation(RawOrigin::Signed(agent).into(), 300, 320, 0),
Error::<T>::NotEnoughFunds
);

// fill up unlocking chunks in core staking.
// 10 is the max chunks
for i in 2..=11 {
// 32 is the max chunks
for i in 2..=33 {
start_era(i);
assert_ok!(Staking::unbond(RawOrigin::Signed(agent).into(), 10));
// no withdrawals from core staking yet.
assert_eq!(get_agent_ledger(&agent).ledger.unclaimed_withdrawals, 0);
}

// another unbond would trigger withdrawal
start_era(12);
start_era(34);
assert_ok!(Staking::unbond(RawOrigin::Signed(agent).into(), 10));

// 8 previous unbonds would be withdrawn as they were already unlocked. Unlocking period
// is 3 eras.
assert_eq!(get_agent_ledger(&agent).ledger.unclaimed_withdrawals, 8 * 10);
// 30 previous unbonds would be withdrawn as they were already unlocked. Unlocking
// period is 3 eras.
assert_eq!(get_agent_ledger(&agent).ledger.unclaimed_withdrawals, 30 * 10);

// release some delegation now.
assert_ok!(DelegatedStaking::release_delegation(
RawOrigin::Signed(agent).into(),
300,
40,
160,
0
));
assert_eq!(get_agent_ledger(&agent).ledger.unclaimed_withdrawals, 80 - 40);
assert_eq!(get_agent_ledger(&agent).ledger.unclaimed_withdrawals, 300 - 160);

// cannot release more than available
assert_noop!(
DelegatedStaking::release_delegation(RawOrigin::Signed(agent).into(), 300, 50, 0),
DelegatedStaking::release_delegation(RawOrigin::Signed(agent).into(), 300, 141, 0),
Error::<T>::NotEnoughFunds
);
assert_ok!(DelegatedStaking::release_delegation(
RawOrigin::Signed(agent).into(),
300,
40,
140,
0
));

assert_eq!(DelegatedStaking::held_balance_of(Delegator::from(300)), 100 - 80);
assert_eq!(DelegatedStaking::held_balance_of(Delegator::from(300)), 320 - 300);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ parameter_types! {
pub const SessionsPerEra: sp_staking::SessionIndex = 2;
pub static BondingDuration: sp_staking::EraIndex = 28;
pub const SlashDeferDuration: sp_staking::EraIndex = 7; // 1/4 the bonding duration.
pub HistoryDepth: u32 = 84;
}

impl pallet_bags_list::Config for Runtime {
Expand Down Expand Up @@ -291,15 +290,12 @@ const MAX_QUOTA_NOMINATIONS: u32 = 16;
/// Disabling factor set explicitly to byzantine threshold
pub(crate) const SLASHING_DISABLING_FACTOR: usize = 3;

#[derive_impl(pallet_staking::config_preludes::TestDefaultConfig)]
impl pallet_staking::Config for Runtime {
type Currency = Balances;
type CurrencyBalance = Balance;
type UnixTime = Timestamp;
type CurrencyToVote = ();
type RewardRemainder = ();
type RuntimeEvent = RuntimeEvent;
type Slash = (); // burn slashes
type Reward = (); // rewards are minted from the void
type SessionsPerEra = SessionsPerEra;
type BondingDuration = BondingDuration;
type SlashDeferDuration = SlashDeferDuration;
Expand All @@ -314,11 +310,8 @@ impl pallet_staking::Config for Runtime {
type NominationsQuota = pallet_staking::FixedNominationsQuota<MAX_QUOTA_NOMINATIONS>;
type TargetList = pallet_staking::UseValidatorsMap<Self>;
type MaxUnlockingChunks = MaxUnlockingChunks;
type MaxControllersInDeprecationBatch = ConstU32<100>;
type HistoryDepth = HistoryDepth;
type EventListeners = Pools;
type WeightInfo = pallet_staking::weights::SubstrateWeight<Runtime>;
type BenchmarkingConfig = pallet_staking::TestBenchmarkingConfig;
type DisablingStrategy = pallet_staking::UpToLimitDisablingStrategy<SLASHING_DISABLING_FACTOR>;
}

Expand Down
1 change: 1 addition & 0 deletions substrate/frame/fast-unstake/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ impl frame_election_provider_support::ElectionProvider for MockElection {
}
}

#[derive_impl(pallet_staking::config_preludes::TestDefaultConfig)]
impl pallet_staking::Config for Runtime {
type Currency = Balances;
type CurrencyBalance = Balance;
Expand Down
1 change: 1 addition & 0 deletions substrate/frame/grandpa/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ impl onchain::Config for OnChainSeqPhragmen {
type Bounds = ElectionsBoundsOnChain;
}

#[derive_impl(pallet_staking::config_preludes::TestDefaultConfig)]
impl pallet_staking::Config for Test {
type RewardRemainder = ();
type CurrencyToVote = ();
Expand Down
1 change: 1 addition & 0 deletions substrate/frame/nomination-pools/benchmarking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pallet_staking_reward_curve::build! {
parameter_types! {
pub const RewardCurve: &'static sp_runtime::curve::PiecewiseLinear<'static> = &I_NPOS;
}
#[derive_impl(pallet_staking::config_preludes::TestDefaultConfig)]
impl pallet_staking::Config for Runtime {
type Currency = Balances;
type CurrencyBalance = Balance;
Expand Down
19 changes: 1 addition & 18 deletions substrate/frame/nomination-pools/test-delegate-stake/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,36 +95,19 @@ parameter_types! {
pub static BondingDuration: u32 = 3;
}

#[derive_impl(pallet_staking::config_preludes::TestDefaultConfig)]
impl pallet_staking::Config for Runtime {
type Currency = Balances;
type CurrencyBalance = Balance;
type UnixTime = pallet_timestamp::Pallet<Self>;
type CurrencyToVote = ();
type RewardRemainder = ();
type RuntimeEvent = RuntimeEvent;
type Slash = ();
type Reward = ();
type SessionsPerEra = ();
type SlashDeferDuration = ();
type AdminOrigin = frame_system::EnsureRoot<Self::AccountId>;
type BondingDuration = BondingDuration;
type SessionInterface = ();
type EraPayout = pallet_staking::ConvertCurve<RewardCurve>;
type NextNewSession = ();
type MaxExposurePageSize = ConstU32<64>;
type ElectionProvider =
frame_election_provider_support::NoElection<(AccountId, BlockNumber, Staking, ())>;
type GenesisElectionProvider = Self::ElectionProvider;
type VoterList = VoterList;
type TargetList = pallet_staking::UseValidatorsMap<Self>;
type NominationsQuota = pallet_staking::FixedNominationsQuota<16>;
type MaxUnlockingChunks = ConstU32<32>;
type MaxControllersInDeprecationBatch = ConstU32<100>;
type HistoryDepth = ConstU32<84>;
type EventListeners = (Pools, DelegatedStaking);
type BenchmarkingConfig = pallet_staking::TestBenchmarkingConfig;
type WeightInfo = ();
type DisablingStrategy = pallet_staking::UpToLimitDisablingStrategy;
}

parameter_types! {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ parameter_types! {
pub static BondingDuration: u32 = 3;
}

#[derive_impl(pallet_staking::config_preludes::TestDefaultConfig)]
impl pallet_staking::Config for Runtime {
type Currency = Balances;
type CurrencyBalance = Balance;
Expand Down
1 change: 1 addition & 0 deletions substrate/frame/root-offences/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ parameter_types! {
pub static LedgerSlashPerEra: (BalanceOf<Test>, BTreeMap<EraIndex, BalanceOf<Test>>) = (Zero::zero(), BTreeMap::new());
}

#[derive_impl(pallet_staking::config_preludes::TestDefaultConfig)]
impl pallet_staking::Config for Test {
type Currency = Balances;
type CurrencyBalance = <Self as pallet_balances::Config>::Balance;
Expand Down
1 change: 1 addition & 0 deletions substrate/frame/session/benchmarking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ impl onchain::Config for OnChainSeqPhragmen {
type Bounds = ElectionsBounds;
}

#[derive_impl(pallet_staking::config_preludes::TestDefaultConfig)]
impl pallet_staking::Config for Test {
type Currency = Balances;
type CurrencyBalance = <Self as pallet_balances::Config>::Balance;
Expand Down
2 changes: 1 addition & 1 deletion substrate/frame/staking/src/pallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ pub mod pallet {
type AdminOrigin: EnsureOrigin<Self::RuntimeOrigin>;

/// Interface for interacting with a session pallet.
#[pallet::no_default]
type SessionInterface: SessionInterface<Self::AccountId>;

/// The payout for validators and the system for the current era.
Expand Down Expand Up @@ -332,6 +331,7 @@ pub mod pallet {
type SessionsPerEra = SessionsPerEra;
type BondingDuration = BondingDuration;
type SlashDeferDuration = ();
type SessionInterface = ();
type NextNewSession = ();
type MaxExposurePageSize = ConstU32<64>;
type MaxUnlockingChunks = ConstU32<32>;
Expand Down

0 comments on commit 8e3baa5

Please sign in to comment.