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

Post RU: Remove old migrations and bump up versions #1772

Merged
merged 4 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

131 changes: 1 addition & 130 deletions pallets/block-rewards/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use frame_support::{
};
#[cfg(feature = "try-runtime")]
use num_traits::Zero;
#[cfg(feature = "try-runtime")]
use parity_scale_codec::{Decode, Encode};
use sp_runtime::FixedPointNumber;
#[cfg(feature = "try-runtime")]
Expand Down Expand Up @@ -164,133 +165,3 @@ pub mod init {
}
}
}

pub mod v2 {
use frame_support::{
pallet_prelude::ValueQuery, storage_alias, DefaultNoBound, RuntimeDebugNoBound,
};
use parity_scale_codec::MaxEncodedLen;
use scale_info::TypeInfo;

use super::*;
use crate::{CollatorChanges, SessionChanges};

const LOG_PREFIX: &str = "RelativeTreasuryInflation";

#[derive(
Encode, Decode, TypeInfo, DefaultNoBound, MaxEncodedLen, PartialEq, Eq, RuntimeDebugNoBound,
)]
#[scale_info(skip_type_params(T))]
struct OldSessionData<T: Config> {
pub collator_reward: T::Balance,
pub total_reward: T::Balance,
pub collator_count: u32,
}

#[derive(
PartialEq,
Clone,
DefaultNoBound,
Encode,
Decode,
TypeInfo,
MaxEncodedLen,
RuntimeDebugNoBound,
)]
#[scale_info(skip_type_params(T))]
struct OldSessionChanges<T: Config> {
pub collators: CollatorChanges<T>,
pub collator_count: Option<u32>,
pub collator_reward: Option<T::Balance>,
pub total_reward: Option<T::Balance>,
}

#[storage_alias]
type ActiveSessionData<T: Config> = StorageValue<Pallet<T>, OldSessionData<T>, ValueQuery>;
#[storage_alias]
type NextSessionChanges<T: Config> = StorageValue<Pallet<T>, OldSessionChanges<T>, ValueQuery>;

pub struct RelativeTreasuryInflationMigration<T, InflationRate>(
PhantomData<(T, InflationRate)>,
);

impl<T, InflationPercentage> OnRuntimeUpgrade
for RelativeTreasuryInflationMigration<T, InflationPercentage>
where
T: Config,
InflationPercentage: Get<u32>,
{
fn on_runtime_upgrade() -> Weight {
if Pallet::<T>::on_chain_storage_version() == StorageVersion::new(1) {
let active = ActiveSessionData::<T>::take();
let next = NextSessionChanges::<T>::take();

pallet::ActiveSessionData::<T>::put(SessionData {
collator_reward: active.collator_reward,
collator_count: active.collator_count,
treasury_inflation_rate: inflation_rate::<T>(InflationPercentage::get()),
last_update: T::Time::now(),
});
log::info!("{LOG_PREFIX} Translated ActiveSessionData");

pallet::NextSessionChanges::<T>::put(SessionChanges {
collators: next.collators,
collator_count: next.collator_count,
collator_reward: next.collator_reward,
treasury_inflation_rate: Some(inflation_rate::<T>(InflationPercentage::get())),
last_update: T::Time::now(),
});
log::info!("{LOG_PREFIX} Translated NextSessionChanges");
Pallet::<T>::current_storage_version().put::<Pallet<T>>();

T::DbWeight::get().reads_writes(1, 5)
} else {
log::info!("{LOG_PREFIX} BlockRewards pallet already on version 2, migration can be removed");
T::DbWeight::get().reads(1)
}
}

#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, TryRuntimeError> {
assert_eq!(
Pallet::<T>::on_chain_storage_version(),
StorageVersion::new(1),
);
assert!(
Pallet::<T>::on_chain_storage_version() < Pallet::<T>::current_storage_version()
);

let active = ActiveSessionData::<T>::get();
let next = NextSessionChanges::<T>::get();

log::info!("{LOG_PREFIX} PRE UPGRADE: Finished");

Ok((active, next).encode())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(pre_state: Vec<u8>) -> Result<(), TryRuntimeError> {
let (old_active, old_next): (OldSessionData<T>, OldSessionChanges<T>) =
Decode::decode(&mut pre_state.as_slice()).expect("Pre state valid; qed");
let active = pallet::ActiveSessionData::<T>::get();
let next = pallet::NextSessionChanges::<T>::get();

assert_eq!(old_active.collator_reward, active.collator_reward);
assert_eq!(old_active.collator_count, active.collator_count);
assert_eq!(old_next.collators, next.collators);
assert_eq!(old_next.collator_count, next.collator_count);
assert_eq!(old_next.collator_reward, next.collator_reward);
assert_eq!(
next.treasury_inflation_rate,
Some(inflation_rate::<T>(InflationPercentage::get()))
);
assert_eq!(
Pallet::<T>::current_storage_version(),
Pallet::<T>::on_chain_storage_version()
);

log::info!("{LOG_PREFIX} POST UPGRADE: Finished");
Ok(())
}
}
}
59 changes: 0 additions & 59 deletions pallets/loans/src/entities/loans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,62 +580,3 @@ impl<T: Config> TryFrom<(T::PoolId, ActiveLoan<T>)> for ActiveLoanInfo<T> {
})
}
}

/// Migration module that contains old loans types.
/// Can be removed once chains contains pallet-loans version v3
pub(crate) mod v2 {
use cfg_traits::Seconds;
use parity_scale_codec::Decode;

use crate::{
entities::pricing::{external::v2::ExternalActivePricing, internal::InternalActivePricing},
types::{LoanRestrictions, RepaidAmount, RepaymentSchedule},
AssetOf, Config,
};

#[derive(Decode)]
pub enum ActivePricing<T: Config> {
Internal(InternalActivePricing<T>),
External(ExternalActivePricing<T>),
}

#[derive(Decode)]
pub struct ActiveLoan<T: Config> {
schedule: RepaymentSchedule,
collateral: AssetOf<T>,
restrictions: LoanRestrictions,
borrower: T::AccountId,
write_off_percentage: T::Rate,
origination_date: Seconds,
pricing: ActivePricing<T>,
total_borrowed: T::Balance,
total_repaid: RepaidAmount<T::Balance>,
repayments_on_schedule_until: Seconds,
}

impl<T: Config> ActiveLoan<T> {
pub fn migrate(self) -> crate::entities::loans::ActiveLoan<T> {
crate::entities::loans::ActiveLoan {
schedule: self.schedule,
collateral: self.collateral,
restrictions: self.restrictions,
borrower: self.borrower,
write_off_percentage: self.write_off_percentage,
origination_date: self.origination_date,
pricing: match self.pricing {
ActivePricing::Internal(inner) => {
crate::entities::pricing::ActivePricing::Internal(inner)
}
ActivePricing::External(inner) => {
crate::entities::pricing::ActivePricing::External(
inner.migrate(self.origination_date),
)
}
},
total_borrowed: self.total_borrowed,
total_repaid: self.total_repaid,
repayments_on_schedule_until: self.repayments_on_schedule_until,
}
}
}
}
35 changes: 0 additions & 35 deletions pallets/loans/src/entities/pricing/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,38 +288,3 @@ impl<T: Config> ExternalActivePricing<T> {
Ok(())
}
}

/// Migration module that contains old loans types.
/// Can be removed once chains contains pallet-loans version v3
pub(crate) mod v2 {
use cfg_traits::Seconds;
use parity_scale_codec::Decode;

use crate::{
entities::{interest::ActiveInterestRate, pricing::external::ExternalPricing},
Config,
};

#[derive(Decode)]
pub struct ExternalActivePricing<T: Config> {
info: ExternalPricing<T>,
outstanding_quantity: T::Quantity,
interest: ActiveInterestRate<T>,
latest_settlement_price: T::Balance,
}

impl<T: Config> ExternalActivePricing<T> {
pub fn migrate(
self,
settlement_price_updated: Seconds,
) -> crate::entities::pricing::external::ExternalActivePricing<T> {
crate::entities::pricing::external::ExternalActivePricing {
info: self.info,
outstanding_quantity: self.outstanding_quantity,
interest: self.interest,
latest_settlement_price: self.latest_settlement_price,
settlement_price_updated,
}
}
}
}
9 changes: 0 additions & 9 deletions pallets/loans/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ mod tests;
#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;

mod migrations;

pub use pallet::*;
pub use weights::WeightInfo;

Expand Down Expand Up @@ -438,13 +436,6 @@ pub mod pallet {
}
}

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_runtime_upgrade() -> frame_support::weights::Weight {
migrations::migrate_from_v2_to_v3::<T>()
}
}

#[pallet::call]
impl<T: Config> Pallet<T> {
/// Creates a new loan against the collateral provided
Expand Down
75 changes: 0 additions & 75 deletions pallets/loans/src/migrations.rs

This file was deleted.

2 changes: 1 addition & 1 deletion runtime/altair/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "altair-runtime"
version = "0.10.34"
version = "0.10.35"
build = "build.rs"
authors.workspace = true
edition.workspace = true
Expand Down
4 changes: 2 additions & 2 deletions runtime/altair/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("altair"),
impl_name: create_runtime_str!("altair"),
authoring_version: 1,
spec_version: 1034,
spec_version: 1035,
impl_version: 1,
#[cfg(not(feature = "disable-runtime-api"))]
apis: RUNTIME_API_VERSIONS,
Expand Down Expand Up @@ -1946,7 +1946,7 @@ pub type Executive = frame_executive::Executive<
frame_system::ChainContext<Runtime>,
Runtime,
AllPalletsWithSystem,
migrations::UpgradeAltair1034,
migrations::UpgradeAltair1035,
>;

// Frame Order in this block dictates the index of each one in the metadata
Expand Down
Loading
Loading