tycho_core/storage/
db.rs

1use tycho_storage::kv::{
2    Migrations, NamedTables, StateVersionProvider, TableContext, WithMigrations,
3};
4use tycho_util::sync::CancellationFlag;
5use weedb::{MigrationError, Semver, VersionProvider, WeeDb};
6
7use super::tables;
8
9pub type CoreDb = WeeDb<CoreTables>;
10pub type CellsDb = WeeDb<CellsTables>;
11
12pub trait CoreDbExt {
13    fn normalize_version(&self) -> anyhow::Result<()>;
14}
15
16impl CoreDbExt for CoreDb {
17    // TEMP: Set a proper version on start. Remove on testnet reset.
18    fn normalize_version(&self) -> anyhow::Result<()> {
19        let provider = CoreTables::new_version_provider();
20
21        // Check if there is NO VERSION
22        if provider.get_version(self.raw())?.is_some() {
23            return Ok(());
24        }
25
26        // Check if the DB is NOT EMPTY
27        {
28            let mut block_handles_iter = self.block_handles.raw_iterator();
29            block_handles_iter.seek_to_first();
30            block_handles_iter.status()?;
31            if block_handles_iter.item().is_none() {
32                return Ok(());
33            }
34        }
35
36        // Set the initial version
37        tracing::warn!("normalizing DB version for core");
38        provider.set_version(self.raw(), [0, 0, 1])?;
39        Ok(())
40    }
41}
42
43impl CoreDbExt for CellsDb {
44    fn normalize_version(&self) -> anyhow::Result<()> {
45        let provider = CellsTables::new_version_provider();
46
47        // Check if there is NO VERSION
48        if provider.get_version(self.raw())?.is_some() {
49            return Ok(());
50        }
51
52        // Check if the DB is NOT EMPTY
53        {
54            let mut cells_iter = self.cells.raw_iterator();
55            cells_iter.seek_to_first();
56            cells_iter.status()?;
57            if cells_iter.item().is_none() {
58                return Ok(());
59            }
60        }
61
62        // Set the initial version
63        tracing::warn!("normalizing DB version for cells");
64        provider.set_version(self.raw(), [0, 0, 1])?;
65        Ok(())
66    }
67}
68
69impl NamedTables for CoreTables {
70    const NAME: &'static str = "core";
71}
72
73impl WithMigrations for CoreTables {
74    const VERSION: Semver = [0, 0, 4];
75
76    type VersionProvider = StateVersionProvider<tables::State>;
77
78    fn new_version_provider() -> Self::VersionProvider {
79        StateVersionProvider::new::<Self>()
80    }
81
82    fn register_migrations(
83        _migrations: &mut Migrations<Self::VersionProvider, Self>,
84        _cancelled: CancellationFlag,
85    ) -> Result<(), MigrationError> {
86        Ok(())
87    }
88}
89
90weedb::tables! {
91    pub struct CoreTables<TableContext> {
92        pub state: tables::State,
93        pub archive_block_ids: tables::ArchiveBlockIds,
94        pub archive_events: tables::ArchiveEvents,
95        pub block_handles: tables::BlockHandles,
96        pub key_blocks: tables::KeyBlocks,
97        pub full_block_ids: tables::FullBlockIds,
98        pub block_connections: tables::BlockConnections,
99    }
100}
101
102weedb::tables! {
103    pub struct CellsTables<TableContext> {
104        pub state: tables::State,
105
106        pub shard_states: tables::ShardStates,
107        pub cells: tables::Cells,
108        pub temp_cells: tables::TempCells,
109    }
110}
111
112impl NamedTables for CellsTables {
113    const NAME: &'static str = "cells";
114}
115
116impl WithMigrations for CellsTables {
117    const VERSION: Semver = [0, 0, 1];
118
119    type VersionProvider = StateVersionProvider<tables::State>;
120
121    fn new_version_provider() -> Self::VersionProvider {
122        StateVersionProvider::new::<Self>()
123    }
124
125    fn register_migrations(
126        _migrations: &mut Migrations<Self::VersionProvider, Self>,
127        _cancelled: CancellationFlag,
128    ) -> Result<(), MigrationError> {
129        Ok(())
130    }
131}