Skip to content

Commit 95dd98e

Browse files
committed
Refactor migration logic to ensure correct versioning and improve weight handling during account migration steps. Updated function names for clarity and adjusted weight consumption checks.
1 parent 044727a commit 95dd98e

File tree

1 file changed

+35
-22
lines changed
  • substrate/frame/indices/src/migration/v1

1 file changed

+35
-22
lines changed

substrate/frame/indices/src/migration/v1/mod.rs

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,17 @@ extern crate alloc;
7878
use super::PALLET_MIGRATIONS_ID;
7979
use crate::{
8080
pallet::{Accounts, Config, HoldReason},
81-
BalanceOf,
81+
weights::WeightInfo,
82+
BalanceOf, Pallet,
8283
};
8384

8485
use frame_support::{
8586
migrations::{MigrationId, SteppedMigration, SteppedMigrationError},
86-
pallet_prelude::PhantomData,
87+
pallet_prelude::*,
8788
traits::{
8889
fungible::{Inspect, MutateHold},
8990
tokens::{Fortitude, Preservation},
90-
Currency, Get, ReservableCurrency,
91+
Currency, ReservableCurrency,
9192
},
9293
weights::WeightMeter,
9394
};
@@ -148,32 +149,44 @@ where
148149
mut cursor: Option<Self::Cursor>,
149150
meter: &mut WeightMeter,
150151
) -> Result<Option<Self::Cursor>, SteppedMigrationError> {
152+
//making sure we are migrating the correct version
153+
if Pallet::<T>::on_chain_storage_version() != Self::id().version_from as u16 {
154+
return Ok(None);
155+
}
156+
151157
// Check if we have minimal weight to proceed
152158
// We need at least enough weight to migrate one account to make progress
153-
let min_required = T::DbWeight::get().reads(1); //T::WeightInfo::migrate_account_step();
159+
let min_required = T::WeightInfo::migrate_account_step();
154160

155161
if meter.remaining().any_lt(min_required) {
156162
return Err(SteppedMigrationError::InsufficientWeight { required: min_required });
157163
}
158164

159-
// Get the iterator for the OLD accounts to migrate
160-
let mut iter = if let Some(Some(last_key)) = cursor {
161-
v0::OldAccounts::<T>::iter_from(v0::OldAccounts::<T>::hashed_key_for(last_key))
162-
} else {
163-
v0::OldAccounts::<T>::iter()
164-
};
165-
166-
// If there is a next item in the iterator, perform the migration.
167-
if let Some((index, (account, old_deposit, frozen))) = iter.next() {
168-
Self::migrate_account(account, index, frozen, old_deposit);
169-
cursor = Some(Some(index));
170-
} else {
171-
// Migration complete
172-
println!("Migration completed - no more accounts to migrate");
173-
return Ok(None);
174-
}
165+
loop {
166+
// Check that we would have enough weight to perform this step
167+
if !meter.can_consume(min_required) {
168+
break;
169+
}
175170

176-
meter.consume(min_required);
171+
// Get the iterator for the OLD accounts to migrate
172+
let mut iter = if let Some(Some(last_key)) = cursor {
173+
v0::OldAccounts::<T>::iter_from(v0::OldAccounts::<T>::hashed_key_for(last_key))
174+
} else {
175+
v0::OldAccounts::<T>::iter()
176+
};
177+
178+
// If there is a next item in the iterator, perform the migration.
179+
if let Some((index, (account, old_deposit, frozen))) = iter.next() {
180+
Self::migrate_account_step(account, index, frozen, old_deposit);
181+
cursor = Some(Some(index));
182+
meter.consume(min_required);
183+
} else {
184+
// Migration complete
185+
println!("Migration completed - no more accounts to migrate");
186+
StorageVersion::new(Self::id().version_to as u16).put::<Pallet<T>>();
187+
return Ok(None);
188+
}
189+
}
177190

178191
Ok(cursor)
179192
}
@@ -261,7 +274,7 @@ impl<T: Config, OldCurrency> MigrateCurrencyToFungibles<T, OldCurrency>
261274
where
262275
OldCurrency: Currency<T::AccountId, Balance = BalanceOf<T>> + ReservableCurrency<T::AccountId>,
263276
{
264-
fn migrate_account(
277+
fn migrate_account_step(
265278
account: T::AccountId,
266279
index: T::AccountIndex,
267280
frozen: bool,

0 commit comments

Comments
 (0)