Skip to content

Commit

Permalink
Release koi-6653 (#1574)
Browse files Browse the repository at this point in the history
* Release `koi-6653`

* Fixes
  • Loading branch information
AurevoirXavier authored Aug 23, 2024
1 parent d9dd210 commit e13e14b
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 69 deletions.
65 changes: 22 additions & 43 deletions pallet/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ macro_rules! call_on_exposure {
(_, $crate::CacheState::$s, _) => Ok(<$crate::ExposureCache1<$t>>$($f)*),
(_, _, $crate::CacheState::$s) => Ok(<$crate::ExposureCache2<$t>>$($f)*),
_ => {
log::error!("[pallet::staking] exposure cache states must be correct; qed");
log::error!("exposure cache states must be correct; qed");

Err("[pallet::staking] exposure cache states must be correct; qed")
},
Expand All @@ -96,7 +96,7 @@ macro_rules! call_on_cache {
(_, $crate::CacheState::$s, _) => Ok(<$crate::CollatorsCache1<$t>>$($f)*),
(_, _, $crate::CacheState::$s) => Ok(<$crate::CollatorsCache2<$t>>$($f)*),
_ => {
log::error!("[pallet::staking] collators cache states must be correct; qed");
log::error!("collators cache states must be correct; qed");

Err("[pallet::staking] collators cache states must be correct; qed")
},
Expand Down Expand Up @@ -183,8 +183,6 @@ pub mod pallet {
Payout { who: T::AccountId, amount: Balance },
/// Unable to pay the staker's reward.
Unpaid { who: T::AccountId, amount: Balance },
/// A new collator set has been elected.
Elected { collators: Vec<T::AccountId> },
}

#[pallet::error]
Expand Down Expand Up @@ -780,43 +778,24 @@ pub mod pallet {

let bn = <frame_system::Pallet<T>>::block_number();

log::info!(
"[pallet::staking] assembling new collators for new session {index} at #{bn:?}",
);
log::info!("assembling new collators for new session {index} at #{bn:?}",);

let (n1, n2) = Self::elect_ns();
let cs_from_contract = Self::try_elect(n1, Self::elect_from_contract);
let cs_from_pallet = Self::try_elect(n2, Self::elect);

if n1 != cs_from_contract.len() as u32 || n2 != cs_from_pallet.len() as u32 {
log::error!(
"[pallet::staking] collator count mismatch; \
expected collator count from contract: {n1}, from pallet: {n2}, \
actual collator count from contract: {}, from pallet: {}",
cs_from_contract.len(),
cs_from_pallet.len(),
);

return None;
}
let cs_from_contract = Self::elect_from_contract(n1)?;
let cs_from_pallet = Self::elect(n2)?;

log::info!("collators from contract {cs_from_contract:?}");
log::info!("collators from pallet {cs_from_pallet:?}");

let cs = [cs_from_contract, cs_from_pallet].concat();

if cs.is_empty() {
// This error log is acceptable when testing with `genesis_collator = false`.
log::error!(
"[pallet::staking] fail to elect collators for new session {index} at #{bn:?}"
);

// Impossible case.
//
// But if there is an issue, retain the old collators; do not alter the session
// collators if any error occurs to prevent the chain from stalling.
None
} else {
// ? if we really need this event.
Self::deposit_event(Event::Elected { collators: cs.clone() });

Some(cs)
}
}
Expand Down Expand Up @@ -889,28 +868,22 @@ pub mod pallet {
pub fn elect_ns() -> (u32, u32) {
let n = <CollatorCount<T>>::get();
let n1 = Self::migration_progress() * n;
let n2 = n - n1;

log::info!("election ns {n1} + {n2}");

(n1, n - n1)
(n1, n2)
}

fn migration_progress() -> Perbill {
const TOTAL: Moment = 30 * 2 * DAY_IN_MILLIS;

let start = <MigrationStartPoint<T>>::get();
let prog = Perbill::from_rational(now::<T>() - start, TOTAL);

Perbill::from_rational(now::<T>() - start, TOTAL)
}
log::info!("migration progress {prog:?}");

fn try_elect<F, R>(n: u32, elect: F) -> R
where
F: FnOnce(u32) -> Option<R>,
R: Default,
{
if n > 0 {
elect(n).unwrap_or_default()
} else {
Default::default()
}
prog
}

fn elect_from_contract(n: u32) -> Option<Vec<T::AccountId>> {
Expand Down Expand Up @@ -940,7 +913,13 @@ pub mod pallet {
fn start_session(_: u32) {}

fn new_session(index: u32) -> Option<Vec<T::AccountId>> {
Self::prepare_new_session(index)
let maybe_collators = Self::prepare_new_session(index);

if maybe_collators.is_some() {
log::error!("fail to elect collators for session {index}");
}

maybe_collators
}
}
}
Expand Down
14 changes: 12 additions & 2 deletions runtime/crab/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,19 @@ fn migrate() -> frame_support::weights::Weight {
b"ExposureCacheStates",
&[],
) {
let _ = migration::put_storage_value(b"DarwinaStaking", b"CacheStates", &[], s);
migration::put_storage_value(b"DarwinaStaking", b"CacheStates", &[], s);
}

if let Ok(owner) =
array_bytes::hex_n_into::<_, AccountId, 20>("0x7FAcDaFB282028E4B3264fB08cd633A9142514df")
{
let _ = <pallet_assets::Pallet<Runtime>>::transfer_ownership(
RuntimeOrigin::signed(ROOT),
codec::Compact(AssetIds::CKton as AssetId),
owner,
);
}

// frame_support::weights::Weight::zero()
<Runtime as frame_system::Config>::DbWeight::get().reads_writes(1, 1)
<Runtime as frame_system::Config>::DbWeight::get().reads_writes(3, 3)
}
2 changes: 1 addition & 1 deletion runtime/crab/src/pallets/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ sp_runtime::impl_opaque_keys! {
}
}

fast_runtime_or_not!(PERIOD, BlockNumber, 5 * MINUTES, 6 * HOURS);
darwinia_common_runtime::fast_runtime_or_not!(PERIOD, BlockNumber, 5 * MINUTES, 6 * HOURS);

type Offset = ConstU32<0>;
type Period = ConstU32<{ PERIOD }>;
Expand Down
2 changes: 1 addition & 1 deletion runtime/crab/src/pallets/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::*;
// polkadot-sdk
use frame_support::traits::Currency;

fast_runtime_or_not!(DURATION, BlockNumber, 5 * MINUTES, 14 * DAYS);
darwinia_common_runtime::fast_runtime_or_not!(DURATION, BlockNumber, 5 * MINUTES, 14 * DAYS);

pub enum RingStaking {}
impl darwinia_staking::Stake for RingStaking {
Expand Down
14 changes: 12 additions & 2 deletions runtime/darwinia/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,19 @@ fn migrate() -> frame_support::weights::Weight {
b"ExposureCacheStates",
&[],
) {
let _ = migration::put_storage_value(b"DarwinaStaking", b"CacheStates", &[], s);
migration::put_storage_value(b"DarwinaStaking", b"CacheStates", &[], s);
}

if let Ok(owner) =
array_bytes::hex_n_into::<_, AccountId, 20>("0x7FAcDaFB282028E4B3264fB08cd633A9142514df")
{
let _ = <pallet_assets::Pallet<Runtime>>::transfer_ownership(
RuntimeOrigin::signed(ROOT),
codec::Compact(AssetIds::Kton as AssetId),
owner,
);
}

// frame_support::weights::Weight::zero()
<Runtime as frame_system::Config>::DbWeight::get().reads_writes(1, 101)
<Runtime as frame_system::Config>::DbWeight::get().reads_writes(3, 103)
}
2 changes: 1 addition & 1 deletion runtime/darwinia/src/pallets/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ sp_runtime::impl_opaque_keys! {
}
}

fast_runtime_or_not!(PERIOD, BlockNumber, 5 * MINUTES, 6 * HOURS);
darwinia_common_runtime::fast_runtime_or_not!(PERIOD, BlockNumber, 5 * MINUTES, 6 * HOURS);

type Offset = ConstU32<0>;
type Period = ConstU32<{ PERIOD }>;
Expand Down
2 changes: 1 addition & 1 deletion runtime/darwinia/src/pallets/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::*;
// polkadot-sdk
use frame_support::traits::Currency;

fast_runtime_or_not!(DURATION, BlockNumber, 5 * MINUTES, 14 * DAYS);
darwinia_common_runtime::fast_runtime_or_not!(DURATION, BlockNumber, 5 * MINUTES, 14 * DAYS);

pub enum RingStaking {}
impl darwinia_staking::Stake for RingStaking {
Expand Down
2 changes: 1 addition & 1 deletion runtime/koi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ pub const VERSION: sp_version::RuntimeVersion = sp_version::RuntimeVersion {
spec_name: sp_runtime::create_runtime_str!("Darwinia Koi"),
impl_name: sp_runtime::create_runtime_str!("DarwiniaOfficialRust"),
authoring_version: 0,
spec_version: 6_6_5_2,
spec_version: 6_6_5_3,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 0,
Expand Down
21 changes: 9 additions & 12 deletions runtime/koi/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,16 @@ impl frame_support::traits::OnRuntimeUpgrade for CustomOnRuntimeUpgrade {
}

fn migrate() -> frame_support::weights::Weight {
let _ =
migration::clear_storage_prefix(b"DarwinaStaking", b"CollatorCacheState", &[], None, None);

// dawinia
use darwinia_staking::CacheState;
if let Some(s) = migration::get_storage_value::<(CacheState, CacheState, CacheState)>(
b"DarwinaStaking",
b"ExposureCacheStates",
&[],
) {
let _ = migration::put_storage_value(b"DarwinaStaking", b"CacheStates", &[], s);
if let Ok(owner) =
array_bytes::hex_n_into::<_, AccountId, 20>("0x7FAcDaFB282028E4B3264fB08cd633A9142514df")
{
let _ = <pallet_assets::Pallet<Runtime>>::transfer_ownership(
RuntimeOrigin::signed(ROOT),
codec::Compact(AssetIds::KKton as AssetId),
owner,
);
}

// frame_support::weights::Weight::zero()
<Runtime as frame_system::Config>::DbWeight::get().reads_writes(1, 2)
<Runtime as frame_system::Config>::DbWeight::get().reads_writes(2, 2)
}
4 changes: 2 additions & 2 deletions runtime/koi/src/pallets/governance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ pub use pallet_collective::Instance1 as TechnicalCollective;

pub(super) use crate::*;

fast_runtime_or_not!(TIME_1, BlockNumber, 2 * MINUTES, 10 * MINUTES);
fast_runtime_or_not!(TIME_2, BlockNumber, 5 * MINUTES, 20 * MINUTES);
darwinia_common_runtime::fast_runtime_or_not!(TIME_1, BlockNumber, 2 * MINUTES, 10 * MINUTES);
darwinia_common_runtime::fast_runtime_or_not!(TIME_2, BlockNumber, 5 * MINUTES, 20 * MINUTES);

type Time1 = ConstU32<TIME_1>;
type Time2 = ConstU32<TIME_2>;
Expand Down
4 changes: 1 addition & 3 deletions runtime/koi/src/pallets/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ sp_runtime::impl_opaque_keys! {
}
}

fast_runtime_or_not!(PERIOD, BlockNumber, 5 * MINUTES, 6 * HOURS);

type Offset = ConstU32<0>;
type Period = ConstU32<{ PERIOD }>;
type Period = ConstU32<{ 5 * MINUTES }>;

impl pallet_session::Config for Runtime {
type Keys = SessionKeys;
Expand Down

0 comments on commit e13e14b

Please sign in to comment.