Skip to content

Commit

Permalink
Fix docs
Browse files Browse the repository at this point in the history
  • Loading branch information
0xLucca committed Jan 9, 2025
1 parent e15079f commit dac5d3e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 234 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -22,9 +22,9 @@ mod runtime {

#[runtime::pallet_index(0)]
pub type System = frame_system::Pallet<Test>;

#[runtime::pallet_index(1)]
pub type CustomPallet = pallet_custom::Pallet<Test>;
pub type CustomPallet = custom_pallet::Pallet<Test>;
}

// System pallet configuration
Expand All @@ -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>;
}

// Test externalities initialization
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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<T>, new_value: u32) -> DispatchResult {
ensure_root(origin)?;
ensure!(
new_value <= T::CounterMaxValue::get(),
Error::<T>::CounterValueExceedsMax
);
CounterValue::<T>::put(new_value);
Self::deposit_event(Event::<T>::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.
Expand Down

0 comments on commit dac5d3e

Please sign in to comment.