2024-02-06 10:41:00 -08:00
|
|
|
//! Initializing the scanner and gRPC server.
|
2024-01-24 14:37:03 -08:00
|
|
|
|
2024-02-07 14:36:01 -08:00
|
|
|
use std::net::SocketAddr;
|
|
|
|
|
2023-11-30 04:59:15 -08:00
|
|
|
use color_eyre::Report;
|
2024-02-07 14:36:01 -08:00
|
|
|
use tokio::task::JoinHandle;
|
2024-01-25 17:29:37 -08:00
|
|
|
use tower::ServiceBuilder;
|
2023-11-30 04:59:15 -08:00
|
|
|
|
2024-02-07 14:36:01 -08:00
|
|
|
use tracing::Instrument;
|
|
|
|
use zebra_chain::{diagnostic::task::WaitForPanics, parameters::Network};
|
2023-12-03 13:58:48 -08:00
|
|
|
use zebra_state::ChainTipChange;
|
2023-11-30 04:59:15 -08:00
|
|
|
|
2024-02-07 14:36:01 -08:00
|
|
|
use crate::{scan, service::ScanService, storage::Storage, Config};
|
2023-11-30 04:59:15 -08:00
|
|
|
|
2024-01-25 17:29:37 -08:00
|
|
|
/// Initialize [`ScanService`] based on its config.
|
2023-11-30 13:27:46 -08:00
|
|
|
///
|
|
|
|
/// TODO: add a test for this function.
|
2024-02-07 14:36:01 -08:00
|
|
|
pub async fn init_with_server(
|
|
|
|
listen_addr: SocketAddr,
|
2023-12-03 13:58:48 -08:00
|
|
|
config: Config,
|
|
|
|
network: Network,
|
|
|
|
state: scan::State,
|
|
|
|
chain_tip_change: ChainTipChange,
|
|
|
|
) -> Result<(), Report> {
|
2024-02-07 14:36:01 -08:00
|
|
|
info!(?config, "starting scan service");
|
|
|
|
let scan_service = ServiceBuilder::new()
|
|
|
|
.buffer(10)
|
|
|
|
.service(ScanService::new(&config, network, state, chain_tip_change).await);
|
|
|
|
|
|
|
|
// TODO: move this to zebra-grpc init() function and include addr
|
|
|
|
info!("starting scan gRPC server");
|
2024-01-25 17:29:37 -08:00
|
|
|
|
|
|
|
// Start the gRPC server.
|
2024-02-07 14:36:01 -08:00
|
|
|
zebra_grpc::server::init(listen_addr, scan_service).await?;
|
2023-11-30 04:59:15 -08:00
|
|
|
|
2024-01-25 17:29:37 -08:00
|
|
|
Ok(())
|
2023-11-30 04:59:15 -08:00
|
|
|
}
|
2024-02-07 14:36:01 -08:00
|
|
|
|
|
|
|
/// Initialize the scanner and its gRPC server based on its config, and spawn a task for it.
|
|
|
|
pub fn spawn_init(
|
|
|
|
config: Config,
|
|
|
|
network: Network,
|
|
|
|
state: scan::State,
|
|
|
|
chain_tip_change: ChainTipChange,
|
|
|
|
) -> JoinHandle<Result<(), Report>> {
|
|
|
|
if let Some(listen_addr) = config.listen_addr {
|
|
|
|
// TODO: spawn an entirely new executor here, to avoid timing attacks.
|
|
|
|
tokio::spawn(
|
|
|
|
init_with_server(listen_addr, config, network, state, chain_tip_change)
|
|
|
|
.in_current_span(),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
// TODO: spawn an entirely new executor here, to avoid timing attacks.
|
|
|
|
tokio::spawn(
|
|
|
|
async move {
|
|
|
|
let storage =
|
|
|
|
tokio::task::spawn_blocking(move || Storage::new(&config, network, false))
|
|
|
|
.wait_for_panics()
|
|
|
|
.await;
|
2024-02-12 16:42:40 -08:00
|
|
|
let (_cmd_sender, cmd_receiver) = tokio::sync::mpsc::channel(1);
|
2024-02-07 14:36:01 -08:00
|
|
|
scan::start(state, chain_tip_change, storage, cmd_receiver).await
|
|
|
|
}
|
|
|
|
.in_current_span(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|