Skip to content

Idea: Reflections for WeightInfo functions #382

@ggwpez

Description

@ggwpez

Currently we have the WeightInfo trait which exposes several weight functions in the form of:

pub trait WeightInfo {
	fn call_a() -> Weight;
	fn call_b() -> Weight;}

Sometimes you want to iterate over all weight functions and do something weight their name or result (eg in paritytech/substrate#12485).

One way to do this would be to have another type; WeightMetaInfo whose implementation is auto-generated as well:

pub trait WeighMetatInfo<W: WeightInfo> {
	/// Executes a callback for each weight function by passing in its name and return value.
	fn visit_weight_functions<F: Fn(&'static str, Weight) -> R, R>(f: F) -> Vec<R>;
}

This would allow us to do:

  • Mocked WeigtInfo implementations such that the weight of a call can be dynamically set in a test (Is already possible, but not in a spell-checked way without ugly macros).
  • Checking that all weights of a pallet are in a specific bound as part of the runtime-integrity check (eg. no Zero or stray todo!()).

I am not entirely convinced that this is the best design, but having this in some way would be nice.

Metadata

Metadata

Assignees

No one assigned

    Labels

    D1-mediumCan be fixed by a coder with good Rust knowledge but little knowledge of the codebase.I5-enhancementAn additional feature request.

    Type

    No type

    Projects

    Status

    Backlog

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions