Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Allow fee calculation to happen off-chain #6076

Merged
merged 8 commits into from
May 21, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 25 additions & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions bin/node-template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use sp_runtime::{
transaction_validity::{TransactionValidity, TransactionSource},
};
use sp_runtime::traits::{
BlakeTwo256, Block as BlockT, IdentityLookup, Verify, ConvertInto, IdentifyAccount, NumberFor,
BlakeTwo256, Block as BlockT, IdentityLookup, Verify, IdentifyAccount, NumberFor,
};
use sp_api::impl_runtime_apis;
use sp_consensus_aura::sr25519::AuthorityId as AuraId;
Expand All @@ -35,7 +35,7 @@ pub use frame_support::{
construct_runtime, parameter_types, StorageValue,
traits::{KeyOwnerProofSystem, Randomness},
weights::{
Weight,
Weight, IdentityFee,
constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight, WEIGHT_PER_SECOND},
},
};
Expand Down Expand Up @@ -233,10 +233,11 @@ parameter_types! {
}

impl transaction_payment::Trait for Runtime {
type Event = Event;
type Currency = balances::Module<Runtime>;
type OnTransactionPayment = ();
type TransactionByteFee = TransactionByteFee;
type WeightToFee = ConvertInto;
type WeightToFee = IdentityFee<Balance>;
type FeeMultiplierUpdate = ();
}

Expand All @@ -262,7 +263,7 @@ construct_runtime!(
Aura: aura::{Module, Config<T>, Inherent(Timestamp)},
Grandpa: grandpa::{Module, Call, Storage, Config, Event},
Balances: balances::{Module, Call, Storage, Config<T>, Event<T>},
TransactionPayment: transaction_payment::{Module, Storage},
TransactionPayment: transaction_payment::{Module, Storage, Event},
Sudo: sudo::{Module, Call, Config<T>, Storage, Event<T>},
// Used for the module template in `./template.rs`
TemplateModule: template::{Module, Call, Storage, Event<T>},
Expand Down
1 change: 1 addition & 0 deletions bin/node/executor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ trie-root = "0.16.0"
frame-benchmarking = { version = "2.0.0-dev", path = "../../../frame/benchmarking" }

[dev-dependencies]
assert_matches = "1.3.0"
criterion = "0.3.0"
frame-support = { version = "2.0.0-dev", path = "../../../frame/support" }
frame-system = { version = "2.0.0-dev", path = "../../../frame/system" }
Expand Down
38 changes: 32 additions & 6 deletions bin/node/executor/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,6 +41,7 @@ use node_runtime::{
use node_primitives::{Balance, Hash};
use wabt;
use node_testing::keyring::*;
use assert_matches::assert_matches;

pub mod common;
use self::common::{*, sign};
Expand All @@ -54,9 +58,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)
}
Expand Down Expand Up @@ -355,7 +359,18 @@ fn full_native_block_import_works() {
topics: vec![],
},
];
assert_eq!(System::events(), events);
let mut is_events = System::events();
assert_matches!(
is_events.remove(0),
EventRecord {
phase: Phase::Initialization,
event: Event::pallet_transaction_payment(
pallet_transaction_payment::Event::PaymentParameters(_)
),
topics: top
} if top == vec![]
);
assert_eq!(is_events, events);
});

fm = t.execute_with(TransactionPayment::next_fee_multiplier);
Expand Down Expand Up @@ -436,7 +451,18 @@ fn full_native_block_import_works() {
topics: vec![],
},
];
assert_eq!(System::events(), events);
let mut is_events = System::events();
assert_matches!(
is_events.remove(0),
EventRecord {
phase: Phase::Initialization,
event: Event::pallet_transaction_payment(
pallet_transaction_payment::Event::PaymentParameters(_)
),
topics: top
} if top == vec![]
);
assert_eq!(is_events, events);
});
}

Expand Down
11 changes: 5 additions & 6 deletions bin/node/executor/tests/fees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand Down Expand Up @@ -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
Expand Down
Loading