Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
c992d67
Basic weights builder.
tomusdrw Jun 23, 2020
52e2033
Fixing WiP
tomusdrw Jun 24, 2020
350976e
Merge branch 'master' into td-weight-parameters-refactor
tomusdrw Jun 25, 2020
01b9aea
Make the tests work.
tomusdrw Jul 1, 2020
201a5ba
Merge branch 'master' into td-weight-parameters-refactor
tomusdrw Jul 1, 2020
6c2676d
Fix weights in node/runtime.
tomusdrw Jul 2, 2020
390a359
Merge branch 'master' into td-weight-parameters-refactor
tomusdrw Jul 6, 2020
4e11daf
WiP.
tomusdrw Jul 6, 2020
f914485
Update pallets with new weights parameters.
tomusdrw Jul 6, 2020
17ce9a3
Validate returns a Result now.
tomusdrw Jul 7, 2020
44e5981
Count mandatory weight separately.
tomusdrw Jul 7, 2020
141fbfb
DRY
tomusdrw Jul 7, 2020
3f0a2e9
BREAKING: Updating state root, because of the left-over weight-tracki…
tomusdrw Jul 8, 2020
782cc72
Update tests affected by Mandatory tracking.
tomusdrw Jul 8, 2020
8185cb1
Fixing tests.
tomusdrw Jul 9, 2020
1525c76
Fix defaults for simple_max
tomusdrw Jul 9, 2020
9441b00
Merge branch 'master' into td-weight-parameters-refactor
tomusdrw Jul 9, 2020
9f3a541
Merge branch 'master' into td-weight-parameters-refactor
tomusdrw Jul 10, 2020
2ab1f18
Merge branch 'master' into td-weight-parameters-refactor
tomusdrw Jul 16, 2020
f09c6f3
Update frame/system/src/weights.rs
tomusdrw Jul 17, 2020
eb84c74
Rework the API a bit.
tomusdrw Jul 23, 2020
edab564
Fix compilation & tests.
tomusdrw Jul 23, 2020
738ac18
Merge remote-tracking branch 'origin/td-weight-parameters-refactor' i…
tomusdrw Jul 23, 2020
319ee5c
Merge branch 'master' into td-weight-parameters-refactor
tomusdrw Jul 23, 2020
da4ff09
Merge branch 'master' into td-weight-parameters-refactor
tomusdrw Jul 27, 2020
95e249c
Apply suggestions from code review
tomusdrw Jul 28, 2020
22ef5d3
Add extra docs & rename few things.
tomusdrw Jul 28, 2020
cad3d96
Fix whitespace in ASCII art.
tomusdrw Jul 28, 2020
22fa090
Merge branch 'master' into td-weight-parameters-refactor
tomusdrw Jul 28, 2020
eb75ba3
Merge branch 'master' into td-weight-parameters-refactor
tomusdrw Jul 30, 2020
98fdb4a
Merge branch 'master' into td-weight-parameters-refactor
tomusdrw Jul 31, 2020
a2359eb
Update frame/system/src/limits.rs
tomusdrw Aug 5, 2020
bea4dd0
Merge branch 'master' into td-weight-parameters-refactor
tomusdrw Aug 5, 2020
31706c6
Fix max_extrinsic calculations.
tomusdrw Aug 6, 2020
fa3203c
Merge branch 'master' into td-weight-parameters-refactor
tomusdrw Aug 18, 2020
e197126
Fix conflicts.
tomusdrw Aug 18, 2020
804bee5
Merge branch 'master' into td-weight-parameters-refactor
tomusdrw Aug 20, 2020
f4da99a
Merge branch 'master' into td-weight-parameters-refactor
tomusdrw Aug 28, 2020
233a5ff
Merge branch 'master' into td-weight-parameters-refactor
tomusdrw Sep 9, 2020
da260bd
Fix compilation.
tomusdrw Sep 9, 2020
91366d9
Merge branch 'master' into td-weight-parameters-refactor
tomusdrw Sep 21, 2020
1e1bcf7
Fix new code.
tomusdrw Sep 21, 2020
9b7d331
re-remove generic asset
shawntabrizi Sep 22, 2020
6176f8e
Merge branch 'master' into td-weight-parameters-refactor
gnunicorn Sep 22, 2020
0a138ac
Merge branch 'master' into td-weight-parameters-refactor
tomusdrw Sep 23, 2020
b69b3e5
Fix usage.
tomusdrw Sep 23, 2020
d6e7776
Update state root.
tomusdrw Sep 23, 2020
2d8875f
Merge branch 'master' into td-weight-parameters-refactor
tomusdrw Sep 28, 2020
6e8ff1d
Update proxy.
tomusdrw Sep 28, 2020
b12359d
Merge branch 'master' into td-weight-parameters-refactor
tomusdrw Nov 17, 2020
45c6fe6
Fix tests.
tomusdrw Nov 17, 2020
543891f
Merge branch 'master' into td-weight-parameters-refactor
tomusdrw Nov 25, 2020
44946a8
Move weights validity to integrity_test
tomusdrw Nov 27, 2020
2cf0631
Remove redundant BlockWeights.
tomusdrw Nov 27, 2020
6f00731
Add all/non_mandatory comment
tomusdrw Nov 27, 2020
7a8b774
Add test.
tomusdrw Nov 27, 2020
8bc7ca5
Remove fn block_weights
tomusdrw Dec 2, 2020
d115561
Make the macro prettier.
tomusdrw Dec 2, 2020
f18cbab
Fix some docs.
tomusdrw Dec 2, 2020
6e4c35c
Merge branch 'master' into td-weight-parameters-refactor
tomusdrw Dec 2, 2020
b4c293c
Merge branch 'master' into td-weight-parameters-refactor
tomusdrw Dec 2, 2020
e2ee2b9
Make max_total behave more predictabily.
tomusdrw Dec 3, 2020
953f845
Add BlockWeights to metadata.
tomusdrw Dec 3, 2020
4854b95
Merge branch 'master' into td-weight-parameters-refactor
shawntabrizi Dec 7, 2020
ddc57b0
fix balances test
gui1117 Dec 8, 2020
ab0ba02
Fix utility test.
tomusdrw Dec 8, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Basic weights builder.
  • Loading branch information
tomusdrw committed Jun 23, 2020
commit c992d674164f340f9d2198ba44b02c84b02aac4c
1 change: 1 addition & 0 deletions bin/node-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ parameter_types! {
impl system::Trait for Runtime {
/// The basic call filter to use in dispatchable.
type BaseCallFilter = ();
type Weights = ();
/// The identifier used to distinguish between accounts.
type AccountId = AccountId;
/// The aggregated dispatch type that is available for extrinsics.
Expand Down
22 changes: 20 additions & 2 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ use frame_support::{
construct_runtime, parameter_types, debug, RuntimeDebug,
weights::{
Weight, IdentityFee,
constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight, WEIGHT_PER_SECOND},
constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight, WEIGHT_PER_SECOND}, DispatchClass,
},
traits::{Currency, Imbalance, KeyOwnerProofSystem, OnUnbalanced, Randomness, LockIdentifier},
};
use frame_system::{EnsureRoot, EnsureOneOf};
use frame_system::{EnsureRoot, EnsureOneOf, Weights, ExtrinsicDispatchClass};
use frame_support::traits::InstanceFilter;
use codec::{Encode, Decode};
use sp_core::{
Expand Down Expand Up @@ -142,12 +142,25 @@ parameter_types! {
* MaximumBlockWeight::get();
pub const MaximumBlockLength: u32 = 5 * 1024 * 1024;
pub const Version: RuntimeVersion = VERSION;

pub RuntimeWeights: Weights = Weights::builder()
.base_block(BlockExecutionWeight::get())
.base_extrinsic(ExtrinsicBaseWeight::get(), ExtrinsicDispatchClass::All)
.max_total(AvailableBlockRatio::get() * MaximumBlockWeight::get(), DispatchClass::Normal)
.max_total(MaximumBlockWeight::get(), DispatchClass::Operational)
.reserved(
Perbill::one().saturating_sub(AvailableBlockRatio::get()) * MaximumBlockWeight::get(),
DispatchClass::Operational
)
.avg_block_initialization(AVERAGE_ON_INITIALIZE_WEIGHT)
.build();
}

const_assert!(AvailableBlockRatio::get().deconstruct() >= AVERAGE_ON_INITIALIZE_WEIGHT.deconstruct());

impl frame_system::Trait for Runtime {
type BaseCallFilter = ();
type Weights = RuntimeWeights;
type Origin = Origin;
type Call = Call;
type Index = Index;
Expand Down Expand Up @@ -1115,4 +1128,9 @@ mod tests {

is_submit_signed_transaction::<Runtime>();
}

#[test]
fn weights_are_valid() {
RuntimeWeights::get().validate()
}
}
1 change: 1 addition & 0 deletions frame/balances/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,7 @@ impl<T: Subtrait<I>, I: Instance> PartialEq for ElevatedTrait<T, I> {
impl<T: Subtrait<I>, I: Instance> Eq for ElevatedTrait<T, I> {}
impl<T: Subtrait<I>, I: Instance> frame_system::Trait for ElevatedTrait<T, I> {
type BaseCallFilter = T::BaseCallFilter;
type Weights = T::Weights;
type Origin = T::Origin;
type Call = T::Call;
type Index = T::Index;
Expand Down
1 change: 1 addition & 0 deletions frame/generic-asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,7 @@ impl<T: Subtrait> PartialEq for ElevatedTrait<T> {
impl<T: Subtrait> Eq for ElevatedTrait<T> {}
impl<T: Subtrait> frame_system::Trait for ElevatedTrait<T> {
type BaseCallFilter = T::BaseCallFilter;
type Weights = T::Weights;
type Origin = T::Origin;
type Call = T::Call;
type Index = T::Index;
Expand Down
1 change: 1 addition & 0 deletions frame/staking/fuzzer/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ pub struct Test;

impl frame_system::Trait for Test {
type BaseCallFilter = ();
type Weights = ();
type Origin = Origin;
type DbWeight = ();
type BlockExecutionWeight = ();
Expand Down
2 changes: 1 addition & 1 deletion frame/support/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ pub trait WeightToFeePolynomial {
Self::polynomial().iter().fold(Self::Balance::saturated_from(0u32), |mut acc, args| {
let w = Self::Balance::saturated_from(*weight).saturating_pow(args.degree.into());

// The sum could get negative. Therefore we only sum with the accumulator.
// The sum could get negative. Therefore we only sum with the accumulator.
// The Perbill Mul implementation is non overflowing.
let frac = args.coeff_frac * w;
let integer = args.coeff_integer.saturating_mul(w);
Expand Down
216 changes: 216 additions & 0 deletions frame/system/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,11 +148,227 @@ pub fn extrinsics_data_root<H: Hash>(xts: Vec<Vec<u8>>) -> H::Output {
H::ordered_trie_root(xts)
}

/// A struct holding value for each `DispatchClass`.
#[derive(RuntimeDebug, Default, Clone)]
pub struct ForDispatchClass<T> {
/// Value for `Normal` extrinsics.
pub normal: T,
/// Value for `Operational` extrinsics.
pub operational: T,
/// Value for `Mandatory` extrinsics.
pub mandatory: T,
}

impl<T: Copy> ForDispatchClass<T> {
/// Set the same value for all classes.
pub fn set(&mut self, value: T, class: ExtrinsicDispatchClass) {
match class {
ExtrinsicDispatchClass::All => {
self.normal = value;
self.operational = value;
self.mandatory = value;
},
ExtrinsicDispatchClass::One(DispatchClass::Normal) => {
self.normal = value;
},
ExtrinsicDispatchClass::One(DispatchClass::Operational) => {
self.operational = value;
},
ExtrinsicDispatchClass::One(DispatchClass::Mandatory) => {
self.mandatory = value;
},
}
}
}

struct Verified;
struct Unverified;

/// Weights configuration.
#[derive(RuntimeDebug, Clone)]
pub struct Weights {
/// Base weight of block execution.
pub base_block: Weight,
/// Base weight of single extrinsic of given class.
pub base_extrinsic: ForDispatchClass<Weight>,
/// Maximal weight of single extrinsic of given class.
///
/// `None` indicates that this class of extrinsics doesn't have a limit.
pub max_extrinsic: ForDispatchClass<Option<Weight>>,
/// Block reserved allowance for all extrinsics of a particular class.
///
/// Setting to `None` indicates that extrinsics of that class are allowed
/// to go over total block weight (but at most `max_allowance`).
/// Setting to `Some(x)` guarantees that at least `x` weight of particular class
/// is processed in every block.
pub reserved: ForDispatchClass<Option<Weight>>,
/// Block maximal total weight for all extrinsics of a particular class.
///
/// `None` indicates that weight sum of this class of extrinsics is not
/// restricted. Use this value carefuly, since it might produce heavily oversized
/// blocks.
///
/// In the worst case, the total weight consumed by a block is going to be:
/// `MAX(max_total) + MAX(reserved)`.
pub max_total: ForDispatchClass<Option<Weight>>,
}

impl Weights {
/// Verifies correctness of this `Weights` object.
pub fn validate(&self) {
fn or_max(w: Option<Weight>) -> Weight {
w.unwrap_or_else(|| Weight::max_value())
}
// Base cost must not be greater than max cost.
assert!(self.base_extrinsic.normal < or_max(self.max_extrinsic.normal));
assert!(self.base_extrinsic.operational < or_max(self.max_extrinsic.operational));
assert!(self.base_extrinsic.mandatory < or_max(self.max_extrinsic.mandatory));
// Max extrinsic can't be greater than max_total.
assert!(or_max(self.max_extrinsic.normal) <= or_max(self.max_total.normal));
assert!(or_max(self.max_extrinsic.operational) <= or_max(self.max_total.operational));
assert!(or_max(self.max_extrinsic.mandatory) <= or_max(self.max_total.mandatory));
// Make sure that if reserved is set it's greater than base_extrinsic.
assert!(
or_max(self.reserved.normal) > self.base_extrinsic.normal
|| or_max(self.reserved.normal) == 0
);
assert!(
or_max(self.reserved.operational) > self.base_extrinsic.operational
|| or_max(self.reserved.operational) == 0
);
assert!(
or_max(self.reserved.mandatory) > self.base_extrinsic.mandatory
|| or_max(self.reserved.mandatory) == 0
);
// Make sure that if total is set it's greater than base_block && base_extrinsic
assert!((
or_max(self.max_total.normal) > self.base_block
&& or_max(self.max_total.normal) > self.base_extrinsic.normal
) || or_max(self.max_total.normal) == 0);
assert!((
or_max(self.max_total.operational) > self.base_block
&& or_max(self.max_total.operational) > self.base_extrinsic.operational
) || or_max(self.max_total.operational) == 0);
assert!((
or_max(self.max_total.mandatory) > self.base_block
&& or_max(self.max_total.mandatory) > self.base_extrinsic.mandatory
) || or_max(self.max_total.mandatory) == 0);

}

/// Start constructing new `Weights` object.
///
/// By default all kinds except of `Mandatory` extrinsics are disallowed.
pub fn builder() -> WeightsBuilder {
let max_total = ForDispatchClass {
normal: Some(0),
operational: Some(0),
mandatory: None,
};
WeightsBuilder {
weights: Weights {
base_block: 0,
base_extrinsic: Default::default(),
max_extrinsic: max_total.clone(),
reserved: max_total.clone(),
max_total,
},
init_cost: Perbill::zero(),
}
}
}

/// Dispatch class to apply change to.
pub enum ExtrinsicDispatchClass {
/// All dispatch classes.
All,
/// Single dispatch class.
One(DispatchClass),
}

impl From<DispatchClass> for ExtrinsicDispatchClass {
fn from(c: DispatchClass) -> Self {
ExtrinsicDispatchClass::One(c)
}
}

pub struct WeightsBuilder {
weights: Weights,
init_cost: Perbill,
}

impl WeightsBuilder {
/// Set base block weight.
pub fn base_block(mut self, base_block: Weight) -> Self {
self.weights.base_block = base_block;
self
}

/// Set base extrinsic weight for given dispatch class.
pub fn base_extrinsic(
mut self,
weight: Weight,
class: impl Into<ExtrinsicDispatchClass>,
) -> Self {
self.weights.base_extrinsic.set(weight, class.into());
self
}

/// Set maximal block total weight for given dispatch class.
pub fn max_total(
mut self,
max_allowance: Weight,
class: impl Into<ExtrinsicDispatchClass>,
) -> Self {
self.weights.max_total.set(max_allowance.into(), class.into());
self
}

/// Set reserved allowance for given dispatch class.
pub fn reserved(
mut self,
reserved: Weight,
class: impl Into<ExtrinsicDispatchClass>,
) -> Self {
self.weights.reserved.set(reserved.into(), class.into());
self
}

/// Average block initialization weight cost.
///
/// This value is used to derive maximal allowed extrinsic weight for each
/// class, based on the allowance.
pub fn avg_block_initialization(mut self, init_cost: Perbill) -> Self {
self.init_cost = init_cost;
self
}

/// Construct the `Weights` object.
pub fn build(self) -> Weights {
use sp_runtime::traits::Saturating;
// compute max extrinsic size
let Self { mut weights, init_cost } = self;

// compute max weight of single extrinsic
let max_rate = Perbill::one().saturating_sub(init_cost);
weights.max_extrinsic.normal = weights.max_total.normal.map(|x| max_rate * x);
weights.max_extrinsic.operational = weights.max_total.operational.map(|x| max_rate * x);
weights.max_extrinsic.mandatory = weights.max_total.mandatory.map(|x| max_rate * x);

weights.validate();

weights
}
}

pub trait Trait: 'static + Eq + Clone {
/// The basic call filter to use in Origin. All origins are built with this filter as base,
/// except Root.
type BaseCallFilter: Filter<Self::Call>;

/// Block, Extrinsics & DB weights configuration.
type Weights: Get<Weights>;

/// The `Origin` type used by dispatchable calls.
type Origin:
Into<Result<RawOrigin<Self::AccountId>, Self::Origin>>
Expand Down