lssa/indexer/core/src/lib.rs

128 lines
4.4 KiB
Rust
Raw Normal View History

use std::sync::Arc;
2026-02-12 14:27:36 +02:00
2026-01-27 09:46:31 +02:00
use anyhow::Result;
2026-02-10 14:03:56 +02:00
use common::block::{Block, HashableBlockData};
2026-02-03 11:36:07 +02:00
// ToDo: Remove after testnet
2026-02-10 14:03:56 +02:00
use common::{HashType, PINATA_BASE58};
use futures::StreamExt as _;
use log::{error, info, warn};
use logos_blockchain_core::header::HeaderId;
use logos_blockchain_zone_sdk::indexer::ZoneIndexer;
2026-01-09 15:10:38 +02:00
2026-02-03 11:36:07 +02:00
use crate::{block_store::IndexerStore, config::IndexerConfig};
2026-01-12 15:51:24 +02:00
2026-02-03 11:36:07 +02:00
pub mod block_store;
2026-01-12 15:51:24 +02:00
pub mod config;
2026-01-30 21:37:27 +03:00
#[derive(Clone)]
2026-01-09 15:10:38 +02:00
pub struct IndexerCore {
pub zone_indexer: Arc<ZoneIndexer>,
2026-02-03 11:36:07 +02:00
pub config: IndexerConfig,
pub store: IndexerStore,
2026-01-09 15:10:38 +02:00
}
impl IndexerCore {
pub fn new(config: IndexerConfig) -> Result<Self> {
2026-02-10 14:03:56 +02:00
let hashable_data = HashableBlockData {
block_id: 1,
transactions: vec![],
prev_block_hash: HashType([0; 32]),
timestamp: 0,
};
2026-02-03 11:36:07 +02:00
2026-02-18 14:58:33 +02:00
// Genesis creation is fine as it is,
// because it will be overwritten by sequencer.
// Therefore:
// ToDo: remove key from indexer config, use some default.
2026-02-10 14:03:56 +02:00
let signing_key = nssa::PrivateKey::try_new(config.signing_key).unwrap();
let channel_genesis_msg_id = [0; 32];
2026-03-04 18:42:33 +03:00
let genesis_block = hashable_data.into_pending_block(&signing_key, channel_genesis_msg_id);
2026-02-03 11:36:07 +02:00
2026-02-18 14:58:33 +02:00
// This is a troubling moment, because changes in key protocol can
// affect this. And indexer can not reliably ask this data from sequencer
// because indexer must be independent from it.
// ToDo: move initial state generation into common and use the same method
// for indexer and sequencer. This way both services buit at same version
// could be in sync.
2026-02-03 11:36:07 +02:00
let initial_commitments: Vec<nssa_core::Commitment> = config
.initial_commitments
.iter()
.map(|init_comm_data| {
let npk = &init_comm_data.npk;
let mut acc = init_comm_data.account.clone();
acc.program_owner = nssa::program::Program::authenticated_transfer_program().id();
nssa_core::Commitment::new(npk, &acc)
})
.collect();
let init_accs: Vec<(nssa::AccountId, u128)> = config
.initial_accounts
.iter()
.map(|acc_data| (acc_data.account_id, acc_data.balance))
2026-02-03 11:36:07 +02:00
.collect();
let mut state = nssa::V02State::new_with_genesis_accounts(&init_accs, &initial_commitments);
// ToDo: Remove after testnet
state.add_pinata_program(PINATA_BASE58.parse().unwrap());
2026-02-13 13:59:06 +02:00
let home = config.home.join("rocksdb");
2026-02-03 11:36:07 +02:00
let auth = config.bedrock_client_config.auth.clone().map(Into::into);
let zone_indexer = ZoneIndexer::new(
config.channel_id,
config.bedrock_client_config.addr.clone(),
auth,
);
2026-01-12 15:51:24 +02:00
Ok(Self {
zone_indexer: Arc::new(zone_indexer),
2026-01-12 15:51:24 +02:00
config,
2026-03-04 18:42:33 +03:00
store: IndexerStore::open_db_with_genesis(&home, &genesis_block, &state)?,
2026-01-12 15:51:24 +02:00
})
2026-01-09 15:10:38 +02:00
}
2026-01-12 15:51:24 +02:00
2026-03-03 23:21:08 +03:00
pub fn subscribe_parse_block_stream(&self) -> impl futures::Stream<Item = Result<Block>> {
2026-01-30 21:37:27 +03:00
async_stream::stream! {
info!("Starting zone-sdk indexer using follow()");
2026-01-30 21:37:27 +03:00
let follow_stream = match self.zone_indexer.follow().await {
Ok(s) => s,
Err(e) => {
error!("Failed to start zone-sdk follow stream: {e}");
return;
2026-03-03 23:21:08 +03:00
}
2026-02-12 14:27:36 +02:00
};
let mut follow_stream = std::pin::pin!(follow_stream);
2026-02-12 14:27:36 +02:00
while let Some(zone_block) = follow_stream.next().await {
let block: Block = match borsh::from_slice(&zone_block.data) {
Ok(b) => b,
Err(e) => {
error!("Failed to deserialize L2 block from zone-sdk: {e}");
continue;
2026-01-16 16:15:21 +02:00
}
2026-03-03 23:21:08 +03:00
};
2026-02-16 17:54:54 +02:00
info!("Indexed L2 block {}", block.header.block_id);
2026-02-16 17:54:54 +02:00
// TODO: Remove l1_header placeholder once storage layer
// no longer requires it. Zone-sdk handles L1 tracking internally.
let placeholder_l1_header = HeaderId::from([0_u8; 32]);
2026-02-24 12:27:47 +02:00
if let Err(err) = self.store.put_block(block.clone(), placeholder_l1_header).await {
error!("Failed to store block {}: {err:#}", block.header.block_id);
2026-02-24 12:27:47 +02:00
}
2026-02-16 17:54:54 +02:00
yield Ok(block);
2026-02-16 17:54:54 +02:00
}
warn!("zone-sdk follow stream ended");
2026-01-16 16:15:21 +02:00
}
2026-01-12 15:51:24 +02:00
}
2026-02-04 14:57:38 +02:00
}