From c1d2b625ea1852962c4b1c09d1439b18ca9b49ac Mon Sep 17 00:00:00 2001 From: Daniil Polyakov Date: Thu, 29 Jan 2026 23:03:00 +0300 Subject: [PATCH] Add IndexerCore to IndexerService --- Cargo.lock | 9 +-------- indexer/core/src/config.rs | 10 +++++----- indexer/service/Cargo.toml | 9 +-------- indexer/service/src/main.rs | 18 +++++++++++++----- indexer/service/src/service.rs | 14 +++++++++++++- 5 files changed, 33 insertions(+), 27 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 46d53b34..d87af8af 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3225,22 +3225,15 @@ version = "0.1.0" dependencies = [ "anyhow", "async-trait", - "bedrock_client", - "borsh", "clap 4.5.53", - "common", "env_logger", - "futures", + "indexer_core", "indexer_service_protocol", "indexer_service_rpc", "jsonrpsee", "log", - "logos-blockchain-core", - "serde", - "serde_json", "tokio", "tokio-util", - "url", ] [[package]] diff --git a/indexer/core/src/config.rs b/indexer/core/src/config.rs index 486a00ac..db7855ba 100644 --- a/indexer/core/src/config.rs +++ b/indexer/core/src/config.rs @@ -1,6 +1,6 @@ use std::{fs::File, io::BufReader, path::Path}; -use anyhow::{Context, Result}; +use anyhow::{Context as _, Result}; use bedrock_client::BackoffConfig; use common::config::BasicAuth; use logos_blockchain_core::mantle::ops::channel::ChannelId; @@ -23,12 +23,12 @@ pub struct IndexerConfig { } impl IndexerConfig { - pub fn from_path(config_home: &Path) -> Result { - let file = File::open(config_home) - .with_context(|| format!("Failed to open indexer config at {config_home:?}"))?; + pub fn from_path(config_path: &Path) -> Result { + let file = File::open(config_path) + .with_context(|| format!("Failed to open indexer config at {config_path:?}"))?; let reader = BufReader::new(file); serde_json::from_reader(reader) - .with_context(|| format!("Failed to parse indexer config at {config_home:?}")) + .with_context(|| format!("Failed to parse indexer config at {config_path:?}")) } } diff --git a/indexer/service/Cargo.toml b/indexer/service/Cargo.toml index 723c82ac..a959fcb1 100644 --- a/indexer/service/Cargo.toml +++ b/indexer/service/Cargo.toml @@ -6,8 +6,7 @@ edition = "2024" [dependencies] indexer_service_protocol.workspace = true indexer_service_rpc = { workspace = true, features = ["server"] } -common.workspace = true -bedrock_client.workspace = true +indexer_core.workspace = true clap = { workspace = true, features = ["derive"] } anyhow.workspace = true @@ -17,12 +16,6 @@ env_logger.workspace = true log.workspace = true jsonrpsee.workspace = true async-trait = "0.1.89" -serde.workspace = true -serde_json.workspace = true -borsh.workspace = true -futures.workspace = true -url.workspace = true -logos-blockchain-core.workspace = true [features] # Return mock responses with generated data for testing purposes diff --git a/indexer/service/src/main.rs b/indexer/service/src/main.rs index 1c6856ec..f18e540b 100644 --- a/indexer/service/src/main.rs +++ b/indexer/service/src/main.rs @@ -1,4 +1,4 @@ -use std::net::SocketAddr; +use std::{net::SocketAddr, path::PathBuf}; use anyhow::{Context as _, Result}; use clap::Parser; @@ -10,6 +10,8 @@ use tokio_util::sync::CancellationToken; #[derive(Debug, Parser)] #[clap(version)] struct Args { + #[clap(rename = "config")] + config_path: PathBuf, #[clap(short, long, default_value = "8779")] port: u16, } @@ -18,11 +20,11 @@ struct Args { async fn main() -> Result<()> { env_logger::init(); - let args = Args::parse(); + let Args { config_path, port } = Args::parse(); let cancellation_token = listen_for_shutdown_signal(); - let handle = run_server(args.port).await?; + let handle = run_server(config_path, port).await?; let handle_clone = handle.clone(); tokio::select! { @@ -39,7 +41,9 @@ async fn main() -> Result<()> { Ok(()) } -async fn run_server(port: u16) -> Result { +async fn run_server(config_path: PathBuf, port: u16) -> Result { + let config = IndexerServiceConfig::from_file(&config_path)?; + let server = Server::builder() .build(SocketAddr::from(([0, 0, 0, 0], port))) .await @@ -52,7 +56,11 @@ async fn run_server(port: u16) -> Result { info!("Starting Indexer Service RPC server on {addr}"); #[cfg(not(feature = "mock-responses"))] - let handle = server.start(indexer_service::service::IndexerService.into_rpc()); + let handle = { + let service = indexer_service::service::IndexerService::new(config) + .context("Failed to initialize indexer service")?; + server.start(service.into_rpc()) + }?; #[cfg(feature = "mock-responses")] let handle = server.start( indexer_service::mock_service::MockIndexerService::new_with_mock_blocks().into_rpc(), diff --git a/indexer/service/src/service.rs b/indexer/service/src/service.rs index 432dcc24..6d61fb41 100644 --- a/indexer/service/src/service.rs +++ b/indexer/service/src/service.rs @@ -1,7 +1,19 @@ +use anyhow::Result; +use indexer_core::{IndexerCore, config::IndexerConfig}; use indexer_service_protocol::{Account, AccountId, Block, BlockId, Hash, Transaction}; use jsonrpsee::{core::SubscriptionResult, types::ErrorObjectOwned}; -pub struct IndexerService; +pub struct IndexerService { + indexer: IndexerCore, +} + +impl IndexerService { + pub fn new(config: IndexerConfig) -> Result { + Ok(Self { + indexer: IndexerCore::new(config)?, + }) + } +} // `async_trait` is required by `jsonrpsee` #[async_trait::async_trait]