Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sync: Show scan progress as a percentage #44

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/commands/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,13 @@ impl Command {
.get_wallet_birthday()?
.unwrap_or_else(|| params.activation_height(NetworkUpgrade::Sapling).unwrap());

#[cfg(feature = "tui")]
let wallet_summary = db_data.get_wallet_summary(10)?;

#[cfg(feature = "tui")]
let tui_handle = if self.defrag {
let mut app = defrag::App::new(shutdown.tui_quit_signal(), wallet_birthday);
let mut app =
defrag::App::new(shutdown.tui_quit_signal(), wallet_birthday, wallet_summary);
let handle = app.handle();
tokio::spawn(async move {
if let Err(e) = app.run(tui).await {
Expand Down Expand Up @@ -534,6 +538,7 @@ fn scan_blocks<P: Parameters + Send + 'static>(
#[cfg(feature = "tui")]
if let Some(handle) = tui_handle {
handle.set_scanning_range(None);
handle.set_wallet_summary(db_data.get_wallet_summary(10)?);
}

match scan_result {
Expand Down
62 changes: 58 additions & 4 deletions src/commands/sync/defrag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ use roaring::RoaringBitmap;
use tokio::sync::{mpsc, oneshot};
use tracing::{error, info, warn};
use tui_logger::{TuiLoggerLevelOutput, TuiLoggerSmartWidget};
use zcash_client_backend::data_api::scanning::{ScanPriority, ScanRange};
use zcash_client_backend::data_api::{
scanning::{ScanPriority, ScanRange},
WalletSummary,
};
use zcash_client_sqlite::AccountId;
use zcash_protocol::consensus::BlockHeight;

use crate::tui;
Expand Down Expand Up @@ -70,12 +74,30 @@ impl AppHandle {
}
}
}

/// Returns `true` if the TUI exited.
pub(super) fn set_wallet_summary(
&self,
wallet_summary: Option<WalletSummary<AccountId>>,
) -> bool {
match self
.action_tx
.send(Action::SetWalletSummary(wallet_summary))
{
Ok(()) => false,
Err(e) => {
error!("Failed to send: {}", e);
true
}
}
}
}

pub(super) struct App {
should_quit: bool,
notify_shutdown: Option<oneshot::Sender<()>>,
wallet_birthday: BlockHeight,
wallet_summary: Option<WalletSummary<AccountId>>,
scan_ranges: BTreeMap<BlockHeight, ScanPriority>,
fetching_set: RoaringBitmap,
fetched_set: RoaringBitmap,
Expand All @@ -86,12 +108,17 @@ pub(super) struct App {
}

impl App {
pub(super) fn new(notify_shutdown: oneshot::Sender<()>, wallet_birthday: BlockHeight) -> Self {
pub(super) fn new(
notify_shutdown: oneshot::Sender<()>,
wallet_birthday: BlockHeight,
wallet_summary: Option<WalletSummary<AccountId>>,
) -> Self {
let (action_tx, action_rx) = mpsc::unbounded_channel();
Self {
should_quit: false,
notify_shutdown: Some(notify_shutdown),
wallet_birthday,
wallet_summary,
scan_ranges: BTreeMap::new(),
fetching_set: RoaringBitmap::new(),
fetched_set: RoaringBitmap::new(),
Expand Down Expand Up @@ -147,6 +174,7 @@ impl App {
self.fetched_set.insert(u32::from(fetched_height));
},
Action::SetScanning(scanning_range) => self.scanning_range = scanning_range,
Action::SetWalletSummary(wallet_summary) => self.wallet_summary = wallet_summary,
Action::Render => {
tui.draw(|f| self.ui(f))?;
}
Expand Down Expand Up @@ -200,8 +228,12 @@ impl App {
}

fn ui(&mut self, frame: &mut Frame) {
let [upper_area, log_area] =
Layout::vertical([Constraint::Min(0), Constraint::Length(15)]).areas(frame.area());
let [upper_area, mid_area, log_area] = Layout::vertical([
Constraint::Min(0),
Constraint::Length(3),
Constraint::Length(15),
])
.areas(frame.area());

let defrag_area = {
let block = Block::bordered().title("Wallet Defragmentor");
Expand Down Expand Up @@ -293,6 +325,27 @@ impl App {
}
}

let stats = Line::from_iter(
self.wallet_summary
.as_ref()
.iter()
.flat_map(|wallet_summary| {
let synced = wallet_summary.scan_progress().map(|progress| {
Span::raw(format!(
"Synced: {:0.3}%",
(*progress.numerator() as f64) * 100f64
/ (*progress.denominator() as f64)
))
});
[synced]
})
.flatten(),
);
frame.render_widget(
Paragraph::new(stats).block(Block::bordered().title("Stats")),
mid_area,
);

frame.render_widget(
TuiLoggerSmartWidget::default()
.title_log("Log Entries")
Expand Down Expand Up @@ -326,6 +379,7 @@ pub(super) enum Action {
SetFetching(Option<Range<BlockHeight>>),
SetFetched(BlockHeight),
SetScanning(Option<Range<BlockHeight>>),
SetWalletSummary(Option<WalletSummary<AccountId>>),
Render,
}

Expand Down