HackMD
  • Prime
    Prime  Full-text search on all paid plans
    Search anywhere and reach everything in a Workspace with Prime plan.
    Got it
      • Create new note
      • Create a note from template
    • Prime  Full-text search on all paid plans
      Prime  Full-text search on all paid plans
      Search anywhere and reach everything in a Workspace with Prime plan.
      Got it
      • Sharing Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • More (Comment, Invitee)
      • Publishing
        Everyone on the web can find and read all notes of this public team.
        After the note is published, everyone on the web can find and read this note.
        See all published notes on profile page.
      • Commenting Enable
        Disabled Forbidden Owners Signed-in users Everyone
      • Permission
        • Forbidden
        • Owners
        • Signed-in users
        • Everyone
      • Invitee
      • No invitee
      • Options
      • Versions and GitHub Sync
      • Transfer ownership
      • Delete this note
      • Template
      • Save as template
      • Insert from template
      • Export
      • Dropbox
      • Google Drive
      • Gist
      • Import
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
      • Download
      • Markdown
      • HTML
      • Raw HTML
    Menu Sharing Create Help
    Create Create new note Create a note from template
    Menu
    Options
    Versions and GitHub Sync Transfer ownership Delete this note
    Export
    Dropbox Google Drive Gist
    Import
    Dropbox Google Drive Gist Clipboard
    Download
    Markdown HTML Raw HTML
    Back
    Sharing
    Sharing Link copied
    /edit
    View mode
    • Edit mode
    • View mode
    • Book mode
    • Slide mode
    Edit mode View mode Book mode Slide mode
    Note Permission
    Read
    Only me
    • Only me
    • Signed-in users
    • Everyone
    Only me Signed-in users Everyone
    Write
    Only me
    • Only me
    • Signed-in users
    • Everyone
    Only me Signed-in users Everyone
    More (Comment, Invitee)
    Publishing
    Everyone on the web can find and read all notes of this public team.
    After the note is published, everyone on the web can find and read this note.
    See all published notes on profile page.
    More (Comment, Invitee)
    Commenting Enable
    Disabled Forbidden Owners Signed-in users Everyone
    Permission
    Owners
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Invitee
    No invitee
       owned this note    owned this note      
    Published Linked with GitHub
    Like2 BookmarkBookmarked
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Substrate Runtime Migration Guide :::warning Living document evolving over time. ::: When you want to change the logic of a running Substrate blockchain you will need to execute a [forkless runtime upgrade](https://substrate.dev/docs/en/tutorials/upgrade-a-chain/). This upgrade will change the logic of your chain, but the logic depends on the state following a certain schema. As a result you will need to include storage migrations that transform the state from the previous into the current schema. We call this transition a runtime storage migration. This guide aims to give some help with and context for Substrate runtime storage migrations. Any runtime migration project can be divided into six phases: 1. **Preparation** 2. **Writing Migrations** 3. **Creating the Runtime Upgrade** 4. **Testing** 5. **Execution** 6. **Removal** We will go through the phases in order and point out relevant considerations and resources. ## 1. Preparation Before actually migrating you need to figure out which migrations to include in your runtime upgrade. Depending on which version you are migrating from (and to) you will need to decide whether to include these migrations. Note that you will also need to determine whether and how the migrations apply to your particular chain as well as their order. All Substrate migration PRs since the start of 2020 are tagged with `D1-runtime-migration` [in this PR listing here](https://github.com/paritytech/substrate/pulls?q=is%3Apr+label%3AD1-runtime-migration). You will need to port these to your chain which might include making adjustments. :::info Referencing the Edgeware migration here as it might be helpful because includes quite a few migrations. [Tracking issue](https://github.com/hicommonwealth/edgeware-node/issues/164). ::: If you need to determine how the runtime has changed between versions you can diff the metadata. You can use `docker run --network=host jacogr/polkadot-js-tools metadata <ws-old> <ws-new>` (e.g. `docker run --network=host jacogr/polkadot-js-tools metadata wss://rpc.polkadot.io ws://localhost:9944` to compare a local instance with the Polkadot production chain) to determine which modules have changes. #### The Hasher Migration If you are migrating from an earlier version of Substrate (`v2.0.0-alpha.3` or earlier; before March 2020) you will need to pay attention to a notable migration. *The Hasher Migration* was a complicated migration that migrated away from opaque hashers (see [the PR here](https://github.com/paritytech/substrate/pull/5226)). **Note**: There were issues in the original hasher migration so don't copy it directly and read the comments left on the PR. If you want to use a similar migration strategy to the one originally chosen for Kusama you will have to provide a [SCALE](https://github.com/paritytech/parity-scale-codec) encoded file of accounts to migrate. ## 2. Writing the Migration If you are a pallet author or you are not just running migrations already included in the pallets you are using you will have to write your own migrations. When writing new migrations keep the following in mind: + Make sure to include storage version checks so that your migration only runs when migrating from the correct version and only executes once. + See [this recent phragmen migration](https://github.com/paritytech/substrate/pull/7040/files#diff-194656be9ce9133b248a5b7cd18842eb5d3faacde3f1221b32e654419870136aR129) for how to do this manually. + See the [Pallet Storage Version PR](https://github.com/paritytech/substrate/pull/7208) for how we want to do these version check in the future. + You are "looking at" the data from the "perspective" of the new runtime. You need to provide the correct (potentially deprecated) types to decode the data and migrate it into the current format. + See [this migration of indices](https://github.com/hicommonwealth/substrate/pull/14/files#diff-00c91017af35e93cc641a7bc0d73202f7bd349f13d5bc7f0736d7a272919195dR5-R22) for an example. + Include logging, including whether the migration ran and whether there were any errors. It will help with manual integration testing. + Prefer making migrations reusable by e.g. extracting them out into functions. This will allow easier testing and will potentially allow others to use them. + See [this example](https://github.com/paritytech/substrate/pull/7040/files#diff-194656be9ce9133b248a5b7cd18842eb5d3faacde3f1221b32e654419870136aR128) (same phragmen migration as above). + Add a unit test if your migration includes any logic ## 3. Creating the Runtime Upgrade When you have identified which migrations to include you need to actually create the runtime upgrade that will apply them. This means choosing how the migrations are applied and in which order. The default of applying the migrations one pallet after the other will work for the common case but might not be suitable for more complicated situations. When wiring the migrations into the runtime you can place them in a custom order by using the custom runtime upgrade facilities. See [here](https://github.com/hicommonwealth/edgeware-node/blob/7b66f4f0a9ec184fdebcccd41533acc728ebe9dc/node/runtime/src/lib.rs#L845-L866) for an example. #### Note for Custom Migration Orders Note the execution order (as defined in the code [here](https://github.com/paritytech/substrate/blob/d766e229466d63afadd19097e277d85146fee3c9/frame/executive/src/lib.rs#L231-L257)): 1. `frame_system::on_runtime_upgrade` 2. Custom `on_runtime_upgrade` 3. All pallet `on_runtime_upgrade`s in reverse order of declaration (see [this tracking issue](https://github.com/paritytech/substrate/issues/6280)). 4. `frame_system::initialize` 5. `frame_system::on_initialize` 6. All pallet `on_initialize`s in reverse order of declaration (see [this tracking issue](https://github.com/paritytech/substrate/issues/6280)). This determines which state is migrated and initialized when. It means e.g. that you cannot access the block number in a migration because it is set in `frame_system::initialize`. ## 4. Testing Runtime storage migrations should be tested in order to reduce the risk of a runtime migration mangling the storage. ### Unit Tests You can run unit tests on generated as well as unit-like tests on live data. 1. Populate the storage of a test externalities environment. 2. Run the migration function. 3. Assert the correct shape of the new data. #### Populate Storage This PR shows how to populate the storage from raw data [here](https://github.com/paritytech/substrate/pull/4474/files#diff-64c049b4e94a55bdeaf757c725cf7d1df623b754557af4ced157024a943c4703R3076) and [here](https://github.com/paritytech/substrate/pull/4474/files#diff-9c165a4d56d77f122b251056c560ece123c5c944d949299b99614d0d17b36f4dR17). Check out [`remote-externalities`](https://github.com/paritytech/substrate-debug-kit/tree/master/remote-externalities) if you want to use live chain data for your migration tests. ### Manual Integration Testing You can fork off from the live chain to then run the migration locally on live chain data but with a different chain spec. [The `fork-off-substrate` tool](https://github.com/maxsam4/fork-off-substrate) is aimed at making this easier. ## 5. Execution Sometimes a new runtime will make use of new host functions which need to be supported by the nodes. You will want to make sure any node upgrades required for the runtime upgrade have high enough prevalence in your network before executing it. ## 6. Removal As migrations are one-off pieces of code they can be removed after successful execution. The recommendation would be to leave the stand-alone migration function around for a while, but remove the usage in `on_runtime_upgrade` in time for the next runtime upgrade. ## Appendix We are coordinating improvements to the migration experience in [this Github issue](https://github.com/paritytech/substrate/issues/6482). ### Vision for Migrations We want storage migrations to be: - **Correct:** Migrations should leave the chain in a correct state after the migration. - **Testable:** It should be possible to determine whether they run correctly. - **Composable:** Running several migrations one after another should not corrupt state. - **Discoverable:** Migrations should be discoverable when users want to apply them or use them as inspiration for their own migration. - **Documented:** Migrations should list assumptions so that users can verify whether they will work for their chain. - **Reusable:** Migrations (especially simple ones) should be reusable so they can be applied to many chains and don't need to be rewritten. - **Easy and Automatic:** Simple migrations should just work™ without a lot of manual integration.

    Import from clipboard

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lost their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template is not available.


    Upgrade

    All
    • All
    • Team
    No template found.

    Create custom template


    Upgrade

    Delete template

    Do you really want to delete this template?

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Tutorials

    Book Mode Tutorial

    Slide Mode Tutorial

    YAML Metadata

    Contacts

    Facebook

    Twitter

    Feedback

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions

    Versions and GitHub Sync

    Sign in to link this note to GitHub Learn more
    This note is not linked with GitHub Learn more
     
    Add badge Pull Push GitHub Link Settings
    Upgrade now

    Version named by    

    More Less
    • Edit
    • Delete

    Note content is identical to the latest version.
    Compare with
      Choose a version
      No search result
      Version not found

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub

        Please sign in to GitHub and install the HackMD app on your GitHub repo. Learn more

         Sign in to GitHub

        HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Available push count

        Upgrade

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Upgrade

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully