Skip to content

Commit

Permalink
don't force nullifier if tx isn't mined yet
Browse files Browse the repository at this point in the history
  • Loading branch information
borngraced committed Jan 29, 2025
1 parent 1416071 commit 36f7977
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 158 deletions.
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.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" }
zcash_client_backend = { path = "../../../librustzcash/zcash_client_backend" }
zcash_extras = { path = "../../../librustzcash/zcash_extras" }
zcash_primitives = {features = ["transparent-inputs"], path = "../../../librustzcash/zcash_primitives"}

[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.2", default-features = false, features = ["local-prover"] }
zcash_proofs = { path = "../../../librustzcash/zcash_proofs", 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.2" }
zcash_proofs = { git = "https://github.com/KomodoPlatform/librustzcash.git", tag = "k-1.4.2", default-features = false, features = ["local-prover", "multicore"] }
zcash_client_sqlite = { path = "../../../librustzcash/zcash_client_sqlite" }
zcash_proofs = { path = "../../../librustzcash/zcash_proofs/" , default-features = false, features = ["local-prover", "multicore"] }

[target.'cfg(windows)'.dependencies]
winapi = "0.3"
Expand Down
294 changes: 156 additions & 138 deletions mm2src/coins/z_coin/storage/walletdb/wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1072,142 +1072,160 @@ mod wasm_test {

// Todo: Uncomment after improving tx creation time
// https://github.com/KomodoPlatform/komodo-defi-framework/issues/2000
// #[wasm_bindgen_test]
// async fn test_create_to_address_fails_on_locked_notes() {
// register_wasm_log();
//
// // init blocks_db
// let ctx = mm_ctx_with_custom_db();
// let blockdb = BlockDbImpl::new(&ctx, TICKER.to_string(), PathBuf::new()).await.unwrap();
//
// // init walletdb.
// let mut walletdb = wallet_db_from_zcoin_builder_for_test(&ctx, TICKER).await;
// let consensus_params = consensus_params();
//
// // Add an account to the wallet
// let extsk = ExtendedSpendingKey::master(&[]);
// let extfvk = ExtendedFullViewingKey::from(&extsk);
// assert!(walletdb.init_accounts_table(&[extfvk.clone()]).await.is_ok());
//
// // Add funds to the wallet in a single note
// let value = Amount::from_u64(50000).unwrap();
// let (cb, _) = fake_compact_block(sapling_activation_height(), BlockHash([0; 32]), extfvk, value);
// let cb_bytes = cb.write_to_bytes().unwrap();
// blockdb.insert_block(cb.height as u32, cb_bytes).await.unwrap();
//
// // Scan the cache
// let scan = DataConnStmtCacheWrapper::new(DataConnStmtCacheWasm(walletdb.clone()));
// blockdb
// .process_blocks_with_mode(consensus_params.clone(), BlockProcessingMode::Scan(scan, None), None, None)
// .await
// .unwrap();
// assert_eq!(walletdb.get_balance(AccountId(0)).await.unwrap(), value);
//
// // Send some of the funds to another address
// let extsk2 = ExtendedSpendingKey::master(&[]);
// let to = extsk2.default_address().unwrap().1.into();
// create_spend_to_address(
// &mut walletdb,
// &network(),
// test_prover().await,
// AccountId(0),
// &extsk,
// &to,
// Amount::from_u64(15000).unwrap(),
// None,
// OvkPolicy::Sender,
// )
// .await
// .unwrap();
//
// // A second spend fails because there are no usable notes
// match create_spend_to_address(
// &mut walletdb,
// &network(),
// test_prover().await,
// AccountId(0),
// &extsk,
// &to,
// Amount::from_u64(2000).unwrap(),
// None,
// OvkPolicy::Sender,
// )
// .await
// {
// Ok(_) => panic!("Should have failed"),
// Err(e) => assert!(e
// .to_string()
// .contains("Insufficient balance (have 0, need 3000 including fee)")),
// }
//
// // Mine blocks SAPLING_ACTIVATION_HEIGHT + 1 to 21 (that don't send us funds)
// // until just before the first transaction expires
// for i in 1..22 {
// let (cb, _) = fake_compact_block(
// sapling_activation_height() + i,
// cb.hash(),
// ExtendedFullViewingKey::from(&ExtendedSpendingKey::master(&[i as u8])),
// value,
// );
// let cb_bytes = cb.write_to_bytes().unwrap();
// blockdb.insert_block(cb.height as u32, cb_bytes).await.unwrap();
// }
// // Scan the cache
// let scan = DataConnStmtCacheWrapper::new(DataConnStmtCacheWasm(walletdb.clone()));
// blockdb
// .process_blocks_with_mode(consensus_params.clone(), BlockProcessingMode::Scan(scan, None), None, None)
// .await
// .unwrap();
//
// // Second spend still fails
// match create_spend_to_address(
// &mut walletdb,
// &network(),
// test_prover().await,
// AccountId(0),
// &extsk,
// &to,
// Amount::from_u64(2000).unwrap(),
// None,
// OvkPolicy::Sender,
// )
// .await
// {
// Ok(_) => panic!("Should have failed"),
// Err(e) => assert!(e
// .to_string()
// .contains("Insufficient balance (have 0, need 3000 including fee)")),
// }
//
// // Mine block SAPLING_ACTIVATION_HEIGHT + 22 so that the first transaction expires
// let (cb, _) = fake_compact_block(
// sapling_activation_height() + 22,
// cb.hash(),
// ExtendedFullViewingKey::from(&ExtendedSpendingKey::master(&[22])),
// value,
// );
// let cb_bytes = cb.write_to_bytes().unwrap();
// blockdb.insert_block(cb.height as u32, cb_bytes).await.unwrap();
// // Scan the cache
// let scan = DataConnStmtCacheWrapper::new(DataConnStmtCacheWasm(walletdb.clone()));
// blockdb
// .process_blocks_with_mode(consensus_params.clone(), BlockProcessingMode::Scan(scan, None), None, None)
// .await
// .unwrap();
//
// // Second spend should now succeed
// create_spend_to_address(
// &mut walletdb,
// &network(),
// test_prover().await,
// AccountId(0),
// &extsk,
// &to,
// Amount::from_u64(2000).unwrap(),
// None,
// OvkPolicy::Sender,
// )
// .await
// .unwrap();
// }
#[wasm_bindgen_test]
async fn test_create_to_address_fails_on_locked_notes() {
common::log::wasm_log::register_wasm_log();

// init blocks_db
let ctx = mm_ctx_with_custom_db();
let blockdb = BlockDbImpl::new(&ctx, TICKER.to_string(), PathBuf::new())
.await
.unwrap();

// init walletdb.
let mut walletdb = wallet_db_from_zcoin_builder_for_test(&ctx, TICKER).await;
let consensus_params = consensus_params();

// Add an account to the wallet
let extsk = ExtendedSpendingKey::master(&[]);
let extfvk = ExtendedFullViewingKey::from(&extsk);
assert!(walletdb.init_accounts_table(&[extfvk.clone()]).await.is_ok());

// Add funds to the wallet in a single note
let value = Amount::from_u64(50000).unwrap();
let (cb, _) = fake_compact_block(sapling_activation_height(), BlockHash([0; 32]), extfvk, value);
let cb_bytes = cb.write_to_bytes().unwrap();
blockdb.insert_block(cb.height as u32, cb_bytes).await.unwrap();

// Scan the cache
let scan = DataConnStmtCacheWrapper::new(DataConnStmtCacheWasm(walletdb.clone()));
blockdb
.process_blocks_with_mode(
consensus_params.clone(),
BlockProcessingMode::Scan(scan, None),
None,
None,
)
.await
.unwrap();
assert_eq!(walletdb.get_balance(AccountId(0)).await.unwrap(), value);

// Send some of the funds to another address
let extsk2 = ExtendedSpendingKey::master(&[]);
let to = extsk2.default_address().unwrap().1.into();
common::log::info!("about to create net to spend");
create_spend_to_address(
&mut walletdb,
&network(),
test_prover().await,
AccountId(0),
&extsk,
&to,
Amount::from_u64(15000).unwrap(),
None,
OvkPolicy::Sender,
)
.await
.unwrap();

// A second spend fails because there are no usable notes
match create_spend_to_address(
&mut walletdb,
&network(),
test_prover().await,
AccountId(0),
&extsk,
&to,
Amount::from_u64(2000).unwrap(),
None,
OvkPolicy::Sender,
)
.await
{
Ok(_) => panic!("Should have failed"),
Err(e) => assert!(e
.to_string()
.contains("Insufficient balance (have 0, need 3000 including fee)")),
}

// Mine blocks SAPLING_ACTIVATION_HEIGHT + 1 to 21 (that don't send us funds)
// until just before the first transaction expires
for i in 1..22 {
let (cb, _) = fake_compact_block(
sapling_activation_height() + i,
cb.hash(),
ExtendedFullViewingKey::from(&ExtendedSpendingKey::master(&[i as u8])),
value,
);
let cb_bytes = cb.write_to_bytes().unwrap();
blockdb.insert_block(cb.height as u32, cb_bytes).await.unwrap();
}
// Scan the cache
let scan = DataConnStmtCacheWrapper::new(DataConnStmtCacheWasm(walletdb.clone()));
blockdb
.process_blocks_with_mode(
consensus_params.clone(),
BlockProcessingMode::Scan(scan, None),
None,
None,
)
.await
.unwrap();

// Second spend still fails
match create_spend_to_address(
&mut walletdb,
&network(),
test_prover().await,
AccountId(0),
&extsk,
&to,
Amount::from_u64(2000).unwrap(),
None,
OvkPolicy::Sender,
)
.await
{
Ok(_) => panic!("Should have failed"),
Err(e) => assert!(e
.to_string()
.contains("Insufficient balance (have 0, need 3000 including fee)")),
}

// Mine block SAPLING_ACTIVATION_HEIGHT + 22 so that the first transaction expires
let (cb, _) = fake_compact_block(
sapling_activation_height() + 22,
cb.hash(),
ExtendedFullViewingKey::from(&ExtendedSpendingKey::master(&[22])),
value,
);
let cb_bytes = cb.write_to_bytes().unwrap();
blockdb.insert_block(cb.height as u32, cb_bytes).await.unwrap();
// Scan the cache
let scan = DataConnStmtCacheWrapper::new(DataConnStmtCacheWasm(walletdb.clone()));
blockdb
.process_blocks_with_mode(
consensus_params.clone(),
BlockProcessingMode::Scan(scan, None),
None,
None,
)
.await
.unwrap();

// Second spend should now succeed
create_spend_to_address(
&mut walletdb,
&network(),
test_prover().await,
AccountId(0),
&extsk,
&to,
Amount::from_u64(2000).unwrap(),
None,
OvkPolicy::Sender,
)
.await
.unwrap();
}
}
14 changes: 1 addition & 13 deletions mm2src/coins/z_coin/storage/walletdb/wasm/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1083,7 +1083,7 @@ impl WalletRead for WalletIndexedDb {
let matching_tx = maybe_txs.iter().find(|(id_tx, _tx)| id_tx.to_bigint() == note.spent);

if let Some((_, tx)) = matching_tx {
if tx.block.is_none() {
if tx.block.is_some() {
nullifiers.push((
AccountId(
note.account
Expand All @@ -1096,18 +1096,6 @@ impl WalletRead for WalletIndexedDb {
.unwrap(),
));
}
} else {
nullifiers.push((
AccountId(
note.account
.to_u32()
.ok_or_else(|| ZcoinStorageError::GetFromStorageError("Invalid amount".to_string()))?,
),
Nullifier::from_slice(&note.nf.clone().ok_or_else(|| {
ZcoinStorageError::GetFromStorageError("Error while putting tx_meta".to_string())
})?)
.unwrap(),
));
}
}

Expand Down
2 changes: 1 addition & 1 deletion mm2src/mm2_main/src/lp_ordermatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3280,7 +3280,6 @@ pub async fn lp_ordermatch_loop(ctx: MmArc) {

{
let mut missing_uuids = Vec::new();
let mut to_cancel = Vec::new();
{
let orderbook = ordermatch_ctx.orderbook.lock();
for (uuid, _) in ordermatch_ctx.maker_orders_ctx.lock().orders.iter() {
Expand All @@ -3290,6 +3289,7 @@ pub async fn lp_ordermatch_loop(ctx: MmArc) {
}
}

let mut to_cancel = Vec::new();
for uuid in missing_uuids {
let order_mutex = match ordermatch_ctx.maker_orders_ctx.lock().get_order(&uuid) {
Some(o) => o.clone(),
Expand Down

0 comments on commit 36f7977

Please sign in to comment.