Skip to main content

forest/state_migration/nv23/
migration.rs

1// Copyright 2019-2026 ChainSafe Systems
2// SPDX-License-Identifier: Apache-2.0, MIT
3//
4//! This module contains the migration logic for the `NV23` upgrade.
5
6use std::sync::Arc;
7
8use crate::networks::{ChainConfig, Height};
9use crate::shim::{
10    address::Address,
11    clock::ChainEpoch,
12    machine::{BuiltinActor, BuiltinActorManifest},
13    state_tree::{StateTree, StateTreeVersion},
14};
15use crate::utils::db::CborStoreExt as _;
16use anyhow::Context as _;
17use cid::Cid;
18
19use fvm_ipld_blockstore::Blockstore;
20
21use super::mining_reserve::MiningReservePostMigrator;
22use super::{SystemStateOld, system, verifier::Verifier};
23use crate::state_migration::common::{StateMigration, migrators::nil_migrator};
24
25impl<BS: Blockstore> StateMigration<BS> {
26    pub fn add_nv23_migrations(
27        &mut self,
28        store: &Arc<BS>,
29        state: &Cid,
30        new_manifest: &BuiltinActorManifest,
31        _chain_config: &ChainConfig,
32    ) -> anyhow::Result<()> {
33        let state_tree = StateTree::new_from_root(store.clone(), state)?;
34        let system_actor = state_tree.get_required_actor(&Address::SYSTEM_ACTOR)?;
35        let system_actor_state = store.get_cbor_required::<SystemStateOld>(&system_actor.state)?;
36
37        let current_manifest_data = system_actor_state.builtin_actors;
38
39        let current_manifest =
40            BuiltinActorManifest::load_v1_actor_list(store, &current_manifest_data)?;
41
42        for (name, code) in current_manifest.builtin_actors() {
43            let new_code = new_manifest.get(name)?;
44            self.add_migrator(code, nil_migrator(new_code))
45        }
46
47        self.add_migrator(
48            current_manifest.get_system(),
49            system::system_migrator(new_manifest),
50        );
51
52        self.add_post_migrator(Arc::new(MiningReservePostMigrator {
53            new_account_code_cid: new_manifest.get(BuiltinActor::Account)?,
54            new_multisig_code_cid: new_manifest.get(BuiltinActor::Multisig)?,
55        }));
56
57        Ok(())
58    }
59}
60
61/// Runs the migration for `NV23`. Returns the new state root.
62pub fn run_migration<DB>(
63    chain_config: &ChainConfig,
64    blockstore: &Arc<DB>,
65    state: &Cid,
66    epoch: ChainEpoch,
67) -> anyhow::Result<Cid>
68where
69    DB: Blockstore + Send + Sync,
70{
71    let new_manifest_cid = chain_config
72        .height_infos
73        .get(&Height::Waffle)
74        .context("no height info for network version NV23")?
75        .bundle
76        .as_ref()
77        .context("no bundle for network version NV23")?;
78
79    blockstore.get(new_manifest_cid)?.context(format!(
80        "manifest for network version NV23 not found in blockstore: {new_manifest_cid}"
81    ))?;
82
83    // Add migration specification verification
84    let verifier = Arc::new(Verifier::default());
85
86    let new_manifest = BuiltinActorManifest::load_manifest(blockstore, new_manifest_cid)?;
87    let mut migration = StateMigration::<DB>::new(Some(verifier));
88    migration.add_nv23_migrations(blockstore, state, &new_manifest, chain_config)?;
89
90    let actors_in = StateTree::new_from_root(blockstore.clone(), state)?;
91    let actors_out = StateTree::new(blockstore.clone(), StateTreeVersion::V5)?;
92    let new_state = migration.migrate_state_tree(blockstore, epoch, actors_in, actors_out)?;
93
94    Ok(new_state)
95}