Skip to content

Commit

Permalink
next_era_start and block_reward adjusment
Browse files Browse the repository at this point in the history
  • Loading branch information
ermalkaleci committed Sep 4, 2024
1 parent f594d61 commit 22abf1a
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 43 deletions.
33 changes: 28 additions & 5 deletions pallets/dapp-staking/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use super::*;
use frame_support::{
migrations::{MigrationId, SteppedMigration, SteppedMigrationError},
storage_alias,
traits::UncheckedOnRuntimeUpgrade,
traits::{OnRuntimeUpgrade, UncheckedOnRuntimeUpgrade},
weights::WeightMeter,
};

Expand Down Expand Up @@ -449,6 +449,7 @@ impl<T: Config, W: WeightInfo> SteppedMigration for LazyMigration<T, W> {
}

let mut count = 0u32;
let mut migrated = 0u32;
let current_block_number =
frame_system::Pallet::<T>::block_number().saturated_into::<u32>();

Expand All @@ -471,9 +472,13 @@ impl<T: Config, W: WeightInfo> SteppedMigration for LazyMigration<T, W> {

// If there's a next item in the iterator, perform the migration.
if let Some((ref last_key, mut ledger)) = iter.next() {
// inc count
count.saturating_inc();

if ledger.unlocking.is_empty() {
// no unlocking for this account, nothing to update
cursor = Some(last_key.clone()); // Return the processed key as the new cursor.
// Return the processed key as the new cursor.
cursor = Some(last_key.clone());
continue;
}
for chunk in ledger.unlocking.iter_mut() {
Expand All @@ -487,8 +492,8 @@ impl<T: Config, W: WeightInfo> SteppedMigration for LazyMigration<T, W> {
// Override ledger
Ledger::<T>::insert(last_key, ledger);

// inc counter
count.saturating_inc();
// inc migrated
migrated.saturating_inc();

// Return the processed key as the new cursor.
cursor = Some(last_key.clone())
Expand All @@ -498,7 +503,25 @@ impl<T: Config, W: WeightInfo> SteppedMigration for LazyMigration<T, W> {
break;
}
}
log::debug!(target: LOG_TARGET, "migrated {count:?} entries");
log::info!(target: LOG_TARGET, "🚚 iterated {count} entries, migrated {migrated}");
Ok(cursor)
}
}

/// Double the remaining block for next era start
pub struct AdjustEraMigration<T>(PhantomData<T>);

impl<T: Config> OnRuntimeUpgrade for AdjustEraMigration<T> {
fn on_runtime_upgrade() -> Weight {
log::info!("🚚 migrated to async backing, adjust next era start");
ActiveProtocolState::<T>::mutate_exists(|maybe| {
if let Some(state) = maybe {
let current_block_number =
frame_system::Pallet::<T>::block_number().saturated_into::<u32>();
let remaining = state.next_era_start.saturating_sub(current_block_number);
state.next_era_start.saturating_accrue(2 * remaining);
}
});
T::DbWeight::get().reads_writes(1, 1)
}
}
1 change: 1 addition & 0 deletions pallets/inflation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ pub use weights::WeightInfo;
#[cfg(any(feature = "runtime-benchmarks"))]
pub mod benchmarking;

pub mod migration;
#[cfg(test)]
mod mock;
#[cfg(test)]
Expand Down
39 changes: 39 additions & 0 deletions pallets/inflation/src/migration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// This file is part of Astar.

// Copyright (C) Stake Technologies Pte.Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later

// Astar is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Astar is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Astar. If not, see <http://www.gnu.org/licenses/>.

use super::*;
use frame_support::pallet_prelude::Weight;
use frame_support::traits::OnRuntimeUpgrade;

/// Half block reward for collators and treasury
pub struct AdjustBlockRewardMigration<T>(core::marker::PhantomData<T>);

impl<T: Config> OnRuntimeUpgrade for AdjustBlockRewardMigration<T> {
fn on_runtime_upgrade() -> Weight {
log::info!("🚚 migrated to async backing, adjust reward per block");
ActiveInflationConfig::<T>::mutate_exists(|maybe| {
if let Some(config) = maybe {
config.collator_reward_per_block =
config.collator_reward_per_block.saturating_div(2);
config.treasury_reward_per_block =
config.treasury_reward_per_block.saturating_div(2);
}
});
T::DbWeight::get().reads_writes(1, 1)
}
}
40 changes: 2 additions & 38 deletions runtime/shibuya/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1646,51 +1646,15 @@ pub type Executive = frame_executive::Executive<
Migrations,
>;

parameter_types! {
// Threshold amount variation allowed for this migration - 150%
pub const ThresholdVariationPercentage: u32 = 150;
// percentages below are calculated based on a total issuance at the time when dApp staking v3 was launched (147M)
pub const TierThresholds: [TierThreshold; 4] = [
TierThreshold::DynamicPercentage {
percentage: Perbill::from_parts(20_000), // 0.0020%
minimum_required_percentage: Perbill::from_parts(17_000), // 0.0017%
},
TierThreshold::DynamicPercentage {
percentage: Perbill::from_parts(13_000), // 0.0013%
minimum_required_percentage: Perbill::from_parts(10_000), // 0.0010%
},
TierThreshold::DynamicPercentage {
percentage: Perbill::from_parts(5_400), // 0.00054%
minimum_required_percentage: Perbill::from_parts(3_400), // 0.00034%
},
TierThreshold::FixedPercentage {
required_percentage: Perbill::from_parts(1_400), // 0.00014%
},
];
}

parameter_types! {
pub const DmpQueuePalletName: &'static str = "DmpQueue";
}

/// All migrations that will run on the next runtime upgrade.
///
/// __NOTE:__ THE ORDER IS IMPORTANT.
pub type Migrations = (Unreleased, Permanent);

/// Unreleased migrations. Add new ones here:
pub type Unreleased = (
// dApp-staking dyn tier threshold migrations
pallet_dapp_staking::migration::versioned_migrations::V7ToV8<
Runtime,
TierThresholds,
ThresholdVariationPercentage,
>,
frame_support::migrations::RemovePallet<
DmpQueuePalletName,
<Runtime as frame_system::Config>::DbWeight,
>,
pallet_contracts::Migration<Runtime>,
pallet_dapp_staking::migration::AdjustEraMigration<Runtime>,
pallet_inflation::migration::AdjustBlockRewardMigration<Runtime>,
);

/// Migrations/checks that do not need to be versioned and can run on every upgrade.
Expand Down

0 comments on commit 22abf1a

Please sign in to comment.