Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stakingv3: rank dapps in-between tiers #1240

Merged
merged 24 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion pallets/dapp-staking-v3/src/benchmarking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1049,7 +1049,7 @@ mod benchmarks {
.try_into()
.expect("Using `NumberOfTiers` as length; QED."),
period: 1,
rank_rewards: vec![0; T::NumberOfTiers::get() as usize]
rank_rewards: vec![0; T::NumberOfTiers::get() as usize],
},
);

Expand Down
4 changes: 3 additions & 1 deletion pallets/dapp-staking-v3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ mod benchmarking;
mod types;
pub use types::*;

pub mod migration;
pub mod weights;

pub use weights::WeightInfo;

const LOG_TARGET: &str = "dapp-staking";
Expand All @@ -92,7 +94,7 @@ pub mod pallet {
use super::*;

/// The current storage version.
pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(6);
pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(7);

#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
Expand Down
112 changes: 112 additions & 0 deletions pallets/dapp-staking-v3/src/migration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// 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::traits::OnRuntimeUpgrade;

#[cfg(feature = "try-runtime")]
use sp_std::vec::Vec;

#[cfg(feature = "try-runtime")]
use sp_runtime::TryRuntimeError;

/// Exports for versioned migration `type`s for this pallet.
pub mod versioned_migrations {
use super::*;

/// Migration V6 to V7 wrapped in a [`frame_support::migrations::VersionedMigration`], ensuring
/// the migration is only performed when on-chain version is 6.
pub type V6ToV7<T> = frame_support::migrations::VersionedMigration<
6,
7,
v7::VersionUncheckedMigrateV6ToV7<T>,
Pallet<T>,
<T as frame_system::Config>::DbWeight,
>;
}

/// Translate DAppTiers to include rank rewards.
mod v7 {
use super::*;
use crate::migration::v6::DAppTierRewards as DAppTierRewardsV6;

pub struct VersionUncheckedMigrateV6ToV7<T>(PhantomData<T>);

impl<T: Config> OnRuntimeUpgrade for VersionUncheckedMigrateV6ToV7<T> {
ermalkaleci marked this conversation as resolved.
Show resolved Hide resolved
fn on_runtime_upgrade() -> Weight {
let current = Pallet::<T>::current_storage_version();

let mut translated = 0usize;
DAppTiers::<T>::translate::<
DAppTierRewardsV6<T::MaxNumberOfContracts, T::NumberOfTiers>,
_,
>(|_key, old_value| {
translated.saturating_inc();
Some(DAppTierRewards {
dapps: old_value.dapps,
rewards: old_value.rewards,
period: old_value.period,
rank_rewards: Default::default(),
ermalkaleci marked this conversation as resolved.
Show resolved Hide resolved
})
});

current.put::<Pallet<T>>();

log::info!("Upgraded {translated} dAppTiers to {current:?}");

T::DbWeight::get().reads_writes(1 + translated as u64, 1 + translated as u64)
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> {
Ok(Vec::new())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(_data: Vec<u8>) -> Result<(), TryRuntimeError> {
ensure!(
Pallet::<T>::on_chain_storage_version() >= 7,
"dapp-staking-v3::migration::v7: wrong storage version"
);
Ok(())
}
}
}

pub mod v6 {
use astar_primitives::{
dapp_staking::{DAppId, PeriodNumber, RankedTier},
Balance,
};
use frame_support::{
pallet_prelude::{Decode, Get},
BoundedBTreeMap, BoundedVec,
};

/// Information about all of the dApps that got into tiers, and tier rewards
#[derive(Decode)]
pub struct DAppTierRewards<MD: Get<u32>, NT: Get<u32>> {
/// DApps and their corresponding tiers (or `None` if they have been claimed in the meantime)
pub dapps: BoundedBTreeMap<DAppId, RankedTier, MD>,
/// Rewards for each tier. First entry refers to the first tier, and so on.
pub rewards: BoundedVec<Balance, NT>,
/// Period during which this struct was created.
#[codec(compact)]
pub period: PeriodNumber,
}
}
2 changes: 1 addition & 1 deletion runtime/astar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1174,7 +1174,7 @@ pub type Executive = frame_executive::Executive<
/// All migrations that will run on the next runtime upgrade.
///
/// Once done, migrations should be removed from the tuple.
pub type Migrations = ();
pub type Migrations = (pallet_dapp_staking_v3::migration::versioned_migrations::V6ToV7<Runtime>);

type EventRecord = frame_system::EventRecord<
<Runtime as frame_system::Config>::RuntimeEvent,
Expand Down
5 changes: 4 additions & 1 deletion runtime/shibuya/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1298,7 +1298,10 @@ pub type Executive = frame_executive::Executive<
/// All migrations that will run on the next runtime upgrade.
///
/// Once done, migrations should be removed from the tuple.
pub type Migrations = ();
pub type Migrations = (
pallet_preimage::migration::v1::Migration<Runtime>,
ermalkaleci marked this conversation as resolved.
Show resolved Hide resolved
pallet_dapp_staking_v3::migration::versioned_migrations::V6ToV7<Runtime>,
);

type EventRecord = frame_system::EventRecord<
<Runtime as frame_system::Config>::RuntimeEvent,
Expand Down
2 changes: 1 addition & 1 deletion runtime/shiden/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1175,7 +1175,7 @@ pub type Executive = frame_executive::Executive<
/// All migrations that will run on the next runtime upgrade.
///
/// Once done, migrations should be removed from the tuple.
pub type Migrations = ();
pub type Migrations = (pallet_dapp_staking_v3::migration::versioned_migrations::V6ToV7<Runtime>,);

type EventRecord = frame_system::EventRecord<
<Runtime as frame_system::Config>::RuntimeEvent,
Expand Down
Loading