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
@@ -59,19 +59,19 @@ const CALL: &<Runtime as frame_system::Config>::Call =
&Call::Balances(BalancesCall::transfer { dest: 2, value: 69 });
thread_local! {
static EXTRINSIC_BASE_WEIGHT: RefCell<u64> = RefCell::new(0);
static EXTRINSIC_BASE_WEIGHT: RefCell<Weight> = RefCell::new(Weight::new());
}
pub struct BlockWeights;
impl Get<frame_system::limits::BlockWeights> for BlockWeights {
fn get() -> frame_system::limits::BlockWeights {
frame_system::limits::BlockWeights::builder()
.base_block(0)
.base_block(Weight::zero())
.for_class(DispatchClass::all(), |weights| {
weights.base_extrinsic = EXTRINSIC_BASE_WEIGHT.with(|v| *v.borrow()).into();
})
.for_class(DispatchClass::non_mandatory(), |weights| {
weights.max_total = 1024.into();
weights.max_total = Weight::from_ref_time(1024).into();
})
.build_or_panic()
}
@@ -129,7 +129,8 @@ impl WeightToFeeT for WeightToFee {
type Balance = u64;
fn weight_to_fee(weight: &Weight) -> Self::Balance {
Self::Balance::saturated_from(*weight).saturating_mul(WEIGHT_TO_FEE.with(|v| *v.borrow()))
Self::Balance::saturated_from(weight.ref_time())
.saturating_mul(WEIGHT_TO_FEE.with(|v| *v.borrow()))
}
}
@@ -137,7 +138,7 @@ impl WeightToFeeT for TransactionByteFee {
type Balance = u64;
fn weight_to_fee(weight: &Weight) -> Self::Balance {
Self::Balance::saturated_from(*weight)
Self::Balance::saturated_from(weight.ref_time())
.saturating_mul(TRANSACTION_BYTE_FEE.with(|v| *v.borrow()))
}
}
@@ -208,19 +209,24 @@ impl Config for Runtime {
pub struct ExtBuilder {
balance_factor: u64,
base_weight: u64,
base_weight: Weight,
byte_fee: u64,
weight_to_fee: u64,
}
impl Default for ExtBuilder {
fn default() -> Self {
Self { balance_factor: 1, base_weight: 0, byte_fee: 1, weight_to_fee: 1 }
Self {
balance_factor: 1,
base_weight: Weight::from_ref_time(0),
byte_fee: 1,
weight_to_fee: 1,
}
}
}
impl ExtBuilder {
pub fn base_weight(mut self, base_weight: u64) -> Self {
pub fn base_weight(mut self, base_weight: Weight) -> Self {
self.base_weight = base_weight;
self
}
@@ -283,19 +289,19 @@ fn transaction_payment_in_native_possible() {
let balance_factor = 100;
ExtBuilder::default()
.balance_factor(balance_factor)
.base_weight(5)
.base_weight(Weight::from_ref_time(5))
.build()
.execute_with(|| {
let len = 10;
let pre = ChargeAssetTxPayment::<Runtime>::from(0, None)
.pre_dispatch(&1, CALL, &info_from_weight(5), len)
.pre_dispatch(&1, CALL, &info_from_weight(Weight::from_ref_time(5)), len)
.unwrap();
let initial_balance = 10 * balance_factor;
assert_eq!(Balances::free_balance(1), initial_balance - 5 - 5 - 10);
assert_ok!(ChargeAssetTxPayment::<Runtime>::post_dispatch(
Some(pre),
&info_from_weight(5),
&info_from_weight(Weight::from_ref_time(5)),
&default_post_info(),
len,
&Ok(())
@@ -303,15 +309,15 @@ fn transaction_payment_in_native_possible() {
assert_eq!(Balances::free_balance(1), initial_balance - 5 - 5 - 10);
let pre = ChargeAssetTxPayment::<Runtime>::from(5 /* tipped */, None)
.pre_dispatch(&2, CALL, &info_from_weight(100), len)
.pre_dispatch(&2, CALL, &info_from_weight(Weight::from_ref_time(100)), len)
.unwrap();
let initial_balance_for_2 = 20 * balance_factor;
assert_eq!(Balances::free_balance(2), initial_balance_for_2 - 5 - 10 - 100 - 5);
assert_ok!(ChargeAssetTxPayment::<Runtime>::post_dispatch(
Some(pre),
&info_from_weight(100),
&post_info_from_weight(50),
&info_from_weight(Weight::from_ref_time(100)),
&post_info_from_weight(Weight::from_ref_time(50)),
len,
&Ok(())
));
@@ -325,7 +331,7 @@ fn transaction_payment_in_asset_possible() {
let balance_factor = 100;
ExtBuilder::default()
.balance_factor(balance_factor)
.base_weight(base_weight)
.base_weight(Weight::from_ref_time(base_weight))
.build()
.execute_with(|| {
// create the asset
@@ -351,7 +357,7 @@ fn transaction_payment_in_asset_possible() {
// existential deposit
let fee = (base_weight + weight + len as u64) * min_balance / ExistentialDeposit::get();
let pre = ChargeAssetTxPayment::<Runtime>::from(0, Some(asset_id))
.pre_dispatch(&caller, CALL, &info_from_weight(weight), len)
.pre_dispatch(&caller, CALL, &info_from_weight(Weight::from_ref_time(weight)), len)
.unwrap();
// assert that native balance is not used
assert_eq!(Balances::free_balance(caller), 10 * balance_factor);
@@ -361,7 +367,7 @@ fn transaction_payment_in_asset_possible() {
assert_ok!(ChargeAssetTxPayment::<Runtime>::post_dispatch(
Some(pre),
&info_from_weight(weight),
&info_from_weight(Weight::from_ref_time(weight)),
&default_post_info(),
len,
&Ok(())
@@ -378,7 +384,7 @@ fn transaction_payment_without_fee() {
let balance_factor = 100;
ExtBuilder::default()
.balance_factor(balance_factor)
.base_weight(base_weight)
.base_weight(Weight::from_ref_time(base_weight))
.build()
.execute_with(|| {
// create the asset
@@ -404,7 +410,7 @@ fn transaction_payment_without_fee() {
// existential deposit
let fee = (base_weight + weight + len as u64) * min_balance / ExistentialDeposit::get();
let pre = ChargeAssetTxPayment::<Runtime>::from(0, Some(asset_id))
.pre_dispatch(&caller, CALL, &info_from_weight(weight), len)
.pre_dispatch(&caller, CALL, &info_from_weight(Weight::from_ref_time(weight)), len)
.unwrap();
// assert that native balance is not used
assert_eq!(Balances::free_balance(caller), 10 * balance_factor);
@@ -414,7 +420,7 @@ fn transaction_payment_without_fee() {
assert_ok!(ChargeAssetTxPayment::<Runtime>::post_dispatch(
Some(pre),
&info_from_weight(weight),
&info_from_weight(Weight::from_ref_time(weight)),
&post_info_from_pays(Pays::No),
len,
&Ok(())
@@ -431,7 +437,7 @@ fn asset_transaction_payment_with_tip_and_refund() {
let base_weight = 5;
ExtBuilder::default()
.balance_factor(100)
.base_weight(base_weight)
.base_weight(Weight::from_ref_time(base_weight))
.build()
.execute_with(|| {
// create the asset
@@ -459,15 +465,15 @@ fn asset_transaction_payment_with_tip_and_refund() {
let fee_with_tip =
(base_weight + weight + len as u64 + tip) * min_balance / ExistentialDeposit::get();
let pre = ChargeAssetTxPayment::<Runtime>::from(tip, Some(asset_id))
.pre_dispatch(&caller, CALL, &info_from_weight(weight), len)
.pre_dispatch(&caller, CALL, &info_from_weight(Weight::from_ref_time(weight)), len)
.unwrap();
assert_eq!(Assets::balance(asset_id, caller), balance - fee_with_tip);
let final_weight = 50;
assert_ok!(ChargeAssetTxPayment::<Runtime>::post_dispatch(
Some(pre),
&info_from_weight(weight),
&post_info_from_weight(final_weight),
&info_from_weight(Weight::from_ref_time(weight)),
&post_info_from_weight(Weight::from_ref_time(final_weight)),
len,
&Ok(())
));
@@ -483,7 +489,7 @@ fn payment_from_account_with_only_assets() {
let base_weight = 5;
ExtBuilder::default()
.balance_factor(100)
.base_weight(base_weight)
.base_weight(Weight::from_ref_time(base_weight))
.build()
.execute_with(|| {
// create the asset
@@ -511,7 +517,7 @@ fn payment_from_account_with_only_assets() {
// existential deposit
let fee = (base_weight + weight + len as u64) * min_balance / ExistentialDeposit::get();
let pre = ChargeAssetTxPayment::<Runtime>::from(0, Some(asset_id))
.pre_dispatch(&caller, CALL, &info_from_weight(weight), len)
.pre_dispatch(&caller, CALL, &info_from_weight(Weight::from_ref_time(weight)), len)
.unwrap();
assert_eq!(Balances::free_balance(caller), 0);
// check that fee was charged in the given asset
@@ -519,7 +525,7 @@ fn payment_from_account_with_only_assets() {
assert_ok!(ChargeAssetTxPayment::<Runtime>::post_dispatch(
Some(pre),
&info_from_weight(weight),
&info_from_weight(Weight::from_ref_time(weight)),
&default_post_info(),
len,
&Ok(())
@@ -534,7 +540,7 @@ fn payment_only_with_existing_sufficient_asset() {
let base_weight = 5;
ExtBuilder::default()
.balance_factor(100)
.base_weight(base_weight)
.base_weight(Weight::from_ref_time(base_weight))
.build()
.execute_with(|| {
let asset_id = 1;
@@ -543,7 +549,7 @@ fn payment_only_with_existing_sufficient_asset() {
let len = 10;
// pre_dispatch fails for non-existent asset
assert!(ChargeAssetTxPayment::<Runtime>::from(0, Some(asset_id))
.pre_dispatch(&caller, CALL, &info_from_weight(weight), len)
.pre_dispatch(&caller, CALL, &info_from_weight(Weight::from_ref_time(weight)), len)
.is_err());
// create the non-sufficient asset
@@ -557,7 +563,7 @@ fn payment_only_with_existing_sufficient_asset() {
));
// pre_dispatch fails for non-sufficient asset
assert!(ChargeAssetTxPayment::<Runtime>::from(0, Some(asset_id))
.pre_dispatch(&caller, CALL, &info_from_weight(weight), len)
.pre_dispatch(&caller, CALL, &info_from_weight(Weight::from_ref_time(weight)), len)
.is_err());
});
}
@@ -567,7 +573,7 @@ fn converted_fee_is_never_zero_if_input_fee_is_not() {
let base_weight = 1;
ExtBuilder::default()
.balance_factor(100)
.base_weight(base_weight)
.base_weight(Weight::from_ref_time(base_weight))
.build()
.execute_with(|| {
// create the asset
@@ -611,14 +617,14 @@ fn converted_fee_is_never_zero_if_input_fee_is_not() {
assert_eq!(Assets::balance(asset_id, caller), balance);
}
let pre = ChargeAssetTxPayment::<Runtime>::from(0, Some(asset_id))
.pre_dispatch(&caller, CALL, &info_from_weight(weight), len)
.pre_dispatch(&caller, CALL, &info_from_weight(Weight::from_ref_time(weight)), len)
.unwrap();
// check that at least one coin was charged in the given asset
assert_eq!(Assets::balance(asset_id, caller), balance - 1);
assert_ok!(ChargeAssetTxPayment::<Runtime>::post_dispatch(
Some(pre),
&info_from_weight(weight),
&info_from_weight(Weight::from_ref_time(weight)),
&default_post_info(),
len,
&Ok(())
@@ -632,7 +638,7 @@ fn post_dispatch_fee_is_zero_if_pre_dispatch_fee_is_zero() {
let base_weight = 1;
ExtBuilder::default()
.balance_factor(100)
.base_weight(base_weight)
.base_weight(Weight::from_ref_time(base_weight))
.build()
.execute_with(|| {
// create the asset
@@ -689,7 +695,7 @@ fn post_dispatch_fee_is_zero_if_unsigned_pre_dispatch_fee_is_zero() {
let base_weight = 1;
ExtBuilder::default()
.balance_factor(100)
.base_weight(base_weight)
.base_weight(Weight::from_ref_time(base_weight))
.build()
.execute_with(|| {
// create the asset
@@ -713,7 +719,7 @@ fn post_dispatch_fee_is_zero_if_unsigned_pre_dispatch_fee_is_zero() {
let len = 1;
ChargeAssetTxPayment::<Runtime>::pre_dispatch_unsigned(
CALL,
&info_from_weight(weight),
&info_from_weight(Weight::from_ref_time(weight)),
len,
)
.unwrap();
@@ -724,7 +730,7 @@ fn post_dispatch_fee_is_zero_if_unsigned_pre_dispatch_fee_is_zero() {
// initial fee)
assert_ok!(ChargeAssetTxPayment::<Runtime>::post_dispatch(
None,
&info_from_weight(weight),
&info_from_weight(Weight::from_ref_time(weight)),
&post_info_from_pays(Pays::Yes),
len,
&Ok(())