diff --git a/key-wallet-ffi/Cargo.toml b/key-wallet-ffi/Cargo.toml index 5148da999..23edb7c31 100644 --- a/key-wallet-ffi/Cargo.toml +++ b/key-wallet-ffi/Cargo.toml @@ -33,5 +33,6 @@ hex = "0.4" cbindgen = "0.29" [dev-dependencies] +key-wallet = { path = "../key-wallet", default-features = false, features = ["std", "test-utils"] } tempfile = "3.0" hex = "0.4" diff --git a/key-wallet-ffi/src/utxo_tests.rs b/key-wallet-ffi/src/utxo_tests.rs index 66d9f5288..422c71b4a 100644 --- a/key-wallet-ffi/src/utxo_tests.rs +++ b/key-wallet-ffi/src/utxo_tests.rs @@ -3,6 +3,7 @@ mod utxo_tests { use super::super::*; use crate::error::{FFIError, FFIErrorCode}; use key_wallet::managed_account::managed_account_type::ManagedAccountType; + use key_wallet::Utxo; use std::ffi::CStr; use std::ptr; @@ -278,11 +279,8 @@ mod utxo_tests { #[test] fn test_managed_wallet_get_utxos_multiple_accounts() { use crate::managed_wallet::FFIManagedWalletInfo; - use dashcore::blockdata::script::ScriptBuf; - use dashcore::{Address, OutPoint, TxOut, Txid}; use key_wallet::account::account_type::StandardAccountType; use key_wallet::managed_account::ManagedAccount; - use key_wallet::utxo::Utxo; use key_wallet::wallet::managed_wallet_info::ManagedWalletInfo; use key_wallet::Network; @@ -313,21 +311,9 @@ mod utxo_tests { false, ); - for i in 0..2 { - let outpoint = OutPoint { - txid: Txid::from([i as u8; 32]), - vout: i as u32, - }; - let txout = TxOut { - value: 10000, - script_pubkey: ScriptBuf::from(vec![]), - }; - // Create a dummy P2PKH address - let dummy_pubkey_hash = dashcore::PubkeyHash::from([0u8; 20]); - let script = ScriptBuf::new_p2pkh(&dummy_pubkey_hash); - let address = Address::from_script(&script, Network::Testnet).unwrap(); - let utxo = Utxo::new(outpoint, txout, address, 100, false); - bip44_account.utxos.insert(outpoint, utxo); + let utxos = Utxo::new_test_batch(0..2, 10000, 100, false, false); + for utxo in utxos { + bip44_account.utxos.insert(utxo.outpoint, utxo); } managed_info.accounts.insert(bip44_account); @@ -351,20 +337,10 @@ mod utxo_tests { false, ); - let outpoint = OutPoint { - txid: Txid::from([10u8; 32]), - vout: 0, - }; - let txout = TxOut { - value: 20000, - script_pubkey: ScriptBuf::from(vec![]), - }; - // Create a dummy P2PKH address - let dummy_pubkey_hash = dashcore::PubkeyHash::from([0u8; 20]); - let script = ScriptBuf::new_p2pkh(&dummy_pubkey_hash); - let address = Address::from_script(&script, Network::Testnet).unwrap(); - let utxo = Utxo::new(outpoint, txout, address, 200, false); - bip32_account.utxos.insert(outpoint, utxo); + let utxos = Utxo::new_test_batch(10..11, 20000, 200, false, false); + for utxo in utxos { + bip32_account.utxos.insert(utxo.outpoint, utxo); + } managed_info.accounts.insert(bip32_account); // Create CoinJoin account with 2 UTXOs @@ -380,21 +356,9 @@ mod utxo_tests { false, ); - for i in 0..2 { - let outpoint = OutPoint { - txid: Txid::from([(20 + i) as u8; 32]), - vout: i as u32, - }; - let txout = TxOut { - value: 30000, - script_pubkey: ScriptBuf::from(vec![]), - }; - // Create a dummy P2PKH address - let dummy_pubkey_hash = dashcore::PubkeyHash::from([0u8; 20]); - let script = ScriptBuf::new_p2pkh(&dummy_pubkey_hash); - let address = Address::from_script(&script, Network::Testnet).unwrap(); - let utxo = Utxo::new(outpoint, txout, address, 300, false); - coinjoin_account.utxos.insert(outpoint, utxo); + let utxos = Utxo::new_test_batch(20..22, 30000, 300, false, false); + for utxo in utxos { + coinjoin_account.utxos.insert(utxo.outpoint, utxo); } managed_info.accounts.insert(coinjoin_account); @@ -418,11 +382,8 @@ mod utxo_tests { #[test] fn test_managed_wallet_get_utxos() { use crate::managed_wallet::FFIManagedWalletInfo; - use dashcore::blockdata::script::ScriptBuf; - use dashcore::{Address, OutPoint, TxOut, Txid}; use key_wallet::account::account_type::StandardAccountType; use key_wallet::managed_account::ManagedAccount; - use key_wallet::utxo::Utxo; use key_wallet::wallet::managed_wallet_info::ManagedWalletInfo; use key_wallet::Network; @@ -454,20 +415,10 @@ mod utxo_tests { false, ); - let outpoint = OutPoint { - txid: Txid::from([1u8; 32]), - vout: 0, - }; - let txout = TxOut { - value: 10000, - script_pubkey: ScriptBuf::from(vec![]), - }; - // Create a dummy P2PKH address - let dummy_pubkey_hash = dashcore::PubkeyHash::from([0u8; 20]); - let script = ScriptBuf::new_p2pkh(&dummy_pubkey_hash); - let address = Address::from_script(&script, Network::Testnet).unwrap(); - let utxo = Utxo::new(outpoint, txout, address, 100, false); - testnet_account.utxos.insert(outpoint, utxo); + let utxos = Utxo::new_test_batch(1..2, 10000, 100, false, false); + for utxo in utxos { + testnet_account.utxos.insert(utxo.outpoint, utxo); + } managed_info.accounts.insert(testnet_account); let ffi_managed_info = Box::into_raw(Box::new(FFIManagedWalletInfo::new(managed_info))); diff --git a/key-wallet/Cargo.toml b/key-wallet/Cargo.toml index 9f2306c0e..520f2bcdd 100644 --- a/key-wallet/Cargo.toml +++ b/key-wallet/Cargo.toml @@ -16,6 +16,7 @@ bincode = ["serde", "dep:bincode", "dep:bincode_derive", "dash-network/bincode", bip38 = ["scrypt", "aes", "bs58", "rand"] eddsa = ["dashcore/eddsa"] bls = ["dashcore/bls"] +test-utils = [] [dependencies] internals = { path = "../internals", package = "dashcore-private" } @@ -47,5 +48,5 @@ async-trait = "0.1" [dev-dependencies] hex = "0.4" -key-wallet = { path = ".", features = ["bip38", "serde", "bincode", "eddsa", "bls"] } +key-wallet = { path = ".", features = ["test-utils", "bip38", "serde", "bincode", "eddsa", "bls"] } tokio = { version = "1", features = ["macros", "rt"] } diff --git a/key-wallet/src/lib.rs b/key-wallet/src/lib.rs index 08e957229..f93661977 100644 --- a/key-wallet/src/lib.rs +++ b/key-wallet/src/lib.rs @@ -12,6 +12,9 @@ extern crate core; #[cfg(feature = "std")] extern crate std; +#[cfg(any(test, feature = "test-utils"))] +pub mod test_utils; + #[cfg(test)] #[macro_use] mod test_macros; diff --git a/key-wallet/src/test_utils/address.rs b/key-wallet/src/test_utils/address.rs new file mode 100644 index 000000000..aa14eff0e --- /dev/null +++ b/key-wallet/src/test_utils/address.rs @@ -0,0 +1,11 @@ +use dashcore::{Address, Network}; + +const TEST_PUBKEY_BYTES: [u8; 33] = [ + 0x02, 0x50, 0x86, 0x3a, 0xd6, 0x4a, 0x87, 0xae, 0x8a, 0x2f, 0xe8, 0x3c, 0x1a, 0xf1, 0xa8, 0x40, + 0x3c, 0xb5, 0x3f, 0x53, 0xe4, 0x86, 0xd8, 0x51, 0x1d, 0xad, 0x8a, 0x04, 0x88, 0x7e, 0x5b, 0x23, + 0x52, +]; + +pub fn test_address() -> Address { + Address::p2pkh(&dashcore::PublicKey::from_slice(&TEST_PUBKEY_BYTES).unwrap(), Network::Testnet) +} diff --git a/key-wallet/src/test_utils/mod.rs b/key-wallet/src/test_utils/mod.rs new file mode 100644 index 000000000..ad2592879 --- /dev/null +++ b/key-wallet/src/test_utils/mod.rs @@ -0,0 +1,4 @@ +mod address; +mod utxo; + +pub use address::*; diff --git a/key-wallet/src/test_utils/utxo.rs b/key-wallet/src/test_utils/utxo.rs new file mode 100644 index 000000000..1a6faa2f7 --- /dev/null +++ b/key-wallet/src/test_utils/utxo.rs @@ -0,0 +1,35 @@ +use std::ops::Range; + +use dashcore::{OutPoint, ScriptBuf, TxOut, Txid}; + +use crate::{test_utils::test_address, Utxo}; + +impl Utxo { + pub fn new_test(id: u8, value: u64, height: u32, coinbase: bool, confirmed: bool) -> Self { + Self::new_test_batch(id..id + 1, value, height, coinbase, confirmed).remove(0) + } + + pub fn new_test_batch( + ids_range: Range, + value: u64, + height: u32, + coinbase: bool, + confirmed: bool, + ) -> Vec { + ids_range + .enumerate() + .map(|(i, id)| { + let outpoint = OutPoint::new(Txid::from([id; 32]), i as u32); + + let txout = TxOut { + value, + script_pubkey: ScriptBuf::new(), + }; + + let mut utxo = Utxo::new(outpoint, txout, test_address(), height, coinbase); + utxo.is_confirmed = confirmed; + utxo + }) + .collect() + } +} diff --git a/key-wallet/src/utxo.rs b/key-wallet/src/utxo.rs index ac607c922..8b187aca7 100644 --- a/key-wallet/src/utxo.rs +++ b/key-wallet/src/utxo.rs @@ -307,42 +307,10 @@ impl Default for UtxoSet { #[cfg(test)] mod tests { use super::*; - use crate::Network; - use dashcore::blockdata::script::ScriptBuf; - use dashcore::Txid; - use dashcore_hashes::{sha256d, Hash}; - - fn test_utxo(value: u64, height: u32) -> Utxo { - test_utxo_with_vout(value, height, 0) - } - - fn test_utxo_with_vout(value: u64, height: u32, vout: u32) -> Utxo { - let outpoint = OutPoint { - txid: Txid::from_raw_hash(sha256d::Hash::from_slice(&[1u8; 32]).unwrap()), - vout, - }; - - let txout = TxOut { - value, - script_pubkey: ScriptBuf::new(), - }; - - let address = Address::p2pkh( - &dashcore::PublicKey::from_slice(&[ - 0x02, 0x50, 0x86, 0x3a, 0xd6, 0x4a, 0x87, 0xae, 0x8a, 0x2f, 0xe8, 0x3c, 0x1a, 0xf1, - 0xa8, 0x40, 0x3c, 0xb5, 0x3f, 0x53, 0xe4, 0x86, 0xd8, 0x51, 0x1d, 0xad, 0x8a, 0x04, - 0x88, 0x7e, 0x5b, 0x23, 0x52, - ]) - .unwrap(), - Network::Testnet, - ); - - Utxo::new(outpoint, txout, address, height, false) - } #[test] fn test_utxo_spendability() { - let mut utxo = test_utxo(100000, 100); + let mut utxo = Utxo::new_test(0, 100000, 100, false, false); // Unconfirmed UTXO should not be spendable assert!(!utxo.is_spendable(200)); @@ -360,8 +328,8 @@ mod tests { fn test_utxo_set_operations() { let mut set = UtxoSet::new(); - let utxo1 = test_utxo_with_vout(100000, 100, 0); - let utxo2 = test_utxo_with_vout(200000, 150, 1); // Different vout to ensure unique OutPoint + let utxo1 = Utxo::new_test(0, 100000, 100, false, false); + let utxo2 = Utxo::new_test(1, 200000, 150, false, false); set.add(utxo1.clone()); set.add(utxo2.clone()); diff --git a/key-wallet/src/wallet/managed_wallet_info/coin_selection.rs b/key-wallet/src/wallet/managed_wallet_info/coin_selection.rs index b83bfe5a7..4432cf8e6 100644 --- a/key-wallet/src/wallet/managed_wallet_info/coin_selection.rs +++ b/key-wallet/src/wallet/managed_wallet_info/coin_selection.rs @@ -690,44 +690,14 @@ impl std::error::Error for SelectionError {} #[cfg(test)] mod tests { use super::*; - use crate::Utxo; - use dashcore::blockdata::script::ScriptBuf; - use dashcore::{Address, Network, OutPoint, TxOut, Txid}; - use dashcore_hashes::{sha256d, Hash}; - - fn test_utxo(value: u64, confirmed: bool) -> Utxo { - let outpoint = OutPoint { - txid: Txid::from_raw_hash(sha256d::Hash::from_slice(&[1u8; 32]).unwrap()), - vout: 0, - }; - - let txout = TxOut { - value, - script_pubkey: ScriptBuf::new(), - }; - - let address = Address::p2pkh( - &dashcore::PublicKey::from_slice(&[ - 0x02, 0x50, 0x86, 0x3a, 0xd6, 0x4a, 0x87, 0xae, 0x8a, 0x2f, 0xe8, 0x3c, 0x1a, 0xf1, - 0xa8, 0x40, 0x3c, 0xb5, 0x3f, 0x53, 0xe4, 0x86, 0xd8, 0x51, 0x1d, 0xad, 0x8a, 0x04, - 0x88, 0x7e, 0x5b, 0x23, 0x52, - ]) - .unwrap(), - Network::Testnet, - ); - - let mut utxo = Utxo::new(outpoint, txout, address, 100, false); - utxo.is_confirmed = confirmed; - utxo - } #[test] fn test_smallest_first_selection() { let utxos = vec![ - test_utxo(10000, true), - test_utxo(20000, true), - test_utxo(30000, true), - test_utxo(40000, true), + Utxo::new_test(0, 10000, 100, false, true), + Utxo::new_test(0, 20000, 100, false, true), + Utxo::new_test(0, 30000, 100, false, true), + Utxo::new_test(0, 40000, 100, false, true), ]; let selector = CoinSelector::new(SelectionStrategy::SmallestFirst); @@ -742,10 +712,10 @@ mod tests { #[test] fn test_largest_first_selection() { let utxos = vec![ - test_utxo(10000, true), - test_utxo(20000, true), - test_utxo(30000, true), - test_utxo(40000, true), + Utxo::new_test(0, 10000, 100, false, true), + Utxo::new_test(0, 20000, 100, false, true), + Utxo::new_test(0, 30000, 100, false, true), + Utxo::new_test(0, 40000, 100, false, true), ]; let selector = CoinSelector::new(SelectionStrategy::LargestFirst); @@ -758,7 +728,10 @@ mod tests { #[test] fn test_insufficient_funds() { - let utxos = vec![test_utxo(10000, true), test_utxo(20000, true)]; + let utxos = vec![ + Utxo::new_test(0, 10000, 100, false, true), + Utxo::new_test(0, 20000, 100, false, true), + ]; let selector = CoinSelector::new(SelectionStrategy::LargestFirst); let result = selector.select_coins(&utxos, 50000, FeeRate::new(1000), 200); @@ -770,12 +743,12 @@ mod tests { fn test_optimal_consolidation_strategy() { // Test that OptimalConsolidation strategy works correctly let utxos = vec![ - test_utxo(100, true), - test_utxo(200, true), - test_utxo(300, true), - test_utxo(500, true), - test_utxo(1000, true), - test_utxo(2000, true), + Utxo::new_test(0, 100, 100, false, true), + Utxo::new_test(0, 200, 100, false, true), + Utxo::new_test(0, 300, 100, false, true), + Utxo::new_test(0, 500, 100, false, true), + Utxo::new_test(0, 1000, 100, false, true), + Utxo::new_test(0, 2000, 100, false, true), ]; let selector = CoinSelector::new(SelectionStrategy::OptimalConsolidation); diff --git a/key-wallet/src/wallet/managed_wallet_info/transaction_builder.rs b/key-wallet/src/wallet/managed_wallet_info/transaction_builder.rs index 9b56466d6..c2c497732 100644 --- a/key-wallet/src/wallet/managed_wallet_info/transaction_builder.rs +++ b/key-wallet/src/wallet/managed_wallet_info/transaction_builder.rs @@ -835,54 +835,15 @@ impl std::error::Error for BuilderError {} #[cfg(test)] mod tests { use super::*; + use crate::test_utils::test_address; use crate::Network; - use dashcore::blockdata::script::ScriptBuf; use dashcore::blockdata::transaction::special_transaction::asset_lock::AssetLockPayload; - use dashcore::{OutPoint, TxOut, Txid}; use dashcore_hashes::{sha256d, Hash}; use hex; - fn test_utxo(value: u64) -> Utxo { - let outpoint = OutPoint { - txid: Txid::from_raw_hash(sha256d::Hash::from_slice(&[1u8; 32]).unwrap()), - vout: 0, - }; - - let txout = TxOut { - value, - script_pubkey: ScriptBuf::new(), - }; - - let address = Address::p2pkh( - &dashcore::PublicKey::from_slice(&[ - 0x02, 0x50, 0x86, 0x3a, 0xd6, 0x4a, 0x87, 0xae, 0x8a, 0x2f, 0xe8, 0x3c, 0x1a, 0xf1, - 0xa8, 0x40, 0x3c, 0xb5, 0x3f, 0x53, 0xe4, 0x86, 0xd8, 0x51, 0x1d, 0xad, 0x8a, 0x04, - 0x88, 0x7e, 0x5b, 0x23, 0x52, - ]) - .unwrap(), - Network::Testnet, - ); - - let mut utxo = Utxo::new(outpoint, txout, address, 100, false); - utxo.is_confirmed = true; - utxo - } - - fn test_address() -> Address { - Address::p2pkh( - &dashcore::PublicKey::from_slice(&[ - 0x03, 0x50, 0x86, 0x3a, 0xd6, 0x4a, 0x87, 0xae, 0x8a, 0x2f, 0xe8, 0x3c, 0x1a, 0xf1, - 0xa8, 0x40, 0x3c, 0xb5, 0x3f, 0x53, 0xe4, 0x86, 0xd8, 0x51, 0x1d, 0xad, 0x8a, 0x04, - 0x88, 0x7e, 0x5b, 0x23, 0x52, - ]) - .unwrap(), - Network::Testnet, - ) - } - #[test] fn test_transaction_builder_basic() { - let utxo = test_utxo(100000); + let utxo = Utxo::new_test(0, 100000, 100, false, true); let destination = test_address(); let change = test_address(); @@ -901,7 +862,7 @@ mod tests { #[test] fn test_insufficient_funds() { - let utxo = test_utxo(10000); + let utxo = Utxo::new_test(0, 10000, 100, false, true); let destination = test_address(); let result = TransactionBuilder::new() @@ -967,7 +928,10 @@ mod tests { #[test] fn test_transaction_size_estimation() { // Test that transaction size estimation is accurate - let utxos = vec![test_utxo(100000), test_utxo(200000)]; + let utxos = vec![ + Utxo::new_test(0, 100000, 100, false, true), + Utxo::new_test(0, 200000, 100, false, true), + ]; let recipient_address = test_address(); let change_address = test_address(); @@ -1001,7 +965,7 @@ mod tests { #[test] fn test_fee_calculation() { // Test that fees are calculated correctly - let utxos = vec![test_utxo(1000000)]; + let utxos = vec![Utxo::new_test(0, 1000000, 100, false, true)]; let recipient_address = test_address(); let change_address = test_address(); @@ -1028,7 +992,7 @@ mod tests { #[test] fn test_exact_change_no_change_output() { // Test when the exact amount is used (no change output needed) - let utxos = vec![test_utxo(150226)]; // Exact amount for output + fee + let utxos = vec![Utxo::new_test(0, 150226, 100, false, true)]; // Exact amount for output + fee let recipient_address = test_address(); let change_address = test_address(); @@ -1050,7 +1014,7 @@ mod tests { #[test] fn test_special_payload_size_calculations() { // Test that special payload sizes are calculated correctly - let utxo = test_utxo(100000); + let utxo = Utxo::new_test(0, 100000, 100, false, true); let destination = test_address(); let change = test_address(); @@ -1115,7 +1079,7 @@ mod tests { #[test] fn test_build_with_payload_override() { // Test that build_with_payload overrides set_special_payload - let utxo = test_utxo(100000); + let utxo = Utxo::new_test(0, 100000, 100, false, true); let destination = test_address(); let change = test_address(); @@ -1161,7 +1125,7 @@ mod tests { #[test] fn test_bip69_output_ordering() { // Test that outputs are sorted according to BIP-69 - let utxo = test_utxo(1000000); + let utxo = Utxo::new_test(0, 1000000, 100, false, true); let address1 = test_address(); let address2 = Address::p2pkh( &dashcore::PublicKey::from_slice(&[ @@ -1280,7 +1244,11 @@ mod tests { #[test] fn test_coin_selection_with_special_payload() { // Test that coin selection considers special payload size - let utxos = vec![test_utxo(50000), test_utxo(60000), test_utxo(70000)]; + let utxos = vec![ + Utxo::new_test(0, 50000, 100, false, true), + Utxo::new_test(0, 60000, 100, false, true), + Utxo::new_test(0, 70000, 100, false, true), + ]; let recipient_address = test_address(); let change_address = test_address(); diff --git a/key-wallet/src/wallet/managed_wallet_info/transaction_building.rs b/key-wallet/src/wallet/managed_wallet_info/transaction_building.rs index 512b330cc..b87b5d41a 100644 --- a/key-wallet/src/wallet/managed_wallet_info/transaction_building.rs +++ b/key-wallet/src/wallet/managed_wallet_info/transaction_building.rs @@ -179,42 +179,19 @@ mod tests { use super::*; use crate::wallet::managed_wallet_info::transaction_builder::TransactionBuilder; use crate::Utxo; - use dashcore::blockdata::script::ScriptBuf; use dashcore::blockdata::transaction::special_transaction::TransactionPayload; - use dashcore::{Address, Network, OutPoint, Transaction, TxOut, Txid}; + use dashcore::{Address, Network, Transaction, Txid}; use dashcore_hashes::{sha256d, Hash}; use std::str::FromStr; - fn test_utxo(value: u64, confirmed: bool) -> Utxo { - let outpoint = OutPoint { - txid: Txid::from_raw_hash(sha256d::Hash::from_slice(&[1u8; 32]).unwrap()), - vout: 0, - }; - - let txout = TxOut { - value, - script_pubkey: ScriptBuf::new(), - }; - - let address = Address::p2pkh( - &dashcore::PublicKey::from_slice(&[ - 0x02, 0x50, 0x86, 0x3a, 0xd6, 0x4a, 0x87, 0xae, 0x8a, 0x2f, 0xe8, 0x3c, 0x1a, 0xf1, - 0xa8, 0x40, 0x3c, 0xb5, 0x3f, 0x53, 0xe4, 0x86, 0xd8, 0x51, 0x1d, 0xad, 0x8a, 0x04, - 0x88, 0x7e, 0x5b, 0x23, 0x52, - ]) - .unwrap(), - Network::Testnet, - ); - - let mut utxo = Utxo::new(outpoint, txout, address, 100, false); - utxo.is_confirmed = confirmed; - utxo - } - #[test] fn test_basic_transaction_creation() { // Test creating a basic transaction with inputs and outputs - let utxos = vec![test_utxo(100000, true), test_utxo(200000, true), test_utxo(300000, true)]; + let utxos = vec![ + Utxo::new_test(0, 100000, 100, false, true), + Utxo::new_test(0, 200000, 100, false, true), + Utxo::new_test(0, 300000, 100, false, true), + ]; let recipient_address = Address::from_str("yTb47qEBpNmgXvYYsHEN4nh8yJwa5iC4Cs") .unwrap() @@ -315,7 +292,10 @@ mod tests { #[test] fn test_transaction_size_estimation() { // Test that transaction size estimation is accurate - let utxos = vec![test_utxo(100000, true), test_utxo(200000, true)]; + let utxos = vec![ + Utxo::new_test(0, 100000, 100, false, true), + Utxo::new_test(0, 200000, 100, false, true), + ]; let recipient_address = Address::from_str("yTb47qEBpNmgXvYYsHEN4nh8yJwa5iC4Cs") .unwrap() @@ -350,7 +330,7 @@ mod tests { #[test] fn test_fee_calculation() { // Test that fees are calculated correctly - let utxos = vec![test_utxo(1000000, true)]; + let utxos = vec![Utxo::new_test(0, 1000000, 100, false, true)]; let recipient_address = Address::from_str("yTb47qEBpNmgXvYYsHEN4nh8yJwa5iC4Cs") .unwrap() @@ -384,7 +364,7 @@ mod tests { #[test] fn test_insufficient_funds() { // Test that insufficient funds returns an error - let utxos = vec![test_utxo(10000, true)]; + let utxos = vec![Utxo::new_test(0, 100000, 100, false, true)]; let recipient_address = Address::from_str("yTb47qEBpNmgXvYYsHEN4nh8yJwa5iC4Cs") .unwrap() @@ -408,7 +388,7 @@ mod tests { #[test] fn test_exact_change_no_change_output() { // Test when the exact amount is used (no change output needed) - let utxos = vec![test_utxo(150226, true)]; // Exact amount for output + fee + let utxos = vec![Utxo::new_test(0, 150226, 100, false, true)]; // Exact amount for output + fee let recipient_address = Address::from_str("yTb47qEBpNmgXvYYsHEN4nh8yJwa5iC4Cs") .unwrap() diff --git a/key-wallet/tests/test_optimal_consolidation.rs b/key-wallet/tests/test_optimal_consolidation.rs index ca9845b42..aa5a1945f 100644 --- a/key-wallet/tests/test_optimal_consolidation.rs +++ b/key-wallet/tests/test_optimal_consolidation.rs @@ -1,47 +1,19 @@ +use key_wallet::Utxo; + // Test for OptimalConsolidation coin selection strategy #[test] fn test_optimal_consolidation_strategy() { - use dashcore::blockdata::script::ScriptBuf; - use dashcore::{Address, Network, OutPoint, TxOut, Txid}; - use dashcore_hashes::{sha256d, Hash}; - use key_wallet::utxo::Utxo; use key_wallet::wallet::managed_wallet_info::coin_selection::*; use key_wallet::wallet::managed_wallet_info::fee::FeeRate; - fn test_utxo(value: u64, confirmed: bool) -> Utxo { - let outpoint = OutPoint { - txid: Txid::from_raw_hash(sha256d::Hash::from_slice(&[1u8; 32]).unwrap()), - vout: 0, - }; - - let txout = TxOut { - value, - script_pubkey: ScriptBuf::new(), - }; - - let address = Address::p2pkh( - &dashcore::PublicKey::from_slice(&[ - 0x02, 0x50, 0x86, 0x3a, 0xd6, 0x4a, 0x87, 0xae, 0x8a, 0x2f, 0xe8, 0x3c, 0x1a, 0xf1, - 0xa8, 0x40, 0x3c, 0xb5, 0x3f, 0x53, 0xe4, 0x86, 0xd8, 0x51, 0x1d, 0xad, 0x8a, 0x04, - 0x88, 0x7e, 0x5b, 0x23, 0x52, - ]) - .unwrap(), - Network::Testnet, - ); - - let mut utxo = Utxo::new(outpoint, txout, address, 100, false); - utxo.is_confirmed = confirmed; - utxo - } - // Test that OptimalConsolidation strategy works correctly let utxos = vec![ - test_utxo(100, true), - test_utxo(200, true), - test_utxo(300, true), - test_utxo(500, true), - test_utxo(1000, true), - test_utxo(2000, true), + Utxo::new_test(0, 100, 100, false, true), + Utxo::new_test(0, 200, 100, false, true), + Utxo::new_test(0, 300, 100, false, true), + Utxo::new_test(0, 500, 100, false, true), + Utxo::new_test(0, 1000, 100, false, true), + Utxo::new_test(0, 2000, 100, false, true), ]; let selector = CoinSelector::new(SelectionStrategy::OptimalConsolidation);