diff --git a/pallets/qpow/src/benchmarking.rs b/pallets/qpow/src/benchmarking.rs index c9d01f54..4216fdaa 100644 --- a/pallets/qpow/src/benchmarking.rs +++ b/pallets/qpow/src/benchmarking.rs @@ -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::::from(1000u32); frame_system::Pallet::::set_block_number(block_number); @@ -31,6 +32,9 @@ mod benchmarks { pallet_timestamp::Pallet::::set_timestamp(now); >::put(now.saturating_sub(T::TargetBlockTime::get())); + // Initialize EMA + >::put(T::TargetBlockTime::get()); + #[block] { QPoW::::on_finalize(block_number); diff --git a/pallets/qpow/src/lib.rs b/pallets/qpow/src/lib.rs index eb904fb7..e34096e6 100644 --- a/pallets/qpow/src/lib.rs +++ b/pallets/qpow/src/lib.rs @@ -120,12 +120,12 @@ pub mod pallet { #[pallet::hooks] impl Hooks> for Pallet { - // TODO: update this fn on_initialize(_block_number: BlockNumberFor) -> Weight { - ::WeightInfo::on_finalize_max_history() + ::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) { let current_difficulty = >::get(); log::debug!(target: "qpow", diff --git a/pallets/qpow/src/weights.rs b/pallets/qpow/src/weights.rs index ce3e5b88..545e68a1 100644 --- a/pallets/qpow/src/weights.rs +++ b/pallets/qpow/src/weights.rs @@ -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(PhantomData); impl WeightInfo for SubstrateWeight { - /// 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)) } }