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