Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ARRR): store unconfirmed change output #2276

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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", branch = "add-change-to-unspent" }
zcash_extras = { git = "https://github.com/KomodoPlatform/librustzcash.git", branch = "add-change-to-unspent" }
zcash_primitives = {features = ["transparent-inputs"], git = "https://github.com/KomodoPlatform/librustzcash.git", branch = "add-change-to-unspent" }

[target.'cfg(target_arch = "wasm32")'.dependencies]
blake2b_simd = "0.5"
Expand All @@ -131,7 +131,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", branch = "add-change-to-unspent", default-features = false, features = ["local-prover"] }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
dirs = { version = "1" }
Expand All @@ -152,8 +152,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", branch = "add-change-to-unspent" }
zcash_proofs = { git = "https://github.com/KomodoPlatform/librustzcash.git", branch = "add-change-to-unspent", 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
.mm_err(GenTxError::Internal)?;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't have to be internal error as it can be due scanning not done yet.


let additional_data = AdditionalTxData {
received_by_me,
spent_by_me: sat_from_big_decimal(&total_input_amount, self.decimals())?,
Expand Down
20 changes: 20 additions & 0 deletions mm2src/coins/z_coin/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ pub use blockdb::*;

pub mod walletdb;
#[cfg(target_arch = "wasm32")] mod z_params;
use mm2_err_handle::prelude::MapToMmResult;
#[cfg(target_arch = "wasm32")]
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 +193,20 @@ 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,
) -> MmResult<(), String> {
let mut data = shared_db.db.get_update_ops().map_to_mm(|err| err.to_string())?;
decrypt_and_store_transaction(params, &mut data, tx)
.await
.map_to_mm(|err| err.to_string())?;

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

Expand Down Expand Up @@ -178,7 +177,6 @@ impl From<GenTxError> for WithdrawError {
| GenTxError::LightClientErr(_)
| GenTxError::SpendableNotesError(_)
| GenTxError::FailedToCreateNote => WithdrawError::InternalError(gen_tx.to_string()),
#[cfg(target_arch = "wasm32")]
GenTxError::Internal(_) => 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
.mm_err(|err| ZP2SHSpendError::GenTxError(GenTxError::Internal(err)))?;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.


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