Allow fee calculation to happen off-chain (#6076)

* Emit a PaymentParameters event once per block

This contains per-block paramaters need to calculate
fees off-chain.

* Add WeightToFee trait

* Add documentation to polynomial types

* Ignore pseudo code snippet for doc tests

* Use `Mul` implementation of Perbill

* Add tests for WeightToFeePolynomial

* Revert "Emit a PaymentParameters event once per block"

This reverts commit 6c4763baff3d8179676a3c1660fe7063fd56a8ca.

Co-authored-by: Gavin Wood <gavin@parity.io>
This commit is contained in:
Alexander Theißen
2020-05-21 12:16:04 +02:00
committed by GitHub
parent e04f237152
commit 9dd21b1eed
14 changed files with 241 additions and 79 deletions
+7 -4
View File
@@ -19,12 +19,15 @@ use codec::{Encode, Decode, Joiner};
use frame_support::{
StorageValue, StorageMap,
traits::Currency,
weights::{GetDispatchInfo, DispatchInfo, DispatchClass, constants::ExtrinsicBaseWeight},
weights::{
GetDispatchInfo, DispatchInfo, DispatchClass, constants::ExtrinsicBaseWeight,
WeightToFeePolynomial,
},
};
use sp_core::{NeverNativeValue, traits::Externalities, storage::well_known_keys};
use sp_runtime::{
ApplyExtrinsicResult, Fixed128,
traits::{Hash as HashT, Convert},
traits::Hash as HashT,
transaction_validity::InvalidTransaction,
};
use pallet_contracts::ContractAddressFor;
@@ -54,9 +57,9 @@ fn transfer_fee<E: Encode>(extrinsic: &E, fee_multiplier: Fixed128) -> Balance {
let length_fee = TransactionByteFee::get() * (extrinsic.encode().len() as Balance);
let base_weight = ExtrinsicBaseWeight::get();
let base_fee = <Runtime as pallet_transaction_payment::Trait>::WeightToFee::convert(base_weight);
let base_fee = <Runtime as pallet_transaction_payment::Trait>::WeightToFee::calc(&base_weight);
let weight = default_transfer_call().get_dispatch_info().weight;
let weight_fee = <Runtime as pallet_transaction_payment::Trait>::WeightToFee::convert(weight);
let weight_fee = <Runtime as pallet_transaction_payment::Trait>::WeightToFee::calc(&weight);
base_fee + fee_multiplier.saturated_multiply_accumulate(length_fee + weight_fee)
}
+5 -6
View File
@@ -19,16 +19,15 @@ use codec::{Encode, Joiner};
use frame_support::{
StorageValue, StorageMap,
traits::Currency,
weights::{GetDispatchInfo, constants::ExtrinsicBaseWeight},
weights::{GetDispatchInfo, constants::ExtrinsicBaseWeight, IdentityFee, WeightToFeePolynomial},
};
use sp_core::NeverNativeValue;
use sp_runtime::{Fixed128, Perbill, traits::Convert};
use sp_runtime::{Fixed128, Perbill};
use node_runtime::{
CheckedExtrinsic, Call, Runtime, Balances, TransactionPayment,
TransactionByteFee, WeightFeeCoefficient,
TransactionByteFee,
constants::currency::*,
};
use node_runtime::impls::LinearWeightToFee;
use node_primitives::Balance;
use node_testing::keyring::*;
@@ -181,13 +180,13 @@ fn transaction_fee_is_correct_ultimate() {
let mut balance_alice = (100 - 69) * DOLLARS;
let base_weight = ExtrinsicBaseWeight::get();
let base_fee = LinearWeightToFee::<WeightFeeCoefficient>::convert(base_weight);
let base_fee = IdentityFee::<Balance>::calc(&base_weight);
let length_fee = TransactionByteFee::get() * (xt.clone().encode().len() as Balance);
balance_alice -= length_fee;
let weight = default_transfer_call().get_dispatch_info().weight;
let weight_fee = LinearWeightToFee::<WeightFeeCoefficient>::convert(weight);
let weight_fee = IdentityFee::<Balance>::calc(&weight);
// we know that weight to fee multiplier is effect-less in block 1.
// current weight of transfer = 200_000_000