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

Weight V2 (Chromatic Weight) #10918

Closed
wants to merge 103 commits into from
Closed

Weight V2 (Chromatic Weight) #10918

wants to merge 103 commits into from

Conversation

shawntabrizi
Copy link
Member

@shawntabrizi shawntabrizi commented Feb 25, 2022

This is an initial PR where we change the Weight type from an primitive u64 to:

pub struct Weight {
	/// The weight of computational time used.
	ref_time: ComputationalWeight,
	/// The weight of the storage bandwidth used.
	proof_size: StorageWeight,
}

Fields are private to allow adding of new fields without needing to touch everything, again.

The strategy here is to migrate the core weight checking logic within frame_system and frame_support. Majority of pallets will still use type ComputationWeight = u64. We will convert all the places that still use the primitive types in a follow up PR.

Right now the bandwidth parameter correctly handled in frame_executive and frame_system, and we will make sure that new extrinsics do not bypass that limit, but basically all Pallets simply return 0 bandwidth for now, so this logic is not really used.

The check_weight extension takes the bandwith into account. The transaction-payment extension only looks at the computation. We will need to come up with a way to fee PoV usage in a follow up. So right now we only make sure that not too much bandwith is used but don't incur a fee for it.

For migrating existing pallets to the new Weight definition we will simply use a Weight instance with computation: weight_v1, bandwidth: 0. The computation part can be accessed with a simple getter weight.computation().

After this PR is merged, each pallet can be migrated one by one to correctly handle the bandwidth parameter.

  • Remove all notions of WeightV1 and replace it by a normal Weight with computation = 0.
  • Make fields of Weight private so we can add more fields later without breaking all the users

fixes #9584

Polkadot companion: paritytech/polkadot#5435

@github-actions github-actions bot added the A0-please_review Pull request needs code review. label Feb 25, 2022
@ggwpez
Copy link
Member

ggwpez commented Feb 25, 2022

The StorageWeight is mainly POV size I assume?

Not sure what is nicer from the naming, but when we do not want to litter the weights mod we could add a v2 mod:

use frame_support::weights::v2::Weight;
// vs
use frame_support::weights::Weight2 as Weight;

@athei
Copy link
Member

athei commented Feb 25, 2022

The StorageWeight is mainly POV size I assume?

FRAME is oblivious to this concept. PoV is what a collator sends to a validator for validation. In the cumulus case this is roughly a witness + extrinsics. For the extrinsics we already account in FRAME (this is the block lenght). The witness is what is missing from the equation. This PoV metering just assumes that accessing storage will grow the witness.

@ggwpez
Copy link
Member

ggwpez commented Feb 25, 2022

Thanks for the explanation @athei !

@ggwpez
Copy link
Member

ggwpez commented May 17, 2022

I think we still need to run the bench bot, to see if the template still works.

@@ -673,11 +675,11 @@ impl pallet_election_provider_multi_phase::MinerConfig for Runtime {
// The unsigned submissions have to respect the weight of the submit_unsigned call, thus their
// weight estimate function is wired to this call's weight.
fn solution_weight(v: u32, t: u32, a: u32, d: u32) -> Weight {
<
Weight::from_computation(<
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does the WeightInfo not now give the new Weight back from its functions?

@@ -39,7 +39,7 @@
#![allow(unused_parens)]
#![allow(unused_imports)]

use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}};
use frame_support::{traits::Get, weights::{ComputationWeight as Weight, constants::RocksDbWeight}};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the weights.rs files be returning the full weight, not just computation weight? Where would we expect the PoV bandwidth weight dimension to come from?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is a needed follow up to regenerate weights with the proper template and values. This is just a backwards compat hack for now.

}
}

/// Construct with computation weight only (zero `bandwith`).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Construct with computation weight only (zero `bandwith`).
/// Construct with computation weight only (zero `bandwidth`).

}
}

/// Try to add some `other` weight while upholding the `limit`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs explicitly mentioning what happens in case of overflow or exceeding any of the limits

}
}

/// Get the aggressive max of `self` and `other` weight.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unclear what does aggressive mean here

pub const MAX: Self =
Self { computation: ComputationWeight::MAX, bandwidth: BandwidthWeight::MAX };

/// Get the conservative min of `self` and `other` weight.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as aggressive

@kianenigma
Copy link
Contributor

Canonical names:

  • ref_time
  • proof_size

Would be good to make a function explicitly named for backwards compatibility, like from_legacy() or from_v1() that is equivalent to from_ref_time, so that it can be easily identified and used as TODO.

self.computation < other.computation || self.bandwidth < other.bandwidth
}

/// Checks if any param of `self` is less than `other`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Checks if any param of `self` is less than `other`.
/// Checks if any param of `self` is greater than `other`.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A3-in_progress Pull request is in progress. No review needed at this stage. C1-low PR touches the given topic and has a low impact on builders. D2-breaksapi
Projects
Status: Done
Status: Done
Development

Successfully merging this pull request may close these issues.

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