From dac5d3e0ea272bf481599e227b2dc3d7be593dac Mon Sep 17 00:00:00 2001 From: 0xLucca <0xlucca.dev@gmail.com> Date: Thu, 9 Jan 2025 12:46:33 -0300 Subject: [PATCH] Fix docs --- .../zero-to-hero/pallet-benchmarking/lib.rs | 199 ------------------ .../pallets/custom-pallet/src/mock.rs | 9 +- .../zero-to-hero/pallet-benchmarking.md | 45 ++-- 3 files changed, 19 insertions(+), 234 deletions(-) delete mode 100644 .snippets/code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking/lib.rs diff --git a/.snippets/code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking/lib.rs b/.snippets/code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking/lib.rs deleted file mode 100644 index 7405ce1d5..000000000 --- a/.snippets/code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking/lib.rs +++ /dev/null @@ -1,199 +0,0 @@ -#![cfg_attr(not(feature = "std"), no_std)] - -pub use pallet::*; - -#[cfg(test)] -mod mock; - -#[cfg(test)] -mod tests; - -#[cfg(feature = "runtime-benchmarks")] -mod benchmarking; - -pub mod weights; -use crate::weights::WeightInfo; - -#[frame_support::pallet(dev_mode)] -pub mod pallet { - use super::*; - use frame_support::pallet_prelude::*; - use frame_system::pallet_prelude::*; - - #[pallet::pallet] - pub struct Pallet(_); - - // Configuration trait for the pallet. - #[pallet::config] - pub trait Config: frame_system::Config { - // Defines the event type for the pallet. - type RuntimeEvent: From> + IsType<::RuntimeEvent>; - - // Defines the maximum value the counter can hold. - #[pallet::constant] - type CounterMaxValue: Get; - - /// A type representing the weights required by the dispatchables of this pallet. - type WeightInfo: WeightInfo; - } - - #[pallet::event] - #[pallet::generate_deposit(pub(super) fn deposit_event)] - pub enum Event { - /// The counter value has been set to a new value by Root. - CounterValueSet { - /// The new value set. - counter_value: u32, - }, - /// A user has successfully incremented the counter. - CounterIncremented { - /// The new value set. - counter_value: u32, - /// The account who incremented the counter. - who: T::AccountId, - /// The amount by which the counter was incremented. - incremented_amount: u32, - }, - /// A user has successfully decremented the counter. - CounterDecremented { - /// The new value set. - counter_value: u32, - /// The account who decremented the counter. - who: T::AccountId, - /// The amount by which the counter was decremented. - decremented_amount: u32, - }, - } - - /// Storage for the current value of the counter. - #[pallet::storage] - pub type CounterValue = StorageValue<_, u32>; - - /// Storage map to track the number of interactions performed by each account. - #[pallet::storage] - pub type UserInteractions = StorageMap<_, Twox64Concat, T::AccountId, u32>; - - #[pallet::error] - pub enum Error { - /// The counter value exceeds the maximum allowed value. - CounterValueExceedsMax, - /// The counter value cannot be decremented below zero. - CounterValueBelowZero, - /// Overflow occurred in the counter. - CounterOverflow, - /// Overflow occurred in user interactions. - UserInteractionOverflow, - } - - #[pallet::call] - impl Pallet { - /// Set the value of the counter. - /// - /// The dispatch origin of this call must be _Root_. - /// - /// - `new_value`: The new value to set for the counter. - /// - /// Emits `CounterValueSet` event when successful. - #[pallet::call_index(0)] - #[pallet::weight(T::WeightInfo::set_counter_value())] - pub fn set_counter_value(origin: OriginFor, new_value: u32) -> DispatchResult { - ensure_root(origin)?; - - ensure!( - new_value <= T::CounterMaxValue::get(), - Error::::CounterValueExceedsMax - ); - - CounterValue::::put(new_value); - - Self::deposit_event(Event::::CounterValueSet { - counter_value: new_value, - }); - - Ok(()) - } - - /// Increment the counter by a specified amount. - /// - /// This function can be called by any signed account. - /// - /// - `amount_to_increment`: The amount by which to increment the counter. - /// - /// Emits `CounterIncremented` event when successful. - #[pallet::call_index(1)] - #[pallet::weight(T::WeightInfo::increment())] - pub fn increment(origin: OriginFor, amount_to_increment: u32) -> DispatchResult { - let who = ensure_signed(origin)?; - - let current_value = CounterValue::::get().unwrap_or(0); - - let new_value = current_value - .checked_add(amount_to_increment) - .ok_or(Error::::CounterOverflow)?; - - ensure!( - new_value <= T::CounterMaxValue::get(), - Error::::CounterValueExceedsMax - ); - - CounterValue::::put(new_value); - - UserInteractions::::try_mutate(&who, |interactions| -> Result<_, Error> { - let new_interactions = interactions - .unwrap_or(0) - .checked_add(1) - .ok_or(Error::::UserInteractionOverflow)?; - *interactions = Some(new_interactions); // Store the new value. - - Ok(()) - })?; - - Self::deposit_event(Event::::CounterIncremented { - counter_value: new_value, - who, - incremented_amount: amount_to_increment, - }); - - Ok(()) - } - - /// Decrement the counter by a specified amount. - /// - /// This function can be called by any signed account. - /// - /// - `amount_to_decrement`: The amount by which to decrement the counter. - /// - /// Emits `CounterDecremented` event when successful. - #[pallet::call_index(2)] - #[pallet::weight(T::WeightInfo::decrement())] - pub fn decrement(origin: OriginFor, amount_to_decrement: u32) -> DispatchResult { - let who = ensure_signed(origin)?; - - let current_value = CounterValue::::get().unwrap_or(0); - - let new_value = current_value - .checked_sub(amount_to_decrement) - .ok_or(Error::::CounterValueBelowZero)?; - - CounterValue::::put(new_value); - - UserInteractions::::try_mutate(&who, |interactions| -> Result<_, Error> { - let new_interactions = interactions - .unwrap_or(0) - .checked_add(1) - .ok_or(Error::::UserInteractionOverflow)?; - *interactions = Some(new_interactions); // Store the new value. - - Ok(()) - })?; - - Self::deposit_event(Event::::CounterDecremented { - counter_value: new_value, - who, - decremented_amount: amount_to_decrement, - }); - - Ok(()) - } - } -} diff --git a/.snippets/code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking/pallets/custom-pallet/src/mock.rs b/.snippets/code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking/pallets/custom-pallet/src/mock.rs index ac39e24cb..98ac7c05f 100644 --- a/.snippets/code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking/pallets/custom-pallet/src/mock.rs +++ b/.snippets/code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking/pallets/custom-pallet/src/mock.rs @@ -1,4 +1,4 @@ -use crate as pallet_custom; +use crate as custom_pallet; use frame_support::{derive_impl, parameter_types}; use sp_runtime::BuildStorage; @@ -22,9 +22,9 @@ mod runtime { #[runtime::pallet_index(0)] pub type System = frame_system::Pallet; - + #[runtime::pallet_index(1)] - pub type CustomPallet = pallet_custom::Pallet; + pub type CustomPallet = custom_pallet::Pallet; } // System pallet configuration @@ -38,9 +38,10 @@ parameter_types! { pub const CounterMaxValue: u32 = 10; } -impl pallet_custom::Config for Test { +impl custom_pallet::Config for Test { type RuntimeEvent = RuntimeEvent; type CounterMaxValue = CounterMaxValue; + type WeightInfo = custom_pallet::weights::SubstrateWeight; } // Test externalities initialization diff --git a/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking.md b/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking.md index 1c504ecaf..936c54abc 100644 --- a/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking.md +++ b/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking.md @@ -29,32 +29,32 @@ Follow these steps to prepare your environment for pallet benchmarking: 1. Add the [`frame-benchmarking`](https://docs.rs/frame-benchmarking/latest/frame_benchmarking/){target=\_blank} dependency: ```toml hl_lines="3" - --8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking/pallet-cargo.toml:10:10' + --8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking/pallets/custom-pallet/Cargo.toml:10:10' ... - --8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking/pallet-cargo.toml:15:15' + --8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking/pallets/custom-pallet/Cargo.toml:15:15' ``` 2. Enable benchmarking in the `std` features: ```toml hl_lines="6" - --8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking/pallet-cargo.toml:24:30' + --8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking/pallets/custom-pallet/Cargo.toml:24:30' ``` 3. Add the `runtime-benchmarks` feature flag: ```toml hl_lines="3-8" - --8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking/pallet-cargo.toml:22:22' + --8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking/pallets/custom-pallet/Cargo.toml:22:22' ... - --8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking/pallet-cargo.toml:31:36' + --8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking/pallets/custom-pallet/Cargo.toml:31:36' ``` 3. Add your pallet to the runtime's benchmark configuration: 1. Register your pallet in `runtime/src/benchmarks.rs`: ```rust hl_lines="11" - --8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking/benchmarks.rs:26:37' + --8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking/runtime/src/benchmarks.rs:26:37' ``` 2. Enable runtime benchmarking for your pallet in `runtime/Cargo.toml`: ```toml hl_lines="25" - --8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking/runtime-cargo.toml:136:161' + --8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking/runtime/Cargo.toml:136:161' ``` 4. Set up the benchmarking module in your pallet: @@ -84,7 +84,7 @@ Every benchmark test must follow a three-step pattern: Check the following example on how to benchmark the `increment` extrinsic: ```rust ---8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking/benchmarking.rs:23:37' +--8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking/pallets/custom-pallet/src/benchmarking.rs:23:37' ``` This benchmark test: @@ -98,7 +98,7 @@ This example demonstrates how to properly set up state, execute an extrinsic, an Now, implement the complete set of benchmark tests. Copy the following content in the `benchmarking.rs` file: ```rust ---8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking/benchmarking.rs' +--8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking//pallets/custom-pallet/src/benchmarking.rs' ``` !!!note @@ -150,48 +150,31 @@ After generating the weight calculations, you need to integrate these weights in First, add the necessary module imports to your pallet. These imports make the weights available to your code: ```rust hl_lines="4-5" ---8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking/lib.rs:11:15' +--8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking/pallets/custom-pallet/src/lib.rs:11:15' ``` Next, update your pallet's `Config` trait to include weight information. Define the `WeightInfo` type: ```rust hl_lines="11-12" ---8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking/lib.rs:26:38' +--8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking/pallets/custom-pallet/src/lib.rs:26:38' ``` Now you can assign weights to your extrinsics. Here's how to add weight calculations to the `set_counter_value` function: ```rust hl_lines="2" -#[pallet::call_index(0)] -#[pallet::weight(T::WeightInfo::set_counter_value())] -pub fn set_counter_value(origin: OriginFor, new_value: u32) -> DispatchResult { - ensure_root(origin)?; - - ensure!( - new_value <= T::CounterMaxValue::get(), - Error::::CounterValueExceedsMax - ); - - CounterValue::::put(new_value); - - Self::deposit_event(Event::::CounterValueSet { - counter_value: new_value, - }); - - Ok(()) -} +--8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking/pallets/custom-pallet/src/lib.rs:97:114' ``` For testing purposes, you need to implement the weight calculations in your mock runtime. Open `custom-pallet/src/mock.rs` and add: ```rust hl_lines="4" ---8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking/mock.rs:41:45' +--8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking/pallets/custom-pallet/src/mock.rs:41:45' ``` Finally, configure the actual weight values in your production runtime. In `runtime/src/config/mod.rs`, add: ```rust hl_lines="5" ---8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking/mod.rs:327:332' +--8<-- 'code/tutorials/polkadot-sdk/parachains/zero-to-hero/pallet-benchmarking/runtime/src/configs/mod.rs:327:332' ``` Your pallet is now complete with full testing and benchmarking support, ready for production use.