diff --git a/storage/src/storable_cell/cells/meta_indexer.rs b/storage/src/indexer/indexer_cells.rs similarity index 50% rename from storage/src/storable_cell/cells/meta_indexer.rs rename to storage/src/indexer/indexer_cells.rs index 7322ba8f..144f56b9 100644 --- a/storage/src/storable_cell/cells/meta_indexer.rs +++ b/storage/src/indexer/indexer_cells.rs @@ -2,42 +2,29 @@ use borsh::{BorshDeserialize, BorshSerialize}; use nssa::V03State; use crate::{ - CF_ACC_META, CF_BREAKPOINT_NAME, CF_HASH_TO_ID, CF_META_NAME, CF_TX_TO_ID, - DB_META_LAST_BREAKPOINT_ID, DB_META_LAST_OBSERVED_L1_LIB_HEADER_ID_IN_DB_KEY, DbResult, + CF_META_NAME, DbResult, error::DbError, + indexer::{ + ACC_NUM_CELL_NAME, BLOCK_HASH_CELL_NAME, BREAKPOINT_CELL_NAME, CF_ACC_META, + CF_BREAKPOINT_NAME, CF_HASH_TO_ID, CF_TX_TO_ID, DB_META_LAST_BREAKPOINT_ID, + DB_META_LAST_OBSERVED_L1_LIB_HEADER_ID_IN_DB_KEY, TX_HASH_CELL_NAME, + }, storable_cell::{SimpleReadableCell, SimpleStorableCell, SimpleWritableCell}, }; -#[derive(Debug)] +#[derive(Debug, BorshSerialize, BorshDeserialize)] pub struct LastObservedL1LibHeaderCell(pub [u8; 32]); -impl BorshSerialize for LastObservedL1LibHeaderCell { - fn serialize(&self, writer: &mut W) -> std::io::Result<()> { - <[u8; 32]>::serialize(&self.0, writer) - } -} - -impl BorshDeserialize for LastObservedL1LibHeaderCell { - fn deserialize_reader(reader: &mut R) -> std::io::Result { - <[u8; 32]>::deserialize_reader(reader).map(LastObservedL1LibHeaderCell) - } -} - impl SimpleStorableCell for LastObservedL1LibHeaderCell { type KeyParams = (); const CELL_NAME: &'static str = DB_META_LAST_OBSERVED_L1_LIB_HEADER_ID_IN_DB_KEY; const CF_NAME: &'static str = CF_META_NAME; +} - fn key_constructor(_params: Self::KeyParams) -> DbResult> { - borsh::to_vec(&Self::CELL_NAME).map_err(|err| { - DbError::borsh_cast_message( - err, - Some(format!("Failed to serialize {:?}", Self::CELL_NAME)), - ) - }) - } +impl SimpleReadableCell for LastObservedL1LibHeaderCell {} +impl SimpleWritableCell for LastObservedL1LibHeaderCell { fn value_constructor(&self) -> DbResult> { borsh::to_vec(&self).map_err(|err| { DbError::borsh_cast_message( @@ -48,40 +35,19 @@ impl SimpleStorableCell for LastObservedL1LibHeaderCell { } } -impl SimpleReadableCell for LastObservedL1LibHeaderCell {} - -impl SimpleWritableCell for LastObservedL1LibHeaderCell {} - -#[derive(Debug)] +#[derive(Debug, BorshSerialize, BorshDeserialize)] pub struct LastBreakpointIdCell(pub u64); -impl BorshSerialize for LastBreakpointIdCell { - fn serialize(&self, writer: &mut W) -> std::io::Result<()> { - u64::serialize(&self.0, writer) - } -} - -impl BorshDeserialize for LastBreakpointIdCell { - fn deserialize_reader(reader: &mut R) -> std::io::Result { - u64::deserialize_reader(reader).map(LastBreakpointIdCell) - } -} - impl SimpleStorableCell for LastBreakpointIdCell { type KeyParams = (); const CELL_NAME: &'static str = DB_META_LAST_BREAKPOINT_ID; const CF_NAME: &'static str = CF_META_NAME; +} - fn key_constructor(_params: Self::KeyParams) -> DbResult> { - borsh::to_vec(&Self::CELL_NAME).map_err(|err| { - DbError::borsh_cast_message( - err, - Some(format!("Failed to serialize {:?}", Self::CELL_NAME)), - ) - }) - } +impl SimpleReadableCell for LastBreakpointIdCell {} +impl SimpleWritableCell for LastBreakpointIdCell { fn value_constructor(&self) -> DbResult> { borsh::to_vec(&self).map_err(|err| { DbError::borsh_cast_message( @@ -92,65 +58,53 @@ impl SimpleStorableCell for LastBreakpointIdCell { } } -impl SimpleReadableCell for LastBreakpointIdCell {} - -impl SimpleWritableCell for LastBreakpointIdCell {} - +#[derive(BorshDeserialize)] pub struct BreakpointCellOwned(pub V03State); -impl BorshDeserialize for BreakpointCellOwned { - fn deserialize_reader(reader: &mut R) -> std::io::Result { - V03State::deserialize_reader(reader).map(BreakpointCellOwned) - } -} - impl SimpleStorableCell for BreakpointCellOwned { type KeyParams = u64; - const CELL_NAME: &'static str = "breakpoint"; + const CELL_NAME: &'static str = BREAKPOINT_CELL_NAME; const CF_NAME: &'static str = CF_BREAKPOINT_NAME; fn key_constructor(params: Self::KeyParams) -> DbResult> { borsh::to_vec(¶ms).map_err(|err| { DbError::borsh_cast_message( err, - Some(format!("Failed to serialize {:?}", Self::CELL_NAME)), + Some(format!( + "Failed to serialize {:?} key params", + Self::CELL_NAME + )), ) }) } - - fn value_constructor(&self) -> DbResult> { - borsh::to_vec(&self.0).map_err(|err| { - DbError::borsh_cast_message(err, Some("Failed to serialize breakpoint".to_owned())) - }) - } } impl SimpleReadableCell for BreakpointCellOwned {} +#[derive(BorshSerialize)] pub struct BreakpointCellRef<'state>(pub &'state V03State); -impl BorshSerialize for BreakpointCellRef<'_> { - fn serialize(&self, writer: &mut W) -> std::io::Result<()> { - V03State::serialize(self.0, writer) - } -} - impl SimpleStorableCell for BreakpointCellRef<'_> { type KeyParams = u64; - const CELL_NAME: &'static str = "breakpoint"; + const CELL_NAME: &'static str = BREAKPOINT_CELL_NAME; const CF_NAME: &'static str = CF_BREAKPOINT_NAME; fn key_constructor(params: Self::KeyParams) -> DbResult> { borsh::to_vec(¶ms).map_err(|err| { DbError::borsh_cast_message( err, - Some(format!("Failed to serialize {:?}", Self::CELL_NAME)), + Some(format!( + "Failed to serialize {:?} key params", + Self::CELL_NAME + )), ) }) } +} +impl SimpleWritableCell for BreakpointCellRef<'_> { fn value_constructor(&self) -> DbResult> { borsh::to_vec(&self).map_err(|err| { DbError::borsh_cast_message(err, Some("Failed to serialize breakpoint".to_owned())) @@ -158,79 +112,31 @@ impl SimpleStorableCell for BreakpointCellRef<'_> { } } -impl SimpleWritableCell for BreakpointCellRef<'_> {} - -#[derive(Debug)] +#[derive(Debug, BorshSerialize, BorshDeserialize)] pub struct BlockHashToBlockIdMapCell(pub u64); -impl BorshSerialize for BlockHashToBlockIdMapCell { - fn serialize(&self, writer: &mut W) -> std::io::Result<()> { - u64::serialize(&self.0, writer) - } -} - -impl BorshDeserialize for BlockHashToBlockIdMapCell { - fn deserialize_reader(reader: &mut R) -> std::io::Result { - u64::deserialize_reader(reader).map(BlockHashToBlockIdMapCell) - } -} - impl SimpleStorableCell for BlockHashToBlockIdMapCell { type KeyParams = [u8; 32]; - const CELL_NAME: &'static str = "block hash"; + const CELL_NAME: &'static str = BLOCK_HASH_CELL_NAME; const CF_NAME: &'static str = CF_HASH_TO_ID; fn key_constructor(params: Self::KeyParams) -> DbResult> { borsh::to_vec(¶ms).map_err(|err| { DbError::borsh_cast_message( err, - Some(format!("Failed to serialize {:?}", Self::CELL_NAME)), + Some(format!( + "Failed to serialize {:?} key params", + Self::CELL_NAME + )), ) }) } - - fn value_constructor(&self) -> DbResult> { - borsh::to_vec(&self).map_err(|err| { - DbError::borsh_cast_message(err, Some("Failed to serialize block id".to_owned())) - }) - } } impl SimpleReadableCell for BlockHashToBlockIdMapCell {} -impl SimpleWritableCell for BlockHashToBlockIdMapCell {} - -#[derive(Debug)] -pub struct TxHashToBlockIdMapCell(pub u64); - -impl BorshSerialize for TxHashToBlockIdMapCell { - fn serialize(&self, writer: &mut W) -> std::io::Result<()> { - u64::serialize(&self.0, writer) - } -} - -impl BorshDeserialize for TxHashToBlockIdMapCell { - fn deserialize_reader(reader: &mut R) -> std::io::Result { - u64::deserialize_reader(reader).map(TxHashToBlockIdMapCell) - } -} - -impl SimpleStorableCell for TxHashToBlockIdMapCell { - type KeyParams = [u8; 32]; - - const CELL_NAME: &'static str = "tx hash"; - const CF_NAME: &'static str = CF_TX_TO_ID; - - fn key_constructor(params: Self::KeyParams) -> DbResult> { - borsh::to_vec(¶ms).map_err(|err| { - DbError::borsh_cast_message( - err, - Some(format!("Failed to serialize {:?}", Self::CELL_NAME)), - ) - }) - } - +impl SimpleWritableCell for BlockHashToBlockIdMapCell { fn value_constructor(&self) -> DbResult> { borsh::to_vec(&self).map_err(|err| { DbError::borsh_cast_message(err, Some("Failed to serialize block id".to_owned())) @@ -238,40 +144,63 @@ impl SimpleStorableCell for TxHashToBlockIdMapCell { } } +#[derive(Debug, BorshSerialize, BorshDeserialize)] +pub struct TxHashToBlockIdMapCell(pub u64); + +impl SimpleStorableCell for TxHashToBlockIdMapCell { + type KeyParams = [u8; 32]; + + const CELL_NAME: &'static str = TX_HASH_CELL_NAME; + const CF_NAME: &'static str = CF_TX_TO_ID; + + fn key_constructor(params: Self::KeyParams) -> DbResult> { + borsh::to_vec(¶ms).map_err(|err| { + DbError::borsh_cast_message( + err, + Some(format!( + "Failed to serialize {:?} key params", + Self::CELL_NAME + )), + ) + }) + } +} + impl SimpleReadableCell for TxHashToBlockIdMapCell {} -impl SimpleWritableCell for TxHashToBlockIdMapCell {} +impl SimpleWritableCell for TxHashToBlockIdMapCell { + fn value_constructor(&self) -> DbResult> { + borsh::to_vec(&self).map_err(|err| { + DbError::borsh_cast_message(err, Some("Failed to serialize block id".to_owned())) + }) + } +} -#[derive(Debug)] +#[derive(Debug, BorshSerialize, BorshDeserialize)] pub struct AccNumTxCell(pub u64); -impl BorshSerialize for AccNumTxCell { - fn serialize(&self, writer: &mut W) -> std::io::Result<()> { - u64::serialize(&self.0, writer) - } -} - -impl BorshDeserialize for AccNumTxCell { - fn deserialize_reader(reader: &mut R) -> std::io::Result { - u64::deserialize_reader(reader).map(AccNumTxCell) - } -} - impl SimpleStorableCell for AccNumTxCell { type KeyParams = [u8; 32]; - const CELL_NAME: &'static str = "acc id"; + const CELL_NAME: &'static str = ACC_NUM_CELL_NAME; const CF_NAME: &'static str = CF_ACC_META; fn key_constructor(params: Self::KeyParams) -> DbResult> { borsh::to_vec(¶ms).map_err(|err| { DbError::borsh_cast_message( err, - Some(format!("Failed to serialize {:?}", Self::CELL_NAME)), + Some(format!( + "Failed to serialize {:?} key params", + Self::CELL_NAME + )), ) }) } +} +impl SimpleReadableCell for AccNumTxCell {} + +impl SimpleWritableCell for AccNumTxCell { fn value_constructor(&self) -> DbResult> { borsh::to_vec(&self).map_err(|err| { DbError::borsh_cast_message( @@ -281,7 +210,3 @@ impl SimpleStorableCell for AccNumTxCell { }) } } - -impl SimpleReadableCell for AccNumTxCell {} - -impl SimpleWritableCell for AccNumTxCell {} diff --git a/storage/src/indexer/mod.rs b/storage/src/indexer/mod.rs index f91c40d8..7d4bce09 100644 --- a/storage/src/indexer/mod.rs +++ b/storage/src/indexer/mod.rs @@ -7,17 +7,43 @@ use rocksdb::{ }; use crate::{ - BREAKPOINT_INTERVAL, CF_ACC_META, CF_ACC_TO_TX, CF_BLOCK_NAME, CF_BREAKPOINT_NAME, - CF_HASH_TO_ID, CF_META_NAME, CF_TX_TO_ID, DbResult, + BREAKPOINT_INTERVAL, CF_BLOCK_NAME, CF_META_NAME, DbResult, error::DbError, storable_cell::{SimpleReadableCell, SimpleWritableCell}, }; +pub mod indexer_cells; pub mod read_multiple; pub mod read_once; pub mod write_atomic; pub mod write_non_atomic; +/// Key base for storing metainformation about id of last observed L1 lib header in db. +pub const DB_META_LAST_OBSERVED_L1_LIB_HEADER_ID_IN_DB_KEY: &str = + "last_observed_l1_lib_header_in_db"; +/// Key base for storing metainformation about the last breakpoint. +pub const DB_META_LAST_BREAKPOINT_ID: &str = "last_breakpoint_id"; + +/// Cell name for a breakpoint. +pub const BREAKPOINT_CELL_NAME: &str = "breakpoint"; +/// Cell name for a block hash to block id map. +pub const BLOCK_HASH_CELL_NAME: &str = "block hash"; +/// Cell name for a tx hash to block id map. +pub const TX_HASH_CELL_NAME: &str = "tx hash"; +/// Cell name for a account number of transactions. +pub const ACC_NUM_CELL_NAME: &str = "acc id"; + +/// Name of breakpoint column family. +pub const CF_BREAKPOINT_NAME: &str = "cf_breakpoint"; +/// Name of hash to id map column family. +pub const CF_HASH_TO_ID: &str = "cf_hash_to_id"; +/// Name of tx hash to id map column family. +pub const CF_TX_TO_ID: &str = "cf_tx_to_id"; +/// Name of account meta column family. +pub const CF_ACC_META: &str = "cf_acc_meta"; +/// Name of account id to tx hash map column family. +pub const CF_ACC_TO_TX: &str = "cf_acc_to_tx"; + pub struct RocksDBIO { pub db: DBWithThreadMode, } diff --git a/storage/src/indexer/read_once.rs b/storage/src/indexer/read_once.rs index a7cedd58..a652b8e9 100644 --- a/storage/src/indexer/read_once.rs +++ b/storage/src/indexer/read_once.rs @@ -1,10 +1,12 @@ use super::{Block, DbResult, RocksDBIO, V03State}; -use crate::storable_cell::cells::{ - meta_indexer::{ +use crate::{ + indexer::indexer_cells::{ AccNumTxCell, BlockHashToBlockIdMapCell, BreakpointCellOwned, LastBreakpointIdCell, LastObservedL1LibHeaderCell, TxHashToBlockIdMapCell, }, - meta_shared::{BlockCell, FirstBlockCell, FirstBlockSetCell, LastBlockCell}, + storable_cell::cells::shared_cells::{ + BlockCell, FirstBlockCell, FirstBlockSetCell, LastBlockCell, + }, }; #[expect(clippy::multiple_inherent_impl, reason = "Readability")] diff --git a/storage/src/indexer/write_atomic.rs b/storage/src/indexer/write_atomic.rs index cff8284a..e1145131 100644 --- a/storage/src/indexer/write_atomic.rs +++ b/storage/src/indexer/write_atomic.rs @@ -5,13 +5,11 @@ use rocksdb::WriteBatch; use super::{BREAKPOINT_INTERVAL, Block, DbError, DbResult, RocksDBIO}; use crate::{ DB_META_FIRST_BLOCK_IN_DB_KEY, - storable_cell::cells::{ - meta_indexer::{ - AccNumTxCell, BlockHashToBlockIdMapCell, LastBreakpointIdCell, - LastObservedL1LibHeaderCell, TxHashToBlockIdMapCell, - }, - meta_shared::{FirstBlockSetCell, LastBlockCell}, + indexer::indexer_cells::{ + AccNumTxCell, BlockHashToBlockIdMapCell, LastBreakpointIdCell, LastObservedL1LibHeaderCell, + TxHashToBlockIdMapCell, }, + storable_cell::cells::shared_cells::{FirstBlockSetCell, LastBlockCell}, }; #[expect(clippy::multiple_inherent_impl, reason = "Readability")] diff --git a/storage/src/indexer/write_non_atomic.rs b/storage/src/indexer/write_non_atomic.rs index 96d133e4..d5a862f4 100644 --- a/storage/src/indexer/write_non_atomic.rs +++ b/storage/src/indexer/write_non_atomic.rs @@ -1,7 +1,9 @@ use super::{BREAKPOINT_INTERVAL, DbError, DbResult, RocksDBIO, V03State}; -use crate::storable_cell::cells::{ - meta_indexer::{BreakpointCellRef, LastBreakpointIdCell, LastObservedL1LibHeaderCell}, - meta_shared::{FirstBlockSetCell, LastBlockCell}, +use crate::{ + indexer::indexer_cells::{ + BreakpointCellRef, LastBreakpointIdCell, LastObservedL1LibHeaderCell, + }, + storable_cell::cells::shared_cells::{FirstBlockSetCell, LastBlockCell}, }; #[expect(clippy::multiple_inherent_impl, reason = "Readability")] diff --git a/storage/src/lib.rs b/storage/src/lib.rs index 9aa17356..5d9f8705 100644 --- a/storage/src/lib.rs +++ b/storage/src/lib.rs @@ -5,8 +5,6 @@ pub mod indexer; pub mod sequencer; pub mod storable_cell; -// General - /// Maximal size of stored blocks in base. /// /// Used to control db size. @@ -26,6 +24,9 @@ pub const DB_META_FIRST_BLOCK_IN_DB_KEY: &str = "first_block_in_db"; /// Key base for storing metainformation about id of last current block in db. pub const DB_META_LAST_BLOCK_IN_DB_KEY: &str = "last_block_in_db"; +/// Cell name for a block. +pub const BLOCK_CELL_NAME: &str = "block"; + /// Interval between state breakpoints. pub const BREAKPOINT_INTERVAL: u8 = 100; @@ -34,36 +35,4 @@ pub const CF_BLOCK_NAME: &str = "cf_block"; /// Name of meta column family. pub const CF_META_NAME: &str = "cf_meta"; -// Indexer-specific - -/// Key base for storing metainformation about id of last observed L1 lib header in db. -pub const DB_META_LAST_OBSERVED_L1_LIB_HEADER_ID_IN_DB_KEY: &str = - "last_observed_l1_lib_header_in_db"; -/// Key base for storing metainformation about the last breakpoint. -pub const DB_META_LAST_BREAKPOINT_ID: &str = "last_breakpoint_id"; - -/// Name of breakpoint column family. -pub const CF_BREAKPOINT_NAME: &str = "cf_breakpoint"; -/// Name of hash to id map column family. -pub const CF_HASH_TO_ID: &str = "cf_hash_to_id"; -/// Name of tx hash to id map column family. -pub const CF_TX_TO_ID: &str = "cf_tx_to_id"; -/// Name of account meta column family. -pub const CF_ACC_META: &str = "cf_acc_meta"; -/// Name of account id to tx hash map column family. -pub const CF_ACC_TO_TX: &str = "cf_acc_to_tx"; - -// Sequencer-specific - -/// Key base for storing metainformation about the last finalized block on Bedrock. -pub const DB_META_LAST_FINALIZED_BLOCK_ID: &str = "last_finalized_block_id"; -/// Key base for storing metainformation about the latest block meta. -pub const DB_META_LATEST_BLOCK_META_KEY: &str = "latest_block_meta"; - -/// Key base for storing the NSSA state. -pub const DB_NSSA_STATE_KEY: &str = "nssa_state"; - -/// Name of state column family. -pub const CF_NSSA_STATE_NAME: &str = "cf_nssa_state"; - pub type DbResult = Result; diff --git a/storage/src/sequencer.rs b/storage/src/sequencer/mod.rs similarity index 93% rename from storage/src/sequencer.rs rename to storage/src/sequencer/mod.rs index c3424fb7..fcd81702 100644 --- a/storage/src/sequencer.rs +++ b/storage/src/sequencer/mod.rs @@ -7,21 +7,30 @@ use rocksdb::{ }; use crate::{ - CF_BLOCK_NAME, CF_META_NAME, CF_NSSA_STATE_NAME, DB_META_FIRST_BLOCK_IN_DB_KEY, + CF_BLOCK_NAME, CF_META_NAME, DB_META_FIRST_BLOCK_IN_DB_KEY, DbResult, error::DbError, + sequencer::sequencer_cells::{ + LastFinalizedBlockIdCell, LatestBlockMetaCellOwned, LatestBlockMetaCellRef, + NSSAStateCellOwned, NSSAStateCellRef, + }, storable_cell::{ SimpleReadableCell, SimpleWritableCell, - cells::{ - meta_sequencer::{ - LastFinalizedBlockIdCell, LatestBlockMetaCellOwned, LatestBlockMetaCellRef, - NSSAStateCellOwned, NSSAStateCellRef, - }, - meta_shared::{BlockCell, FirstBlockCell, FirstBlockSetCell, LastBlockCell}, - }, + cells::shared_cells::{BlockCell, FirstBlockCell, FirstBlockSetCell, LastBlockCell}, }, }; -pub type DbResult = Result; +pub mod sequencer_cells; + +/// Key base for storing metainformation about the last finalized block on Bedrock. +pub const DB_META_LAST_FINALIZED_BLOCK_ID: &str = "last_finalized_block_id"; +/// Key base for storing metainformation about the latest block meta. +pub const DB_META_LATEST_BLOCK_META_KEY: &str = "latest_block_meta"; + +/// Key base for storing the NSSA state. +pub const DB_NSSA_STATE_KEY: &str = "nssa_state"; + +/// Name of state column family. +pub const CF_NSSA_STATE_NAME: &str = "cf_nssa_state"; pub struct RocksDBIO { pub db: DBWithThreadMode, diff --git a/storage/src/sequencer/sequencer_cells.rs b/storage/src/sequencer/sequencer_cells.rs new file mode 100644 index 00000000..7fa0b48a --- /dev/null +++ b/storage/src/sequencer/sequencer_cells.rs @@ -0,0 +1,96 @@ +use borsh::{BorshDeserialize, BorshSerialize}; +use common::block::BlockMeta; +use nssa::V03State; + +use crate::{ + CF_META_NAME, DbResult, + error::DbError, + sequencer::{ + CF_NSSA_STATE_NAME, DB_META_LAST_FINALIZED_BLOCK_ID, DB_META_LATEST_BLOCK_META_KEY, + DB_NSSA_STATE_KEY, + }, + storable_cell::{SimpleReadableCell, SimpleStorableCell, SimpleWritableCell}, +}; + +#[derive(BorshDeserialize)] +pub struct NSSAStateCellOwned(pub V03State); + +impl SimpleStorableCell for NSSAStateCellOwned { + type KeyParams = (); + + const CELL_NAME: &'static str = DB_NSSA_STATE_KEY; + const CF_NAME: &'static str = CF_NSSA_STATE_NAME; +} + +impl SimpleReadableCell for NSSAStateCellOwned {} + +#[derive(BorshSerialize)] +pub struct NSSAStateCellRef<'state>(pub &'state V03State); + +impl SimpleStorableCell for NSSAStateCellRef<'_> { + type KeyParams = (); + + const CELL_NAME: &'static str = DB_NSSA_STATE_KEY; + const CF_NAME: &'static str = CF_NSSA_STATE_NAME; +} + +impl SimpleWritableCell for NSSAStateCellRef<'_> { + fn value_constructor(&self) -> DbResult> { + borsh::to_vec(&self).map_err(|err| { + DbError::borsh_cast_message(err, Some("Failed to serialize last state".to_owned())) + }) + } +} + +#[derive(Debug, BorshSerialize, BorshDeserialize)] +pub struct LastFinalizedBlockIdCell(pub Option); + +impl SimpleStorableCell for LastFinalizedBlockIdCell { + type KeyParams = (); + + const CELL_NAME: &'static str = DB_META_LAST_FINALIZED_BLOCK_ID; + const CF_NAME: &'static str = CF_META_NAME; +} + +impl SimpleReadableCell for LastFinalizedBlockIdCell {} + +impl SimpleWritableCell for LastFinalizedBlockIdCell { + fn value_constructor(&self) -> DbResult> { + borsh::to_vec(&self).map_err(|err| { + DbError::borsh_cast_message( + err, + Some("Failed to serialize last finalized block id".to_owned()), + ) + }) + } +} + +#[derive(BorshDeserialize)] +pub struct LatestBlockMetaCellOwned(pub BlockMeta); + +impl SimpleStorableCell for LatestBlockMetaCellOwned { + type KeyParams = (); + + const CELL_NAME: &'static str = DB_META_LATEST_BLOCK_META_KEY; + const CF_NAME: &'static str = CF_META_NAME; +} + +impl SimpleReadableCell for LatestBlockMetaCellOwned {} + +#[derive(BorshSerialize)] +pub struct LatestBlockMetaCellRef<'blockmeta>(pub &'blockmeta BlockMeta); + +impl SimpleStorableCell for LatestBlockMetaCellRef<'_> { + type KeyParams = (); + + const CELL_NAME: &'static str = DB_META_LATEST_BLOCK_META_KEY; + const CF_NAME: &'static str = CF_META_NAME; +} + +impl SimpleWritableCell for LatestBlockMetaCellRef<'_> { + fn value_constructor(&self) -> DbResult> { + borsh::to_vec(&self).map_err(|err| { + DbError::borsh_cast_message(err, Some("Failed to serialize last block meta".to_owned())) + }) + } +} diff --git a/storage/src/storable_cell/cells/meta_sequencer.rs b/storage/src/storable_cell/cells/meta_sequencer.rs deleted file mode 100644 index 9c564fd5..00000000 --- a/storage/src/storable_cell/cells/meta_sequencer.rs +++ /dev/null @@ -1,182 +0,0 @@ -use borsh::{BorshDeserialize, BorshSerialize}; -use common::block::BlockMeta; -use nssa::V03State; - -use crate::{ - CF_META_NAME, CF_NSSA_STATE_NAME, DB_META_LAST_FINALIZED_BLOCK_ID, - DB_META_LATEST_BLOCK_META_KEY, DB_NSSA_STATE_KEY, DbResult, - error::DbError, - storable_cell::{SimpleReadableCell, SimpleStorableCell, SimpleWritableCell}, -}; - -pub struct NSSAStateCellOwned(pub V03State); - -impl BorshDeserialize for NSSAStateCellOwned { - fn deserialize_reader(reader: &mut R) -> std::io::Result { - V03State::deserialize_reader(reader).map(NSSAStateCellOwned) - } -} - -impl SimpleStorableCell for NSSAStateCellOwned { - type KeyParams = (); - - const CELL_NAME: &'static str = DB_NSSA_STATE_KEY; - const CF_NAME: &'static str = CF_NSSA_STATE_NAME; - - fn key_constructor(_params: Self::KeyParams) -> DbResult> { - borsh::to_vec(&Self::CELL_NAME).map_err(|err| { - DbError::borsh_cast_message( - err, - Some(format!("Failed to serialize {:?}", Self::CELL_NAME)), - ) - }) - } - - fn value_constructor(&self) -> DbResult> { - borsh::to_vec(&self.0).map_err(|err| { - DbError::borsh_cast_message(err, Some("Failed to serialize last state".to_owned())) - }) - } -} - -impl SimpleReadableCell for NSSAStateCellOwned {} - -pub struct NSSAStateCellRef<'state>(pub &'state V03State); - -impl BorshSerialize for NSSAStateCellRef<'_> { - fn serialize(&self, writer: &mut W) -> std::io::Result<()> { - V03State::serialize(self.0, writer) - } -} - -impl SimpleStorableCell for NSSAStateCellRef<'_> { - type KeyParams = (); - - const CELL_NAME: &'static str = DB_NSSA_STATE_KEY; - const CF_NAME: &'static str = CF_NSSA_STATE_NAME; - - fn key_constructor(_params: Self::KeyParams) -> DbResult> { - borsh::to_vec(&Self::CELL_NAME).map_err(|err| { - DbError::borsh_cast_message( - err, - Some(format!("Failed to serialize {:?}", Self::CELL_NAME)), - ) - }) - } - - fn value_constructor(&self) -> DbResult> { - borsh::to_vec(&self).map_err(|err| { - DbError::borsh_cast_message(err, Some("Failed to serialize last state".to_owned())) - }) - } -} - -impl SimpleWritableCell for NSSAStateCellRef<'_> {} - -#[derive(Debug)] -pub struct LastFinalizedBlockIdCell(pub Option); - -impl BorshSerialize for LastFinalizedBlockIdCell { - fn serialize(&self, writer: &mut W) -> std::io::Result<()> { - Option::::serialize(&self.0, writer) - } -} - -impl BorshDeserialize for LastFinalizedBlockIdCell { - fn deserialize_reader(reader: &mut R) -> std::io::Result { - Option::::deserialize_reader(reader).map(LastFinalizedBlockIdCell) - } -} - -impl SimpleStorableCell for LastFinalizedBlockIdCell { - type KeyParams = (); - - const CELL_NAME: &'static str = DB_META_LAST_FINALIZED_BLOCK_ID; - const CF_NAME: &'static str = CF_META_NAME; - - fn key_constructor(_params: Self::KeyParams) -> DbResult> { - borsh::to_vec(&Self::CELL_NAME).map_err(|err| { - DbError::borsh_cast_message( - err, - Some(format!("Failed to serialize {:?}", Self::CELL_NAME)), - ) - }) - } - - fn value_constructor(&self) -> DbResult> { - borsh::to_vec(&self).map_err(|err| { - DbError::borsh_cast_message( - err, - Some("Failed to serialize last finalized block id".to_owned()), - ) - }) - } -} - -impl SimpleReadableCell for LastFinalizedBlockIdCell {} - -impl SimpleWritableCell for LastFinalizedBlockIdCell {} - -pub struct LatestBlockMetaCellOwned(pub BlockMeta); - -impl BorshDeserialize for LatestBlockMetaCellOwned { - fn deserialize_reader(reader: &mut R) -> std::io::Result { - BlockMeta::deserialize_reader(reader).map(LatestBlockMetaCellOwned) - } -} - -impl SimpleStorableCell for LatestBlockMetaCellOwned { - type KeyParams = (); - - const CELL_NAME: &'static str = DB_META_LATEST_BLOCK_META_KEY; - const CF_NAME: &'static str = CF_META_NAME; - - fn key_constructor(_params: Self::KeyParams) -> DbResult> { - borsh::to_vec(&Self::CELL_NAME).map_err(|err| { - DbError::borsh_cast_message( - err, - Some(format!("Failed to serialize {:?}", Self::CELL_NAME)), - ) - }) - } - - fn value_constructor(&self) -> DbResult> { - borsh::to_vec(&self.0).map_err(|err| { - DbError::borsh_cast_message(err, Some("Failed to serialize last block meta".to_owned())) - }) - } -} - -impl SimpleReadableCell for LatestBlockMetaCellOwned {} - -pub struct LatestBlockMetaCellRef<'blockmeta>(pub &'blockmeta BlockMeta); - -impl BorshSerialize for LatestBlockMetaCellRef<'_> { - fn serialize(&self, writer: &mut W) -> std::io::Result<()> { - BlockMeta::serialize(self.0, writer) - } -} - -impl SimpleStorableCell for LatestBlockMetaCellRef<'_> { - type KeyParams = (); - - const CELL_NAME: &'static str = DB_META_LATEST_BLOCK_META_KEY; - const CF_NAME: &'static str = CF_META_NAME; - - fn key_constructor(_params: Self::KeyParams) -> DbResult> { - borsh::to_vec(&Self::CELL_NAME).map_err(|err| { - DbError::borsh_cast_message( - err, - Some(format!("Failed to serialize {:?}", Self::CELL_NAME)), - ) - }) - } - - fn value_constructor(&self) -> DbResult> { - borsh::to_vec(&self).map_err(|err| { - DbError::borsh_cast_message(err, Some("Failed to serialize last block meta".to_owned())) - }) - } -} - -impl SimpleWritableCell for LatestBlockMetaCellRef<'_> {} diff --git a/storage/src/storable_cell/cells/meta_shared.rs b/storage/src/storable_cell/cells/meta_shared.rs deleted file mode 100644 index 21ca74fc..00000000 --- a/storage/src/storable_cell/cells/meta_shared.rs +++ /dev/null @@ -1,173 +0,0 @@ -use borsh::{BorshDeserialize, BorshSerialize}; -use common::block::Block; - -use crate::{ - CF_BLOCK_NAME, CF_META_NAME, DB_META_FIRST_BLOCK_IN_DB_KEY, DB_META_FIRST_BLOCK_SET_KEY, - DB_META_LAST_BLOCK_IN_DB_KEY, DbResult, - error::DbError, - storable_cell::{SimpleReadableCell, SimpleStorableCell, SimpleWritableCell}, -}; - -#[derive(Debug)] -pub struct LastBlockCell(pub u64); - -impl BorshSerialize for LastBlockCell { - fn serialize(&self, writer: &mut W) -> std::io::Result<()> { - u64::serialize(&self.0, writer) - } -} - -impl BorshDeserialize for LastBlockCell { - fn deserialize_reader(reader: &mut R) -> std::io::Result { - u64::deserialize_reader(reader).map(LastBlockCell) - } -} - -impl SimpleStorableCell for LastBlockCell { - type KeyParams = (); - - const CELL_NAME: &'static str = DB_META_LAST_BLOCK_IN_DB_KEY; - const CF_NAME: &'static str = CF_META_NAME; - - fn key_constructor(_params: Self::KeyParams) -> DbResult> { - borsh::to_vec(&Self::CELL_NAME).map_err(|err| { - DbError::borsh_cast_message( - err, - Some(format!("Failed to serialize {:?}", Self::CELL_NAME)), - ) - }) - } - - fn value_constructor(&self) -> DbResult> { - borsh::to_vec(&self).map_err(|err| { - DbError::borsh_cast_message(err, Some("Failed to serialize last block id".to_owned())) - }) - } -} - -impl SimpleReadableCell for LastBlockCell {} - -impl SimpleWritableCell for LastBlockCell {} - -#[derive(Debug)] -pub struct FirstBlockSetCell(pub bool); - -impl BorshSerialize for FirstBlockSetCell { - fn serialize(&self, writer: &mut W) -> std::io::Result<()> { - bool::serialize(&self.0, writer) - } -} - -impl BorshDeserialize for FirstBlockSetCell { - fn deserialize_reader(reader: &mut R) -> std::io::Result { - bool::deserialize_reader(reader).map(FirstBlockSetCell) - } -} - -impl SimpleStorableCell for FirstBlockSetCell { - type KeyParams = (); - - const CELL_NAME: &'static str = DB_META_FIRST_BLOCK_SET_KEY; - const CF_NAME: &'static str = CF_META_NAME; - - fn key_constructor(_params: Self::KeyParams) -> DbResult> { - borsh::to_vec(&Self::CELL_NAME).map_err(|err| { - DbError::borsh_cast_message( - err, - Some(format!("Failed to serialize {:?}", Self::CELL_NAME)), - ) - }) - } - - fn value_constructor(&self) -> DbResult> { - borsh::to_vec(&self).map_err(|err| { - DbError::borsh_cast_message( - err, - Some("Failed to serialize first block set flag".to_owned()), - ) - }) - } -} - -impl SimpleReadableCell for FirstBlockSetCell {} - -impl SimpleWritableCell for FirstBlockSetCell {} - -#[derive(Debug)] -pub struct FirstBlockCell(pub u64); - -impl BorshSerialize for FirstBlockCell { - fn serialize(&self, writer: &mut W) -> std::io::Result<()> { - u64::serialize(&self.0, writer) - } -} - -impl BorshDeserialize for FirstBlockCell { - fn deserialize_reader(reader: &mut R) -> std::io::Result { - u64::deserialize_reader(reader).map(FirstBlockCell) - } -} - -impl SimpleStorableCell for FirstBlockCell { - type KeyParams = (); - - const CELL_NAME: &'static str = DB_META_FIRST_BLOCK_IN_DB_KEY; - const CF_NAME: &'static str = CF_META_NAME; - - fn key_constructor(_params: Self::KeyParams) -> DbResult> { - borsh::to_vec(&Self::CELL_NAME).map_err(|err| { - DbError::borsh_cast_message( - err, - Some(format!("Failed to serialize {:?}", Self::CELL_NAME)), - ) - }) - } - - fn value_constructor(&self) -> DbResult> { - borsh::to_vec(&self).map_err(|err| { - DbError::borsh_cast_message(err, Some("Failed to serialize first block id".to_owned())) - }) - } -} - -impl SimpleReadableCell for FirstBlockCell {} - -#[derive(Debug)] -pub struct BlockCell(pub Block); - -impl BorshSerialize for BlockCell { - fn serialize(&self, writer: &mut W) -> std::io::Result<()> { - Block::serialize(&self.0, writer) - } -} - -impl BorshDeserialize for BlockCell { - fn deserialize_reader(reader: &mut R) -> std::io::Result { - Block::deserialize_reader(reader).map(BlockCell) - } -} - -impl SimpleStorableCell for BlockCell { - type KeyParams = u64; - - const CELL_NAME: &'static str = "block"; - const CF_NAME: &'static str = CF_BLOCK_NAME; - - fn key_constructor(params: Self::KeyParams) -> DbResult> { - // ToDo: Replace with increasing ordering serialization - borsh::to_vec(¶ms).map_err(|err| { - DbError::borsh_cast_message( - err, - Some(format!("Failed to serialize {:?}", Self::CELL_NAME)), - ) - }) - } - - fn value_constructor(&self) -> DbResult> { - borsh::to_vec(&self).map_err(|err| { - DbError::borsh_cast_message(err, Some("Failed to serialize block".to_owned())) - }) - } -} - -impl SimpleReadableCell for BlockCell {} diff --git a/storage/src/storable_cell/cells/mod.rs b/storage/src/storable_cell/cells/mod.rs index 914befa1..a464144b 100644 --- a/storage/src/storable_cell/cells/mod.rs +++ b/storage/src/storable_cell/cells/mod.rs @@ -1,3 +1 @@ -pub mod meta_indexer; -pub mod meta_sequencer; -pub mod meta_shared; +pub mod shared_cells; diff --git a/storage/src/storable_cell/cells/shared_cells.rs b/storage/src/storable_cell/cells/shared_cells.rs new file mode 100644 index 00000000..6dc434c2 --- /dev/null +++ b/storage/src/storable_cell/cells/shared_cells.rs @@ -0,0 +1,89 @@ +use borsh::{BorshDeserialize, BorshSerialize}; +use common::block::Block; + +use crate::{ + BLOCK_CELL_NAME, CF_BLOCK_NAME, CF_META_NAME, DB_META_FIRST_BLOCK_IN_DB_KEY, + DB_META_FIRST_BLOCK_SET_KEY, DB_META_LAST_BLOCK_IN_DB_KEY, DbResult, + error::DbError, + storable_cell::{SimpleReadableCell, SimpleStorableCell, SimpleWritableCell}, +}; + +#[derive(Debug, BorshSerialize, BorshDeserialize)] +pub struct LastBlockCell(pub u64); + +impl SimpleStorableCell for LastBlockCell { + type KeyParams = (); + + const CELL_NAME: &'static str = DB_META_LAST_BLOCK_IN_DB_KEY; + const CF_NAME: &'static str = CF_META_NAME; +} + +impl SimpleReadableCell for LastBlockCell {} + +impl SimpleWritableCell for LastBlockCell { + fn value_constructor(&self) -> DbResult> { + borsh::to_vec(&self).map_err(|err| { + DbError::borsh_cast_message(err, Some("Failed to serialize last block id".to_owned())) + }) + } +} + +#[derive(Debug, BorshSerialize, BorshDeserialize)] +pub struct FirstBlockSetCell(pub bool); + +impl SimpleStorableCell for FirstBlockSetCell { + type KeyParams = (); + + const CELL_NAME: &'static str = DB_META_FIRST_BLOCK_SET_KEY; + const CF_NAME: &'static str = CF_META_NAME; +} + +impl SimpleReadableCell for FirstBlockSetCell {} + +impl SimpleWritableCell for FirstBlockSetCell { + fn value_constructor(&self) -> DbResult> { + borsh::to_vec(&self).map_err(|err| { + DbError::borsh_cast_message( + err, + Some("Failed to serialize first block set flag".to_owned()), + ) + }) + } +} + +#[derive(Debug, BorshSerialize, BorshDeserialize)] +pub struct FirstBlockCell(pub u64); + +impl SimpleStorableCell for FirstBlockCell { + type KeyParams = (); + + const CELL_NAME: &'static str = DB_META_FIRST_BLOCK_IN_DB_KEY; + const CF_NAME: &'static str = CF_META_NAME; +} + +impl SimpleReadableCell for FirstBlockCell {} + +#[derive(Debug, BorshSerialize, BorshDeserialize)] +pub struct BlockCell(pub Block); + +impl SimpleStorableCell for BlockCell { + type KeyParams = u64; + + const CELL_NAME: &'static str = BLOCK_CELL_NAME; + const CF_NAME: &'static str = CF_BLOCK_NAME; + + fn key_constructor(params: Self::KeyParams) -> DbResult> { + // ToDo: Replace with increasing ordering serialization + borsh::to_vec(¶ms).map_err(|err| { + DbError::borsh_cast_message( + err, + Some(format!( + "Failed to serialize {:?} key params", + Self::CELL_NAME + )), + ) + }) + } +} + +impl SimpleReadableCell for BlockCell {} diff --git a/storage/src/storable_cell/mod.rs b/storage/src/storable_cell/mod.rs index 8aa8b779..8322b6ae 100644 --- a/storage/src/storable_cell/mod.rs +++ b/storage/src/storable_cell/mod.rs @@ -12,8 +12,14 @@ pub trait SimpleStorableCell { const CELL_NAME: &'static str; type KeyParams; - fn key_constructor(params: Self::KeyParams) -> DbResult>; - fn value_constructor(&self) -> DbResult>; + fn key_constructor(_params: Self::KeyParams) -> DbResult> { + borsh::to_vec(&Self::CELL_NAME).map_err(|err| { + DbError::borsh_cast_message( + err, + Some(format!("Failed to serialize {:?}", Self::CELL_NAME)), + ) + }) + } fn column_ref(db: &DBWithThreadMode) -> Arc> { db.cf_handle(Self::CF_NAME) @@ -23,29 +29,17 @@ pub trait SimpleStorableCell { pub trait SimpleReadableCell: SimpleStorableCell + BorshDeserialize { fn get(db: &DBWithThreadMode, params: Self::KeyParams) -> DbResult { - let cf_ref = Self::column_ref(db); - let res = db - .get_cf(&cf_ref, Self::key_constructor(params)?) - .map_err(|rerr| { - DbError::rocksdb_cast_message( - rerr, - Some(format!("Failed to read {:?}", Self::CELL_NAME)), - ) - })?; + let res = Self::get_opt(db, params)?; - if let Some(data) = res { - Ok(borsh::from_slice::(&data).map_err(|err| { - DbError::borsh_cast_message( - err, - Some(format!("Failed to deserialize {:?}", Self::CELL_NAME)), - ) - })?) - } else { - Err(DbError::db_interaction_error(format!( - "{:?} not found", - Self::CELL_NAME - ))) - } + res.map_or_else( + || { + Err(DbError::db_interaction_error(format!( + "{:?} not found", + Self::CELL_NAME + ))) + }, + |data| Ok(data), + ) } fn get_opt( @@ -75,6 +69,8 @@ pub trait SimpleReadableCell: SimpleStorableCell + BorshDeserialize { } pub trait SimpleWritableCell: SimpleStorableCell + BorshSerialize { + fn value_constructor(&self) -> DbResult>; + fn put(&self, db: &DBWithThreadMode, params: Self::KeyParams) -> DbResult<()> { let cf_ref = Self::column_ref(db); db.put_cf(