mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-20 17:31:02 +00:00
9543d31474
This MR contains two major changes and some maintenance cleanup. ## 1. Free Standing Pallet Benchmark Runner Closes https://github.com/paritytech/polkadot-sdk/issues/3045, depends on your runtime exposing the `GenesisBuilderApi` (like https://github.com/paritytech/polkadot-sdk/pull/1492). Introduces a new binary crate: `frame-omni-bencher`. It allows to directly benchmark a WASM blob - without needing a node or chain spec. This makes it much easier to generate pallet weights and should allow us to remove bloaty code from the node. It should work for all FRAME runtimes that dont use 3rd party host calls or non `BlakeTwo256` block hashing (basically all polkadot parachains should work). It is 100% backwards compatible with the old CLI args, when the `v1` compatibility command is used. This is done to allow for forwards compatible addition of new commands. ### Example (full example in the Rust docs) Installing the CLI: ```sh cargo install --locked --path substrate/utils/frame/omni-bencher frame-omni-bencher --help ``` Building the Westend runtime: ```sh cargo build -p westend-runtime --release --features runtime-benchmarks ``` Benchmarking the runtime: ```sh frame-omni-bencher v1 benchmark pallet --runtime target/release/wbuild/westend-runtime/westend_runtime.compact.compressed.wasm --all ``` ## 2. Building the Benchmark Genesis State in the Runtime Closes https://github.com/paritytech/polkadot-sdk/issues/2664 This adds `--runtime` and `--genesis-builder=none|runtime|spec` arguments to the `benchmark pallet` command to make it possible to generate the genesis storage by the runtime. This can be used with both the node and the freestanding benchmark runners. It utilizes the new `GenesisBuilder` RA and depends on having https://github.com/paritytech/polkadot-sdk/pull/3412 deployed. ## 3. Simpler args for `PalletCmd::run` You can do three things here to integrate the changes into your node: - nothing: old code keeps working as before but emits a deprecated warning - delete: remove the pallet benchmarking code from your node and use the omni-bencher instead - patch: apply the patch below and keep using as currently. This emits a deprecated warning at runtime, since it uses the old way to generate a genesis state, but is the smallest change. ```patch runner.sync_run(|config| cmd - .run::<HashingFor<Block>, ReclaimHostFunctions>(config) + .run_with_spec::<HashingFor<Block>, ReclaimHostFunctions>(Some(config.chain_spec)) ) ``` ## 4. Maintenance Change - `pallet-nis` get a `BenchmarkSetup` config item to prepare its counterparty asset. - Add percent progress print when running benchmarks. - Dont immediately exit on benchmark error but try to run as many as possible and print errors last. --------- Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> Co-authored-by: Liam Aharon <liam.aharon@hotmail.com>
250 lines
8.8 KiB
Rust
250 lines
8.8 KiB
Rust
// This file is part of Substrate.
|
|
|
|
// Copyright (C) Parity Technologies (UK) Ltd.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
//! Benchmarks for NIS Pallet
|
|
|
|
#![cfg(feature = "runtime-benchmarks")]
|
|
|
|
use super::*;
|
|
use frame_benchmarking::v1::{account, benchmarks, whitelisted_caller, BenchmarkError};
|
|
use frame_support::traits::{
|
|
fungible::Inspect as FunInspect, nonfungible::Inspect, EnsureOrigin, Get,
|
|
};
|
|
use frame_system::RawOrigin;
|
|
use sp_arithmetic::Perquintill;
|
|
use sp_runtime::{
|
|
traits::{Bounded, One, Zero},
|
|
DispatchError, PerThing,
|
|
};
|
|
use sp_std::prelude::*;
|
|
|
|
use crate::Pallet as Nis;
|
|
|
|
const SEED: u32 = 0;
|
|
|
|
type BalanceOf<T> =
|
|
<<T as Config>::Currency as FunInspect<<T as frame_system::Config>::AccountId>>::Balance;
|
|
|
|
fn fill_queues<T: Config>() -> Result<(), DispatchError> {
|
|
// filling queues involves filling the first queue entirely and placing a single item in all
|
|
// other queues.
|
|
|
|
let queues = T::QueueCount::get();
|
|
let bids = T::MaxQueueLen::get();
|
|
|
|
let caller: T::AccountId = whitelisted_caller();
|
|
T::Currency::set_balance(&caller, T::MinBid::get() * BalanceOf::<T>::from(queues + bids));
|
|
|
|
for _ in 0..bids {
|
|
Nis::<T>::place_bid(RawOrigin::Signed(caller.clone()).into(), T::MinBid::get(), 1)?;
|
|
}
|
|
for d in 1..queues {
|
|
Nis::<T>::place_bid(RawOrigin::Signed(caller.clone()).into(), T::MinBid::get(), 1 + d)?;
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
benchmarks! {
|
|
place_bid {
|
|
let l in 0..(T::MaxQueueLen::get() - 1);
|
|
let caller: T::AccountId = whitelisted_caller();
|
|
let ed = T::Currency::minimum_balance();
|
|
let bid = T::MinBid::get();
|
|
T::Currency::set_balance(&caller, (ed + bid) * BalanceOf::<T>::from(l + 1) + bid);
|
|
for i in 0..l {
|
|
Nis::<T>::place_bid(RawOrigin::Signed(caller.clone()).into(), T::MinBid::get(), 1)?;
|
|
}
|
|
}: _(RawOrigin::Signed(caller.clone()), T::MinBid::get() * BalanceOf::<T>::from(2u32), 1)
|
|
verify {
|
|
assert_eq!(QueueTotals::<T>::get()[0], (l + 1, T::MinBid::get() * BalanceOf::<T>::from(l + 2)));
|
|
}
|
|
|
|
place_bid_max {
|
|
let caller: T::AccountId = whitelisted_caller();
|
|
let origin = RawOrigin::Signed(caller.clone());
|
|
let ed = T::Currency::minimum_balance();
|
|
let bid = T::MinBid::get();
|
|
let ql = T::MaxQueueLen::get();
|
|
T::Currency::set_balance(&caller, (ed + bid) * BalanceOf::<T>::from(ql + 1) + bid);
|
|
for i in 0..T::MaxQueueLen::get() {
|
|
Nis::<T>::place_bid(origin.clone().into(), T::MinBid::get(), 1)?;
|
|
}
|
|
}: place_bid(origin, T::MinBid::get() * BalanceOf::<T>::from(2u32), 1)
|
|
verify {
|
|
assert_eq!(QueueTotals::<T>::get()[0], (
|
|
T::MaxQueueLen::get(),
|
|
T::MinBid::get() * BalanceOf::<T>::from(T::MaxQueueLen::get() + 1),
|
|
));
|
|
}
|
|
|
|
retract_bid {
|
|
let l in 1..T::MaxQueueLen::get();
|
|
let caller: T::AccountId = whitelisted_caller();
|
|
let ed = T::Currency::minimum_balance();
|
|
let bid = T::MinBid::get();
|
|
T::Currency::set_balance(&caller, (ed + bid) * BalanceOf::<T>::from(l + 1) + bid);
|
|
for i in 0..l {
|
|
Nis::<T>::place_bid(RawOrigin::Signed(caller.clone()).into(), T::MinBid::get(), 1)?;
|
|
}
|
|
}: _(RawOrigin::Signed(caller.clone()), T::MinBid::get(), 1)
|
|
verify {
|
|
assert_eq!(QueueTotals::<T>::get()[0], (l - 1, T::MinBid::get() * BalanceOf::<T>::from(l - 1)));
|
|
}
|
|
|
|
fund_deficit {
|
|
T::BenchmarkSetup::create_counterpart_asset();
|
|
let origin =
|
|
T::FundOrigin::try_successful_origin().map_err(|_| BenchmarkError::Weightless)?;
|
|
let caller: T::AccountId = whitelisted_caller();
|
|
let bid = T::MinBid::get().max(One::one());
|
|
let ed = T::Currency::minimum_balance();
|
|
T::Currency::set_balance(&caller, ed + bid);
|
|
Nis::<T>::place_bid(RawOrigin::Signed(caller.clone()).into(), bid, 1)?;
|
|
Nis::<T>::process_queues(Perquintill::one(), 1, 1, &mut WeightCounter::unlimited());
|
|
Nis::<T>::communify(RawOrigin::Signed(caller.clone()).into(), 0)?;
|
|
let original = T::Currency::balance(&Nis::<T>::account_id());
|
|
T::Currency::set_balance(&Nis::<T>::account_id(), BalanceOf::<T>::min_value());
|
|
}: _<T::RuntimeOrigin>(origin)
|
|
verify {
|
|
// Must fund at least 99.999% of the required amount.
|
|
let missing = Perquintill::from_rational(
|
|
T::Currency::balance(&Nis::<T>::account_id()), original).left_from_one();
|
|
assert!(missing <= Perquintill::one() / 100_000);
|
|
}
|
|
|
|
communify {
|
|
T::BenchmarkSetup::create_counterpart_asset();
|
|
let caller: T::AccountId = whitelisted_caller();
|
|
let bid = T::MinBid::get().max(One::one()) * 100u32.into();
|
|
let ed = T::Currency::minimum_balance();
|
|
T::Currency::set_balance(&caller, ed + bid + bid);
|
|
Nis::<T>::place_bid(RawOrigin::Signed(caller.clone()).into(), bid, 1)?;
|
|
Nis::<T>::place_bid(RawOrigin::Signed(caller.clone()).into(), bid, 1)?;
|
|
Nis::<T>::process_queues(Perquintill::one(), 1, 2, &mut WeightCounter::unlimited());
|
|
}: _(RawOrigin::Signed(caller.clone()), 0)
|
|
verify {
|
|
assert_eq!(Nis::<T>::owner(&0), None);
|
|
}
|
|
|
|
privatize {
|
|
T::BenchmarkSetup::create_counterpart_asset();
|
|
let caller: T::AccountId = whitelisted_caller();
|
|
let bid = T::MinBid::get().max(One::one());
|
|
let ed = T::Currency::minimum_balance();
|
|
T::Currency::set_balance(&caller, ed + bid + bid);
|
|
Nis::<T>::place_bid(RawOrigin::Signed(caller.clone()).into(), bid, 1)?;
|
|
Nis::<T>::place_bid(RawOrigin::Signed(caller.clone()).into(), bid, 1)?;
|
|
Nis::<T>::process_queues(Perquintill::one(), 1, 2, &mut WeightCounter::unlimited());
|
|
Nis::<T>::communify(RawOrigin::Signed(caller.clone()).into(), 0)?;
|
|
}: _(RawOrigin::Signed(caller.clone()), 0)
|
|
verify {
|
|
assert_eq!(Nis::<T>::owner(&0), Some(caller));
|
|
}
|
|
|
|
thaw_private {
|
|
T::BenchmarkSetup::create_counterpart_asset();
|
|
let whale: T::AccountId = account("whale", 0, SEED);
|
|
let caller: T::AccountId = whitelisted_caller();
|
|
let bid = T::MinBid::get().max(One::one());
|
|
let ed = T::Currency::minimum_balance();
|
|
T::Currency::set_balance(&caller, ed + bid + bid);
|
|
// Ensure we don't get throttled.
|
|
T::Currency::set_balance(&whale, T::ThawThrottle::get().0.saturating_reciprocal_mul_ceil(T::Currency::balance(&caller)));
|
|
Nis::<T>::place_bid(RawOrigin::Signed(caller.clone()).into(), bid, 1)?;
|
|
Nis::<T>::place_bid(RawOrigin::Signed(caller.clone()).into(), bid, 1)?;
|
|
Nis::<T>::process_queues(Perquintill::one(), 1, 2, &mut WeightCounter::unlimited());
|
|
frame_system::Pallet::<T>::set_block_number(Receipts::<T>::get(0).unwrap().expiry);
|
|
}: _(RawOrigin::Signed(caller.clone()), 0, None)
|
|
verify {
|
|
assert!(Receipts::<T>::get(0).is_none());
|
|
}
|
|
|
|
thaw_communal {
|
|
T::BenchmarkSetup::create_counterpart_asset();
|
|
let whale: T::AccountId = account("whale", 0, SEED);
|
|
let caller: T::AccountId = whitelisted_caller();
|
|
let bid = T::MinBid::get().max(One::one());
|
|
let ed = T::Currency::minimum_balance();
|
|
T::Currency::set_balance(&caller, ed + bid + bid);
|
|
// Ensure we don't get throttled.
|
|
T::Currency::set_balance(&whale, T::ThawThrottle::get().0.saturating_reciprocal_mul_ceil(T::Currency::balance(&caller)));
|
|
Nis::<T>::place_bid(RawOrigin::Signed(caller.clone()).into(), bid, 1)?;
|
|
Nis::<T>::place_bid(RawOrigin::Signed(caller.clone()).into(), bid, 1)?;
|
|
Nis::<T>::process_queues(Perquintill::one(), 1, 2, &mut WeightCounter::unlimited());
|
|
frame_system::Pallet::<T>::set_block_number(Receipts::<T>::get(0).unwrap().expiry);
|
|
Nis::<T>::communify(RawOrigin::Signed(caller.clone()).into(), 0)?;
|
|
}: _(RawOrigin::Signed(caller.clone()), 0)
|
|
verify {
|
|
assert!(Receipts::<T>::get(0).is_none());
|
|
}
|
|
|
|
process_queues {
|
|
fill_queues::<T>()?;
|
|
}: {
|
|
Nis::<T>::process_queues(
|
|
Perquintill::one(),
|
|
Zero::zero(),
|
|
u32::max_value(),
|
|
&mut WeightCounter::unlimited(),
|
|
)
|
|
}
|
|
|
|
process_queue {
|
|
let our_account = Nis::<T>::account_id();
|
|
let issuance = Nis::<T>::issuance();
|
|
let mut summary = Summary::<T>::get();
|
|
}: {
|
|
Nis::<T>::process_queue(
|
|
1u32,
|
|
1u32.into(),
|
|
&our_account,
|
|
&issuance,
|
|
0,
|
|
&mut Bounded::max_value(),
|
|
&mut (T::MaxQueueLen::get(), Bounded::max_value()),
|
|
&mut summary,
|
|
&mut WeightCounter::unlimited(),
|
|
)
|
|
}
|
|
|
|
process_bid {
|
|
let who = account::<T::AccountId>("bidder", 0, SEED);
|
|
let min_bid = T::MinBid::get().max(One::one());
|
|
let ed = T::Currency::minimum_balance();
|
|
T::Currency::set_balance(&who, ed + min_bid);
|
|
let bid = Bid {
|
|
amount: T::MinBid::get(),
|
|
who,
|
|
};
|
|
let our_account = Nis::<T>::account_id();
|
|
let issuance = Nis::<T>::issuance();
|
|
let mut summary = Summary::<T>::get();
|
|
}: {
|
|
Nis::<T>::process_bid(
|
|
bid,
|
|
2u32.into(),
|
|
&our_account,
|
|
&issuance,
|
|
&mut Bounded::max_value(),
|
|
&mut Bounded::max_value(),
|
|
&mut summary,
|
|
)
|
|
}
|
|
|
|
impl_benchmark_test_suite!(Nis, crate::mock::new_test_ext_empty(), crate::mock::Test);
|
|
}
|