feat: Rebrand Polkadot/Substrate references to PezkuwiChain
This commit systematically rebrands various references from Parity Technologies' Polkadot/Substrate ecosystem to PezkuwiChain within the kurdistan-sdk. Key changes include: - Updated external repository URLs (zombienet-sdk, parity-db, parity-scale-codec, wasm-instrument) to point to pezkuwichain forks. - Modified internal documentation and code comments to reflect PezkuwiChain naming and structure. - Replaced direct references to with or specific paths within the for XCM, Pezkuwi, and other modules. - Cleaned up deprecated issue and PR references in various and files, particularly in and modules. - Adjusted image and logo URLs in documentation to point to PezkuwiChain assets. - Removed or rephrased comments related to external Polkadot/Substrate PRs and issues. This is a significant step towards fully customizing the SDK for the PezkuwiChain ecosystem.
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
// This file is part of Bizinikiwi.
|
||||
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Benchmarks for Transaction Payment Pallet's transaction extension
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
use super::*;
|
||||
use crate::Pallet;
|
||||
use pezframe_benchmarking::v2::*;
|
||||
use pezframe_support::dispatch::{DispatchInfo, PostDispatchInfo};
|
||||
use pezframe_system::{EventRecord, RawOrigin};
|
||||
use pezsp_runtime::traits::{AsTransactionAuthorizedOrigin, DispatchTransaction, Dispatchable};
|
||||
|
||||
fn assert_last_event<T: Config>(generic_event: <T as Config>::RuntimeEvent) {
|
||||
let events = pezframe_system::Pallet::<T>::events();
|
||||
let system_event: <T as pezframe_system::Config>::RuntimeEvent = generic_event.into();
|
||||
// compare to the last event record
|
||||
let EventRecord { event, .. } = &events[events.len() - 1];
|
||||
assert_eq!(event, &system_event);
|
||||
}
|
||||
|
||||
#[benchmarks(where
|
||||
T: Config,
|
||||
T::RuntimeOrigin: AsTransactionAuthorizedOrigin,
|
||||
T::RuntimeCall: Dispatchable<Info = DispatchInfo, PostInfo = PostDispatchInfo>,
|
||||
)]
|
||||
mod benchmarks {
|
||||
use super::*;
|
||||
|
||||
#[benchmark]
|
||||
fn charge_transaction_payment() {
|
||||
let caller: T::AccountId = account("caller", 0, 0);
|
||||
let existential_deposit =
|
||||
<T::OnChargeTransaction as OnChargeTransaction<T>>::minimum_balance();
|
||||
|
||||
let (amount_to_endow, tip) = if existential_deposit.is_zero() {
|
||||
let min_tip: <<T as pallet::Config>::OnChargeTransaction as payment::OnChargeTransaction<T>>::Balance = 1_000_000_000u32.into();
|
||||
(min_tip * 1000u32.into(), min_tip)
|
||||
} else {
|
||||
(existential_deposit * 1000u32.into(), existential_deposit)
|
||||
};
|
||||
|
||||
<T::OnChargeTransaction as OnChargeTransaction<T>>::endow_account(&caller, amount_to_endow);
|
||||
|
||||
let ext: ChargeTransactionPayment<T> = ChargeTransactionPayment::from(tip);
|
||||
let inner = pezframe_system::Call::remark { remark: alloc::vec![] };
|
||||
let call = T::RuntimeCall::from(inner);
|
||||
let extension_weight = ext.weight(&call);
|
||||
let info = DispatchInfo {
|
||||
call_weight: Weight::from_parts(100, 0),
|
||||
extension_weight,
|
||||
class: DispatchClass::Operational,
|
||||
pays_fee: Pays::Yes,
|
||||
};
|
||||
let mut post_info = PostDispatchInfo {
|
||||
actual_weight: Some(Weight::from_parts(10, 0)),
|
||||
pays_fee: Pays::Yes,
|
||||
};
|
||||
|
||||
#[block]
|
||||
{
|
||||
assert!(ext
|
||||
.test_run(RawOrigin::Signed(caller.clone()).into(), &call, &info, 10, 0, |_| Ok(
|
||||
post_info
|
||||
))
|
||||
.unwrap()
|
||||
.is_ok());
|
||||
}
|
||||
|
||||
post_info.actual_weight.as_mut().map(|w| w.saturating_accrue(extension_weight));
|
||||
let actual_fee = Pallet::<T>::compute_actual_fee(10, &info, &post_info, tip);
|
||||
assert_last_event::<T>(
|
||||
Event::<T>::TransactionFeePaid { who: caller, actual_fee, tip }.into(),
|
||||
);
|
||||
}
|
||||
|
||||
impl_benchmark_test_suite!(Pallet, crate::mock::new_test_ext(), crate::mock::Runtime);
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,148 @@
|
||||
// This file is part of Bizinikiwi.
|
||||
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use super::*;
|
||||
use crate as pezpallet_transaction_payment;
|
||||
use pezframe_support::{
|
||||
derive_impl,
|
||||
dispatch::DispatchClass,
|
||||
parameter_types,
|
||||
traits::{fungible, Imbalance, OnUnbalanced},
|
||||
weights::{Weight, WeightToFee as WeightToFeeT},
|
||||
};
|
||||
use pezframe_system as system;
|
||||
use pezpallet_balances::Call as BalancesCall;
|
||||
|
||||
type Block = pezframe_system::mocking::MockBlock<Runtime>;
|
||||
|
||||
pezframe_support::construct_runtime!(
|
||||
pub struct Runtime
|
||||
{
|
||||
System: system,
|
||||
Balances: pezpallet_balances,
|
||||
TransactionPayment: pezpallet_transaction_payment::{Pallet, Storage, Event<T>},
|
||||
}
|
||||
);
|
||||
|
||||
pub(crate) const CALL: &<Runtime as pezframe_system::Config>::RuntimeCall =
|
||||
&RuntimeCall::Balances(BalancesCall::transfer_allow_death { dest: 2, value: 69 });
|
||||
|
||||
parameter_types! {
|
||||
pub(crate) static ExtrinsicBaseWeight: Weight = Weight::zero();
|
||||
}
|
||||
|
||||
pub struct BlockWeights;
|
||||
impl Get<pezframe_system::limits::BlockWeights> for BlockWeights {
|
||||
fn get() -> pezframe_system::limits::BlockWeights {
|
||||
pezframe_system::limits::BlockWeights::builder()
|
||||
.base_block(Weight::zero())
|
||||
.for_class(DispatchClass::all(), |weights| {
|
||||
weights.base_extrinsic = ExtrinsicBaseWeight::get().into();
|
||||
})
|
||||
.for_class(DispatchClass::non_mandatory(), |weights| {
|
||||
weights.max_total = Weight::from_parts(1024, u64::MAX).into();
|
||||
})
|
||||
.build_or_panic()
|
||||
}
|
||||
}
|
||||
|
||||
parameter_types! {
|
||||
pub static WeightToFee: u64 = 1;
|
||||
pub static TransactionByteFee: u64 = 1;
|
||||
pub static OperationalFeeMultiplier: u8 = 5;
|
||||
}
|
||||
|
||||
#[derive_impl(pezframe_system::config_preludes::TestDefaultConfig)]
|
||||
impl pezframe_system::Config for Runtime {
|
||||
type BlockWeights = BlockWeights;
|
||||
type Block = Block;
|
||||
type AccountData = pezpallet_balances::AccountData<Self::AccountId>;
|
||||
}
|
||||
|
||||
#[derive_impl(pezpallet_balances::config_preludes::TestDefaultConfig)]
|
||||
impl pezpallet_balances::Config for Runtime {
|
||||
type AccountStore = System;
|
||||
}
|
||||
|
||||
impl WeightToFeeT for WeightToFee {
|
||||
type Balance = u64;
|
||||
|
||||
fn weight_to_fee(weight: &Weight) -> Self::Balance {
|
||||
Self::Balance::saturated_from(weight.ref_time())
|
||||
.saturating_mul(WEIGHT_TO_FEE.with(|v| *v.borrow()))
|
||||
}
|
||||
}
|
||||
|
||||
impl WeightToFeeT for TransactionByteFee {
|
||||
type Balance = u64;
|
||||
|
||||
fn weight_to_fee(weight: &Weight) -> Self::Balance {
|
||||
Self::Balance::saturated_from(weight.ref_time())
|
||||
.saturating_mul(TRANSACTION_BYTE_FEE.with(|v| *v.borrow()))
|
||||
}
|
||||
}
|
||||
|
||||
parameter_types! {
|
||||
pub(crate) static TipUnbalancedAmount: u64 = 0;
|
||||
pub(crate) static FeeUnbalancedAmount: u64 = 0;
|
||||
}
|
||||
|
||||
pub struct DealWithFees;
|
||||
impl OnUnbalanced<fungible::Credit<<Runtime as pezframe_system::Config>::AccountId, Balances>>
|
||||
for DealWithFees
|
||||
{
|
||||
fn on_unbalanceds(
|
||||
mut fees_then_tips: impl Iterator<
|
||||
Item = fungible::Credit<<Runtime as pezframe_system::Config>::AccountId, Balances>,
|
||||
>,
|
||||
) {
|
||||
if let Some(fees) = fees_then_tips.next() {
|
||||
FeeUnbalancedAmount::mutate(|a| *a += fees.peek());
|
||||
if let Some(tips) = fees_then_tips.next() {
|
||||
TipUnbalancedAmount::mutate(|a| *a += tips.peek());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Weights used in testing.
|
||||
pub struct MockWeights;
|
||||
|
||||
impl WeightInfo for MockWeights {
|
||||
fn charge_transaction_payment() -> Weight {
|
||||
Weight::from_parts(10, 0)
|
||||
}
|
||||
}
|
||||
|
||||
impl Config for Runtime {
|
||||
type RuntimeEvent = RuntimeEvent;
|
||||
type OnChargeTransaction = FungibleAdapter<Balances, DealWithFees>;
|
||||
type OperationalFeeMultiplier = OperationalFeeMultiplier;
|
||||
type WeightToFee = WeightToFee;
|
||||
type LengthToFee = TransactionByteFee;
|
||||
type FeeMultiplierUpdate = ();
|
||||
type WeightInfo = MockWeights;
|
||||
}
|
||||
|
||||
#[cfg(feature = "runtime-benchmarks")]
|
||||
pub fn new_test_ext() -> pezsp_io::TestExternalities {
|
||||
crate::tests::ExtBuilder::default()
|
||||
.base_weight(Weight::from_parts(100, 0))
|
||||
.byte_fee(10)
|
||||
.balance_factor(0)
|
||||
.build()
|
||||
}
|
||||
@@ -0,0 +1,363 @@
|
||||
// This file is part of Bizinikiwi.
|
||||
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
/// ! Traits and default implementation for paying transaction fees.
|
||||
use crate::{Config, Pallet, TxPaymentCredit, LOG_TARGET};
|
||||
|
||||
use codec::{DecodeWithMemTracking, FullCodec, MaxEncodedLen};
|
||||
use core::marker::PhantomData;
|
||||
use pezframe_support::{
|
||||
traits::{
|
||||
fungible::{Balanced, Credit, Inspect},
|
||||
tokens::{Precision, WithdrawConsequence},
|
||||
Currency, ExistenceRequirement, Imbalance, NoDrop, OnUnbalanced, SuppressedDrop,
|
||||
WithdrawReasons,
|
||||
},
|
||||
unsigned::TransactionValidityError,
|
||||
};
|
||||
use scale_info::TypeInfo;
|
||||
use pezsp_runtime::{
|
||||
traits::{CheckedSub, DispatchInfoOf, PostDispatchInfoOf, Saturating, Zero},
|
||||
transaction_validity::InvalidTransaction,
|
||||
};
|
||||
|
||||
type NegativeImbalanceOf<C, T> =
|
||||
<C as Currency<<T as pezframe_system::Config>::AccountId>>::NegativeImbalance;
|
||||
|
||||
/// Handle withdrawing, refunding and depositing of transaction fees.
|
||||
pub trait OnChargeTransaction<T: Config>: TxCreditHold<T> {
|
||||
/// The underlying integer type in which fees are calculated.
|
||||
type Balance: pezframe_support::traits::tokens::Balance;
|
||||
|
||||
type LiquidityInfo: Default;
|
||||
|
||||
/// Before the transaction is executed the payment of the transaction fees
|
||||
/// need to be secured.
|
||||
///
|
||||
/// Returns the tip credit
|
||||
fn withdraw_fee(
|
||||
who: &T::AccountId,
|
||||
call: &T::RuntimeCall,
|
||||
dispatch_info: &DispatchInfoOf<T::RuntimeCall>,
|
||||
fee_with_tip: Self::Balance,
|
||||
tip: Self::Balance,
|
||||
) -> Result<Self::LiquidityInfo, TransactionValidityError>;
|
||||
|
||||
/// Check if the predicted fee from the transaction origin can be withdrawn.
|
||||
fn can_withdraw_fee(
|
||||
who: &T::AccountId,
|
||||
call: &T::RuntimeCall,
|
||||
dispatch_info: &DispatchInfoOf<T::RuntimeCall>,
|
||||
fee_with_tip: Self::Balance,
|
||||
tip: Self::Balance,
|
||||
) -> Result<(), TransactionValidityError>;
|
||||
|
||||
/// After the transaction was executed the actual fee can be calculated.
|
||||
/// This function should refund any overpaid fees and optionally deposit
|
||||
/// the corrected amount.
|
||||
///
|
||||
/// Note: The `fee` already includes the `tip`.
|
||||
fn correct_and_deposit_fee(
|
||||
who: &T::AccountId,
|
||||
dispatch_info: &DispatchInfoOf<T::RuntimeCall>,
|
||||
post_info: &PostDispatchInfoOf<T::RuntimeCall>,
|
||||
corrected_fee_with_tip: Self::Balance,
|
||||
tip: Self::Balance,
|
||||
liquidity_info: Self::LiquidityInfo,
|
||||
) -> Result<(), TransactionValidityError>;
|
||||
|
||||
#[cfg(feature = "runtime-benchmarks")]
|
||||
fn endow_account(who: &T::AccountId, amount: Self::Balance);
|
||||
|
||||
#[cfg(feature = "runtime-benchmarks")]
|
||||
fn minimum_balance() -> Self::Balance;
|
||||
}
|
||||
|
||||
/// Needs to be implemented for every [`OnChargeTransaction`].
|
||||
///
|
||||
/// Cannot be added to `OnChargeTransaction` directly as this would
|
||||
/// cause cycles in trait resolution.
|
||||
pub trait TxCreditHold<T: Config> {
|
||||
/// The credit that is used to represent the withdrawn transaction fees.
|
||||
///
|
||||
/// The pallet will put this into a temporary storage item in order to
|
||||
/// make it available to other pallets during tx application.
|
||||
///
|
||||
/// Is only used within a transaction. Hence changes to the encoding of this
|
||||
/// type **won't** require a storage migration.
|
||||
///
|
||||
/// Set to `()` if your `OnChargeTransaction` impl does not store the credit.
|
||||
type Credit: FullCodec + DecodeWithMemTracking + MaxEncodedLen + TypeInfo + SuppressedDrop;
|
||||
}
|
||||
|
||||
/// Implements transaction payment for a pallet implementing the [`pezframe_support::traits::fungible`]
|
||||
/// trait (eg. pezpallet_balances) using an unbalance handler (implementing
|
||||
/// [`OnUnbalanced`]).
|
||||
///
|
||||
/// The unbalance handler is given 2 unbalanceds in [`OnUnbalanced::on_unbalanceds`]: `fee` and
|
||||
/// then `tip`.
|
||||
pub struct FungibleAdapter<F, OU>(PhantomData<(F, OU)>);
|
||||
|
||||
impl<T, F, OU> OnChargeTransaction<T> for FungibleAdapter<F, OU>
|
||||
where
|
||||
T: Config,
|
||||
T::OnChargeTransaction: TxCreditHold<T, Credit = NoDrop<Credit<T::AccountId, F>>>,
|
||||
F: Balanced<T::AccountId> + 'static,
|
||||
OU: OnUnbalanced<<Self::Credit as SuppressedDrop>::Inner>,
|
||||
{
|
||||
type LiquidityInfo = Option<<Self::Credit as SuppressedDrop>::Inner>;
|
||||
type Balance = <F as Inspect<<T as pezframe_system::Config>::AccountId>>::Balance;
|
||||
|
||||
fn withdraw_fee(
|
||||
who: &<T>::AccountId,
|
||||
_call: &<T>::RuntimeCall,
|
||||
_dispatch_info: &DispatchInfoOf<<T>::RuntimeCall>,
|
||||
fee_with_tip: Self::Balance,
|
||||
tip: Self::Balance,
|
||||
) -> Result<Self::LiquidityInfo, TransactionValidityError> {
|
||||
if fee_with_tip.is_zero() {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let credit = F::withdraw(
|
||||
who,
|
||||
fee_with_tip,
|
||||
Precision::Exact,
|
||||
pezframe_support::traits::tokens::Preservation::Preserve,
|
||||
pezframe_support::traits::tokens::Fortitude::Polite,
|
||||
)
|
||||
.map_err(|_| InvalidTransaction::Payment)?;
|
||||
|
||||
let (tip_credit, inclusion_fee) = credit.split(tip);
|
||||
|
||||
<Pallet<T>>::deposit_txfee(inclusion_fee);
|
||||
|
||||
Ok(Some(tip_credit))
|
||||
}
|
||||
|
||||
fn can_withdraw_fee(
|
||||
who: &T::AccountId,
|
||||
_call: &T::RuntimeCall,
|
||||
_dispatch_info: &DispatchInfoOf<T::RuntimeCall>,
|
||||
fee_with_tip: Self::Balance,
|
||||
_tip: Self::Balance,
|
||||
) -> Result<(), TransactionValidityError> {
|
||||
if fee_with_tip.is_zero() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
match F::can_withdraw(who, fee_with_tip) {
|
||||
WithdrawConsequence::Success => Ok(()),
|
||||
_ => Err(InvalidTransaction::Payment.into()),
|
||||
}
|
||||
}
|
||||
|
||||
fn correct_and_deposit_fee(
|
||||
who: &<T>::AccountId,
|
||||
_dispatch_info: &DispatchInfoOf<<T>::RuntimeCall>,
|
||||
_post_info: &PostDispatchInfoOf<<T>::RuntimeCall>,
|
||||
corrected_fee_with_tip: Self::Balance,
|
||||
tip: Self::Balance,
|
||||
tip_credit: Self::LiquidityInfo,
|
||||
) -> Result<(), TransactionValidityError> {
|
||||
let corrected_fee = corrected_fee_with_tip.saturating_sub(tip);
|
||||
|
||||
let remaining_credit = <TxPaymentCredit<T>>::take()
|
||||
.map(|stored_credit| stored_credit.into_inner())
|
||||
.unwrap_or_default();
|
||||
|
||||
// If pallets take away too much it makes the transaction invalid. They need to make
|
||||
// sure that this does not happen. We do not invalide the transaction because we already
|
||||
// executed it and we rather collect too little fees than none at all.
|
||||
if remaining_credit.peek() < corrected_fee {
|
||||
log::error!(target: LOG_TARGET, "Not enough balance on hold to pay tx fees. This is a bug.");
|
||||
}
|
||||
|
||||
// skip refund if account was killed by the tx
|
||||
let fee_credit = if pezframe_system::Pallet::<T>::account_exists(who) {
|
||||
let (mut fee_credit, refund_credit) = remaining_credit.split(corrected_fee);
|
||||
// resolve might fail if refund is below the ed and account
|
||||
// is kept alive by other providers
|
||||
if !refund_credit.peek().is_zero() {
|
||||
if let Err(not_refunded) = F::resolve(who, refund_credit) {
|
||||
fee_credit.subsume(not_refunded);
|
||||
}
|
||||
}
|
||||
fee_credit
|
||||
} else {
|
||||
remaining_credit
|
||||
};
|
||||
|
||||
OU::on_unbalanceds(Some(fee_credit).into_iter().chain(tip_credit));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(feature = "runtime-benchmarks")]
|
||||
fn endow_account(who: &T::AccountId, amount: Self::Balance) {
|
||||
let _ = F::deposit(who, amount, Precision::BestEffort);
|
||||
}
|
||||
|
||||
#[cfg(feature = "runtime-benchmarks")]
|
||||
fn minimum_balance() -> Self::Balance {
|
||||
F::minimum_balance()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, F, OU> TxCreditHold<T> for FungibleAdapter<F, OU>
|
||||
where
|
||||
T: Config,
|
||||
F: Balanced<T::AccountId> + 'static,
|
||||
{
|
||||
type Credit = NoDrop<Credit<<T as pezframe_system::Config>::AccountId, F>>;
|
||||
}
|
||||
|
||||
/// Implements the transaction payment for a pallet implementing the [`Currency`]
|
||||
/// trait (eg. the pezpallet_balances) using an unbalance handler (implementing
|
||||
/// [`OnUnbalanced`]).
|
||||
///
|
||||
/// The unbalance handler is given 2 unbalanceds in [`OnUnbalanced::on_unbalanceds`]: `fee` and
|
||||
/// then `tip`.
|
||||
#[deprecated(
|
||||
note = "Please use the fungible trait and FungibleAdapter. This struct will be removed some time after March 2024."
|
||||
)]
|
||||
pub struct CurrencyAdapter<C, OU>(PhantomData<(C, OU)>);
|
||||
|
||||
/// Default implementation for a Currency and an OnUnbalanced handler.
|
||||
///
|
||||
/// The unbalance handler is given 2 unbalanceds in [`OnUnbalanced::on_unbalanceds`]: `fee` and
|
||||
/// then `tip`.
|
||||
#[allow(deprecated)]
|
||||
impl<T, C, OU> OnChargeTransaction<T> for CurrencyAdapter<C, OU>
|
||||
where
|
||||
T: Config,
|
||||
C: Currency<<T as pezframe_system::Config>::AccountId>,
|
||||
C::PositiveImbalance: Imbalance<
|
||||
<C as Currency<<T as pezframe_system::Config>::AccountId>>::Balance,
|
||||
Opposite = C::NegativeImbalance,
|
||||
>,
|
||||
C::NegativeImbalance: Imbalance<
|
||||
<C as Currency<<T as pezframe_system::Config>::AccountId>>::Balance,
|
||||
Opposite = C::PositiveImbalance,
|
||||
>,
|
||||
OU: OnUnbalanced<NegativeImbalanceOf<C, T>>,
|
||||
{
|
||||
type LiquidityInfo = Option<NegativeImbalanceOf<C, T>>;
|
||||
type Balance = <C as Currency<<T as pezframe_system::Config>::AccountId>>::Balance;
|
||||
|
||||
/// Withdraw the predicted fee from the transaction origin.
|
||||
///
|
||||
/// Note: The `fee` already includes the `tip`.
|
||||
fn withdraw_fee(
|
||||
who: &T::AccountId,
|
||||
_call: &T::RuntimeCall,
|
||||
_info: &DispatchInfoOf<T::RuntimeCall>,
|
||||
fee_with_tip: Self::Balance,
|
||||
tip: Self::Balance,
|
||||
) -> Result<Self::LiquidityInfo, TransactionValidityError> {
|
||||
if fee_with_tip.is_zero() {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let withdraw_reason = if tip.is_zero() {
|
||||
WithdrawReasons::TRANSACTION_PAYMENT
|
||||
} else {
|
||||
WithdrawReasons::TRANSACTION_PAYMENT | WithdrawReasons::TIP
|
||||
};
|
||||
|
||||
match C::withdraw(who, fee_with_tip, withdraw_reason, ExistenceRequirement::KeepAlive) {
|
||||
Ok(imbalance) => Ok(Some(imbalance)),
|
||||
Err(_) => Err(InvalidTransaction::Payment.into()),
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if the predicted fee from the transaction origin can be withdrawn.
|
||||
///
|
||||
/// Note: The `fee` already includes the `tip`.
|
||||
fn can_withdraw_fee(
|
||||
who: &T::AccountId,
|
||||
_call: &T::RuntimeCall,
|
||||
_info: &DispatchInfoOf<T::RuntimeCall>,
|
||||
fee_with_tip: Self::Balance,
|
||||
tip: Self::Balance,
|
||||
) -> Result<(), TransactionValidityError> {
|
||||
if fee_with_tip.is_zero() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let withdraw_reason = if tip.is_zero() {
|
||||
WithdrawReasons::TRANSACTION_PAYMENT
|
||||
} else {
|
||||
WithdrawReasons::TRANSACTION_PAYMENT | WithdrawReasons::TIP
|
||||
};
|
||||
|
||||
let new_balance = C::free_balance(who)
|
||||
.checked_sub(&fee_with_tip)
|
||||
.ok_or(InvalidTransaction::Payment)?;
|
||||
C::ensure_can_withdraw(who, fee_with_tip, withdraw_reason, new_balance)
|
||||
.map(|_| ())
|
||||
.map_err(|_| InvalidTransaction::Payment.into())
|
||||
}
|
||||
|
||||
/// Hand the fee and the tip over to the `[OnUnbalanced]` implementation.
|
||||
/// Since the predicted fee might have been too high, parts of the fee may
|
||||
/// be refunded.
|
||||
///
|
||||
/// Note: The `corrected_fee` already includes the `tip`.
|
||||
fn correct_and_deposit_fee(
|
||||
who: &T::AccountId,
|
||||
_dispatch_info: &DispatchInfoOf<T::RuntimeCall>,
|
||||
_post_info: &PostDispatchInfoOf<T::RuntimeCall>,
|
||||
corrected_fee_with_tip: Self::Balance,
|
||||
tip: Self::Balance,
|
||||
already_withdrawn: Self::LiquidityInfo,
|
||||
) -> Result<(), TransactionValidityError> {
|
||||
if let Some(paid) = already_withdrawn {
|
||||
// Calculate how much refund we should return
|
||||
let refund_amount = paid.peek().saturating_sub(corrected_fee_with_tip);
|
||||
// refund to the the account that paid the fees. If this fails, the
|
||||
// account might have dropped below the existential balance. In
|
||||
// that case we don't refund anything.
|
||||
let refund_imbalance = C::deposit_into_existing(who, refund_amount)
|
||||
.unwrap_or_else(|_| C::PositiveImbalance::zero());
|
||||
// merge the imbalance caused by paying the fees and refunding parts of it again.
|
||||
let adjusted_paid = paid
|
||||
.offset(refund_imbalance)
|
||||
.same()
|
||||
.map_err(|_| TransactionValidityError::Invalid(InvalidTransaction::Payment))?;
|
||||
// Call someone else to handle the imbalance (fee and tip separately)
|
||||
let (tip, fee) = adjusted_paid.split(tip);
|
||||
OU::on_unbalanceds(Some(fee).into_iter().chain(Some(tip)));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(feature = "runtime-benchmarks")]
|
||||
fn endow_account(who: &T::AccountId, amount: Self::Balance) {
|
||||
let _ = C::deposit_creating(who, amount);
|
||||
}
|
||||
|
||||
#[cfg(feature = "runtime-benchmarks")]
|
||||
fn minimum_balance() -> Self::Balance {
|
||||
C::minimum_balance()
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(deprecated)]
|
||||
impl<T: Config, C, OU> TxCreditHold<T> for CurrencyAdapter<C, OU> {
|
||||
type Credit = ();
|
||||
}
|
||||
@@ -0,0 +1,917 @@
|
||||
// This file is part of Bizinikiwi.
|
||||
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
use super::*;
|
||||
use crate as pezpallet_transaction_payment;
|
||||
|
||||
use codec::Encode;
|
||||
|
||||
use pezsp_runtime::{
|
||||
generic::UncheckedExtrinsic,
|
||||
traits::{DispatchTransaction, One},
|
||||
transaction_validity::{InvalidTransaction, TransactionSource::External},
|
||||
BuildStorage,
|
||||
};
|
||||
|
||||
use pezframe_support::{
|
||||
assert_ok,
|
||||
dispatch::{DispatchClass, DispatchInfo, GetDispatchInfo, PostDispatchInfo},
|
||||
traits::{Currency, OriginTrait},
|
||||
weights::Weight,
|
||||
};
|
||||
use pezframe_system as system;
|
||||
use mock::*;
|
||||
use pezpallet_balances::Call as BalancesCall;
|
||||
|
||||
pub struct ExtBuilder {
|
||||
balance_factor: u64,
|
||||
base_weight: Weight,
|
||||
byte_fee: u64,
|
||||
weight_to_fee: u64,
|
||||
initial_multiplier: Option<Multiplier>,
|
||||
}
|
||||
|
||||
impl Default for ExtBuilder {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
balance_factor: 1,
|
||||
base_weight: Weight::zero(),
|
||||
byte_fee: 1,
|
||||
weight_to_fee: 1,
|
||||
initial_multiplier: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ExtBuilder {
|
||||
pub fn base_weight(mut self, base_weight: Weight) -> Self {
|
||||
self.base_weight = base_weight;
|
||||
self
|
||||
}
|
||||
pub fn byte_fee(mut self, byte_fee: u64) -> Self {
|
||||
self.byte_fee = byte_fee;
|
||||
self
|
||||
}
|
||||
pub fn weight_fee(mut self, weight_to_fee: u64) -> Self {
|
||||
self.weight_to_fee = weight_to_fee;
|
||||
self
|
||||
}
|
||||
pub fn balance_factor(mut self, factor: u64) -> Self {
|
||||
self.balance_factor = factor;
|
||||
self
|
||||
}
|
||||
pub fn with_initial_multiplier(mut self, multiplier: Multiplier) -> Self {
|
||||
self.initial_multiplier = Some(multiplier);
|
||||
self
|
||||
}
|
||||
fn set_constants(&self) {
|
||||
ExtrinsicBaseWeight::mutate(|v| *v = 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);
|
||||
}
|
||||
pub fn build(self) -> pezsp_io::TestExternalities {
|
||||
self.set_constants();
|
||||
let mut t = pezframe_system::GenesisConfig::<Runtime>::default().build_storage().unwrap();
|
||||
pezpallet_balances::GenesisConfig::<Runtime> {
|
||||
balances: if self.balance_factor > 0 {
|
||||
vec![
|
||||
(1, 10 * self.balance_factor),
|
||||
(2, 20 * self.balance_factor),
|
||||
(3, 30 * self.balance_factor),
|
||||
(4, 40 * self.balance_factor),
|
||||
(5, 50 * self.balance_factor),
|
||||
(6, 60 * self.balance_factor),
|
||||
]
|
||||
} else {
|
||||
vec![]
|
||||
},
|
||||
..Default::default()
|
||||
}
|
||||
.assimilate_storage(&mut t)
|
||||
.unwrap();
|
||||
|
||||
if let Some(multiplier) = self.initial_multiplier {
|
||||
pallet::GenesisConfig::<Runtime> { multiplier, ..Default::default() }
|
||||
.assimilate_storage(&mut t)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
t.into()
|
||||
}
|
||||
}
|
||||
|
||||
/// create a transaction info struct from weight. Handy to avoid building the whole struct.
|
||||
pub fn info_from_weight(w: Weight) -> DispatchInfo {
|
||||
// pays_fee: Pays::Yes -- class: DispatchClass::Normal
|
||||
DispatchInfo { call_weight: w, ..Default::default() }
|
||||
}
|
||||
|
||||
fn post_info_from_weight(w: Weight) -> PostDispatchInfo {
|
||||
PostDispatchInfo { actual_weight: Some(w), pays_fee: Default::default() }
|
||||
}
|
||||
|
||||
fn post_info_from_pays(p: Pays) -> PostDispatchInfo {
|
||||
PostDispatchInfo { actual_weight: None, pays_fee: p }
|
||||
}
|
||||
|
||||
fn default_post_info() -> PostDispatchInfo {
|
||||
PostDispatchInfo { actual_weight: None, pays_fee: Default::default() }
|
||||
}
|
||||
|
||||
type Ext = ChargeTransactionPayment<Runtime>;
|
||||
|
||||
#[test]
|
||||
fn transaction_extension_transaction_payment_work() {
|
||||
ExtBuilder::default()
|
||||
.balance_factor(10)
|
||||
.base_weight(Weight::from_parts(5, 0))
|
||||
.build()
|
||||
.execute_with(|| {
|
||||
let mut info = info_from_weight(Weight::from_parts(5, 0));
|
||||
let ext = Ext::from(0);
|
||||
let ext_weight = ext.weight(CALL);
|
||||
info.extension_weight = ext_weight;
|
||||
ext.test_run(Some(1).into(), CALL, &info, 10, 0, |_| {
|
||||
assert_eq!(Balances::free_balance(1), 100 - 5 - 5 - 10 - 10);
|
||||
Ok(default_post_info())
|
||||
})
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert_eq!(Balances::free_balance(1), 100 - 5 - 5 - 10 - 10);
|
||||
assert_eq!(FeeUnbalancedAmount::get(), 5 + 5 + 10 + 10);
|
||||
assert_eq!(TipUnbalancedAmount::get(), 0);
|
||||
|
||||
FeeUnbalancedAmount::mutate(|a| *a = 0);
|
||||
|
||||
let mut info = info_from_weight(Weight::from_parts(100, 0));
|
||||
info.extension_weight = ext_weight;
|
||||
Ext::from(5 /* tipped */)
|
||||
.test_run(Some(2).into(), CALL, &info, 10, 0, |_| {
|
||||
assert_eq!(Balances::free_balance(2), 200 - 5 - 10 - 100 - 10 - 5);
|
||||
Ok(post_info_from_weight(Weight::from_parts(50, 0)))
|
||||
})
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert_eq!(Balances::free_balance(2), 200 - 5 - 10 - 50 - 10 - 5);
|
||||
assert_eq!(FeeUnbalancedAmount::get(), 5 + 10 + 50 + 10);
|
||||
assert_eq!(TipUnbalancedAmount::get(), 5);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transaction_extension_transaction_payment_multiplied_refund_works() {
|
||||
ExtBuilder::default()
|
||||
.balance_factor(10)
|
||||
.base_weight(Weight::from_parts(5, 0))
|
||||
.build()
|
||||
.execute_with(|| {
|
||||
NextFeeMultiplier::<Runtime>::put(Multiplier::saturating_from_rational(3, 2));
|
||||
|
||||
let len = 10;
|
||||
let origin = Some(2).into();
|
||||
let mut info = info_from_weight(Weight::from_parts(100, 0));
|
||||
let ext = Ext::from(5 /* tipped */);
|
||||
let ext_weight = ext.weight(CALL);
|
||||
info.extension_weight = ext_weight;
|
||||
ext.test_run(origin, CALL, &info, len, 0, |_| {
|
||||
// 5 base fee, 10 byte fee, 3/2 * (100 call weight fee + 10 ext weight fee), 5
|
||||
// tip
|
||||
assert_eq!(Balances::free_balance(2), 200 - 5 - 10 - 165 - 5);
|
||||
Ok(post_info_from_weight(Weight::from_parts(50, 0)))
|
||||
})
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
|
||||
// 75 (3/2 of the returned 50 units of call weight, 0 returned of ext weight) is
|
||||
// refunded
|
||||
assert_eq!(Balances::free_balance(2), 200 - 5 - 10 - (165 - 75) - 5);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transaction_extension_transaction_payment_is_bounded() {
|
||||
ExtBuilder::default().balance_factor(1000).byte_fee(0).build().execute_with(|| {
|
||||
// maximum weight possible
|
||||
let info = info_from_weight(Weight::MAX);
|
||||
assert_ok!(Ext::from(0).validate_and_prepare(Some(1).into(), CALL, &info, 10, 0));
|
||||
// fee will be proportional to what is the actual maximum weight in the runtime.
|
||||
assert_eq!(
|
||||
Balances::free_balance(&1),
|
||||
(10000 - <Runtime as pezframe_system::Config>::BlockWeights::get().max_block.ref_time())
|
||||
as u64
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transaction_extension_allows_free_transactions() {
|
||||
ExtBuilder::default()
|
||||
.base_weight(Weight::from_parts(100, 0))
|
||||
.balance_factor(0)
|
||||
.build()
|
||||
.execute_with(|| {
|
||||
// 1 ain't have a penny.
|
||||
assert_eq!(Balances::free_balance(1), 0);
|
||||
|
||||
let len = 100;
|
||||
|
||||
// This is a completely free (and thus wholly insecure/DoS-ridden) transaction.
|
||||
let op_tx = DispatchInfo {
|
||||
call_weight: Weight::from_parts(0, 0),
|
||||
extension_weight: Weight::zero(),
|
||||
class: DispatchClass::Operational,
|
||||
pays_fee: Pays::No,
|
||||
};
|
||||
assert_ok!(Ext::from(0).validate_only(Some(1).into(), CALL, &op_tx, len, External, 0));
|
||||
|
||||
// like a InsecureFreeNormal
|
||||
let free_tx = DispatchInfo {
|
||||
call_weight: Weight::from_parts(0, 0),
|
||||
extension_weight: Weight::zero(),
|
||||
class: DispatchClass::Normal,
|
||||
pays_fee: Pays::Yes,
|
||||
};
|
||||
assert_eq!(
|
||||
Ext::from(0)
|
||||
.validate_only(Some(1).into(), CALL, &free_tx, len, External, 0)
|
||||
.unwrap_err(),
|
||||
TransactionValidityError::Invalid(InvalidTransaction::Payment),
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn transaction_ext_length_fee_is_also_updated_per_congestion() {
|
||||
ExtBuilder::default()
|
||||
.base_weight(Weight::from_parts(5, 0))
|
||||
.balance_factor(10)
|
||||
.build()
|
||||
.execute_with(|| {
|
||||
// all fees should be x1.5
|
||||
NextFeeMultiplier::<Runtime>::put(Multiplier::saturating_from_rational(3, 2));
|
||||
let len = 10;
|
||||
let info = info_from_weight(Weight::from_parts(3, 0));
|
||||
assert_ok!(Ext::from(10).validate_and_prepare(Some(1).into(), CALL, &info, len, 0));
|
||||
assert_eq!(
|
||||
Balances::free_balance(1),
|
||||
100 // original
|
||||
- 10 // tip
|
||||
- 5 // base
|
||||
- 10 // len
|
||||
- (3 * 3 / 2) // adjusted weight
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn query_info_and_fee_details_works() {
|
||||
let call = RuntimeCall::Balances(BalancesCall::transfer_allow_death { dest: 2, value: 69 });
|
||||
let origin = 111111;
|
||||
let extra = ();
|
||||
let xt = UncheckedExtrinsic::new_signed(call.clone(), origin, (), extra);
|
||||
let info = xt.get_dispatch_info();
|
||||
let ext = xt.encode();
|
||||
let len = ext.len() as u32;
|
||||
|
||||
let unsigned_xt = UncheckedExtrinsic::<u64, _, (), ()>::new_bare(call);
|
||||
let unsigned_xt_info = unsigned_xt.get_dispatch_info();
|
||||
|
||||
ExtBuilder::default()
|
||||
.base_weight(Weight::from_parts(5, 0))
|
||||
.weight_fee(2)
|
||||
.build()
|
||||
.execute_with(|| {
|
||||
// all fees should be x1.5
|
||||
NextFeeMultiplier::<Runtime>::put(Multiplier::saturating_from_rational(3, 2));
|
||||
|
||||
assert_eq!(
|
||||
TransactionPayment::query_info(xt.clone(), len),
|
||||
RuntimeDispatchInfo {
|
||||
weight: info.total_weight(),
|
||||
class: info.class,
|
||||
partial_fee: 5 * 2 /* base * weight_fee */
|
||||
+ len as u64 /* len * 1 */
|
||||
+ info.total_weight().min(BlockWeights::get().max_block).ref_time() as u64 * 2 * 3 / 2 /* weight */
|
||||
},
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
TransactionPayment::query_info(unsigned_xt.clone(), len),
|
||||
RuntimeDispatchInfo {
|
||||
weight: unsigned_xt_info.call_weight,
|
||||
class: unsigned_xt_info.class,
|
||||
partial_fee: 0,
|
||||
},
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
TransactionPayment::query_fee_details(xt, len),
|
||||
FeeDetails {
|
||||
inclusion_fee: Some(InclusionFee {
|
||||
base_fee: 5 * 2,
|
||||
len_fee: len as u64,
|
||||
adjusted_weight_fee: info
|
||||
.total_weight()
|
||||
.min(BlockWeights::get().max_block)
|
||||
.ref_time() as u64 * 2 * 3 / 2
|
||||
}),
|
||||
tip: 0,
|
||||
},
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
TransactionPayment::query_fee_details(unsigned_xt, len),
|
||||
FeeDetails { inclusion_fee: None, tip: 0 },
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn query_call_info_and_fee_details_works() {
|
||||
let call = RuntimeCall::Balances(BalancesCall::transfer_allow_death { dest: 2, value: 69 });
|
||||
let info = call.get_dispatch_info();
|
||||
let encoded_call = call.encode();
|
||||
let len = encoded_call.len() as u32;
|
||||
|
||||
ExtBuilder::default()
|
||||
.base_weight(Weight::from_parts(5, 0))
|
||||
.weight_fee(2)
|
||||
.build()
|
||||
.execute_with(|| {
|
||||
// all fees should be x1.5
|
||||
NextFeeMultiplier::<Runtime>::put(Multiplier::saturating_from_rational(3, 2));
|
||||
|
||||
assert_eq!(
|
||||
TransactionPayment::query_call_info(call.clone(), len),
|
||||
RuntimeDispatchInfo {
|
||||
weight: info.total_weight(),
|
||||
class: info.class,
|
||||
partial_fee: 5 * 2 /* base * weight_fee */
|
||||
+ len as u64 /* len * 1 */
|
||||
+ info.total_weight().min(BlockWeights::get().max_block).ref_time() as u64 * 2 * 3 / 2 /* weight */
|
||||
},
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
TransactionPayment::query_call_fee_details(call, len),
|
||||
FeeDetails {
|
||||
inclusion_fee: Some(InclusionFee {
|
||||
base_fee: 5 * 2, /* base * weight_fee */
|
||||
len_fee: len as u64, /* len * 1 */
|
||||
adjusted_weight_fee: info
|
||||
.total_weight()
|
||||
.min(BlockWeights::get().max_block)
|
||||
.ref_time() as u64 * 2 * 3 / 2 /* weight * weight_fee * multipler */
|
||||
}),
|
||||
tip: 0,
|
||||
},
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compute_fee_works_without_multiplier() {
|
||||
ExtBuilder::default()
|
||||
.base_weight(Weight::from_parts(100, 0))
|
||||
.byte_fee(10)
|
||||
.balance_factor(0)
|
||||
.build()
|
||||
.execute_with(|| {
|
||||
// Next fee multiplier is zero
|
||||
assert_eq!(NextFeeMultiplier::<Runtime>::get(), Multiplier::one());
|
||||
|
||||
// Tip only, no fees works
|
||||
let dispatch_info = DispatchInfo {
|
||||
call_weight: Weight::from_parts(0, 0),
|
||||
extension_weight: Weight::zero(),
|
||||
class: DispatchClass::Operational,
|
||||
pays_fee: Pays::No,
|
||||
};
|
||||
assert_eq!(Pallet::<Runtime>::compute_fee(0, &dispatch_info, 10), 10);
|
||||
// No tip, only base fee works
|
||||
let dispatch_info = DispatchInfo {
|
||||
call_weight: Weight::from_parts(0, 0),
|
||||
extension_weight: Weight::zero(),
|
||||
class: DispatchClass::Operational,
|
||||
pays_fee: Pays::Yes,
|
||||
};
|
||||
assert_eq!(Pallet::<Runtime>::compute_fee(0, &dispatch_info, 0), 100);
|
||||
// Tip + base fee works
|
||||
assert_eq!(Pallet::<Runtime>::compute_fee(0, &dispatch_info, 69), 169);
|
||||
// Len (byte fee) + base fee works
|
||||
assert_eq!(Pallet::<Runtime>::compute_fee(42, &dispatch_info, 0), 520);
|
||||
// Weight fee + base fee works
|
||||
let dispatch_info = DispatchInfo {
|
||||
call_weight: Weight::from_parts(1000, 0),
|
||||
extension_weight: Weight::zero(),
|
||||
class: DispatchClass::Operational,
|
||||
pays_fee: Pays::Yes,
|
||||
};
|
||||
assert_eq!(Pallet::<Runtime>::compute_fee(0, &dispatch_info, 0), 1100);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compute_fee_works_with_multiplier() {
|
||||
ExtBuilder::default()
|
||||
.base_weight(Weight::from_parts(100, 0))
|
||||
.byte_fee(10)
|
||||
.balance_factor(0)
|
||||
.build()
|
||||
.execute_with(|| {
|
||||
// Add a next fee multiplier. Fees will be x3/2.
|
||||
NextFeeMultiplier::<Runtime>::put(Multiplier::saturating_from_rational(3, 2));
|
||||
// Base fee is unaffected by multiplier
|
||||
let dispatch_info = DispatchInfo {
|
||||
call_weight: Weight::from_parts(0, 0),
|
||||
extension_weight: Weight::zero(),
|
||||
class: DispatchClass::Operational,
|
||||
pays_fee: Pays::Yes,
|
||||
};
|
||||
assert_eq!(Pallet::<Runtime>::compute_fee(0, &dispatch_info, 0), 100);
|
||||
|
||||
// Everything works together :)
|
||||
let dispatch_info = DispatchInfo {
|
||||
call_weight: Weight::from_parts(123, 0),
|
||||
extension_weight: Weight::zero(),
|
||||
class: DispatchClass::Operational,
|
||||
pays_fee: Pays::Yes,
|
||||
};
|
||||
// 123 weight, 456 length, 100 base
|
||||
assert_eq!(
|
||||
Pallet::<Runtime>::compute_fee(456, &dispatch_info, 789),
|
||||
100 + (3 * 123 / 2) + 4560 + 789,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compute_fee_works_with_negative_multiplier() {
|
||||
ExtBuilder::default()
|
||||
.base_weight(Weight::from_parts(100, 0))
|
||||
.byte_fee(10)
|
||||
.balance_factor(0)
|
||||
.build()
|
||||
.execute_with(|| {
|
||||
// Add a next fee multiplier. All fees will be x1/2.
|
||||
NextFeeMultiplier::<Runtime>::put(Multiplier::saturating_from_rational(1, 2));
|
||||
|
||||
// Base fee is unaffected by multiplier.
|
||||
let dispatch_info = DispatchInfo {
|
||||
call_weight: Weight::from_parts(0, 0),
|
||||
extension_weight: Weight::zero(),
|
||||
class: DispatchClass::Operational,
|
||||
pays_fee: Pays::Yes,
|
||||
};
|
||||
assert_eq!(Pallet::<Runtime>::compute_fee(0, &dispatch_info, 0), 100);
|
||||
|
||||
// Everything works together.
|
||||
let dispatch_info = DispatchInfo {
|
||||
call_weight: Weight::from_parts(123, 0),
|
||||
extension_weight: Weight::zero(),
|
||||
class: DispatchClass::Operational,
|
||||
pays_fee: Pays::Yes,
|
||||
};
|
||||
// 123 weight, 456 length, 100 base
|
||||
assert_eq!(
|
||||
Pallet::<Runtime>::compute_fee(456, &dispatch_info, 789),
|
||||
100 + (123 / 2) + 4560 + 789,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn compute_fee_does_not_overflow() {
|
||||
ExtBuilder::default()
|
||||
.base_weight(Weight::from_parts(100, 0))
|
||||
.byte_fee(10)
|
||||
.balance_factor(0)
|
||||
.build()
|
||||
.execute_with(|| {
|
||||
// Overflow is handled
|
||||
let dispatch_info = DispatchInfo {
|
||||
call_weight: Weight::MAX,
|
||||
extension_weight: Weight::zero(),
|
||||
class: DispatchClass::Operational,
|
||||
pays_fee: Pays::Yes,
|
||||
};
|
||||
assert_eq!(
|
||||
Pallet::<Runtime>::compute_fee(u32::MAX, &dispatch_info, u64::MAX),
|
||||
u64::MAX
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refund_does_not_recreate_account() {
|
||||
ExtBuilder::default()
|
||||
.balance_factor(10)
|
||||
.base_weight(Weight::from_parts(5, 0))
|
||||
.build()
|
||||
.execute_with(|| {
|
||||
// So events are emitted
|
||||
System::set_block_number(10);
|
||||
let info = info_from_weight(Weight::from_parts(100, 0));
|
||||
Ext::from(5 /* tipped */)
|
||||
.test_run(Some(2).into(), CALL, &info, 10, 0, |origin| {
|
||||
assert_eq!(Balances::free_balance(2), 200 - 5 - 10 - 100 - 5);
|
||||
|
||||
// kill the account between pre and post dispatch
|
||||
assert_ok!(Balances::transfer_allow_death(
|
||||
origin,
|
||||
3,
|
||||
Balances::free_balance(2)
|
||||
));
|
||||
assert_eq!(Balances::free_balance(2), 0);
|
||||
|
||||
Ok(post_info_from_weight(Weight::from_parts(50, 0)))
|
||||
})
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert_eq!(Balances::free_balance(2), 0);
|
||||
// Transfer Event
|
||||
System::assert_has_event(RuntimeEvent::Balances(pezpallet_balances::Event::Transfer {
|
||||
from: 2,
|
||||
to: 3,
|
||||
amount: 80,
|
||||
}));
|
||||
// Killed Event
|
||||
System::assert_has_event(RuntimeEvent::System(system::Event::KilledAccount {
|
||||
account: 2,
|
||||
}));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn actual_weight_higher_than_max_refunds_nothing() {
|
||||
ExtBuilder::default()
|
||||
.balance_factor(10)
|
||||
.base_weight(Weight::from_parts(5, 0))
|
||||
.build()
|
||||
.execute_with(|| {
|
||||
let info = info_from_weight(Weight::from_parts(100, 0));
|
||||
Ext::from(5 /* tipped */)
|
||||
.test_run(Some(2).into(), CALL, &info, 10, 0, |_| {
|
||||
assert_eq!(Balances::free_balance(2), 200 - 5 - 10 - 100 - 5);
|
||||
Ok(post_info_from_weight(Weight::from_parts(101, 0)))
|
||||
})
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert_eq!(Balances::free_balance(2), 200 - 5 - 10 - 100 - 5);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn zero_transfer_on_free_transaction() {
|
||||
ExtBuilder::default()
|
||||
.balance_factor(10)
|
||||
.base_weight(Weight::from_parts(5, 0))
|
||||
.build()
|
||||
.execute_with(|| {
|
||||
// So events are emitted
|
||||
System::set_block_number(10);
|
||||
let info = DispatchInfo {
|
||||
call_weight: Weight::from_parts(100, 0),
|
||||
extension_weight: Weight::zero(),
|
||||
pays_fee: Pays::No,
|
||||
class: DispatchClass::Normal,
|
||||
};
|
||||
let user = 69;
|
||||
Ext::from(0)
|
||||
.test_run(Some(user).into(), CALL, &info, 10, 0, |_| {
|
||||
assert_eq!(Balances::total_balance(&user), 0);
|
||||
Ok(default_post_info())
|
||||
})
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert_eq!(Balances::total_balance(&user), 0);
|
||||
// TransactionFeePaid Event
|
||||
System::assert_has_event(RuntimeEvent::TransactionPayment(
|
||||
pezpallet_transaction_payment::Event::TransactionFeePaid {
|
||||
who: user,
|
||||
actual_fee: 0,
|
||||
tip: 0,
|
||||
},
|
||||
));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn refund_consistent_with_actual_weight() {
|
||||
ExtBuilder::default()
|
||||
.balance_factor(10)
|
||||
.base_weight(Weight::from_parts(7, 0))
|
||||
.build()
|
||||
.execute_with(|| {
|
||||
let mut info = info_from_weight(Weight::from_parts(100, 0));
|
||||
let tip = 5;
|
||||
let ext = Ext::from(tip);
|
||||
let ext_weight = ext.weight(CALL);
|
||||
info.extension_weight = ext_weight;
|
||||
let mut post_info = post_info_from_weight(Weight::from_parts(33, 0));
|
||||
let prev_balance = Balances::free_balance(2);
|
||||
let len = 10;
|
||||
|
||||
NextFeeMultiplier::<Runtime>::put(Multiplier::saturating_from_rational(5, 4));
|
||||
|
||||
let actual_post_info = ext
|
||||
.test_run(Some(2).into(), CALL, &info, len, 0, |_| Ok(post_info))
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
post_info
|
||||
.actual_weight
|
||||
.as_mut()
|
||||
.map(|w| w.saturating_accrue(Ext::from(tip).weight(CALL)));
|
||||
assert_eq!(post_info, actual_post_info);
|
||||
|
||||
let refund_based_fee = prev_balance - Balances::free_balance(2);
|
||||
let actual_fee =
|
||||
Pallet::<Runtime>::compute_actual_fee(len as u32, &info, &actual_post_info, tip);
|
||||
|
||||
// 33 call weight, 10 ext weight, 10 length, 7 base, 5 tip
|
||||
assert_eq!(actual_fee, 7 + 10 + ((33 + 10) * 5 / 4) + 5);
|
||||
assert_eq!(refund_based_fee, actual_fee);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_alter_operational_priority() {
|
||||
let tip = 5;
|
||||
let len = 10;
|
||||
|
||||
ExtBuilder::default().balance_factor(100).build().execute_with(|| {
|
||||
let normal = DispatchInfo {
|
||||
call_weight: Weight::from_parts(100, 0),
|
||||
extension_weight: Weight::zero(),
|
||||
class: DispatchClass::Normal,
|
||||
pays_fee: Pays::Yes,
|
||||
};
|
||||
|
||||
let ext = Ext::from(tip);
|
||||
let priority = ext
|
||||
.validate_only(Some(2).into(), CALL, &normal, len, External, 0)
|
||||
.unwrap()
|
||||
.0
|
||||
.priority;
|
||||
assert_eq!(priority, 60);
|
||||
|
||||
let ext = Ext::from(2 * tip);
|
||||
let priority = ext
|
||||
.validate_only(Some(2).into(), CALL, &normal, len, External, 0)
|
||||
.unwrap()
|
||||
.0
|
||||
.priority;
|
||||
assert_eq!(priority, 110);
|
||||
});
|
||||
|
||||
ExtBuilder::default().balance_factor(100).build().execute_with(|| {
|
||||
let op = DispatchInfo {
|
||||
call_weight: Weight::from_parts(100, 0),
|
||||
extension_weight: Weight::zero(),
|
||||
class: DispatchClass::Operational,
|
||||
pays_fee: Pays::Yes,
|
||||
};
|
||||
|
||||
let ext = Ext::from(tip);
|
||||
let priority = ext
|
||||
.validate_only(Some(2).into(), CALL, &op, len, External, 0)
|
||||
.unwrap()
|
||||
.0
|
||||
.priority;
|
||||
assert_eq!(priority, 5810);
|
||||
|
||||
let ext = Ext::from(2 * tip);
|
||||
let priority = ext
|
||||
.validate_only(Some(2).into(), CALL, &op, len, External, 0)
|
||||
.unwrap()
|
||||
.0
|
||||
.priority;
|
||||
assert_eq!(priority, 6110);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_tip_has_some_priority() {
|
||||
let tip = 0;
|
||||
let len = 10;
|
||||
|
||||
ExtBuilder::default().balance_factor(100).build().execute_with(|| {
|
||||
let normal = DispatchInfo {
|
||||
call_weight: Weight::from_parts(100, 0),
|
||||
extension_weight: Weight::zero(),
|
||||
class: DispatchClass::Normal,
|
||||
pays_fee: Pays::Yes,
|
||||
};
|
||||
let ext = Ext::from(tip);
|
||||
let priority = ext
|
||||
.validate_only(Some(2).into(), CALL, &normal, len, External, 0)
|
||||
.unwrap()
|
||||
.0
|
||||
.priority;
|
||||
assert_eq!(priority, 10);
|
||||
});
|
||||
|
||||
ExtBuilder::default().balance_factor(100).build().execute_with(|| {
|
||||
let op = DispatchInfo {
|
||||
call_weight: Weight::from_parts(100, 0),
|
||||
extension_weight: Weight::zero(),
|
||||
class: DispatchClass::Operational,
|
||||
pays_fee: Pays::Yes,
|
||||
};
|
||||
let ext = Ext::from(tip);
|
||||
let priority = ext
|
||||
.validate_only(Some(2).into(), CALL, &op, len, External, 0)
|
||||
.unwrap()
|
||||
.0
|
||||
.priority;
|
||||
assert_eq!(priority, 5510);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn higher_tip_have_higher_priority() {
|
||||
let get_priorities = |tip: u64| {
|
||||
let mut pri1 = 0;
|
||||
let mut pri2 = 0;
|
||||
let len = 10;
|
||||
ExtBuilder::default().balance_factor(100).build().execute_with(|| {
|
||||
let normal = DispatchInfo {
|
||||
call_weight: Weight::from_parts(100, 0),
|
||||
extension_weight: Weight::zero(),
|
||||
class: DispatchClass::Normal,
|
||||
pays_fee: Pays::Yes,
|
||||
};
|
||||
let ext = Ext::from(tip);
|
||||
|
||||
pri1 = ext
|
||||
.validate_only(Some(2).into(), CALL, &normal, len, External, 0)
|
||||
.unwrap()
|
||||
.0
|
||||
.priority;
|
||||
});
|
||||
|
||||
ExtBuilder::default().balance_factor(100).build().execute_with(|| {
|
||||
let op = DispatchInfo {
|
||||
call_weight: Weight::from_parts(100, 0),
|
||||
extension_weight: Weight::zero(),
|
||||
class: DispatchClass::Operational,
|
||||
pays_fee: Pays::Yes,
|
||||
};
|
||||
let ext = Ext::from(tip);
|
||||
pri2 = ext
|
||||
.validate_only(Some(2).into(), CALL, &op, len, External, 0)
|
||||
.unwrap()
|
||||
.0
|
||||
.priority;
|
||||
});
|
||||
|
||||
(pri1, pri2)
|
||||
};
|
||||
|
||||
let mut prev_priorities = get_priorities(0);
|
||||
|
||||
for tip in 1..3 {
|
||||
let priorities = get_priorities(tip);
|
||||
assert!(prev_priorities.0 < priorities.0);
|
||||
assert!(prev_priorities.1 < priorities.1);
|
||||
prev_priorities = priorities;
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn post_info_can_change_pays_fee() {
|
||||
ExtBuilder::default()
|
||||
.balance_factor(10)
|
||||
.base_weight(Weight::from_parts(7, 0))
|
||||
.build()
|
||||
.execute_with(|| {
|
||||
let info = info_from_weight(Weight::from_parts(100, 0));
|
||||
let post_info = post_info_from_pays(Pays::No);
|
||||
let prev_balance = Balances::free_balance(2);
|
||||
let len = 10;
|
||||
let tip = 5;
|
||||
|
||||
NextFeeMultiplier::<Runtime>::put(Multiplier::saturating_from_rational(5, 4));
|
||||
|
||||
let post_info = ChargeTransactionPayment::<Runtime>::from(tip)
|
||||
.test_run(Some(2).into(), CALL, &info, len, 0, |_| Ok(post_info))
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
|
||||
let refund_based_fee = prev_balance - Balances::free_balance(2);
|
||||
let actual_fee =
|
||||
Pallet::<Runtime>::compute_actual_fee(len as u32, &info, &post_info, tip);
|
||||
|
||||
// Only 5 tip is paid
|
||||
assert_eq!(actual_fee, 5);
|
||||
assert_eq!(refund_based_fee, actual_fee);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn genesis_config_works() {
|
||||
ExtBuilder::default()
|
||||
.with_initial_multiplier(Multiplier::from_u32(100))
|
||||
.build()
|
||||
.execute_with(|| {
|
||||
assert_eq!(
|
||||
NextFeeMultiplier::<Runtime>::get(),
|
||||
Multiplier::saturating_from_integer(100)
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn genesis_default_works() {
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
assert_eq!(NextFeeMultiplier::<Runtime>::get(), Multiplier::saturating_from_integer(1));
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_fee_and_no_weight_for_other_origins() {
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
let ext = Ext::from(0);
|
||||
|
||||
let mut info = CALL.get_dispatch_info();
|
||||
info.extension_weight = ext.weight(CALL);
|
||||
|
||||
// Ensure we test the refund.
|
||||
assert!(info.extension_weight != Weight::zero());
|
||||
|
||||
let len = CALL.encoded_size();
|
||||
|
||||
let origin = pezframe_system::RawOrigin::Root.into();
|
||||
let (pre, origin) = ext.validate_and_prepare(origin, CALL, &info, len, 0).unwrap();
|
||||
|
||||
assert!(origin.as_system_ref().unwrap().is_root());
|
||||
|
||||
let pd_res = Ok(());
|
||||
let mut post_info = pezframe_support::dispatch::PostDispatchInfo {
|
||||
actual_weight: Some(info.total_weight()),
|
||||
pays_fee: Default::default(),
|
||||
};
|
||||
|
||||
<Ext as TransactionExtension<RuntimeCall>>::post_dispatch(
|
||||
pre,
|
||||
&info,
|
||||
&mut post_info,
|
||||
len,
|
||||
&pd_res,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(post_info.actual_weight, Some(info.call_weight));
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn fungible_adapter_no_zero_refund_action() {
|
||||
type FungibleAdapterT = payment::FungibleAdapter<Balances, DealWithFees>;
|
||||
|
||||
ExtBuilder::default().balance_factor(10).build().execute_with(|| {
|
||||
System::set_block_number(10);
|
||||
|
||||
let dummy_acc = 1;
|
||||
let (actual_fee, no_tip) = (10, 0);
|
||||
let already_paid = <FungibleAdapterT as OnChargeTransaction<Runtime>>::withdraw_fee(
|
||||
&dummy_acc,
|
||||
CALL,
|
||||
&CALL.get_dispatch_info(),
|
||||
actual_fee,
|
||||
no_tip,
|
||||
).expect("Account must have enough funds.");
|
||||
|
||||
// Correction action with no expected side effect.
|
||||
assert!(<FungibleAdapterT as OnChargeTransaction<Runtime>>::correct_and_deposit_fee(
|
||||
&dummy_acc,
|
||||
&CALL.get_dispatch_info(),
|
||||
&default_post_info(),
|
||||
actual_fee,
|
||||
no_tip,
|
||||
already_paid,
|
||||
).is_ok());
|
||||
|
||||
// Ensure no zero amount deposit event is emitted.
|
||||
let events = System::events();
|
||||
assert!(!events
|
||||
.iter()
|
||||
.any(|record| matches!(record.event, RuntimeEvent::Balances(pezpallet_balances::Event::Deposit { amount, .. }) if amount.is_zero())),
|
||||
"No zero amount deposit amount event should be emitted.",
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
// This file is part of Bizinikiwi.
|
||||
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Types for transaction-payment RPC.
|
||||
|
||||
use codec::{Decode, Encode};
|
||||
#[cfg(feature = "std")]
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use scale_info::TypeInfo;
|
||||
|
||||
use pezsp_runtime::traits::{AtLeast32BitUnsigned, Zero};
|
||||
|
||||
use pezframe_support::dispatch::DispatchClass;
|
||||
|
||||
/// The base fee and adjusted weight and length fees constitute the _inclusion fee_.
|
||||
#[derive(Encode, Decode, Clone, Eq, PartialEq, TypeInfo)]
|
||||
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
|
||||
pub struct InclusionFee<Balance> {
|
||||
/// This is the minimum amount a user pays for a transaction. It is declared
|
||||
/// as a base _weight_ in the runtime and converted to a fee using `WeightToFee`.
|
||||
pub base_fee: Balance,
|
||||
/// The length fee, the amount paid for the encoded length (in bytes) of the transaction.
|
||||
pub len_fee: Balance,
|
||||
///
|
||||
/// - `targeted_fee_adjustment`: This is a multiplier that can tune the final fee based on the
|
||||
/// congestion of the network.
|
||||
/// - `weight_fee`: This amount is computed based on the weight of the transaction. Weight
|
||||
/// accounts for the execution time of a transaction.
|
||||
///
|
||||
/// adjusted_weight_fee = targeted_fee_adjustment * weight_fee
|
||||
pub adjusted_weight_fee: Balance,
|
||||
}
|
||||
|
||||
impl<Balance: AtLeast32BitUnsigned + Copy> InclusionFee<Balance> {
|
||||
/// Returns the total of inclusion fee.
|
||||
///
|
||||
/// ```ignore
|
||||
/// inclusion_fee = base_fee + len_fee + adjusted_weight_fee
|
||||
/// ```
|
||||
pub fn inclusion_fee(&self) -> Balance {
|
||||
self.base_fee
|
||||
.saturating_add(self.len_fee)
|
||||
.saturating_add(self.adjusted_weight_fee)
|
||||
}
|
||||
}
|
||||
|
||||
/// The `FeeDetails` is composed of:
|
||||
/// - (Optional) `inclusion_fee`: Only the `Pays::Yes` transaction can have the inclusion fee.
|
||||
/// - `tip`: If included in the transaction, the tip will be added on top. Only signed
|
||||
/// transactions can have a tip.
|
||||
#[derive(Encode, Decode, Clone, Eq, PartialEq, TypeInfo)]
|
||||
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
|
||||
#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
|
||||
pub struct FeeDetails<Balance> {
|
||||
/// The minimum fee for a transaction to be included in a block.
|
||||
pub inclusion_fee: Option<InclusionFee<Balance>>,
|
||||
// Do not serialize and deserialize `tip` as we actually can not pass any tip to the RPC.
|
||||
#[cfg_attr(feature = "std", serde(skip))]
|
||||
pub tip: Balance,
|
||||
}
|
||||
|
||||
impl<Balance: AtLeast32BitUnsigned + Copy> FeeDetails<Balance> {
|
||||
/// Returns the final fee.
|
||||
///
|
||||
/// ```ignore
|
||||
/// final_fee = inclusion_fee + tip;
|
||||
/// ```
|
||||
pub fn final_fee(&self) -> Balance {
|
||||
self.inclusion_fee
|
||||
.as_ref()
|
||||
.map(|i| i.inclusion_fee())
|
||||
.unwrap_or_else(|| Zero::zero())
|
||||
.saturating_add(self.tip)
|
||||
}
|
||||
}
|
||||
|
||||
/// Information related to a dispatchable's class, weight, and fee that can be queried from the
|
||||
/// runtime.
|
||||
#[derive(Eq, PartialEq, Encode, Decode, Default, TypeInfo)]
|
||||
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize, Clone))]
|
||||
#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
|
||||
#[cfg_attr(
|
||||
feature = "std",
|
||||
serde(bound(serialize = "Balance: std::fmt::Display, Weight: Serialize"))
|
||||
)]
|
||||
#[cfg_attr(
|
||||
feature = "std",
|
||||
serde(bound(deserialize = "Balance: std::str::FromStr, Weight: Deserialize<'de>"))
|
||||
)]
|
||||
pub struct RuntimeDispatchInfo<Balance, Weight = pezframe_support::weights::Weight> {
|
||||
/// Weight of this dispatch.
|
||||
pub weight: Weight,
|
||||
/// Class of this dispatch.
|
||||
pub class: DispatchClass,
|
||||
/// The inclusion fee of this dispatch.
|
||||
///
|
||||
/// This does not include a tip or anything else that
|
||||
/// depends on the signature (i.e. depends on a `TransactionExtension`).
|
||||
#[cfg_attr(feature = "std", serde(with = "serde_balance"))]
|
||||
pub partial_fee: Balance,
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
mod serde_balance {
|
||||
use serde::{Deserialize, Deserializer, Serializer};
|
||||
|
||||
pub fn serialize<S: Serializer, T: std::fmt::Display>(
|
||||
t: &T,
|
||||
serializer: S,
|
||||
) -> Result<S::Ok, S::Error> {
|
||||
serializer.serialize_str(&t.to_string())
|
||||
}
|
||||
|
||||
pub fn deserialize<'de, D: Deserializer<'de>, T: std::str::FromStr>(
|
||||
deserializer: D,
|
||||
) -> Result<T, D::Error> {
|
||||
let s = String::deserialize(deserializer)?;
|
||||
s.parse::<T>().map_err(|_| serde::de::Error::custom("Parse from string failed"))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use pezframe_support::weights::Weight;
|
||||
|
||||
#[test]
|
||||
fn should_serialize_and_deserialize_properly_with_string() {
|
||||
let info = RuntimeDispatchInfo {
|
||||
weight: Weight::from_parts(5, 0),
|
||||
class: DispatchClass::Normal,
|
||||
partial_fee: 1_000_000_u64,
|
||||
};
|
||||
|
||||
let json_str =
|
||||
r#"{"weight":{"ref_time":5,"proof_size":0},"class":"normal","partialFee":"1000000"}"#;
|
||||
|
||||
assert_eq!(serde_json::to_string(&info).unwrap(), json_str);
|
||||
assert_eq!(serde_json::from_str::<RuntimeDispatchInfo<u64>>(json_str).unwrap(), info);
|
||||
|
||||
// should not panic
|
||||
serde_json::to_value(&info).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_serialize_and_deserialize_properly_large_value() {
|
||||
let info = RuntimeDispatchInfo {
|
||||
weight: Weight::from_parts(5, 0),
|
||||
class: DispatchClass::Normal,
|
||||
partial_fee: u128::max_value(),
|
||||
};
|
||||
|
||||
let json_str = r#"{"weight":{"ref_time":5,"proof_size":0},"class":"normal","partialFee":"340282366920938463463374607431768211455"}"#;
|
||||
|
||||
assert_eq!(serde_json::to_string(&info).unwrap(), json_str);
|
||||
assert_eq!(serde_json::from_str::<RuntimeDispatchInfo<u128>>(json_str).unwrap(), info);
|
||||
|
||||
// should not panic
|
||||
serde_json::to_value(&info).unwrap();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
// This file is part of Bizinikiwi.
|
||||
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// This file is part of Bizinikiwi.
|
||||
|
||||
// Copyright (C) Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Autogenerated weights for `pezpallet_transaction_payment`
|
||||
//!
|
||||
//! THIS FILE WAS AUTO-GENERATED USING THE BIZINIKIWI BENCHMARK CLI VERSION 32.0.0
|
||||
//! DATE: 2025-02-21, STEPS: `50`, REPEAT: `20`, LOW RANGE: `[]`, HIGH RANGE: `[]`
|
||||
//! WORST CASE MAP SIZE: `1000000`
|
||||
//! HOSTNAME: `4563561839a5`, CPU: `Intel(R) Xeon(R) CPU @ 2.60GHz`
|
||||
//! WASM-EXECUTION: `Compiled`, CHAIN: `None`, DB CACHE: `1024`
|
||||
|
||||
// Executed Command:
|
||||
// frame-omni-bencher
|
||||
// v1
|
||||
// benchmark
|
||||
// pallet
|
||||
// --extrinsic=*
|
||||
// --runtime=target/production/wbuild/kitchensink-runtime/kitchensink_runtime.wasm
|
||||
// --pallet=pezpallet_transaction_payment
|
||||
// --header=/__w/pezkuwi-sdk/pezkuwi-sdk/bizinikiwi/HEADER-APACHE2
|
||||
// --output=/__w/pezkuwi-sdk/pezkuwi-sdk/bizinikiwi/pezframe/transaction-payment/src/weights.rs
|
||||
// --wasm-execution=compiled
|
||||
// --steps=50
|
||||
// --repeat=20
|
||||
// --heap-pages=4096
|
||||
// --template=bizinikiwi/.maintain/frame-weight-template.hbs
|
||||
// --no-storage-info
|
||||
// --no-min-squares
|
||||
// --no-median-slopes
|
||||
// --genesis-builder-policy=none
|
||||
// --exclude-pallets=pezpallet_xcm,pezpallet_xcm_benchmarks::fungible,pezpallet_xcm_benchmarks::generic,pezpallet_nomination_pools,pezpallet_remark,pezpallet_transaction_storage,pezpallet_election_provider_multi_block,pezpallet_election_provider_multi_block::signed,pezpallet_election_provider_multi_block::unsigned,pezpallet_election_provider_multi_block::verifier
|
||||
|
||||
#![cfg_attr(rustfmt, rustfmt_skip)]
|
||||
#![allow(unused_parens)]
|
||||
#![allow(unused_imports)]
|
||||
#![allow(missing_docs)]
|
||||
#![allow(dead_code)]
|
||||
|
||||
use pezframe_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}};
|
||||
use core::marker::PhantomData;
|
||||
|
||||
/// Weight functions needed for `pezpallet_transaction_payment`.
|
||||
pub trait WeightInfo {
|
||||
fn charge_transaction_payment() -> Weight;
|
||||
}
|
||||
|
||||
/// Weights for `pezpallet_transaction_payment` using the Bizinikiwi node and recommended hardware.
|
||||
pub struct BizinikiwiWeight<T>(PhantomData<T>);
|
||||
impl<T: pezframe_system::Config> WeightInfo for BizinikiwiWeight<T> {
|
||||
/// Storage: `System::Account` (r:1 w:1)
|
||||
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
|
||||
fn charge_transaction_payment() -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `52`
|
||||
// Estimated: `3593`
|
||||
// Minimum execution time: 35_425_000 picoseconds.
|
||||
Weight::from_parts(35_979_000, 3593)
|
||||
.saturating_add(T::DbWeight::get().reads(1_u64))
|
||||
.saturating_add(T::DbWeight::get().writes(1_u64))
|
||||
}
|
||||
}
|
||||
|
||||
// For backwards compatibility and tests.
|
||||
impl WeightInfo for () {
|
||||
/// Storage: `System::Account` (r:1 w:1)
|
||||
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
|
||||
fn charge_transaction_payment() -> Weight {
|
||||
// Proof Size summary in bytes:
|
||||
// Measured: `52`
|
||||
// Estimated: `3593`
|
||||
// Minimum execution time: 35_425_000 picoseconds.
|
||||
Weight::from_parts(35_979_000, 3593)
|
||||
.saturating_add(RocksDbWeight::get().reads(1_u64))
|
||||
.saturating_add(RocksDbWeight::get().writes(1_u64))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user