Skip to content
Merged
93 changes: 72 additions & 21 deletions crates/subspace-runtime-primitives/src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,28 @@
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_core::Get;
use sp_runtime::impl_tx_ext_default;
use sp_runtime::traits::{
AsSystemOriginSigner, DispatchInfoOf, Dispatchable, TransactionExtension, ValidateResult,
AsSystemOriginSigner, DispatchInfoOf, DispatchOriginOf, Dispatchable, PostDispatchInfoOf,
TransactionExtension, ValidateResult,
};
use sp_runtime::transaction_validity::{
InvalidTransaction, TransactionSource, TransactionValidity, ValidTransaction,
InvalidTransaction, TransactionSource, TransactionValidityError, ValidTransaction,
};
use sp_runtime::DispatchResult;

/// Maximum number of calls we benchmarked for.
const MAXIMUM_NUMBER_OF_CALLS: u32 = 1000;
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

Copy link
Contributor

Choose a reason for hiding this comment

The 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:

  1. utility([non_transfer_call; 2000]): 2000 calls in one utility call
  2. utility([utility([non_transfer_call; 100]); 100]): 100 utility calls each containing 100 calls
  3. a btree structure, with 2 calls at each level and 2 nested utility calls within them, this would take around 10 levels to have more than 1000 calls

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).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The MAXIMUM_NUMBER_OF_CALLS is inevitable because the weight system works as:

  1. Charge for the worst situation weight
  2. Do execution/validation and get an accurate weight
  3. Refund any overcharged weight

So we have to first define the worst situation weight with MAXIMUM_NUMBER_OF_CALLS, the value of MAXIMUM_NUMBER_OF_CALLS is hard to determine since we don't really have an explicit limit on the maximum nested calls, options I can think of:

  • Add such an explicit limit to the extension
  • Calculate MAXIMUM_NUMBER_OF_CALLS by block_size / min_extrinsic_size
  • Leave MAXIMUM_NUMBER_OF_CALLS as is or pick another value, since the check is lightweight, the resulting weight won't be much different I guess.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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:
https://github.com/autonomys/subspace/tree/benchmarks_disable_extension_exploration

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 {
Expand Down Expand Up @@ -59,17 +64,26 @@ where
Runtime: BalanceTransferChecks + pallet_balances::Config,
RuntimeCallFor<Runtime>: MaybeBalancesCall<Runtime> + MaybeNestedCall<Runtime>,
{
fn do_validate_signed(call: &RuntimeCallFor<Runtime>) -> TransactionValidity {
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.
if !Runtime::is_balance_transferable() && Self::contains_balance_transfer(call) {
let (contains_balance_call, calls) = Self::contains_balance_transfer(call);
if contains_balance_call {
Err(InvalidTransaction::Call.into())
} else {
Ok(ValidTransaction::default())
Ok((ValidTransaction::default(), calls))
}
}

fn contains_balance_transfer(call: &RuntimeCallFor<Runtime>) -> bool {
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()
Expand All @@ -80,12 +94,24 @@ where
| BalancesCall::transfer_all { .. }
)
{
return true;
return (true, calls);
}
}

false
(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>>
Expand All @@ -102,15 +128,13 @@ where
AsSystemOriginSigner<<Runtime as Config>::AccountId> + Clone,
RuntimeCallFor<Runtime>: MaybeBalancesCall<Runtime> + MaybeNestedCall<Runtime>,
{
const IDENTIFIER: &'static str = "DisablePallets";
const IDENTIFIER: &'static str = "BalanceTransferCheckExtension";
type Implicit = ();
type Val = ();
type Pre = ();
type Val = Option<u32>;
type Pre = Pre;

// TODO: calculate weight for extension
fn weight(&self, _call: &RuntimeCallFor<Runtime>) -> Weight {
// there is always one storage read
<Runtime as Config>::DbWeight::get().reads(1)
Self::get_weights(MAXIMUM_NUMBER_OF_CALLS)
}

fn validate(
Expand All @@ -123,14 +147,41 @@ where
_inherited_implication: &impl Encode,
_source: TransactionSource,
) -> ValidateResult<Self::Val, RuntimeCallFor<Runtime>> {
let validity = if origin.as_system_origin_signer().is_some() {
Self::do_validate_signed(call)?
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()
(ValidTransaction::default(), None)
};

Ok((validity, (), origin))
Ok((validity, maybe_calls, origin))
}

impl_tx_ext_default!(RuntimeCallFor<Runtime>; prepare);
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)),
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)
}
}
Loading