8

If I want to migrate the Balance::Locks to a new structure. Imagine there are millions of accounts in the storage.

This is what I came out. Is this the right way? Split them into multiple blocks. If yes, how can I record the pos?

storage OldLocks
storage NewLocks
storage LastPos

fn on_new_block() {
    LastPos::put(migrate_from(LastPos::get()));
}

// Aka the locks' `getter` during the migration
fn lock_of(account) -> Amount {
    let mut locks = [];

    if let Some(lock) = NewLocks::get(account) {
        locks.append(lock);
    }
    if let Some(lock) = OldLocks::get(account) {
        locks.append(lock);
    }

    return locks.cal_total_lock_amount();
}

Furthermore, what if I want to keep using the old name? I don't want to break the API. move_prefix(b"Locks", b"OldLocks") first, does this take too much weights?

1

2 Answers 2

12

This is basically the correct approach.

There isn't actually any need to record the position: you should be removing items from OldLocks and inserting equivalents into NewLocks. You can stop the migration once OldLocks is empty. As long as you can guarantee that migration will remove at least one item from OldLocks and that your runtime never introduces any items into OldLocks then this will complete in a finite time.

See the pallet-xcm in Polkadot for an example on multi-block migrations.

3
  • What if I want to keep using the old name? I don't want to break the API. move_prefix(b"Locks", b"OldLocks") Does this works?
    – AurevoirXavier
    Mar 29, 2022 at 7:17
  • I think so, move_prefix is O(1) operation, try it(Before launch, test it locally and in your own testnet). Mar 30, 2022 at 7:12
  • 1
    From looking at the code of move_prefix; it is at least O(n). AFAIK It could be implemented in O(1), but currently there is no host function for that. In any case, it is better to benchmark migrations - especially for parachains. Jun 17, 2023 at 15:00
1

After 2 years. We have a built-in multi-block migration method now!

Check https://github.com/paritytech/polkadot-sdk/pull/1781 for more detail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.