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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Copyright 2019-2024 ChainSafe Systems
// SPDX-License-Identifier: Apache-2.0, MIT

//! Migration logic from version 0.12.1
//! This is more of an exercise and a PoC than a real migration, the reason being that the
//! database directories before the Forest version 0.12.1 were not yet versioned and so we can only guess the version
//! of their underlying databases.
//! All in all, it gives us a good idea of how to do a migration and solves potential caveats
//! coming from the rolling database and ParityDb.

use crate::db::migration::migration_map::temporary_db_name;
use crate::Config;
use fs_extra::dir::CopyOptions;
use semver::Version;
use std::path::{Path, PathBuf};
use tracing::info;

use super::migration_map::MigrationOperation;

pub(super) struct Migration0_12_1_0_13_0 {
    from: Version,
    to: Version,
}

/// Migrates the database from version 0.12.1 to 0.13.0
/// This migration is needed because the `Settings` column in the `ParityDb` table changed to
/// binary-tree indexed and the data from `HEAD` and `meta.yaml` files was moved to the `Settings` column.
impl MigrationOperation for Migration0_12_1_0_13_0 {
    fn pre_checks(&self, _chain_data_path: &Path) -> anyhow::Result<()> {
        Ok(())
    }

    fn migrate(&self, chain_data_path: &Path, _config: &Config) -> anyhow::Result<PathBuf> {
        let source_db = chain_data_path.join(self.from.to_string());

        let temp_db_path = chain_data_path.join(temporary_db_name(&self.from, &self.to));
        if temp_db_path.exists() {
            info!(
                "removing old temporary database {temp_db_path}",
                temp_db_path = temp_db_path.display()
            );
            std::fs::remove_dir_all(&temp_db_path)?;
        }

        // copy the old database to a new directory
        info!(
            "copying old database from {source_db} to {temp_db_path}",
            source_db = source_db.display(),
            temp_db_path = temp_db_path.display()
        );
        fs_extra::copy_items(
            &[source_db.as_path()],
            &temp_db_path,
            &CopyOptions::default().copy_inside(true),
        )?;

        // there are two YAML files that are supposed to be in the `Settings` column now.
        // We need to add them manually to the database.
        let head_file_path = chain_data_path.join("HEAD");
        let head = std::fs::read(&head_file_path)?;

        // The HEAD type was kept in binary format, so we need to convert it to JSON.
        let head: Vec<cid::Cid> = fvm_ipld_encoding::from_slice(&head)?;
        let head = serde_json::to_vec(&head)?;

        // Estimated records were kept in a YAML file...
        let estimated_records_path = chain_data_path.join("meta.yaml");
        let estimated_records = std::fs::read_to_string(&estimated_records_path)?;
        let estimated_records = estimated_records
            .split_once(':')
            .ok_or_else(|| {
                anyhow::anyhow!(
                    "meta.yaml file is not in the expected format, expected `key: value`"
                )
            })
            .map(|(_, value)| value.trim())?
            .parse::<u64>()?;
        let estimated_records = serde_json::to_vec(&estimated_records)?;

        // because of the rolling db, we have to do the migration for each sub-database...
        for sub_db in temp_db_path
            .read_dir()?
            .filter_map(|entry| Some(entry.ok()?.path()))
            .filter(|entry| entry.is_dir())
        {
            let db = paritydb_0_12_1::ParityDb::open(&sub_db)?;

            // The `Settings` column is now binary-tree indexed, there is only one entry in this version
            // that needs to be migrated.
            // It is very unlikely this entry was changed, but we treat it as the first migration proof of
            // concept.
            let mpool_config = db
                .db
                .get(paritydb_0_12_1::DbColumn::Settings as u8, b"/mpool/config")?;

            drop(db);

            let mut db_config = paritydb_0_12_1::ParityDb::to_options(sub_db.clone());
            let settings_column_config_new = parity_db::ColumnOptions {
                preimage: false,
                btree_index: true,
                compression: parity_db::CompressionType::Lz4,
                ..Default::default()
            };
            parity_db::Db::reset_column(
                &mut db_config,
                paritydb_0_12_1::DbColumn::Settings as u8,
                Some(settings_column_config_new),
            )?;

            let db = paritydb_0_13_0::ParityDb::open(&sub_db)?;

            let tx = [(
                paritydb_0_13_0::DbColumn::Settings as u8,
                b"/mpool/config",
                mpool_config,
            )];
            db.db.commit(tx)?;

            let tx = [(
                paritydb_0_13_0::DbColumn::Settings as u8,
                b"head",
                Some(head.clone()),
            )];
            db.db.commit(tx)?;

            let tx = [(
                paritydb_0_13_0::DbColumn::Settings as u8,
                b"estimated_reachable_records",
                Some(estimated_records.clone()),
            )];
            db.db.commit(tx)?;
        }

        std::fs::remove_file(head_file_path)?;
        std::fs::remove_file(estimated_records_path)?;

        Ok(temp_db_path)
    }

    fn post_checks(&self, chain_data_path: &Path) -> anyhow::Result<()> {
        let temp_db_name = temporary_db_name(&self.from, &self.to);
        if !chain_data_path.join(&temp_db_name).exists() {
            anyhow::bail!(
                "migration database {} does not exist",
                chain_data_path.join(temp_db_name).display()
            );
        }
        Ok(())
    }

    fn new(from: Version, to: Version) -> Self
    where
        Self: Sized,
    {
        Self { from, to }
    }
}

/// Database settings from Forest `v0.12.1`
mod paritydb_0_12_1 {
    use parity_db::{CompressionType, Db, Options};
    use std::path::PathBuf;
    use strum::{Display, EnumIter, IntoEnumIterator};

    #[derive(Copy, Clone, Debug, PartialEq, EnumIter, Display)]
    #[repr(u8)]
    pub(super) enum DbColumn {
        GraphDagCborBlake2b256,
        GraphFull,
        Settings,
    }

    impl DbColumn {
        fn create_column_options(compression: CompressionType) -> Vec<parity_db::ColumnOptions> {
            DbColumn::iter()
                .map(|col| {
                    match col {
                        DbColumn::GraphDagCborBlake2b256 | DbColumn::Settings => {
                            parity_db::ColumnOptions {
                                preimage: true,
                                compression,
                                ..Default::default()
                            }
                        }
                        DbColumn::GraphFull => parity_db::ColumnOptions {
                            preimage: true,
                            // This is needed for key retrieval.
                            btree_index: true,
                            compression,
                            ..Default::default()
                        },
                    }
                })
                .collect()
        }
    }

    pub(super) struct ParityDb {
        pub db: parity_db::Db,
    }

    impl ParityDb {
        pub(super) fn to_options(path: PathBuf) -> Options {
            Options {
                path,
                sync_wal: true,
                sync_data: true,
                stats: false,
                salt: None,
                columns: DbColumn::create_column_options(CompressionType::Lz4),
                compression_threshold: [(0, 128)].into_iter().collect(),
            }
        }

        pub(super) fn open(path: impl Into<PathBuf>) -> anyhow::Result<Self> {
            let opts = Self::to_options(path.into());
            Ok(Self {
                db: Db::open_or_create(&opts)?,
            })
        }
    }
}

/// Database settings from Forest `v0.13.0`
mod paritydb_0_13_0 {

    use parity_db::{CompressionType, Db, Options};
    use std::path::PathBuf;
    use strum::{Display, EnumIter, IntoEnumIterator};

    #[derive(Copy, Clone, Debug, PartialEq, EnumIter, Display)]
    #[repr(u8)]
    pub(super) enum DbColumn {
        GraphDagCborBlake2b256,
        GraphFull,
        Settings,
    }

    impl DbColumn {
        fn create_column_options(compression: CompressionType) -> Vec<parity_db::ColumnOptions> {
            DbColumn::iter()
                .map(|col| {
                    match col {
                        DbColumn::GraphDagCborBlake2b256 => parity_db::ColumnOptions {
                            preimage: true,
                            compression,
                            ..Default::default()
                        },
                        DbColumn::GraphFull => parity_db::ColumnOptions {
                            preimage: true,
                            // This is needed for key retrieval.
                            btree_index: true,
                            compression,
                            ..Default::default()
                        },
                        DbColumn::Settings => parity_db::ColumnOptions {
                            // explicitly disable preimage for settings column
                            // othewise we are not able to overwrite entries
                            preimage: false,
                            // This is needed for key retrieval.
                            btree_index: true,
                            compression,
                            ..Default::default()
                        },
                    }
                })
                .collect()
        }
    }

    pub(super) struct ParityDb {
        pub db: parity_db::Db,
    }

    impl ParityDb {
        pub(super) fn to_options(path: PathBuf) -> Options {
            Options {
                path,
                sync_wal: true,
                sync_data: true,
                stats: false,
                salt: None,
                columns: DbColumn::create_column_options(CompressionType::Lz4),
                compression_threshold: [(0, 128)].into_iter().collect(),
            }
        }

        pub(super) fn open(path: impl Into<PathBuf>) -> anyhow::Result<Self> {
            let opts = Self::to_options(path.into());
            Ok(Self {
                db: Db::open_or_create(&opts)?,
            })
        }
    }
}