Skip to content

Commit

Permalink
Fix for incorrect eras per cycle function (#1166)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinonard authored Feb 7, 2024
1 parent 15809d4 commit 8b2013c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
12 changes: 9 additions & 3 deletions pallets/inflation/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,14 +460,20 @@ fn cycle_configuration_works() {
ExternalityBuilder::build().execute_with(|| {
type CycleConfig = <Test as Config>::CycleConfiguration;

let eras_per_period = CycleConfig::eras_per_voting_subperiod()
+ CycleConfig::eras_per_build_and_earn_subperiod();
let eras_per_period = CycleConfig::eras_per_build_and_earn_subperiod() + 1;
assert_eq!(CycleConfig::eras_per_period(), eras_per_period);

let period_in_era_lengths = CycleConfig::eras_per_voting_subperiod()
+ CycleConfig::eras_per_build_and_earn_subperiod();
assert_eq!(CycleConfig::period_in_era_lengths(), period_in_era_lengths);

let eras_per_cycle = eras_per_period * CycleConfig::periods_per_cycle();
assert_eq!(CycleConfig::eras_per_cycle(), eras_per_cycle);

let blocks_per_cycle = eras_per_cycle * CycleConfig::blocks_per_era();
let cycle_in_era_lengths = period_in_era_lengths * CycleConfig::periods_per_cycle();
assert_eq!(CycleConfig::cycle_in_era_lengths(), cycle_in_era_lengths);

let blocks_per_cycle = cycle_in_era_lengths * CycleConfig::blocks_per_era();
assert_eq!(CycleConfig::blocks_per_cycle(), blocks_per_cycle);

let build_and_earn_eras_per_cycle =
Expand Down
18 changes: 14 additions & 4 deletions primitives/src/dapp_staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,34 @@ pub trait CycleConfiguration {
fn blocks_per_era() -> BlockNumber;

/// For how many standard era lengths does the period last.
fn eras_per_period() -> EraNumber {
fn period_in_era_lengths() -> EraNumber {
Self::eras_per_voting_subperiod().saturating_add(Self::eras_per_build_and_earn_subperiod())
}

/// For how many standard era lengths does the cycle (a 'year') last.
fn eras_per_cycle() -> EraNumber {
Self::eras_per_period().saturating_mul(Self::periods_per_cycle())
fn cycle_in_era_lengths() -> EraNumber {
Self::period_in_era_lengths().saturating_mul(Self::periods_per_cycle())
}

/// How many blocks are there per cycle (a 'year').
fn blocks_per_cycle() -> BlockNumber {
Self::blocks_per_era().saturating_mul(Self::eras_per_cycle())
Self::blocks_per_era().saturating_mul(Self::cycle_in_era_lengths())
}

/// For how many standard era lengths do all the build&earn subperiods in a cycle last.
fn build_and_earn_eras_per_cycle() -> EraNumber {
Self::eras_per_build_and_earn_subperiod().saturating_mul(Self::periods_per_cycle())
}

/// How many distinct eras are there in a single period.
fn eras_per_period() -> EraNumber {
Self::eras_per_build_and_earn_subperiod().saturating_add(1)
}

/// How many distinct eras are there in a cycle.
fn eras_per_cycle() -> EraNumber {
Self::eras_per_period().saturating_mul(Self::periods_per_cycle())
}
}

/// Trait for observers (listeners) of various events related to dApp staking protocol.
Expand Down
19 changes: 19 additions & 0 deletions runtime/shiden/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1106,8 +1106,27 @@ pub type Migrations = (
DappStakingMigrationName,
<Runtime as frame_system::Config>::DbWeight,
>,
// Part of shiden-119
RecalculationEraFix,
);

use frame_support::traits::OnRuntimeUpgrade;
pub struct RecalculationEraFix;
impl OnRuntimeUpgrade for RecalculationEraFix {
fn on_runtime_upgrade() -> Weight {
let first_dapp_staking_v3_era = 743;

let expected_recalculation_era =
InflationCycleConfig::eras_per_cycle().saturating_add(first_dapp_staking_v3_era);

pallet_inflation::ActiveInflationConfig::<Runtime>::mutate(|config| {
config.recalculation_era = expected_recalculation_era;
});

<Runtime as frame_system::Config>::DbWeight::get().reads_writes(1, 1)
}
}

type EventRecord = frame_system::EventRecord<
<Runtime as frame_system::Config>::RuntimeEvent,
<Runtime as frame_system::Config>::Hash,
Expand Down

0 comments on commit 8b2013c

Please sign in to comment.