Skip to content
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

[MOON-1806] remove deprecated parachain-staking items #1700

Merged
merged 4 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 0 additions & 36 deletions pallets/parachain-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,18 +470,6 @@ pub mod pallet {
/// Current round index and next round scheduled transition
pub(crate) type Round<T: Config> = StorageValue<_, RoundInfo<T::BlockNumber>, ValueQuery>;

#[pallet::storage]
#[pallet::getter(fn nominator_state2)]
/// DEPRECATED in favor of DelegatorState
/// Get nominator state associated with an account if account is nominating else None
pub(crate) type NominatorState2<T: Config> = StorageMap<
_,
Twox64Concat,
T::AccountId,
Nominator2<T::AccountId, BalanceOf<T>>,
OptionQuery,
>;

#[pallet::storage]
#[pallet::getter(fn delegator_state)]
/// Get delegator state associated with an account if account is delegating else None
Expand All @@ -493,18 +481,6 @@ pub mod pallet {
OptionQuery,
>;

#[pallet::storage]
#[pallet::getter(fn candidate_state)]
/// DEPRECATED
/// Get collator candidate state associated with an account if account is a candidate else None
pub(crate) type CandidateState<T: Config> = StorageMap<
_,
Twox64Concat,
T::AccountId,
CollatorCandidate<T::AccountId, BalanceOf<T>>,
OptionQuery,
>;

#[pallet::storage]
#[pallet::getter(fn candidate_info)]
/// Get collator candidate info associated with an account if account is candidate else None
Expand Down Expand Up @@ -554,18 +530,6 @@ pub mod pallet {
OptionQuery,
>;

#[pallet::storage]
#[pallet::getter(fn collator_state2)]
/// DEPRECATED in favor of CandidateState
/// Get collator state associated with an account if account is collating else None
pub(crate) type CollatorState2<T: Config> = StorageMap<
_,
Twox64Concat,
T::AccountId,
Collator2<T::AccountId, BalanceOf<T>>,
OptionQuery,
>;

#[pallet::storage]
#[pallet::getter(fn selected_candidates)]
/// The collator candidates selected for the current round
Expand Down
94 changes: 2 additions & 92 deletions pallets/parachain-staking/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@ use crate::pallet::{DelegationScheduledRequests, DelegatorState, Total};
use crate::types::deprecated::{DelegationChange, Delegator as OldDelegator};
use crate::types::Delegator;
use crate::{
BalanceOf, Bond, BottomDelegations, CandidateInfo, CandidateMetadata, CandidateState,
CapacityStatus, CollatorCandidate, Config, Delegations, Event, Pallet, Points, Round, Staked,
TopDelegations,
BalanceOf, Bond, BottomDelegations, CandidateInfo, CandidateMetadata, CapacityStatus,
CollatorCandidate, Config, Delegations, Event, Pallet, Points, Round, Staked, TopDelegations,
};
#[cfg(feature = "try-runtime")]
use frame_support::traits::OnRuntimeUpgradeHelpersExt;
Expand Down Expand Up @@ -525,95 +524,6 @@ impl<T: Config> OnRuntimeUpgrade for SplitCandidateStateToDecreasePoV<T> {
}
*/

/// Migration to properly increase maximum delegations per collator
/// The logic may be used to recompute the top and bottom delegations whenever
/// MaxTopDelegationsPerCandidate changes (works for if decreases as well)
pub struct IncreaseMaxDelegationsPerCandidate<T>(PhantomData<T>);
impl<T: Config> OnRuntimeUpgrade for IncreaseMaxDelegationsPerCandidate<T> {
fn on_runtime_upgrade() -> Weight {
let (mut reads, mut writes) = (0u64, 0u64);
for (account, state) in <CandidateState<T>>::iter() {
reads = reads.saturating_add(1u64);
// 1. collect all delegations into single vec and order them
let mut all_delegations = state.top_delegations.clone();
let mut starting_bottom_delegations = state.bottom_delegations.clone();
all_delegations.append(&mut starting_bottom_delegations);
// sort all delegations from greatest to least
all_delegations.sort_unstable_by(|a, b| b.amount.cmp(&a.amount));
let top_n = T::MaxTopDelegationsPerCandidate::get() as usize;
// 2. split them into top and bottom using the T::MaxNominatorsPerCollator
let top_delegations: Vec<Bond<T::AccountId, BalanceOf<T>>> =
all_delegations.iter().take(top_n).cloned().collect();
let bottom_delegations = if all_delegations.len() > top_n {
let rest = all_delegations.len() - top_n;
let bottom: Vec<Bond<T::AccountId, BalanceOf<T>>> =
all_delegations.iter().rev().take(rest).cloned().collect();
bottom
} else {
// empty, all nominations are in top
Vec::new()
};
let (mut total_counted, mut total_backing): (BalanceOf<T>, BalanceOf<T>) =
(state.bond.into(), state.bond.into());
for Bond { amount, .. } in &top_delegations {
total_counted = total_counted.saturating_add(*amount);
total_backing = total_backing.saturating_add(*amount);
}
for Bond { amount, .. } in &bottom_delegations {
total_backing = total_backing.saturating_add(*amount);
}
// update candidate pool with new total counted if it changed
if state.total_counted != total_counted && state.is_active() {
reads = reads.saturating_add(1u64);
writes = writes.saturating_add(1u64);
<Pallet<T>>::update_active(account.clone(), total_counted);
}
<CandidateState<T>>::insert(
account,
CollatorCandidate {
top_delegations,
bottom_delegations,
total_counted,
total_backing,
..state
},
);
writes = writes.saturating_add(1u64);
}
let weight = T::DbWeight::get();
// 20% of the max block weight as safety margin for computation
weight.reads(reads) + weight.writes(writes) + 100_000_000_000
}
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<(), &'static str> {
// get delegation count for all candidates to check consistency
for (account, state) in <CandidateState<T>>::iter() {
// insert top + bottom into some temp map?
let total_delegation_count =
state.top_delegations.len() as u32 + state.bottom_delegations.len() as u32;
Self::set_temp_storage(
total_delegation_count,
&format!("Candidate{:?}DelegationCount", account)[..],
);
}
Ok(())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade() -> Result<(), &'static str> {
// check that top + bottom are the same as the expected (stored in temp)
for (account, state) in <CandidateState<T>>::iter() {
let expected_count: u32 =
Self::get_temp_storage(&format!("Candidate{:?}DelegationCount", account)[..])
.expect("qed");
let actual_count =
state.top_delegations.len() as u32 + state.bottom_delegations.len() as u32;
assert_eq!(expected_count, actual_count);
}
Ok(())
}
}

/// Migration to replace the automatic ExitQueue with a manual exits API.
/// This migration is idempotent so it can be run more than once without any risk.
// pub struct RemoveExitQueue<T>(PhantomData<T>);
Expand Down
4 changes: 2 additions & 2 deletions pallets/parachain-staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,7 @@ fn execute_leave_candidates_removes_candidate_state() {
1,
0
));
assert!(ParachainStaking::candidate_state(1).is_none());
assert!(ParachainStaking::candidate_info(1).is_none());
});
}

Expand Down Expand Up @@ -1099,7 +1099,7 @@ fn execute_leave_candidates_removes_pending_delegation_requests() {
1,
1
));
assert!(ParachainStaking::candidate_state(1).is_none());
assert!(ParachainStaking::candidate_info(1).is_none());
assert!(
!ParachainStaking::delegation_scheduled_requests(&1)
.iter()
Expand Down
57 changes: 1 addition & 56 deletions precompiles/parachain-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,57 +39,35 @@ type BalanceOf<Runtime> = <<Runtime as pallet_parachain_staking::Config>::Curren
#[generate_function_selector]
#[derive(Debug, PartialEq)]
enum Action {
// DEPRECATED
MinNomination = "min_nomination()",
MinDelegation = "min_delegation()",
Points = "points(uint256)",
CandidateCount = "candidate_count()",
Round = "round()",
// DEPRECATED
CollatorNominationCount = "collator_nomination_count(address)",
// DEPRECATED
NominatorNominationCount = "nominator_nomination_count(address)",
CandidateDelegationCount = "candidate_delegation_count(address)",
DelegatorDelegationCount = "delegator_delegation_count(address)",
SelectedCandidates = "selected_candidates()",
// DEPRECATED
IsNominator = "is_nominator(address)",
IsDelegator = "is_delegator(address)",
IsCandidate = "is_candidate(address)",
IsSelectedCandidate = "is_selected_candidate(address)",
DelegationRequestIsPending = "delegation_request_is_pending(address,address)",
CandidateExitIsPending = "candidate_exit_is_pending(address)",
CandidateRequestIsPending = "candidate_request_is_pending(address)",
JoinCandidates = "join_candidates(uint256,uint256)",
// DEPRECATED
LeaveCandidates = "leave_candidates(uint256)",
ScheduleLeaveCandidates = "schedule_leave_candidates(uint256)",
ExecuteLeaveCandidates = "execute_leave_candidates(address,uint256)",
CancelLeaveCandidates = "cancel_leave_candidates(uint256)",
GoOffline = "go_offline()",
GoOnline = "go_online()",
// DEPRECATED
CandidateBondLess = "candidate_bond_less(uint256)",
ScheduleCandidateBondLess = "schedule_candidate_bond_less(uint256)",
CandidateBondMore = "candidate_bond_more(uint256)",
ExecuteCandidateBondLess = "execute_candidate_bond_less(address)",
CancelCandidateBondLess = "cancel_candidate_bond_less()",
// DEPRECATED
Nominate = "nominate(address,uint256,uint256,uint256)",
Delegate = "delegate(address,uint256,uint256,uint256)",
// DEPRECATED
LeaveNominators = "leave_nominators(uint256)",
ScheduleLeaveDelegators = "schedule_leave_delegators()",
ExecuteLeaveDelegators = "execute_leave_delegators(address,uint256)",
CancelLeaveDelegators = "cancel_leave_delegators()",
// DEPRECATED
RevokeNomination = "revoke_nomination(address)",
ScheduleRevokeDelegation = "schedule_revoke_delegation(address)",
// DEPRECATED
NominatorBondLess = "nominator_bond_less(address,uint256)",
ScheduleDelegatorBondLess = "schedule_delegator_bond_less(address,uint256)",
// DEPRECATED
NominatorBondMore = "nominator_bond_more(address,uint256)",
DelegatorBondMore = "delegator_bond_more(address,uint256)",
ExecuteDelegationRequest = "execute_delegation_request(address,address)",
CancelDelegationRequest = "cancel_delegation_request(address)",
Expand Down Expand Up @@ -119,17 +97,13 @@ where

handle.check_function_modifier(match selector {
// Views
Action::IsNominator
| Action::IsDelegator
Action::IsDelegator
| Action::IsCandidate
| Action::IsSelectedCandidate
| Action::MinNomination
| Action::MinDelegation
| Action::Points
| Action::CandidateCount
| Action::Round
| Action::CollatorNominationCount
| Action::NominatorNominationCount
| Action::CandidateDelegationCount
| Action::DelegatorDelegationCount
| Action::SelectedCandidates
Expand All @@ -138,50 +112,35 @@ where
| Action::CandidateRequestIsPending => FunctionModifier::View,
// Non-payables
Action::JoinCandidates
| Action::LeaveCandidates
| Action::ScheduleLeaveCandidates
| Action::ExecuteLeaveCandidates
| Action::CancelLeaveCandidates
| Action::GoOffline
| Action::GoOnline
| Action::CandidateBondLess
| Action::ScheduleCandidateBondLess
| Action::CandidateBondMore
| Action::ExecuteCandidateBondLess
| Action::CancelCandidateBondLess
| Action::Nominate
| Action::Delegate
| Action::LeaveNominators
| Action::ScheduleLeaveDelegators
| Action::ExecuteLeaveDelegators
| Action::CancelLeaveDelegators
| Action::RevokeNomination
| Action::ScheduleRevokeDelegation
| Action::NominatorBondLess
| Action::ScheduleDelegatorBondLess
| Action::NominatorBondMore
| Action::DelegatorBondMore
| Action::ExecuteDelegationRequest
| Action::CancelDelegationRequest => FunctionModifier::NonPayable,
})?;

// Return early if storage getter; return (origin, call) if dispatchable
let (origin, call) = match selector {
// DEPRECATED
Action::MinNomination => return Self::min_delegation(handle),
Action::MinDelegation => return Self::min_delegation(handle),
Action::Points => return Self::points(handle),
Action::CandidateCount => return Self::candidate_count(handle),
Action::Round => return Self::round(handle),
// DEPRECATED
Action::CollatorNominationCount => return Self::candidate_delegation_count(handle),
// DEPRECATED
Action::NominatorNominationCount => return Self::delegator_delegation_count(handle),
Action::CandidateDelegationCount => return Self::candidate_delegation_count(handle),
Action::DelegatorDelegationCount => return Self::delegator_delegation_count(handle),
Action::SelectedCandidates => return Self::selected_candidates(handle),
// DEPRECATED
Action::IsNominator => return Self::is_delegator(handle),
Action::IsDelegator => return Self::is_delegator(handle),
Action::IsCandidate => return Self::is_candidate(handle),
Action::IsSelectedCandidate => return Self::is_selected_candidate(handle),
Expand All @@ -192,35 +151,21 @@ where
Action::CandidateRequestIsPending => return Self::candidate_request_is_pending(handle),
// runtime methods (dispatchables)
Action::JoinCandidates => Self::join_candidates(handle)?,
// DEPRECATED
Action::LeaveCandidates => Self::schedule_leave_candidates(handle)?,
Action::ScheduleLeaveCandidates => Self::schedule_leave_candidates(handle)?,
Action::ExecuteLeaveCandidates => Self::execute_leave_candidates(handle)?,
Action::CancelLeaveCandidates => Self::cancel_leave_candidates(handle)?,
Action::GoOffline => Self::go_offline(handle)?,
Action::GoOnline => Self::go_online(handle)?,
// DEPRECATED
Action::CandidateBondLess => Self::schedule_candidate_bond_less(handle)?,
Action::ScheduleCandidateBondLess => Self::schedule_candidate_bond_less(handle)?,
Action::CandidateBondMore => Self::candidate_bond_more(handle)?,
Action::ExecuteCandidateBondLess => Self::execute_candidate_bond_less(handle)?,
Action::CancelCandidateBondLess => Self::cancel_candidate_bond_less(handle)?,
// DEPRECATED
Action::Nominate => Self::delegate(handle)?,
Action::Delegate => Self::delegate(handle)?,
// DEPRECATED
Action::LeaveNominators => Self::schedule_leave_delegators(handle)?,
Action::ScheduleLeaveDelegators => Self::schedule_leave_delegators(handle)?,
Action::ExecuteLeaveDelegators => Self::execute_leave_delegators(handle)?,
Action::CancelLeaveDelegators => Self::cancel_leave_delegators(handle)?,
// DEPRECATED
Action::RevokeNomination => Self::schedule_revoke_delegation(handle)?,
Action::ScheduleRevokeDelegation => Self::schedule_revoke_delegation(handle)?,
// DEPRECATED
Action::NominatorBondLess => Self::schedule_delegator_bond_less(handle)?,
Action::ScheduleDelegatorBondLess => Self::schedule_delegator_bond_less(handle)?,
// DEPRECATED
Action::NominatorBondMore => Self::delegator_bond_more(handle)?,
Action::DelegatorBondMore => Self::delegator_bond_more(handle)?,
Action::ExecuteDelegationRequest => Self::execute_delegation_request(handle)?,
Action::CancelDelegationRequest => Self::cancel_delegation_request(handle)?,
Expand Down
Loading