Skip to content

Commit

Permalink
Fix genesis chain specs (#1046)
Browse files Browse the repository at this point in the history
* fix genesis chain_specs so round inflation is set based on default blocks per round configured

* fix build

* use gorka suggestion to use associated type instead of storage item

* fix units

* export ParachainStakingConfigTrait from runtime to use in chainspecs

* fix

* revert export use direct const getter

* unused import

* try TS inflation genesis config

* fix arithmetic

* fix annual to round inflation in genesis

* fix

* fix

* pub mod inflation

* compiles still TS tests off

* fix ts
  • Loading branch information
4meta5 authored Dec 2, 2021
1 parent 3afe74b commit e385285
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 39 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions node/service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ moonbeam-rpc-primitives-debug = { path = "../../primitives/rpc/debug" }
moonbeam-rpc-primitives-txpool = { path = "../../primitives/rpc/txpool" }
moonbeam-rpc-trace = { path = "../../client/rpc/trace" }
moonbeam-rpc-txpool = { path = "../../client/rpc/txpool" }
parachain-staking = { path = "../../pallets/parachain-staking" }

# Moonbeam runtimes
moonbase-runtime = { path = "../../runtime/moonbase", optional = true }
Expand Down
28 changes: 17 additions & 11 deletions node/service/src/chain_spec/moonbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,23 +161,29 @@ pub fn get_chain_spec(para_id: ParaId) -> ChainSpec {
}

pub fn moonbeam_inflation_config() -> InflationInfo<Balance> {
fn to_round_inflation(annual: Range<Perbill>) -> Range<Perbill> {
use parachain_staking::inflation::{perbill_annual_to_perbill_round, BLOCKS_PER_YEAR};
perbill_annual_to_perbill_round(
annual,
// rounds per year
BLOCKS_PER_YEAR / moonbase_runtime::DefaultBlocksPerRound::get(),
)
}
let annual = Range {
min: Perbill::from_percent(4),
ideal: Perbill::from_percent(5),
max: Perbill::from_percent(5),
};
InflationInfo {
// staking expectations
expect: Range {
min: 100_000 * UNIT,
ideal: 200_000 * UNIT,
max: 500_000 * UNIT,
},
annual: Range {
min: Perbill::from_percent(4),
ideal: Perbill::from_percent(5),
max: Perbill::from_percent(5),
},
// 8766 rounds (hours) in a year
round: Range {
min: Perbill::from_parts(Perbill::from_percent(4).deconstruct() / 8766),
ideal: Perbill::from_parts(Perbill::from_percent(5).deconstruct() / 8766),
max: Perbill::from_parts(Perbill::from_percent(5).deconstruct() / 8766),
},
// annual inflation
annual,
round: to_round_inflation(annual),
}
}

Expand Down
28 changes: 17 additions & 11 deletions node/service/src/chain_spec/moonbeam.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,23 +144,29 @@ pub fn get_chain_spec(para_id: ParaId) -> ChainSpec {
}

pub fn moonbeam_inflation_config() -> InflationInfo<Balance> {
fn to_round_inflation(annual: Range<Perbill>) -> Range<Perbill> {
use parachain_staking::inflation::{perbill_annual_to_perbill_round, BLOCKS_PER_YEAR};
perbill_annual_to_perbill_round(
annual,
// rounds per year
BLOCKS_PER_YEAR / moonbeam_runtime::DefaultBlocksPerRound::get(),
)
}
let annual = Range {
min: Perbill::from_percent(4),
ideal: Perbill::from_percent(5),
max: Perbill::from_percent(5),
};
InflationInfo {
// staking expectations
expect: Range {
min: 100_000 * GLMR,
ideal: 200_000 * GLMR,
max: 500_000 * GLMR,
},
annual: Range {
min: Perbill::from_percent(4),
ideal: Perbill::from_percent(5),
max: Perbill::from_percent(5),
},
// 8766 rounds (hours) in a year
round: Range {
min: Perbill::from_parts(Perbill::from_percent(4).deconstruct() / 8766),
ideal: Perbill::from_parts(Perbill::from_percent(5).deconstruct() / 8766),
max: Perbill::from_parts(Perbill::from_percent(5).deconstruct() / 8766),
},
// annual inflation
annual,
round: to_round_inflation(annual),
}
}

Expand Down
28 changes: 17 additions & 11 deletions node/service/src/chain_spec/moonriver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,23 +140,29 @@ pub fn get_chain_spec(para_id: ParaId) -> ChainSpec {
}

pub fn moonbeam_inflation_config() -> InflationInfo<Balance> {
fn to_round_inflation(annual: Range<Perbill>) -> Range<Perbill> {
use parachain_staking::inflation::{perbill_annual_to_perbill_round, BLOCKS_PER_YEAR};
perbill_annual_to_perbill_round(
annual,
// rounds per year
BLOCKS_PER_YEAR / moonriver_runtime::DefaultBlocksPerRound::get(),
)
}
let annual = Range {
min: Perbill::from_percent(4),
ideal: Perbill::from_percent(5),
max: Perbill::from_percent(5),
};
InflationInfo {
// staking expectations
expect: Range {
min: 100_000 * MOVR,
ideal: 200_000 * MOVR,
max: 500_000 * MOVR,
},
annual: Range {
min: Perbill::from_percent(4),
ideal: Perbill::from_percent(5),
max: Perbill::from_percent(5),
},
// 8766 rounds (hours) in a year
round: Range {
min: Perbill::from_parts(Perbill::from_percent(4).deconstruct() / 8766),
ideal: Perbill::from_parts(Perbill::from_percent(5).deconstruct() / 8766),
max: Perbill::from_parts(Perbill::from_percent(5).deconstruct() / 8766),
},
// annual inflation
annual,
round: to_round_inflation(annual),
}
}

Expand Down
7 changes: 5 additions & 2 deletions pallets/parachain-staking/src/inflation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use substrate_fixed::types::{I32F32, I64F64};

const SECONDS_PER_YEAR: u32 = 31557600;
const SECONDS_PER_BLOCK: u32 = 12;
const BLOCKS_PER_YEAR: u32 = SECONDS_PER_YEAR / SECONDS_PER_BLOCK;
pub const BLOCKS_PER_YEAR: u32 = SECONDS_PER_YEAR / SECONDS_PER_BLOCK;

fn rounds_per_year<T: Config>() -> u32 {
let blocks_per_round = <Pallet<T>>::round().length;
Expand Down Expand Up @@ -60,7 +60,10 @@ impl<T: Ord + Copy> From<T> for Range<T> {
}
/// Convert an annual inflation to a round inflation
/// round = 1 - (1+annual)^(1/rounds_per_year)
fn perbill_annual_to_perbill_round(annual: Range<Perbill>, rounds_per_year: u32) -> Range<Perbill> {
pub fn perbill_annual_to_perbill_round(
annual: Range<Perbill>,
rounds_per_year: u32,
) -> Range<Perbill> {
let exponent = I32F32::from_num(1) / I32F32::from_num(rounds_per_year);
let annual_to_round = |annual: Perbill| -> Perbill {
let x = I32F32::from_num(annual.deconstruct()) / I32F32::from_num(Perbill::ACCURACY);
Expand Down
2 changes: 1 addition & 1 deletion pallets/parachain-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

#[cfg(any(test, feature = "runtime-benchmarks"))]
mod benchmarks;
mod inflation;
pub mod inflation;
pub mod migrations;
#[cfg(test)]
mod mock;
Expand Down
6 changes: 3 additions & 3 deletions tests/tests/test-staking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ describeDevMoonbeam("Staking - Genesis", (context) => {
expect(inflationInfo.toHuman()["annual"]["ideal"]).to.eq("5.00%");
expect(inflationInfo.toHuman()["annual"]["max"]).to.eq("5.00%");
expect(inflationInfo.toHuman()["round"]["min"]).to.eq("0.00%");
expect(Number(inflationInfo["round"]["min"])).to.eq(4563); // 4% / 8766 * 10^9
expect(Number(inflationInfo["round"]["min"])).to.eq(8949); // 4% / blocks per year * 10^9
expect(inflationInfo.toHuman()["round"]["ideal"]).to.eq("0.00%");
expect(Number(inflationInfo["round"]["ideal"])).to.eq(5703); // 5% / 8766 * 10^9
expect(Number(inflationInfo["round"]["ideal"])).to.eq(11132); // 5% / blocks per year * 10^9
expect(inflationInfo.toHuman()["round"]["max"]).to.eq("0.00%");
expect(Number(inflationInfo["round"]["max"])).to.eq(5703); // 5% / 8766 * 10^9
expect(Number(inflationInfo["round"]["max"])).to.eq(11132); // 5% / blocks per year * 10^9
});
});

Expand Down

0 comments on commit e385285

Please sign in to comment.