Skip to content

Commit

Permalink
fix(ARRR): store unconfirmed change output (#2276)
Browse files Browse the repository at this point in the history
  • Loading branch information
borngraced authored and shamardy committed Jan 30, 2025
1 parent 59154bd commit aba22e2
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 18 deletions.
14 changes: 7 additions & 7 deletions Cargo.lock

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

12 changes: 6 additions & 6 deletions mm2src/coins/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ uuid = { version = "1.2.2", features = ["fast-rng", "serde", "v4"] }
# We don't need the default web3 features at all since we added our own web3 transport using shared HYPER instance.
web3 = { git = "https://github.com/KomodoPlatform/rust-web3", tag = "v0.20.0", default-features = false }
zbase32 = "0.1.2"
zcash_client_backend = { git = "https://github.com/KomodoPlatform/librustzcash.git", tag = "k-1.4.1" }
zcash_extras = { git = "https://github.com/KomodoPlatform/librustzcash.git", tag = "k-1.4.1" }
zcash_primitives = {features = ["transparent-inputs"], git = "https://github.com/KomodoPlatform/librustzcash.git", tag = "k-1.4.1" }
zcash_client_backend = { git = "https://github.com/KomodoPlatform/librustzcash.git", tag = "k-1.4.2" }
zcash_extras = { git = "https://github.com/KomodoPlatform/librustzcash.git", tag = "k-1.4.2" }
zcash_primitives = {features = ["transparent-inputs"], git = "https://github.com/KomodoPlatform/librustzcash.git", tag = "k-1.4.2" }

[target.'cfg(target_arch = "wasm32")'.dependencies]
blake2b_simd = "0.5"
Expand All @@ -132,7 +132,7 @@ wasm-bindgen = "0.2.86"
wasm-bindgen-futures = { version = "0.4.1" }
wasm-bindgen-test = { version = "0.3.2" }
web-sys = { version = "0.3.55", features = ["console", "Headers", "Request", "RequestInit", "RequestMode", "Response", "Window"] }
zcash_proofs = { git = "https://github.com/KomodoPlatform/librustzcash.git", tag = "k-1.4.1", default-features = false, features = ["local-prover"] }
zcash_proofs = { git = "https://github.com/KomodoPlatform/librustzcash.git", tag = "k-1.4.2", default-features = false, features = ["local-prover"] }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
dirs = { version = "1" }
Expand All @@ -154,8 +154,8 @@ tokio = { version = "1.20" }
tokio-rustls = { version = "0.24" }
tonic = { version = "0.10", features = ["tls", "tls-webpki-roots", "gzip"] }
webpki-roots = { version = "0.25" }
zcash_client_sqlite = { git = "https://github.com/KomodoPlatform/librustzcash.git", tag = "k-1.4.1" }
zcash_proofs = { git = "https://github.com/KomodoPlatform/librustzcash.git", tag = "k-1.4.1", default-features = false, features = ["local-prover", "multicore"] }
zcash_client_sqlite = { git = "https://github.com/KomodoPlatform/librustzcash.git", tag = "k-1.4.2" }
zcash_proofs = { git = "https://github.com/KomodoPlatform/librustzcash.git", tag = "k-1.4.2", default-features = false, features = ["local-prover", "multicore"] }

[target.'cfg(windows)'.dependencies]
winapi = "0.3"
Expand Down
10 changes: 9 additions & 1 deletion mm2src/coins/z_coin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ use zcash_primitives::{constants::mainnet as z_mainnet_constants, sapling::Payme
zip32::ExtendedFullViewingKey, zip32::ExtendedSpendingKey};
use zcash_proofs::prover::LocalTxProver;

use self::storage::store_change_output;

cfg_native!(
use common::{async_blocking, sha256_digest};
use zcash_client_sqlite::error::SqliteClientError as ZcashClientError;
Expand Down Expand Up @@ -208,8 +210,8 @@ pub struct ZCoinFields {
z_tx_prover: Arc<LocalTxProver>,
light_wallet_db: WalletDbShared,
consensus_params: ZcoinConsensusParams,
sync_state_connector: AsyncMutex<SaplingSyncConnector>,
z_balance_event_handler: Option<ZBalanceEventHandler>,
sync_state_connector: AsyncMutex<SaplingSyncConnector>,
}

impl Transaction for ZTransaction {
Expand Down Expand Up @@ -474,6 +476,12 @@ impl ZCoin {
.await?
.tx_result?;

// Store any change outputs we created in this transaction by decrypting them with our keys
// and saving them to the wallet database for future spends
store_change_output(self.consensus_params_ref(), &self.z_fields.light_wallet_db, &tx)
.await
.map_to_mm(GenTxError::SaveChangeNotesError)?;

let additional_data = AdditionalTxData {
received_by_me,
spent_by_me: sat_from_big_decimal(&total_input_amount, self.decimals())?,
Expand Down
17 changes: 17 additions & 0 deletions mm2src/coins/z_coin/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ pub mod walletdb;
pub(crate) use z_params::ZcashParamsWasmImpl;

pub use walletdb::*;
use zcash_extras::wallet::decrypt_and_store_transaction;
use zcash_primitives::transaction::Transaction;

use crate::z_coin::z_balance_streaming::ZBalanceEventSender;
use mm2_err_handle::mm_error::MmResult;
Expand Down Expand Up @@ -190,3 +192,18 @@ pub async fn scan_cached_block(
// If there are any transactions in the block, return the transaction count
Ok(txs.len())
}

/// Processes and stores any change outputs created in the transaction by:
/// - Decrypting outputs using wallet viewing keys
/// - Adding decrypted change notes to the wallet database
/// - Making change notes available for future spends
pub(crate) async fn store_change_output(
params: &ZcoinConsensusParams,
shared_db: &WalletDbShared,
tx: &Transaction,
) -> Result<(), String> {
let mut data = try_s!(shared_db.db.get_update_ops());
try_s!(decrypt_and_store_transaction(params, &mut data, tx).await);

Ok(())
}
8 changes: 4 additions & 4 deletions mm2src/coins/z_coin/z_coin_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ pub enum GenTxError {
LightClientErr(String),
FailedToCreateNote,
SpendableNotesError(String),
#[cfg(target_arch = "wasm32")]
Internal(String),
SaveChangeNotesError(String),
}

impl From<GetUnspentWitnessErr> for GenTxError {
Expand Down Expand Up @@ -177,9 +177,9 @@ impl From<GenTxError> for WithdrawError {
| GenTxError::BlockchainScanStopped
| GenTxError::LightClientErr(_)
| GenTxError::SpendableNotesError(_)
| GenTxError::FailedToCreateNote => WithdrawError::InternalError(gen_tx.to_string()),
#[cfg(target_arch = "wasm32")]
GenTxError::Internal(_) => WithdrawError::InternalError(gen_tx.to_string()),
| GenTxError::FailedToCreateNote
| GenTxError::Internal(_)
| GenTxError::SaveChangeNotesError(_) => WithdrawError::InternalError(gen_tx.to_string()),
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions mm2src/coins/z_coin/z_htlc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// taker payment spend - https://zombie.explorer.lordofthechains.com/tx/af6bb0f99f9a5a070a0c1f53d69e4189b0e9b68f9d66e69f201a6b6d9f93897e
// maker payment spend - https://rick.explorer.dexstats.info/tx/6a2dcc866ad75cebecb780a02320073a88bcf5e57ddccbe2657494e7747d591e

use super::storage::store_change_output;
use super::{GenTxError, ZCoin};
use crate::utxo::rpc_clients::{UtxoRpcClientEnum, UtxoRpcError};
use crate::utxo::utxo_common::payment_script;
Expand Down Expand Up @@ -190,6 +191,12 @@ pub async fn z_p2sh_spend(
let mut tx_buffer = Vec::with_capacity(1024);
zcash_tx.write(&mut tx_buffer)?;

// Store any change outputs we created in this transaction by decrypting them with our keys
// and saving them to the wallet database for future spends
store_change_output(coin.consensus_params_ref(), &coin.z_fields.light_wallet_db, &zcash_tx)
.await
.map_to_mm(|err| ZP2SHSpendError::GenTxError(GenTxError::SaveChangeNotesError(err)))?;

coin.utxo_rpc_client()
.send_raw_transaction(tx_buffer.into())
.compat()
Expand Down

0 comments on commit aba22e2

Please sign in to comment.