Skip to content

Commit

Permalink
Migrate pallet-transaction-storage and pallet-indices to benchmark v2 (
Browse files Browse the repository at this point in the history
…paritytech#6290)

Part of:
paritytech#6202

---------

Co-authored-by: Giuseppe Re <[email protected]>
Co-authored-by: GitHub Action <[email protected]>
  • Loading branch information
3 people authored Nov 8, 2024
1 parent 8c8f339 commit edf79aa
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 44 deletions.
11 changes: 11 additions & 0 deletions prdoc/pr_6290.prdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
title: Migrate pallet-transaction-storage and pallet-indices to benchmark v2
doc:
- audience: Runtime Dev
description: |-
Part of:
#6202
crates:
- name: pallet-indices
bump: patch
- name: pallet-transaction-storage
bump: patch
70 changes: 45 additions & 25 deletions substrate/frame/indices/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,31 @@

#![cfg(feature = "runtime-benchmarks")]

use super::*;
use frame_benchmarking::v1::{account, benchmarks, whitelisted_caller};
use crate::*;
use frame_benchmarking::v2::*;
use frame_system::RawOrigin;
use sp_runtime::traits::Bounded;

use crate::Pallet as Indices;

const SEED: u32 = 0;

benchmarks! {
claim {
#[benchmarks]
mod benchmarks {
use super::*;

#[benchmark]
fn claim() {
let account_index = T::AccountIndex::from(SEED);
let caller: T::AccountId = whitelisted_caller();
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
}: _(RawOrigin::Signed(caller.clone()), account_index)
verify {

#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), account_index);

assert_eq!(Accounts::<T>::get(account_index).unwrap().0, caller);
}

transfer {
#[benchmark]
fn transfer() -> Result<(), BenchmarkError> {
let account_index = T::AccountIndex::from(SEED);
// Setup accounts
let caller: T::AccountId = whitelisted_caller();
Expand All @@ -47,25 +52,33 @@ benchmarks! {
let recipient_lookup = T::Lookup::unlookup(recipient.clone());
T::Currency::make_free_balance_be(&recipient, BalanceOf::<T>::max_value());
// Claim the index
Indices::<T>::claim(RawOrigin::Signed(caller.clone()).into(), account_index)?;
}: _(RawOrigin::Signed(caller.clone()), recipient_lookup, account_index)
verify {
Pallet::<T>::claim(RawOrigin::Signed(caller.clone()).into(), account_index)?;

#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), recipient_lookup, account_index);

assert_eq!(Accounts::<T>::get(account_index).unwrap().0, recipient);
Ok(())
}

free {
#[benchmark]
fn free() -> Result<(), BenchmarkError> {
let account_index = T::AccountIndex::from(SEED);
// Setup accounts
let caller: T::AccountId = whitelisted_caller();
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
// Claim the index
Indices::<T>::claim(RawOrigin::Signed(caller.clone()).into(), account_index)?;
}: _(RawOrigin::Signed(caller.clone()), account_index)
verify {
Pallet::<T>::claim(RawOrigin::Signed(caller.clone()).into(), account_index)?;

#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), account_index);

assert_eq!(Accounts::<T>::get(account_index), None);
Ok(())
}

force_transfer {
#[benchmark]
fn force_transfer() -> Result<(), BenchmarkError> {
let account_index = T::AccountIndex::from(SEED);
// Setup accounts
let original: T::AccountId = account("original", 0, SEED);
Expand All @@ -74,25 +87,32 @@ benchmarks! {
let recipient_lookup = T::Lookup::unlookup(recipient.clone());
T::Currency::make_free_balance_be(&recipient, BalanceOf::<T>::max_value());
// Claim the index
Indices::<T>::claim(RawOrigin::Signed(original).into(), account_index)?;
}: _(RawOrigin::Root, recipient_lookup, account_index, false)
verify {
Pallet::<T>::claim(RawOrigin::Signed(original).into(), account_index)?;

#[extrinsic_call]
_(RawOrigin::Root, recipient_lookup, account_index, false);

assert_eq!(Accounts::<T>::get(account_index).unwrap().0, recipient);
Ok(())
}

freeze {
#[benchmark]
fn freeze() -> Result<(), BenchmarkError> {
let account_index = T::AccountIndex::from(SEED);
// Setup accounts
let caller: T::AccountId = whitelisted_caller();
T::Currency::make_free_balance_be(&caller, BalanceOf::<T>::max_value());
// Claim the index
Indices::<T>::claim(RawOrigin::Signed(caller.clone()).into(), account_index)?;
}: _(RawOrigin::Signed(caller.clone()), account_index)
verify {
Pallet::<T>::claim(RawOrigin::Signed(caller.clone()).into(), account_index)?;

#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), account_index);

assert_eq!(Accounts::<T>::get(account_index).unwrap().2, true);
Ok(())
}

// TODO in another PR: lookup and unlookup trait weights (not critical)

impl_benchmark_test_suite!(Indices, crate::mock::new_test_ext(), crate::mock::Test);
impl_benchmark_test_suite!(Pallet, mock::new_test_ext(), mock::Test);
}
51 changes: 32 additions & 19 deletions substrate/frame/transaction-storage/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@

#![cfg(feature = "runtime-benchmarks")]

use super::*;
use crate::*;
use alloc::{vec, vec::Vec};
use frame_benchmarking::v1::{benchmarks, whitelisted_caller};
use frame_benchmarking::v2::*;
use frame_support::traits::{Get, OnFinalize, OnInitialize};
use frame_system::{pallet_prelude::BlockNumberFor, EventRecord, Pallet as System, RawOrigin};
use sp_runtime::traits::{Bounded, CheckedDiv, One, Zero};
use sp_transaction_storage_proof::TransactionStorageProof;

use crate::Pallet as TransactionStorage;

// Proof generated from max size storage:
// ```
// let mut transactions = Vec::new();
Expand Down Expand Up @@ -122,50 +120,65 @@ pub fn run_to_block<T: Config>(n: frame_system::pallet_prelude::BlockNumberFor<T
}
}

benchmarks! {
store {
let l in 1 .. T::MaxTransactionSize::get();
#[benchmarks]
mod benchmarks {
use super::*;

#[benchmark]
fn store(l: Linear<1, { T::MaxTransactionSize::get() }>) {
let caller: T::AccountId = whitelisted_caller();
let initial_balance = BalanceOf::<T>::max_value().checked_div(&2u32.into()).unwrap();
T::Currency::set_balance(&caller, initial_balance);
}: _(RawOrigin::Signed(caller.clone()), vec![0u8; l as usize])
verify {

#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), vec![0u8; l as usize]);

assert!(!BlockTransactions::<T>::get().is_empty());
assert_last_event::<T>(Event::Stored { index: 0 }.into());
}

renew {
#[benchmark]
fn renew() -> Result<(), BenchmarkError> {
let caller: T::AccountId = whitelisted_caller();
let initial_balance = BalanceOf::<T>::max_value().checked_div(&2u32.into()).unwrap();
T::Currency::set_balance(&caller, initial_balance);
TransactionStorage::<T>::store(
Pallet::<T>::store(
RawOrigin::Signed(caller.clone()).into(),
vec![0u8; T::MaxTransactionSize::get() as usize],
)?;
run_to_block::<T>(1u32.into());
}: _(RawOrigin::Signed(caller.clone()), BlockNumberFor::<T>::zero(), 0)
verify {

#[extrinsic_call]
_(RawOrigin::Signed(caller.clone()), BlockNumberFor::<T>::zero(), 0);

assert_last_event::<T>(Event::Renewed { index: 0 }.into());

Ok(())
}

check_proof_max {
#[benchmark]
fn check_proof_max() -> Result<(), BenchmarkError> {
run_to_block::<T>(1u32.into());
let caller: T::AccountId = whitelisted_caller();
let initial_balance = BalanceOf::<T>::max_value().checked_div(&2u32.into()).unwrap();
T::Currency::set_balance(&caller, initial_balance);
for _ in 0 .. T::MaxBlockTransactions::get() {
TransactionStorage::<T>::store(
for _ in 0..T::MaxBlockTransactions::get() {
Pallet::<T>::store(
RawOrigin::Signed(caller.clone()).into(),
vec![0u8; T::MaxTransactionSize::get() as usize],
)?;
}
run_to_block::<T>(StoragePeriod::<T>::get() + BlockNumberFor::<T>::one());
let encoded_proof = proof();
let proof = TransactionStorageProof::decode(&mut &*encoded_proof).unwrap();
}: check_proof(RawOrigin::None, proof)
verify {

#[extrinsic_call]
check_proof(RawOrigin::None, proof);

assert_last_event::<T>(Event::ProofChecked.into());

Ok(())
}

impl_benchmark_test_suite!(TransactionStorage, crate::mock::new_test_ext(), crate::mock::Test);
impl_benchmark_test_suite!(Pallet, mock::new_test_ext(), mock::Test);
}

0 comments on commit edf79aa

Please sign in to comment.