Skip to content

Commit

Permalink
Fix collator migration for RTU4 (#598)
Browse files Browse the repository at this point in the history
* add devops section to prep env script

* fix collator migration

* revert test period
  • Loading branch information
mikiquantum authored Jan 21, 2022
1 parent 3bfe3e6 commit 3c7f93d
Showing 1 changed file with 8 additions and 157 deletions.
165 changes: 8 additions & 157 deletions runtime/altair/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,12 @@ pub fn native_version() -> NativeVersion {
}
}

use frame_support::sp_runtime::traits::{Convert, IdentifyAccount, OpaqueKeys, Verify};
/// Custom runtime upgrades
///
/// Migration to include collator-selection in a running chain
use pallet_collator_selection::CandidateInfo;
pub struct IntegrateCollatorSelection<T>(PhantomData<T>);

const CANDIDATES: [[u8; 32]; 0] = [];

const DESIRED_CANDIDATES: u32 = 0;
const CANDIDACY_BOND: Balance = 1 * CFG;

type AccountPublic = <Signature as Verify>::Signer;
type BalanceOfCollatorSelection<T> =
<<T as pallet_collator_selection::Config>::Currency as Currency<
<T as frame_system::Config>::AccountId,
Expand All @@ -116,76 +108,6 @@ where
1008
}

fn inject_invulnerables(invulnerables: &[(T::AccountId, T::Keys)]) -> Weight {
// Store the keys of any additional invulnerables in the `NextKeys` storage
let consumed = Self::set_keys(invulnerables);

let session_validators = invulnerables
.iter()
.map(|(who, _keys)| who.clone())
.collect();

<pallet_collator_selection::Invulnerables<T>>::set(session_validators);

let queued_validators: Vec<(<T as pallet_session::Config>::ValidatorId, T::Keys)> =
invulnerables
.iter()
.map(|(who, keys)| {
(
// TODO(frederik): Take care of handling this unwrap()
<<T as pallet_session::Config>::ValidatorIdOf>::convert(who.clone())
.unwrap(),
keys.clone(),
)
})
.collect();

<pallet_session::QueuedKeys<T>>::set(queued_validators);

consumed + Self::db_access_weights(Some(1), Some(2))
}

fn inject_desired_candidates(max: u32) -> Weight {
<pallet_collator_selection::DesiredCandidates<T>>::set(max);

Self::db_access_weights(None, Some(1))
}

fn inject_candidates(
candidates: &[(T::AccountId, T::Keys)],
deposit: BalanceOfCollatorSelection<T>,
) -> Weight {
// Store the keys of any additional candidates in the `NextKeys` storage
let consumed = Self::set_keys(candidates);
let threshold = T::KickThreshold::get();
let now = frame_system::Pallet::<T>::block_number();

let infos = candidates
.iter()
.map(|(who, _key)| {
<pallet_collator_selection::LastAuthoredBlock<T>>::insert(
who.clone(),
now + threshold,
);

CandidateInfo {
who: who.clone(),
deposit,
}
})
.collect();

<pallet_collator_selection::Candidates<T>>::set(infos);

consumed + Self::db_access_weights(Some(2), Some(1 + 1 * (candidates.len() as u64)))
}

fn inject_candidacy_bond(bond: BalanceOfCollatorSelection<T>) -> Weight {
<pallet_collator_selection::CandidacyBond<T>>::set(bond);

Self::db_access_weights(None, Some(1))
}

fn db_access_weights(reads: Option<u64>, writes: Option<u64>) -> Weight {
let mut weight: Weight = 0u32 as Weight;

Expand All @@ -203,67 +125,6 @@ where

weight
}

fn set_keys(from: &[(T::AccountId, T::Keys)]) -> Weight {
let inner_set_keys = |who, keys: T::Keys| {
let old_keys = <pallet_session::NextKeys<T>>::get(&who);

for id in T::Keys::key_ids() {
let _key = keys.get_raw(*id);

// TODO(frederik): Do we need this
// ensure keys are without duplication.
// let is_owner = <pallet_session::KeyOwner<T>>::get((id, key).map_or(true, |owner| &owner == who);
}

for id in T::Keys::key_ids() {
let key = keys.get_raw(*id);

if let Some(old) = old_keys.as_ref().map(|k| k.get_raw(*id)) {
if key == old {
continue;
}

<pallet_session::KeyOwner<T>>::remove((*id, old));
}

<pallet_session::KeyOwner<T>>::insert((*id, key), &who);
}

<pallet_session::NextKeys<T>>::insert(&who, &keys);
};

from.iter().for_each(|(who, keys)| {
inner_set_keys(
// TODO(frederik): think of something to recover from the convert...
<T as pallet_session::Config>::ValidatorIdOf::convert(who.clone()).unwrap(),
keys.clone(),
)
});

Self::db_access_weights(Some(3 * (from.len() as u64)), Some(2 * (from.len() as u64)))
}

#[allow(non_snake_case)]
fn into_T_tuple(raw_ids: Vec<[u8; 32]>) -> Vec<(T::AccountId, T::Keys)> {
raw_ids
.to_vec()
.into_iter()
.map::<(T::AccountId, T::Keys), _>(|bytes| {
(
AccountPublic::from(sp_core::sr25519::Public(bytes))
.into_account()
.into(),
SessionKeys {
aura: sp_consensus_aura::sr25519::AuthorityId::from(
sp_core::sr25519::Public(bytes),
),
}
.into(),
)
})
.collect::<Vec<(T::AccountId, T::Keys)>>()
}
}

impl<T> OnRuntimeUpgrade for IntegrateCollatorSelection<T>
Expand All @@ -272,30 +133,20 @@ where
BalanceOfCollatorSelection<T>: From<u128>,
T::AccountId: From<sp_runtime::AccountId32>,
T::Keys: From<SessionKeys>,
[u8; 32]: From<<T as pallet_session::Config>::ValidatorId>,
<T as frame_system::Config>::AccountId: From<<T as pallet_session::Config>::ValidatorId>,
{
fn on_runtime_upgrade() -> Weight {
let mut consumed: Weight = 0;

let current_validators_ids: Vec<[u8; 32]> = <pallet_session::Validators<T>>::get()
.into_iter()
.map(|who| Into::<[u8; 32]>::into(who))
.collect();
if VERSION.spec_version == IntegrateCollatorSelection::<T>::to_version() {
let current_validators = <pallet_session::Validators<T>>::get()
.into_iter()
.map(|who| who.into())
.collect();

let invulnerables = Self::into_T_tuple(current_validators_ids);
let candidates = Self::into_T_tuple(CANDIDATES.to_vec());
<pallet_collator_selection::Invulnerables<T>>::set(current_validators);

if VERSION.spec_version == IntegrateCollatorSelection::<T>::to_version() {
consumed +=
IntegrateCollatorSelection::<T>::inject_invulnerables(invulnerables.as_slice());
consumed +=
IntegrateCollatorSelection::<T>::inject_desired_candidates(DESIRED_CANDIDATES);
consumed += IntegrateCollatorSelection::<T>::inject_candidates(
candidates.as_slice(),
CANDIDACY_BOND.into(),
);
consumed +=
IntegrateCollatorSelection::<T>::inject_candidacy_bond(CANDIDACY_BOND.into());
consumed += Self::db_access_weights(Some(1), Some(1))
}

return consumed;
Expand Down

0 comments on commit 3c7f93d

Please sign in to comment.