Introduce BlockExecutionWeight and ExtrinsicBaseWeight (#5722)

* Introduce `BlockExectionWeight` and `ExtrinsicBaseWeight`

* Add new traits everywhere

* Missed one update

* fix tests

* Update `check_weight` logic

* introduce `max_extrinsic_weight` function

* fix + add tests

* format nits

* remove println

* make test a bit more clear

* Remove minimum weight

* newlines left over from find/replace

* Fix test, improve clarity

* Fix executor tests

* Extrinsic base weight same as old `MINIMUM_WEIGHT`

* fix example test

* Expose constants

* Add test for full block with operational and normal

* Initiate test environment with `BlockExecutionWeight` weight

* format nit

* Update frame/system/src/lib.rs

Co-Authored-By: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Replace `TransactionBaseFee` with `ExtrinsicBaseWeight` (#5761)

* Replace `TransactionBaseFee` with `ExtrinsicBaseFee`

* Fix stuff

* Fix and make tests better

* Forgot to update this test

* Fix priority number in test

* Remove minimum weight from merge

* Fix weight in contracts

* remove `TransactionBaseFee` from contract tests

* Let `register_extra_weight_unchecked` go past `MaximumBlockWeight`

* address feedback

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
This commit is contained in:
Shawn Tabrizi
2020-04-25 07:59:54 +02:00
committed by GitHub
parent 3793fbf9cc
commit 8a33c297b4
74 changed files with 518 additions and 301 deletions
+28 -30
View File
@@ -67,9 +67,6 @@ pub trait Trait: frame_system::Trait {
/// if any.
type OnTransactionPayment: OnUnbalanced<NegativeImbalanceOf<Self>>;
/// The fee to be paid for making a transaction; the base.
type TransactionBaseFee: Get<BalanceOf<Self>>;
/// The fee to be paid for making a transaction; the per-byte portion.
type TransactionByteFee: Get<BalanceOf<Self>>;
@@ -88,9 +85,6 @@ decl_storage! {
decl_module! {
pub struct Module<T: Trait> for enum Call where origin: T::Origin {
/// The fee to be paid for making a transaction; the base.
const TransactionBaseFee: BalanceOf<T> = T::TransactionBaseFee::get();
/// The fee to be paid for making a transaction; the per-byte portion.
const TransactionByteFee: BalanceOf<T> = T::TransactionByteFee::get();
@@ -178,7 +172,7 @@ impl<T: Trait> Module<T> {
let targeted_fee_adjustment = NextFeeMultiplier::get();
let adjusted_fee = targeted_fee_adjustment.saturated_multiply_accumulate(adjustable_fee.saturated_into());
let base_fee = T::TransactionBaseFee::get();
let base_fee = Self::weight_to_fee(T::ExtrinsicBaseWeight::get());
base_fee.saturating_add(adjusted_fee.saturated_into()).saturating_add(tip)
} else {
tip
@@ -367,6 +361,15 @@ mod tests {
pub enum Origin for Runtime {}
}
thread_local! {
static EXTRINSIC_BASE_WEIGHT: RefCell<u64> = RefCell::new(0);
}
pub struct ExtrinsicBaseWeight;
impl Get<u64> for ExtrinsicBaseWeight {
fn get() -> u64 { EXTRINSIC_BASE_WEIGHT.with(|v| *v.borrow()) }
}
parameter_types! {
pub const BlockHashCount: u64 = 250;
pub const MaximumBlockWeight: Weight = 1024;
@@ -388,6 +391,8 @@ mod tests {
type BlockHashCount = BlockHashCount;
type MaximumBlockWeight = MaximumBlockWeight;
type DbWeight = ();
type BlockExecutionWeight = ();
type ExtrinsicBaseWeight = ExtrinsicBaseWeight;
type MaximumBlockLength = MaximumBlockLength;
type AvailableBlockRatio = AvailableBlockRatio;
type Version = ();
@@ -409,16 +414,10 @@ mod tests {
type AccountStore = System;
}
thread_local! {
static TRANSACTION_BASE_FEE: RefCell<u64> = RefCell::new(0);
static TRANSACTION_BYTE_FEE: RefCell<u64> = RefCell::new(1);
static WEIGHT_TO_FEE: RefCell<u64> = RefCell::new(1);
}
pub struct TransactionBaseFee;
impl Get<u64> for TransactionBaseFee {
fn get() -> u64 { TRANSACTION_BASE_FEE.with(|v| *v.borrow()) }
}
pub struct TransactionByteFee;
impl Get<u64> for TransactionByteFee {
fn get() -> u64 { TRANSACTION_BYTE_FEE.with(|v| *v.borrow()) }
@@ -434,7 +433,6 @@ mod tests {
impl Trait for Runtime {
type Currency = pallet_balances::Module<Runtime>;
type OnTransactionPayment = ();
type TransactionBaseFee = TransactionBaseFee;
type TransactionByteFee = TransactionByteFee;
type WeightToFee = WeightToFee;
type FeeMultiplierUpdate = ();
@@ -446,7 +444,7 @@ mod tests {
pub struct ExtBuilder {
balance_factor: u64,
base_fee: u64,
base_weight: u64,
byte_fee: u64,
weight_to_fee: u64
}
@@ -455,7 +453,7 @@ mod tests {
fn default() -> Self {
Self {
balance_factor: 1,
base_fee: 0,
base_weight: 0,
byte_fee: 1,
weight_to_fee: 1,
}
@@ -463,8 +461,8 @@ mod tests {
}
impl ExtBuilder {
pub fn base_fee(mut self, base_fee: u64) -> Self {
self.base_fee = base_fee;
pub fn base_weight(mut self, base_weight: u64) -> Self {
self.base_weight = base_weight;
self
}
pub fn byte_fee(mut self, byte_fee: u64) -> Self {
@@ -480,7 +478,7 @@ mod tests {
self
}
fn set_constants(&self) {
TRANSACTION_BASE_FEE.with(|v| *v.borrow_mut() = self.base_fee);
EXTRINSIC_BASE_WEIGHT.with(|v| *v.borrow_mut() = self.base_weight);
TRANSACTION_BYTE_FEE.with(|v| *v.borrow_mut() = self.byte_fee);
WEIGHT_TO_FEE.with(|v| *v.borrow_mut() = self.weight_to_fee);
}
@@ -523,7 +521,7 @@ mod tests {
fn signed_extension_transaction_payment_work() {
ExtBuilder::default()
.balance_factor(10)
.base_fee(5)
.base_weight(5)
.build()
.execute_with(||
{
@@ -558,7 +556,7 @@ mod tests {
fn signed_extension_transaction_payment_multiplied_refund_works() {
ExtBuilder::default()
.balance_factor(10)
.base_fee(5)
.base_weight(5)
.build()
.execute_with(||
{
@@ -606,7 +604,7 @@ mod tests {
#[test]
fn signed_extension_allows_free_transactions() {
ExtBuilder::default()
.base_fee(100)
.base_weight(100)
.balance_factor(0)
.build()
.execute_with(||
@@ -645,7 +643,7 @@ mod tests {
#[test]
fn signed_ext_length_fee_is_also_updated_per_congestion() {
ExtBuilder::default()
.base_fee(5)
.base_weight(5)
.balance_factor(10)
.build()
.execute_with(||
@@ -673,7 +671,7 @@ mod tests {
let ext = xt.encode();
let len = ext.len() as u32;
ExtBuilder::default()
.base_fee(5)
.base_weight(5)
.weight_fee(2)
.build()
.execute_with(||
@@ -687,7 +685,7 @@ mod tests {
weight: info.weight,
class: info.class,
partial_fee:
5 /* base */
5 * 2 /* base * weight_fee */
+ (
len as u64 /* len * 1 */
+ info.weight.min(MaximumBlockWeight::get()) as u64 * 2 /* weight * weight_to_fee */
@@ -701,7 +699,7 @@ mod tests {
#[test]
fn compute_fee_works_without_multiplier() {
ExtBuilder::default()
.base_fee(100)
.base_weight(100)
.byte_fee(10)
.balance_factor(0)
.build()
@@ -741,7 +739,7 @@ mod tests {
#[test]
fn compute_fee_works_with_multiplier() {
ExtBuilder::default()
.base_fee(100)
.base_weight(100)
.byte_fee(10)
.balance_factor(0)
.build()
@@ -774,7 +772,7 @@ mod tests {
#[test]
fn compute_fee_does_not_overflow() {
ExtBuilder::default()
.base_fee(100)
.base_weight(100)
.byte_fee(10)
.balance_factor(0)
.build()
@@ -801,7 +799,7 @@ mod tests {
fn refund_does_not_recreate_account() {
ExtBuilder::default()
.balance_factor(10)
.base_fee(5)
.base_weight(5)
.build()
.execute_with(||
{
@@ -828,7 +826,7 @@ mod tests {
fn actual_weight_higher_than_max_refunds_nothing() {
ExtBuilder::default()
.balance_factor(10)
.base_fee(5)
.base_weight(5)
.build()
.execute_with(||
{