Skip to main content

Linear weighting struct

Get the simple things down first.

Goal#

Understand how to calculate transaction weights using a custom weighting struct for single transaction values.

Use cases#

Calculate correct weight for a transaction involving a u32.

Overview#

This guide goes over the components of a simple weighting struct designed for a single argument dispatch of type u32. The ultimate weight of the transaction is the product of the transaction parameter and the field of this struct.

Steps#

1. Write the WeighData struct#

Using WeighData, write a weighting struct that takes a single u32 parameter:

pub struct Linear(u32);
impl WeighData<(&u32,)> for Linear {
fn weigh_data(&self, (x,): (&u32,)) -> Weight {
// Use saturation so that an extremely large
// parameter value does not cause overflow
x.saturating_mul(self.0).into()
}
}

2. Classify dispatch calls#

Since this implementation of WeighData requires a Dispatch, use [default][dispatchclass-rustdocs] to classify all calls as normal—as opposed to operational.

// Implement ClassifyDispatch
impl<T> ClassifyDispatch<T> for Linear {
fn classify_dispatch(&self, _: T) -> DispatchClass {
// Classify all calls as Normal (which is the default).
Default::default()
}
}

3. Implement PaysFee#

Implement the PaysFee trait to indicate whether fees should actually be charged from the caller. If not, the weights are still applied toward the block maximums.

// Implement PaysFee
impl<T> PaysFee<T> for Linear {
fn pays_fee(&self, _: T) -> Pays {
Pays::Yes
}
}

Examples#

  • Feeless transaction pallet
  • pallet-weights

Related material#

How-to guides#

Knowledgebase#

Other#

Was this guide useful?