Skip to main content

forest/state_migration/nv28/
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 `NV28` upgrade.
5
6use std::sync::Arc;
7
8use crate::networks::{ChainConfig, Height};
9use crate::shim::{
10    address::Address,
11    clock::ChainEpoch,
12    machine::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::{SystemStateOld, system, verifier::Verifier};
22use crate::state_migration::common::{StateMigration, migrators::nil_migrator};
23
24impl<BS: Blockstore> StateMigration<BS> {
25    pub fn add_nv28_migrations(
26        &mut self,
27        store: &Arc<BS>,
28        state: &Cid,
29        new_manifest: &BuiltinActorManifest,
30    ) -> anyhow::Result<()> {
31        let state_tree = StateTree::new_from_root(store.clone(), state)?;
32        let system_actor = state_tree.get_required_actor(&Address::SYSTEM_ACTOR)?;
33        let system_actor_state = store.get_cbor_required::<SystemStateOld>(&system_actor.state)?;
34
35        let current_manifest_data = system_actor_state.builtin_actors;
36
37        let current_manifest =
38            BuiltinActorManifest::load_v1_actor_list(store, &current_manifest_data)?;
39
40        for (name, code) in current_manifest.builtin_actors() {
41            let new_code = new_manifest.get(name)?;
42            self.add_migrator(code, nil_migrator(new_code))
43        }
44
45        self.add_migrator(
46            current_manifest.get_system(),
47            system::system_migrator(new_manifest),
48        );
49
50        Ok(())
51    }
52}
53
54/// Runs the migration for `NV28`. Returns the new state root.
55pub fn run_migration<DB>(
56    chain_config: &ChainConfig,
57    blockstore: &Arc<DB>,
58    state: &Cid,
59    epoch: ChainEpoch,
60) -> anyhow::Result<Cid>
61where
62    DB: Blockstore + Send + Sync,
63{
64    let new_manifest_cid = chain_config
65        .height_infos
66        .get(&Height::FireHorse)
67        .context("no height info for network version NV28")?
68        .bundle
69        .as_ref()
70        .context("no bundle for network version NV28")?;
71
72    blockstore.get(new_manifest_cid)?.context(format!(
73        "manifest for network version NV28 not found in blockstore: {new_manifest_cid}"
74    ))?;
75
76    // Add migration specification verification
77    let verifier = Arc::new(Verifier::default());
78
79    let new_manifest = BuiltinActorManifest::load_manifest(blockstore, new_manifest_cid)?;
80    let mut migration = StateMigration::<DB>::new(Some(verifier));
81    migration.add_nv28_migrations(blockstore, state, &new_manifest)?;
82
83    let actors_in = StateTree::new_from_root(blockstore.clone(), state)?;
84    let actors_out = StateTree::new(blockstore.clone(), StateTreeVersion::V5)?;
85    let new_state = migration.migrate_state_tree(blockstore, epoch, actors_in, actors_out)?;
86
87    Ok(new_state)
88}