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

fix: Update storage version type. #1106

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 5 additions & 11 deletions crates/fee/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use sp_std::{
fmt::Debug,
};
use staking::StakingApi;
use types::{BalanceOf, DefaultVaultCurrencyPair, DefaultVaultId, UnsignedFixedPoint, Version};
use types::{BalanceOf, DefaultVaultCurrencyPair, DefaultVaultId, UnsignedFixedPoint};

#[frame_support::pallet]
pub mod pallet {
Expand Down Expand Up @@ -156,16 +156,6 @@ pub mod pallet {
#[pallet::getter(fn replace_griefing_collateral)]
pub type ReplaceGriefingCollateral<T: Config> = StorageValue<_, UnsignedFixedPoint<T>, ValueQuery>;

#[pallet::type_value]
pub(super) fn DefaultForStorageVersion() -> Version {
Version::V0
}

/// Build storage at V1 (requires default 0).
#[pallet::storage]
#[pallet::getter(fn storage_version)]
pub(super) type StorageVersion<T: Config> = StorageValue<_, Version, ValueQuery, DefaultForStorageVersion>;

/// The fraction up rewards going straight to the vault operator. The rest goes to the vault's pool.
#[pallet::storage]
pub(super) type Commission<T: Config> =
Expand Down Expand Up @@ -207,7 +197,11 @@ pub mod pallet {
}
}

/// The current storage version.
const STORAGE_VERSION: StorageVersion = StorageVersion::new(0);

#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
pub struct Pallet<T>(_);

// The pallet's dispatchable functions.
Expand Down
22 changes: 12 additions & 10 deletions crates/issue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ pub mod pallet {
type WeightInfo: WeightInfo;
}

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_runtime_upgrade() -> Weight {
StorageVersion::new(Version::V4 as u16).put::<Pallet<T>>();
Default::default()
}
}

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
Expand Down Expand Up @@ -148,16 +156,6 @@ pub mod pallet {
#[pallet::storage]
pub(super) type IssueBtcDustValue<T: Config> = StorageValue<_, BalanceOf<T>, ValueQuery>;

#[pallet::type_value]
pub(super) fn DefaultForStorageVersion() -> Version {
Version::V4
}

/// Build storage at V1 (requires default 0).
#[pallet::storage]
#[pallet::getter(fn storage_version)]
pub(super) type StorageVersion<T: Config> = StorageValue<_, Version, ValueQuery, DefaultForStorageVersion>;

#[pallet::genesis_config]
pub struct GenesisConfig<T: Config> {
pub issue_period: T::BlockNumber,
Expand All @@ -182,7 +180,11 @@ pub mod pallet {
}
}

/// The current storage version.
const STORAGE_VERSION: StorageVersion = StorageVersion::new(0);

#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
pub struct Pallet<T>(_);

// The pallet's dispatchable functions.
Expand Down
10 changes: 5 additions & 5 deletions crates/issue/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ use crate::Config;
#[derive(Encode, Decode, Eq, PartialEq, TypeInfo, MaxEncodedLen)]
pub enum Version {
/// Initial version.
V0,
V0 = 0,
/// BtcAddress type with script format.
V1,
V1 = 1,
/// IssueRequestStatus
V2,
V2 = 2,
/// ActiveBlockNumber, btc_height
V3,
V3 = 3,
/// Removed refund
V4,
V4 = 4,
}

pub(crate) type BalanceOf<T> = <T as currency::Config>::Balance;
Expand Down
16 changes: 5 additions & 11 deletions crates/oracle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extern crate mocktopus;
#[cfg(test)]
use mocktopus::macros::mockable;

use crate::types::{BalanceOf, UnsignedFixedPoint, Version};
use crate::types::{BalanceOf, UnsignedFixedPoint};
use codec::{Decode, Encode, MaxEncodedLen};
use currency::Amount;
use frame_support::{
Expand Down Expand Up @@ -153,16 +153,6 @@ pub mod pallet {
#[pallet::getter(fn authorized_oracles)]
pub type AuthorizedOracles<T: Config> = StorageMap<_, Blake2_128Concat, T::AccountId, NameOf<T>, ValueQuery>;

#[pallet::type_value]
pub(super) fn DefaultForStorageVersion() -> Version {
Version::V0
}

/// Build storage at V1 (requires default 0).
#[pallet::storage]
#[pallet::getter(fn storage_version)]
pub(super) type StorageVersion<T: Config> = StorageValue<_, Version, ValueQuery, DefaultForStorageVersion>;

#[pallet::genesis_config]
pub struct GenesisConfig<T: Config> {
pub max_delay: u32,
Expand Down Expand Up @@ -192,7 +182,11 @@ pub mod pallet {
}
}

/// The current storage version.
const STORAGE_VERSION: StorageVersion = StorageVersion::new(0);

#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
pub struct Pallet<T>(_);

// The pallet's dispatchable functions.
Expand Down
16 changes: 5 additions & 11 deletions crates/redeem/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub mod types;
#[doc(inline)]
pub use crate::types::{DefaultRedeemRequest, RedeemRequest, RedeemRequestStatus};

use crate::types::{BalanceOf, RedeemRequestExt, Version};
use crate::types::{BalanceOf, RedeemRequestExt};
use bitcoin::types::{MerkleProof, Transaction};
use btc_relay::BtcAddress;
use currency::Amount;
Expand Down Expand Up @@ -163,16 +163,6 @@ pub mod pallet {
#[pallet::getter(fn redeem_transaction_size)]
pub(super) type RedeemTransactionSize<T: Config> = StorageValue<_, u32, ValueQuery>;

#[pallet::type_value]
pub(super) fn DefaultForStorageVersion() -> Version {
Version::V0
}

/// Build storage at V1 (requires default 0).
#[pallet::storage]
#[pallet::getter(fn storage_version)]
pub(super) type StorageVersion<T: Config> = StorageValue<_, Version, ValueQuery, DefaultForStorageVersion>;

#[pallet::genesis_config]
pub struct GenesisConfig<T: Config> {
pub redeem_period: T::BlockNumber,
Expand Down Expand Up @@ -203,7 +193,11 @@ pub mod pallet {
#[pallet::hooks]
impl<T: Config> Hooks<T::BlockNumber> for Pallet<T> {}

/// The current storage version.
const STORAGE_VERSION: StorageVersion = StorageVersion::new(0);

#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
pub struct Pallet<T>(_);

// The pallet's dispatchable functions.
Expand Down
9 changes: 0 additions & 9 deletions crates/redeem/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
pub use primitives::redeem::{RedeemRequest, RedeemRequestStatus};
use primitives::VaultId;
use scale_info::TypeInfo;
use sp_runtime::DispatchError;
use vault_registry::types::CurrencyId;

use crate::Config;
use codec::{Decode, Encode, MaxEncodedLen};
use currency::Amount;

/// Storage version.
#[derive(Encode, Decode, Eq, PartialEq, TypeInfo, MaxEncodedLen)]
pub enum Version {
/// Initial version.
V0,
}

pub(crate) type BalanceOf<T> = <T as currency::Config>::Balance;

pub(crate) type DefaultVaultId<T> = VaultId<<T as frame_system::Config>::AccountId, CurrencyId<T>>;
Expand Down
16 changes: 5 additions & 11 deletions crates/replace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ extern crate mocktopus;
#[cfg(test)]
use mocktopus::macros::mockable;

use crate::types::{BalanceOf, ReplaceRequestExt, Version};
use crate::types::{BalanceOf, ReplaceRequestExt};
pub use crate::types::{DefaultReplaceRequest, ReplaceRequest, ReplaceRequestStatus};
use bitcoin::types::{MerkleProof, Transaction};
use btc_relay::BtcAddress;
Expand Down Expand Up @@ -153,16 +153,6 @@ pub mod pallet {
#[pallet::getter(fn replace_btc_dust_value)]
pub(super) type ReplaceBtcDustValue<T: Config> = StorageValue<_, BalanceOf<T>, ValueQuery>;

#[pallet::type_value]
pub(super) fn DefaultForStorageVersion() -> Version {
Version::V0
}

/// Build storage at V1 (requires default 0).
#[pallet::storage]
#[pallet::getter(fn storage_version)]
pub(super) type StorageVersion<T: Config> = StorageValue<_, Version, ValueQuery, DefaultForStorageVersion>;

#[pallet::genesis_config]
pub struct GenesisConfig<T: Config> {
pub replace_period: T::BlockNumber,
Expand Down Expand Up @@ -190,7 +180,11 @@ pub mod pallet {
#[pallet::hooks]
impl<T: Config> Hooks<T::BlockNumber> for Pallet<T> {}

/// The current storage version.
const STORAGE_VERSION: StorageVersion = StorageVersion::new(0);

#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
pub struct Pallet<T>(_);

// The pallet's dispatchable functions.
Expand Down
17 changes: 6 additions & 11 deletions crates/vault-registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ pub mod pallet {
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;

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

#[pallet::pallet]
#[pallet::storage_version(STORAGE_VERSION)]
pub struct Pallet<T>(_);

#[pallet::config]
Expand Down Expand Up @@ -634,16 +638,6 @@ pub mod pallet {
pub(super) type TotalUserVaultCollateral<T: Config> =
StorageMap<_, Blake2_128Concat, DefaultVaultCurrencyPair<T>, BalanceOf<T>, ValueQuery>;

#[pallet::type_value]
pub(super) fn DefaultForStorageVersion() -> Version {
Version::V6
}

/// Pallet storage version
#[pallet::storage]
#[pallet::getter(fn storage_version)]
pub(super) type StorageVersion<T: Config> = StorageValue<_, Version, ValueQuery, DefaultForStorageVersion>;

#[pallet::genesis_config]
pub struct GenesisConfig<T: Config> {
pub minimum_collateral_vault: Vec<(CurrencyId<T>, BalanceOf<T>)>,
Expand Down Expand Up @@ -687,7 +681,8 @@ pub mod pallet {
for (currency_pair, threshold) in self.liquidation_collateral_threshold.iter() {
LiquidationCollateralThreshold::<T>::insert(currency_pair, threshold);
}
StorageVersion::<T>::put(Version::V3);

StorageVersion::new(Version::V6 as u16).put::<Pallet<T>>();
}
}
}
Expand Down
22 changes: 13 additions & 9 deletions crates/vault-registry/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ pub use bitcoin::{Address as BtcAddress, PublicKey as BtcPublicKey};
#[derive(Encode, Decode, Eq, PartialEq, TypeInfo, MaxEncodedLen)]
pub enum Version {
/// Initial version.
V0,
V0 = 0,
/// BtcAddress type with script format.
V1,
V1 = 1,
/// added replace_collateral to vault, changed vaultStatus enum
V2,
V2 = 2,
/// moved public_key out of the vault struct
V3,
V3 = 3,
/// Fixed liquidation vault
V4,
V4 = 4,
/// Added custom pervault secure collateral threshold
V5,
V5 = 5,
/// Removed wallet
V6,
V6 = 6,
}

#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -110,16 +110,20 @@ pub type DefaultVaultId<T> = VaultId<<T as frame_system::Config>::AccountId, Cur
pub type DefaultVaultCurrencyPair<T> = VaultCurrencyPair<CurrencyId<T>>;

pub mod v1 {
use frame_support::{dispatch::GetStorageVersion, traits::StorageVersion};

use super::*;

pub fn migrate_v1_to_v6<T: Config>() -> frame_support::weights::Weight {
// kintsugi is on V6 but interlay is still on V1
if !matches!(crate::StorageVersion::<T>::get(), Version::V1) {
let current_storage_version = Pallet::<T>::current_storage_version();
let _expected_storage_version = Version::V1 as u16;
if !matches!(current_storage_version, _expected_storage_version) {
log::info!("Not running vault storage migration");
return T::DbWeight::get().reads(1); // already upgraded; don't run migration
}
// nothing to do other than update version
crate::StorageVersion::<T>::put(Version::V6);
StorageVersion::new(Version::V6 as u16).put::<Pallet<T>>();
T::DbWeight::get().reads_writes(0, 1)
}
}
Expand Down