-
Notifications
You must be signed in to change notification settings - Fork 251
Benchmarks for balance transfer check extension #3557
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
9238983
3b0ea9d
a56a06e
4ae0708
734a273
7a789ef
fe4075a
4f4900a
0d37ad2
7c80471
2c43c56
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| #[cfg(feature = "runtime-benchmarks")] | ||
| pub mod benchmarking; | ||
| pub mod weights; | ||
|
|
||
| use crate::extension::weights::WeightInfo as SubstrateWeightInfo; | ||
| use crate::utility::{nested_call_iter, MaybeNestedCall}; | ||
| use core::marker::PhantomData; | ||
| use frame_support::pallet_prelude::Weight; | ||
| use frame_support::RuntimeDebugNoBound; | ||
| use frame_system::pallet_prelude::{OriginFor, RuntimeCallFor}; | ||
| use frame_system::Config; | ||
| use pallet_balances::Call as BalancesCall; | ||
| use parity_scale_codec::{Decode, Encode}; | ||
| use scale_info::prelude::fmt; | ||
| use scale_info::TypeInfo; | ||
| use sp_runtime::traits::{ | ||
| AsSystemOriginSigner, DispatchInfoOf, DispatchOriginOf, Dispatchable, PostDispatchInfoOf, | ||
| TransactionExtension, ValidateResult, | ||
| }; | ||
| use sp_runtime::transaction_validity::{ | ||
| InvalidTransaction, TransactionSource, TransactionValidityError, ValidTransaction, | ||
| }; | ||
| use sp_runtime::DispatchResult; | ||
|
|
||
| /// Maximum number of calls we benchmarked for. | ||
| const MAXIMUM_NUMBER_OF_CALLS: u32 = 1000; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happens if the actual number of nested calls is greater than this? Does the submitter get the extra calls for free?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes if it exceeds 1000, the remaining are free but max decoding depth for extrinsics is 256. So we will never reach that
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are at least 3 ways to get more than 1000 loop iterations without exceeding the decoding depth:
It is likely that decoding will have different performance for case 3, due to branch misprediction (there are no long runs of the same item).
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
So we have to first define the worst situation weight with
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have tried again with max of 1000 nested utility calls with 500 system.remark calls until the last nested call. Last one included on balance transfer. Allocator failed to allocate the memory. If such a case arise in practical, that extrinsic will never be inluded
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if that's the data structure I was talking about above. Either way, there's going to be some number of calls that fits within the allocation limits. I'll try a few things and add a benchmark if it's heavier.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A list of 1000 non-transfer calls fits in memory and is heavier in some cases: I haven't had time to explore the other data structures like squares and trees. Since both "long" and "tall" calls have similar results, I'm not sure it's going to be much different. Trees might be different if there's a lot of pointer dereferences, but I don't think it's a blocker for this PR. |
||
|
|
||
| /// Weights for the balance transfer check extension. | ||
| pub trait WeightInfo { | ||
| fn balance_transfer_check_mixed(c: u32) -> Weight; | ||
| fn balance_transfer_check_utility(c: u32) -> Weight; | ||
| fn balance_transfer_check_multisig(c: u32) -> Weight; | ||
| } | ||
|
|
||
| /// Trait to convert Runtime call to possible Balance call. | ||
| pub trait MaybeBalancesCall<Runtime> | ||
| where | ||
| Runtime: pallet_balances::Config, | ||
| { | ||
| fn maybe_balance_call(&self) -> Option<&BalancesCall<Runtime>>; | ||
| } | ||
|
|
||
| /// Trait to check if the Balance transfers are enabled. | ||
| pub trait BalanceTransferChecks { | ||
| fn is_balance_transferable() -> bool; | ||
| } | ||
|
|
||
| /// Disable balance transfers, if configured in the runtime. | ||
| #[derive(Debug, Encode, Decode, Clone, Eq, PartialEq, TypeInfo)] | ||
| pub struct BalanceTransferCheckExtension<Runtime>(PhantomData<Runtime>); | ||
|
|
||
| impl<Runtime> Default for BalanceTransferCheckExtension<Runtime> | ||
| where | ||
| Runtime: BalanceTransferChecks + pallet_balances::Config, | ||
| RuntimeCallFor<Runtime>: MaybeBalancesCall<Runtime> + MaybeNestedCall<Runtime>, | ||
| { | ||
| fn default() -> Self { | ||
| Self(PhantomData) | ||
| } | ||
| } | ||
|
|
||
| impl<Runtime> BalanceTransferCheckExtension<Runtime> | ||
| where | ||
| Runtime: BalanceTransferChecks + pallet_balances::Config, | ||
| RuntimeCallFor<Runtime>: MaybeBalancesCall<Runtime> + MaybeNestedCall<Runtime>, | ||
| { | ||
| fn do_validate_signed( | ||
| call: &RuntimeCallFor<Runtime>, | ||
| ) -> Result<(ValidTransaction, u32), TransactionValidityError> { | ||
| if Runtime::is_balance_transferable() { | ||
| return Ok((ValidTransaction::default(), 0)); | ||
| } | ||
|
|
||
| // Disable normal balance transfers. | ||
| let (contains_balance_call, calls) = Self::contains_balance_transfer(call); | ||
| if contains_balance_call { | ||
| Err(InvalidTransaction::Call.into()) | ||
| } else { | ||
| Ok((ValidTransaction::default(), calls)) | ||
| } | ||
| } | ||
|
|
||
| fn contains_balance_transfer(call: &RuntimeCallFor<Runtime>) -> (bool, u32) { | ||
| let mut calls = 0; | ||
| for call in nested_call_iter::<Runtime>(call) { | ||
| calls += 1; | ||
| // Any other calls might contain nested calls, so we can only return early if we find a | ||
| // balance transfer call. | ||
| if let Some(balance_call) = call.maybe_balance_call() | ||
| && matches!( | ||
| balance_call, | ||
| BalancesCall::transfer_allow_death { .. } | ||
| | BalancesCall::transfer_keep_alive { .. } | ||
| | BalancesCall::transfer_all { .. } | ||
| ) | ||
| { | ||
| return (true, calls); | ||
| } | ||
| } | ||
|
|
||
| (false, calls) | ||
| } | ||
|
|
||
| fn get_weights(n: u32) -> Weight { | ||
| SubstrateWeightInfo::<Runtime>::balance_transfer_check_multisig(n) | ||
| .max(SubstrateWeightInfo::<Runtime>::balance_transfer_check_mixed(n)) | ||
| .max(SubstrateWeightInfo::<Runtime>::balance_transfer_check_utility(n)) | ||
| } | ||
| } | ||
|
|
||
| /// Data passed from prepare to post_dispatch. | ||
| #[derive(RuntimeDebugNoBound)] | ||
| pub enum Pre { | ||
| Refund(Weight), | ||
| } | ||
|
|
||
| impl<Runtime> TransactionExtension<RuntimeCallFor<Runtime>> | ||
| for BalanceTransferCheckExtension<Runtime> | ||
| where | ||
| Runtime: Config | ||
| + pallet_balances::Config | ||
| + scale_info::TypeInfo | ||
| + fmt::Debug | ||
| + Send | ||
| + Sync | ||
| + BalanceTransferChecks, | ||
| <RuntimeCallFor<Runtime> as Dispatchable>::RuntimeOrigin: | ||
| AsSystemOriginSigner<<Runtime as Config>::AccountId> + Clone, | ||
| RuntimeCallFor<Runtime>: MaybeBalancesCall<Runtime> + MaybeNestedCall<Runtime>, | ||
| { | ||
| const IDENTIFIER: &'static str = "BalanceTransferCheckExtension"; | ||
| type Implicit = (); | ||
| type Val = Option<u32>; | ||
| type Pre = Pre; | ||
|
|
||
| fn weight(&self, _call: &RuntimeCallFor<Runtime>) -> Weight { | ||
| Self::get_weights(MAXIMUM_NUMBER_OF_CALLS) | ||
| } | ||
|
|
||
| fn validate( | ||
| &self, | ||
| origin: OriginFor<Runtime>, | ||
| call: &RuntimeCallFor<Runtime>, | ||
| _info: &DispatchInfoOf<RuntimeCallFor<Runtime>>, | ||
| _len: usize, | ||
| _self_implicit: Self::Implicit, | ||
| _inherited_implication: &impl Encode, | ||
| _source: TransactionSource, | ||
| ) -> ValidateResult<Self::Val, RuntimeCallFor<Runtime>> { | ||
| let (validity, maybe_calls) = if origin.as_system_origin_signer().is_some() { | ||
| Self::do_validate_signed(call).map(|(valid, calls)| (valid, Some(calls)))? | ||
| } else { | ||
| (ValidTransaction::default(), None) | ||
| }; | ||
|
|
||
| Ok((validity, maybe_calls, origin)) | ||
| } | ||
|
|
||
| fn prepare( | ||
| self, | ||
| val: Self::Val, | ||
| _origin: &DispatchOriginOf<RuntimeCallFor<Runtime>>, | ||
| _call: &RuntimeCallFor<Runtime>, | ||
| _info: &DispatchInfoOf<RuntimeCallFor<Runtime>>, | ||
| _len: usize, | ||
| ) -> Result<Self::Pre, TransactionValidityError> { | ||
| let assigned_weight = Self::get_weights(MAXIMUM_NUMBER_OF_CALLS); | ||
| match val { | ||
| None => Ok(Pre::Refund(assigned_weight)), | ||
|
vedhavyas marked this conversation as resolved.
Outdated
|
||
| Some(calls) => { | ||
| let actual_weights = Self::get_weights(calls); | ||
| Ok(Pre::Refund(assigned_weight.saturating_sub(actual_weights))) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn post_dispatch_details( | ||
| pre: Self::Pre, | ||
| _info: &DispatchInfoOf<RuntimeCallFor<Runtime>>, | ||
| _post_info: &PostDispatchInfoOf<RuntimeCallFor<Runtime>>, | ||
| _len: usize, | ||
| _result: &DispatchResult, | ||
| ) -> Result<Weight, TransactionValidityError> { | ||
| let Pre::Refund(weight) = pre; | ||
| Ok(weight) | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.