diff --git a/zcash_client_sqlite/src/wallet.rs b/zcash_client_sqlite/src/wallet.rs index 00618e9d1..43d8d7777 100644 --- a/zcash_client_sqlite/src/wallet.rs +++ b/zcash_client_sqlite/src/wallet.rs @@ -3223,9 +3223,38 @@ fn recipient_params( } } +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( - conn: &rusqlite::Connection, + conn: &rusqlite::Transaction, params: &P, tx_ref: TxRef, from_account: AccountId, @@ -3253,6 +3282,7 @@ pub(crate) fn insert_sent_output( ]; stmt_insert_sent_output.execute(sql_args)?; + flag_previously_received_change(conn, tx_ref)?; Ok(()) } @@ -3270,7 +3300,7 @@ pub(crate) fn insert_sent_output( /// the transaction. #[allow(clippy::too_many_arguments)] pub(crate) fn put_sent_output( - conn: &rusqlite::Connection, + conn: &rusqlite::Transaction, params: &P, from_account: AccountId, tx_ref: TxRef, @@ -3307,6 +3337,7 @@ pub(crate) fn put_sent_output( ]; stmt_upsert_sent_output.execute(sql_args)?; + flag_previously_received_change(conn, tx_ref)?; Ok(()) }