From 6e95f609eafd07b71c81fc765dd1e77a67a0993c Mon Sep 17 00:00:00 2001 From: Hanh Date: Sun, 13 Nov 2022 11:27:52 +0800 Subject: [PATCH] Progress report --- src/scan.rs | 17 ++++++++++++++--- src/sync.rs | 11 ++++++++--- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/scan.rs b/src/scan.rs index a7fed99..f5eb248 100644 --- a/src/scan.rs +++ b/src/scan.rs @@ -82,7 +82,7 @@ pub async fn sync_async<'a>( get_tx: bool, // TODO target_height_offset: u32, max_cost: u32, - _progress_callback: AMProgressCallback, // TODO + progress_callback: AMProgressCallback, // TODO cancel: &'static std::sync::Mutex, ) -> anyhow::Result<()> { let c = CoinConfig::get(coin); @@ -127,6 +127,12 @@ pub async fn sync_async<'a>( coin_type: c.coin_type, db_path: db_path.clone(), }; + let mut progress = Progress { + height: 0, + trial_decryptions: 0, + downloaded: 0, + }; + while let Some(blocks) = blocks_rx.recv().await { let first_block = blocks.0.first().unwrap(); // cannot be empty because blocks are not log::info!("Height: {}", first_block.height); @@ -135,6 +141,9 @@ pub async fn sync_async<'a>( let last_height = last_block.height as u32; let last_timestamp = last_block.time; + progress.downloaded += blocks.1; + progress.height = last_height; + // Sapling log::info!("Sapling"); { @@ -148,7 +157,7 @@ pub async fn sync_async<'a>( "sapling".to_string(), ); synchronizer.initialize(height)?; - synchronizer.process(&blocks.0)?; + progress.trial_decryptions += synchronizer.process(&blocks.0)? as u64; } // Orchard @@ -164,12 +173,14 @@ pub async fn sync_async<'a>( "orchard".to_string(), ); synchronizer.initialize(height)?; - synchronizer.process(&blocks.0)?; + progress.trial_decryptions += synchronizer.process(&blocks.0)? as u64; } let db = db_builder.build()?; db.store_block_timestamp(last_height, &last_hash, last_timestamp)?; height = last_height; + let cb = progress_callback.lock().await; + cb(progress.clone()); } if get_tx { diff --git a/src/sync.rs b/src/sync.rs index 56c8c55..442fd34 100644 --- a/src/sync.rs +++ b/src/sync.rs @@ -84,15 +84,20 @@ impl< Ok(()) } - pub fn process(&mut self, blocks: &[CompactBlock]) -> Result<()> { + pub fn process(&mut self, blocks: &[CompactBlock]) -> Result { if blocks.is_empty() { - return Ok(()); + return Ok(0); } let decrypter = self.decrypter.clone(); let decrypted_blocks: Vec<_> = blocks .par_iter() .map(|b| decrypter.decrypt_notes(b, &self.vks)) .collect(); + let count_outputs: usize = decrypted_blocks + .iter() + .map(|b| b.count_outputs) + .sum::() as usize; + let mut db = self.db.build()?; self.warper.initialize(&self.tree, &self.witnesses); let db_tx = db.begin_transaction()?; @@ -179,7 +184,7 @@ impl< self.witnesses = updated_witnesses; db_tx.commit()?; - Ok(()) + Ok(count_outputs * self.vks.len()) } }