Skip to content

Commit

Permalink
Merge branch 'main' into bear-evm-opengov
Browse files Browse the repository at this point in the history
  • Loading branch information
boundless-forest authored Oct 17, 2023
2 parents 2efcf70 + b30a14a commit b2236e8
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 57 deletions.
1 change: 0 additions & 1 deletion pallet/account-migration/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,6 @@ impl darwinia_deposit::Config for Runtime {
impl darwinia_staking::Config for Runtime {
type Deposit = Deposit;
type Kton = Dummy;
type MaxCommission = ();
type MaxDeposits = ();
type MaxUnstakings = ();
type MinStakingDuration = ();
Expand Down
50 changes: 21 additions & 29 deletions pallet/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,6 @@ pub mod pallet {
/// Deposit [`StakeExt`] interface.
type Deposit: StakeExt<AccountId = Self::AccountId, Amount = Balance>;

/// Maximum commission rate.
#[pallet::constant]
type MaxCommission: Get<Perbill>;

/// Minimum time to stake at least.
#[pallet::constant]
type MinStakingDuration: Get<Self::BlockNumber>;
Expand Down Expand Up @@ -168,8 +164,6 @@ pub mod pallet {

#[pallet::error]
pub enum Error<T> {
/// Commission rate must be less than maximum commission rate.
CommissionTooHigh,
/// Exceed maximum deposit count.
ExceedMaxDeposits,
/// Exceed maximum unstaking/unbonding count.
Expand Down Expand Up @@ -464,10 +458,6 @@ pub mod pallet {
pub fn collect(origin: OriginFor<T>, commission: Perbill) -> DispatchResult {
let who = ensure_signed(origin)?;

if commission > T::MaxCommission::get() {
Err(<Error<T>>::CommissionTooHigh)?;
}

<Collators<T>>::mutate(&who, |c| *c = Some(commission));

Self::deposit_event(Event::CommissionUpdated { who, commission });
Expand Down Expand Up @@ -859,7 +849,7 @@ pub mod pallet {

for n_exposure in c_exposure.nominators {
let n_payout =
Perbill::from_rational(n_exposure.value, c_exposure.total) * n_payout;
Perbill::from_rational(n_exposure.vote, c_exposure.vote) * n_payout;

if c == n_exposure.who {
// If the collator nominated themselves.
Expand Down Expand Up @@ -901,24 +891,25 @@ pub mod pallet {
///
/// This should only be called by the [`pallet_session::SessionManager::new_session`].
pub fn elect() -> Vec<T::AccountId> {
let mut collators = <Collators<T>>::iter_keys()
.map(|c| {
let mut t_power = 0;
let i_exposures = <Nominators<T>>::iter()
let mut collators = <Collators<T>>::iter()
.map(|(c, cm)| {
let scaler = (Perbill::one() - cm);
let mut collator_v = 0;
let nominators = <Nominators<T>>::iter()
.filter_map(|(n, c_)| {
if c_ == c {
let n_power = Self::power_of(&n);
let nominator_v = scaler * Self::power_of(&n);

t_power += n_power;
collator_v += nominator_v;

Some(IndividualExposure { who: n, value: n_power })
Some(IndividualExposure { who: n, vote: nominator_v })
} else {
None
}
})
.collect();

((c, Exposure { total: t_power, nominators: i_exposures }), t_power)
((c, Exposure { vote: collator_v, nominators }), collator_v)
})
.collect::<Vec<_>>();

Expand All @@ -940,6 +931,7 @@ pub use pallet::*;

type RewardPoint = u32;
type Power = u32;
type Vote = u32;

type DepositId<T> = <<T as Config>::Deposit as Stake>::Item;
type NegativeImbalance<T> = <<T as Config>::RingCurrency as Currency<
Expand Down Expand Up @@ -997,18 +989,18 @@ pub struct Exposure<AccountId>
where
AccountId: PartialEq,
{
/// The total power backing this collator.
pub total: Power,
/// Nominators' stake power.
/// The total vote backing this collator.
pub vote: Vote,
/// Nominator staking map.
pub nominators: Vec<IndividualExposure<AccountId>>,
}
/// A snapshot of the stake backing a single collator in the system.
#[cfg(not(feature = "try-runtime"))]
#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, RuntimeDebug)]
pub struct Exposure<AccountId> {
/// The total power backing this collator.
pub total: Power,
/// Nominators' stake power.
/// The total vote backing this collator.
pub vote: Vote,
/// Nominator staking map.
pub nominators: Vec<IndividualExposure<AccountId>>,
}
/// A snapshot of the staker's state.
Expand All @@ -1020,17 +1012,17 @@ where
{
/// Nominator.
pub who: AccountId,
/// Nominator's stake power.
pub value: Power,
/// Nominator's staking vote.
pub vote: Vote,
}
/// A snapshot of the staker's state.
#[cfg(not(feature = "try-runtime"))]
#[derive(Encode, Decode, MaxEncodedLen, TypeInfo, RuntimeDebug)]
pub struct IndividualExposure<AccountId> {
/// Nominator.
pub who: AccountId,
/// Nominator's stake power.
pub value: Power,
/// Nominator's staking vote.
pub vote: Vote,
}

// Add reward points to block authors:
Expand Down
2 changes: 0 additions & 2 deletions pallet/staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,13 +245,11 @@ impl darwinia_staking::Stake for KtonStaking {
}
}
frame_support::parameter_types! {
pub const MaxCommission: sp_runtime::Perbill = sp_runtime::Perbill::from_percent(99);
pub const PayoutFraction: sp_runtime::Perbill = sp_runtime::Perbill::from_percent(40);
}
impl darwinia_staking::Config for Runtime {
type Deposit = Deposit;
type Kton = KtonStaking;
type MaxCommission = MaxCommission;
type MaxDeposits = <Self as darwinia_deposit::Config>::MaxDeposits;
type MaxUnstakings = frame_support::traits::ConstU32<16>;
type MinStakingDuration = frame_support::traits::ConstU64<3>;
Expand Down
21 changes: 8 additions & 13 deletions pallet/staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,11 +363,6 @@ fn collect_should_work() {
assert_ok!(Staking::collect(RuntimeOrigin::signed(1), c));
assert_eq!(Staking::collator_of(1).unwrap(), c);
});

assert_noop!(
Staking::collect(RuntimeOrigin::signed(1), Perbill::from_percent(100)),
<Error<Runtime>>::CommissionTooHigh
);
});
}

Expand Down Expand Up @@ -563,16 +558,16 @@ fn payout_should_work() {
let session_duration = Duration::new(6 * 60 * 60, 0).as_millis();
Staking::payout(session_duration, Staking::elapsed_time());
let rewards = [
1_366_118_870_124_739_965_121_u128,
2_550_088_512_393_184_504_844,
1_366_118_850_452_628_471_390,
2_550_088_490_535_282_845_143,
3_551_909_019_701_415_672_898,
4_371_580_342_869_154_734_956,
5_009_102_454_574_024_616_391,
4_098_356_544_800_514_916_261,
2_914_386_902_532_070_376_538,
4_371_580_329_754_413_739_136,
5_009_102_470_967_450_861_167,
4_098_356_559_554_598_536_559,
2_914_386_924_389_972_036_239,
1_912_566_395_223_839_208_483,
1_092_895_072_056_100_146_426,
455_372_960_351_230_264_991,
1_092_895_081_892_155_893_291,
455_372_941_225_566_312_752,
];
(1..=10)
.zip(rewards.iter())
Expand Down
4 changes: 0 additions & 4 deletions precompile/staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,9 @@ impl darwinia_staking::Stake for KtonStaking {
}
}

frame_support::parameter_types! {
pub const MaxCommission: sp_runtime::Perbill = sp_runtime::Perbill::from_percent(99);
}
impl darwinia_staking::Config for TestRuntime {
type Deposit = Deposit;
type Kton = KtonStaking;
type MaxCommission = MaxCommission;
type MaxDeposits = <Self as darwinia_deposit::Config>::MaxDeposits;
type MaxUnstakings = frame_support::traits::ConstU32<16>;
type MinStakingDuration = frame_support::traits::ConstU64<3>;
Expand Down
2 changes: 0 additions & 2 deletions runtime/crab/src/pallets/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,12 @@ impl darwinia_staking::Stake for KtonStaking {
}

frame_support::parameter_types! {
pub const MaxCommission: sp_runtime::Perbill = sp_runtime::Perbill::from_percent(30);
pub const PayoutFraction: sp_runtime::Perbill = sp_runtime::Perbill::from_percent(40);
}

impl darwinia_staking::Config for Runtime {
type Deposit = Deposit;
type Kton = KtonStaking;
type MaxCommission = MaxCommission;
type MaxDeposits = <Self as darwinia_deposit::Config>::MaxDeposits;
type MaxUnstakings = ConstU32<16>;
type MinStakingDuration = MinStakingDuration;
Expand Down
2 changes: 0 additions & 2 deletions runtime/darwinia/src/pallets/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,12 @@ impl darwinia_staking::Stake for KtonStaking {
}

frame_support::parameter_types! {
pub const MaxCommission: sp_runtime::Perbill = sp_runtime::Perbill::from_percent(30);
pub const PayoutFraction: sp_runtime::Perbill = sp_runtime::Perbill::from_percent(40);
}

impl darwinia_staking::Config for Runtime {
type Deposit = Deposit;
type Kton = KtonStaking;
type MaxCommission = MaxCommission;
type MaxDeposits = <Self as darwinia_deposit::Config>::MaxDeposits;
type MaxUnstakings = ConstU32<16>;
type MinStakingDuration = MinStakingDuration;
Expand Down
2 changes: 0 additions & 2 deletions runtime/pangolin/src/pallets/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,12 @@ impl darwinia_staking::Stake for KtonStaking {
}

frame_support::parameter_types! {
pub const MaxCommission: sp_runtime::Perbill = sp_runtime::Perbill::from_percent(30);
pub const PayoutFraction: sp_runtime::Perbill = sp_runtime::Perbill::from_percent(40);
}

impl darwinia_staking::Config for Runtime {
type Deposit = Deposit;
type Kton = KtonStaking;
type MaxCommission = MaxCommission;
type MaxDeposits = <Self as darwinia_deposit::Config>::MaxDeposits;
type MaxUnstakings = ConstU32<16>;
type MinStakingDuration = ConstU32<{ 2 * MINUTES }>;
Expand Down
2 changes: 0 additions & 2 deletions runtime/pangoro/src/pallets/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,12 @@ impl darwinia_staking::Stake for KtonStaking {
}

frame_support::parameter_types! {
pub const MaxCommission: sp_runtime::Perbill = sp_runtime::Perbill::from_percent(30);
pub const PayoutFraction: sp_runtime::Perbill = sp_runtime::Perbill::from_percent(40);
}

impl darwinia_staking::Config for Runtime {
type Deposit = Deposit;
type Kton = KtonStaking;
type MaxCommission = MaxCommission;
type MaxDeposits = <Self as darwinia_deposit::Config>::MaxDeposits;
type MaxUnstakings = ConstU32<16>;
type MinStakingDuration = ConstU32<{ 10 * MINUTES }>;
Expand Down

0 comments on commit b2236e8

Please sign in to comment.