From c13026200ee6362ee266ea15a609d4c911eb4890 Mon Sep 17 00:00:00 2001 From: Trubnikov Sergey Date: Thu, 2 Mar 2023 08:56:11 +0000 Subject: [PATCH] Refactor --- frame/utility/src/lib.rs | 78 ++++++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 40 deletions(-) diff --git a/frame/utility/src/lib.rs b/frame/utility/src/lib.rs index da2578d621f6a..c3bf21b0c0af5 100644 --- a/frame/utility/src/lib.rs +++ b/frame/utility/src/lib.rs @@ -61,8 +61,11 @@ use frame_support::{ dispatch::{ extract_actual_weight, DispatchErrorWithPostInfo, GetDispatchInfo, PostDispatchInfo, }, + pallet_prelude::DispatchResultWithPostInfo, traits::{Contains, IsSubType, OriginTrait, UnfilteredDispatchable}, + weights::Weight, }; +use frame_system::pallet_prelude::OriginFor; use sp_core::TypeId; use sp_io::hashing::blake2_256; use sp_runtime::traits::{BadOrigin, Dispatchable, TrailingZeroInput}; @@ -223,14 +226,12 @@ pub mod pallet { for (index, call) in calls.into_iter().enumerate() { let info = call.get_dispatch_info(); - let result = Self::check_call_filter(is_root, &call).and_then(|_| { - // If origin is root, don't apply any dispatch filters; root can call anything. - if is_root { - call.dispatch_bypass_filter(origin.clone()) - } else { - call.dispatch(origin.clone()) - } - }); + // If origin is root, don't apply any dispatch filters; root can call anything. + let result = if is_root { + call.dispatch_bypass_filter(origin.clone()) + } else { + Self::dispatch_filtered(origin.clone(), call) + }; // Add the weight of this call. weight = weight.saturating_add(extract_actual_weight(&result, &info)); @@ -286,7 +287,7 @@ pub mod pallet { origin.set_caller_from(frame_system::RawOrigin::Signed(pseudonym)); let info = call.get_dispatch_info(); - let result = Self::check_call_filter(false, &call).and_then(|_| call.dispatch(origin)); + let result = Self::dispatch_filtered(origin, *call); // Always take into account the base weight of this call. let mut weight = T::WeightInfo::as_derivative() .saturating_add(T::DbWeight::get().reads_writes(1, 1)); @@ -351,22 +352,20 @@ pub mod pallet { for (index, call) in calls.into_iter().enumerate() { let info = call.get_dispatch_info(); - let result = Self::check_call_filter(is_root, &call).and_then(|_| { - // If origin is root, bypass any dispatch filter; root can call anything. - if is_root { - call.dispatch_bypass_filter(origin.clone()) - } else { - let mut filtered_origin = origin.clone(); - // Don't allow users to nest `batch_all` calls. - filtered_origin.add_filter( - move |c: &::RuntimeCall| { - let c = ::RuntimeCall::from_ref(c); - !matches!(c.is_sub_type(), Some(Call::batch_all { .. })) - }, - ); - call.dispatch(filtered_origin) - } - }); + // If origin is root, bypass any dispatch filter; root can call anything. + let result = if is_root { + call.dispatch_bypass_filter(origin.clone()) + } else { + let mut filtered_origin = origin.clone(); + // Don't allow users to nest `batch_all` calls. + filtered_origin.add_filter( + move |c: &::RuntimeCall| { + let c = ::RuntimeCall::from_ref(c); + !matches!(c.is_sub_type(), Some(Call::batch_all { .. })) + }, + ); + Self::dispatch_filtered(filtered_origin, call) + }; // Add the weight of this call. weight = weight.saturating_add(extract_actual_weight(&result, &info)); result.map_err(|mut err| { @@ -466,14 +465,13 @@ pub mod pallet { for call in calls.into_iter() { let info = call.get_dispatch_info(); - let result = Self::check_call_filter(is_root, &call).and_then(|_| { - // If origin is root, don't apply any dispatch filters; root can call anything. - if is_root { - call.dispatch_bypass_filter(origin.clone()) - } else { - call.dispatch(origin.clone()) - } - }); + // If origin is root, don't apply any dispatch filters; root can call anything. + let result = if is_root { + call.dispatch_bypass_filter(origin.clone()) + } else { + Self::dispatch_filtered(origin.clone(), call) + }; + // Add the weight of this call. weight = weight.saturating_add(extract_actual_weight(&result, &info)); if let Err(e) = result { @@ -528,16 +526,16 @@ impl Pallet { .expect("infinite length input; no invalid inputs for type; qed") } - fn check_call_filter( - is_root: bool, - call: &::RuntimeCall, - ) -> Result<(), DispatchErrorWithPostInfo> { - if is_root || ::CallFilter::contains(call) { - return Ok(()) + fn dispatch_filtered( + origin: OriginFor, + call: ::RuntimeCall, + ) -> DispatchResultWithPostInfo { + if ::CallFilter::contains(&call) { + return call.dispatch(origin) } Err(DispatchErrorWithPostInfo { - post_info: None::.into(), + post_info: Some(Weight::default()).into(), error: >::CallFiltered.into(), }) }