Skip to content

Commit 2af1ecb

Browse files
gabriele-0201rphmeier
authored andcommitted
fixes and cleanups
1 parent 1e8176e commit 2af1ecb

File tree

3 files changed

+21
-72
lines changed

3 files changed

+21
-72
lines changed

sugondat-chain/primitives/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use sp_runtime::{
55
MultiSignature,
66
};
77

8-
// TODO: probably this could be moved into runtimes/kusama-runtime/src/constants.rs
8+
// Maximum Length of the Block in bytes
99
pub const MAXIMUM_BLOCK_LENGTH: u32 = 5 * 1024 * 1024;
1010

1111
/// An index to a block.

sugondat-chain/runtimes/sugondat-kusama/src/lib.rs

Lines changed: 17 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,10 @@ parameter_types! {
275275
/// Relay Chain `TransactionByteFee` / 10
276276
pub const TransactionByteFee: Balance = MILLICENTS;
277277

278+
// parameters used by BlobsFeeAdjustment
279+
// to update NextFeeMultiplier and NextLengthMultiplier
280+
//
278281
// Common constants used in all runtimes for SlowAdjustingFeeUpdate
279-
280282
/// The portion of the `NORMAL_DISPATCH_RATIO` that we adjust the fees with. Blocks filled less
281283
/// than this will decrease the weight and more will increase.
282284
pub storage TargetBlockFullness: Perquintill = Perquintill::from_percent(25);
@@ -291,44 +293,17 @@ parameter_types! {
291293
pub MaximumMultiplierBlockFullness: Multiplier = Bounded::max_value();
292294

293295

294-
// parameters used by BlobsFeeAdjustment
295-
296296
pub storage NextLengthMultiplier: Multiplier = Multiplier::saturating_from_integer(1);
297-
298-
pub storage TargetBlockSize: u32 = 820 * 1024; // 0.8MiB
297+
pub storage TargetBlockSize: Perquintill = Perquintill::from_percent(16); // 0.8MiB
299298
// TODO: update those value accordingly with https://github.com/thrumdev/blobs/issues/16
300299
pub AdjustmentVariableBlockSize: Multiplier = Multiplier::saturating_from_rational(75, 1000_000);
301300
pub MinimumMultiplierBlockSize: Multiplier = Multiplier::saturating_from_rational(1, 10u128);
302301
pub MaximumMultiplierBlockSize: Multiplier = Bounded::max_value();
303-
304-
// A positive number represents the count of consecutive blocks that exceeded
305-
// the TargetBlockSize, while a negative number represents the count of
306-
// consecutive blocks that were below the TargetBlockSize - NegativeDeltaTargetBlockFullness.
307-
pub storage BlockSizeTracker: u32 = 0; // 0.8MiB
308-
309-
// The number of consecutive blocks after which the TargetBlockSize will increase
310-
pub storage IncreaseDeltaBlocks: u32 = 10 * DAYS;
311-
312-
// The number of bytes that will be added to TargetBlockSize each update
313-
pub storage DeltaTargetBlockSize: u32 = 205 * 1024;// 0.2MiB
314-
315-
//pub storage DecreaseDeltaBlocks: u32 = 10 * DAYS;,
316-
//pub storage LowerBoundTargetBlockSize
317302
}
318303

319-
/// Parameterized slow adjusting fee updated based on
320-
/// <https://research.web3.foundation/Polkadot/overview/token-economics#2-slow-adjusting-mechanism>
321-
//pub type SlowAdjustingFeeUpdate<R> = TargetedFeeAdjustment<
322-
// R,
323-
// TargetBlockFullness,
324-
// AdjustmentVariable,
325-
// MinimumMultiplier,
326-
// MaximumMultiplier,
327-
//>;
328-
329304
/// Currently pallet_transaction_payment use the following formula:
330305
///
331-
/// ```
306+
/// ```ignore
332307
/// inclusion_fee = base_fee + length_fee + [targeted_fee_adjustment * weight_fee];
333308
/// ```
334309
///
@@ -340,66 +315,41 @@ parameter_types! {
340315
/// What this struct does is this PLUS a side effect, the goal is to reach a different formula to
341316
/// calculate fees:
342317
///
343-
/// ```
318+
/// ```ignore
344319
/// inclusion_fee = base_fee + [targeted_length_fee_adjustment * length_fee] + [targeted_weight_fee_adjustment * weight_fee];
345320
/// ```
346321
///
347322
/// As you can see `targeted_fee_adjustment` becomes `targeted_weight_fee_adjustment` but the behavior
348323
/// remains the same, the side effect is the changing to the value `targeted_length_fee_adjustment`,
349324
/// this formula is achievable because inside pallet_transaction_payment the function `compute_fee_raw`
350-
/// that just computes the final fee associated with an extrinsic uses the associated type `LenghtToFee`
325+
/// that just computes the final fee associated with an extrinsic uses the associated type `LengthToFee`
351326
/// that converts the length of an extrinsic to a fee.
352327
///
353328
/// By default the implementation is a constant multiplication but we want to achieve a dynamic formula
354329
/// that can adapt based on the usage of the network, this can't solely be done by this struct but needs
355-
/// to be bundled with a custom implementation of `LenghtToFee`.
330+
/// to be bundled with a custom implementation of `LengthToFee`.
356331
///
357332
/// This struct ONLY provide a dynamic update of `targeted_length_fee_adjustment` and `targeted_weight_fee_adjustment`
358333
/// based on the congestion and usage of the blocks, while the formula si effectively implemented like
359-
/// explained above only thanks to `LenghtToFee`
334+
/// explained above only thanks to `LengthToFee`
360335
pub struct BlobsFeeAdjustment<T: frame_system::Config>(core::marker::PhantomData<T>);
361336

362337
impl<T: frame_system::Config> Convert<Multiplier, Multiplier> for BlobsFeeAdjustment<T>
363338
where
364339
T: frame_system::Config,
365340
{
366341
/// This function should be a pure function used to update NextFeeMultiplier
367-
/// but will also has the side effect of update NextLenghtMultiplier
342+
/// but will also has the side effect of update NextLengthMultiplier
368343
fn convert(previous_fee_multiplier: Multiplier) -> Multiplier {
369-
// Update TargetBlockSize if needed
370-
let all_extrinsic_len = <frame_system::Pallet<T>>::all_extrinsics_len();
371-
372-
if all_extrinsic_len > TargetBlockSize::get() {
373-
// Increase the tracker if needed
374-
let tracker = BlockSizeTracker::get();
375-
376-
// If the used_block_size is larger than the TargetBlockSize
377-
// for more than IncreaseDeltaBlocks consecutive, then the TargetBlockSize
378-
// will be increased by DeltaTargetBlockSize.
379-
if tracker + 1 >= IncreaseDeltaBlocks::get() {
380-
let current_target_block_size = TargetBlockSize::get();
381-
TargetBlockSize::set(&(current_target_block_size + DeltaTargetBlockSize::get()));
382-
BlockSizeTracker::set(&0);
383-
}
384-
385-
BlockSizeTracker::set(&(tracker + 1));
386-
} else {
387-
// The logic for updating to a new TargetBlockSize currently requires IncreaseDeltaBlocks
388-
// to be constantly larger than TargetBlockSize. However, it might be possible
389-
// to implement a window where we reset the tracker only if the
390-
// block size remains below the TargetBlockSize for a certain number of blocks
391-
BlockSizeTracker::set(&0);
392-
}
393-
394344
// Update NextLengthMultiplier
395345

396346
// To update the value will be used the same formula as TargetedFeeAdjustment,
397347
// described here: https://research.web3.foundation/Polkadot/overview/token-economics#2-slow-adjusting-mechanism
398348
//
399349
// so this is mainly a copy paste of that function because it works on normalized mesurments,
400-
// so if it is ref_time, proof_size or lenght of the extrinsic the mutliplier will be evaluated properly.
350+
// so if it is ref_time, proof_size or length of the extrinsic the mutliplier will be evaluated properly.
401351
// The main problem is that TargetedFeeAdjustment::convert uses directly a call to the storage to extract
402-
// the weight of the current block so there is no way to pass the lenght as input argument,
352+
// the weight of the current block so there is no way to pass the length as input argument,
403353
// here I will copy paste all the needed part to update properly NextLengthMultiplier
404354

405355
// Defensive only. The multiplier in storage should always be at most positive. Nonetheless
@@ -409,14 +359,15 @@ where
409359
let previous_len_multiplier = NextLengthMultiplier::get();
410360
let min_multiplier = MinimumMultiplierBlockSize::get();
411361
let max_multiplier = MaximumMultiplierBlockSize::get();
412-
// TODO: why?
413362
let previous_len_multiplier = previous_len_multiplier.max(min_multiplier);
414363

415364
// Pick the limiting dimension. (from TargetedFeeAdjustment::convert)
416365
//
417366
// In this case it is the length of all extrinsic, always
418-
let (normal_limiting_dimension, max_limiting_dimension) =
419-
(all_extrinsic_len, MAXIMUM_BLOCK_LENGTH);
367+
let (normal_limiting_dimension, max_limiting_dimension) = (
368+
<frame_system::Pallet<T>>::all_extrinsics_len(),
369+
MAXIMUM_BLOCK_LENGTH as u64,
370+
);
420371

421372
let target_block_size = TargetBlockSize::get();
422373
let adjustment_variable = AdjustmentVariableBlockSize::get();
@@ -462,7 +413,7 @@ where
462413
//
463414
// Here is the tricky part, this method return the new value associated with
464415
// NextFeeMultiplier (in the old fashion) because weight dynamic adjustment is battle tested
465-
// while previously have updated the `NextLengthMultiplier` used in `LenghtToWeight`
416+
// while previously have updated the `NextLengthMultiplier` used in `LengthToWeight`
466417
TargetedFeeAdjustment::<
467418
T,
468419
TargetBlockFullness,
@@ -508,9 +459,7 @@ impl pallet_transaction_payment::Config for Runtime {
508459
type RuntimeEvent = RuntimeEvent;
509460
type OnChargeTransaction = pallet_transaction_payment::CurrencyAdapter<Balances, ()>;
510461
type WeightToFee = WeightToFee;
511-
//type LengthToFee = ConstantMultiplier<Balance, TransactionByteFee>;
512462
type LengthToFee = BlobsLengthToFee<Self>;
513-
//type FeeMultiplierUpdate = SlowAdjustingFeeUpdate<Self>;
514463
type FeeMultiplierUpdate = BlobsFeeAdjustment<Self>;
515464
type OperationalFeeMultiplier = ConstU8<5>;
516465
}

sugondat-chain/runtimes/sugondat-kusama/tests/integration_test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,12 @@ fn test_inclusion_fee() {
209209

210210
let length_fee = inclusion_fee - inclusion_fee_zero_length;
211211

212-
let expected_lenght_fee = BlobsLengthToFee::<Runtime>::weight_to_fee(&Weight::from_parts(
212+
let expected_length_fee = BlobsLengthToFee::<Runtime>::weight_to_fee(&Weight::from_parts(
213213
call.size_hint() as u64,
214214
0,
215215
));
216216

217-
assert_eq!(length_fee, expected_lenght_fee);
217+
assert_eq!(length_fee, expected_length_fee);
218218
});
219219
}
220220

@@ -229,6 +229,6 @@ fn test_update_length_and_fee_multipliers() {
229229
NextLengthMultiplier::set(&multiplier);
230230
TransactionPayment::on_finalize(System::block_number());
231231
let new_mutliplier = NextLengthMultiplier::get();
232-
assert!(multiplier != multiplier);
232+
assert!(multiplier != new_mutliplier);
233233
});
234234
}

0 commit comments

Comments
 (0)