From c9319e0f51b9c866703d1403cc590909c6178135 Mon Sep 17 00:00:00 2001 From: Arya Date: Thu, 13 Jun 2024 18:09:19 -0400 Subject: [PATCH] Moves disk IO to blocking tasks --- zebra-rpc/src/sync.rs | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/zebra-rpc/src/sync.rs b/zebra-rpc/src/sync.rs index bebd38ecf..49b5c0f70 100644 --- a/zebra-rpc/src/sync.rs +++ b/zebra-rpc/src/sync.rs @@ -73,6 +73,7 @@ impl TrustedChainSync { /// Starts syncing blocks from the node's non-finalized best chain. async fn sync(&mut self) { + self.try_catch_up_with_primary().await; let mut last_chain_tip_hash = if let Some(finalized_tip_block) = self.finalized_chain_tip_block().await { let last_chain_tip_hash = finalized_tip_block.hash; @@ -92,13 +93,7 @@ impl TrustedChainSync { "got a chain tip change" ); - let catch_up_result = self.db.try_catch_up_with_primary(); - - info!(?catch_up_result, "trying to catch up to primary"); - - // TODO: do in spawn_blocking - if self.non_finalized_state.chain_count() == 0 && self.db.contains_hash(target_tip_hash) - { + if self.is_finalized_tip_change(target_tip_hash).await { let block = self.finalized_chain_tip_block().await.expect( "should have genesis block after successful bestblockheightandhash response", ); @@ -194,6 +189,29 @@ impl TrustedChainSync { } } + async fn try_catch_up_with_primary(&self) { + let db = self.db.clone(); + tokio::task::spawn_blocking(move || { + let catch_up_result = db.try_catch_up_with_primary(); + tracing::debug!(?catch_up_result, "trying to catch up to primary"); + }) + .wait_for_panics() + .await + } + + async fn is_finalized_tip_change(&self, target_tip_hash: block::Hash) -> bool { + self.non_finalized_state.chain_count() == 0 && { + let db = self.db.clone(); + tokio::task::spawn_blocking(move || { + let catch_up_result = db.try_catch_up_with_primary(); + tracing::debug!(?catch_up_result, "trying to catch up to primary"); + db.contains_hash(target_tip_hash) + }) + .wait_for_panics() + .await + } + } + /// Returns the current tip height and hash async fn next_block_height_and_prev_hash(&self) -> (block::Height, block::Hash) { if let Some(tip) = self.non_finalized_state.best_tip() {