Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions pallets/qpow/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ use sp_runtime::traits::Get;
mod benchmarks {
use super::*;

/// Benchmark for the on_finalize hook which performs EMA-based difficulty adjustment.
#[benchmark]
fn on_finalize_max_history() {
// Setup state with maximum history size to test worst-case scenario
fn on_finalize() {
// Setup state with typical block for difficulty adjustment
let block_number = BlockNumberFor::<T>::from(1000u32);
frame_system::Pallet::<T>::set_block_number(block_number);

Expand All @@ -31,6 +32,9 @@ mod benchmarks {
pallet_timestamp::Pallet::<T>::set_timestamp(now);
<LastBlockTime<T>>::put(now.saturating_sub(T::TargetBlockTime::get()));

// Initialize EMA
<BlockTimeEma<T>>::put(T::TargetBlockTime::get());

#[block]
{
QPoW::<T>::on_finalize(block_number);
Expand Down
6 changes: 3 additions & 3 deletions pallets/qpow/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@ pub mod pallet {

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
// TODO: update this
fn on_initialize(_block_number: BlockNumberFor<T>) -> Weight {
<T as crate::Config>::WeightInfo::on_finalize_max_history()
<T as crate::Config>::WeightInfo::on_finalize()
}

/// Called at the end of each block.
/// Called at the end of each block to adjust mining difficulty based on
/// observed block times using Exponential Moving Average (EMA).
fn on_finalize(block_number: BlockNumberFor<T>) {
let current_difficulty = <CurrentDifficulty<T>>::get();
log::debug!(target: "qpow",
Expand Down
42 changes: 21 additions & 21 deletions pallets/qpow/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,52 +51,52 @@ use core::marker::PhantomData;

/// Weight functions needed for `pallet_qpow`.
pub trait WeightInfo {
fn on_finalize_max_history() -> Weight;
fn on_finalize() -> Weight;
}

/// Weights for `pallet_qpow` using the Substrate node and recommended hardware.
pub struct SubstrateWeight<T>(PhantomData<T>);
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
/// Storage: `QPoW::CurrentDifficulty` (r:1 w:1)
/// Storage: `QPoW::CurrentDifficulty` (r:2 w:1)
/// Proof: `QPoW::CurrentDifficulty` (`max_values`: Some(1), `max_size`: Some(64), added: 559, mode: `MaxEncodedLen`)
/// Storage: `Timestamp::Now` (r:1 w:0)
/// Proof: `Timestamp::Now` (`max_values`: Some(1), `max_size`: Some(8), added: 503, mode: `MaxEncodedLen`)
/// Storage: `QPoW::LastBlockTime` (r:1 w:1)
/// Proof: `QPoW::LastBlockTime` (`max_values`: Some(1), `max_size`: Some(8), added: 503, mode: `MaxEncodedLen`)
/// Storage: `QPoW::TotalWork` (r:1 w:1)
/// Proof: `QPoW::TotalWork` (`max_values`: Some(1), `max_size`: Some(64), added: 559, mode: `MaxEncodedLen`)
/// Storage: `QPoW::BlockTimeEma` (r:1 w:1)
/// Proof: `QPoW::BlockTimeEma` (`max_values`: Some(1), `max_size`: Some(8), added: 503, mode: `MaxEncodedLen`)
/// Storage: `QPoW::LastBlockDuration` (r:0 w:1)
/// Proof: `QPoW::LastBlockDuration` (`max_values`: Some(1), `max_size`: Some(8), added: 503, mode: `MaxEncodedLen`)
fn on_finalize_max_history() -> Weight {
fn on_finalize() -> Weight {
// Proof Size summary in bytes:
// Measured: `707`
// Estimated: `23445`
// Minimum execution time: 213_000_000 picoseconds.
Weight::from_parts(217_000_000, 23445)
.saturating_add(T::DbWeight::get().reads(16_u64))
.saturating_add(T::DbWeight::get().writes(9_u64))
// Measured: `680`
// Estimated: `1549`
// Minimum execution time: 106_000_000 picoseconds.
Weight::from_parts(109_000_000, 1549)
.saturating_add(T::DbWeight::get().reads(5_u64))
.saturating_add(T::DbWeight::get().writes(4_u64))
}
}

// For backwards compatibility and tests.
impl WeightInfo for () {
/// Storage: `QPoW::CurrentDifficulty` (r:1 w:1)
/// Storage: `QPoW::CurrentDifficulty` (r:2 w:1)
/// Proof: `QPoW::CurrentDifficulty` (`max_values`: Some(1), `max_size`: Some(64), added: 559, mode: `MaxEncodedLen`)
/// Storage: `Timestamp::Now` (r:1 w:0)
/// Proof: `Timestamp::Now` (`max_values`: Some(1), `max_size`: Some(8), added: 503, mode: `MaxEncodedLen`)
/// Storage: `QPoW::LastBlockTime` (r:1 w:1)
/// Proof: `QPoW::LastBlockTime` (`max_values`: Some(1), `max_size`: Some(8), added: 503, mode: `MaxEncodedLen`)
/// Storage: `QPoW::TotalWork` (r:1 w:1)
/// Proof: `QPoW::TotalWork` (`max_values`: Some(1), `max_size`: Some(64), added: 559, mode: `MaxEncodedLen`)
/// Storage: `QPoW::BlockTimeEma` (r:1 w:1)
/// Proof: `QPoW::BlockTimeEma` (`max_values`: Some(1), `max_size`: Some(8), added: 503, mode: `MaxEncodedLen`)
/// Storage: `QPoW::LastBlockDuration` (r:0 w:1)
/// Proof: `QPoW::LastBlockDuration` (`max_values`: Some(1), `max_size`: Some(8), added: 503, mode: `MaxEncodedLen`)
fn on_finalize_max_history() -> Weight {
fn on_finalize() -> Weight {
// Proof Size summary in bytes:
// Measured: `707`
// Estimated: `23445`
// Minimum execution time: 213_000_000 picoseconds.
Weight::from_parts(217_000_000, 23445)
.saturating_add(RocksDbWeight::get().reads(16_u64))
.saturating_add(RocksDbWeight::get().writes(9_u64))
// Measured: `680`
// Estimated: `1549`
// Minimum execution time: 106_000_000 picoseconds.
Weight::from_parts(109_000_000, 1549)
.saturating_add(RocksDbWeight::get().reads(5_u64))
.saturating_add(RocksDbWeight::get().writes(4_u64))
}
}
Loading