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

Take into account proof size for transaction payment and priority #13958

Merged
merged 22 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
10 changes: 5 additions & 5 deletions bin/node/runtime/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ mod multiplier_tests {
fn multiplier_can_grow_from_zero() {
// if the min is too small, then this will not change, and we are doomed forever.
// the weight is 1/100th bigger than target.
run_with_system_weight(target().set_ref_time(target().ref_time() * 101 / 100), || {
run_with_system_weight(target().set_ref_time(target().ref_time() * 101 / 100).set_proof_size(0), || {
let next = runtime_multiplier_update(min_multiplier());
assert!(next > min_multiplier(), "{:?} !>= {:?}", next, min_multiplier());
})
Expand Down Expand Up @@ -424,10 +424,10 @@ mod multiplier_tests {
10u64 * mb,
Weight::from_parts(2147483647, 0),
Weight::from_parts(4294967295, 0),
BlockWeights::get().max_block / 2,
BlockWeights::get().max_block,
Weight::MAX / 2,
Weight::MAX,
BlockWeights::get().max_block.set_proof_size(0) / 2,
BlockWeights::get().max_block.set_proof_size(0),
Weight::MAX.set_proof_size(0) / 2,
Weight::MAX.set_proof_size(0),
]
.into_iter()
.for_each(|i| {
Expand Down
62 changes: 33 additions & 29 deletions frame/transaction-payment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@
use codec::{Decode, Encode, MaxEncodedLen};
use scale_info::TypeInfo;

use frame_support::{
dispatch::{
DispatchClass, DispatchInfo, DispatchResult, GetDispatchInfo, Pays, PostDispatchInfo,
},
traits::{EstimateCallFee, Get},
weights::{Weight, WeightToFee},
};
pub use pallet::*;
pub use payment::*;
use sp_runtime::{
traits::{
Convert, DispatchInfoOf, Dispatchable, One, PostDispatchInfoOf, SaturatedConversion,
Expand All @@ -61,14 +70,7 @@ use sp_runtime::{
FixedPointNumber, FixedPointOperand, FixedU128, Perquintill, RuntimeDebug,
};
use sp_std::prelude::*;

use frame_support::{
dispatch::{
DispatchClass, DispatchInfo, DispatchResult, GetDispatchInfo, Pays, PostDispatchInfo,
},
traits::{EstimateCallFee, Get},
weights::{Weight, WeightToFee},
};
pub use types::{FeeDetails, InclusionFee, RuntimeDispatchInfo};

#[cfg(test)]
mod mock;
Expand All @@ -78,10 +80,6 @@ mod tests;
mod payment;
mod types;

pub use pallet::*;
pub use payment::*;
pub use types::{FeeDetails, InclusionFee, RuntimeDispatchInfo};

/// Fee multiplier.
pub type Multiplier = FixedU128;

Expand Down Expand Up @@ -207,14 +205,18 @@ where
let normal_block_weight =
current_block_weight.get(DispatchClass::Normal).min(normal_max_weight);

// TODO: Handle all weight dimensions
let normal_max_weight = normal_max_weight.ref_time();
let normal_block_weight = normal_block_weight.ref_time();
// take only the scarcer resource into account.
let (normal_block_weight, normal_max_weight) =
if normal_block_weight.ref_time() > normal_block_weight.proof_size() {
ggwpez marked this conversation as resolved.
Show resolved Hide resolved
(normal_block_weight.ref_time(), normal_max_weight.ref_time())
} else {
(normal_block_weight.proof_size(), normal_max_weight.proof_size())
};

let s = S::get();
let v = V::get();
let target_block_fullness = S::get();
let adjustment_variable = V::get();

let target_weight = (s * normal_max_weight) as u128;
let target_weight = (target_block_fullness * normal_max_weight) as u128;
let block_weight = normal_block_weight as u128;

// determines if the first_term is positive
Expand All @@ -226,9 +228,10 @@ where
let diff = Multiplier::saturating_from_rational(diff_abs, normal_max_weight.max(1));
let diff_squared = diff.saturating_mul(diff);

let v_squared_2 = v.saturating_mul(v) / Multiplier::saturating_from_integer(2);
let v_squared_2 = adjustment_variable.saturating_mul(adjustment_variable) /
Multiplier::saturating_from_integer(2);

let first_term = v.saturating_mul(diff);
let first_term = adjustment_variable.saturating_mul(diff);
let second_term = v_squared_2.saturating_mul(diff_squared);

if positive {
Expand Down Expand Up @@ -290,10 +293,11 @@ const MULTIPLIER_DEFAULT_VALUE: Multiplier = Multiplier::from_u32(1);

#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;

use super::*;

#[pallet::pallet]
pub struct Pallet<T>(_);

Expand Down Expand Up @@ -711,19 +715,19 @@ where
tip: BalanceOf<T>,
final_fee: BalanceOf<T>,
) -> TransactionPriority {
// Calculate how many such extrinsics we could fit into an empty block and take
// the limitting factor.
// Calculate how many such extrinsics we could fit into an empty block and take the
// limiting factor.
let max_block_weight = T::BlockWeights::get().max_block;
let max_block_length = *T::BlockLength::get().max.get(info.class) as u64;

// TODO: Take into account all dimensions of weight
let max_block_weight = max_block_weight.ref_time();
let info_weight = info.weight.ref_time();

let bounded_weight = info_weight.clamp(1, max_block_weight);
// bounded_weight is used as a divisor later so we keep it non-zero.
let bounded_weight = info.weight.max(Weight::from_parts(1, 1)).min(max_block_weight);
let bounded_length = (len as u64).clamp(1, max_block_length);

let max_tx_per_block_weight = max_block_weight / bounded_weight;
// returns the scarce resource, i.e. the one that is limiting the number of transactions.
let max_tx_per_block_weight = max_block_weight
.checked_div_per_component(&bounded_weight)
.expect("bounded_weight is non-zero; qed");
Ank4n marked this conversation as resolved.
Show resolved Hide resolved
let max_tx_per_block_length = max_block_length / bounded_length;
// Given our current knowledge this value is going to be in a reasonable range - i.e.
// less than 10^9 (2^30), so multiplying by the `tip` value is unlikely to overflow the
Expand Down