Skip to content

Commit

Permalink
feat(runtime): make transaction cost multiplier smoother (#3198)
Browse files Browse the repository at this point in the history
  • Loading branch information
shamilsan authored Sep 1, 2023
1 parent e9b6d89 commit b4497e3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 18 deletions.
6 changes: 2 additions & 4 deletions pallets/payment/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,8 @@ where
let len_step = S::get().max(1); // Avoiding division by 0.

let queue_len: u128 = QueueOf::<T>::len().saturated_into();
// min(30) in order to not goes into negative multiplier or UB.
// 30 is the last not negative bit in i32.
let pow = queue_len.saturating_div(len_step).min(30);
Multiplier::saturating_from_integer(1 << pow)
let multiplier = queue_len.saturating_div(len_step).saturating_add(1);
Multiplier::saturating_from_integer(multiplier)
}
}

Expand Down
24 changes: 12 additions & 12 deletions pallets/payment/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,19 @@ fn custom_fee_multiplier_updated_per_block() {
populate_message_queue::<Test>(10);
run_to_block(2);

// CustomFeeMultiplier is 2^(10 / 5) == 4
// CustomFeeMultiplier is (10 / 5 + 1) == 3
assert_eq!(
TransactionPayment::next_fee_multiplier(),
Multiplier::saturating_from_integer(4)
Multiplier::saturating_from_integer(3)
);

populate_message_queue::<Test>(33);
run_to_block(3);

// CustomFeeMultiplier is 2^(33 / 5) == 64
// CustomFeeMultiplier is (33 / 5 + 1) == 7
assert_eq!(
TransactionPayment::next_fee_multiplier(),
Multiplier::saturating_from_integer(64)
Multiplier::saturating_from_integer(7)
);
});
}
Expand Down Expand Up @@ -261,14 +261,14 @@ fn mq_size_affecting_fee_works() {
let alice_initial_balance = Balances::free_balance(ALICE);
let author_initial_balance = Balances::free_balance(BLOCK_AUTHOR);

// Fee multiplier should have been set to 16
// Fee multiplier should have been set to 5
let pre = CustomChargeTransactionPayment::<Test>::from(0)
.pre_dispatch(&ALICE, call, &info_from_weight(weight), len)
.unwrap();

assert_eq!(
Balances::free_balance(ALICE),
alice_initial_balance - (fee_weight * 16 + fee_length)
alice_initial_balance - (fee_weight * 5 + fee_length)
);

assert_ok!(CustomChargeTransactionPayment::<Test>::post_dispatch(
Expand All @@ -280,11 +280,11 @@ fn mq_size_affecting_fee_works() {
));
assert_eq!(
Balances::free_balance(ALICE),
alice_initial_balance - (fee_weight * 16 + fee_length)
alice_initial_balance - (fee_weight * 5 + fee_length)
);
assert_eq!(
Balances::free_balance(BLOCK_AUTHOR),
author_initial_balance + (fee_weight * 16 + fee_length)
author_initial_balance + (fee_weight * 5 + fee_length)
);
});
}
Expand Down Expand Up @@ -492,21 +492,21 @@ fn query_info_and_fee_details_work() {
populate_message_queue::<Test>(20);
run_to_block(2);

// Extra fee multiplier is now 2^(20 / 5) == 16
// Extra fee multiplier is now (20 / 5 + 1) == 5
assert_eq!(
GearPayment::query_info(xt_affecting_mq.clone(), len_affecting_mq),
RuntimeDispatchInfo {
weight: info_affecting_mq.weight,
class: info_affecting_mq.class,
partial_fee: 0 /* base_fee */
+ fee_affecting_length /* len * 1 */
+ fee_affecting_weight * 16u128 /* weight * 16 */
+ fee_affecting_weight * 5u128 /* weight * 5 */
},
);

// Extra fee not applicable => fee should be exactly what it was for empty MQ
// However, we must account for the rounding error in this case
let rounding_error = WeightToFeeFor::<Test>::weight_to_fee(&Weight::from_parts(16, 0));
let rounding_error = WeightToFeeFor::<Test>::weight_to_fee(&Weight::from_parts(5, 0));
assert_eq!(
GearPayment::query_info(xt_not_affecting_mq.clone(), len_not_affecting_mq),
RuntimeDispatchInfo {
Expand All @@ -533,7 +533,7 @@ fn query_info_and_fee_details_work() {
inclusion_fee: Some(InclusionFee {
base_fee: 0,
len_fee: fee_affecting_length,
adjusted_weight_fee: fee_affecting_weight * 16u128,
adjusted_weight_fee: fee_affecting_weight * 5u128,
}),
tip: 0,
},
Expand Down
2 changes: 1 addition & 1 deletion runtime/gear/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ impl pallet_balances::Config for Runtime {

parameter_types! {
pub const TransactionByteFee: Balance = 1;
pub const QueueLengthStep: u128 = 10;
pub const QueueLengthStep: u128 = 1000;
pub const OperationalFeeMultiplier: u8 = 5;
}

Expand Down
2 changes: 1 addition & 1 deletion runtime/vara/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ impl pallet_balances::Config for Runtime {

parameter_types! {
pub const TransactionByteFee: Balance = 1;
pub const QueueLengthStep: u128 = 10;
pub const QueueLengthStep: u128 = 1000;
pub const OperationalFeeMultiplier: u8 = 5;
}

Expand Down

0 comments on commit b4497e3

Please sign in to comment.