lssa/storage/src/indexer/write_non_atomic.rs

60 lines
2.0 KiB
Rust
Raw Normal View History

2026-03-25 16:14:33 +02:00
use super::{BREAKPOINT_INTERVAL, DbError, DbResult, RocksDBIO, V03State};
2026-03-31 09:08:40 +03:00
use crate::{
indexer::indexer_cells::{
BreakpointCellRef, LastBreakpointIdCell, LastObservedL1LibHeaderCell,
},
storable_cell::cells::shared_cells::{FirstBlockSetCell, LastBlockCell},
2026-03-18 17:52:28 +02:00
};
2026-03-09 11:15:57 +02:00
2026-03-18 17:52:28 +02:00
#[expect(clippy::multiple_inherent_impl, reason = "Readability")]
2026-03-09 11:15:57 +02:00
impl RocksDBIO {
2026-03-09 16:35:03 +02:00
// Meta
2026-03-09 11:15:57 +02:00
pub fn put_meta_last_block_in_db(&self, block_id: u64) -> DbResult<()> {
2026-03-26 13:10:31 +02:00
self.put(&LastBlockCell(block_id), ())
2026-03-09 11:15:57 +02:00
}
pub fn put_meta_last_observed_l1_lib_header_in_db(
&self,
l1_lib_header: [u8; 32],
) -> DbResult<()> {
2026-03-26 13:10:31 +02:00
self.put(&LastObservedL1LibHeaderCell(l1_lib_header), ())
2026-03-09 11:15:57 +02:00
}
pub fn put_meta_last_breakpoint_id(&self, br_id: u64) -> DbResult<()> {
2026-03-26 13:10:31 +02:00
self.put(&LastBreakpointIdCell(br_id), ())
2026-03-09 11:15:57 +02:00
}
pub fn put_meta_is_first_block_set(&self) -> DbResult<()> {
2026-03-26 13:10:31 +02:00
self.put(&FirstBlockSetCell(true), ())
2026-03-09 11:15:57 +02:00
}
2026-03-09 16:35:03 +02:00
// State
2026-03-09 11:15:57 +02:00
2026-03-23 18:16:53 +02:00
pub fn put_breakpoint(&self, br_id: u64, breakpoint: &V03State) -> DbResult<()> {
2026-03-26 13:10:31 +02:00
self.put(&BreakpointCellRef(breakpoint), br_id)
2026-03-09 11:15:57 +02:00
}
2026-03-11 18:37:54 +02:00
pub fn put_next_breakpoint(&self) -> DbResult<()> {
let last_block = self.get_meta_last_block_in_db()?;
2026-03-18 17:52:28 +02:00
let next_breakpoint_id = self
.get_meta_last_breakpoint_id()?
.checked_add(1)
.expect("Breakpoint Id will be lesser than u64::MAX");
let block_to_break_id = next_breakpoint_id
.checked_mul(u64::from(BREAKPOINT_INTERVAL))
.expect("Reached maximum breakpoint id");
2026-03-11 18:37:54 +02:00
if block_to_break_id <= last_block {
let next_breakpoint = self.calculate_state_for_id(block_to_break_id)?;
self.put_breakpoint(next_breakpoint_id, &next_breakpoint)?;
2026-03-11 18:37:54 +02:00
self.put_meta_last_breakpoint_id(next_breakpoint_id)
} else {
Err(DbError::db_interaction_error(
"Breakpoint not yet achieved".to_owned(),
2026-03-11 18:37:54 +02:00
))
}
}
2026-03-09 16:35:03 +02:00
}