From 5ad42105869c4e2d4fcbde197daba1877b371c44 Mon Sep 17 00:00:00 2001 From: Rakan Al-Huneiti Date: Fri, 26 Jan 2024 13:01:26 +0400 Subject: [PATCH] Chore: Move constants to runtime-common (#251) ### Description 1. Constants in both runtimes are identiical. 2. There is no need for them to have their own separate crates. 3. We introduce `constants.rs` per runtime if some runtime-specific constants need to be defined. ### Types of Changes - [x] Tech Debt (Code improvements) - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Dependency upgrade (A change in substrate or any 3rd party crate version) ### Migrations and Hooks - [ ] This change requires a runtime migration. - [ ] Modifies `on_initialize` - [ ] Modifies `on_finalize` ### Checklist - [ ] Change has been tested locally. - [ ] Change adds / updates tests. - [ ] Changelog doc updated. --- Cargo.lock | 19 +---- node/service/Cargo.toml | 4 +- node/service/src/chain_spec.rs | 4 +- runtime/cere-dev/Cargo.toml | 2 - runtime/cere-dev/constants/Cargo.toml | 15 ---- runtime/cere-dev/src/lib.rs | 2 +- runtime/cere/Cargo.toml | 2 - runtime/cere/constants/Cargo.toml | 14 ---- runtime/cere/constants/src/lib.rs | 77 ------------------- runtime/cere/src/lib.rs | 2 +- .../src/lib.rs => common/src/constants.rs} | 0 runtime/common/src/lib.rs | 2 + 12 files changed, 9 insertions(+), 134 deletions(-) delete mode 100644 runtime/cere-dev/constants/Cargo.toml delete mode 100644 runtime/cere/constants/Cargo.toml delete mode 100644 runtime/cere/constants/src/lib.rs rename runtime/{cere-dev/constants/src/lib.rs => common/src/constants.rs} (100%) diff --git a/Cargo.lock b/Cargo.lock index cd9e7b1bf..1647bdea2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -836,7 +836,6 @@ dependencies = [ name = "cere-dev-runtime" version = "4.8.8" dependencies = [ - "cere-dev-runtime-constants", "cere-runtime-common", "ddc-primitives", "frame-benchmarking", @@ -922,14 +921,6 @@ dependencies = [ "substrate-wasm-builder", ] -[[package]] -name = "cere-dev-runtime-constants" -version = "4.8.8" -dependencies = [ - "node-primitives", - "sp-runtime", -] - [[package]] name = "cere-rpc" version = "4.8.8" @@ -964,7 +955,6 @@ name = "cere-runtime" version = "4.8.8" dependencies = [ "cere-runtime-common", - "cere-runtime-constants", "ddc-primitives", "frame-benchmarking", "frame-election-provider-support", @@ -1059,22 +1049,15 @@ dependencies = [ "sp-runtime", ] -[[package]] -name = "cere-runtime-constants" -version = "4.8.8" -dependencies = [ - "node-primitives", -] - [[package]] name = "cere-service" version = "4.8.8" dependencies = [ "cere-client", "cere-dev-runtime", - "cere-dev-runtime-constants", "cere-rpc", "cere-runtime", + "cere-runtime-common", "futures", "jsonrpsee", "node-primitives", diff --git a/node/service/Cargo.toml b/node/service/Cargo.toml index 64ea4fd06..57fa76ba4 100644 --- a/node/service/Cargo.toml +++ b/node/service/Cargo.toml @@ -41,8 +41,8 @@ sp-trie = { git = "https://github.com/paritytech/substrate", branch = "polkadot- # Local cere-client = { path = "../client", default-features = false, optional = true } -cere-dev-runtime-constants = { path = "../../runtime/cere-dev/constants", optional = true } cere-rpc = { path = "../../rpc" } +cere-runtime-common = { path = "../../runtime/common", optional = true } cere-dev-runtime = { path = "../../runtime/cere-dev", optional = true } cere-runtime = { path = "../../runtime/cere", optional = true } @@ -50,7 +50,7 @@ cere-runtime = { path = "../../runtime/cere", optional = true } [features] default = ["cere-native"] cere-native = ["cere-runtime", "cere-client/cere"] -cere-dev-native = ["cere-dev-runtime", "cere-dev-runtime-constants", "cere-client/cere-dev"] +cere-dev-native = ["cere-dev-runtime", "cere-runtime-common", "cere-client/cere-dev"] runtime-benchmarks = [ "cere-runtime/runtime-benchmarks", diff --git a/node/service/src/chain_spec.rs b/node/service/src/chain_spec.rs index 4cf2fb2a8..0c7aa1156 100644 --- a/node/service/src/chain_spec.rs +++ b/node/service/src/chain_spec.rs @@ -1,9 +1,9 @@ #[cfg(feature = "cere-dev-native")] use cere_dev_runtime as cere_dev; -#[cfg(feature = "cere-dev-native")] -use cere_dev_runtime_constants::currency::DOLLARS as TEST_UNITS; #[cfg(feature = "cere-native")] use cere_runtime as cere; +#[cfg(feature = "cere-dev-native")] +use cere_runtime_common::constants::currency::DOLLARS as TEST_UNITS; use jsonrpsee::core::__reexports::serde_json; pub use node_primitives::{AccountId, Balance, Block, Signature}; use pallet_im_online::sr25519::AuthorityId as ImOnlineId; diff --git a/runtime/cere-dev/Cargo.toml b/runtime/cere-dev/Cargo.toml index 7ec5bf874..90231840a 100644 --- a/runtime/cere-dev/Cargo.toml +++ b/runtime/cere-dev/Cargo.toml @@ -92,7 +92,6 @@ pallet-utility = { default-features = false, git = "https://github.com/paritytec pallet-vesting = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.40" } # cere dependencies -cere-dev-runtime-constants = { path = "./constants", default-features = false } cere-runtime-common = { path = "../common", default-features = false } ddc-primitives = { path = "../../primitives", default-features = false } pallet-ddc-clusters = { version = "4.8.8", default-features = false, path = "../../pallets/ddc-clusters" } @@ -181,7 +180,6 @@ std = [ "pallet-ddc-nodes/std", "pallet-ddc-staking/std", "cere-runtime-common/std", - "cere-dev-runtime-constants/std", "pallet-ddc-customers/std", "pallet-ddc-clusters/std", "pallet-ddc-payouts/std", diff --git a/runtime/cere-dev/constants/Cargo.toml b/runtime/cere-dev/constants/Cargo.toml deleted file mode 100644 index 1de9ac691..000000000 --- a/runtime/cere-dev/constants/Cargo.toml +++ /dev/null @@ -1,15 +0,0 @@ -[package] -name = "cere-dev-runtime-constants" -version = "4.8.8" -authors = ["Parity Technologies "] -edition = "2021" - -[dependencies] -node-primitives = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.40" } -sp-runtime = { git = "https://github.com/paritytech/substrate", default-features = false, branch = "polkadot-v0.9.40" } - -[features] -default = ["std"] -std = [ - "node-primitives/std", -] diff --git a/runtime/cere-dev/src/lib.rs b/runtime/cere-dev/src/lib.rs index 12cce3783..56fee1227 100644 --- a/runtime/cere-dev/src/lib.rs +++ b/runtime/cere-dev/src/lib.rs @@ -99,7 +99,7 @@ use static_assertions::const_assert; /// Implementations of some helper traits passed into runtime modules as associated types. pub mod impls; /// Constant values used within the runtime. -use cere_dev_runtime_constants::{currency::*, time::*}; +use cere_runtime_common::constants::{currency::*, time::*}; use impls::Author; use sp_runtime::generic::Era; diff --git a/runtime/cere/Cargo.toml b/runtime/cere/Cargo.toml index a1d0f2d88..44866de79 100644 --- a/runtime/cere/Cargo.toml +++ b/runtime/cere/Cargo.toml @@ -93,7 +93,6 @@ pallet-vesting = { default-features = false, git = "https://github.com/paritytec # cere dependencies cere-runtime-common = { path = "../common", default-features = false } -cere-runtime-constants = { path = "./constants", default-features = false } ddc-primitives = { path = "../../primitives", default-features = false } pallet-ddc-clusters = { version = "4.8.8", default-features = false, path = "../../pallets/ddc-clusters" } pallet-ddc-customers = { version = "4.8.8", default-features = false, path = "../../pallets/ddc-customers" } @@ -182,7 +181,6 @@ std = [ "pallet-ddc-payouts/std", "pallet-ddc-staking/std", "cere-runtime-common/std", - "cere-runtime-constants/std", ] runtime-benchmarks = [ "frame-benchmarking/runtime-benchmarks", diff --git a/runtime/cere/constants/Cargo.toml b/runtime/cere/constants/Cargo.toml deleted file mode 100644 index 7b8ed708c..000000000 --- a/runtime/cere/constants/Cargo.toml +++ /dev/null @@ -1,14 +0,0 @@ -[package] -name = "cere-runtime-constants" -version = "4.8.8" -authors = ["Parity Technologies "] -edition = "2021" - -[dependencies] -node-primitives = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.40" } - -[features] -default = ["std"] -std = [ - "node-primitives/std", -] diff --git a/runtime/cere/constants/src/lib.rs b/runtime/cere/constants/src/lib.rs deleted file mode 100644 index 6da8a04ea..000000000 --- a/runtime/cere/constants/src/lib.rs +++ /dev/null @@ -1,77 +0,0 @@ -// This file is part of Substrate. - -// Copyright (C) 2019-2022 Parity Technologies (UK) Ltd. -// SPDX-License-Identifier: Apache-2.0 - -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -//! A set of constant values used in substrate runtime. - -#![cfg_attr(not(feature = "std"), no_std)] - -/// Money matters. -pub mod currency { - use node_primitives::Balance; - - pub const MILLICENTS: Balance = 100_000; - pub const CENTS: Balance = 1_000 * MILLICENTS; // assume this is worth about a cent. - pub const DOLLARS: Balance = 100 * CENTS; - - pub const fn deposit(items: u32, bytes: u32) -> Balance { - items as Balance * 15 * CENTS + (bytes as Balance) * 6 * CENTS - } -} - -/// Time. -pub mod time { - use node_primitives::{BlockNumber, Moment}; - - /// Since BABE is probabilistic this is the average expected block time that - /// we are targeting. Blocks will be produced at a minimum duration defined - /// by `SLOT_DURATION`, but some slots will not be allocated to any - /// authority and hence no block will be produced. We expect to have this - /// block time on average following the defined slot duration and the value - /// of `c` configured for BABE (where `1 - c` represents the probability of - /// a slot being empty). - /// This value is only used indirectly to define the unit constants below - /// that are expressed in blocks. The rest of the code should use - /// `SLOT_DURATION` instead (like the Timestamp pallet for calculating the - /// minimum period). - /// - /// If using BABE with secondary slots (default) then all of the slots will - /// always be assigned, in which case `MILLISECS_PER_BLOCK` and - /// `SLOT_DURATION` should have the same value. - /// - /// - pub const MILLISECS_PER_BLOCK: Moment = 6000; - pub const SECS_PER_BLOCK: Moment = MILLISECS_PER_BLOCK / 1000; - - // NOTE: Currently it is not possible to change the slot duration after the chain has started. - // Attempting to do so will brick block production. - pub const SLOT_DURATION: Moment = MILLISECS_PER_BLOCK; - - // 1 in 4 blocks (on average, not counting collisions) will be primary BABE blocks. - pub const PRIMARY_PROBABILITY: (u64, u64) = (1, 4); - - pub const EPOCH_DURATION_IN_BLOCKS: BlockNumber = 4 * HOURS; - pub const EPOCH_DURATION_IN_SLOTS: u64 = { - const SLOT_FILL_RATE: f64 = MILLISECS_PER_BLOCK as f64 / SLOT_DURATION as f64; - - (EPOCH_DURATION_IN_BLOCKS as f64 * SLOT_FILL_RATE) as u64 - }; - - // These time units are defined in number of blocks. - pub const MINUTES: BlockNumber = 60 / (SECS_PER_BLOCK as BlockNumber); - pub const HOURS: BlockNumber = MINUTES * 60; - pub const DAYS: BlockNumber = HOURS * 24; -} diff --git a/runtime/cere/src/lib.rs b/runtime/cere/src/lib.rs index 0fcfef2e2..92d4538de 100644 --- a/runtime/cere/src/lib.rs +++ b/runtime/cere/src/lib.rs @@ -94,7 +94,7 @@ use static_assertions::const_assert; /// Implementations of some helper traits passed into runtime modules as associated types. pub mod impls; /// Constant values used within the runtime. -use cere_runtime_constants::{currency::*, time::*}; +use cere_runtime_common::constants::{currency::*, time::*}; use impls::Author; use sp_runtime::generic::Era; diff --git a/runtime/cere-dev/constants/src/lib.rs b/runtime/common/src/constants.rs similarity index 100% rename from runtime/cere-dev/constants/src/lib.rs rename to runtime/common/src/constants.rs diff --git a/runtime/common/src/lib.rs b/runtime/common/src/lib.rs index 03d133233..fd38fa810 100644 --- a/runtime/common/src/lib.rs +++ b/runtime/common/src/lib.rs @@ -1,5 +1,7 @@ #![cfg_attr(not(feature = "std"), no_std)] +pub mod constants; + use node_primitives::Balance; /// Convert a balance to an unsigned 256-bit number, use in nomination pools.