Skip to main content

forest/state_migration/nv21/
migration.rs

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