From 281668bd828857ddb4a391c0d329456c172d1350 Mon Sep 17 00:00:00 2001 From: Arya Date: Thu, 6 Jun 2024 20:24:13 -0400 Subject: [PATCH] moves elasticsearch initialization to `FinalizedState::new_with_debug()` --- zebra-state/src/service.rs | 58 +--------------------- zebra-state/src/service/finalized_state.rs | 42 ++++++++++------ 2 files changed, 30 insertions(+), 70 deletions(-) diff --git a/zebra-state/src/service.rs b/zebra-state/src/service.rs index 8675cb45632..d6b3a416021 100644 --- a/zebra-state/src/service.rs +++ b/zebra-state/src/service.rs @@ -24,15 +24,6 @@ use std::{ time::{Duration, Instant}, }; -#[cfg(feature = "elasticsearch")] -use elasticsearch::{ - auth::Credentials::Basic, - cert::CertificateValidation, - http::transport::{SingleNodeConnectionPool, TransportBuilder}, - http::Url, - Elasticsearch, -}; - use futures::future::FutureExt; use tokio::sync::{oneshot, watch}; use tower::{util::BoxService, Service, ServiceExt}; @@ -319,29 +310,7 @@ impl StateService { checkpoint_verify_concurrency_limit: usize, ) -> (Self, ReadStateService, LatestChainTip, ChainTipChange) { let timer = CodeTimer::start(); - - #[cfg(feature = "elasticsearch")] - let finalized_state = { - let conn_pool = SingleNodeConnectionPool::new( - Url::parse(config.elasticsearch_url.as_str()) - .expect("configured elasticsearch url is invalid"), - ); - let transport = TransportBuilder::new(conn_pool) - .cert_validation(CertificateValidation::None) - .auth(Basic( - config.clone().elasticsearch_username, - config.clone().elasticsearch_password, - )) - .build() - .expect("elasticsearch transport builder should not fail"); - let elastic_db = Some(Elasticsearch::new(transport)); - - FinalizedState::new(&config, network, elastic_db) - }; - - #[cfg(not(feature = "elasticsearch"))] let finalized_state = { FinalizedState::new(&config, network) }; - timer.finish(module_path!(), line!(), "opening finalized state database"); let timer = CodeTimer::start(); @@ -1959,36 +1928,13 @@ pub fn init_read_only( ZebraDb, tokio::sync::watch::Sender, ) { + let finalized_state = { FinalizedState::new_with_debug(&config, network, true, true) }; let (non_finalized_state_sender, non_finalized_state_receiver) = tokio::sync::watch::channel(NonFinalizedState::new(network)); - #[cfg(feature = "elasticsearch")] - let finalized_state = { - let conn_pool = SingleNodeConnectionPool::new( - Url::parse(config.elasticsearch_url.as_str()) - .expect("configured elasticsearch url is invalid"), - ); - let transport = TransportBuilder::new(conn_pool) - .cert_validation(CertificateValidation::None) - .auth(Basic( - config.clone().elasticsearch_username, - config.clone().elasticsearch_password, - )) - .build() - .expect("elasticsearch transport builder should not fail"); - let elastic_db = Some(Elasticsearch::new(transport)); - - FinalizedState::new_with_debug(&config, network, true, elastic_db, true) - }; - - #[cfg(not(feature = "elasticsearch"))] - let finalized_state = { FinalizedState::new_with_debug(&config, network, true, true) }; - - let db = finalized_state.db.clone(); - ( ReadStateService::new(&finalized_state, None, non_finalized_state_receiver), - db, + finalized_state.db.clone(), non_finalized_state_sender, ) } diff --git a/zebra-state/src/service/finalized_state.rs b/zebra-state/src/service/finalized_state.rs index 7256d89dac2..26e7b62a1f7 100644 --- a/zebra-state/src/service/finalized_state.rs +++ b/zebra-state/src/service/finalized_state.rs @@ -139,19 +139,8 @@ pub struct FinalizedState { impl FinalizedState { /// Returns an on-disk database instance for `config`, `network`, and `elastic_db`. /// If there is no existing database, creates a new database on disk. - pub fn new( - config: &Config, - network: &Network, - #[cfg(feature = "elasticsearch")] elastic_db: Option, - ) -> Self { - Self::new_with_debug( - config, - network, - false, - #[cfg(feature = "elasticsearch")] - elastic_db, - false, - ) + pub fn new(config: &Config, network: &Network) -> Self { + Self::new_with_debug(config, network, false, false) } /// Returns an on-disk database instance with the supplied production and debug settings. @@ -162,9 +151,34 @@ impl FinalizedState { config: &Config, network: &Network, debug_skip_format_upgrades: bool, - #[cfg(feature = "elasticsearch")] elastic_db: Option, read_only: bool, ) -> Self { + #[cfg(feature = "elasticsearch")] + let elastic_db = { + use elasticsearch::{ + auth::Credentials::Basic, + cert::CertificateValidation, + http::transport::{SingleNodeConnectionPool, TransportBuilder}, + http::Url, + Elasticsearch, + }; + + let conn_pool = SingleNodeConnectionPool::new( + Url::parse(config.elasticsearch_url.as_str()) + .expect("configured elasticsearch url is invalid"), + ); + let transport = TransportBuilder::new(conn_pool) + .cert_validation(CertificateValidation::None) + .auth(Basic( + config.clone().elasticsearch_username, + config.clone().elasticsearch_password, + )) + .build() + .expect("elasticsearch transport builder should not fail"); + + Some(Elasticsearch::new(transport)) + }; + let db = ZebraDb::new( config, STATE_DATABASE_KIND,