From 8e14d92afb8439e09330f012d1a49b60a66021af Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Wed, 9 Mar 2022 12:16:43 +0100 Subject: [PATCH 01/18] Copy Substrate weights into each runtime Signed-off-by: Oliver Tale-Yazdi --- .../constants/src/weights/block_weights.rs | 46 ++++++++++++++ .../src/weights/extrinsic_weights.rs | 46 ++++++++++++++ runtime/kusama/constants/src/weights/mod.rs | 28 +++++++++ .../constants/src/weights/paritydb_weights.rs | 63 +++++++++++++++++++ .../constants/src/weights/rocksdb_weights.rs | 63 +++++++++++++++++++ .../constants/src/weights/block_weights.rs | 46 ++++++++++++++ .../src/weights/extrinsic_weights.rs | 46 ++++++++++++++ runtime/polkadot/constants/src/weights/mod.rs | 28 +++++++++ .../constants/src/weights/paritydb_weights.rs | 63 +++++++++++++++++++ .../constants/src/weights/rocksdb_weights.rs | 63 +++++++++++++++++++ .../constants/src/weights/block_weights.rs | 46 ++++++++++++++ .../src/weights/extrinsic_weights.rs | 46 ++++++++++++++ runtime/rococo/constants/src/weights/mod.rs | 28 +++++++++ .../constants/src/weights/paritydb_weights.rs | 63 +++++++++++++++++++ .../constants/src/weights/rocksdb_weights.rs | 63 +++++++++++++++++++ .../constants/src/weights/block_weights.rs | 46 ++++++++++++++ .../src/weights/extrinsic_weights.rs | 46 ++++++++++++++ runtime/westend/constants/src/weights/mod.rs | 28 +++++++++ .../constants/src/weights/paritydb_weights.rs | 63 +++++++++++++++++++ .../constants/src/weights/rocksdb_weights.rs | 63 +++++++++++++++++++ 20 files changed, 984 insertions(+) create mode 100644 runtime/kusama/constants/src/weights/block_weights.rs create mode 100644 runtime/kusama/constants/src/weights/extrinsic_weights.rs create mode 100644 runtime/kusama/constants/src/weights/mod.rs create mode 100644 runtime/kusama/constants/src/weights/paritydb_weights.rs create mode 100644 runtime/kusama/constants/src/weights/rocksdb_weights.rs create mode 100644 runtime/polkadot/constants/src/weights/block_weights.rs create mode 100644 runtime/polkadot/constants/src/weights/extrinsic_weights.rs create mode 100644 runtime/polkadot/constants/src/weights/mod.rs create mode 100644 runtime/polkadot/constants/src/weights/paritydb_weights.rs create mode 100644 runtime/polkadot/constants/src/weights/rocksdb_weights.rs create mode 100644 runtime/rococo/constants/src/weights/block_weights.rs create mode 100644 runtime/rococo/constants/src/weights/extrinsic_weights.rs create mode 100644 runtime/rococo/constants/src/weights/mod.rs create mode 100644 runtime/rococo/constants/src/weights/paritydb_weights.rs create mode 100644 runtime/rococo/constants/src/weights/rocksdb_weights.rs create mode 100644 runtime/westend/constants/src/weights/block_weights.rs create mode 100644 runtime/westend/constants/src/weights/extrinsic_weights.rs create mode 100644 runtime/westend/constants/src/weights/mod.rs create mode 100644 runtime/westend/constants/src/weights/paritydb_weights.rs create mode 100644 runtime/westend/constants/src/weights/rocksdb_weights.rs diff --git a/runtime/kusama/constants/src/weights/block_weights.rs b/runtime/kusama/constants/src/weights/block_weights.rs new file mode 100644 index 000000000000..4db90f0c0207 --- /dev/null +++ b/runtime/kusama/constants/src/weights/block_weights.rs @@ -0,0 +1,46 @@ +// This file is part of Substrate. + +// Copyright (C) 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. + +pub mod constants { + use frame_support::{ + parameter_types, + weights::{constants, Weight}, + }; + + parameter_types! { + /// Importing a block with 0 Extrinsics. + pub const BlockExecutionWeight: Weight = 5_000_000 * constants::WEIGHT_PER_NANOS; + } + + #[cfg(test)] + mod test_weights { + use frame_support::weights::constants; + + /// Checks that the weight exists and is sane. + // NOTE: If this test fails but you are sure that the generated values are fine, + // you can delete it. + #[test] + fn sane() { + let w = super::constants::BlockExecutionWeight::get(); + + // At least 100 µs. + assert!(w >= 100 * constants::WEIGHT_PER_MICROS, "Weight should be at least 100 µs."); + // At most 50 ms. + assert!(w <= 50 * constants::WEIGHT_PER_MILLIS, "Weight should be at most 50 ms."); + } + } +} diff --git a/runtime/kusama/constants/src/weights/extrinsic_weights.rs b/runtime/kusama/constants/src/weights/extrinsic_weights.rs new file mode 100644 index 000000000000..158ba99c6a4c --- /dev/null +++ b/runtime/kusama/constants/src/weights/extrinsic_weights.rs @@ -0,0 +1,46 @@ +// This file is part of Substrate. + +// Copyright (C) 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. + +pub mod constants { + use frame_support::{ + parameter_types, + weights::{constants, Weight}, + }; + + parameter_types! { + /// Executing a NO-OP `System::remarks` Extrinsic. + pub const ExtrinsicBaseWeight: Weight = 125_000 * constants::WEIGHT_PER_NANOS; + } + + #[cfg(test)] + mod test_weights { + use frame_support::weights::constants; + + /// Checks that the weight exists and is sane. + // NOTE: If this test fails but you are sure that the generated values are fine, + // you can delete it. + #[test] + fn sane() { + let w = super::constants::ExtrinsicBaseWeight::get(); + + // At least 10 µs. + assert!(w >= 10 * constants::WEIGHT_PER_MICROS, "Weight should be at least 10 µs."); + // At most 1 ms. + assert!(w <= constants::WEIGHT_PER_MILLIS, "Weight should be at most 1 ms."); + } + } +} diff --git a/runtime/kusama/constants/src/weights/mod.rs b/runtime/kusama/constants/src/weights/mod.rs new file mode 100644 index 000000000000..ed0b4dbcd47f --- /dev/null +++ b/runtime/kusama/constants/src/weights/mod.rs @@ -0,0 +1,28 @@ +// This file is part of Substrate. + +// Copyright (C) 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. + +//! Expose the auto generated weight files. + +pub mod block_weights; +pub mod extrinsic_weights; +pub mod paritydb_weights; +pub mod rocksdb_weights; + +pub use block_weights::constants::BlockExecutionWeight; +pub use extrinsic_weights::constants::ExtrinsicBaseWeight; +pub use paritydb_weights::constants::ParityDbWeight; +pub use rocksdb_weights::constants::RocksDbWeight; diff --git a/runtime/kusama/constants/src/weights/paritydb_weights.rs b/runtime/kusama/constants/src/weights/paritydb_weights.rs new file mode 100644 index 000000000000..572187ba78a9 --- /dev/null +++ b/runtime/kusama/constants/src/weights/paritydb_weights.rs @@ -0,0 +1,63 @@ +// This file is part of Substrate. + +// Copyright (C) 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. + +pub mod constants { + use frame_support::{ + parameter_types, + weights::{constants, RuntimeDbWeight}, + }; + + parameter_types! { + /// ParityDB can be enabled with a feature flag, but is still experimental. These weights + /// are available for brave runtime engineers who may want to try this out as default. + pub const ParityDbWeight: RuntimeDbWeight = RuntimeDbWeight { + read: 8_000 * constants::WEIGHT_PER_NANOS, + write: 50_000 * constants::WEIGHT_PER_NANOS, + }; + } + + #[cfg(test)] + mod test_db_weights { + use super::constants::ParityDbWeight as W; + use frame_support::weights::constants; + + /// Checks that all weights exist and have sane values. + // NOTE: If this test fails but you are sure that the generated values are fine, + // you can delete it. + #[test] + fn sane() { + // At least 1 µs. + assert!( + W::get().reads(1) >= constants::WEIGHT_PER_MICROS, + "Read weight should be at least 1 µs." + ); + assert!( + W::get().writes(1) >= constants::WEIGHT_PER_MICROS, + "Write weight should be at least 1 µs." + ); + // At most 1 ms. + assert!( + W::get().reads(1) <= constants::WEIGHT_PER_MILLIS, + "Read weight should be at most 1 ms." + ); + assert!( + W::get().writes(1) <= constants::WEIGHT_PER_MILLIS, + "Write weight should be at most 1 ms." + ); + } + } +} diff --git a/runtime/kusama/constants/src/weights/rocksdb_weights.rs b/runtime/kusama/constants/src/weights/rocksdb_weights.rs new file mode 100644 index 000000000000..f37964dcbd82 --- /dev/null +++ b/runtime/kusama/constants/src/weights/rocksdb_weights.rs @@ -0,0 +1,63 @@ +// This file is part of Substrate. + +// Copyright (C) 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. + +pub mod constants { + use frame_support::{ + parameter_types, + weights::{constants, RuntimeDbWeight}, + }; + + parameter_types! { + /// By default, Substrate uses RocksDB, so this will be the weight used throughout + /// the runtime. + pub const RocksDbWeight: RuntimeDbWeight = RuntimeDbWeight { + read: 25_000 * constants::WEIGHT_PER_NANOS, + write: 100_000 * constants::WEIGHT_PER_NANOS, + }; + } + + #[cfg(test)] + mod test_db_weights { + use super::constants::RocksDbWeight as W; + use frame_support::weights::constants; + + /// Checks that all weights exist and have sane values. + // NOTE: If this test fails but you are sure that the generated values are fine, + // you can delete it. + #[test] + fn sane() { + // At least 1 µs. + assert!( + W::get().reads(1) >= constants::WEIGHT_PER_MICROS, + "Read weight should be at least 1 µs." + ); + assert!( + W::get().writes(1) >= constants::WEIGHT_PER_MICROS, + "Write weight should be at least 1 µs." + ); + // At most 1 ms. + assert!( + W::get().reads(1) <= constants::WEIGHT_PER_MILLIS, + "Read weight should be at most 1 ms." + ); + assert!( + W::get().writes(1) <= constants::WEIGHT_PER_MILLIS, + "Write weight should be at most 1 ms." + ); + } + } +} diff --git a/runtime/polkadot/constants/src/weights/block_weights.rs b/runtime/polkadot/constants/src/weights/block_weights.rs new file mode 100644 index 000000000000..4db90f0c0207 --- /dev/null +++ b/runtime/polkadot/constants/src/weights/block_weights.rs @@ -0,0 +1,46 @@ +// This file is part of Substrate. + +// Copyright (C) 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. + +pub mod constants { + use frame_support::{ + parameter_types, + weights::{constants, Weight}, + }; + + parameter_types! { + /// Importing a block with 0 Extrinsics. + pub const BlockExecutionWeight: Weight = 5_000_000 * constants::WEIGHT_PER_NANOS; + } + + #[cfg(test)] + mod test_weights { + use frame_support::weights::constants; + + /// Checks that the weight exists and is sane. + // NOTE: If this test fails but you are sure that the generated values are fine, + // you can delete it. + #[test] + fn sane() { + let w = super::constants::BlockExecutionWeight::get(); + + // At least 100 µs. + assert!(w >= 100 * constants::WEIGHT_PER_MICROS, "Weight should be at least 100 µs."); + // At most 50 ms. + assert!(w <= 50 * constants::WEIGHT_PER_MILLIS, "Weight should be at most 50 ms."); + } + } +} diff --git a/runtime/polkadot/constants/src/weights/extrinsic_weights.rs b/runtime/polkadot/constants/src/weights/extrinsic_weights.rs new file mode 100644 index 000000000000..158ba99c6a4c --- /dev/null +++ b/runtime/polkadot/constants/src/weights/extrinsic_weights.rs @@ -0,0 +1,46 @@ +// This file is part of Substrate. + +// Copyright (C) 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. + +pub mod constants { + use frame_support::{ + parameter_types, + weights::{constants, Weight}, + }; + + parameter_types! { + /// Executing a NO-OP `System::remarks` Extrinsic. + pub const ExtrinsicBaseWeight: Weight = 125_000 * constants::WEIGHT_PER_NANOS; + } + + #[cfg(test)] + mod test_weights { + use frame_support::weights::constants; + + /// Checks that the weight exists and is sane. + // NOTE: If this test fails but you are sure that the generated values are fine, + // you can delete it. + #[test] + fn sane() { + let w = super::constants::ExtrinsicBaseWeight::get(); + + // At least 10 µs. + assert!(w >= 10 * constants::WEIGHT_PER_MICROS, "Weight should be at least 10 µs."); + // At most 1 ms. + assert!(w <= constants::WEIGHT_PER_MILLIS, "Weight should be at most 1 ms."); + } + } +} diff --git a/runtime/polkadot/constants/src/weights/mod.rs b/runtime/polkadot/constants/src/weights/mod.rs new file mode 100644 index 000000000000..ed0b4dbcd47f --- /dev/null +++ b/runtime/polkadot/constants/src/weights/mod.rs @@ -0,0 +1,28 @@ +// This file is part of Substrate. + +// Copyright (C) 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. + +//! Expose the auto generated weight files. + +pub mod block_weights; +pub mod extrinsic_weights; +pub mod paritydb_weights; +pub mod rocksdb_weights; + +pub use block_weights::constants::BlockExecutionWeight; +pub use extrinsic_weights::constants::ExtrinsicBaseWeight; +pub use paritydb_weights::constants::ParityDbWeight; +pub use rocksdb_weights::constants::RocksDbWeight; diff --git a/runtime/polkadot/constants/src/weights/paritydb_weights.rs b/runtime/polkadot/constants/src/weights/paritydb_weights.rs new file mode 100644 index 000000000000..572187ba78a9 --- /dev/null +++ b/runtime/polkadot/constants/src/weights/paritydb_weights.rs @@ -0,0 +1,63 @@ +// This file is part of Substrate. + +// Copyright (C) 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. + +pub mod constants { + use frame_support::{ + parameter_types, + weights::{constants, RuntimeDbWeight}, + }; + + parameter_types! { + /// ParityDB can be enabled with a feature flag, but is still experimental. These weights + /// are available for brave runtime engineers who may want to try this out as default. + pub const ParityDbWeight: RuntimeDbWeight = RuntimeDbWeight { + read: 8_000 * constants::WEIGHT_PER_NANOS, + write: 50_000 * constants::WEIGHT_PER_NANOS, + }; + } + + #[cfg(test)] + mod test_db_weights { + use super::constants::ParityDbWeight as W; + use frame_support::weights::constants; + + /// Checks that all weights exist and have sane values. + // NOTE: If this test fails but you are sure that the generated values are fine, + // you can delete it. + #[test] + fn sane() { + // At least 1 µs. + assert!( + W::get().reads(1) >= constants::WEIGHT_PER_MICROS, + "Read weight should be at least 1 µs." + ); + assert!( + W::get().writes(1) >= constants::WEIGHT_PER_MICROS, + "Write weight should be at least 1 µs." + ); + // At most 1 ms. + assert!( + W::get().reads(1) <= constants::WEIGHT_PER_MILLIS, + "Read weight should be at most 1 ms." + ); + assert!( + W::get().writes(1) <= constants::WEIGHT_PER_MILLIS, + "Write weight should be at most 1 ms." + ); + } + } +} diff --git a/runtime/polkadot/constants/src/weights/rocksdb_weights.rs b/runtime/polkadot/constants/src/weights/rocksdb_weights.rs new file mode 100644 index 000000000000..f37964dcbd82 --- /dev/null +++ b/runtime/polkadot/constants/src/weights/rocksdb_weights.rs @@ -0,0 +1,63 @@ +// This file is part of Substrate. + +// Copyright (C) 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. + +pub mod constants { + use frame_support::{ + parameter_types, + weights::{constants, RuntimeDbWeight}, + }; + + parameter_types! { + /// By default, Substrate uses RocksDB, so this will be the weight used throughout + /// the runtime. + pub const RocksDbWeight: RuntimeDbWeight = RuntimeDbWeight { + read: 25_000 * constants::WEIGHT_PER_NANOS, + write: 100_000 * constants::WEIGHT_PER_NANOS, + }; + } + + #[cfg(test)] + mod test_db_weights { + use super::constants::RocksDbWeight as W; + use frame_support::weights::constants; + + /// Checks that all weights exist and have sane values. + // NOTE: If this test fails but you are sure that the generated values are fine, + // you can delete it. + #[test] + fn sane() { + // At least 1 µs. + assert!( + W::get().reads(1) >= constants::WEIGHT_PER_MICROS, + "Read weight should be at least 1 µs." + ); + assert!( + W::get().writes(1) >= constants::WEIGHT_PER_MICROS, + "Write weight should be at least 1 µs." + ); + // At most 1 ms. + assert!( + W::get().reads(1) <= constants::WEIGHT_PER_MILLIS, + "Read weight should be at most 1 ms." + ); + assert!( + W::get().writes(1) <= constants::WEIGHT_PER_MILLIS, + "Write weight should be at most 1 ms." + ); + } + } +} diff --git a/runtime/rococo/constants/src/weights/block_weights.rs b/runtime/rococo/constants/src/weights/block_weights.rs new file mode 100644 index 000000000000..4db90f0c0207 --- /dev/null +++ b/runtime/rococo/constants/src/weights/block_weights.rs @@ -0,0 +1,46 @@ +// This file is part of Substrate. + +// Copyright (C) 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. + +pub mod constants { + use frame_support::{ + parameter_types, + weights::{constants, Weight}, + }; + + parameter_types! { + /// Importing a block with 0 Extrinsics. + pub const BlockExecutionWeight: Weight = 5_000_000 * constants::WEIGHT_PER_NANOS; + } + + #[cfg(test)] + mod test_weights { + use frame_support::weights::constants; + + /// Checks that the weight exists and is sane. + // NOTE: If this test fails but you are sure that the generated values are fine, + // you can delete it. + #[test] + fn sane() { + let w = super::constants::BlockExecutionWeight::get(); + + // At least 100 µs. + assert!(w >= 100 * constants::WEIGHT_PER_MICROS, "Weight should be at least 100 µs."); + // At most 50 ms. + assert!(w <= 50 * constants::WEIGHT_PER_MILLIS, "Weight should be at most 50 ms."); + } + } +} diff --git a/runtime/rococo/constants/src/weights/extrinsic_weights.rs b/runtime/rococo/constants/src/weights/extrinsic_weights.rs new file mode 100644 index 000000000000..158ba99c6a4c --- /dev/null +++ b/runtime/rococo/constants/src/weights/extrinsic_weights.rs @@ -0,0 +1,46 @@ +// This file is part of Substrate. + +// Copyright (C) 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. + +pub mod constants { + use frame_support::{ + parameter_types, + weights::{constants, Weight}, + }; + + parameter_types! { + /// Executing a NO-OP `System::remarks` Extrinsic. + pub const ExtrinsicBaseWeight: Weight = 125_000 * constants::WEIGHT_PER_NANOS; + } + + #[cfg(test)] + mod test_weights { + use frame_support::weights::constants; + + /// Checks that the weight exists and is sane. + // NOTE: If this test fails but you are sure that the generated values are fine, + // you can delete it. + #[test] + fn sane() { + let w = super::constants::ExtrinsicBaseWeight::get(); + + // At least 10 µs. + assert!(w >= 10 * constants::WEIGHT_PER_MICROS, "Weight should be at least 10 µs."); + // At most 1 ms. + assert!(w <= constants::WEIGHT_PER_MILLIS, "Weight should be at most 1 ms."); + } + } +} diff --git a/runtime/rococo/constants/src/weights/mod.rs b/runtime/rococo/constants/src/weights/mod.rs new file mode 100644 index 000000000000..ed0b4dbcd47f --- /dev/null +++ b/runtime/rococo/constants/src/weights/mod.rs @@ -0,0 +1,28 @@ +// This file is part of Substrate. + +// Copyright (C) 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. + +//! Expose the auto generated weight files. + +pub mod block_weights; +pub mod extrinsic_weights; +pub mod paritydb_weights; +pub mod rocksdb_weights; + +pub use block_weights::constants::BlockExecutionWeight; +pub use extrinsic_weights::constants::ExtrinsicBaseWeight; +pub use paritydb_weights::constants::ParityDbWeight; +pub use rocksdb_weights::constants::RocksDbWeight; diff --git a/runtime/rococo/constants/src/weights/paritydb_weights.rs b/runtime/rococo/constants/src/weights/paritydb_weights.rs new file mode 100644 index 000000000000..572187ba78a9 --- /dev/null +++ b/runtime/rococo/constants/src/weights/paritydb_weights.rs @@ -0,0 +1,63 @@ +// This file is part of Substrate. + +// Copyright (C) 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. + +pub mod constants { + use frame_support::{ + parameter_types, + weights::{constants, RuntimeDbWeight}, + }; + + parameter_types! { + /// ParityDB can be enabled with a feature flag, but is still experimental. These weights + /// are available for brave runtime engineers who may want to try this out as default. + pub const ParityDbWeight: RuntimeDbWeight = RuntimeDbWeight { + read: 8_000 * constants::WEIGHT_PER_NANOS, + write: 50_000 * constants::WEIGHT_PER_NANOS, + }; + } + + #[cfg(test)] + mod test_db_weights { + use super::constants::ParityDbWeight as W; + use frame_support::weights::constants; + + /// Checks that all weights exist and have sane values. + // NOTE: If this test fails but you are sure that the generated values are fine, + // you can delete it. + #[test] + fn sane() { + // At least 1 µs. + assert!( + W::get().reads(1) >= constants::WEIGHT_PER_MICROS, + "Read weight should be at least 1 µs." + ); + assert!( + W::get().writes(1) >= constants::WEIGHT_PER_MICROS, + "Write weight should be at least 1 µs." + ); + // At most 1 ms. + assert!( + W::get().reads(1) <= constants::WEIGHT_PER_MILLIS, + "Read weight should be at most 1 ms." + ); + assert!( + W::get().writes(1) <= constants::WEIGHT_PER_MILLIS, + "Write weight should be at most 1 ms." + ); + } + } +} diff --git a/runtime/rococo/constants/src/weights/rocksdb_weights.rs b/runtime/rococo/constants/src/weights/rocksdb_weights.rs new file mode 100644 index 000000000000..f37964dcbd82 --- /dev/null +++ b/runtime/rococo/constants/src/weights/rocksdb_weights.rs @@ -0,0 +1,63 @@ +// This file is part of Substrate. + +// Copyright (C) 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. + +pub mod constants { + use frame_support::{ + parameter_types, + weights::{constants, RuntimeDbWeight}, + }; + + parameter_types! { + /// By default, Substrate uses RocksDB, so this will be the weight used throughout + /// the runtime. + pub const RocksDbWeight: RuntimeDbWeight = RuntimeDbWeight { + read: 25_000 * constants::WEIGHT_PER_NANOS, + write: 100_000 * constants::WEIGHT_PER_NANOS, + }; + } + + #[cfg(test)] + mod test_db_weights { + use super::constants::RocksDbWeight as W; + use frame_support::weights::constants; + + /// Checks that all weights exist and have sane values. + // NOTE: If this test fails but you are sure that the generated values are fine, + // you can delete it. + #[test] + fn sane() { + // At least 1 µs. + assert!( + W::get().reads(1) >= constants::WEIGHT_PER_MICROS, + "Read weight should be at least 1 µs." + ); + assert!( + W::get().writes(1) >= constants::WEIGHT_PER_MICROS, + "Write weight should be at least 1 µs." + ); + // At most 1 ms. + assert!( + W::get().reads(1) <= constants::WEIGHT_PER_MILLIS, + "Read weight should be at most 1 ms." + ); + assert!( + W::get().writes(1) <= constants::WEIGHT_PER_MILLIS, + "Write weight should be at most 1 ms." + ); + } + } +} diff --git a/runtime/westend/constants/src/weights/block_weights.rs b/runtime/westend/constants/src/weights/block_weights.rs new file mode 100644 index 000000000000..4db90f0c0207 --- /dev/null +++ b/runtime/westend/constants/src/weights/block_weights.rs @@ -0,0 +1,46 @@ +// This file is part of Substrate. + +// Copyright (C) 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. + +pub mod constants { + use frame_support::{ + parameter_types, + weights::{constants, Weight}, + }; + + parameter_types! { + /// Importing a block with 0 Extrinsics. + pub const BlockExecutionWeight: Weight = 5_000_000 * constants::WEIGHT_PER_NANOS; + } + + #[cfg(test)] + mod test_weights { + use frame_support::weights::constants; + + /// Checks that the weight exists and is sane. + // NOTE: If this test fails but you are sure that the generated values are fine, + // you can delete it. + #[test] + fn sane() { + let w = super::constants::BlockExecutionWeight::get(); + + // At least 100 µs. + assert!(w >= 100 * constants::WEIGHT_PER_MICROS, "Weight should be at least 100 µs."); + // At most 50 ms. + assert!(w <= 50 * constants::WEIGHT_PER_MILLIS, "Weight should be at most 50 ms."); + } + } +} diff --git a/runtime/westend/constants/src/weights/extrinsic_weights.rs b/runtime/westend/constants/src/weights/extrinsic_weights.rs new file mode 100644 index 000000000000..158ba99c6a4c --- /dev/null +++ b/runtime/westend/constants/src/weights/extrinsic_weights.rs @@ -0,0 +1,46 @@ +// This file is part of Substrate. + +// Copyright (C) 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. + +pub mod constants { + use frame_support::{ + parameter_types, + weights::{constants, Weight}, + }; + + parameter_types! { + /// Executing a NO-OP `System::remarks` Extrinsic. + pub const ExtrinsicBaseWeight: Weight = 125_000 * constants::WEIGHT_PER_NANOS; + } + + #[cfg(test)] + mod test_weights { + use frame_support::weights::constants; + + /// Checks that the weight exists and is sane. + // NOTE: If this test fails but you are sure that the generated values are fine, + // you can delete it. + #[test] + fn sane() { + let w = super::constants::ExtrinsicBaseWeight::get(); + + // At least 10 µs. + assert!(w >= 10 * constants::WEIGHT_PER_MICROS, "Weight should be at least 10 µs."); + // At most 1 ms. + assert!(w <= constants::WEIGHT_PER_MILLIS, "Weight should be at most 1 ms."); + } + } +} diff --git a/runtime/westend/constants/src/weights/mod.rs b/runtime/westend/constants/src/weights/mod.rs new file mode 100644 index 000000000000..ed0b4dbcd47f --- /dev/null +++ b/runtime/westend/constants/src/weights/mod.rs @@ -0,0 +1,28 @@ +// This file is part of Substrate. + +// Copyright (C) 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. + +//! Expose the auto generated weight files. + +pub mod block_weights; +pub mod extrinsic_weights; +pub mod paritydb_weights; +pub mod rocksdb_weights; + +pub use block_weights::constants::BlockExecutionWeight; +pub use extrinsic_weights::constants::ExtrinsicBaseWeight; +pub use paritydb_weights::constants::ParityDbWeight; +pub use rocksdb_weights::constants::RocksDbWeight; diff --git a/runtime/westend/constants/src/weights/paritydb_weights.rs b/runtime/westend/constants/src/weights/paritydb_weights.rs new file mode 100644 index 000000000000..572187ba78a9 --- /dev/null +++ b/runtime/westend/constants/src/weights/paritydb_weights.rs @@ -0,0 +1,63 @@ +// This file is part of Substrate. + +// Copyright (C) 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. + +pub mod constants { + use frame_support::{ + parameter_types, + weights::{constants, RuntimeDbWeight}, + }; + + parameter_types! { + /// ParityDB can be enabled with a feature flag, but is still experimental. These weights + /// are available for brave runtime engineers who may want to try this out as default. + pub const ParityDbWeight: RuntimeDbWeight = RuntimeDbWeight { + read: 8_000 * constants::WEIGHT_PER_NANOS, + write: 50_000 * constants::WEIGHT_PER_NANOS, + }; + } + + #[cfg(test)] + mod test_db_weights { + use super::constants::ParityDbWeight as W; + use frame_support::weights::constants; + + /// Checks that all weights exist and have sane values. + // NOTE: If this test fails but you are sure that the generated values are fine, + // you can delete it. + #[test] + fn sane() { + // At least 1 µs. + assert!( + W::get().reads(1) >= constants::WEIGHT_PER_MICROS, + "Read weight should be at least 1 µs." + ); + assert!( + W::get().writes(1) >= constants::WEIGHT_PER_MICROS, + "Write weight should be at least 1 µs." + ); + // At most 1 ms. + assert!( + W::get().reads(1) <= constants::WEIGHT_PER_MILLIS, + "Read weight should be at most 1 ms." + ); + assert!( + W::get().writes(1) <= constants::WEIGHT_PER_MILLIS, + "Write weight should be at most 1 ms." + ); + } + } +} diff --git a/runtime/westend/constants/src/weights/rocksdb_weights.rs b/runtime/westend/constants/src/weights/rocksdb_weights.rs new file mode 100644 index 000000000000..f37964dcbd82 --- /dev/null +++ b/runtime/westend/constants/src/weights/rocksdb_weights.rs @@ -0,0 +1,63 @@ +// This file is part of Substrate. + +// Copyright (C) 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. + +pub mod constants { + use frame_support::{ + parameter_types, + weights::{constants, RuntimeDbWeight}, + }; + + parameter_types! { + /// By default, Substrate uses RocksDB, so this will be the weight used throughout + /// the runtime. + pub const RocksDbWeight: RuntimeDbWeight = RuntimeDbWeight { + read: 25_000 * constants::WEIGHT_PER_NANOS, + write: 100_000 * constants::WEIGHT_PER_NANOS, + }; + } + + #[cfg(test)] + mod test_db_weights { + use super::constants::RocksDbWeight as W; + use frame_support::weights::constants; + + /// Checks that all weights exist and have sane values. + // NOTE: If this test fails but you are sure that the generated values are fine, + // you can delete it. + #[test] + fn sane() { + // At least 1 µs. + assert!( + W::get().reads(1) >= constants::WEIGHT_PER_MICROS, + "Read weight should be at least 1 µs." + ); + assert!( + W::get().writes(1) >= constants::WEIGHT_PER_MICROS, + "Write weight should be at least 1 µs." + ); + // At most 1 ms. + assert!( + W::get().reads(1) <= constants::WEIGHT_PER_MILLIS, + "Read weight should be at most 1 ms." + ); + assert!( + W::get().writes(1) <= constants::WEIGHT_PER_MILLIS, + "Write weight should be at most 1 ms." + ); + } + } +} From edf017e3c1cd58d9785fef61b3c14f85a8151c85 Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Wed, 9 Mar 2022 12:12:18 +0100 Subject: [PATCH 02/18] Define weights per runtime Signed-off-by: Oliver Tale-Yazdi --- runtime/common/src/elections.rs | 54 ++++++------ runtime/common/src/lib.rs | 118 +++++++++++++++----------- runtime/kusama/constants/src/lib.rs | 5 +- runtime/kusama/src/lib.rs | 11 ++- runtime/polkadot/constants/src/lib.rs | 4 +- runtime/polkadot/src/lib.rs | 10 +-- runtime/rococo/constants/src/lib.rs | 4 +- runtime/rococo/src/lib.rs | 8 +- runtime/westend/constants/src/lib.rs | 4 +- runtime/westend/src/lib.rs | 10 +-- 10 files changed, 128 insertions(+), 100 deletions(-) diff --git a/runtime/common/src/elections.rs b/runtime/common/src/elections.rs index 87a0a318eb9d..f94b8ccf869c 100644 --- a/runtime/common/src/elections.rs +++ b/runtime/common/src/elections.rs @@ -16,33 +16,35 @@ //! Code for elections. -use super::{BlockExecutionWeight, BlockLength, BlockWeights}; use frame_election_provider_support::{SortedListProvider, VoteWeight}; -use frame_support::{ - parameter_types, - weights::{DispatchClass, Weight}, -}; -use sp_runtime::Perbill; -use sp_std::{boxed::Box, convert::From, marker::PhantomData}; - -parameter_types! { - /// A limit for off-chain phragmen unsigned solution submission. - /// - /// We want to keep it as high as possible, but can't risk having it reject, - /// so we always subtract the base block execution weight. - pub OffchainSolutionWeightLimit: Weight = BlockWeights::get() - .get(DispatchClass::Normal) - .max_extrinsic - .expect("Normal extrinsics have weight limit configured by default; qed") - .saturating_sub(BlockExecutionWeight::get()); - - /// A limit for off-chain phragmen unsigned solution length. - /// - /// We allow up to 90% of the block's size to be consumed by the solution. - pub OffchainSolutionLengthLimit: u32 = Perbill::from_rational(90_u32, 100) * - *BlockLength::get() - .max - .get(DispatchClass::Normal); +use sp_std::{boxed::Box, marker::PhantomData}; + +/// Implements the weight types for the elections module and a specific +/// runtime. +/// This macro should not be called directly; use [`impl_runtime_weights`] instead. +#[macro_export] +macro_rules! impl_elections_weights { + ($runtime:ident) => { + parameter_types! { + /// A limit for off-chain phragmen unsigned solution submission. + /// + /// We want to keep it as high as possible, but can't risk having it reject, + /// so we always subtract the base block execution weight. + pub OffchainSolutionWeightLimit: Weight = BlockWeights::get() + .get(DispatchClass::Normal) + .max_extrinsic + .expect("Normal extrinsics have weight limit configured by default; qed") + .saturating_sub($runtime::weights::BlockExecutionWeight::get()); + + /// A limit for off-chain phragmen unsigned solution length. + /// + /// We allow up to 90% of the block's size to be consumed by the solution. + pub OffchainSolutionLengthLimit: u32 = Perbill::from_rational(90_u32, 100) * + *BlockLength::get() + .max + .get(DispatchClass::Normal); + } + }; } /// The numbers configured here could always be more than the the maximum limits of staking pallet diff --git a/runtime/common/src/lib.rs b/runtime/common/src/lib.rs index 2170ba1c72e6..6880f612f805 100644 --- a/runtime/common/src/lib.rs +++ b/runtime/common/src/lib.rs @@ -37,21 +37,14 @@ mod integration_tests; #[cfg(test)] mod mock; -pub use frame_support::weights::constants::{ - BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight, -}; use frame_support::{ - parameter_types, traits::{ConstU32, Currency, OneSessionHandler}, - weights::{constants::WEIGHT_PER_SECOND, DispatchClass, Weight}, + weights::{constants::WEIGHT_PER_SECOND, Weight}, }; -use frame_system::limits; -use pallet_transaction_payment::{Multiplier, TargetedFeeAdjustment}; -use primitives::v1::{AssignmentId, BlockNumber, ValidatorId}; -use sp_runtime::{FixedPointNumber, Perbill, Perquintill}; +use primitives::v1::{AssignmentId, ValidatorId}; +use sp_runtime::Perbill; use static_assertions::const_assert; -pub use elections::{OffchainSolutionLengthLimit, OffchainSolutionWeightLimit}; pub use pallet_balances::Call as BalancesCall; #[cfg(feature = "std")] pub use pallet_staking::StakerStatus; @@ -71,53 +64,76 @@ pub type NegativeImbalance = as Currency< pub const AVERAGE_ON_INITIALIZE_RATIO: Perbill = Perbill::from_percent(1); /// We allow `Normal` extrinsics to fill up the block up to 75%, the rest can be used /// by Operational extrinsics. -const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75); +pub const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75); /// We allow for 2 seconds of compute with a 6 second average block time. pub const MAXIMUM_BLOCK_WEIGHT: Weight = 2 * WEIGHT_PER_SECOND; const_assert!(NORMAL_DISPATCH_RATIO.deconstruct() >= AVERAGE_ON_INITIALIZE_RATIO.deconstruct()); -// Common constants used in all runtimes. -parameter_types! { - pub const BlockHashCount: BlockNumber = 2400; - /// The portion of the `NORMAL_DISPATCH_RATIO` that we adjust the fees with. Blocks filled less - /// than this will decrease the weight and more will increase. - pub const TargetBlockFullness: Perquintill = Perquintill::from_percent(25); - /// The adjustment variable of the runtime. Higher values will cause `TargetBlockFullness` to - /// change the fees more rapidly. - pub AdjustmentVariable: Multiplier = Multiplier::saturating_from_rational(3, 100_000); - /// Minimum amount of the multiplier. This value cannot be too low. A test case should ensure - /// that combined with `AdjustmentVariable`, we can recover from the minimum. - /// See `multiplier_can_grow_from_zero`. - pub MinimumMultiplier: Multiplier = Multiplier::saturating_from_rational(1, 1_000_000u128); - /// Maximum length of block. Up to 5MB. - pub BlockLength: limits::BlockLength = - limits::BlockLength::max_with_normal_ratio(5 * 1024 * 1024, NORMAL_DISPATCH_RATIO); - /// Block weights base values and limits. - pub BlockWeights: limits::BlockWeights = limits::BlockWeights::builder() - .base_block(BlockExecutionWeight::get()) - .for_class(DispatchClass::all(), |weights| { - weights.base_extrinsic = ExtrinsicBaseWeight::get(); - }) - .for_class(DispatchClass::Normal, |weights| { - weights.max_total = Some(NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT); - }) - .for_class(DispatchClass::Operational, |weights| { - weights.max_total = Some(MAXIMUM_BLOCK_WEIGHT); - // Operational transactions have an extra reserved space, so that they - // are included even if block reached `MAXIMUM_BLOCK_WEIGHT`. - weights.reserved = Some( - MAXIMUM_BLOCK_WEIGHT - NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT, - ); - }) - .avg_block_initialization(AVERAGE_ON_INITIALIZE_RATIO) - .build_or_panic(); -} +/// Implements the weight types for a runtime. +/// It expects the passed runtime constants to contain a `weights` module. +/// The generated weight types were formerly part of the common +/// runtime but are now runtime dependant. +/// +/// Example: +/// impl_runtime_weights!(polkadot_runtime_constants); +#[macro_export] +macro_rules! impl_runtime_weights { + ($runtime:ident) => { + use frame_support::weights::{DispatchClass, Weight}; + use frame_system::limits; + use pallet_transaction_payment::{Multiplier, TargetedFeeAdjustment}; + pub use runtime_common::{ + AVERAGE_ON_INITIALIZE_RATIO, MAXIMUM_BLOCK_WEIGHT, NORMAL_DISPATCH_RATIO, + }; + use sp_runtime::{FixedPointNumber, Perquintill}; + + /// Implement the weight types of the elections module. + runtime_common::impl_elections_weights!($runtime); + + // Common constants used in all runtimes. + parameter_types! { + pub const BlockHashCount: BlockNumber = 2400; + /// The portion of the `NORMAL_DISPATCH_RATIO` that we adjust the fees with. Blocks filled less + /// than this will decrease the weight and more will increase. + pub const TargetBlockFullness: Perquintill = Perquintill::from_percent(25); + /// The adjustment variable of the runtime. Higher values will cause `TargetBlockFullness` to + /// change the fees more rapidly. + pub AdjustmentVariable: Multiplier = Multiplier::saturating_from_rational(3, 100_000); + /// Minimum amount of the multiplier. This value cannot be too low. A test case should ensure + /// that combined with `AdjustmentVariable`, we can recover from the minimum. + /// See `multiplier_can_grow_from_zero`. + pub MinimumMultiplier: Multiplier = Multiplier::saturating_from_rational(1, 1_000_000u128); + /// Maximum length of block. Up to 5MB. + pub BlockLength: limits::BlockLength = + limits::BlockLength::max_with_normal_ratio(5 * 1024 * 1024, NORMAL_DISPATCH_RATIO); + /// Block weights base values and limits. + pub BlockWeights: limits::BlockWeights = limits::BlockWeights::builder() + .base_block($runtime::weights::BlockExecutionWeight::get()) + .for_class(DispatchClass::all(), |weights| { + weights.base_extrinsic = $runtime::weights::ExtrinsicBaseWeight::get(); + }) + .for_class(DispatchClass::Normal, |weights| { + weights.max_total = Some(NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT); + }) + .for_class(DispatchClass::Operational, |weights| { + weights.max_total = Some(MAXIMUM_BLOCK_WEIGHT); + // Operational transactions have an extra reserved space, so that they + // are included even if block reached `MAXIMUM_BLOCK_WEIGHT`. + weights.reserved = Some( + MAXIMUM_BLOCK_WEIGHT - NORMAL_DISPATCH_RATIO * MAXIMUM_BLOCK_WEIGHT, + ); + }) + .avg_block_initialization(AVERAGE_ON_INITIALIZE_RATIO) + .build_or_panic(); + } -/// Parameterized slow adjusting fee updated based on -/// https://research.web3.foundation/en/latest/polkadot/overview/2-token-economics.html#-2.-slow-adjusting-mechanism -pub type SlowAdjustingFeeUpdate = - TargetedFeeAdjustment; + /// Parameterized slow adjusting fee updated based on + /// https://research.web3.foundation/en/latest/polkadot/overview/2-token-economics.html#-2.-slow-adjusting-mechanism + pub type SlowAdjustingFeeUpdate = + TargetedFeeAdjustment; + }; +} /// The type used for currency conversion. /// diff --git a/runtime/kusama/constants/src/lib.rs b/runtime/kusama/constants/src/lib.rs index f9cdf807c27d..cc3505330eb1 100644 --- a/runtime/kusama/constants/src/lib.rs +++ b/runtime/kusama/constants/src/lib.rs @@ -15,6 +15,9 @@ // along with Polkadot. If not, see . #![cfg_attr(not(feature = "std"), no_std)] + +pub mod weights; + /// Money matters. pub mod currency { use primitives::v0::Balance; @@ -51,11 +54,11 @@ pub mod time { /// Fee-related. pub mod fee { + use crate::weights::ExtrinsicBaseWeight; use frame_support::weights::{ WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial, }; use primitives::v0::Balance; - use runtime_common::ExtrinsicBaseWeight; use smallvec::smallvec; pub use sp_runtime::Perbill; diff --git a/runtime/kusama/src/lib.rs b/runtime/kusama/src/lib.rs index 3b7cfe528c04..3c89edaf529d 100644 --- a/runtime/kusama/src/lib.rs +++ b/runtime/kusama/src/lib.rs @@ -33,9 +33,8 @@ use primitives::{ v2::SessionInfo, }; use runtime_common::{ - auctions, claims, crowdloan, impls::DealWithFees, paras_registrar, prod_or_fast, slots, - BlockHashCount, BlockLength, BlockWeights, CurrencyToVote, OffchainSolutionLengthLimit, - OffchainSolutionWeightLimit, RocksDbWeight, SlowAdjustingFeeUpdate, + auctions, claims, crowdloan, impl_runtime_weights, impls::DealWithFees, paras_registrar, + prod_or_fast, slots, CurrencyToVote, }; use sp_std::{cmp::Ordering, collections::btree_map::BTreeMap, prelude::*}; @@ -56,7 +55,6 @@ use frame_support::{ Contains, EnsureOneOf, InstanceFilter, KeyOwnerProofSystem, LockIdentifier, OnRuntimeUpgrade, PrivilegeCmp, }, - weights::Weight, PalletId, RuntimeDebug, }; use frame_system::EnsureRoot; @@ -65,7 +63,6 @@ use pallet_im_online::sr25519::AuthorityId as ImOnlineId; use pallet_mmr_primitives as mmr; use pallet_session::historical as session_historical; use pallet_transaction_payment::{FeeDetails, RuntimeDispatchInfo}; -use sp_arithmetic::Perquintill; use sp_core::OpaqueMetadata; use sp_runtime::{ create_runtime_str, generic, impl_opaque_keys, @@ -91,7 +88,7 @@ pub use pallet_timestamp::Call as TimestampCall; pub use sp_runtime::BuildStorage; /// Constant values used within the runtime. -use kusama_runtime_constants::{currency::*, fee::*, time::*}; +use kusama_runtime_constants::{currency::*, fee::*, time::*, weights::RocksDbWeight}; // Weights used in the runtime. mod weights; @@ -105,6 +102,8 @@ pub mod xcm_config; #[cfg(test)] mod tests; +impl_runtime_weights!(kusama_runtime_constants); + // Make the WASM binary available. #[cfg(feature = "std")] include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); diff --git a/runtime/polkadot/constants/src/lib.rs b/runtime/polkadot/constants/src/lib.rs index 37c26c62074f..6f5911095b04 100644 --- a/runtime/polkadot/constants/src/lib.rs +++ b/runtime/polkadot/constants/src/lib.rs @@ -16,6 +16,8 @@ #![cfg_attr(not(feature = "std"), no_std)] +pub mod weights; + pub use self::currency::DOLLARS; /// Money matters. @@ -54,11 +56,11 @@ pub mod time { /// Fee-related. pub mod fee { + use crate::weights::ExtrinsicBaseWeight; use frame_support::weights::{ WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial, }; use primitives::v0::Balance; - use runtime_common::ExtrinsicBaseWeight; use smallvec::smallvec; pub use sp_runtime::Perbill; diff --git a/runtime/polkadot/src/lib.rs b/runtime/polkadot/src/lib.rs index 0933801332ee..b02a00487385 100644 --- a/runtime/polkadot/src/lib.rs +++ b/runtime/polkadot/src/lib.rs @@ -22,9 +22,8 @@ use pallet_transaction_payment::CurrencyAdapter; use runtime_common::{ - auctions, claims, crowdloan, impls::DealWithFees, paras_registrar, prod_or_fast, slots, - BlockHashCount, BlockLength, BlockWeights, CurrencyToVote, OffchainSolutionLengthLimit, - OffchainSolutionWeightLimit, RocksDbWeight, SlowAdjustingFeeUpdate, + auctions, claims, crowdloan, impl_runtime_weights, impls::DealWithFees, paras_registrar, + prod_or_fast, slots, CurrencyToVote, }; use runtime_parachains::{ @@ -44,7 +43,6 @@ use frame_support::{ Contains, EnsureOneOf, InstanceFilter, KeyOwnerProofSystem, LockIdentifier, OnRuntimeUpgrade, PrivilegeCmp, }, - weights::Weight, PalletId, RuntimeDebug, }; use frame_system::EnsureRoot; @@ -92,7 +90,7 @@ pub use pallet_timestamp::Call as TimestampCall; pub use sp_runtime::BuildStorage; /// Constant values used within the runtime. -use polkadot_runtime_constants::{currency::*, fee::*, time::*}; +use polkadot_runtime_constants::{currency::*, fee::*, time::*, weights::RocksDbWeight}; // Weights used in the runtime. mod weights; @@ -101,6 +99,8 @@ mod bag_thresholds; pub mod xcm_config; +impl_runtime_weights!(polkadot_runtime_constants); + // Make the WASM binary available. #[cfg(feature = "std")] include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); diff --git a/runtime/rococo/constants/src/lib.rs b/runtime/rococo/constants/src/lib.rs index bb0bfc4a9747..4b1fff6f75a0 100644 --- a/runtime/rococo/constants/src/lib.rs +++ b/runtime/rococo/constants/src/lib.rs @@ -16,6 +16,8 @@ #![cfg_attr(not(feature = "std"), no_std)] +pub mod weights; + /// Money matters. pub mod currency { use primitives::v0::Balance; @@ -53,11 +55,11 @@ pub mod time { /// Fee-related. pub mod fee { + use crate::weights::ExtrinsicBaseWeight; use frame_support::weights::{ WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial, }; use primitives::v0::Balance; - use runtime_common::ExtrinsicBaseWeight; use smallvec::smallvec; pub use sp_runtime::Perbill; diff --git a/runtime/rococo/src/lib.rs b/runtime/rococo/src/lib.rs index fe3548aec5dc..6421f8acd6fa 100644 --- a/runtime/rococo/src/lib.rs +++ b/runtime/rococo/src/lib.rs @@ -44,8 +44,8 @@ use primitives::{ v2::{PvfCheckStatement, SessionInfo as SessionInfoData}, }; use runtime_common::{ - assigned_slots, auctions, crowdloan, impls::ToAuthor, paras_registrar, paras_sudo_wrapper, - slots, BlockHashCount, BlockLength, BlockWeights, RocksDbWeight, SlowAdjustingFeeUpdate, + assigned_slots, auctions, crowdloan, impl_runtime_weights, impls::ToAuthor, paras_registrar, + paras_sudo_wrapper, slots, }; use runtime_parachains::{self, runtime_api_impl::v1 as runtime_api_impl}; use scale_info::TypeInfo; @@ -82,7 +82,7 @@ pub use pallet_balances::Call as BalancesCall; use polkadot_parachain::primitives::Id as ParaId; /// Constant values used within the runtime. -use rococo_runtime_constants::{currency::*, fee::*, time::*}; +use rococo_runtime_constants::{currency::*, fee::*, time::*, weights::RocksDbWeight}; use frame_support::traits::{InstanceFilter, OnRuntimeUpgrade}; @@ -91,6 +91,8 @@ mod validator_manager; mod weights; pub mod xcm_config; +impl_runtime_weights!(rococo_runtime_constants); + // Make the WASM binary available. #[cfg(feature = "std")] include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); diff --git a/runtime/westend/constants/src/lib.rs b/runtime/westend/constants/src/lib.rs index 46ce6d9e4af3..199a26ed6dbb 100644 --- a/runtime/westend/constants/src/lib.rs +++ b/runtime/westend/constants/src/lib.rs @@ -16,6 +16,8 @@ #![cfg_attr(not(feature = "std"), no_std)] +pub mod weights; + /// Money matters. pub mod currency { use primitives::v0::Balance; @@ -53,11 +55,11 @@ pub mod time { /// Fee-related. pub mod fee { + use crate::weights::ExtrinsicBaseWeight; use frame_support::weights::{ WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial, }; use primitives::v1::Balance; - use runtime_common::ExtrinsicBaseWeight; use smallvec::smallvec; pub use sp_runtime::Perbill; diff --git a/runtime/westend/src/lib.rs b/runtime/westend/src/lib.rs index 7d3416ed0aae..0d664c0b5c5a 100644 --- a/runtime/westend/src/lib.rs +++ b/runtime/westend/src/lib.rs @@ -33,9 +33,8 @@ use primitives::{ v2::SessionInfo, }; use runtime_common::{ - assigned_slots, auctions, crowdloan, impls::ToAuthor, paras_registrar, paras_sudo_wrapper, - slots, BlockHashCount, BlockLength, BlockWeights, CurrencyToVote, OffchainSolutionLengthLimit, - OffchainSolutionWeightLimit, RocksDbWeight, SlowAdjustingFeeUpdate, + assigned_slots, auctions, crowdloan, impl_runtime_weights, impls::ToAuthor, paras_registrar, + paras_sudo_wrapper, slots, CurrencyToVote, }; use sp_std::{collections::btree_map::BTreeMap, prelude::*}; @@ -53,7 +52,6 @@ use beefy_primitives::crypto::AuthorityId as BeefyId; use frame_support::{ construct_runtime, parameter_types, traits::{Contains, InstanceFilter, KeyOwnerProofSystem, OnRuntimeUpgrade}, - weights::Weight, PalletId, RuntimeDebug, }; use frame_system::EnsureRoot; @@ -88,7 +86,7 @@ pub use pallet_timestamp::Call as TimestampCall; pub use sp_runtime::BuildStorage; /// Constant values used within the runtime. -use westend_runtime_constants::{currency::*, fee::*, time::*}; +use westend_runtime_constants::{currency::*, fee::*, time::*, weights::RocksDbWeight}; // Weights used in the runtime mod weights; @@ -102,6 +100,8 @@ pub mod xcm_config; #[cfg(test)] mod tests; +impl_runtime_weights!(westend_runtime_constants); + // Make the WASM binary available. #[cfg(feature = "std")] include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); From c40cdfbe4eb8c1f5829f1b5a07d700a1e6581f63 Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Wed, 9 Mar 2022 13:11:29 +0100 Subject: [PATCH 03/18] Do the same for test-runtime Signed-off-by: Oliver Tale-Yazdi --- runtime/test-runtime/constants/src/lib.rs | 4 +- .../constants/src/weights/block_weights.rs | 46 ++++++++++++++ .../src/weights/extrinsic_weights.rs | 46 ++++++++++++++ .../test-runtime/constants/src/weights/mod.rs | 28 +++++++++ .../constants/src/weights/paritydb_weights.rs | 63 +++++++++++++++++++ .../constants/src/weights/rocksdb_weights.rs | 63 +++++++++++++++++++ runtime/test-runtime/src/lib.rs | 2 + 7 files changed, 251 insertions(+), 1 deletion(-) create mode 100644 runtime/test-runtime/constants/src/weights/block_weights.rs create mode 100644 runtime/test-runtime/constants/src/weights/extrinsic_weights.rs create mode 100644 runtime/test-runtime/constants/src/weights/mod.rs create mode 100644 runtime/test-runtime/constants/src/weights/paritydb_weights.rs create mode 100644 runtime/test-runtime/constants/src/weights/rocksdb_weights.rs diff --git a/runtime/test-runtime/constants/src/lib.rs b/runtime/test-runtime/constants/src/lib.rs index 331e3ab47cb8..e8ed5a65ccb2 100644 --- a/runtime/test-runtime/constants/src/lib.rs +++ b/runtime/test-runtime/constants/src/lib.rs @@ -16,6 +16,8 @@ #![cfg_attr(not(feature = "std"), no_std)] +pub mod weights; + /// Money matters. pub mod currency { use primitives::v0::Balance; @@ -46,11 +48,11 @@ pub mod time { /// Fee-related. pub mod fee { + use crate::weights::ExtrinsicBaseWeight; use frame_support::weights::{ WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial, }; use primitives::v0::Balance; - use runtime_common::ExtrinsicBaseWeight; use smallvec::smallvec; pub use sp_runtime::Perbill; diff --git a/runtime/test-runtime/constants/src/weights/block_weights.rs b/runtime/test-runtime/constants/src/weights/block_weights.rs new file mode 100644 index 000000000000..4db90f0c0207 --- /dev/null +++ b/runtime/test-runtime/constants/src/weights/block_weights.rs @@ -0,0 +1,46 @@ +// This file is part of Substrate. + +// Copyright (C) 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. + +pub mod constants { + use frame_support::{ + parameter_types, + weights::{constants, Weight}, + }; + + parameter_types! { + /// Importing a block with 0 Extrinsics. + pub const BlockExecutionWeight: Weight = 5_000_000 * constants::WEIGHT_PER_NANOS; + } + + #[cfg(test)] + mod test_weights { + use frame_support::weights::constants; + + /// Checks that the weight exists and is sane. + // NOTE: If this test fails but you are sure that the generated values are fine, + // you can delete it. + #[test] + fn sane() { + let w = super::constants::BlockExecutionWeight::get(); + + // At least 100 µs. + assert!(w >= 100 * constants::WEIGHT_PER_MICROS, "Weight should be at least 100 µs."); + // At most 50 ms. + assert!(w <= 50 * constants::WEIGHT_PER_MILLIS, "Weight should be at most 50 ms."); + } + } +} diff --git a/runtime/test-runtime/constants/src/weights/extrinsic_weights.rs b/runtime/test-runtime/constants/src/weights/extrinsic_weights.rs new file mode 100644 index 000000000000..158ba99c6a4c --- /dev/null +++ b/runtime/test-runtime/constants/src/weights/extrinsic_weights.rs @@ -0,0 +1,46 @@ +// This file is part of Substrate. + +// Copyright (C) 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. + +pub mod constants { + use frame_support::{ + parameter_types, + weights::{constants, Weight}, + }; + + parameter_types! { + /// Executing a NO-OP `System::remarks` Extrinsic. + pub const ExtrinsicBaseWeight: Weight = 125_000 * constants::WEIGHT_PER_NANOS; + } + + #[cfg(test)] + mod test_weights { + use frame_support::weights::constants; + + /// Checks that the weight exists and is sane. + // NOTE: If this test fails but you are sure that the generated values are fine, + // you can delete it. + #[test] + fn sane() { + let w = super::constants::ExtrinsicBaseWeight::get(); + + // At least 10 µs. + assert!(w >= 10 * constants::WEIGHT_PER_MICROS, "Weight should be at least 10 µs."); + // At most 1 ms. + assert!(w <= constants::WEIGHT_PER_MILLIS, "Weight should be at most 1 ms."); + } + } +} diff --git a/runtime/test-runtime/constants/src/weights/mod.rs b/runtime/test-runtime/constants/src/weights/mod.rs new file mode 100644 index 000000000000..ed0b4dbcd47f --- /dev/null +++ b/runtime/test-runtime/constants/src/weights/mod.rs @@ -0,0 +1,28 @@ +// This file is part of Substrate. + +// Copyright (C) 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. + +//! Expose the auto generated weight files. + +pub mod block_weights; +pub mod extrinsic_weights; +pub mod paritydb_weights; +pub mod rocksdb_weights; + +pub use block_weights::constants::BlockExecutionWeight; +pub use extrinsic_weights::constants::ExtrinsicBaseWeight; +pub use paritydb_weights::constants::ParityDbWeight; +pub use rocksdb_weights::constants::RocksDbWeight; diff --git a/runtime/test-runtime/constants/src/weights/paritydb_weights.rs b/runtime/test-runtime/constants/src/weights/paritydb_weights.rs new file mode 100644 index 000000000000..572187ba78a9 --- /dev/null +++ b/runtime/test-runtime/constants/src/weights/paritydb_weights.rs @@ -0,0 +1,63 @@ +// This file is part of Substrate. + +// Copyright (C) 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. + +pub mod constants { + use frame_support::{ + parameter_types, + weights::{constants, RuntimeDbWeight}, + }; + + parameter_types! { + /// ParityDB can be enabled with a feature flag, but is still experimental. These weights + /// are available for brave runtime engineers who may want to try this out as default. + pub const ParityDbWeight: RuntimeDbWeight = RuntimeDbWeight { + read: 8_000 * constants::WEIGHT_PER_NANOS, + write: 50_000 * constants::WEIGHT_PER_NANOS, + }; + } + + #[cfg(test)] + mod test_db_weights { + use super::constants::ParityDbWeight as W; + use frame_support::weights::constants; + + /// Checks that all weights exist and have sane values. + // NOTE: If this test fails but you are sure that the generated values are fine, + // you can delete it. + #[test] + fn sane() { + // At least 1 µs. + assert!( + W::get().reads(1) >= constants::WEIGHT_PER_MICROS, + "Read weight should be at least 1 µs." + ); + assert!( + W::get().writes(1) >= constants::WEIGHT_PER_MICROS, + "Write weight should be at least 1 µs." + ); + // At most 1 ms. + assert!( + W::get().reads(1) <= constants::WEIGHT_PER_MILLIS, + "Read weight should be at most 1 ms." + ); + assert!( + W::get().writes(1) <= constants::WEIGHT_PER_MILLIS, + "Write weight should be at most 1 ms." + ); + } + } +} diff --git a/runtime/test-runtime/constants/src/weights/rocksdb_weights.rs b/runtime/test-runtime/constants/src/weights/rocksdb_weights.rs new file mode 100644 index 000000000000..f37964dcbd82 --- /dev/null +++ b/runtime/test-runtime/constants/src/weights/rocksdb_weights.rs @@ -0,0 +1,63 @@ +// This file is part of Substrate. + +// Copyright (C) 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. + +pub mod constants { + use frame_support::{ + parameter_types, + weights::{constants, RuntimeDbWeight}, + }; + + parameter_types! { + /// By default, Substrate uses RocksDB, so this will be the weight used throughout + /// the runtime. + pub const RocksDbWeight: RuntimeDbWeight = RuntimeDbWeight { + read: 25_000 * constants::WEIGHT_PER_NANOS, + write: 100_000 * constants::WEIGHT_PER_NANOS, + }; + } + + #[cfg(test)] + mod test_db_weights { + use super::constants::RocksDbWeight as W; + use frame_support::weights::constants; + + /// Checks that all weights exist and have sane values. + // NOTE: If this test fails but you are sure that the generated values are fine, + // you can delete it. + #[test] + fn sane() { + // At least 1 µs. + assert!( + W::get().reads(1) >= constants::WEIGHT_PER_MICROS, + "Read weight should be at least 1 µs." + ); + assert!( + W::get().writes(1) >= constants::WEIGHT_PER_MICROS, + "Write weight should be at least 1 µs." + ); + // At most 1 ms. + assert!( + W::get().reads(1) <= constants::WEIGHT_PER_MILLIS, + "Read weight should be at most 1 ms." + ); + assert!( + W::get().writes(1) <= constants::WEIGHT_PER_MILLIS, + "Write weight should be at most 1 ms." + ); + } + } +} diff --git a/runtime/test-runtime/src/lib.rs b/runtime/test-runtime/src/lib.rs index aad95bbe0c06..e73746f50b0a 100644 --- a/runtime/test-runtime/src/lib.rs +++ b/runtime/test-runtime/src/lib.rs @@ -87,6 +87,8 @@ pub use sp_runtime::BuildStorage; use test_runtime_constants::{currency::*, fee::*, time::*}; pub mod xcm_config; +impl_runtime_weights!(test_runtime_constants); + // Make the WASM binary available. #[cfg(feature = "std")] include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); From 26c9893a1ed5845588804234ec45ecca06b0b39d Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Wed, 9 Mar 2022 13:22:33 +0100 Subject: [PATCH 04/18] Fix CI Signed-off-by: Oliver Tale-Yazdi --- runtime/test-runtime/src/lib.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/runtime/test-runtime/src/lib.rs b/runtime/test-runtime/src/lib.rs index e73746f50b0a..2e971ab20fe9 100644 --- a/runtime/test-runtime/src/lib.rs +++ b/runtime/test-runtime/src/lib.rs @@ -54,9 +54,7 @@ use primitives::{ }, v2::SessionInfo as SessionInfoData, }; -use runtime_common::{ - claims, paras_sudo_wrapper, BlockHashCount, BlockLength, BlockWeights, SlowAdjustingFeeUpdate, -}; +use runtime_common::{claims, impl_runtime_weights, paras_sudo_wrapper}; use sp_core::OpaqueMetadata; use sp_runtime::{ create_runtime_str, From 8fe576cfd2e70323f32aed2c623614c5f79f9b20 Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Wed, 9 Mar 2022 14:01:56 +0100 Subject: [PATCH 05/18] Generate multiplier tests via macro Signed-off-by: Oliver Tale-Yazdi --- Cargo.lock | 1 - node/test/service/Cargo.toml | 1 - node/test/service/src/lib.rs | 5 +- runtime/common/src/lib.rs | 209 ++++++++++++++++++----------------- 4 files changed, 111 insertions(+), 105 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 50e0ab63f280..38830849a914 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7673,7 +7673,6 @@ dependencies = [ "polkadot-parachain", "polkadot-primitives", "polkadot-rpc", - "polkadot-runtime-common", "polkadot-runtime-parachains", "polkadot-service", "polkadot-test-runtime", diff --git a/node/test/service/Cargo.toml b/node/test/service/Cargo.toml index a85777238174..c2a6393a6158 100644 --- a/node/test/service/Cargo.toml +++ b/node/test/service/Cargo.toml @@ -18,7 +18,6 @@ polkadot-overseer = { path = "../../overseer" } polkadot-primitives = { path = "../../../primitives" } polkadot-parachain = { path = "../../../parachain" } polkadot-rpc = { path = "../../../rpc" } -polkadot-runtime-common = { path = "../../../runtime/common" } polkadot-service = { path = "../../service" } polkadot-node-subsystem = { path = "../../subsystem" } polkadot-node-primitives = { path = "../../primitives" } diff --git a/node/test/service/src/lib.rs b/node/test/service/src/lib.rs index 112471025bc1..7c2927ba4677 100644 --- a/node/test/service/src/lib.rs +++ b/node/test/service/src/lib.rs @@ -26,14 +26,13 @@ use polkadot_node_primitives::{CollationGenerationConfig, CollatorFn}; use polkadot_node_subsystem::messages::{CollationGenerationMessage, CollatorProtocolMessage}; use polkadot_overseer::Handle; use polkadot_primitives::v1::{Balance, CollatorPair, HeadData, Id as ParaId, ValidationCode}; -use polkadot_runtime_common::BlockHashCount; use polkadot_runtime_parachains::paras::ParaGenesisArgs; use polkadot_service::{ ClientHandle, Error, ExecuteWithClient, FullClient, IsCollator, NewFull, PrometheusConfig, }; use polkadot_test_runtime::{ - ParasSudoWrapperCall, Runtime, SignedExtra, SignedPayload, SudoCall, UncheckedExtrinsic, - VERSION, + BlockHashCount, ParasSudoWrapperCall, Runtime, SignedExtra, SignedPayload, SudoCall, + UncheckedExtrinsic, VERSION, }; use sc_chain_spec::ChainSpec; use sc_client_api::execution_extensions::ExecutionStrategies; diff --git a/runtime/common/src/lib.rs b/runtime/common/src/lib.rs index 6880f612f805..3cee0da2f304 100644 --- a/runtime/common/src/lib.rs +++ b/runtime/common/src/lib.rs @@ -88,8 +88,10 @@ macro_rules! impl_runtime_weights { }; use sp_runtime::{FixedPointNumber, Perquintill}; - /// Implement the weight types of the elections module. + // Implement the weight types of the elections module. runtime_common::impl_elections_weights!($runtime); + // Implement tests for the weight multiplier. + runtime_common::impl_multiplier_tests(); // Common constants used in all runtimes. parameter_types! { @@ -235,112 +237,119 @@ macro_rules! prod_or_fast { }; } -#[cfg(test)] -mod multiplier_tests { - use super::*; - use frame_support::{parameter_types, weights::Weight}; - use sp_core::H256; - use sp_runtime::{ - testing::Header, - traits::{BlakeTwo256, Convert, IdentityLookup, One}, - Perbill, - }; +/// Generates tests that check that the different weight multiplier work together. +/// Should not be called directly, use [`impl_runtime_weights`] instead. +#[macro_export] +macro_rules! impl_multiplier_tests { + () => { + #[cfg(test)] + mod multiplier_tests { + use super::*; + use frame_support::{parameter_types, weights::Weight}; + use sp_core::H256; + use sp_runtime::{ + testing::Header, + traits::{BlakeTwo256, Convert, IdentityLookup}, + Perbill, + }; - type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; - type Block = frame_system::mocking::MockBlock; + type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; + type Block = frame_system::mocking::MockBlock; - frame_support::construct_runtime!( - pub enum Runtime where - Block = Block, - NodeBlock = Block, - UncheckedExtrinsic = UncheckedExtrinsic, - { - System: frame_system::{Pallet, Call, Config, Storage, Event} + frame_support::construct_runtime!( + pub enum Runtime where + Block = Block, + NodeBlock = Block, + UncheckedExtrinsic = UncheckedExtrinsic, + { + System: frame_system::{Pallet, Call, Config, Storage, Event} + } + ); + + parameter_types! { + pub const BlockHashCount: u64 = 250; + pub const AvailableBlockRatio: Perbill = Perbill::one(); + pub BlockLength: frame_system::limits::BlockLength = + frame_system::limits::BlockLength::max(2 * 1024); + pub BlockWeights: frame_system::limits::BlockWeights = + frame_system::limits::BlockWeights::simple_max(1024); } - ); - - parameter_types! { - pub const BlockHashCount: u64 = 250; - pub const AvailableBlockRatio: Perbill = Perbill::one(); - pub BlockLength: frame_system::limits::BlockLength = - frame_system::limits::BlockLength::max(2 * 1024); - pub BlockWeights: frame_system::limits::BlockWeights = - frame_system::limits::BlockWeights::simple_max(1024); - } - impl frame_system::Config for Runtime { - type BaseCallFilter = frame_support::traits::Everything; - type BlockWeights = BlockWeights; - type BlockLength = (); - type DbWeight = (); - type Origin = Origin; - type Index = u64; - type BlockNumber = u64; - type Call = Call; - type Hash = H256; - type Hashing = BlakeTwo256; - type AccountId = u64; - type Lookup = IdentityLookup; - type Header = Header; - type Event = Event; - type BlockHashCount = BlockHashCount; - type Version = (); - type PalletInfo = PalletInfo; - type AccountData = (); - type OnNewAccount = (); - type OnKilledAccount = (); - type SystemWeightInfo = (); - type SS58Prefix = (); - type OnSetCode = (); - type MaxConsumers = frame_support::traits::ConstU32<16>; - } + impl frame_system::Config for Runtime { + type BaseCallFilter = frame_support::traits::Everything; + type BlockWeights = BlockWeights; + type BlockLength = (); + type DbWeight = (); + type Origin = Origin; + type Index = u64; + type BlockNumber = u64; + type Call = Call; + type Hash = H256; + type Hashing = BlakeTwo256; + type AccountId = u64; + type Lookup = IdentityLookup; + type Header = Header; + type Event = Event; + type BlockHashCount = BlockHashCount; + type Version = (); + type PalletInfo = PalletInfo; + type AccountData = (); + type OnNewAccount = (); + type OnKilledAccount = (); + type SystemWeightInfo = (); + type SS58Prefix = (); + type OnSetCode = (); + type MaxConsumers = frame_support::traits::ConstU32<16>; + } - fn run_with_system_weight(w: Weight, mut assertions: F) - where - F: FnMut() -> (), - { - let mut t: sp_io::TestExternalities = frame_system::GenesisConfig::default() - .build_storage::() - .unwrap() - .into(); - t.execute_with(|| { - System::set_block_consumed_resources(w, 0); - assertions() - }); - } + fn run_with_system_weight(w: Weight, mut assertions: F) + where + F: FnMut() -> (), + { + let mut t: sp_io::TestExternalities = frame_system::GenesisConfig::default() + .build_storage::() + .unwrap() + .into(); + t.execute_with(|| { + System::set_block_consumed_resources(w, 0); + assertions() + }); + } - #[test] - fn multiplier_can_grow_from_zero() { - let minimum_multiplier = MinimumMultiplier::get(); - let target = TargetBlockFullness::get() * - BlockWeights::get().get(DispatchClass::Normal).max_total.unwrap(); - // if the min is too small, then this will not change, and we are doomed forever. - // the weight is 1/100th bigger than target. - run_with_system_weight(target * 101 / 100, || { - let next = SlowAdjustingFeeUpdate::::convert(minimum_multiplier); - assert!(next > minimum_multiplier, "{:?} !>= {:?}", next, minimum_multiplier); - }) - } + #[test] + fn multiplier_can_grow_from_zero() { + let minimum_multiplier = MinimumMultiplier::get(); + let target = TargetBlockFullness::get() * + BlockWeights::get().get(DispatchClass::Normal).max_total.unwrap(); + // if the min is too small, then this will not change, and we are doomed forever. + // the weight is 1/100th bigger than target. + run_with_system_weight(target * 101 / 100, || { + let next = SlowAdjustingFeeUpdate::::convert(minimum_multiplier); + assert!(next > minimum_multiplier, "{:?} !>= {:?}", next, minimum_multiplier); + }) + } - #[test] - #[ignore] - fn multiplier_growth_simulator() { - // assume the multiplier is initially set to its minimum. We update it with values twice the - //target (target is 25%, thus 50%) and we see at which point it reaches 1. - let mut multiplier = MinimumMultiplier::get(); - let block_weight = TargetBlockFullness::get() * - BlockWeights::get().get(DispatchClass::Normal).max_total.unwrap() * - 2; - let mut blocks = 0; - while multiplier <= Multiplier::one() { - run_with_system_weight(block_weight, || { - let next = SlowAdjustingFeeUpdate::::convert(multiplier); - // ensure that it is growing as well. - assert!(next > multiplier, "{:?} !>= {:?}", next, multiplier); - multiplier = next; - }); - blocks += 1; - println!("block = {} multiplier {:?}", blocks, multiplier); + #[test] + #[ignore] + fn multiplier_growth_simulator() { + // assume the multiplier is initially set to its minimum. We update it with values twice the + //target (target is 25%, thus 50%) and we see at which point it reaches 1. + let mut multiplier = MinimumMultiplier::get(); + let block_weight = TargetBlockFullness::get() * + BlockWeights::get().get(DispatchClass::Normal).max_total.unwrap() * + 2; + let mut blocks = 0; + while multiplier <= Multiplier::one() { + run_with_system_weight(block_weight, || { + let next = SlowAdjustingFeeUpdate::::convert(multiplier); + // ensure that it is growing as well. + assert!(next > multiplier, "{:?} !>= {:?}", next, multiplier); + multiplier = next; + }); + blocks += 1; + println!("block = {} multiplier {:?}", blocks, multiplier); + } } } + } } From a5fe30f81a8f37367eb3663e8c086ca0cc58a0f1 Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Wed, 9 Mar 2022 14:14:33 +0100 Subject: [PATCH 06/18] Fix tests Signed-off-by: Oliver Tale-Yazdi --- runtime/common/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/common/src/lib.rs b/runtime/common/src/lib.rs index 3cee0da2f304..8cf9aa7dc44e 100644 --- a/runtime/common/src/lib.rs +++ b/runtime/common/src/lib.rs @@ -91,7 +91,7 @@ macro_rules! impl_runtime_weights { // Implement the weight types of the elections module. runtime_common::impl_elections_weights!($runtime); // Implement tests for the weight multiplier. - runtime_common::impl_multiplier_tests(); + runtime_common::impl_multiplier_tests!(); // Common constants used in all runtimes. parameter_types! { From d147d8de552380631e70bfbc146135ec46af21d2 Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Wed, 9 Mar 2022 14:53:55 +0100 Subject: [PATCH 07/18] Fix CI Signed-off-by: Oliver Tale-Yazdi --- runtime/kusama/constants/src/lib.rs | 3 ++- runtime/polkadot/constants/src/lib.rs | 3 ++- runtime/rococo/constants/src/lib.rs | 2 +- runtime/westend/constants/src/lib.rs | 3 ++- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/runtime/kusama/constants/src/lib.rs b/runtime/kusama/constants/src/lib.rs index cc3505330eb1..1c2bf4d4948b 100644 --- a/runtime/kusama/constants/src/lib.rs +++ b/runtime/kusama/constants/src/lib.rs @@ -97,9 +97,10 @@ mod tests { use super::{ currency::{CENTS, MILLICENTS}, fee::WeightToFee, + weights::ExtrinsicBaseWeight, }; use frame_support::weights::WeightToFeePolynomial; - use runtime_common::{ExtrinsicBaseWeight, MAXIMUM_BLOCK_WEIGHT}; + use runtime_common::MAXIMUM_BLOCK_WEIGHT; #[test] // This function tests that the fee for `MAXIMUM_BLOCK_WEIGHT` of weight is correct diff --git a/runtime/polkadot/constants/src/lib.rs b/runtime/polkadot/constants/src/lib.rs index 6f5911095b04..c2dabbcf1899 100644 --- a/runtime/polkadot/constants/src/lib.rs +++ b/runtime/polkadot/constants/src/lib.rs @@ -99,9 +99,10 @@ mod tests { use super::{ currency::{CENTS, DOLLARS, MILLICENTS}, fee::WeightToFee, + ExtrinsicBaseWeight, }; use frame_support::weights::WeightToFeePolynomial; - use runtime_common::{ExtrinsicBaseWeight, MAXIMUM_BLOCK_WEIGHT}; + use runtime_common::MAXIMUM_BLOCK_WEIGHT; #[test] // This function tests that the fee for `MAXIMUM_BLOCK_WEIGHT` of weight is correct diff --git a/runtime/rococo/constants/src/lib.rs b/runtime/rococo/constants/src/lib.rs index 4b1fff6f75a0..ce7292d40729 100644 --- a/runtime/rococo/constants/src/lib.rs +++ b/runtime/rococo/constants/src/lib.rs @@ -98,9 +98,9 @@ mod tests { use super::{ currency::{CENTS, DOLLARS, MILLICENTS}, fee::WeightToFee, + BlockWeights, }; use frame_support::weights::{DispatchClass, WeightToFeePolynomial}; - use runtime_common::BlockWeights; #[test] // This function tests that the fee for `MaximumBlockWeight` of weight is correct diff --git a/runtime/westend/constants/src/lib.rs b/runtime/westend/constants/src/lib.rs index 199a26ed6dbb..e65da4d0f80a 100644 --- a/runtime/westend/constants/src/lib.rs +++ b/runtime/westend/constants/src/lib.rs @@ -98,9 +98,10 @@ mod tests { use super::{ currency::{CENTS, MILLICENTS}, fee::WeightToFee, + ExtrinsicBaseWeight, }; use frame_support::weights::WeightToFeePolynomial; - use runtime_common::{ExtrinsicBaseWeight, MAXIMUM_BLOCK_WEIGHT}; + use runtime_common::MAXIMUM_BLOCK_WEIGHT; #[test] // This function tests that the fee for `MAXIMUM_BLOCK_WEIGHT` of weight is correct From 81c71000e2557e7c51e6f76f74c366beb3cf8f9b Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Wed, 9 Mar 2022 16:15:04 +0100 Subject: [PATCH 08/18] Fix imports Signed-off-by: Oliver Tale-Yazdi --- runtime/rococo/constants/src/lib.rs | 2 +- runtime/westend/constants/src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/runtime/rococo/constants/src/lib.rs b/runtime/rococo/constants/src/lib.rs index ce7292d40729..8f033b3c8b80 100644 --- a/runtime/rococo/constants/src/lib.rs +++ b/runtime/rococo/constants/src/lib.rs @@ -98,7 +98,7 @@ mod tests { use super::{ currency::{CENTS, DOLLARS, MILLICENTS}, fee::WeightToFee, - BlockWeights, + weights::BlockWeights, }; use frame_support::weights::{DispatchClass, WeightToFeePolynomial}; diff --git a/runtime/westend/constants/src/lib.rs b/runtime/westend/constants/src/lib.rs index e65da4d0f80a..eab93223cad6 100644 --- a/runtime/westend/constants/src/lib.rs +++ b/runtime/westend/constants/src/lib.rs @@ -98,7 +98,7 @@ mod tests { use super::{ currency::{CENTS, MILLICENTS}, fee::WeightToFee, - ExtrinsicBaseWeight, + weights::ExtrinsicBaseWeight, }; use frame_support::weights::WeightToFeePolynomial; use runtime_common::MAXIMUM_BLOCK_WEIGHT; From c9197e3f932eee5fd3a3b04a3f4e0cba7b34a43d Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Wed, 9 Mar 2022 16:27:19 +0100 Subject: [PATCH 09/18] Fix test Signed-off-by: Oliver Tale-Yazdi --- runtime/kusama/constants/src/lib.rs | 2 +- runtime/polkadot/constants/src/lib.rs | 2 +- runtime/rococo/constants/src/lib.rs | 2 +- runtime/westend/constants/src/lib.rs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/runtime/kusama/constants/src/lib.rs b/runtime/kusama/constants/src/lib.rs index 1c2bf4d4948b..fd1b91a1980d 100644 --- a/runtime/kusama/constants/src/lib.rs +++ b/runtime/kusama/constants/src/lib.rs @@ -97,8 +97,8 @@ mod tests { use super::{ currency::{CENTS, MILLICENTS}, fee::WeightToFee, - weights::ExtrinsicBaseWeight, }; + use crate::weights::ExtrinsicBaseWeight; use frame_support::weights::WeightToFeePolynomial; use runtime_common::MAXIMUM_BLOCK_WEIGHT; diff --git a/runtime/polkadot/constants/src/lib.rs b/runtime/polkadot/constants/src/lib.rs index c2dabbcf1899..5c6323c5244e 100644 --- a/runtime/polkadot/constants/src/lib.rs +++ b/runtime/polkadot/constants/src/lib.rs @@ -99,8 +99,8 @@ mod tests { use super::{ currency::{CENTS, DOLLARS, MILLICENTS}, fee::WeightToFee, - ExtrinsicBaseWeight, }; + use crate::weights::BlockWeights; use frame_support::weights::WeightToFeePolynomial; use runtime_common::MAXIMUM_BLOCK_WEIGHT; diff --git a/runtime/rococo/constants/src/lib.rs b/runtime/rococo/constants/src/lib.rs index 8f033b3c8b80..c8b8ef286bd7 100644 --- a/runtime/rococo/constants/src/lib.rs +++ b/runtime/rococo/constants/src/lib.rs @@ -98,8 +98,8 @@ mod tests { use super::{ currency::{CENTS, DOLLARS, MILLICENTS}, fee::WeightToFee, - weights::BlockWeights, }; + use crate::weights::BlockWeights; use frame_support::weights::{DispatchClass, WeightToFeePolynomial}; #[test] diff --git a/runtime/westend/constants/src/lib.rs b/runtime/westend/constants/src/lib.rs index eab93223cad6..5c5d04fa1257 100644 --- a/runtime/westend/constants/src/lib.rs +++ b/runtime/westend/constants/src/lib.rs @@ -98,8 +98,8 @@ mod tests { use super::{ currency::{CENTS, MILLICENTS}, fee::WeightToFee, - weights::ExtrinsicBaseWeight, }; + use crate::weights::ExtrinsicBaseWeight; use frame_support::weights::WeightToFeePolynomial; use runtime_common::MAXIMUM_BLOCK_WEIGHT; From 49c35e4589743c5f86cb889370bb327065c88db9 Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Wed, 9 Mar 2022 16:41:07 +0100 Subject: [PATCH 10/18] Fix imports Signed-off-by: Oliver Tale-Yazdi --- runtime/polkadot/constants/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/polkadot/constants/src/lib.rs b/runtime/polkadot/constants/src/lib.rs index 5c6323c5244e..28bff0fe3fd3 100644 --- a/runtime/polkadot/constants/src/lib.rs +++ b/runtime/polkadot/constants/src/lib.rs @@ -100,7 +100,7 @@ mod tests { currency::{CENTS, DOLLARS, MILLICENTS}, fee::WeightToFee, }; - use crate::weights::BlockWeights; + use crate::weights::ExtrinsicBaseWeight; use frame_support::weights::WeightToFeePolynomial; use runtime_common::MAXIMUM_BLOCK_WEIGHT; From 8b2c0320e4b75bfe222ba3a3f7c391689d564724 Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Wed, 9 Mar 2022 16:41:31 +0100 Subject: [PATCH 11/18] Change rococo tests to use ExtrinsicBaseWeight Signed-off-by: Oliver Tale-Yazdi --- runtime/rococo/constants/src/lib.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/runtime/rococo/constants/src/lib.rs b/runtime/rococo/constants/src/lib.rs index c8b8ef286bd7..3d8fa0fe5b3e 100644 --- a/runtime/rococo/constants/src/lib.rs +++ b/runtime/rococo/constants/src/lib.rs @@ -99,15 +99,16 @@ mod tests { currency::{CENTS, DOLLARS, MILLICENTS}, fee::WeightToFee, }; - use crate::weights::BlockWeights; + use crate::weights::ExtrinsicBaseWeight; use frame_support::weights::{DispatchClass, WeightToFeePolynomial}; + use runtime_common::MAXIMUM_BLOCK_WEIGHT; #[test] - // This function tests that the fee for `MaximumBlockWeight` of weight is correct + // This function tests that the fee for `MAXIMUM_BLOCK_WEIGHT` of weight is correct fn full_block_fee_is_correct() { // A full block should cost 16 DOLLARS - println!("Base: {}", BlockWeights::get().get(DispatchClass::Normal).base_extrinsic); - let x = WeightToFee::calc(&BlockWeights::get().max_block); + println!("Base: {}", ExtrinsicBaseWeight::get()); + let x = WeightToFee::calc(&MAXIMUM_BLOCK_WEIGHT); let y = 16 * DOLLARS; assert!(x.max(y) - x.min(y) < MILLICENTS); } @@ -116,9 +117,8 @@ mod tests { // This function tests that the fee for `ExtrinsicBaseWeight` of weight is correct fn extrinsic_base_fee_is_correct() { // `ExtrinsicBaseWeight` should cost 1/10 of a CENT - let base_weight = BlockWeights::get().get(DispatchClass::Normal).base_extrinsic; - println!("Base: {}", base_weight); - let x = WeightToFee::calc(&base_weight); + println!("Base: {}", ExtrinsicBaseWeight::get()); + let x = WeightToFee::calc(&ExtrinsicBaseWeight::get()); let y = CENTS / 10; assert!(x.max(y) - x.min(y) < MILLICENTS); } From fd59c614420322f04d2d93ff138bb134107752cd Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Wed, 9 Mar 2022 17:15:11 +0100 Subject: [PATCH 12/18] Fix imports Signed-off-by: Oliver Tale-Yazdi --- runtime/common/src/lib.rs | 2 +- runtime/kusama/src/tests.rs | 2 +- runtime/polkadot/src/lib.rs | 2 +- runtime/rococo/constants/src/lib.rs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/runtime/common/src/lib.rs b/runtime/common/src/lib.rs index 8cf9aa7dc44e..5c5d2d7c234b 100644 --- a/runtime/common/src/lib.rs +++ b/runtime/common/src/lib.rs @@ -249,7 +249,7 @@ macro_rules! impl_multiplier_tests { use sp_core::H256; use sp_runtime::{ testing::Header, - traits::{BlakeTwo256, Convert, IdentityLookup}, + traits::{BlakeTwo256, One, Convert, IdentityLookup}, Perbill, }; diff --git a/runtime/kusama/src/tests.rs b/runtime/kusama/src/tests.rs index c81848194fcd..add97b0b8708 100644 --- a/runtime/kusama/src/tests.rs +++ b/runtime/kusama/src/tests.rs @@ -79,7 +79,7 @@ fn block_cost() { #[test] #[ignore] fn transfer_cost_min_multiplier() { - let min_multiplier = runtime_common::MinimumMultiplier::get(); + let min_multiplier = MinimumMultiplier::get(); let call = pallet_balances::Call::::transfer_keep_alive { dest: Charlie.to_account_id().into(), value: Default::default(), diff --git a/runtime/polkadot/src/lib.rs b/runtime/polkadot/src/lib.rs index b02a00487385..039e48e35541 100644 --- a/runtime/polkadot/src/lib.rs +++ b/runtime/polkadot/src/lib.rs @@ -2156,7 +2156,7 @@ mod test_fees { #[test] #[ignore] fn transfer_cost_min_multiplier() { - let min_multiplier = runtime_common::MinimumMultiplier::get(); + let min_multiplier = MinimumMultiplier::get(); let call = pallet_balances::Call::::transfer_keep_alive { dest: Charlie.to_account_id().into(), value: Default::default(), diff --git a/runtime/rococo/constants/src/lib.rs b/runtime/rococo/constants/src/lib.rs index 3d8fa0fe5b3e..0b017672e94f 100644 --- a/runtime/rococo/constants/src/lib.rs +++ b/runtime/rococo/constants/src/lib.rs @@ -100,7 +100,7 @@ mod tests { fee::WeightToFee, }; use crate::weights::ExtrinsicBaseWeight; - use frame_support::weights::{DispatchClass, WeightToFeePolynomial}; + use frame_support::weights::WeightToFeePolynomial; use runtime_common::MAXIMUM_BLOCK_WEIGHT; #[test] From bcede286fd2186adce0a637af86558f397918c2b Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Wed, 9 Mar 2022 17:47:52 +0100 Subject: [PATCH 13/18] Spellcheck on Signed-off-by: Oliver Tale-Yazdi --- runtime/kusama/constants/src/weights/rocksdb_weights.rs | 2 +- runtime/polkadot/constants/src/weights/rocksdb_weights.rs | 2 +- runtime/rococo/constants/src/weights/rocksdb_weights.rs | 2 +- runtime/test-runtime/constants/src/weights/rocksdb_weights.rs | 2 +- runtime/westend/constants/src/weights/rocksdb_weights.rs | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/runtime/kusama/constants/src/weights/rocksdb_weights.rs b/runtime/kusama/constants/src/weights/rocksdb_weights.rs index f37964dcbd82..05e06b0eabe3 100644 --- a/runtime/kusama/constants/src/weights/rocksdb_weights.rs +++ b/runtime/kusama/constants/src/weights/rocksdb_weights.rs @@ -22,7 +22,7 @@ pub mod constants { }; parameter_types! { - /// By default, Substrate uses RocksDB, so this will be the weight used throughout + /// By default, Substrate uses `RocksDB`, so this will be the weight used throughout /// the runtime. pub const RocksDbWeight: RuntimeDbWeight = RuntimeDbWeight { read: 25_000 * constants::WEIGHT_PER_NANOS, diff --git a/runtime/polkadot/constants/src/weights/rocksdb_weights.rs b/runtime/polkadot/constants/src/weights/rocksdb_weights.rs index f37964dcbd82..05e06b0eabe3 100644 --- a/runtime/polkadot/constants/src/weights/rocksdb_weights.rs +++ b/runtime/polkadot/constants/src/weights/rocksdb_weights.rs @@ -22,7 +22,7 @@ pub mod constants { }; parameter_types! { - /// By default, Substrate uses RocksDB, so this will be the weight used throughout + /// By default, Substrate uses `RocksDB`, so this will be the weight used throughout /// the runtime. pub const RocksDbWeight: RuntimeDbWeight = RuntimeDbWeight { read: 25_000 * constants::WEIGHT_PER_NANOS, diff --git a/runtime/rococo/constants/src/weights/rocksdb_weights.rs b/runtime/rococo/constants/src/weights/rocksdb_weights.rs index f37964dcbd82..05e06b0eabe3 100644 --- a/runtime/rococo/constants/src/weights/rocksdb_weights.rs +++ b/runtime/rococo/constants/src/weights/rocksdb_weights.rs @@ -22,7 +22,7 @@ pub mod constants { }; parameter_types! { - /// By default, Substrate uses RocksDB, so this will be the weight used throughout + /// By default, Substrate uses `RocksDB`, so this will be the weight used throughout /// the runtime. pub const RocksDbWeight: RuntimeDbWeight = RuntimeDbWeight { read: 25_000 * constants::WEIGHT_PER_NANOS, diff --git a/runtime/test-runtime/constants/src/weights/rocksdb_weights.rs b/runtime/test-runtime/constants/src/weights/rocksdb_weights.rs index f37964dcbd82..05e06b0eabe3 100644 --- a/runtime/test-runtime/constants/src/weights/rocksdb_weights.rs +++ b/runtime/test-runtime/constants/src/weights/rocksdb_weights.rs @@ -22,7 +22,7 @@ pub mod constants { }; parameter_types! { - /// By default, Substrate uses RocksDB, so this will be the weight used throughout + /// By default, Substrate uses `RocksDB`, so this will be the weight used throughout /// the runtime. pub const RocksDbWeight: RuntimeDbWeight = RuntimeDbWeight { read: 25_000 * constants::WEIGHT_PER_NANOS, diff --git a/runtime/westend/constants/src/weights/rocksdb_weights.rs b/runtime/westend/constants/src/weights/rocksdb_weights.rs index f37964dcbd82..05e06b0eabe3 100644 --- a/runtime/westend/constants/src/weights/rocksdb_weights.rs +++ b/runtime/westend/constants/src/weights/rocksdb_weights.rs @@ -22,7 +22,7 @@ pub mod constants { }; parameter_types! { - /// By default, Substrate uses RocksDB, so this will be the weight used throughout + /// By default, Substrate uses `RocksDB`, so this will be the weight used throughout /// the runtime. pub const RocksDbWeight: RuntimeDbWeight = RuntimeDbWeight { read: 25_000 * constants::WEIGHT_PER_NANOS, From 85aca137b962da7df7eb0259ba4ed46fec26e093 Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Wed, 9 Mar 2022 17:53:10 +0100 Subject: [PATCH 14/18] Spellcheck on Signed-off-by: Oliver Tale-Yazdi --- runtime/kusama/constants/src/weights/paritydb_weights.rs | 2 +- runtime/polkadot/constants/src/weights/paritydb_weights.rs | 2 +- runtime/rococo/constants/src/weights/paritydb_weights.rs | 2 +- runtime/test-runtime/constants/src/weights/paritydb_weights.rs | 2 +- runtime/westend/constants/src/weights/paritydb_weights.rs | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/runtime/kusama/constants/src/weights/paritydb_weights.rs b/runtime/kusama/constants/src/weights/paritydb_weights.rs index 572187ba78a9..843823c1bf30 100644 --- a/runtime/kusama/constants/src/weights/paritydb_weights.rs +++ b/runtime/kusama/constants/src/weights/paritydb_weights.rs @@ -22,7 +22,7 @@ pub mod constants { }; parameter_types! { - /// ParityDB can be enabled with a feature flag, but is still experimental. These weights + /// `ParityDB` can be enabled with a feature flag, but is still experimental. These weights /// are available for brave runtime engineers who may want to try this out as default. pub const ParityDbWeight: RuntimeDbWeight = RuntimeDbWeight { read: 8_000 * constants::WEIGHT_PER_NANOS, diff --git a/runtime/polkadot/constants/src/weights/paritydb_weights.rs b/runtime/polkadot/constants/src/weights/paritydb_weights.rs index 572187ba78a9..843823c1bf30 100644 --- a/runtime/polkadot/constants/src/weights/paritydb_weights.rs +++ b/runtime/polkadot/constants/src/weights/paritydb_weights.rs @@ -22,7 +22,7 @@ pub mod constants { }; parameter_types! { - /// ParityDB can be enabled with a feature flag, but is still experimental. These weights + /// `ParityDB` can be enabled with a feature flag, but is still experimental. These weights /// are available for brave runtime engineers who may want to try this out as default. pub const ParityDbWeight: RuntimeDbWeight = RuntimeDbWeight { read: 8_000 * constants::WEIGHT_PER_NANOS, diff --git a/runtime/rococo/constants/src/weights/paritydb_weights.rs b/runtime/rococo/constants/src/weights/paritydb_weights.rs index 572187ba78a9..843823c1bf30 100644 --- a/runtime/rococo/constants/src/weights/paritydb_weights.rs +++ b/runtime/rococo/constants/src/weights/paritydb_weights.rs @@ -22,7 +22,7 @@ pub mod constants { }; parameter_types! { - /// ParityDB can be enabled with a feature flag, but is still experimental. These weights + /// `ParityDB` can be enabled with a feature flag, but is still experimental. These weights /// are available for brave runtime engineers who may want to try this out as default. pub const ParityDbWeight: RuntimeDbWeight = RuntimeDbWeight { read: 8_000 * constants::WEIGHT_PER_NANOS, diff --git a/runtime/test-runtime/constants/src/weights/paritydb_weights.rs b/runtime/test-runtime/constants/src/weights/paritydb_weights.rs index 572187ba78a9..843823c1bf30 100644 --- a/runtime/test-runtime/constants/src/weights/paritydb_weights.rs +++ b/runtime/test-runtime/constants/src/weights/paritydb_weights.rs @@ -22,7 +22,7 @@ pub mod constants { }; parameter_types! { - /// ParityDB can be enabled with a feature flag, but is still experimental. These weights + /// `ParityDB` can be enabled with a feature flag, but is still experimental. These weights /// are available for brave runtime engineers who may want to try this out as default. pub const ParityDbWeight: RuntimeDbWeight = RuntimeDbWeight { read: 8_000 * constants::WEIGHT_PER_NANOS, diff --git a/runtime/westend/constants/src/weights/paritydb_weights.rs b/runtime/westend/constants/src/weights/paritydb_weights.rs index 572187ba78a9..843823c1bf30 100644 --- a/runtime/westend/constants/src/weights/paritydb_weights.rs +++ b/runtime/westend/constants/src/weights/paritydb_weights.rs @@ -22,7 +22,7 @@ pub mod constants { }; parameter_types! { - /// ParityDB can be enabled with a feature flag, but is still experimental. These weights + /// `ParityDB` can be enabled with a feature flag, but is still experimental. These weights /// are available for brave runtime engineers who may want to try this out as default. pub const ParityDbWeight: RuntimeDbWeight = RuntimeDbWeight { read: 8_000 * constants::WEIGHT_PER_NANOS, From 90fdabda10c88cf70ff4c113848877fe31f94ea3 Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Wed, 9 Mar 2022 18:04:49 +0100 Subject: [PATCH 15/18] Spellcheck Signed-off-by: Oliver Tale-Yazdi --- runtime/common/src/lib.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/runtime/common/src/lib.rs b/runtime/common/src/lib.rs index 5c5d2d7c234b..15d80703c760 100644 --- a/runtime/common/src/lib.rs +++ b/runtime/common/src/lib.rs @@ -74,9 +74,6 @@ const_assert!(NORMAL_DISPATCH_RATIO.deconstruct() >= AVERAGE_ON_INITIALIZE_RATIO /// It expects the passed runtime constants to contain a `weights` module. /// The generated weight types were formerly part of the common /// runtime but are now runtime dependant. -/// -/// Example: -/// impl_runtime_weights!(polkadot_runtime_constants); #[macro_export] macro_rules! impl_runtime_weights { ($runtime:ident) => { From 0ed2949e2ce7a03a73250bc68ccc34e5ee69eef7 Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Wed, 9 Mar 2022 20:19:59 +0100 Subject: [PATCH 16/18] Re-Export weight constants in the runtime Signed-off-by: Oliver Tale-Yazdi --- runtime/common/src/lib.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/runtime/common/src/lib.rs b/runtime/common/src/lib.rs index 15d80703c760..ccf4f68b5311 100644 --- a/runtime/common/src/lib.rs +++ b/runtime/common/src/lib.rs @@ -90,6 +90,11 @@ macro_rules! impl_runtime_weights { // Implement tests for the weight multiplier. runtime_common::impl_multiplier_tests!(); + // Expose the weight from the runtime constants module. + pub weights::constants::{ + BlockExecutionWeight, ExtrinsicBaseWeight, ParityDbWeight, RocksDbWeight, + }; + // Common constants used in all runtimes. parameter_types! { pub const BlockHashCount: BlockNumber = 2400; From 749c669862705660183dadadc40eff56630abadb Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Wed, 9 Mar 2022 22:28:59 +0000 Subject: [PATCH 17/18] Prepare for cumulus companion Signed-off-by: Oliver Tale-Yazdi --- node/test/service/src/lib.rs | 1 - runtime/common/src/lib.rs | 14 ++++++-------- runtime/kusama/src/lib.rs | 2 +- runtime/polkadot/src/lib.rs | 2 +- runtime/rococo/src/lib.rs | 2 +- runtime/westend/src/lib.rs | 2 +- 6 files changed, 10 insertions(+), 13 deletions(-) diff --git a/node/test/service/src/lib.rs b/node/test/service/src/lib.rs index cb78e9c66935..dbcbaad14431 100644 --- a/node/test/service/src/lib.rs +++ b/node/test/service/src/lib.rs @@ -26,7 +26,6 @@ use polkadot_node_primitives::{CollationGenerationConfig, CollatorFn}; use polkadot_node_subsystem::messages::{CollationGenerationMessage, CollatorProtocolMessage}; use polkadot_overseer::Handle; use polkadot_primitives::v2::{Balance, CollatorPair, HeadData, Id as ParaId, ValidationCode}; -use polkadot_runtime_common::BlockHashCount; use polkadot_runtime_parachains::paras::ParaGenesisArgs; use polkadot_service::{ ClientHandle, Error, ExecuteWithClient, FullClient, IsCollator, NewFull, PrometheusConfig, diff --git a/runtime/common/src/lib.rs b/runtime/common/src/lib.rs index ca0097a3edec..074e7ac71bfb 100644 --- a/runtime/common/src/lib.rs +++ b/runtime/common/src/lib.rs @@ -41,10 +41,8 @@ use frame_support::{ traits::{ConstU32, Currency, OneSessionHandler}, weights::{constants::WEIGHT_PER_SECOND, Weight}, }; -use frame_system::limits; -use pallet_transaction_payment::{Multiplier, TargetedFeeAdjustment}; -use primitives::v2::{AssignmentId, BlockNumber, ValidatorId}; -use sp_runtime::{FixedPointNumber, Perbill, Perquintill}; +use primitives::v2::{AssignmentId, ValidatorId}; +use sp_runtime::{Perbill}; use static_assertions::const_assert; pub use pallet_balances::Call as BalancesCall; @@ -83,17 +81,17 @@ macro_rules! impl_runtime_weights { use frame_system::limits; use pallet_transaction_payment::{Multiplier, TargetedFeeAdjustment}; pub use runtime_common::{ - AVERAGE_ON_INITIALIZE_RATIO, MAXIMUM_BLOCK_WEIGHT, NORMAL_DISPATCH_RATIO, + AVERAGE_ON_INITIALIZE_RATIO, MAXIMUM_BLOCK_WEIGHT, NORMAL_DISPATCH_RATIO, impl_multiplier_tests, impl_elections_weights, }; use sp_runtime::{FixedPointNumber, Perquintill}; // Implement the weight types of the elections module. - runtime_common::impl_elections_weights!($runtime); + impl_elections_weights!($runtime); // Implement tests for the weight multiplier. - runtime_common::impl_multiplier_tests!(); + impl_multiplier_tests!(); // Expose the weight from the runtime constants module. - pub weights::constants::{ + pub use $runtime::weights::{ BlockExecutionWeight, ExtrinsicBaseWeight, ParityDbWeight, RocksDbWeight, }; diff --git a/runtime/kusama/src/lib.rs b/runtime/kusama/src/lib.rs index abbb5a013a29..d5b48d403837 100644 --- a/runtime/kusama/src/lib.rs +++ b/runtime/kusama/src/lib.rs @@ -84,7 +84,7 @@ pub use pallet_timestamp::Call as TimestampCall; pub use sp_runtime::BuildStorage; /// Constant values used within the runtime. -use kusama_runtime_constants::{currency::*, fee::*, time::*, weights::RocksDbWeight}; +use kusama_runtime_constants::{currency::*, fee::*, time::*}; // Weights used in the runtime. mod weights; diff --git a/runtime/polkadot/src/lib.rs b/runtime/polkadot/src/lib.rs index 3ca6e23020ca..d1cb5dcbae89 100644 --- a/runtime/polkadot/src/lib.rs +++ b/runtime/polkadot/src/lib.rs @@ -86,7 +86,7 @@ pub use pallet_timestamp::Call as TimestampCall; pub use sp_runtime::BuildStorage; /// Constant values used within the runtime. -use polkadot_runtime_constants::{currency::*, fee::*, time::*, weights::RocksDbWeight}; +use polkadot_runtime_constants::{currency::*, fee::*, time::*}; // Weights used in the runtime. mod weights; diff --git a/runtime/rococo/src/lib.rs b/runtime/rococo/src/lib.rs index 4516cef3b0f7..4eae99e240c9 100644 --- a/runtime/rococo/src/lib.rs +++ b/runtime/rococo/src/lib.rs @@ -80,7 +80,7 @@ pub use pallet_balances::Call as BalancesCall; use polkadot_parachain::primitives::Id as ParaId; /// Constant values used within the runtime. -use rococo_runtime_constants::{currency::*, fee::*, time::*, weights::RocksDbWeight}; +use rococo_runtime_constants::{currency::*, fee::*, time::*}; use frame_support::traits::{InstanceFilter, OnRuntimeUpgrade}; diff --git a/runtime/westend/src/lib.rs b/runtime/westend/src/lib.rs index fc16a4c06e9a..2ad92134ef21 100644 --- a/runtime/westend/src/lib.rs +++ b/runtime/westend/src/lib.rs @@ -82,7 +82,7 @@ pub use pallet_timestamp::Call as TimestampCall; pub use sp_runtime::BuildStorage; /// Constant values used within the runtime. -use westend_runtime_constants::{currency::*, fee::*, time::*, weights::RocksDbWeight}; +use westend_runtime_constants::{currency::*, fee::*, time::*}; // Weights used in the runtime mod weights; From a564113a74a3b246a49970811211c42ec6ac6799 Mon Sep 17 00:00:00 2001 From: Oliver Tale-Yazdi Date: Wed, 9 Mar 2022 23:58:13 +0100 Subject: [PATCH 18/18] Fmt Signed-off-by: Oliver Tale-Yazdi --- runtime/common/src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/runtime/common/src/lib.rs b/runtime/common/src/lib.rs index 074e7ac71bfb..40dee1a735db 100644 --- a/runtime/common/src/lib.rs +++ b/runtime/common/src/lib.rs @@ -42,7 +42,7 @@ use frame_support::{ weights::{constants::WEIGHT_PER_SECOND, Weight}, }; use primitives::v2::{AssignmentId, ValidatorId}; -use sp_runtime::{Perbill}; +use sp_runtime::Perbill; use static_assertions::const_assert; pub use pallet_balances::Call as BalancesCall; @@ -81,7 +81,8 @@ macro_rules! impl_runtime_weights { use frame_system::limits; use pallet_transaction_payment::{Multiplier, TargetedFeeAdjustment}; pub use runtime_common::{ - AVERAGE_ON_INITIALIZE_RATIO, MAXIMUM_BLOCK_WEIGHT, NORMAL_DISPATCH_RATIO, impl_multiplier_tests, impl_elections_weights, + impl_elections_weights, impl_multiplier_tests, AVERAGE_ON_INITIALIZE_RATIO, + MAXIMUM_BLOCK_WEIGHT, NORMAL_DISPATCH_RATIO, }; use sp_runtime::{FixedPointNumber, Perquintill};