Skip to content

Commit ecc129b

Browse files
committed
split config decay and block reward payouts
1 parent 3e7b3b7 commit ecc129b

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

pallets/inflation/src/lib.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -228,12 +228,12 @@ pub mod pallet {
228228
#[pallet::hooks]
229229
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
230230
fn on_initialize(_now: BlockNumberFor<T>) -> Weight {
231-
let decay_payout_weight = Self::decay_and_payout_block_rewards();
231+
let (config, weight) = Self::decay_config();
232+
Self::payout_block_rewards(&config);
232233

233234
// Benchmarks won't account for the whitelisted storage access so this needs to be added manually.
234235
// DoRecalculation - 1 DB read
235-
decay_payout_weight
236-
.saturating_add(<T as frame_system::Config>::DbWeight::get().reads(1))
236+
weight.saturating_add(<T as frame_system::Config>::DbWeight::get().reads(1))
237237
}
238238

239239
fn on_finalize(_now: BlockNumberFor<T>) {
@@ -354,10 +354,20 @@ pub mod pallet {
354354
}
355355

356356
impl<T: Config> Pallet<T> {
357-
/// Apply per-block decay and payout block rewards.
358-
pub(crate) fn decay_and_payout_block_rewards() -> Weight {
357+
/// Payout block rewards to the beneficiaries using the provided inflation config.
358+
pub(crate) fn payout_block_rewards(config: &InflationConfiguration) {
359+
let collator_amount = T::Currency::issue(config.collator_reward_per_block);
360+
let treasury_amount = T::Currency::issue(config.treasury_reward_per_block);
361+
362+
T::PayoutPerBlock::collators(collator_amount);
363+
T::PayoutPerBlock::treasury(treasury_amount);
364+
}
365+
366+
/// Apply decay to the inflation config.
367+
/// Returns the updated configuration and the weight consumed.
368+
pub(crate) fn decay_config() -> (InflationConfiguration, Weight) {
359369
let mut config = ActiveInflationConfig::<T>::get();
360-
let mut weight = T::DbWeight::get().reads(1);
370+
let weight = T::DbWeight::get().reads(1);
361371

362372
let decay_rate = config.decay_rate;
363373
if decay_rate != Perquintill::one() {
@@ -372,16 +382,10 @@ pub mod pallet {
372382
decay_rate * config.bonus_reward_pool_per_period;
373383

374384
ActiveInflationConfig::<T>::put(&config);
375-
weight = T::DbWeight::get().reads_writes(1, 1);
385+
(config, T::DbWeight::get().reads_writes(1, 1))
386+
} else {
387+
(config, weight)
376388
}
377-
378-
let collator_amount = T::Currency::issue(config.collator_reward_per_block);
379-
let treasury_amount = T::Currency::issue(config.treasury_reward_per_block);
380-
381-
T::PayoutPerBlock::collators(collator_amount);
382-
T::PayoutPerBlock::treasury(treasury_amount);
383-
384-
weight
385389
}
386390

387391
/// Recalculates the inflation based on the current total issuance & inflation parameters.
@@ -421,6 +425,7 @@ pub mod pallet {
421425
Self::new_config(config.recalculation_era, max_emission)
422426
}
423427

428+
/// This function derives the `max_emission` value used to calculate the provided inflation config.
424429
pub(crate) fn derive_max_emission_from_config(config: &InflationConfiguration) -> Balance {
425430
// 1. Simple type conversion.
426431
let blocks_per_cycle = Balance::from(T::CycleConfiguration::blocks_per_cycle());

pallets/inflation/src/tests.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,8 @@ fn decay_and_payout_works() {
534534
config.treasury_reward_per_block = 5;
535535
});
536536

537-
Inflation::decay_and_payout_block_rewards();
537+
let (config, _) = Inflation::decay_config();
538+
Inflation::payout_block_rewards(&config);
538539

539540
// Config unchanged
540541
let cfg = ActiveInflationConfig::<Test>::get();
@@ -546,7 +547,8 @@ fn decay_and_payout_works() {
546547
config.decay_rate = Perquintill::from_percent(50);
547548
});
548549

549-
Inflation::decay_and_payout_block_rewards();
550+
let (config, _) = Inflation::decay_config();
551+
Inflation::payout_block_rewards(&config);
550552

551553
// Config decayed
552554
let cfg = ActiveInflationConfig::<Test>::get();

0 commit comments

Comments
 (0)