Skip to content

Commit c5c8e08

Browse files
committed
Migrated pallet funding for block number provider
1 parent d1e02ed commit c5c8e08

File tree

5 files changed

+167
-13
lines changed

5 files changed

+167
-13
lines changed

pallets/funding/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ pub use types::*;
9595
use xcm::v5::prelude::*;
9696

9797
pub mod functions;
98-
pub mod storage_migrations;
98+
pub mod migrations;
9999
pub mod traits;
100100
pub mod types;
101101
pub mod weights;
@@ -166,7 +166,7 @@ pub mod pallet {
166166
}
167167

168168
#[pallet::pallet]
169-
#[pallet::storage_version(storage_migrations::STORAGE_VERSION)]
169+
#[pallet::storage_version(migrations::STORAGE_VERSION)]
170170
pub struct Pallet<T>(_);
171171

172172
#[pallet::config]

pallets/funding/src/migrations/mod.rs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//! Migrations
2+
3+
use frame_support::traits::StorageVersion;
4+
5+
pub mod storage_migrations;
6+
pub mod vesting_info;
7+
8+
/// Current storage version
9+
pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(7);

pallets/funding/src/storage_migrations.rs pallets/funding/src/migrations/storage_migrations.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{
44
ProjectMetadataOf, StringLimitOf,
55
};
66
use core::marker::PhantomData;
7-
use frame_support::traits::{StorageVersion, UncheckedOnRuntimeUpgrade};
7+
use frame_support::traits::UncheckedOnRuntimeUpgrade;
88
use polimec_common::{assets::AcceptedFundingAsset, credentials::Cid};
99
use scale_info::TypeInfo;
1010
use serde::{Deserialize, Serialize};
@@ -15,10 +15,6 @@ use alloc::vec::Vec;
1515
use polimec_common::migration_types::{MigrationInfo, ParticipationType};
1616
use xcm::v5::Location;
1717

18-
/// The current storage version
19-
pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(6);
20-
pub const LOG: &str = "runtime::funding::migration";
21-
2218
pub mod v5_storage_items {
2319

2420
#[derive(Clone, Copy, Encode, Decode, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
@@ -200,11 +196,11 @@ pub mod v5_storage_items {
200196
}
201197

202198
pub mod v6 {
203-
use super::*;
204-
use crate::{
205-
storage_migrations::v5_storage_items::{OldMigration, OldProjectStatus, MAX_PARTICIPATIONS_PER_USER},
206-
EvaluationRoundInfo, ProjectDetailsOf, ProjectStatus, TicketSize,
199+
use super::{
200+
v5_storage_items::{OldMigration, OldProjectStatus, MAX_PARTICIPATIONS_PER_USER},
201+
*,
207202
};
203+
use crate::{EvaluationRoundInfo, ProjectDetailsOf, ProjectStatus, TicketSize};
208204
use frame_system::pallet_prelude::BlockNumberFor;
209205
use polimec_common::{
210206
credentials::Did,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
// storage_migrations.rs
2+
3+
use crate::Config;
4+
use alloc::vec::Vec;
5+
use frame_support::{pallet_prelude::*, traits::UncheckedOnRuntimeUpgrade, weights::Weight};
6+
use polimec_common::migration_types::{Migration, MigrationInfo, MigrationStatus};
7+
use sp_runtime::{traits::ConstU32, WeakBoundedVec};
8+
9+
pub mod v7 {
10+
use crate::{AccountIdOf, ProjectId, UserMigrations};
11+
12+
use super::*;
13+
14+
const LOG: &str = "funding::migration::v7";
15+
pub const MAX_PARTICIPATIONS_PER_USER: u32 = 10_000;
16+
17+
type UserMigrationsKey<T> = (ProjectId, AccountIdOf<T>);
18+
type UserMigrationsValue = (MigrationStatus, WeakBoundedVec<Migration, ConstU32<MAX_PARTICIPATIONS_PER_USER>>);
19+
20+
pub struct UncheckedMigrationToV7<T: Config>(PhantomData<T>);
21+
impl<T: Config> UncheckedOnRuntimeUpgrade for UncheckedMigrationToV7<T> {
22+
#[cfg(feature = "try-runtime")]
23+
fn pre_upgrade() -> Result<Vec<u8>, DispatchError> {
24+
let migration_count = UserMigrations::<T>::iter().count() as u32;
25+
log::info!(target: LOG, "Pre-upgrade: {} UserMigrations entries", migration_count);
26+
27+
// Encode counts and sample pre-migration vesting times for validation
28+
let user_migrations = UserMigrations::<T>::iter().collect::<Vec<_>>();
29+
30+
Ok((migration_count, user_migrations).encode())
31+
}
32+
33+
fn on_runtime_upgrade() -> Weight {
34+
let mut items = 0u64;
35+
log::info!(target: LOG, "Starting vesting time migration to V7");
36+
37+
let translate_migrations =
38+
|_: UserMigrationsKey<T>, migration_value: UserMigrationsValue| -> Option<UserMigrationsValue> {
39+
let (status, migrations) = migration_value;
40+
let migrated: Vec<_> = migrations
41+
.iter()
42+
.map(|migration| {
43+
items = items.saturating_add(1);
44+
45+
Migration {
46+
info: MigrationInfo {
47+
vesting_time: migration.info.vesting_time.saturating_div(2),
48+
..migration.info
49+
},
50+
..migration.clone()
51+
}
52+
})
53+
.collect();
54+
55+
let bounded = WeakBoundedVec::try_from(migrated)
56+
.unwrap_or_else(|_| panic!("MaxParticipationsPerUser exceeded during migration"));
57+
58+
Some((status, bounded))
59+
};
60+
61+
UserMigrations::<T>::translate(translate_migrations);
62+
63+
log::info!(target: LOG, "Migrated {} vesting entries", items);
64+
T::DbWeight::get().reads_writes(items, items)
65+
}
66+
67+
#[cfg(feature = "try-runtime")]
68+
fn post_upgrade(pre_state: Vec<u8>) -> Result<(), DispatchError> {
69+
let (pre_migration_count, pre_user_migrations): (u32, Vec<(UserMigrationsKey<T>, UserMigrationsValue)>) =
70+
Decode::decode(&mut &pre_state[..]).expect("Failed to decode pre-migration state");
71+
72+
let post_migration_count = UserMigrations::<T>::iter().count() as u32;
73+
74+
if pre_migration_count != post_migration_count {
75+
return Err("Migration count mismatch".into());
76+
}
77+
78+
for (key, pre_value) in pre_user_migrations {
79+
let post_value = UserMigrations::<T>::get(key.clone())
80+
.unwrap_or_else(|| panic!("Post-migration UserMigrations entry not found for {:?}", key));
81+
82+
for (pre_migration, post_migration) in pre_value.1.iter().zip(post_value.1.iter()) {
83+
if pre_migration.info.vesting_time.saturating_div(2) != post_migration.info.vesting_time {
84+
return Err("Migration vesting time mismatch".into());
85+
}
86+
}
87+
}
88+
89+
Ok(())
90+
}
91+
}
92+
93+
pub type MigrationToV8<T> = frame_support::migrations::VersionedMigration<
94+
6,
95+
7,
96+
UncheckedMigrationToV7<T>,
97+
crate::Pallet<T>,
98+
<T as frame_system::Config>::DbWeight,
99+
>;
100+
}
101+
102+
#[cfg(test)]
103+
mod tests {
104+
use super::*;
105+
use crate::{
106+
mock::{new_test_ext, TestRuntime as Test},
107+
UserMigrations,
108+
};
109+
use cumulus_primitives_core::Junction;
110+
use polimec_common::migration_types::MigrationOrigin;
111+
use v7::{UncheckedMigrationToV7, MAX_PARTICIPATIONS_PER_USER};
112+
113+
#[test]
114+
fn migration_to_v7() {
115+
let mut ext = new_test_ext();
116+
ext.execute_with(|| {
117+
assert_eq!(UserMigrations::<Test>::iter().count(), 0);
118+
119+
let mut migrations = Vec::new();
120+
for i in 0..MAX_PARTICIPATIONS_PER_USER {
121+
migrations.push(Migration {
122+
info: MigrationInfo { vesting_time: i as u64, contribution_token_amount: 1_u128 },
123+
origin: MigrationOrigin {
124+
user: Junction::OnlyChild,
125+
participation_type: polimec_common::migration_types::ParticipationType::Bid,
126+
},
127+
});
128+
}
129+
130+
let bounded_migrations = WeakBoundedVec::try_from(migrations).unwrap();
131+
132+
UserMigrations::<Test>::insert((1, 1), (MigrationStatus::Confirmed, bounded_migrations));
133+
134+
assert_eq!(UserMigrations::<Test>::iter().count(), 1);
135+
136+
let weight = UncheckedMigrationToV7::<Test>::on_runtime_upgrade();
137+
assert_eq!(UserMigrations::<Test>::iter().count(), 1);
138+
139+
for (_, (_, migrations)) in UserMigrations::<Test>::iter() {
140+
for (i, migration) in migrations.iter().enumerate() {
141+
assert_eq!(migration.info.vesting_time, i as u64 / 2);
142+
}
143+
}
144+
145+
assert!(weight.is_zero());
146+
});
147+
}
148+
}

runtimes/polimec/src/lib.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,10 @@ pub mod migrations {
188188
#[allow(unused_parens)]
189189
pub type Unreleased = (
190190
super::custom_migrations::asset_id_migration::FromOldAssetIdMigration,
191-
super::custom_migrations::linear_release::LinearReleaseVestingMigration,
192-
pallet_funding::storage_migrations::v6::MigrationToV6<Runtime>,
191+
// super::custom_migrations::linear_release::LinearReleaseVestingMigration,
192+
// pallet_funding::migrations::storage_migrations::v6::MigrationToV6<Runtime>,
193193
RemovePallet<IdentityPalletName, RuntimeDbWeight>,
194+
pallet_funding::migrations::vesting_info::v7::MigrationToV8<Runtime>,
194195
);
195196
}
196197

0 commit comments

Comments
 (0)