Weight v1.5: Opaque Struct (#12138)

* initial idea

* update frame_support

* update a bunch more

* add ord

* adjust RuntimeDbWeight

* frame_system builds

* re-export

* frame_support tests pass

* frame_executive compile

* frame_executive builds

* frame_system tests passing

* pallet-utility tests pass

* fix a bunch of pallets

* more

* phragmen

* state-trie-migration

* scheduler and referenda

* pallet-election-provider-multi-phase

* aura

* staking

* more

* babe

* balances

* bunch more

* sudo

* transaction-payment

* asset-tx-payment

* last pallets

* fix alliance merge

* fix node template runtime

* fix pallet-contracts cc @athei

* fix node runtime

* fix compile on runtime-benchmarks feature

* comment

* fix frame-support-test

* fix more tests

* weight regex

* frame system works

* fix a bunch

* more

* more

* more

* more

* more

* more fixes

* update templates

* fix contracts benchmarks

* Update lib.rs

* Update lib.rs

* fix ui

* make scalar saturating mul const

* more const functions

* scalar div

* refactor using constant functions

* move impl

* fix overhead template

* use compactas

* Update lib.rs
This commit is contained in:
Shawn Tabrizi
2022-08-31 12:26:13 +01:00
committed by GitHub
parent 299f4ba541
commit 30951822ba
187 changed files with 5932 additions and 4930 deletions
@@ -503,17 +503,25 @@ fn call_encode_is_correct_and_decode_works() {
fn call_weight_should_attach_to_call_enum() {
use frame_support::{
dispatch::{DispatchInfo, GetDispatchInfo},
weights::{DispatchClass, Pays},
weights::{DispatchClass, Pays, Weight},
};
// operational.
assert_eq!(
module3::Call::<Runtime>::operational {}.get_dispatch_info(),
DispatchInfo { weight: 5, class: DispatchClass::Operational, pays_fee: Pays::Yes },
DispatchInfo {
weight: Weight::from_ref_time(5),
class: DispatchClass::Operational,
pays_fee: Pays::Yes
},
);
// custom basic
assert_eq!(
module3::Call::<Runtime>::aux_4 {}.get_dispatch_info(),
DispatchInfo { weight: 3, class: DispatchClass::Normal, pays_fee: Pays::Yes },
DispatchInfo {
weight: Weight::from_ref_time(3),
class: DispatchClass::Normal,
pays_fee: Pays::Yes
},
);
}
+22 -12
View File
@@ -23,7 +23,7 @@ use frame_support::{
ConstU32, GetCallName, GetStorageVersion, OnFinalize, OnGenesis, OnInitialize,
OnRuntimeUpgrade, PalletError, PalletInfoAccess, StorageVersion,
},
weights::{DispatchClass, DispatchInfo, GetDispatchInfo, Pays, RuntimeDbWeight},
weights::{DispatchClass, DispatchInfo, GetDispatchInfo, Pays, RuntimeDbWeight, Weight},
};
use scale_info::{meta_type, TypeInfo};
use sp_io::{
@@ -165,7 +165,7 @@ pub mod pallet {
let _ = T::AccountId::from(SomeType1); // Test for where clause
let _ = T::AccountId::from(SomeType2); // Test for where clause
Self::deposit_event(Event::Something(10));
10
Weight::from_ref_time(10)
}
fn on_finalize(_: BlockNumberFor<T>) {
let _ = T::AccountId::from(SomeType1); // Test for where clause
@@ -176,7 +176,7 @@ pub mod pallet {
let _ = T::AccountId::from(SomeType1); // Test for where clause
let _ = T::AccountId::from(SomeType2); // Test for where clause
Self::deposit_event(Event::Something(30));
30
Weight::from_ref_time(30)
}
fn integrity_test() {
let _ = T::AccountId::from(SomeType1); // Test for where clause
@@ -190,7 +190,7 @@ pub mod pallet {
T::AccountId: From<SomeType1> + From<SomeType3> + SomeAssociation1,
{
/// Doc comment put in metadata
#[pallet::weight(Weight::from(*_foo))]
#[pallet::weight(Weight::from_ref_time(*_foo as u64))]
pub fn foo(
origin: OriginFor<T>,
#[pallet::compact] _foo: u32,
@@ -492,14 +492,14 @@ pub mod pallet2 {
{
fn on_initialize(_: BlockNumberFor<T>) -> Weight {
Self::deposit_event(Event::Something(11));
0
Weight::zero()
}
fn on_finalize(_: BlockNumberFor<T>) {
Self::deposit_event(Event::Something(21));
}
fn on_runtime_upgrade() -> Weight {
Self::deposit_event(Event::Something(31));
0
Weight::zero()
}
}
@@ -668,7 +668,11 @@ fn call_expand() {
let call_foo = pallet::Call::<Runtime>::foo { foo: 3, bar: 0 };
assert_eq!(
call_foo.get_dispatch_info(),
DispatchInfo { weight: 3, class: DispatchClass::Normal, pays_fee: Pays::Yes }
DispatchInfo {
weight: frame_support::weights::Weight::from_ref_time(3),
class: DispatchClass::Normal,
pays_fee: Pays::Yes
}
);
assert_eq!(call_foo.get_call_name(), "foo");
assert_eq!(
@@ -1046,10 +1050,10 @@ fn pallet_hooks_expand() {
TestExternalities::default().execute_with(|| {
frame_system::Pallet::<Runtime>::set_block_number(1);
assert_eq!(AllPalletsWithoutSystem::on_initialize(1), 10);
assert_eq!(AllPalletsWithoutSystem::on_initialize(1), Weight::from_ref_time(10));
AllPalletsWithoutSystem::on_finalize(1);
assert_eq!(AllPalletsWithoutSystem::on_runtime_upgrade(), 30);
assert_eq!(AllPalletsWithoutSystem::on_runtime_upgrade(), Weight::from_ref_time(30));
assert_eq!(
frame_system::Pallet::<Runtime>::events()[0].event,
@@ -1085,10 +1089,16 @@ fn all_pallets_type_reversed_order_is_correct() {
#[allow(deprecated)]
{
assert_eq!(AllPalletsWithoutSystemReversed::on_initialize(1), 10);
assert_eq!(
AllPalletsWithoutSystemReversed::on_initialize(1),
Weight::from_ref_time(10)
);
AllPalletsWithoutSystemReversed::on_finalize(1);
assert_eq!(AllPalletsWithoutSystemReversed::on_runtime_upgrade(), 30);
assert_eq!(
AllPalletsWithoutSystemReversed::on_runtime_upgrade(),
Weight::from_ref_time(30)
);
}
assert_eq!(
@@ -1155,7 +1165,7 @@ fn migrate_from_pallet_version_to_storage_version() {
>(&db_weight);
// 4 pallets, 2 writes and every write costs 5 weight.
assert_eq!(4 * 2 * 5, weight);
assert_eq!(Weight::from_ref_time(4 * 2 * 5), weight);
// All pallet versions should be removed
assert!(sp_io::storage::get(&pallet_version_key(Example::name())).is_none());
@@ -31,7 +31,10 @@ impl SomeAssociation for u64 {
mod pallet_old {
use super::SomeAssociation;
use frame_support::{
decl_error, decl_event, decl_module, decl_storage, traits::Get, weights::Weight, Parameter,
decl_error, decl_event, decl_module, decl_storage,
traits::Get,
weights::{RefTimeWeight, Weight},
Parameter,
};
use frame_system::ensure_root;
@@ -40,7 +43,7 @@ mod pallet_old {
type Balance: Parameter
+ codec::HasCompact
+ From<u32>
+ Into<Weight>
+ Into<RefTimeWeight>
+ Default
+ SomeAssociation;
type Event: From<Event<Self>> + Into<<Self as frame_system::Config>::Event>;
@@ -75,7 +78,7 @@ mod pallet_old {
fn deposit_event() = default;
const SomeConst: T::Balance = T::SomeConst::get();
#[weight = <T::Balance as Into<Weight>>::into(new_value.clone())]
#[weight = <T::Balance as Into<RefTimeWeight>>::into(new_value.clone())]
fn set_dummy(origin, #[compact] new_value: T::Balance) {
ensure_root(origin)?;
@@ -85,7 +88,7 @@ mod pallet_old {
fn on_initialize(_n: T::BlockNumber) -> Weight {
<Dummy<T>>::put(T::Balance::from(10));
10
Weight::from_ref_time(10)
}
fn on_finalize(_n: T::BlockNumber) {
@@ -113,7 +116,7 @@ pub mod pallet {
type Balance: Parameter
+ codec::HasCompact
+ From<u32>
+ Into<Weight>
+ Into<RefTimeWeight>
+ Default
+ MaybeSerializeDeserialize
+ SomeAssociation
@@ -131,7 +134,7 @@ pub mod pallet {
impl<T: Config> Hooks<T::BlockNumber> for Pallet<T> {
fn on_initialize(_n: T::BlockNumber) -> Weight {
<Dummy<T>>::put(T::Balance::from(10));
10
Weight::from_ref_time(10)
}
fn on_finalize(_n: T::BlockNumber) {
@@ -141,7 +144,7 @@ pub mod pallet {
#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::weight(<T::Balance as Into<Weight>>::into(new_value.clone()))]
#[pallet::weight(<T::Balance as Into<RefTimeWeight>>::into(new_value.clone()))]
pub fn set_dummy(
origin: OriginFor<T>,
#[pallet::compact] new_value: T::Balance,
@@ -23,13 +23,16 @@ use frame_support::traits::{ConstU32, ConstU64};
mod pallet_old {
use frame_support::{
decl_error, decl_event, decl_module, decl_storage, traits::Get, weights::Weight, Parameter,
decl_error, decl_event, decl_module, decl_storage,
traits::Get,
weights::{RefTimeWeight, Weight},
Parameter,
};
use frame_system::ensure_root;
pub trait Config<I: Instance = DefaultInstance>: frame_system::Config {
type SomeConst: Get<Self::Balance>;
type Balance: Parameter + codec::HasCompact + From<u32> + Into<Weight> + Default;
type Balance: Parameter + codec::HasCompact + From<u32> + Into<RefTimeWeight> + Default;
type Event: From<Event<Self, I>> + Into<<Self as frame_system::Config>::Event>;
}
@@ -62,7 +65,7 @@ mod pallet_old {
fn deposit_event() = default;
const SomeConst: T::Balance = T::SomeConst::get();
#[weight = <T::Balance as Into<Weight>>::into(new_value.clone())]
#[weight = <T::Balance as Into<RefTimeWeight>>::into(new_value.clone())]
fn set_dummy(origin, #[compact] new_value: T::Balance) {
ensure_root(origin)?;
@@ -72,7 +75,7 @@ mod pallet_old {
fn on_initialize(_n: T::BlockNumber) -> Weight {
<Dummy<T, I>>::put(T::Balance::from(10));
10
Weight::from_ref_time(10)
}
fn on_finalize(_n: T::BlockNumber) {
@@ -99,7 +102,7 @@ pub mod pallet {
type Balance: Parameter
+ codec::HasCompact
+ From<u32>
+ Into<Weight>
+ Into<RefTimeWeight>
+ Default
+ MaybeSerializeDeserialize
+ scale_info::StaticTypeInfo;
@@ -116,7 +119,7 @@ pub mod pallet {
impl<T: Config<I>, I: 'static> Hooks<T::BlockNumber> for Pallet<T, I> {
fn on_initialize(_n: T::BlockNumber) -> Weight {
<Dummy<T, I>>::put(T::Balance::from(10));
10
Weight::from_ref_time(10)
}
fn on_finalize(_n: T::BlockNumber) {
@@ -126,7 +129,7 @@ pub mod pallet {
#[pallet::call]
impl<T: Config<I>, I: 'static> Pallet<T, I> {
#[pallet::weight(<T::Balance as Into<Weight>>::into(new_value.clone()))]
#[pallet::weight(<T::Balance as Into<RefTimeWeight>>::into(new_value.clone()))]
pub fn set_dummy(
origin: OriginFor<T>,
#[pallet::compact] new_value: T::Balance,
@@ -54,10 +54,10 @@ pub mod pallet {
fn on_initialize(_: BlockNumberFor<T>) -> Weight {
if TypeId::of::<I>() == TypeId::of::<()>() {
Self::deposit_event(Event::Something(10));
10
Weight::from_ref_time(10)
} else {
Self::deposit_event(Event::Something(11));
11
Weight::from_ref_time(11)
}
}
fn on_finalize(_: BlockNumberFor<T>) {
@@ -70,10 +70,10 @@ pub mod pallet {
fn on_runtime_upgrade() -> Weight {
if TypeId::of::<I>() == TypeId::of::<()>() {
Self::deposit_event(Event::Something(30));
30
Weight::from_ref_time(30)
} else {
Self::deposit_event(Event::Something(31));
31
Weight::from_ref_time(31)
}
}
fn integrity_test() {}
@@ -82,7 +82,7 @@ pub mod pallet {
#[pallet::call]
impl<T: Config<I>, I: 'static> Pallet<T, I> {
/// Doc comment put in metadata
#[pallet::weight(Weight::from(*_foo))]
#[pallet::weight(Weight::from_ref_time(*_foo as u64))]
pub fn foo(
origin: OriginFor<T>,
#[pallet::compact] _foo: u32,
@@ -345,12 +345,18 @@ frame_support::construct_runtime!(
}
);
use frame_support::weights::Weight;
#[test]
fn call_expand() {
let call_foo = pallet::Call::<Runtime>::foo { foo: 3 };
assert_eq!(
call_foo.get_dispatch_info(),
DispatchInfo { weight: 3, class: DispatchClass::Normal, pays_fee: Pays::Yes }
DispatchInfo {
weight: Weight::from_ref_time(3),
class: DispatchClass::Normal,
pays_fee: Pays::Yes
}
);
assert_eq!(call_foo.get_call_name(), "foo");
assert_eq!(pallet::Call::<Runtime>::get_call_names(), &["foo", "foo_storage_layer"]);
@@ -358,7 +364,11 @@ fn call_expand() {
let call_foo = pallet::Call::<Runtime, pallet::Instance1>::foo { foo: 3 };
assert_eq!(
call_foo.get_dispatch_info(),
DispatchInfo { weight: 3, class: DispatchClass::Normal, pays_fee: Pays::Yes }
DispatchInfo {
weight: Weight::from_ref_time(3),
class: DispatchClass::Normal,
pays_fee: Pays::Yes
}
);
assert_eq!(call_foo.get_call_name(), "foo");
assert_eq!(
@@ -651,10 +661,10 @@ fn pallet_hooks_expand() {
TestExternalities::default().execute_with(|| {
frame_system::Pallet::<Runtime>::set_block_number(1);
assert_eq!(AllPalletsWithoutSystem::on_initialize(1), 21);
assert_eq!(AllPalletsWithoutSystem::on_initialize(1), Weight::from_ref_time(21));
AllPalletsWithoutSystem::on_finalize(1);
assert_eq!(AllPalletsWithoutSystem::on_runtime_upgrade(), 61);
assert_eq!(AllPalletsWithoutSystem::on_runtime_upgrade(), Weight::from_ref_time(61));
assert_eq!(
frame_system::Pallet::<Runtime>::events()[0].event,
@@ -28,7 +28,7 @@ error[E0277]: the trait bound `Bar: EncodeLike` is not satisfied
<&[(T,)] as EncodeLike<BinaryHeap<LikeT>>>
<&[(T,)] as EncodeLike<LinkedList<LikeT>>>
<&[T] as EncodeLike<Vec<U>>>
and 278 others
and 279 others
= note: required because of the requirements on the impl of `FullEncode` for `Bar`
= note: required because of the requirements on the impl of `FullCodec` for `Bar`
= note: required because of the requirements on the impl of `PartialStorageInfoTrait` for `frame_support::pallet_prelude::StorageValue<_GeneratedPrefixForStorageFoo<T>, Bar>`
@@ -69,7 +69,7 @@ error[E0277]: the trait bound `Bar: TypeInfo` is not satisfied
(A, B, C, D)
(A, B, C, D, E)
(A, B, C, D, E, F)
and 158 others
and 159 others
= note: required because of the requirements on the impl of `StaticTypeInfo` for `Bar`
= note: required because of the requirements on the impl of `StorageEntryMetadataBuilder` for `frame_support::pallet_prelude::StorageValue<_GeneratedPrefixForStorageFoo<T>, Bar>`
@@ -103,7 +103,7 @@ error[E0277]: the trait bound `Bar: EncodeLike` is not satisfied
<&[(T,)] as EncodeLike<BinaryHeap<LikeT>>>
<&[(T,)] as EncodeLike<LinkedList<LikeT>>>
<&[T] as EncodeLike<Vec<U>>>
and 278 others
and 279 others
= note: required because of the requirements on the impl of `FullEncode` for `Bar`
= note: required because of the requirements on the impl of `FullCodec` for `Bar`
= note: required because of the requirements on the impl of `StorageEntryMetadataBuilder` for `frame_support::pallet_prelude::StorageValue<_GeneratedPrefixForStorageFoo<T>, Bar>`
@@ -28,7 +28,7 @@ error[E0277]: the trait bound `Bar: EncodeLike` is not satisfied
<&[(T,)] as EncodeLike<BinaryHeap<LikeT>>>
<&[(T,)] as EncodeLike<LinkedList<LikeT>>>
<&[T] as EncodeLike<Vec<U>>>
and 278 others
and 279 others
= note: required because of the requirements on the impl of `FullEncode` for `Bar`
= note: required because of the requirements on the impl of `FullCodec` for `Bar`
= note: required because of the requirements on the impl of `PartialStorageInfoTrait` for `frame_support::pallet_prelude::StorageValue<_GeneratedPrefixForStorageFoo<T>, Bar>`
@@ -69,7 +69,7 @@ error[E0277]: the trait bound `Bar: TypeInfo` is not satisfied
(A, B, C, D)
(A, B, C, D, E)
(A, B, C, D, E, F)
and 158 others
and 159 others
= note: required because of the requirements on the impl of `StaticTypeInfo` for `Bar`
= note: required because of the requirements on the impl of `StorageEntryMetadataBuilder` for `frame_support::pallet_prelude::StorageValue<_GeneratedPrefixForStorageFoo<T>, Bar>`
@@ -103,7 +103,7 @@ error[E0277]: the trait bound `Bar: EncodeLike` is not satisfied
<&[(T,)] as EncodeLike<BinaryHeap<LikeT>>>
<&[(T,)] as EncodeLike<LinkedList<LikeT>>>
<&[T] as EncodeLike<Vec<U>>>
and 278 others
and 279 others
= note: required because of the requirements on the impl of `FullEncode` for `Bar`
= note: required because of the requirements on the impl of `FullCodec` for `Bar`
= note: required because of the requirements on the impl of `StorageEntryMetadataBuilder` for `frame_support::pallet_prelude::StorageValue<_GeneratedPrefixForStorageFoo<T>, Bar>`
@@ -13,5 +13,5 @@ error[E0277]: the trait bound `Bar: MaxEncodedLen` is not satisfied
(TupleElement0, TupleElement1, TupleElement2, TupleElement3, TupleElement4, TupleElement5)
(TupleElement0, TupleElement1, TupleElement2, TupleElement3, TupleElement4, TupleElement5, TupleElement6)
(TupleElement0, TupleElement1, TupleElement2, TupleElement3, TupleElement4, TupleElement5, TupleElement6, TupleElement7)
and 75 others
and 76 others
= note: required because of the requirements on the impl of `StorageInfoTrait` for `frame_support::pallet_prelude::StorageValue<_GeneratedPrefixForStorageFoo<T>, Bar>`
@@ -13,6 +13,6 @@ error[E0277]: the trait bound `Bar: MaxEncodedLen` is not satisfied
(TupleElement0, TupleElement1, TupleElement2, TupleElement3, TupleElement4, TupleElement5)
(TupleElement0, TupleElement1, TupleElement2, TupleElement3, TupleElement4, TupleElement5, TupleElement6)
(TupleElement0, TupleElement1, TupleElement2, TupleElement3, TupleElement4, TupleElement5, TupleElement6, TupleElement7)
and 75 others
and 76 others
= note: required because of the requirements on the impl of `KeyGeneratorMaxEncodedLen` for `Key<frame_support::Twox64Concat, Bar>`
= note: required because of the requirements on the impl of `StorageInfoTrait` for `frame_support::pallet_prelude::StorageNMap<_GeneratedPrefixForStorageFoo<T>, Key<frame_support::Twox64Concat, Bar>, u32>`
@@ -54,7 +54,7 @@ frame_support::decl_module! {
}
fn on_initialize(_n: T::BlockNumber) -> frame_support::weights::Weight {
0
frame_support::weights::Weight::zero()
}
}
}