Skip to content

Commit

Permalink
storage weights integrated
Browse files Browse the repository at this point in the history
  • Loading branch information
Ellenn-A committed Sep 8, 2023
1 parent 053dd93 commit 5412b7c
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 45 deletions.
9 changes: 9 additions & 0 deletions pallets/symmetric-key/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,12 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
.saturating_add(T::DbWeight::get().writes(1))
}
}

impl WeightInfo for () {
fn update_key() -> Weight {
Weight::from_parts(0, 0)
}
fn rotate_key() -> Weight {
Weight::from_parts(0, 0)
}
}
6 changes: 6 additions & 0 deletions pallets/utxo-nft/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,9 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
.saturating_add(Weight::from_parts(0, 9436).saturating_mul(i.into()))
}
}

impl WeightInfo for () {
fn run_process(_: u32, _: u32) -> Weight {
Weight::from_parts(0, 0)
}
}
44 changes: 0 additions & 44 deletions runtime/src/constants.rs

This file was deleted.

2 changes: 2 additions & 0 deletions runtime/src/constants/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod time;
pub mod weights;
43 changes: 43 additions & 0 deletions runtime/src/constants/time.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

use crate::{BlockNumber, Moment};

/// Since BABE is probabilistic this is the average expected block time that
/// we are targeting. Blocks will be produced at a minimum duration defined
/// by `SLOT_DURATION`, but some slots will not be allocated to any
/// authority and hence no block will be produced. We expect to have this
/// block time on average following the defined slot duration and the value
/// of `c` configured for BABE (where `1 - c` represents the probability of
/// a slot being empty).
/// This value is only used indirectly to define the unit constants below
/// that are expressed in blocks. The rest of the code should use
/// `SLOT_DURATION` instead (like the Timestamp pallet for calculating the
/// minimum period).
///
/// If using BABE with secondary slots (default) then all of the slots will
/// always be assigned, in which case `MILLISECS_PER_BLOCK` and
/// `SLOT_DURATION` should have the same value.
///
/// <https://research.web3.foundation/en/latest/polkadot/block-production/Babe.html#-6.-practical-results>
pub const MILLISECS_PER_BLOCK: Moment = 6000;
pub const SECS_PER_BLOCK: Moment = MILLISECS_PER_BLOCK / 1000;

// NOTE: Currently it is not possible to change the slot duration after the chain has started.
// Attempting to do so will brick block production.
pub const SLOT_DURATION: Moment = MILLISECS_PER_BLOCK;

// 1 in 4 blocks (on average, not counting collisions) will be primary BABE blocks.
pub const PRIMARY_PROBABILITY: (u64, u64) = (1, 4);

// NOTE: Currently it is not possible to change the epoch duration after the chain has started.
// Attempting to do so will brick block production.
pub const EPOCH_DURATION_IN_BLOCKS: BlockNumber = 10 * MINUTES;
pub const EPOCH_DURATION_IN_SLOTS: u64 = {
const SLOT_FILL_RATE: f64 = MILLISECS_PER_BLOCK as f64 / SLOT_DURATION as f64;

(EPOCH_DURATION_IN_BLOCKS as f64 * SLOT_FILL_RATE) as u64
};

// These time units are defined in number of blocks.
pub const MINUTES: BlockNumber = 60 / (SECS_PER_BLOCK as BlockNumber);
pub const HOURS: BlockNumber = MINUTES * 60;
pub const DAYS: BlockNumber = HOURS * 24;
69 changes: 69 additions & 0 deletions runtime/src/constants/weights.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use frame_support::weights::{constants, RuntimeDbWeight};
use sp_core::parameter_types;

parameter_types! {
/// By default, Substrate uses `RocksDB`, so this will be the weight used throughout
/// the runtime.
pub const RocksDbWeight: RuntimeDbWeight = RuntimeDbWeight {
/// Time to read one storage item.
/// Calculated by multiplying the *Average* of all values with `1.0` and adding `0`.
///
/// Stats nanoseconds:
/// Min, Max: 4_030, 54_531
/// Average: 7_338
/// Median: 5_630
/// Std-Dev: 7759.74
///
/// Percentiles nanoseconds:
/// 99th: 54_531
/// 95th: 9_000
/// 75th: 6_851
read: 7_338 * constants::WEIGHT_REF_TIME_PER_NANOS,

/// Time to write one storage item.
/// Calculated by multiplying the *Average* of all values with `1.0` and adding `0`.
///
/// Stats nanoseconds:
/// Min, Max: 15_450, 2_179_245
/// Average: 87_849
/// Median: 28_621
/// Std-Dev: 339729.37
///
/// Percentiles nanoseconds:
/// 99th: 2_179_245
/// 95th: 126_782
/// 75th: 35_410
write: 87_849 * constants::WEIGHT_REF_TIME_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 bound() {
// At least 1 µs.
assert!(
W::get().reads(1).ref_time() >= constants::WEIGHT_REF_TIME_PER_MICROS,
"Read weight should be at least 1 µs."
);
assert!(
W::get().writes(1).ref_time() >= constants::WEIGHT_REF_TIME_PER_MICROS,
"Write weight should be at least 1 µs."
);
// At most 1 ms.
assert!(
W::get().reads(1).ref_time() <= constants::WEIGHT_REF_TIME_PER_MILLIS,
"Read weight should be at most 1 ms."
);
assert!(
W::get().writes(1).ref_time() <= constants::WEIGHT_REF_TIME_PER_MILLIS,
"Write weight should be at most 1 ms."
);
}
}
2 changes: 1 addition & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ impl frame_system::Config for Runtime {
/// Maximum number of block number to block hash mappings to keep (oldest pruned first).
type BlockHashCount = BlockHashCount;
/// The weight of database operations that the runtime can invoke.
type DbWeight = RocksDbWeight;
type DbWeight = constants::weights::RocksDbWeight;
/// Version of the runtime.
type Version = Version;
/// Converts a module to the index of the module in `construct_runtime!`.
Expand Down

0 comments on commit 5412b7c

Please sign in to comment.