Skip to content
Closed
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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions pepper-sync/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ const VERIFY_BLOCK_RANGE_SIZE: u32 = 10;
pub struct SyncStatus {
pub scan_ranges: Vec<ScanRange>,
pub sync_start_height: BlockHeight,
pub total_blocks: u32,
pub total_outputs: u32,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comment below

pub session_blocks_scanned: u32,
pub total_blocks_scanned: u32,
pub percentage_session_blocks_scanned: f32,
Expand Down Expand Up @@ -98,6 +100,11 @@ impl From<SyncStatus> for json::JsonValue {
})
.collect();

// Derive a simple boolean completeness flag from integer counts
let sync_complete = value.total_blocks_scanned >= value.total_blocks
&& (value.total_sapling_outputs_scanned + value.total_orchard_outputs_scanned)
>= value.total_outputs;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this sync completeness is equivalent to percentage_total_outputs_scanned being 100%. if there is an issue with using percentage_total_outputs_scanned (?) then this should be fixed instead of duplicating the logic into the json conversion. due to this it is unnecessary for the sync engine to maintain the two new fields added to SyncStatus, these values can actually be derived from the other data if necessary so im not sure this is the right approach

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh sorry, it is actually equivalent to the sync poll returning a sync complete. is there an issue with sync poll you are experiencing? if you could write up a detailed issue we can discuss the solution if thats ok

json::object! {
"scan_ranges" => scan_ranges,
"sync_start_height" => u32::from(value.sync_start_height),
Expand All @@ -111,6 +118,7 @@ impl From<SyncStatus> for json::JsonValue {
"total_orchard_outputs_scanned" => value.total_orchard_outputs_scanned,
"percentage_session_outputs_scanned" => value.percentage_session_outputs_scanned,
"percentage_total_outputs_scanned" => value.percentage_total_outputs_scanned,
"sync_complete" => sync_complete,
}
}
}
Expand Down Expand Up @@ -598,6 +606,8 @@ where
return Ok(SyncStatus {
scan_ranges: sync_state.scan_ranges.clone(),
sync_start_height: 0.into(),
total_blocks: 0,
total_outputs: 0,
session_blocks_scanned: 0,
total_blocks_scanned: 0,
percentage_session_blocks_scanned: 0.0,
Expand Down Expand Up @@ -671,6 +681,8 @@ where
Ok(SyncStatus {
scan_ranges: sync_state.scan_ranges.clone(),
sync_start_height: sync_state.initial_sync_state.sync_start_height,
total_blocks,
total_outputs,
session_blocks_scanned,
total_blocks_scanned,
percentage_session_blocks_scanned,
Expand Down
1 change: 1 addition & 0 deletions zingo-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ regtest = [ "zingolib/regtest", "zingo_common_components/for_test" ]

[dependencies]
pepper-sync = { workspace = true }
serde_json = { workspace = true }
zingo_common_components = { workspace = true, optional = true }
zingolib = { workspace = true }

Expand Down
51 changes: 29 additions & 22 deletions zingo-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,37 +516,44 @@ fn dispatch_command_or_start_interactive(cli_config: &ConfigTemplate) {
if cli_config.command.is_none() {
start_interactive(command_transmitter, resp_receiver);
} else {
// Optionally wait for background sync to finish before executing command
// Optionally wait for background sync to finish before executing command.
// Retry requesting status until the channel closes or the percentage field
// indicates completion.
if cli_config.sync && cli_config.waitsync {
use std::{thread, time::Duration};

loop {
// Poll sync task status
// Request machine-readable sync status.
command_transmitter
.send(("sync".to_string(), vec!["poll".to_string()]))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sync poll cannot be replaced by sync status. sync poll is polling the sync handle and it the only way to return the actual errors from the sync task.

.send(("sync".to_string(), vec!["status".to_string()]))
.unwrap();

match resp_receiver.recv() {
Ok(resp) => {
if resp.starts_with("Error:") {
eprintln!(
"Sync error while waiting: {resp}\nProceeding to execute the command."
);
break;
} else if resp.starts_with("Sync completed succesfully:") {
// Sync finished; proceed
break;
} else if resp == "Sync task has not been launched." {
// Try to launch sync and continue waiting
command_transmitter
.send(("sync".to_string(), vec!["run".to_string()]))
.unwrap();
let _ = resp_receiver.recv();
thread::sleep(Duration::from_millis(500));
} else {
// Not ready yet
thread::sleep(Duration::from_millis(500));
// Parse JSON and inspect the numeric completion field.
match serde_json::from_str::<serde_json::Value>(&resp) {
Ok(json_val) => {
// if sync is complete, stop waiting
if let Some(true) =
json_val.get("sync_complete").and_then(|v| v.as_bool())
{
break;
}

// Not complete yet; wait a short interval before re-checking.
thread::sleep(Duration::from_millis(500));
continue;
}
Err(_) => {
// Parse error
break;
}
}
}
Err(_) => break,
Err(_) => {
// Channel closed; stop waiting.
break;
}
}
}
}
Expand Down