Skip to main content

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