From e385285a10feb406536855782e1f5992bc80657a Mon Sep 17 00:00:00 2001 From: Amar Singh Date: Thu, 2 Dec 2021 14:11:28 -0500 Subject: [PATCH] Fix genesis chain specs (#1046) * 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 --- Cargo.lock | 1 + node/service/Cargo.toml | 1 + node/service/src/chain_spec/moonbase.rs | 28 +++++++++++++--------- node/service/src/chain_spec/moonbeam.rs | 28 +++++++++++++--------- node/service/src/chain_spec/moonriver.rs | 28 +++++++++++++--------- pallets/parachain-staking/src/inflation.rs | 7 ++++-- pallets/parachain-staking/src/lib.rs | 2 +- tests/tests/test-staking.ts | 6 ++--- 8 files changed, 62 insertions(+), 39 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bbb8b3f3b9..8949f303a8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5516,6 +5516,7 @@ dependencies = [ "pallet-sudo", "pallet-transaction-payment-rpc", "pallet-transaction-payment-rpc-runtime-api", + "parachain-staking", "parity-scale-codec", "parking_lot 0.9.0", "polkadot-cli", diff --git a/node/service/Cargo.toml b/node/service/Cargo.toml index a2db03a187..e4ed8da644 100644 --- a/node/service/Cargo.toml +++ b/node/service/Cargo.toml @@ -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 } diff --git a/node/service/src/chain_spec/moonbase.rs b/node/service/src/chain_spec/moonbase.rs index c2a503f6b2..fef53efa41 100644 --- a/node/service/src/chain_spec/moonbase.rs +++ b/node/service/src/chain_spec/moonbase.rs @@ -161,23 +161,29 @@ pub fn get_chain_spec(para_id: ParaId) -> ChainSpec { } pub fn moonbeam_inflation_config() -> InflationInfo { + fn to_round_inflation(annual: Range) -> Range { + 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), } } diff --git a/node/service/src/chain_spec/moonbeam.rs b/node/service/src/chain_spec/moonbeam.rs index e1207b996d..4a47cfd7f0 100644 --- a/node/service/src/chain_spec/moonbeam.rs +++ b/node/service/src/chain_spec/moonbeam.rs @@ -144,23 +144,29 @@ pub fn get_chain_spec(para_id: ParaId) -> ChainSpec { } pub fn moonbeam_inflation_config() -> InflationInfo { + fn to_round_inflation(annual: Range) -> Range { + 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), } } diff --git a/node/service/src/chain_spec/moonriver.rs b/node/service/src/chain_spec/moonriver.rs index 748b8bc13e..8ce76f7686 100644 --- a/node/service/src/chain_spec/moonriver.rs +++ b/node/service/src/chain_spec/moonriver.rs @@ -140,23 +140,29 @@ pub fn get_chain_spec(para_id: ParaId) -> ChainSpec { } pub fn moonbeam_inflation_config() -> InflationInfo { + fn to_round_inflation(annual: Range) -> Range { + 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), } } diff --git a/pallets/parachain-staking/src/inflation.rs b/pallets/parachain-staking/src/inflation.rs index af3e7dcb94..0d1bcca8eb 100644 --- a/pallets/parachain-staking/src/inflation.rs +++ b/pallets/parachain-staking/src/inflation.rs @@ -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() -> u32 { let blocks_per_round = >::round().length; @@ -60,7 +60,10 @@ impl From for Range { } /// Convert an annual inflation to a round inflation /// round = 1 - (1+annual)^(1/rounds_per_year) -fn perbill_annual_to_perbill_round(annual: Range, rounds_per_year: u32) -> Range { +pub fn perbill_annual_to_perbill_round( + annual: Range, + rounds_per_year: u32, +) -> Range { 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); diff --git a/pallets/parachain-staking/src/lib.rs b/pallets/parachain-staking/src/lib.rs index f5e120ce38..16c2c98044 100644 --- a/pallets/parachain-staking/src/lib.rs +++ b/pallets/parachain-staking/src/lib.rs @@ -47,7 +47,7 @@ #[cfg(any(test, feature = "runtime-benchmarks"))] mod benchmarks; -mod inflation; +pub mod inflation; pub mod migrations; #[cfg(test)] mod mock; diff --git a/tests/tests/test-staking.ts b/tests/tests/test-staking.ts index c0dc333950..221da8cf39 100644 --- a/tests/tests/test-staking.ts +++ b/tests/tests/test-staking.ts @@ -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 }); });