Skip to content

Commit

Permalink
Re-order Zebra startup, so slow services are launched last (#3091)
Browse files Browse the repository at this point in the history
* Start network before verifiers

This makes the Groth16 download task start as late as possible.

* Explain why the Groth16 download must happen first

* Speed up Zebra shutdown: skip waiting for the tokio runtime
  • Loading branch information
teor2345 authored Nov 23, 2021
1 parent 8e49663 commit 68d7198
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 217 deletions.
3 changes: 2 additions & 1 deletion zebra-consensus/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ where
S: Service<zs::Request, Response = zs::Response, Error = BoxError> + Send + Clone + 'static,
S::Future: Send + 'static,
{
// pre-download Groth16 parameters async
// Pre-download Groth16 parameters in a separate thread.
// This thread must be launched first, so the download doesn't happen on the startup thread.

let groth16_download_handle = spawn_blocking(|| {
tracing::info!("checking if Zcash Sapling and Sprout parameters have been downloaded");
Expand Down
44 changes: 26 additions & 18 deletions zebrad/src/commands/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ use tower::{builder::ServiceBuilder, util::BoxService};

use crate::{
components::{
inbound::InboundSetupData,
mempool::{self, Mempool},
sync,
tokio::{RuntimeRun, TokioComponent},
Expand Down Expand Up @@ -83,15 +84,6 @@ impl StartCmd {
zebra_state::init(config.state.clone(), config.network.network);
let state = ServiceBuilder::new().buffer(20).service(state_service);

info!("initializing verifiers");
let (chain_verifier, tx_verifier, mut groth16_download_handle) =
zebra_consensus::chain::init(
config.consensus.clone(),
config.network.network,
state.clone(),
)
.await;

info!("initializing network");
// The service that our node uses to respond to requests by peers. The
// load_shed middleware ensures that we reduce the size of the peer set
Expand All @@ -100,24 +92,33 @@ impl StartCmd {
let inbound = ServiceBuilder::new()
.load_shed()
.buffer(20)
.service(Inbound::new(
setup_rx,
state.clone(),
chain_verifier.clone(),
));
.service(Inbound::new(setup_rx));

let (peer_set, address_book) =
zebra_network::init(config.network.clone(), inbound, latest_chain_tip.clone()).await;

info!("initializing verifiers");
let (chain_verifier, tx_verifier, mut groth16_download_handle) =
zebra_consensus::chain::init(
config.consensus.clone(),
config.network.network,
state.clone(),
)
.await;

info!("initializing syncer");
let (syncer, sync_status) =
ChainSync::new(&config, peer_set.clone(), state.clone(), chain_verifier);
let (syncer, sync_status) = ChainSync::new(
&config,
peer_set.clone(),
state.clone(),
chain_verifier.clone(),
);

info!("initializing mempool");
let (mempool, mempool_transaction_receiver) = Mempool::new(
&config.mempool,
peer_set.clone(),
state,
state.clone(),
tx_verifier,
sync_status.clone(),
latest_chain_tip,
Expand All @@ -126,8 +127,15 @@ impl StartCmd {
let mempool = BoxService::new(mempool);
let mempool = ServiceBuilder::new().buffer(20).service(mempool);

let setup_data = InboundSetupData {
address_book,
block_download_peer_set: peer_set.clone(),
block_verifier: chain_verifier,
mempool: mempool.clone(),
state,
};
setup_tx
.send((peer_set.clone(), address_book, mempool.clone()))
.send(setup_data)
.map_err(|_| eyre!("could not send setup data to inbound service"))?;

let syncer_error_future = syncer.sync();
Expand Down
2 changes: 1 addition & 1 deletion zebrad/src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! component and dependency injection models are designed to work together, but
//! don't fit the async context well.
mod inbound;
pub mod inbound;
#[allow(missing_docs)]
pub mod mempool;
pub mod metrics;
Expand Down
Loading

0 comments on commit 68d7198

Please sign in to comment.