124 lines
3.6 KiB
Rust
Raw Normal View History

2026-02-03 11:36:07 +02:00
use std::{path::Path, sync::Arc};
2026-01-30 10:25:34 +02:00
use anyhow::Result;
2026-02-12 14:27:36 +02:00
use bedrock_client::HeaderId;
2026-02-26 13:46:38 +02:00
use common::{
block::{BedrockStatus, Block},
transaction::NSSATransaction,
};
2026-02-04 14:57:38 +02:00
use nssa::{Account, AccountId, V02State};
2026-01-30 10:25:34 +02:00
use storage::indexer::RocksDBIO;
2026-02-03 11:36:07 +02:00
#[derive(Clone)]
2026-01-30 10:25:34 +02:00
pub struct IndexerStore {
2026-02-03 11:36:07 +02:00
dbio: Arc<RocksDBIO>,
2026-01-30 10:25:34 +02:00
}
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-02-04 14:57:38 +02:00
Ok(Self {
dbio: Arc::new(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)
}
2026-02-18 14:58:33 +02:00
pub fn last_observed_l1_lib_header(&self) -> Result<Option<HeaderId>> {
2026-02-12 14:27:36 +02:00
Ok(self
.dbio
2026-02-18 14:58:33 +02:00
.get_meta_last_observed_l1_lib_header_in_db()?
.map(HeaderId::from))
2026-02-12 14:27:36 +02:00
}
2026-02-09 15:05:01 +02:00
pub fn get_last_block_id(&self) -> Result<u64> {
Ok(self.dbio.get_meta_last_block_in_db()?)
}
2026-01-30 10:25:34 +02:00
pub fn get_block_at_id(&self, id: u64) -> Result<Block> {
Ok(self.dbio.get_block(id)?)
}
2026-02-04 14:57:38 +02:00
pub fn get_block_batch(&self, offset: u64, limit: u64) -> Result<Vec<Block>> {
Ok(self.dbio.get_block_batch(offset, limit)?)
}
pub fn get_transaction_by_hash(&self, tx_hash: [u8; 32]) -> Result<NSSATransaction> {
let block = self.get_block_at_id(self.dbio.get_block_id_by_tx_hash(tx_hash)?)?;
let transaction = block
2026-02-04 14:57:38 +02:00
.body
.transactions
.iter()
2026-02-10 15:54:57 +02:00
.find(|enc_tx| enc_tx.hash().0 == tx_hash)
2026-02-04 14:57:38 +02:00
.ok_or_else(|| anyhow::anyhow!("Transaction not found in DB"))?;
Ok(transaction.clone())
2026-02-04 14:57:38 +02:00
}
pub fn get_block_by_hash(&self, hash: [u8; 32]) -> Result<Block> {
2026-02-10 15:54:57 +02:00
self.get_block_at_id(self.dbio.get_block_id_by_hash(hash)?)
2026-02-04 14:57:38 +02:00
}
2026-02-05 16:21:08 +02:00
pub fn get_transactions_by_account(
&self,
acc_id: [u8; 32],
offset: u64,
limit: u64,
) -> Result<Vec<NSSATransaction>> {
Ok(self.dbio.get_acc_transactions(acc_id, offset, limit)?)
}
2026-01-30 10:25:34 +02:00
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-02-04 14:57:38 +02:00
pub fn get_account_final(&self, account_id: &AccountId) -> Result<Account> {
Ok(self.final_state()?.get_account_by_id(*account_id))
2026-02-04 14:57:38 +02:00
}
pub fn put_block(&self, mut block: Block, l1_header: HeaderId) -> Result<()> {
2026-01-30 10:25:34 +02:00
let mut final_state = self.dbio.final_state()?;
for transaction in &block.body.transactions {
2026-02-23 10:55:00 +02:00
transaction
.clone()
.transaction_stateless_check()?
.execute_check_on_state(&mut final_state)?;
2026-01-30 10:25:34 +02:00
}
2026-02-26 13:46:38 +02:00
// ToDo: Currently we are fetching only finalized blocks
// if it changes, the following lines need to be updated
// to represent correct block finality
block.bedrock_status = BedrockStatus::Finalized;
2026-02-12 14:27:36 +02:00
Ok(self.dbio.put_block(block, l1_header.into())?)
2026-01-30 10:25:34 +02:00
}
}