Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Consider changing Weight to be a 1-element tuple struct rather than a type alias to u64 #9584

@KiChjang

Description

@KiChjang

Nowadays weights are defined as a type alias to u64:

pub type Weight = u64;

This could bring in problems for functions that accept a Weight, e.g. the GasMeter constructor in the contracts pallet:

fn new(gas_limit: Weight) -> Self

There could be a potential hazard where the caller sent in a u64 that does not represent the gas limit, but instead represents some other quantity (such as a block number or an account ID). The compiler currently does not catch such kinds of errors, and will happily compile the code successfully.

More importantly, converting the type alias to a struct is part of the vision of trying to create Chromatic Weights, where not only do we record the weight of the time used for computation, we'd also include the storage used and the heap memory used to execute a particular extrinsic or function.

We could imagine that Chromatic Weights will have the following format:

struct ChromaticWeight {
    pub storage: u64,
    pub time: u64,
    pub memory: u64,
}

If I'm not mistaken, the current Weight records only the computation time used, so its value would belong to the time field. PoV size limits can then also be generalized into storage usage and so be recorded into the storage field.

Code: frame/support/src/weights.rs, and many other files that contain a function accepting Weight as a parameter

Metadata

Metadata

Assignees

No one assigned

    Labels

    I4-annoyanceThe client behaves within expectations, however this “expected behaviour” itself is at issue.I7-refactorCode needs refactoring.J0-enhancementAn additional feature request.Z2-mediumCan be fixed by a coder with good Rust knowledge but little knowledge of the codebase.Z6-mentorAn easy task where a mentor is available. Please indicate in the issue who the mentor could be.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions