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

Fix/incorrect eras per cycle function #1166

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
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
Loading