Skip to content

Commit 42e2c95

Browse files
tomusdrwkianenigmashawntabrizignunicorngui1117
authored
Streamline frame_system weight parametrization (#6629)
* Basic weights builder. * Fixing WiP * Make the tests work. * Fix weights in node/runtime. * WiP. * Update pallets with new weights parameters. * Validate returns a Result now. * Count mandatory weight separately. * DRY * BREAKING: Updating state root, because of the left-over weight-tracking stuff * Update tests affected by Mandatory tracking. * Fixing tests. * Fix defaults for simple_max * Update frame/system/src/weights.rs Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> * Rework the API a bit. * Fix compilation & tests. * Apply suggestions from code review Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> * Add extra docs & rename few things. * Fix whitespace in ASCII art. * Update frame/system/src/limits.rs Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> * Fix max_extrinsic calculations. * Fix conflicts. * Fix compilation. * Fix new code. * re-remove generic asset * Fix usage. * Update state root. * Update proxy. * Fix tests. * Move weights validity to integrity_test * Remove redundant BlockWeights. * Add all/non_mandatory comment * Add test. * Remove fn block_weights * Make the macro prettier. * Fix some docs. * Make max_total behave more predictabily. * Add BlockWeights to metadata. * fix balances test * Fix utility test. Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com> Co-authored-by: Benjamin Kampmann <ben@gnunicorn.org> Co-authored-by: thiolliere <gui.thiolliere@gmail.com>
1 parent b8abe90 commit 42e2c95

File tree

1 file changed

+61
-33
lines changed

1 file changed

+61
-33
lines changed

src/lib.rs

Lines changed: 61 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use frame_support::{
4040
traits::Get,
4141
weights::{
4242
Weight, DispatchInfo, PostDispatchInfo, GetDispatchInfo, Pays, WeightToFeePolynomial,
43-
WeightToFeeCoefficient,
43+
WeightToFeeCoefficient, DispatchClass,
4444
},
4545
dispatch::DispatchResult,
4646
};
@@ -158,14 +158,14 @@ impl<T, S, V, M> Convert<Multiplier, Multiplier> for TargetedFeeAdjustment<T, S,
158158
let min_multiplier = M::get();
159159
let previous = previous.max(min_multiplier);
160160

161+
let weights = T::BlockWeights::get();
161162
// the computed ratio is only among the normal class.
162-
let normal_max_weight =
163-
<T as frame_system::Config>::AvailableBlockRatio::get() *
164-
<T as frame_system::Config>::MaximumBlockWeight::get();
165-
let normal_block_weight =
166-
<frame_system::Module<T>>::block_weight()
167-
.get(frame_support::weights::DispatchClass::Normal)
168-
.min(normal_max_weight);
163+
let normal_max_weight = weights.get(DispatchClass::Normal).max_total
164+
.unwrap_or_else(|| weights.max_block);
165+
let current_block_weight = <frame_system::Module<T>>::block_weight();
166+
let normal_block_weight = *current_block_weight
167+
.get(DispatchClass::Normal)
168+
.min(&normal_max_weight);
169169

170170
let s = S::get();
171171
let v = V::get();
@@ -257,13 +257,13 @@ decl_module! {
257257

258258
fn integrity_test() {
259259
// given weight == u64, we build multipliers from `diff` of two weight values, which can
260-
// at most be MaximumBlockWeight. Make sure that this can fit in a multiplier without
260+
// at most be maximum block weight. Make sure that this can fit in a multiplier without
261261
// loss.
262262
use sp_std::convert::TryInto;
263263
assert!(
264264
<Multiplier as sp_runtime::traits::Bounded>::max_value() >=
265265
Multiplier::checked_from_integer(
266-
<T as frame_system::Config>::MaximumBlockWeight::get().try_into().unwrap()
266+
T::BlockWeights::get().max_block.try_into().unwrap()
267267
).unwrap(),
268268
);
269269

@@ -272,9 +272,11 @@ decl_module! {
272272
// that if we collapse to minimum, the trend will be positive with a weight value
273273
// which is 1% more than the target.
274274
let min_value = T::FeeMultiplierUpdate::min();
275-
let mut target =
276-
T::FeeMultiplierUpdate::target() *
277-
(T::AvailableBlockRatio::get() * T::MaximumBlockWeight::get());
275+
let mut target = T::FeeMultiplierUpdate::target() *
276+
T::BlockWeights::get().get(DispatchClass::Normal).max_total.expect(
277+
"Setting `max_total` for `Normal` dispatch class is not compatible with \
278+
`transaction-payment` pallet."
279+
);
278280

279281
// add 1 percent;
280282
let addition = target / 100;
@@ -285,7 +287,7 @@ decl_module! {
285287
target += addition;
286288

287289
sp_io::TestExternalities::new_empty().execute_with(|| {
288-
<frame_system::Module<T>>::set_block_limits(target, 0);
290+
<frame_system::Module<T>>::set_block_consumed_resources(target, 0);
289291
let next = T::FeeMultiplierUpdate::convert(min_value);
290292
assert!(next > min_value, "The minimum bound of the multiplier is too low. When \
291293
block saturation is more than target by 1% and multiplier is minimal then \
@@ -357,7 +359,13 @@ impl<T: Config> Module<T> where
357359
) -> BalanceOf<T> where
358360
T::Call: Dispatchable<Info=DispatchInfo>,
359361
{
360-
Self::compute_fee_raw(len, info.weight, tip, info.pays_fee)
362+
Self::compute_fee_raw(
363+
len,
364+
info.weight,
365+
tip,
366+
info.pays_fee,
367+
info.class,
368+
)
361369
}
362370

363371
/// Compute the actual post dispatch fee for a particular transaction.
@@ -372,14 +380,21 @@ impl<T: Config> Module<T> where
372380
) -> BalanceOf<T> where
373381
T::Call: Dispatchable<Info=DispatchInfo,PostInfo=PostDispatchInfo>,
374382
{
375-
Self::compute_fee_raw(len, post_info.calc_actual_weight(info), tip, post_info.pays_fee(info))
383+
Self::compute_fee_raw(
384+
len,
385+
post_info.calc_actual_weight(info),
386+
tip,
387+
post_info.pays_fee(info),
388+
info.class,
389+
)
376390
}
377391

378392
fn compute_fee_raw(
379393
len: u32,
380394
weight: Weight,
381395
tip: BalanceOf<T>,
382396
pays_fee: Pays,
397+
class: DispatchClass,
383398
) -> BalanceOf<T> {
384399
if pays_fee == Pays::Yes {
385400
let len = <BalanceOf<T>>::from(len);
@@ -394,7 +409,7 @@ impl<T: Config> Module<T> where
394409
// final adjusted weight fee.
395410
let adjusted_weight_fee = multiplier.saturating_mul_int(unadjusted_weight_fee);
396411

397-
let base_fee = Self::weight_to_fee(T::ExtrinsicBaseWeight::get());
412+
let base_fee = Self::weight_to_fee(T::BlockWeights::get().get(class).base_extrinsic);
398413
base_fee
399414
.saturating_add(fixed_len_fee)
400415
.saturating_add(adjusted_weight_fee)
@@ -407,7 +422,7 @@ impl<T: Config> Module<T> where
407422
fn weight_to_fee(weight: Weight) -> BalanceOf<T> {
408423
// cap the weight to the maximum defined in runtime, otherwise it will be the
409424
// `Bounded` maximum of its data type, which is not desired.
410-
let capped_weight = weight.min(<T as frame_system::Config>::MaximumBlockWeight::get());
425+
let capped_weight = weight.min(T::BlockWeights::get().max_block);
411426
T::WeightToFee::calc(&capped_weight)
412427
}
413428
}
@@ -471,8 +486,9 @@ impl<T: Config + Send + Sync> ChargeTransactionPayment<T> where
471486
/// that the transaction which consumes more resources (either length or weight) with the same
472487
/// `fee` ends up having lower priority.
473488
fn get_priority(len: usize, info: &DispatchInfoOf<T::Call>, final_fee: BalanceOf<T>) -> TransactionPriority {
474-
let weight_saturation = T::MaximumBlockWeight::get() / info.weight.max(1);
475-
let len_saturation = T::MaximumBlockLength::get() as u64 / (len as u64).max(1);
489+
let weight_saturation = T::BlockWeights::get().max_block / info.weight.max(1);
490+
let max_block_length = *T::BlockLength::get().max.get(DispatchClass::Normal);
491+
let len_saturation = max_block_length as u64 / (len as u64).max(1);
476492
let coefficient: BalanceOf<T> = weight_saturation.min(len_saturation).saturated_into::<BalanceOf<T>>();
477493
final_fee.saturating_mul(coefficient).saturated_into::<TransactionPriority>()
478494
}
@@ -571,6 +587,7 @@ mod tests {
571587
traits::{BlakeTwo256, IdentityLookup},
572588
Perbill,
573589
};
590+
use std::cell::RefCell;
574591
use smallvec::smallvec;
575592

576593
const CALL: &<Runtime as frame_system::Config>::Call =
@@ -598,18 +615,36 @@ mod tests {
598615
pub enum Origin for Runtime {}
599616
}
600617

618+
thread_local! {
619+
static EXTRINSIC_BASE_WEIGHT: RefCell<u64> = RefCell::new(0);
620+
}
621+
622+
pub struct BlockWeights;
623+
impl Get<frame_system::limits::BlockWeights> for BlockWeights {
624+
fn get() -> frame_system::limits::BlockWeights {
625+
frame_system::limits::BlockWeights::builder()
626+
.base_block(0)
627+
.for_class(DispatchClass::all(), |weights| {
628+
weights.base_extrinsic = EXTRINSIC_BASE_WEIGHT.with(|v| *v.borrow()).into();
629+
})
630+
.for_class(DispatchClass::non_mandatory(), |weights| {
631+
weights.max_total = 1024.into();
632+
})
633+
.build_or_panic()
634+
}
635+
}
636+
601637
parameter_types! {
602638
pub const BlockHashCount: u64 = 250;
603-
pub const MaximumBlockWeight: Weight = 1024;
604-
pub const MaximumBlockLength: u32 = 2 * 1024;
605-
pub const AvailableBlockRatio: Perbill = Perbill::one();
606-
pub static ExtrinsicBaseWeight: u64 = 0;
607639
pub static TransactionByteFee: u64 = 1;
608640
pub static WeightToFee: u64 = 1;
609641
}
610642

611643
impl frame_system::Config for Runtime {
612644
type BaseCallFilter = ();
645+
type BlockWeights = BlockWeights;
646+
type BlockLength = ();
647+
type DbWeight = ();
613648
type Origin = Origin;
614649
type Index = u64;
615650
type BlockNumber = u64;
@@ -621,13 +656,6 @@ mod tests {
621656
type Header = Header;
622657
type Event = Event;
623658
type BlockHashCount = BlockHashCount;
624-
type MaximumBlockWeight = MaximumBlockWeight;
625-
type DbWeight = ();
626-
type BlockExecutionWeight = ();
627-
type ExtrinsicBaseWeight = ExtrinsicBaseWeight;
628-
type MaximumExtrinsicWeight = MaximumBlockWeight;
629-
type MaximumBlockLength = MaximumBlockLength;
630-
type AvailableBlockRatio = AvailableBlockRatio;
631659
type Version = ();
632660
type PalletInfo = ();
633661
type AccountData = pallet_balances::AccountData<u64>;
@@ -841,7 +869,7 @@ mod tests {
841869
// fee will be proportional to what is the actual maximum weight in the runtime.
842870
assert_eq!(
843871
Balances::free_balance(&1),
844-
(10000 - <Runtime as frame_system::Config>::MaximumBlockWeight::get()) as u64
872+
(10000 - <Runtime as frame_system::Config>::BlockWeights::get().max_block) as u64
845873
);
846874
});
847875
}
@@ -939,7 +967,7 @@ mod tests {
939967
partial_fee:
940968
5 * 2 /* base * weight_fee */
941969
+ len as u64 /* len * 1 */
942-
+ info.weight.min(MaximumBlockWeight::get()) as u64 * 2 * 3 / 2 /* weight */
970+
+ info.weight.min(BlockWeights::get().max_block) as u64 * 2 * 3 / 2 /* weight */
943971
},
944972
);
945973

0 commit comments

Comments
 (0)