72 lines
1.9 KiB
Rust
Raw Normal View History

2026-01-30 10:25:34 +02:00
use std::path::Path;
use anyhow::Result;
2026-01-30 12:51:18 +02:00
use common::{
block::Block,
transaction::{NSSATransaction, execute_check_transaction_on_state, transaction_pre_check},
};
2026-01-30 10:25:34 +02:00
use nssa::V02State;
use storage::indexer::RocksDBIO;
pub struct IndexerStore {
dbio: RocksDBIO,
}
impl IndexerStore {
/// Starting database at the start of new chain.
/// Creates files if necessary.
///
/// ATTENTION: Will overwrite genesis block.
pub fn open_db_with_genesis(
location: &Path,
start_data: Option<(Block, V02State)>,
) -> Result<Self> {
let dbio = RocksDBIO::open_or_create(location, start_data)?;
2026-01-30 12:51:18 +02:00
Ok(Self { dbio })
2026-01-30 10:25:34 +02:00
}
/// Reopening existing database
pub fn open_db_restart(location: &Path) -> Result<Self> {
Self::open_db_with_genesis(location, None)
}
pub fn get_block_at_id(&self, id: u64) -> Result<Block> {
Ok(self.dbio.get_block(id)?)
}
pub fn genesis_id(&self) -> u64 {
2026-01-30 12:51:18 +02:00
self.dbio
.get_meta_first_block_in_db()
.expect("Must be set at the DB startup")
2026-01-30 10:25:34 +02:00
}
pub fn last_block(&self) -> u64 {
2026-01-30 12:51:18 +02:00
self.dbio
.get_meta_last_block_in_db()
.expect("Must be set at the DB startup")
2026-01-30 10:25:34 +02:00
}
pub fn get_state_at_block(&self, block_id: u64) -> Result<V02State> {
Ok(self.dbio.calculate_state_for_id(block_id)?)
}
2026-01-30 12:51:18 +02:00
pub fn final_state(&self) -> Result<V02State> {
Ok(self.dbio.final_state()?)
}
2026-01-30 10:25:34 +02:00
pub fn put_block(&self, block: Block) -> Result<()> {
let mut final_state = self.dbio.final_state()?;
for encoded_transaction in &block.body.transactions {
let transaction = NSSATransaction::try_from(encoded_transaction)?;
2026-01-30 12:51:18 +02:00
execute_check_transaction_on_state(
&mut final_state,
transaction_pre_check(transaction)?,
)?;
2026-01-30 10:25:34 +02:00
}
Ok(self.dbio.put_block(block)?)
}
}