Skip to content

Commit

Permalink
zcash_client_sqlite: Ensure that previously-received change is correc…
Browse files Browse the repository at this point in the history
…tly flagged when recording sent outputs.

Fixes #1571
  • Loading branch information
nuttycom committed Oct 24, 2024
1 parent 72dad92 commit bca02d5
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions zcash_client_sqlite/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3221,9 +3221,38 @@ fn recipient_params<P: consensus::Parameters>(
}
}

fn flag_previously_received_change(
conn: &rusqlite::Transaction,
tx_ref: TxRef,
) -> Result<(), SqliteClientError> {
let flag_received_change = |table_prefix| {
conn.execute(
&format!(
"UPDATE {table_prefix}_received_notes
SET is_change = 1
FROM sent_notes sn
WHERE sn.tx = {table_prefix}_received_notes.tx
AND sn.tx = :tx
AND sn.from_account_id = {table_prefix}_received_notes.account_id
AND {table_prefix}_received_notes.recipient_key_scope = :internal_scope"
),
named_params! {
":tx": tx_ref.0,
":internal_scope": scope_code(Scope::Internal)
},
)
};

flag_received_change(SAPLING_TABLES_PREFIX)?;
#[cfg(feature = "orchard")]
flag_received_change(ORCHARD_TABLES_PREFIX)?;

Ok(())
}

/// Records information about a transaction output that your wallet created.
pub(crate) fn insert_sent_output<P: consensus::Parameters>(
conn: &rusqlite::Connection,
conn: &rusqlite::Transaction,
params: &P,
tx_ref: TxRef,
from_account: AccountId,
Expand Down Expand Up @@ -3251,6 +3280,7 @@ pub(crate) fn insert_sent_output<P: consensus::Parameters>(
];

stmt_insert_sent_output.execute(sql_args)?;
flag_previously_received_change(conn, tx_ref)?;

Ok(())
}
Expand All @@ -3268,7 +3298,7 @@ pub(crate) fn insert_sent_output<P: consensus::Parameters>(
/// the transaction.
#[allow(clippy::too_many_arguments)]
pub(crate) fn put_sent_output<P: consensus::Parameters>(
conn: &rusqlite::Connection,
conn: &rusqlite::Transaction,
params: &P,
from_account: AccountId,
tx_ref: TxRef,
Expand Down Expand Up @@ -3305,6 +3335,7 @@ pub(crate) fn put_sent_output<P: consensus::Parameters>(
];

stmt_upsert_sent_output.execute(sql_args)?;
flag_previously_received_change(conn, tx_ref)?;

Ok(())
}
Expand Down

0 comments on commit bca02d5

Please sign in to comment.