zebra_state/service/finalized_state/disk_format/upgrade/
tree_keys_and_caches_upgrade.rs

1//! Applies the [`fix_tree_key_type`] and [`cache_genesis_roots`] upgrades to the database.
2
3use crossbeam_channel::Receiver;
4
5use semver::Version;
6use zebra_chain::block::Height;
7
8use crate::service::finalized_state::ZebraDb;
9
10use super::{cache_genesis_roots, fix_tree_key_type, CancelFormatChange, DiskFormatUpgrade};
11
12/// Implements [`DiskFormatUpgrade`] for updating the sprout and history tree key type from
13/// `Height` to the empty key `()` and the genesis note commitment trees to cache their roots
14pub struct FixTreeKeyTypeAndCacheGenesisRoots;
15
16impl DiskFormatUpgrade for FixTreeKeyTypeAndCacheGenesisRoots {
17    fn version(&self) -> Version {
18        Version::new(25, 3, 0)
19    }
20
21    fn description(&self) -> &'static str {
22        "tree keys and caches upgrade"
23    }
24
25    #[allow(clippy::unwrap_in_result)]
26    fn run(
27        &self,
28        initial_tip_height: Height,
29        db: &ZebraDb,
30        cancel_receiver: &Receiver<CancelFormatChange>,
31    ) -> Result<(), CancelFormatChange> {
32        // It shouldn't matter what order these are run in.
33        cache_genesis_roots::run(initial_tip_height, db, cancel_receiver)?;
34        fix_tree_key_type::run(initial_tip_height, db, cancel_receiver)?;
35        Ok(())
36    }
37
38    #[allow(clippy::unwrap_in_result)]
39    fn validate(
40        &self,
41        db: &ZebraDb,
42        cancel_receiver: &Receiver<CancelFormatChange>,
43    ) -> Result<Result<(), String>, CancelFormatChange> {
44        let results = [
45            cache_genesis_roots::detailed_check(db, cancel_receiver)?,
46            fix_tree_key_type::detailed_check(db, cancel_receiver)?,
47        ];
48
49        let result = if results.iter().any(Result::is_err) {
50            Err(format!("{results:?}"))
51        } else {
52            Ok(())
53        };
54
55        Ok(result)
56    }
57}