Transaction Fee Multiplier (#2854)

* added fee calculations; need some type conversions

* cleaned up make_payment and other stuff

* rename vars to compile

* add WeightToFee type

* clean test files after new type added to balances

* fmting

* fix balance configs in tests

* more fixing mocks and tests

* more comprehensive block weight limit test

* fix compilation errors

* more srml/executive tests && started fixing node/executor tests

* new fee multiplier; still overflows :(

* perbill at the end attempt; needs to be changed

* clean fmting, rename some vars

* new PoC implementation.

* test weight_to_fee range and verify functionality

* 12 of 15 tests in node executor are passing

* 1 test failing; big_block imports are failing for wrong reasons

* Update srml/executive/src/lib.rs

Co-Authored-By: Kian Peymani <Kianenigma@users.noreply.github.com>

* Some cleanup.

* consolidate tests in runtime impls

* clean and condition executive for stateful fee range test

* remove comments to self

* Major cleanup.

* More cleanup.

* Fix lock files.

* Fix build.

* Update node-template/runtime/Cargo.toml

Co-Authored-By: Gavin Wood <github@gavwood.com>

* Update node/executor/src/lib.rs

Co-Authored-By: Gavin Wood <github@gavwood.com>

* Update node/executor/src/lib.rs

Co-Authored-By: Gavin Wood <github@gavwood.com>

* Update node/executor/src/lib.rs

Co-Authored-By: Gavin Wood <github@gavwood.com>

* Update node/executor/src/lib.rs

Co-Authored-By: Gavin Wood <github@gavwood.com>

* Update node/executor/src/lib.rs

Co-Authored-By: Gavin Wood <github@gavwood.com>

* Update node/executor/src/lib.rs

Co-Authored-By: Gavin Wood <github@gavwood.com>

* Per-block update.

* nit.

* Update docs.

* Fix contracts test.

* Stateful fee update.

* Update lock files.

* Update node/runtime/src/impls.rs

* Revamped again with fixed64.

* fix cargo file.

* nits.

* Some cleanup.

* Some nits.

* Fix build.

* Bump.

* Rename to WeightMultiplier

* Update node/executor/src/lib.rs

Co-Authored-By: Tomasz Drwięga <tomusdrw@users.noreply.github.com>

* Add weight to election module mock.

* Fix build.

* finalize merge

* Update srml/system/src/lib.rs

* Bring back fees.

* Some nits.

* Code shifting for simplicity.

* Fix build + more tests.

* Update weights.rs

* Update core/sr-primitives/src/weights.rs

* Update lib.rs

* Fix test build
This commit is contained in:
Amar Singh
2019-07-19 14:21:05 +02:00
committed by Kian Peymani
parent a313935947
commit a757dfb222
36 changed files with 1103 additions and 411 deletions
+31 -6
View File
@@ -76,10 +76,11 @@ use serde::Serialize;
use rstd::prelude::*;
#[cfg(any(feature = "std", test))]
use rstd::map;
use primitives::weights::{Weight, WeightMultiplier};
use primitives::{generic, traits::{self, CheckEqual, SimpleArithmetic,
SimpleBitOps, Hash, Member, MaybeDisplay, EnsureOrigin, CurrentHeight, BlockNumberToHash,
MaybeSerializeDebugButNotDeserialize, MaybeSerializeDebug, StaticLookup, One, Bounded, Lookup,
Zero,
Zero, Convert,
}};
use substrate_primitives::storage::well_known_keys;
use srml_support::{
@@ -174,6 +175,14 @@ pub trait Trait: 'static + Eq + Clone {
/// (e.g. Indices module) may provide more functional/efficient alternatives.
type Lookup: StaticLookup<Target = Self::AccountId>;
/// Handler for updating the weight multiplier at the end of each block.
///
/// It receives the current block's weight as input and returns the next weight multiplier for next
/// block.
///
/// Note that passing `()` will keep the value constant.
type WeightMultiplierUpdate: Convert<(Weight, WeightMultiplier), WeightMultiplier>;
/// The block header.
type Header: Parameter + traits::Header<
Number = Self::BlockNumber,
@@ -315,7 +324,10 @@ decl_storage! {
/// Total extrinsics count for the current block.
ExtrinsicCount: Option<u32>;
/// Total weight for all extrinsics put together, for the current block.
AllExtrinsicsWeight: Option<u32>;
AllExtrinsicsWeight: Option<Weight>;
/// The next weight multiplier. This should be updated at the end of each block based on the
/// saturation level (weight).
pub NextWeightMultiplier get(next_weight_multiplier): WeightMultiplier = Default::default();
/// Map of block numbers to block hashes.
pub BlockHash get(block_hash) build(|_| vec![(T::BlockNumber::zero(), hash69())]): map T::BlockNumber => T::Hash;
/// Extrinsics data for the current block (maps an extrinsic's index to its data).
@@ -533,10 +545,21 @@ impl<T: Trait> Module<T> {
}
/// Gets a total weight of all executed extrinsics.
pub fn all_extrinsics_weight() -> u32 {
pub fn all_extrinsics_weight() -> Weight {
AllExtrinsicsWeight::get().unwrap_or_default()
}
/// Update the next weight multiplier.
///
/// This should be called at then end of each block, before `all_extrinsics_weight` is cleared.
pub fn update_weight_multiplier() {
// update the multiplier based on block weight.
let current_weight = Self::all_extrinsics_weight();
NextWeightMultiplier::mutate(|fm| {
*fm = T::WeightMultiplierUpdate::convert((current_weight, *fm))
});
}
/// Start the execution of a particular block.
pub fn initialize(
number: &T::BlockNumber,
@@ -565,6 +588,7 @@ impl<T: Trait> Module<T> {
/// Remove temporary "environment" entries in storage.
pub fn finalize() -> T::Header {
ExtrinsicCount::kill();
Self::update_weight_multiplier();
AllExtrinsicsWeight::kill();
let number = <Number<T>>::take();
@@ -720,17 +744,17 @@ impl<T: Trait> Module<T> {
}
/// To be called immediately after an extrinsic has been applied.
pub fn note_applied_extrinsic(r: &Result<(), &'static str>, encoded_len: u32) {
pub fn note_applied_extrinsic(r: &Result<(), &'static str>, weight: Weight) {
Self::deposit_event(match r {
Ok(_) => Event::ExtrinsicSuccess,
Err(_) => Event::ExtrinsicFailed,
}.into());
let next_extrinsic_index = Self::extrinsic_index().unwrap_or_default() + 1u32;
let total_length = encoded_len.saturating_add(Self::all_extrinsics_weight());
let total_weight = weight.saturating_add(Self::all_extrinsics_weight());
storage::unhashed::put(well_known_keys::EXTRINSIC_INDEX, &next_extrinsic_index);
AllExtrinsicsWeight::put(&total_length);
AllExtrinsicsWeight::put(&total_weight);
}
/// To be called immediately after `note_applied_extrinsic` of the last extrinsic of the block
@@ -806,6 +830,7 @@ mod tests {
type AccountId = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type WeightMultiplierUpdate = ();
type Event = u16;
type BlockHashCount = BlockHashCount;
}