From 40f8750278f353c580a80b160261905ee66a49a9 Mon Sep 17 00:00:00 2001 From: Pravdyvy Date: Wed, 1 Apr 2026 08:53:45 +0300 Subject: [PATCH] fix: suggestions 2 --- storage/src/indexer/indexer_cells.rs | 18 ++++++++++++ storage/src/indexer/mod.rs | 37 +++++------------------- storage/src/indexer/read_once.rs | 1 + storage/src/indexer/write_atomic.rs | 2 +- storage/src/indexer/write_non_atomic.rs | 1 + storage/src/lib.rs | 33 ++++++++++++++++++++- storage/src/sequencer/mod.rs | 36 ++++++----------------- storage/src/sequencer/sequencer_cells.rs | 36 +++++++++++++++++++++++ storage/src/storable_cell/mod.rs | 10 +------ 9 files changed, 107 insertions(+), 67 deletions(-) diff --git a/storage/src/indexer/indexer_cells.rs b/storage/src/indexer/indexer_cells.rs index 144f56b9..57766252 100644 --- a/storage/src/indexer/indexer_cells.rs +++ b/storage/src/indexer/indexer_cells.rs @@ -210,3 +210,21 @@ impl SimpleWritableCell for AccNumTxCell { }) } } + +#[cfg(test)] +mod uniform_tests { + use crate::{ + indexer::indexer_cells::{BreakpointCellOwned, BreakpointCellRef}, + storable_cell::SimpleStorableCell as _, + }; + + #[test] + fn breakpoint_ref_and_owned_is_aligned() { + assert_eq!(BreakpointCellRef::CELL_NAME, BreakpointCellOwned::CELL_NAME); + assert_eq!(BreakpointCellRef::CF_NAME, BreakpointCellOwned::CF_NAME); + assert_eq!( + BreakpointCellRef::key_constructor(1000).unwrap(), + BreakpointCellOwned::key_constructor(1000).unwrap() + ); + } +} diff --git a/storage/src/indexer/mod.rs b/storage/src/indexer/mod.rs index 7d4bce09..d3e91c89 100644 --- a/storage/src/indexer/mod.rs +++ b/storage/src/indexer/mod.rs @@ -3,14 +3,10 @@ use std::{path::Path, sync::Arc}; use common::block::Block; use nssa::V03State; use rocksdb::{ - BoundColumnFamily, ColumnFamilyDescriptor, DBWithThreadMode, MultiThreaded, Options, WriteBatch, + BoundColumnFamily, ColumnFamilyDescriptor, DBWithThreadMode, MultiThreaded, Options, }; -use crate::{ - BREAKPOINT_INTERVAL, CF_BLOCK_NAME, CF_META_NAME, DbResult, - error::DbError, - storable_cell::{SimpleReadableCell, SimpleWritableCell}, -}; +use crate::{BREAKPOINT_INTERVAL, CF_BLOCK_NAME, CF_META_NAME, DBIO, DbResult, error::DbError}; pub mod indexer_cells; pub mod read_multiple; @@ -48,6 +44,12 @@ pub struct RocksDBIO { pub db: DBWithThreadMode, } +impl DBIO for RocksDBIO { + fn db(&self) -> &DBWithThreadMode { + &self.db + } +} + impl RocksDBIO { pub fn open_or_create( path: &Path, @@ -145,29 +147,6 @@ impl RocksDBIO { .expect("Account meta column should exist") } - // Generics - - fn get(&self, params: T::KeyParams) -> DbResult { - T::get(&self.db, params) - } - - fn get_opt(&self, params: T::KeyParams) -> DbResult> { - T::get_opt(&self.db, params) - } - - fn put(&self, cell: &T, params: T::KeyParams) -> DbResult<()> { - cell.put(&self.db, params) - } - - fn put_batch( - &self, - cell: &T, - params: T::KeyParams, - write_batch: &mut WriteBatch, - ) -> DbResult<()> { - cell.put_batch(&self.db, params, write_batch) - } - // State pub fn calculate_state_for_id(&self, block_id: u64) -> DbResult { diff --git a/storage/src/indexer/read_once.rs b/storage/src/indexer/read_once.rs index a652b8e9..bf084c84 100644 --- a/storage/src/indexer/read_once.rs +++ b/storage/src/indexer/read_once.rs @@ -1,5 +1,6 @@ use super::{Block, DbResult, RocksDBIO, V03State}; use crate::{ + DBIO as _, indexer::indexer_cells::{ AccNumTxCell, BlockHashToBlockIdMapCell, BreakpointCellOwned, LastBreakpointIdCell, LastObservedL1LibHeaderCell, TxHashToBlockIdMapCell, diff --git a/storage/src/indexer/write_atomic.rs b/storage/src/indexer/write_atomic.rs index e1145131..b8ea4751 100644 --- a/storage/src/indexer/write_atomic.rs +++ b/storage/src/indexer/write_atomic.rs @@ -4,7 +4,7 @@ use rocksdb::WriteBatch; use super::{BREAKPOINT_INTERVAL, Block, DbError, DbResult, RocksDBIO}; use crate::{ - DB_META_FIRST_BLOCK_IN_DB_KEY, + DB_META_FIRST_BLOCK_IN_DB_KEY, DBIO as _, indexer::indexer_cells::{ AccNumTxCell, BlockHashToBlockIdMapCell, LastBreakpointIdCell, LastObservedL1LibHeaderCell, TxHashToBlockIdMapCell, diff --git a/storage/src/indexer/write_non_atomic.rs b/storage/src/indexer/write_non_atomic.rs index d5a862f4..a250dcc7 100644 --- a/storage/src/indexer/write_non_atomic.rs +++ b/storage/src/indexer/write_non_atomic.rs @@ -1,5 +1,6 @@ use super::{BREAKPOINT_INTERVAL, DbError, DbResult, RocksDBIO, V03State}; use crate::{ + DBIO as _, indexer::indexer_cells::{ BreakpointCellRef, LastBreakpointIdCell, LastObservedL1LibHeaderCell, }, diff --git a/storage/src/lib.rs b/storage/src/lib.rs index 5d9f8705..182b8991 100644 --- a/storage/src/lib.rs +++ b/storage/src/lib.rs @@ -1,4 +1,9 @@ -use crate::error::DbError; +use rocksdb::{DBWithThreadMode, MultiThreaded, WriteBatch}; + +use crate::{ + error::DbError, + storable_cell::{SimpleReadableCell, SimpleWritableCell}, +}; pub mod error; pub mod indexer; @@ -36,3 +41,29 @@ pub const CF_BLOCK_NAME: &str = "cf_block"; pub const CF_META_NAME: &str = "cf_meta"; pub type DbResult = Result; + +/// Minimal requirements for DB IO. +pub trait DBIO { + fn db(&self) -> &DBWithThreadMode; + + fn get(&self, params: T::KeyParams) -> DbResult { + T::get(self.db(), params) + } + + fn get_opt(&self, params: T::KeyParams) -> DbResult> { + T::get_opt(self.db(), params) + } + + fn put(&self, cell: &T, params: T::KeyParams) -> DbResult<()> { + cell.put(self.db(), params) + } + + fn put_batch( + &self, + cell: &T, + params: T::KeyParams, + write_batch: &mut WriteBatch, + ) -> DbResult<()> { + cell.put_batch(self.db(), params, write_batch) + } +} diff --git a/storage/src/sequencer/mod.rs b/storage/src/sequencer/mod.rs index fcd81702..140008b5 100644 --- a/storage/src/sequencer/mod.rs +++ b/storage/src/sequencer/mod.rs @@ -7,15 +7,14 @@ use rocksdb::{ }; use crate::{ - CF_BLOCK_NAME, CF_META_NAME, DB_META_FIRST_BLOCK_IN_DB_KEY, DbResult, + CF_BLOCK_NAME, CF_META_NAME, DB_META_FIRST_BLOCK_IN_DB_KEY, DBIO, DbResult, error::DbError, sequencer::sequencer_cells::{ LastFinalizedBlockIdCell, LatestBlockMetaCellOwned, LatestBlockMetaCellRef, NSSAStateCellOwned, NSSAStateCellRef, }, - storable_cell::{ - SimpleReadableCell, SimpleWritableCell, - cells::shared_cells::{BlockCell, FirstBlockCell, FirstBlockSetCell, LastBlockCell}, + storable_cell::cells::shared_cells::{ + BlockCell, FirstBlockCell, FirstBlockSetCell, LastBlockCell, }, }; @@ -36,6 +35,12 @@ pub struct RocksDBIO { pub db: DBWithThreadMode, } +impl DBIO for RocksDBIO { + fn db(&self) -> &DBWithThreadMode { + &self.db + } +} + impl RocksDBIO { pub fn open_or_create( path: &Path, @@ -116,29 +121,6 @@ impl RocksDBIO { .expect("State should exist") } - // Generics - - fn get(&self, params: T::KeyParams) -> DbResult { - T::get(&self.db, params) - } - - fn get_opt(&self, params: T::KeyParams) -> DbResult> { - T::get_opt(&self.db, params) - } - - fn put(&self, cell: &T, params: T::KeyParams) -> DbResult<()> { - cell.put(&self.db, params) - } - - fn put_batch( - &self, - cell: &T, - params: T::KeyParams, - write_batch: &mut WriteBatch, - ) -> DbResult<()> { - cell.put_batch(&self.db, params, write_batch) - } - // Meta pub fn get_meta_first_block_in_db(&self) -> DbResult { diff --git a/storage/src/sequencer/sequencer_cells.rs b/storage/src/sequencer/sequencer_cells.rs index 7fa0b48a..386cd953 100644 --- a/storage/src/sequencer/sequencer_cells.rs +++ b/storage/src/sequencer/sequencer_cells.rs @@ -94,3 +94,39 @@ impl SimpleWritableCell for LatestBlockMetaCellRef<'_> { }) } } + +#[cfg(test)] +mod uniform_tests { + use crate::{ + sequencer::sequencer_cells::{ + LatestBlockMetaCellOwned, LatestBlockMetaCellRef, NSSAStateCellOwned, NSSAStateCellRef, + }, + storable_cell::SimpleStorableCell as _, + }; + + #[test] + fn state_ref_and_owned_is_aligned() { + assert_eq!(NSSAStateCellRef::CELL_NAME, NSSAStateCellOwned::CELL_NAME); + assert_eq!(NSSAStateCellRef::CF_NAME, NSSAStateCellOwned::CF_NAME); + assert_eq!( + NSSAStateCellRef::key_constructor(()).unwrap(), + NSSAStateCellOwned::key_constructor(()).unwrap() + ); + } + + #[test] + fn block_meta_ref_and_owned_is_aligned() { + assert_eq!( + LatestBlockMetaCellRef::CELL_NAME, + LatestBlockMetaCellOwned::CELL_NAME + ); + assert_eq!( + LatestBlockMetaCellRef::CF_NAME, + LatestBlockMetaCellOwned::CF_NAME + ); + assert_eq!( + LatestBlockMetaCellRef::key_constructor(()).unwrap(), + LatestBlockMetaCellOwned::key_constructor(()).unwrap() + ); + } +} diff --git a/storage/src/storable_cell/mod.rs b/storage/src/storable_cell/mod.rs index 8322b6ae..5a00e3a6 100644 --- a/storage/src/storable_cell/mod.rs +++ b/storage/src/storable_cell/mod.rs @@ -31,15 +31,7 @@ pub trait SimpleReadableCell: SimpleStorableCell + BorshDeserialize { fn get(db: &DBWithThreadMode, params: Self::KeyParams) -> DbResult { let res = Self::get_opt(db, params)?; - res.map_or_else( - || { - Err(DbError::db_interaction_error(format!( - "{:?} not found", - Self::CELL_NAME - ))) - }, - |data| Ok(data), - ) + res.ok_or_else(|| DbError::db_interaction_error(format!("{:?} not found", Self::CELL_NAME))) } fn get_opt(