1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Copyright 2019-2024 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

use std::sync::Arc;

use cid::Cid;
use fvm_ipld_blockstore::Blockstore;

use super::{ActorMigration, ActorMigrationInput, ActorMigrationOutput};

/// Migrator which preserves the head CID and provides a fixed result code CID.
/// This is used to migrate actors which do not require any state migration.
pub(in crate::state_migration) struct NilMigrator(Cid);

impl<BS: Blockstore> ActorMigration<BS> for NilMigrator {
    fn migrate_state(
        &self,
        _store: &BS,
        input: ActorMigrationInput,
    ) -> anyhow::Result<Option<ActorMigrationOutput>> {
        Ok(Some(ActorMigrationOutput {
            new_code_cid: self.0,
            new_head: input.head,
        }))
    }
}

/// Creates a new migrator which preserves the head CID and provides a fixed
/// result code CID.
pub(in crate::state_migration) fn nil_migrator<BS: Blockstore>(
    cid: Cid,
) -> Arc<dyn ActorMigration<BS> + Send + Sync> {
    Arc::new(NilMigrator(cid))
}

/// A migrator that does nothing but delegates the explicit migration logic to post migrator(s)
pub(in crate::state_migration) struct DeferredMigrator;

impl<BS: Blockstore> ActorMigration<BS> for DeferredMigrator {
    fn migrate_state(
        &self,
        _store: &BS,
        _input: ActorMigrationInput,
    ) -> anyhow::Result<Option<ActorMigrationOutput>> {
        Ok(None)
    }
}