Skip to content

Commit

Permalink
fix: added test case for redeem migration.
Browse files Browse the repository at this point in the history
  • Loading branch information
nakul1010 committed Aug 21, 2023
1 parent a007a5c commit c7f5ee2
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 7 deletions.
2 changes: 1 addition & 1 deletion crates/issue/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use vault_registry::types::CurrencyId;
use crate::Config;

/// Storage version.
#[derive(Encode, Decode, Eq, PartialEq, TypeInfo, MaxEncodedLen)]
#[derive(Debug, Encode, Decode, Eq, PartialEq, TypeInfo, MaxEncodedLen)]
pub enum Version {
/// Initial version.
V0,
Expand Down
69 changes: 63 additions & 6 deletions crates/redeem/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,26 @@ use scale_info::TypeInfo;
use sp_runtime::DispatchError;
use vault_registry::types::CurrencyId;

use crate::Config;
use crate::{Config, Pallet};
use btc_relay::BtcAddress;
use codec::{Decode, Encode, MaxEncodedLen};
use currency::Amount;
use frame_support::traits::Get;
use frame_support::{pallet_prelude::OptionQuery, traits::Get, Blake2_128Concat};
#[cfg(feature = "std")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use sp_core::H256;

/// Storage version.
#[derive(Encode, Decode, Eq, PartialEq, TypeInfo, MaxEncodedLen)]
#[derive(Debug, Encode, Decode, Eq, PartialEq, TypeInfo, MaxEncodedLen)]
pub enum Version {
/// Initial version.
V0,
/// Added `issue_id` to `RedeemRequest`
V1,
}

pub mod v1 {
mod v0 {
use super::*;
use btc_relay::BtcAddress;

pub type DefaultRedeemRequest<T> = RedeemRequest<
<T as frame_system::Config>::AccountId,
Expand Down Expand Up @@ -88,12 +89,20 @@ pub mod v1 {
.map_err(|_| serde::de::Error::custom("Parse from string failed"))
}

#[frame_support::storage_alias]
pub(super) type RedeemRequests<T: Config> =
StorageMap<Pallet<T>, Blake2_128Concat, H256, DefaultRedeemRequest<T>, OptionQuery>;
}

pub mod v1 {
use super::*;

pub fn migrate_v0_to_v1<T: Config>() -> frame_support::weights::Weight {
if !matches!(crate::StorageVersion::<T>::get(), Version::V0) {
return T::DbWeight::get().reads(1); // already upgraded; don't run migration
}
// update vault struct to remove replace pallet fields
crate::RedeemRequests::<T>::translate(|_key, old: v1::DefaultRedeemRequest<T>| {
crate::RedeemRequests::<T>::translate(|_key, old: v0::DefaultRedeemRequest<T>| {
Some(crate::DefaultRedeemRequest::<T> {
vault: old.vault,
opentime: old.opentime,
Expand Down Expand Up @@ -149,3 +158,51 @@ impl<T: Config> RedeemRequestExt<T> for RedeemRequest<T::AccountId, T::BlockNumb
Amount::new(self.transfer_fee_btc, self.vault.wrapped_currency())
}
}

#[cfg(test)]
mod test {
use super::*;
use crate::mock::{Test as T, *};

#[test]
fn migrating_from_v0_to_v1() {
run_test(|| {
assert_eq!(crate::StorageVersion::<T>::get(), Version::V0);

let old = v0::DefaultRedeemRequest::<T> {
vault: DefaultVaultId::<T>::new(123, Token(DOT), Token(IBTC)),
opentime: 1,
period: 12313,
fee: 456,
transfer_fee_btc: 1,
amount_btc: 100,
premium: 0,
redeemer: USER,
btc_address: BtcAddress::random(),
btc_height: 0,
status: RedeemRequestStatus::Reimbursed(false),
};
let key = H256::zero();
v0::RedeemRequests::<T>::insert(key, old.clone());

v1::migrate_v0_to_v1::<T>();

assert_eq!(crate::StorageVersion::<T>::get(), Version::V1);

let new = crate::RedeemRequests::<T>::get(key).unwrap();

assert!(old.vault == new.vault);
assert!(old.opentime == new.opentime);
assert!(old.period == new.period);
assert!(old.fee == new.fee);
assert!(old.transfer_fee_btc == new.transfer_fee_btc);
assert!(old.amount_btc == new.amount_btc);
assert!(old.premium == new.premium);
assert!(old.redeemer == new.redeemer);
assert!(old.btc_address == new.btc_address);
assert!(old.btc_height == new.btc_height);
assert!(old.status == new.status);
assert_eq!(new.issue_id.is_none(), true);
});
}
}

0 comments on commit c7f5ee2

Please sign in to comment.