Skip to content

Commit 9f63f4d

Browse files
author
dio-will
committed
Apply storage migration
1 parent 52369a5 commit 9f63f4d

File tree

3 files changed

+107
-8
lines changed

3 files changed

+107
-8
lines changed

pallets/crowdloans/src/lib.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1810,7 +1810,7 @@ pub mod pallet {
18101810
) -> DispatchResult {
18111811
let ctoken = Self::ctoken_of((&lease_start, &lease_end))
18121812
.ok_or(Error::<T>::CTokenDoesNotExist)?;
1813-
let vault = Self::vaults((&crowdloan, &lease_start, &lease_end))
1813+
let mut vault = Self::vaults((&crowdloan, &lease_start, &lease_end))
18141814
.ok_or(Error::<T>::VaultDoesNotExist)?;
18151815

18161816
ensure!(
@@ -1832,18 +1832,17 @@ pub mod pallet {
18321832
let ctoken_balance = T::Assets::reducible_balance(ctoken, &who, false);
18331833
ensure!(ctoken_balance >= amount, Error::<T>::InsufficientBalance);
18341834

1835-
// NOTE: skipping the vault contribution check
1836-
// vault.contributed = vault
1837-
// .contributed
1838-
// .checked_sub(amount)
1839-
// .ok_or(ArithmeticError::Underflow)?;
1835+
vault.contributed = vault
1836+
.contributed
1837+
.checked_sub(amount)
1838+
.ok_or(ArithmeticError::Underflow)?;
18401839

18411840
T::Assets::burn_from(ctoken, &who, amount)?;
18421841
// SovereignAccount on relaychain must have
18431842
// withdrawn the contribution
18441843
T::Assets::mint_into(T::RelayCurrency::get(), &who, amount)?;
18451844

1846-
// Vaults::<T>::insert((&crowdloan, &lease_start, &lease_end), vault);
1845+
Vaults::<T>::insert((&crowdloan, &lease_start, &lease_end), vault);
18471846

18481847
Self::deposit_event(Event::<T>::VaultRedeemed(
18491848
crowdloan,

pallets/crowdloans/src/migrations.rs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,3 +382,86 @@ pub mod v3 {
382382
Ok(())
383383
}
384384
}
385+
386+
pub mod v4 {
387+
use super::*;
388+
use frame_support::{log, traits::Get};
389+
use primitives::ParaId;
390+
use sp_std::{vec, vec::Vec};
391+
use types::*;
392+
393+
pub fn pre_migrate<T: Config>() -> Result<Vec<u8>, &'static str> {
394+
frame_support::ensure!(
395+
StorageVersion::<T>::get() == Releases::V3_0_0,
396+
"must be V3_0_0"
397+
);
398+
frame_support::ensure!(NextTrieIndex::<T>::get() == 31, "must be 31");
399+
Ok(Vec::new())
400+
}
401+
402+
pub fn migrate<T: Config>() -> frame_support::weights::Weight {
403+
if StorageVersion::<T>::get() == Releases::V3_0_0 {
404+
log::info!(
405+
target: "crowdloans::migrate",
406+
"migrating crowdloan storage"
407+
);
408+
// paraId, ctoken, contributed, cap, end_block, trie_index, lease_start, lease_end
409+
let batch: Vec<(u32, u32, u128, u128, u32, u32, u32, u32)> = vec![
410+
// 2040,8-15,3000000000000000,150000000000000000,10881401
411+
(
412+
2040,
413+
200080015,
414+
3000000000000000,
415+
150_000_000_000_000_000,
416+
10881401,
417+
25,
418+
8,
419+
15,
420+
),
421+
];
422+
let length = batch.len() as u64;
423+
for (para_id, _, raised, _, _, _, lease_start, lease_end) in batch.into_iter() {
424+
match Vaults::<T>::get((&ParaId::from(para_id), &lease_start, &lease_end)) {
425+
Some(vault) if vault.phase == VaultPhase::Expired => {
426+
Vaults::<T>::insert(
427+
(&ParaId::from(para_id), &lease_start, &lease_end),
428+
Vault {
429+
contributed: raised,
430+
..vault
431+
},
432+
);
433+
}
434+
Some(_) => {
435+
log::error!("Vault for para_id {} is not in Expired phase", para_id);
436+
}
437+
None => {
438+
log::error!(
439+
"No vault found for para_id {} ({}, {})",
440+
para_id,
441+
lease_start,
442+
lease_end
443+
);
444+
}
445+
}
446+
}
447+
log::info!(
448+
target: "crowdloans::migrate",
449+
"completed crowdloans storage migration"
450+
);
451+
<T as frame_system::Config>::DbWeight::get().writes(length * 3 + 1u64)
452+
} else {
453+
T::DbWeight::get().reads(1)
454+
}
455+
}
456+
457+
pub fn post_migrate<T: Config>() -> Result<(), &'static str> {
458+
frame_support::ensure!(
459+
StorageVersion::<T>::get() == Releases::V3_0_0,
460+
"must be V3_0_0"
461+
);
462+
frame_support::ensure!(NextTrieIndex::<T>::get() == 31, "must be 31");
463+
log::info!("👜 crowdloan migration passes POST migrate checks ✅",);
464+
465+
Ok(())
466+
}
467+
}

runtime/parallel/src/lib.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2073,7 +2073,7 @@ pub type Executive = frame_executive::Executive<
20732073
frame_system::ChainContext<Runtime>,
20742074
Runtime,
20752075
AllPalletsWithSystem,
2076-
CrowdloansMigrationV3,
2076+
CrowdloansMigrationV4,
20772077
>;
20782078

20792079
impl fp_self_contained::SelfContainedCall for RuntimeCall {
@@ -2134,6 +2134,23 @@ impl fp_self_contained::SelfContainedCall for RuntimeCall {
21342134
}
21352135
}
21362136

2137+
pub struct CrowdloansMigrationV4;
2138+
impl OnRuntimeUpgrade for CrowdloansMigrationV4 {
2139+
fn on_runtime_upgrade() -> frame_support::weights::Weight {
2140+
pallet_crowdloans::migrations::v4::migrate::<Runtime>()
2141+
}
2142+
2143+
#[cfg(feature = "try-runtime")]
2144+
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
2145+
pallet_crowdloans::migrations::v4::pre_migrate::<Runtime>()
2146+
}
2147+
2148+
#[cfg(feature = "try-runtime")]
2149+
fn post_upgrade(_: Vec<u8>) -> Result<(), &'static str> {
2150+
pallet_crowdloans::migrations::v4::post_migrate::<Runtime>()
2151+
}
2152+
}
2153+
21372154
impl_runtime_apis! {
21382155
impl sp_consensus_aura::AuraApi<Block, AuraId> for Runtime {
21392156
fn slot_duration() -> sp_consensus_aura::SlotDuration {

0 commit comments

Comments
 (0)