fix: suggestions 2

This commit is contained in:
Pravdyvy 2026-04-01 08:53:45 +03:00
parent f59bcde78a
commit 40f8750278
9 changed files with 107 additions and 67 deletions

View File

@ -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()
);
}
}

View File

@ -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<MultiThreaded>,
}
impl DBIO for RocksDBIO {
fn db(&self) -> &DBWithThreadMode<MultiThreaded> {
&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<T: SimpleReadableCell>(&self, params: T::KeyParams) -> DbResult<T> {
T::get(&self.db, params)
}
fn get_opt<T: SimpleReadableCell>(&self, params: T::KeyParams) -> DbResult<Option<T>> {
T::get_opt(&self.db, params)
}
fn put<T: SimpleWritableCell>(&self, cell: &T, params: T::KeyParams) -> DbResult<()> {
cell.put(&self.db, params)
}
fn put_batch<T: SimpleWritableCell>(
&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<V03State> {

View File

@ -1,5 +1,6 @@
use super::{Block, DbResult, RocksDBIO, V03State};
use crate::{
DBIO as _,
indexer::indexer_cells::{
AccNumTxCell, BlockHashToBlockIdMapCell, BreakpointCellOwned, LastBreakpointIdCell,
LastObservedL1LibHeaderCell, TxHashToBlockIdMapCell,

View File

@ -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,

View File

@ -1,5 +1,6 @@
use super::{BREAKPOINT_INTERVAL, DbError, DbResult, RocksDBIO, V03State};
use crate::{
DBIO as _,
indexer::indexer_cells::{
BreakpointCellRef, LastBreakpointIdCell, LastObservedL1LibHeaderCell,
},

View File

@ -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<T> = Result<T, DbError>;
/// Minimal requirements for DB IO.
pub trait DBIO {
fn db(&self) -> &DBWithThreadMode<MultiThreaded>;
fn get<T: SimpleReadableCell>(&self, params: T::KeyParams) -> DbResult<T> {
T::get(self.db(), params)
}
fn get_opt<T: SimpleReadableCell>(&self, params: T::KeyParams) -> DbResult<Option<T>> {
T::get_opt(self.db(), params)
}
fn put<T: SimpleWritableCell>(&self, cell: &T, params: T::KeyParams) -> DbResult<()> {
cell.put(self.db(), params)
}
fn put_batch<T: SimpleWritableCell>(
&self,
cell: &T,
params: T::KeyParams,
write_batch: &mut WriteBatch,
) -> DbResult<()> {
cell.put_batch(self.db(), params, write_batch)
}
}

View File

@ -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<MultiThreaded>,
}
impl DBIO for RocksDBIO {
fn db(&self) -> &DBWithThreadMode<MultiThreaded> {
&self.db
}
}
impl RocksDBIO {
pub fn open_or_create(
path: &Path,
@ -116,29 +121,6 @@ impl RocksDBIO {
.expect("State should exist")
}
// Generics
fn get<T: SimpleReadableCell>(&self, params: T::KeyParams) -> DbResult<T> {
T::get(&self.db, params)
}
fn get_opt<T: SimpleReadableCell>(&self, params: T::KeyParams) -> DbResult<Option<T>> {
T::get_opt(&self.db, params)
}
fn put<T: SimpleWritableCell>(&self, cell: &T, params: T::KeyParams) -> DbResult<()> {
cell.put(&self.db, params)
}
fn put_batch<T: SimpleWritableCell>(
&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<u64> {

View File

@ -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()
);
}
}

View File

@ -31,15 +31,7 @@ pub trait SimpleReadableCell: SimpleStorableCell + BorshDeserialize {
fn get(db: &DBWithThreadMode<MultiThreaded>, params: Self::KeyParams) -> DbResult<Self> {
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(