Progress report

This commit is contained in:
Hanh 2022-11-13 11:27:52 +08:00
parent f27283c521
commit 6e95f609ea
2 changed files with 22 additions and 6 deletions

View File

@ -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<bool>,
) -> 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 {

View File

@ -84,15 +84,20 @@ impl<
Ok(())
}
pub fn process(&mut self, blocks: &[CompactBlock]) -> Result<()> {
pub fn process(&mut self, blocks: &[CompactBlock]) -> Result<usize> {
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::<u32>() 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())
}
}