-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove without_storage_info for the staking pallet #621
Closed
rajesh-nodle
wants to merge
2
commits into
NodleCode:master
from
rajesh-nodle:shamb0/bounded_staking
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,11 +20,15 @@ use super::{ | |
ActiveSession, BalanceOf, BondedSessions, Config, Event, NegativeImbalanceOf, Pallet, SessionAccumulatedBalance, | ||
SessionValidatorReward, SlashRewardProportion, Staked, Store, Total, | ||
}; | ||
use crate::slashing; | ||
use crate::types::{ValidatorSnapshot, ValidatorSnapshotOf}; | ||
// use crate::slashing; | ||
use crate::{ | ||
slashing::SlashParams, | ||
types::{ValidatorSnapshot, ValidatorSnapshotOf}, | ||
}; | ||
use frame_support::{ | ||
pallet_prelude::*, | ||
traits::{Currency, Get, Imbalance, OnUnbalanced}, | ||
BoundedVec, | ||
}; | ||
use frame_system::{self as system}; | ||
use pallet_session::historical; | ||
|
@@ -77,7 +81,6 @@ where | |
} | ||
} | ||
} | ||
|
||
/// In this implementation `new_session(session)` must be called before `end_session(session-1)` | ||
/// i.e. the new session must be planned before the ending of the previous session. | ||
/// | ||
|
@@ -89,7 +92,10 @@ impl<T: Config> pallet_session::SessionManager<T::AccountId> for Pallet<T> { | |
let current_block_number = system::Pallet::<T>::block_number(); | ||
|
||
// select top collator validators for next round | ||
let (validator_count, total_staked) = Self::select_session_validators(new_index); | ||
let (validator_count, total_staked) = match Self::select_session_validators(new_index) { | ||
Ok((validator_count, total_staked)) => (validator_count, total_staked), | ||
Err(_) => return None, | ||
}; | ||
|
||
// snapshot total stake | ||
<Staked<T>>::insert(new_index, <Total<T>>::get()); | ||
|
@@ -109,7 +115,7 @@ impl<T: Config> pallet_session::SessionManager<T::AccountId> for Pallet<T> { | |
total_staked, | ||
); | ||
|
||
Some(Self::selected_validators()) | ||
Some(Self::selected_validators().to_vec()) | ||
} | ||
fn start_session(start_index: SessionIndex) { | ||
log::trace!("start_session:[{:#?}] - Sess-idx[{:#?}]", line!(), start_index); | ||
|
@@ -119,18 +125,28 @@ impl<T: Config> pallet_session::SessionManager<T::AccountId> for Pallet<T> { | |
let bonding_duration = T::BondedDuration::get(); | ||
|
||
<BondedSessions<T>>::mutate(|bonded| { | ||
bonded.push(start_index); | ||
let _ = match bonded.try_push(start_index) { | ||
Err(_) => { | ||
log::error!( | ||
"start_session:[{:#?}] - Error, Could be BondedSessions Overflow", | ||
line!() | ||
); | ||
return (); | ||
} | ||
Ok(_) => (), | ||
}; | ||
|
||
if start_index > bonding_duration { | ||
let first_kept = start_index - bonding_duration; | ||
|
||
// prune out everything that's from before the first-kept index. | ||
let n_to_prune = bonded | ||
.to_vec() | ||
.iter() | ||
.take_while(|&&session_idx| session_idx < first_kept) | ||
.count(); | ||
|
||
for prune_session in bonded.drain(..n_to_prune) { | ||
for prune_session in bonded.to_vec().drain(..n_to_prune) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can't we get an iter from an bounded vec? |
||
// Clear the DB cached state of last session | ||
Self::clear_session_information(prune_session); | ||
} | ||
|
@@ -161,8 +177,8 @@ impl<T: Config> pallet_session::SessionManager<T::AccountId> for Pallet<T> { | |
// pay all stakers for T::BondedDuration rounds ago | ||
Self::pay_stakers(end_index); | ||
|
||
// // Clear the DB cached state of last session | ||
// Self::clear_session_information(Self::active_session()); | ||
// Clear the DB cached state of last session | ||
Self::clear_session_information(Self::active_session()); | ||
} else { | ||
log::error!( | ||
"end_session:[{:#?}] - Something wrong (CSI[{}], ESI[{}])", | ||
|
@@ -173,7 +189,6 @@ impl<T: Config> pallet_session::SessionManager<T::AccountId> for Pallet<T> { | |
} | ||
} | ||
} | ||
|
||
/// Means for interacting with a specialized version of the `session` trait. | ||
/// | ||
/// This is needed because `Staking` sets the `ValidatorIdOf` of the `pallet_session::Config` | ||
|
@@ -194,8 +209,8 @@ impl<T: Config> SessionInterface<<T as frame_system::Config>::AccountId> for T | |
where | ||
T: pallet_session::Config<ValidatorId = <T as frame_system::Config>::AccountId>, | ||
T: pallet_session::historical::Config< | ||
FullIdentification = ValidatorSnapshot<<T as frame_system::Config>::AccountId, BalanceOf<T>>, | ||
FullIdentificationOf = ValidatorSnapshotOf<T>, | ||
FullIdentification = ValidatorSnapshot<T, T::MaxNominatorsPerValidator>, | ||
FullIdentificationOf = ValidatorSnapshotOf<T, T::MaxNominatorsPerValidator>, | ||
>, | ||
T::SessionHandler: pallet_session::SessionHandler<<T as frame_system::Config>::AccountId>, | ||
T::SessionManager: pallet_session::SessionManager<<T as frame_system::Config>::AccountId>, | ||
|
@@ -217,10 +232,12 @@ where | |
} | ||
} | ||
|
||
impl<T: Config> historical::SessionManager<T::AccountId, ValidatorSnapshot<T::AccountId, BalanceOf<T>>> for Pallet<T> { | ||
impl<T: Config> historical::SessionManager<T::AccountId, ValidatorSnapshot<T, T::MaxNominatorsPerValidator>> | ||
for Pallet<T> | ||
{ | ||
fn new_session( | ||
new_index: SessionIndex, | ||
) -> Option<Vec<(T::AccountId, ValidatorSnapshot<T::AccountId, BalanceOf<T>>)>> { | ||
) -> Option<Vec<(T::AccountId, ValidatorSnapshot<T, T::MaxNominatorsPerValidator>)>> { | ||
<Self as pallet_session::SessionManager<_>>::new_session(new_index).map(|validators| { | ||
validators | ||
.into_iter() | ||
|
@@ -244,8 +261,8 @@ impl<T: Config> OnOffenceHandler<T::AccountId, pallet_session::historical::Ident | |
where | ||
T: pallet_session::Config<ValidatorId = <T as frame_system::Config>::AccountId>, | ||
T: pallet_session::historical::Config< | ||
FullIdentification = ValidatorSnapshot<<T as frame_system::Config>::AccountId, BalanceOf<T>>, | ||
FullIdentificationOf = ValidatorSnapshotOf<T>, | ||
FullIdentification = ValidatorSnapshot<T, T::MaxNominatorsPerValidator>, | ||
FullIdentificationOf = ValidatorSnapshotOf<T, T::MaxNominatorsPerValidator>, | ||
>, | ||
T::SessionHandler: pallet_session::SessionHandler<<T as frame_system::Config>::AccountId>, | ||
T::SessionManager: pallet_session::SessionManager<<T as frame_system::Config>::AccountId>, | ||
|
@@ -289,50 +306,72 @@ where | |
continue; | ||
} | ||
|
||
let unapplied = slashing::compute_slash::<T>(slashing::SlashParams { | ||
controller, | ||
let slash_param: SlashParams<T, T::MaxNominatorsPerValidator> = SlashParams { | ||
controller: controller.clone(), | ||
slash: *slash_fraction, | ||
exposure, | ||
exposure: exposure.clone(), | ||
slash_session, | ||
window_start, | ||
now: active_session, | ||
reward_proportion, | ||
disable_strategy, | ||
}); | ||
}; | ||
|
||
if let Some(mut unapplied) = unapplied { | ||
let nominators_len = unapplied.others.len() as u64; | ||
let reporters_len = details.reporters.len() as u64; | ||
|
||
{ | ||
let upper_bound = 1 /* Validator/NominatorSlashInEra */ + 2 /* fetch_spans */; | ||
let rw = upper_bound + nominators_len * upper_bound; | ||
add_db_reads_writes(rw, rw); | ||
match slash_param.compute_slash() { | ||
Err(err) => { | ||
log::error!("on_offence:[{:#?}] - compute_slash Err[{:#?}]", line!(), err,); | ||
} | ||
unapplied.reporters = details.reporters.clone(); | ||
if slash_defer_duration == 0 { | ||
// apply right away. | ||
slashing::apply_slash::<T>(unapplied); | ||
|
||
let slash_cost = (6, 5); | ||
let reward_cost = (2, 2); | ||
add_db_reads_writes( | ||
(1 + nominators_len) * slash_cost.0 + reward_cost.0 * reporters_len, | ||
(1 + nominators_len) * slash_cost.1 + reward_cost.1 * reporters_len, | ||
); | ||
} else { | ||
// defer to end of some `slash_defer_duration` from now. | ||
let apply_at = active_session.saturating_add(slash_defer_duration); | ||
|
||
<Self as Store>::UnappliedSlashes::mutate(apply_at, |for_later| for_later.push(unapplied.clone())); | ||
|
||
<Pallet<T>>::deposit_event(Event::DeferredUnappliedSlash(active_session, unapplied.validator)); | ||
|
||
add_db_reads_writes(1, 1); | ||
Ok(None) => { | ||
log::trace!("on_offence:[{:#?}] - NOP", line!(),); | ||
add_db_reads_writes(4 /* fetch_spans */, 5 /* kick_out_if_recent */); | ||
} | ||
Ok(Some(mut unapplied)) => { | ||
let nominators_len = unapplied.others.len() as u64; | ||
let reporters_len = details.reporters.to_vec().len() as u64; | ||
|
||
{ | ||
let upper_bound = 1 /* Validator/NominatorSlashInEra */ + 2 /* fetch_spans */; | ||
let rw = upper_bound + nominators_len * upper_bound; | ||
add_db_reads_writes(rw, rw); | ||
} | ||
|
||
unapplied.reporters = | ||
<BoundedVec<T::AccountId, T::MaxSlashReporters>>::try_from(details.reporters.clone()) | ||
.expect("OnOffenceHandler Reporters Overflow Error"); | ||
|
||
if slash_defer_duration == 0 { | ||
// apply right away. | ||
unapplied.apply_slash(); | ||
|
||
let slash_cost = (6, 5); | ||
let reward_cost = (2, 2); | ||
add_db_reads_writes( | ||
(1 + nominators_len) * slash_cost.0 + reward_cost.0 * reporters_len, | ||
(1 + nominators_len) * slash_cost.1 + reward_cost.1 * reporters_len, | ||
); | ||
} else { | ||
// defer to end of some `slash_defer_duration` from now. | ||
let apply_at = active_session.saturating_add(slash_defer_duration); | ||
|
||
let unapplied_for_event = unapplied.clone(); | ||
|
||
<Self as Store>::UnappliedSlashes::mutate(apply_at, move |for_later| { | ||
match for_later.try_push(unapplied) { | ||
Err(_) => { | ||
log::error!("on_offence:[{:#?}] - UnappliedSlashes Overflow", line!()); | ||
} | ||
Ok(_) => {} | ||
} | ||
}); | ||
|
||
<Pallet<T>>::deposit_event(Event::DeferredUnappliedSlash( | ||
active_session, | ||
unapplied_for_event.validator, | ||
)); | ||
|
||
add_db_reads_writes(1, 1); | ||
} | ||
} | ||
} else { | ||
log::trace!("on_offence:[{:#?}] - NOP", line!(),); | ||
add_db_reads_writes(4 /* fetch_spans */, 5 /* kick_out_if_recent */); | ||
} | ||
} | ||
consumed_weight | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this return needed here?