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

Commit

Permalink
Companion of paritytech/substrate#10403 part.1, !
Browse files Browse the repository at this point in the history
  • Loading branch information
AurevoirXavier committed Sep 14, 2022
1 parent 47c8aae commit ab8ec9c
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 96 deletions.
10 changes: 7 additions & 3 deletions frame/staking/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1530,10 +1530,14 @@ where
Self::reward_by_ids(vec![(author, 20)]);
}

fn note_uncle(author: AccountId<T>, _age: BlockNumberFor<T>) {
Self::reward_by_ids(vec![(<pallet_authorship::Pallet<T>>::author(), 2), (author, 1)]);
fn note_uncle(uncle_author: T::AccountId, _age: T::BlockNumber) {
// defensive-only: block author must exist.
if let Some(block_author) = <pallet_authorship::Pallet<T>>::author() {
Self::reward_by_ids(vec![(block_author, 2), (uncle_author, 1)])
} else {
crate::log!(warn, "block author not set, this should never happen");
}
}
}

/// Means for interacting with a specialized version of the `session` trait.
///
Expand Down
22 changes: 15 additions & 7 deletions frame/staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ impl ExtBuilder {
.assimilate_storage(&mut storage);
}

let mut stakers = vec![];
let mut stakers = Vec::new();

if self.has_stakers {
stakers = vec![
Expand Down Expand Up @@ -507,7 +507,7 @@ impl ExtBuilder {
}
let _ = darwinia_staking::GenesisConfig::<Test> {
history_depth: 84,
stakers,
stakers: stakers.clone(),
validator_count: self.validator_count,
minimum_validator_count: self.minimum_validator_count,
invulnerables: self.invulnerables,
Expand All @@ -520,12 +520,15 @@ impl ExtBuilder {
.assimilate_storage(&mut storage);
let _ = pallet_session::GenesisConfig::<Test> {
keys: if self.has_stakers {
// genesis election will overwrite this, no worries.
Default::default()
// set the keys for the first session.
stakers
.into_iter()
.map(|(id, ..)| (id, id, SessionKeys { other: id.into() }))
.collect()
} else {
// set some dummy validators in genesis.
(0..self.validator_count as u64)
.map(|x| (x, x, SessionKeys { other: UintAuthorityId(x as u64) }))
.map(|id| (id, id, SessionKeys { other: id.into() }))
.collect()
},
}
Expand Down Expand Up @@ -559,7 +562,7 @@ impl Default for ExtBuilder {
nominate: true,
validator_count: 2,
minimum_validator_count: 0,
invulnerables: vec![],
invulnerables: Vec::new(),
has_stakers: true,
initialize_first_session: true,
min_nominator_bond: ExistentialDeposit::get(),
Expand Down Expand Up @@ -736,6 +739,11 @@ fn bond(stash: AccountId, controller: AccountId, val: StakingBalanceT<Test>) {
pub fn bond_validator(stash: AccountId, controller: AccountId, val: StakingBalanceT<Test>) {
bond(stash, controller, val);
assert_ok!(Staking::validate(Origin::signed(controller), ValidatorPrefs::default()));
assert_ok!(Session::set_keys(
Origin::signed(ctrl),
SessionKeys { other: ctrl.into() },
Vec::new()
));
}

pub fn bond_nominator(
Expand Down Expand Up @@ -902,7 +910,7 @@ pub fn add_slash(who: &AccountId) {
on_offence_now(
&[OffenceDetails {
offender: (who.clone(), Staking::eras_stakers(active_era(), who.clone())),
reporters: vec![],
reporters: Vec::new(),
}],
&[Perbill::from_percent(10)],
);
Expand Down
64 changes: 55 additions & 9 deletions frame/staking/src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ impl<T: Config> Convert<AccountId<T>, Option<ExposureT<T>>> for ExposureOf<T> {
}
}
/// A snapshot of the stake backing a single validator in the system.
#[derive(
Clone, Default, PartialEq, Eq, PartialOrd, Ord, Encode, Decode, RuntimeDebug, TypeInfo,
)]
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Encode, Decode, RuntimeDebug, TypeInfo)]
pub struct Exposure<AccountId, RingBalance, KtonBalance>
where
RingBalance: HasCompact,
Expand All @@ -49,6 +47,20 @@ where
/// The portions of nominators stashes that are exposed.
pub others: Vec<IndividualExposure<AccountId, RingBalance, KtonBalance>>,
}
impl<AccountId, Balance> Default for Exposure<AccountId, Balance>
where
Balance: HasCompact + Zero,
{
fn default() -> Self {
Self {
own_ring_balance: Zero::zero(),
own_kton_balance: Zero::zero(),
own_power: 0,
total_power: 0,
others: Vec::new(),
}
}
}
/// The amount of exposure (to slashing) than an individual nominator has.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Encode, Decode, RuntimeDebug, TypeInfo)]
pub struct IndividualExposure<AccountId, RingBalance, KtonBalance>
Expand Down Expand Up @@ -79,7 +91,7 @@ pub struct ActiveEraInfo {
}

/// The ledger of a (bonded) stash.
#[derive(Clone, Default, PartialEq, Eq, Encode, Decode, RuntimeDebug, TypeInfo)]
#[derive(Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug, TypeInfo)]
pub struct StakingLedger<AccountId, RingBalance, KtonBalance, BlockNumber>
where
RingBalance: HasCompact,
Expand Down Expand Up @@ -117,11 +129,25 @@ where
impl<AccountId, RingBalance, KtonBalance, BlockNumber>
StakingLedger<AccountId, RingBalance, KtonBalance, BlockNumber>
where
RingBalance: Copy + AtLeast32BitUnsigned + Saturating,
KtonBalance: Copy + AtLeast32BitUnsigned + Saturating,
RingBalance: Copy + AtLeast32BitUnsigned + Saturating + Zero,
KtonBalance: Copy + AtLeast32BitUnsigned + Saturating + Zero,
BlockNumber: Copy + PartialOrd,
TsInMs: PartialOrd,
{
/// Initializes the default object using the given `validator`.
pub fn default_from(stash: AccountId) -> Self {
Self {
stash,
active: Zero::zero(),
active_deposit_ring: Zero::zero(),
active_kton: Zero::zero(),
deposit_items: Vec::new(),
ring_staking_lock: Vec::new(),
kton_staking_lock: Vec::new(),
claimed_rewards: Vec::new(),
}
}

pub fn consolidate_unbondings(&mut self, now: BlockNumber) {
self.ring_staking_lock.refresh(now);
self.kton_staking_lock.refresh(now);
Expand Down Expand Up @@ -379,17 +405,22 @@ pub struct Nominations<AccountId> {
/// Reward points of an era. Used to split era total payout between validators.
///
/// This points will be used to reward validators and their respective nominators.
#[derive(Debug, Default, PartialEq, Encode, Decode, TypeInfo)]
#[derive(Debug, PartialEq, Encode, Decode, TypeInfo)]
pub struct EraRewardPoints<AccountId: Ord> {
/// Total number of points. Equals the sum of reward points for each validator.
pub total: RewardPoint,
/// The reward points earned by a given validator.
pub individual: BTreeMap<AccountId, RewardPoint>,
}
impl<AccountId: Ord> Default for EraRewardPoints<AccountId> {
fn default() -> Self {
EraRewardPoints { total: 0, individual: BTreeMap::new() }
}
}

/// Mode of era-forcing.
#[derive(Copy, Clone, PartialEq, Eq, Encode, Decode, RuntimeDebug, TypeInfo)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "std", derive(Clone, Serialize, Deserialize))]
pub enum Forcing {
/// Not forcing anything - just let whatever happen.
NotForcing,
Expand All @@ -410,7 +441,7 @@ impl Default for Forcing {

/// A pending slash record. The value of the slash has been computed but not applied yet,
/// rather deferred for several eras.
#[derive(Default, Encode, Decode, RuntimeDebug, TypeInfo)]
#[derive(Encode, Decode, RuntimeDebug, TypeInfo)]
pub struct UnappliedSlash<AccountId, RingBalance, KtonBalance> {
/// The stash ID of the offending validator.
pub validator: AccountId,
Expand All @@ -423,6 +454,21 @@ pub struct UnappliedSlash<AccountId, RingBalance, KtonBalance> {
/// The amount of payout.
pub payout: slashing::RK<RingBalance, KtonBalance>,
}
impl<AccountId, Balance> UnappliedSlash<AccountId, Balance>
where
Balance: HasCompact + Zero,
{
/// Initializes the default object using the given `validator`.
pub fn default_from(validator: AccountId) -> Self {
Self {
validator,
own: Zero::zero(),
others: Vec::new(),
reporters: Vec::new(),
payout: Zero::zero(),
}
}
}

// A value placed in storage that represents the current version of the Staking storage. This value
// is used by the `on_runtime_upgrade` logic to determine whether we run storage migration logic.
Expand Down
Loading

0 comments on commit ab8ec9c

Please sign in to comment.