Skip to content

Commit

Permalink
Use spawn_blocking() for database methods
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 committed Nov 30, 2023
1 parent ee11f59 commit 166376d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
22 changes: 17 additions & 5 deletions zebra-scan/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,30 @@ use color_eyre::Report;
use tokio::task::JoinHandle;
use tracing::Instrument;

use zebra_chain::parameters::Network;
use zebra_chain::{diagnostic::task::WaitForPanics, parameters::Network};

use crate::{scan, storage::Storage, Config};

/// Initialize the scanner based on its config.
pub fn init(
/// Initialize the scanner based on its config, and spawn a task for it.
///
/// TODO: add a test for this function.
pub fn spawn_init(
config: &Config,
network: Network,
state: scan::State,
) -> JoinHandle<Result<(), Report>> {
let storage = Storage::new(config, network);
let config = config.clone();
tokio::spawn(init(config, network, state).in_current_span())
}

/// Initialize the scanner based on its config.
///
/// TODO: add a test for this function.
pub async fn init(config: Config, network: Network, state: scan::State) -> Result<(), Report> {
let storage = tokio::task::spawn_blocking(move || Storage::new(&config, network))
.wait_for_panics()
.await;

// TODO: add more tasks here?
tokio::spawn(scan::start(state, storage).in_current_span())
scan::start(state, storage).await
}
2 changes: 1 addition & 1 deletion zebra-scan/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ pub mod storage;
mod tests;

pub use config::Config;
pub use init::init;
pub use init::{init, spawn_init};
2 changes: 2 additions & 0 deletions zebra-scan/src/storage/db/sapling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ impl Storage {
// Reading Sapling database entries

/// Returns the results for a specific key and block height.
//
// TODO: add tests for this method
pub fn sapling_result_for_key_and_block(
&self,
index: &SaplingScannedDatabaseIndex,
Expand Down
2 changes: 1 addition & 1 deletion zebrad/src/commands/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ impl StartCmd {
// Spawn never ending scan task.
let scan_task_handle = {
info!("spawning shielded scanner with configured viewing keys");
zebra_scan::init(&config.shielded_scan, config.network.network, state)
zebra_scan::spawn_init(&config.shielded_scan, config.network.network, state)
};

#[cfg(not(feature = "zebra-scan"))]
Expand Down

0 comments on commit 166376d

Please sign in to comment.