Skip to content

Commit 54504b6

Browse files
authored
Merge pull request #2400 from opentensor/proxy-batch-dispatch-class-normal
Proxy & Batch `DispatchClass::Normal`
2 parents 198ac7e + 4dd4d03 commit 54504b6

File tree

5 files changed

+31
-46
lines changed

5 files changed

+31
-46
lines changed

pallets/admin-utils/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1685,7 +1685,7 @@ pub mod pallet {
16851685
#[pallet::weight((
16861686
Weight::from_parts(3_918_000, 0) // TODO: add benchmarks
16871687
.saturating_add(T::DbWeight::get().writes(1_u64)),
1688-
DispatchClass::Operational,
1688+
DispatchClass::Normal,
16891689
Pays::Yes
16901690
))]
16911691
pub fn sudo_set_subnet_owner_hotkey(

pallets/proxy/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ pub mod pallet {
235235
// AccountData for inner call origin accountdata.
236236
.saturating_add(T::DbWeight::get().reads_writes(1, 1))
237237
.saturating_add(di.call_weight),
238-
di.class, di.pays_fee)
238+
DispatchClass::Normal, di.pays_fee)
239239
})]
240240
pub fn proxy(
241241
origin: OriginFor<T>,
@@ -544,7 +544,7 @@ pub mod pallet {
544544
// AccountData for inner call origin accountdata.
545545
.saturating_add(T::DbWeight::get().reads_writes(1, 1))
546546
.saturating_add(di.call_weight),
547-
di.class)
547+
DispatchClass::Normal)
548548
})]
549549
pub fn proxy_announced(
550550
origin: OriginFor<T>,

pallets/subtensor/src/macros/dispatches.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2037,7 +2037,7 @@ mod dispatches {
20372037

20382038
/// Sets the pending childkey cooldown (in blocks). Root only.
20392039
#[pallet::call_index(109)]
2040-
#[pallet::weight((Weight::from_parts(10_000, 0), DispatchClass::Operational, Pays::Yes))]
2040+
#[pallet::weight((Weight::from_parts(1_970_000_000_000, 0), DispatchClass::Operational, Pays::Yes))]
20412041
pub fn set_pending_childkey_cooldown(
20422042
origin: OriginFor<T>,
20432043
cooldown: u64,
@@ -2413,7 +2413,7 @@ mod dispatches {
24132413
Weight::from_parts(5_711_000, 0)
24142414
.saturating_add(T::DbWeight::get().reads(0_u64))
24152415
.saturating_add(T::DbWeight::get().writes(1_u64)),
2416-
DispatchClass::Operational,
2416+
DispatchClass::Normal,
24172417
Pays::Yes
24182418
))]
24192419
pub fn sudo_set_root_claim_threshold(

pallets/utility/src/lib.rs

Lines changed: 25 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,7 @@ extern crate alloc;
6161
use alloc::{boxed::Box, vec::Vec};
6262
use codec::{Decode, Encode};
6363
use frame_support::{
64-
dispatch::{
65-
DispatchClass::{Normal, Operational},
66-
GetDispatchInfo, PostDispatchInfo, extract_actual_weight,
67-
},
64+
dispatch::{GetDispatchInfo, PostDispatchInfo, extract_actual_weight},
6865
traits::{IsSubType, OriginTrait, UnfilteredDispatchable},
6966
};
7067
use sp_core::TypeId;
@@ -198,9 +195,9 @@ pub mod pallet {
198195
/// event is deposited.
199196
#[pallet::call_index(0)]
200197
#[pallet::weight({
201-
let (dispatch_weight, dispatch_class, pays) = Pallet::<T>::weight_and_dispatch_class(calls);
198+
let (dispatch_weight, pays) = Pallet::<T>::weight_and_dispatch_class(calls);
202199
let dispatch_weight = dispatch_weight.saturating_add(T::WeightInfo::batch(calls.len() as u32));
203-
(dispatch_weight, dispatch_class, pays)
200+
(dispatch_weight, DispatchClass::Normal, pays)
204201
})]
205202
pub fn batch(
206203
origin: OriginFor<T>,
@@ -268,7 +265,7 @@ pub mod pallet {
268265
// AccountData for inner call origin accountdata.
269266
.saturating_add(T::DbWeight::get().reads_writes(1, 1))
270267
.saturating_add(dispatch_info.call_weight),
271-
dispatch_info.class,
268+
DispatchClass::Normal,
272269
)
273270
})]
274271
pub fn as_derivative(
@@ -310,9 +307,9 @@ pub mod pallet {
310307
/// - O(C) where C is the number of calls to be batched.
311308
#[pallet::call_index(2)]
312309
#[pallet::weight({
313-
let (dispatch_weight, dispatch_class, pays) = Pallet::<T>::weight_and_dispatch_class(calls);
310+
let (dispatch_weight, pays) = Pallet::<T>::weight_and_dispatch_class(calls);
314311
let dispatch_weight = dispatch_weight.saturating_add(T::WeightInfo::batch_all(calls.len() as u32));
315-
(dispatch_weight, dispatch_class, pays)
312+
(dispatch_weight, DispatchClass::Normal, pays)
316313
})]
317314
pub fn batch_all(
318315
origin: OriginFor<T>,
@@ -376,7 +373,7 @@ pub mod pallet {
376373
(
377374
T::WeightInfo::dispatch_as()
378375
.saturating_add(dispatch_info.call_weight),
379-
dispatch_info.class,
376+
DispatchClass::Normal,
380377
)
381378
})]
382379
pub fn dispatch_as(
@@ -409,9 +406,9 @@ pub mod pallet {
409406
/// - O(C) where C is the number of calls to be batched.
410407
#[pallet::call_index(4)]
411408
#[pallet::weight({
412-
let (dispatch_weight, dispatch_class, pays) = Pallet::<T>::weight_and_dispatch_class(calls);
409+
let (dispatch_weight, pays) = Pallet::<T>::weight_and_dispatch_class(calls);
413410
let dispatch_weight = dispatch_weight.saturating_add(T::WeightInfo::force_batch(calls.len() as u32));
414-
(dispatch_weight, dispatch_class, pays)
411+
(dispatch_weight, DispatchClass::Normal, pays)
415412
})]
416413
pub fn force_batch(
417414
origin: OriginFor<T>,
@@ -466,7 +463,7 @@ pub mod pallet {
466463
///
467464
/// The dispatch origin for this call must be _Root_.
468465
#[pallet::call_index(5)]
469-
#[pallet::weight((*weight, call.get_dispatch_info().class))]
466+
#[pallet::weight((*weight, DispatchClass::Normal))]
470467
pub fn with_weight(
471468
origin: OriginFor<T>,
472469
call: Box<<T as Config>::RuntimeCall>,
@@ -510,7 +507,7 @@ pub mod pallet {
510507
T::WeightInfo::if_else()
511508
.saturating_add(main.call_weight)
512509
.saturating_add(fallback.call_weight),
513-
if main.class == Operational && fallback.class == Operational { Operational } else { Normal },
510+
DispatchClass::Normal,
514511
)
515512
})]
516513
pub fn if_else(
@@ -584,7 +581,7 @@ pub mod pallet {
584581
(
585582
T::WeightInfo::dispatch_as_fallible()
586583
.saturating_add(dispatch_info.call_weight),
587-
dispatch_info.class,
584+
DispatchClass::Normal,
588585
)
589586
})]
590587
pub fn dispatch_as_fallible(
@@ -604,32 +601,20 @@ pub mod pallet {
604601
}
605602

606603
impl<T: Config> Pallet<T> {
607-
/// Get the accumulated `weight` and the dispatch class for the given `calls`.
608-
fn weight_and_dispatch_class(
609-
calls: &[<T as Config>::RuntimeCall],
610-
) -> (Weight, DispatchClass, Pays) {
611-
let dispatch_infos = calls.iter().map(|call| call.get_dispatch_info());
612-
let pays = if dispatch_infos.clone().any(|di| di.pays_fee == Pays::No) {
613-
Pays::No
614-
} else {
615-
Pays::Yes
616-
};
617-
let (dispatch_weight, dispatch_class) = dispatch_infos.fold(
618-
(Weight::zero(), DispatchClass::Operational),
619-
|(total_weight, dispatch_class): (Weight, DispatchClass), di| {
620-
(
621-
total_weight.saturating_add(di.call_weight),
622-
// If not all are `Operational`, we want to use `DispatchClass::Normal`.
623-
if di.class == DispatchClass::Normal {
624-
di.class
625-
} else {
626-
dispatch_class
627-
},
628-
)
629-
},
630-
);
604+
/// Get the accumulated `weight` and `pays` for the given `calls`.
605+
/// The outer dispatch class is intentionally always `Normal`.
606+
fn weight_and_dispatch_class(calls: &[<T as Config>::RuntimeCall]) -> (Weight, Pays) {
607+
let mut total_weight = Weight::zero();
608+
let mut pays = Pays::No;
609+
610+
for di in calls.iter().map(|call| call.get_dispatch_info()) {
611+
total_weight = total_weight.saturating_add(di.call_weight);
612+
if di.pays_fee == Pays::Yes {
613+
pays = Pays::Yes;
614+
}
615+
}
631616

632-
(dispatch_weight, dispatch_class, pays)
617+
(total_weight, pays)
633618
}
634619
}
635620
}

pallets/utility/src/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,7 @@ fn with_weight_works() {
839839
);
840840
assert_eq!(
841841
with_weight_call.get_dispatch_info().class,
842-
frame_support::dispatch::DispatchClass::Operational
842+
frame_support::dispatch::DispatchClass::Normal // We only allow normal in subtensor
843843
);
844844
})
845845
}

0 commit comments

Comments
 (0)