Skip to content

Commit e453cee

Browse files
committed
zcash_client_sqlite: Extract common query from subtree_scan_progress
1 parent 1b5f4f4 commit e453cee

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

zcash_client_sqlite/src/wallet.rs

+17-20
Original file line numberDiff line numberDiff line change
@@ -839,21 +839,23 @@ fn subtree_scan_progress(
839839
fully_scanned_height: BlockHeight,
840840
chain_tip_height: BlockHeight,
841841
) -> Result<Option<Ratio<u64>>, SqliteClientError> {
842+
let mut stmt_scanned_count_from = conn.prepare_cached(&format!(
843+
"SELECT SUM({output_count_col})
844+
FROM blocks
845+
WHERE :start_height <= height",
846+
))?;
847+
842848
if fully_scanned_height == chain_tip_height {
843849
// Compute the total blocks scanned since the wallet birthday
844-
conn.query_row(
845-
&format!(
846-
"SELECT SUM({output_count_col})
847-
FROM blocks
848-
WHERE height >= :birthday_height",
849-
),
850-
named_params![":birthday_height": u32::from(birthday_height)],
851-
|row| {
852-
let scanned = row.get::<_, Option<u64>>(0)?;
853-
Ok(scanned.map(|n| Ratio::new(n, n)))
854-
},
855-
)
856-
.map_err(SqliteClientError::from)
850+
stmt_scanned_count_from
851+
.query_row(
852+
named_params![":start_height": u32::from(birthday_height)],
853+
|row| {
854+
let scanned = row.get::<_, Option<u64>>(0)?;
855+
Ok(scanned.map(|n| Ratio::new(n, n)))
856+
},
857+
)
858+
.map_err(SqliteClientError::from)
857859
} else {
858860
// Get the starting note commitment tree size from the wallet birthday, or failing that
859861
// from the blocks table.
@@ -887,13 +889,8 @@ fn subtree_scan_progress(
887889
.transpose()?;
888890

889891
// Compute the total blocks scanned so far above the starting height
890-
let scanned_count = conn.query_row(
891-
&format!(
892-
"SELECT SUM({output_count_col})
893-
FROM blocks
894-
WHERE height > :start_height",
895-
),
896-
named_params![":start_height": u32::from(birthday_height)],
892+
let scanned_count = stmt_scanned_count_from.query_row(
893+
named_params![":start_height": u32::from(birthday_height + 1)],
897894
|row| row.get::<_, Option<u64>>(0),
898895
)?;
899896

0 commit comments

Comments
 (0)