Struct frame_system::limits::BlockWeightsBuilder
source · pub struct BlockWeightsBuilder { /* private fields */ }Expand description
An opinionated builder for Weights object.
Implementations§
source§impl BlockWeightsBuilder
impl BlockWeightsBuilder
sourcepub fn base_block(self, base_block: Weight) -> Self
pub fn base_block(self, base_block: Weight) -> Self
Set base block weight.
Examples found in repository?
304 305 306 307 308 309 310 311 312 313 314 315
pub fn simple_max(block_weight: Weight) -> Self {
Self::builder()
.base_block(Weight::zero())
.for_class(DispatchClass::all(), |weights| {
weights.base_extrinsic = Weight::zero();
})
.for_class(DispatchClass::non_mandatory(), |weights| {
weights.max_total = block_weight.into();
})
.build()
.expect("We only specify max_total and leave base values as defaults; qed")
}sourcepub fn avg_block_initialization(self, init_cost: Perbill) -> Self
pub fn avg_block_initialization(self, init_cost: Perbill) -> Self
Average block initialization weight cost.
This value is used to derive maximal allowed extrinsic weight for each class, based on the allowance.
This is to make sure that extrinsics don’t stay forever in the pool,
because they could seamingly fit the block (since they are below max_block),
but the cost of calling on_initialize always prevents them from being included.
Examples found in repository?
323 324 325 326 327 328 329 330 331 332 333 334 335 336
pub fn with_sensible_defaults(expected_block_weight: Weight, normal_ratio: Perbill) -> Self {
let normal_weight = normal_ratio * expected_block_weight;
Self::builder()
.for_class(DispatchClass::Normal, |weights| {
weights.max_total = normal_weight.into();
})
.for_class(DispatchClass::Operational, |weights| {
weights.max_total = expected_block_weight.into();
weights.reserved = (expected_block_weight - normal_weight).into();
})
.avg_block_initialization(Perbill::from_percent(10))
.build()
.expect("Sensible defaults are tested to be valid; qed")
}sourcepub fn for_class(
self,
class: impl OneOrMany<DispatchClass>,
action: impl Fn(&mut WeightsPerClass)
) -> Self
pub fn for_class(
self,
class: impl OneOrMany<DispatchClass>,
action: impl Fn(&mut WeightsPerClass)
) -> Self
Set parameters for particular class.
Note: None values of max_extrinsic will be overwritten in build in case
avg_block_initialization rate is set to a non-zero value.
Examples found in repository?
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
pub fn simple_max(block_weight: Weight) -> Self {
Self::builder()
.base_block(Weight::zero())
.for_class(DispatchClass::all(), |weights| {
weights.base_extrinsic = Weight::zero();
})
.for_class(DispatchClass::non_mandatory(), |weights| {
weights.max_total = block_weight.into();
})
.build()
.expect("We only specify max_total and leave base values as defaults; qed")
}
/// Create a sensible default weights system given only expected maximal block weight and the
/// ratio that `Normal` extrinsics should occupy.
///
/// Assumptions:
/// - Average block initialization is assumed to be `10%`.
/// - `Operational` transactions have reserved allowance (`1.0 - normal_ratio`)
pub fn with_sensible_defaults(expected_block_weight: Weight, normal_ratio: Perbill) -> Self {
let normal_weight = normal_ratio * expected_block_weight;
Self::builder()
.for_class(DispatchClass::Normal, |weights| {
weights.max_total = normal_weight.into();
})
.for_class(DispatchClass::Operational, |weights| {
weights.max_total = expected_block_weight.into();
weights.reserved = (expected_block_weight - normal_weight).into();
})
.avg_block_initialization(Perbill::from_percent(10))
.build()
.expect("Sensible defaults are tested to be valid; qed")
}sourcepub fn build(self) -> ValidationResult
pub fn build(self) -> ValidationResult
Construct the BlockWeights object.
Examples found in repository?
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
pub fn simple_max(block_weight: Weight) -> Self {
Self::builder()
.base_block(Weight::zero())
.for_class(DispatchClass::all(), |weights| {
weights.base_extrinsic = Weight::zero();
})
.for_class(DispatchClass::non_mandatory(), |weights| {
weights.max_total = block_weight.into();
})
.build()
.expect("We only specify max_total and leave base values as defaults; qed")
}
/// Create a sensible default weights system given only expected maximal block weight and the
/// ratio that `Normal` extrinsics should occupy.
///
/// Assumptions:
/// - Average block initialization is assumed to be `10%`.
/// - `Operational` transactions have reserved allowance (`1.0 - normal_ratio`)
pub fn with_sensible_defaults(expected_block_weight: Weight, normal_ratio: Perbill) -> Self {
let normal_weight = normal_ratio * expected_block_weight;
Self::builder()
.for_class(DispatchClass::Normal, |weights| {
weights.max_total = normal_weight.into();
})
.for_class(DispatchClass::Operational, |weights| {
weights.max_total = expected_block_weight.into();
weights.reserved = (expected_block_weight - normal_weight).into();
})
.avg_block_initialization(Perbill::from_percent(10))
.build()
.expect("Sensible defaults are tested to be valid; qed")
}
/// Start constructing new `BlockWeights` object.
///
/// By default all kinds except of `Mandatory` extrinsics are disallowed.
pub fn builder() -> BlockWeightsBuilder {
BlockWeightsBuilder {
weights: BlockWeights {
base_block: constants::BlockExecutionWeight::get(),
max_block: Weight::zero(),
per_class: PerDispatchClass::new(|class| {
let initial =
if class == DispatchClass::Mandatory { None } else { Some(Weight::zero()) };
WeightsPerClass {
base_extrinsic: constants::ExtrinsicBaseWeight::get(),
max_extrinsic: None,
max_total: initial,
reserved: initial,
}
}),
},
init_cost: None,
}
}
}
/// An opinionated builder for `Weights` object.
pub struct BlockWeightsBuilder {
weights: BlockWeights,
init_cost: Option<Perbill>,
}
impl BlockWeightsBuilder {
/// Set base block weight.
pub fn base_block(mut self, base_block: Weight) -> Self {
self.weights.base_block = base_block;
self
}
/// Average block initialization weight cost.
///
/// This value is used to derive maximal allowed extrinsic weight for each
/// class, based on the allowance.
///
/// This is to make sure that extrinsics don't stay forever in the pool,
/// because they could seamingly fit the block (since they are below `max_block`),
/// but the cost of calling `on_initialize` always prevents them from being included.
pub fn avg_block_initialization(mut self, init_cost: Perbill) -> Self {
self.init_cost = Some(init_cost);
self
}
/// Set parameters for particular class.
///
/// Note: `None` values of `max_extrinsic` will be overwritten in `build` in case
/// `avg_block_initialization` rate is set to a non-zero value.
pub fn for_class(
mut self,
class: impl OneOrMany<DispatchClass>,
action: impl Fn(&mut WeightsPerClass),
) -> Self {
for class in class.into_iter() {
action(self.weights.per_class.get_mut(class));
}
self
}
/// Construct the `BlockWeights` object.
pub fn build(self) -> ValidationResult {
// compute max extrinsic size
let Self { mut weights, init_cost } = self;
// compute max block size.
for class in DispatchClass::all() {
weights.max_block = match weights.per_class.get(*class).max_total {
Some(max) => max.max(weights.max_block),
_ => weights.max_block,
};
}
// compute max size of single extrinsic
if let Some(init_weight) = init_cost.map(|rate| rate * weights.max_block) {
for class in DispatchClass::all() {
let per_class = weights.per_class.get_mut(*class);
if per_class.max_extrinsic.is_none() && init_cost.is_some() {
per_class.max_extrinsic = per_class
.max_total
.map(|x| x.saturating_sub(init_weight))
.map(|x| x.saturating_sub(per_class.base_extrinsic));
}
}
}
// Validate the result
weights.validate()
}
/// Construct the `BlockWeights` object or panic if it's invalid.
///
/// This is a convenience method to be called whenever you construct a runtime.
pub fn build_or_panic(self) -> BlockWeights {
self.build().expect(
"Builder finished with `build_or_panic`; The panic is expected if runtime weights are not correct"
)
}sourcepub fn build_or_panic(self) -> BlockWeights
pub fn build_or_panic(self) -> BlockWeights
Construct the BlockWeights object or panic if it’s invalid.
This is a convenience method to be called whenever you construct a runtime.
Auto Trait Implementations§
impl RefUnwindSafe for BlockWeightsBuilder
impl Send for BlockWeightsBuilder
impl Sync for BlockWeightsBuilder
impl Unpin for BlockWeightsBuilder
impl UnwindSafe for BlockWeightsBuilder
Blanket Implementations§
source§impl<T> CheckedConversion for T
impl<T> CheckedConversion for T
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any + 'static>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T, Outer> IsWrappedBy<Outer> for Twhere
Outer: AsRef<T> + AsMut<T> + From<T>,
T: From<Outer>,
impl<T, Outer> IsWrappedBy<Outer> for Twhere
Outer: AsRef<T> + AsMut<T> + From<T>,
T: From<Outer>,
§impl<T> SaturatedConversion for T
impl<T> SaturatedConversion for T
§fn saturated_from<T>(t: T) -> Selfwhere
Self: UniqueSaturatedFrom<T>,
fn saturated_from<T>(t: T) -> Selfwhere
Self: UniqueSaturatedFrom<T>,
§fn saturated_into<T>(self) -> Twhere
Self: UniqueSaturatedInto<T>,
fn saturated_into<T>(self) -> Twhere
Self: UniqueSaturatedInto<T>,
T. Read moresource§impl<S, T> UncheckedInto<T> for Swhere
T: UncheckedFrom<S>,
impl<S, T> UncheckedInto<T> for Swhere
T: UncheckedFrom<S>,
source§fn unchecked_into(self) -> T
fn unchecked_into(self) -> T
unchecked_from.§impl<T, S> UniqueSaturatedInto<T> for Swhere
T: Bounded,
S: TryInto<T>,
impl<T, S> UniqueSaturatedInto<T> for Swhere
T: Bounded,
S: TryInto<T>,
§fn unique_saturated_into(self) -> T
fn unique_saturated_into(self) -> T
T.