Weight
Weight is an absolute unit of execution time.
The longer your extrinsic runs, the more Weight it has.
Substrate uses this to know in advance how many transactions fit in a block.
This is important to keep the block production at a constant pace.
An extrinsic in Substrate will not revert or error when it uses more Weight than specified.
For development you are safe to use 0
as your node is not doing anything else.
Later on for a real deployment you should properly benchmark it.
The Idea Of Benchmarking
The idea is to call your extrinsic with different arguments and measure how they influence the execution time.
In your example this boils down to different lengths of the argument vector.
More generally speaking you must ensure that the worst-case execution time is covered.
A Minimal Example
A benchmark for your extrinsic could look like this:
sum {
// Define a complexity component.
// This will be used as length for the vector.
// The 256 is arbitrary and a hard limit should be added to your extrinsic,
// otherwise its weight is unbounded.
let l in 0 .. 256;
// Create a vector with value `123` repeated `l` times.
let values = vec![123; l];
// The account that will sign the call.
let acc: T::AccountId = whitelisted_caller();
}: _(RawOrigin::Signed(acc), values) // Call the extrinsic.
Substrate will try to figure out how the l
component influences the execution time of your extrinsic.
If you just loop over the vector and sum up the values, a linear growth is expected.
In fact; Substrate currently only supports linear growth.
Substrate then generates a rust file which makes it possible to use weight annotations in your pallet with:
#[pallet::weight(T::WeightInfo::sum(values.len()))]
.
The whole setup for this is rather involved and requires a node.
You are best of looking at the pallet-template benchmark for a complete example and the benchmarking tutorial for a walk-through.
... or ask here obviously 😀