Skip to content

Commit

Permalink
fixes panic when trying to open multiple mutable storage instances.
Browse files Browse the repository at this point in the history
  • Loading branch information
arya2 committed Feb 7, 2024
1 parent 30a681b commit 3ca3af0
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 43 deletions.
5 changes: 3 additions & 2 deletions zebra-scan/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use tracing::Instrument;
use zebra_chain::parameters::Network;
use zebra_state::ChainTipChange;

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

/// Initialize [`ScanService`] based on its config.
///
Expand Down Expand Up @@ -56,8 +56,9 @@ pub fn spawn_init(
// TODO: spawn an entirely new executor here, to avoid timing attacks.
tokio::spawn(
async move {
let db = Storage::new(&config, network, false);
let (_cmd_sender, cmd_receiver) = std::sync::mpsc::channel();
scan::init(config, network, state, chain_tip_change, cmd_receiver).await
scan::start(state, chain_tip_change, db, cmd_receiver).await
}
.in_current_span(),
)
Expand Down
6 changes: 4 additions & 2 deletions zebra-scan/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ impl ScanService {
state: scan::State,
chain_tip_change: ChainTipChange,
) -> Self {
let db = Storage::new(config, network, false);

Self {
db: Storage::new(config, network, false),
scan_task: ScanTask::spawn(config, network, state, chain_tip_change),
scan_task: ScanTask::spawn(db.clone(), state, chain_tip_change),
db,
}
}

Expand Down
18 changes: 3 additions & 15 deletions zebra-scan/src/service/scan_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ use std::sync::{mpsc, Arc};
use color_eyre::Report;
use tokio::task::JoinHandle;

use zebra_chain::parameters::Network;
use zebra_state::ChainTipChange;

use crate::Config;
use crate::storage::Storage;

mod commands;
mod executor;
Expand All @@ -31,23 +30,12 @@ pub struct ScanTask {

impl ScanTask {
/// Spawns a new [`ScanTask`].
pub fn spawn(
config: &Config,
network: Network,
state: scan::State,
chain_tip_change: ChainTipChange,
) -> Self {
pub fn spawn(db: Storage, state: scan::State, chain_tip_change: ChainTipChange) -> Self {
// TODO: Use a bounded channel or move this logic to the scan service or another service.
let (cmd_sender, cmd_receiver) = mpsc::channel();

Self {
handle: Arc::new(scan::spawn_init(
config,
network,
state,
chain_tip_change,
cmd_receiver,
)),
handle: Arc::new(scan::spawn_init(db, state, chain_tip_change, cmd_receiver)),
cmd_sender,
}
}
Expand Down
26 changes: 2 additions & 24 deletions zebra-scan/src/service/scan_task/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ use zebra_state::{ChainTipChange, SaplingScannedResult, TransactionIndex};
use crate::{
service::{ScanTask, ScanTaskCommand},
storage::{SaplingScanningKey, Storage},
Config,
};

use super::executor;
Expand Down Expand Up @@ -489,31 +488,10 @@ async fn tip_height(mut state: State) -> Result<Height, Report> {
///
/// TODO: add a test for this function.
pub fn spawn_init(
config: &Config,
network: Network,
storage: Storage,
state: State,
chain_tip_change: ChainTipChange,
cmd_receiver: Receiver<ScanTaskCommand>,
) -> JoinHandle<Result<(), Report>> {
let config = config.clone();

tokio::spawn(init(config, network, state, chain_tip_change, cmd_receiver).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: State,
chain_tip_change: ChainTipChange,
cmd_receiver: Receiver<ScanTaskCommand>,
) -> Result<(), Report> {
let storage = tokio::task::spawn_blocking(move || Storage::new(&config, network, false))
.wait_for_panics()
.await;

// TODO: add more tasks here?
start(state, chain_tip_change, storage, cmd_receiver).await
tokio::spawn(start(state, chain_tip_change, storage, cmd_receiver).in_current_span())
}

0 comments on commit 3ca3af0

Please sign in to comment.