zcash_client_sqlite: Ensure that the shard at the chain tip is complete to anchor height.

If the tip shard has any un-scanned range below the anchor height, we
can't compute witnesses for any of our notes.
This commit is contained in:
Kris Nuttycombe 2023-08-09 15:51:02 -06:00
parent 50ea2a5b0f
commit 0e7ee0ebd7
1 changed files with 26 additions and 0 deletions

View File

@ -132,6 +132,19 @@ pub(crate) fn get_spendable_sapling_notes(
anchor_height: BlockHeight,
exclude: &[ReceivedNoteId],
) -> Result<Vec<ReceivedSaplingNote<ReceivedNoteId>>, SqliteClientError> {
let mut stmt_unscanned_tip = conn.prepare_cached(
"SELECT 1 FROM v_sapling_shard_unscanned_ranges
WHERE :anchor_height BETWEEN subtree_start_height AND IFNULL(subtree_end_height, :anchor_height)
AND block_range_start <= :anchor_height",
)?;
let mut unscanned =
stmt_unscanned_tip.query(named_params![":anchor_height": &u32::from(anchor_height),])?;
if unscanned.next()?.is_some() {
// if the tip shard has unscanned ranges below the anchor height, none of our notes can be
// spent
return Ok(vec![]);
}
let mut stmt_select_notes = conn.prepare_cached(
"SELECT id_note, diversifier, value, rcm, commitment_tree_position
FROM sapling_received_notes
@ -172,6 +185,19 @@ pub(crate) fn select_spendable_sapling_notes(
anchor_height: BlockHeight,
exclude: &[ReceivedNoteId],
) -> Result<Vec<ReceivedSaplingNote<ReceivedNoteId>>, SqliteClientError> {
let mut stmt_unscanned_tip = conn.prepare_cached(
"SELECT 1 FROM v_sapling_shard_unscanned_ranges
WHERE :anchor_height BETWEEN subtree_start_height AND IFNULL(subtree_end_height, :anchor_height)
AND block_range_start <= :anchor_height",
)?;
let mut unscanned =
stmt_unscanned_tip.query(named_params![":anchor_height": &u32::from(anchor_height),])?;
if unscanned.next()?.is_some() {
// if the tip shard has unscanned ranges below the anchor height, none of our notes can be
// spent
return Ok(vec![]);
}
// The goal of this SQL statement is to select the oldest notes until the required
// value has been reached.
// 1) Use a window function to create a view of all notes, ordered from oldest to