|
17 | 17 | // along with Astar. If not, see <http://www.gnu.org/licenses/>.
|
18 | 18 |
|
19 | 19 | use super::*;
|
20 |
| -use frame_support::{storage_alias, traits::UncheckedOnRuntimeUpgrade}; |
| 20 | +use frame_support::{ |
| 21 | + migrations::{MigrationId, SteppedMigration, SteppedMigrationError}, |
| 22 | + storage_alias, |
| 23 | + traits::UncheckedOnRuntimeUpgrade, |
| 24 | + weights::WeightMeter, |
| 25 | +}; |
21 | 26 |
|
22 | 27 | #[cfg(feature = "try-runtime")]
|
23 | 28 | use sp_std::vec::Vec;
|
@@ -412,3 +417,80 @@ pub mod v6 {
|
412 | 417 | pub period: PeriodNumber,
|
413 | 418 | }
|
414 | 419 | }
|
| 420 | + |
| 421 | +const PALLET_MIGRATIONS_ID: &[u8; 16] = b"dapp-staking-mbm"; |
| 422 | + |
| 423 | +pub struct LazyMigration<T, W: WeightInfo>(PhantomData<(T, W)>); |
| 424 | + |
| 425 | +impl<T: Config, W: WeightInfo> SteppedMigration for LazyMigration<T, W> { |
| 426 | + type Cursor = <T as frame_system::Config>::AccountId; |
| 427 | + // Without the explicit length here the construction of the ID would not be infallible. |
| 428 | + type Identifier = MigrationId<16>; |
| 429 | + |
| 430 | + /// The identifier of this migration. Which should be globally unique. |
| 431 | + fn id() -> Self::Identifier { |
| 432 | + MigrationId { |
| 433 | + pallet_id: *PALLET_MIGRATIONS_ID, |
| 434 | + version_from: 0, |
| 435 | + version_to: 1, |
| 436 | + } |
| 437 | + } |
| 438 | + |
| 439 | + fn step( |
| 440 | + mut cursor: Option<Self::Cursor>, |
| 441 | + meter: &mut WeightMeter, |
| 442 | + ) -> Result<Option<Self::Cursor>, SteppedMigrationError> { |
| 443 | + let required = W::step(); |
| 444 | + // If there is not enough weight for a single step, return an error. This case can be |
| 445 | + // problematic if it is the first migration that ran in this block. But there is nothing |
| 446 | + // that we can do about it here. |
| 447 | + if meter.remaining().any_lt(required) { |
| 448 | + return Err(SteppedMigrationError::InsufficientWeight { required }); |
| 449 | + } |
| 450 | + |
| 451 | + let mut count = 0u32; |
| 452 | + let current_block_number = |
| 453 | + frame_system::Pallet::<T>::block_number().saturated_into::<u32>(); |
| 454 | + |
| 455 | + // We loop here to do as much progress as possible per step. |
| 456 | + loop { |
| 457 | + if meter.try_consume(required).is_err() { |
| 458 | + break; |
| 459 | + } |
| 460 | + |
| 461 | + let mut iter = if let Some(last_key) = cursor { |
| 462 | + // If a cursor is provided, start iterating from the stored value |
| 463 | + // corresponding to the last key processed in the previous step. |
| 464 | + // Note that this only works if the old and the new map use the same way to hash |
| 465 | + // storage keys. |
| 466 | + Ledger::<T>::iter_from(Ledger::<T>::hashed_key_for(last_key)) |
| 467 | + } else { |
| 468 | + // If no cursor is provided, start iterating from the beginning. |
| 469 | + Ledger::<T>::iter() |
| 470 | + }; |
| 471 | + |
| 472 | + // If there's a next item in the iterator, perform the migration. |
| 473 | + if let Some((ref last_key, mut ledger)) = iter.next() { |
| 474 | + for chunk in ledger.unlocking.iter_mut() { |
| 475 | + let remaining_blocks = chunk.unlock_block.saturating_sub(current_block_number); |
| 476 | + chunk.unlock_block.saturating_accrue(remaining_blocks); |
| 477 | + } |
| 478 | + |
| 479 | + // Override ledger |
| 480 | + Ledger::<T>::insert(last_key, ledger); |
| 481 | + |
| 482 | + // inc counter |
| 483 | + count.saturating_inc(); |
| 484 | + |
| 485 | + // Return the processed key as the new cursor. |
| 486 | + cursor = Some(last_key.clone()) |
| 487 | + } else { |
| 488 | + // Signal that the migration is complete (no more items to process). |
| 489 | + cursor = None; |
| 490 | + break; |
| 491 | + } |
| 492 | + } |
| 493 | + log::debug!(target: LOG_TARGET, "migrated {count:?} entries"); |
| 494 | + Ok(cursor) |
| 495 | + } |
| 496 | +} |
0 commit comments