Skip to content

Commit c8b1548

Browse files
refactor: consolidate UTXO helpers in test_utils module (#334)
* refactor: consolidate utxos helpers in `test_utils.rs` * move test_util out of tests module * expose and used test_utils in itegration tests * utxo constructor using range * cleanup * Just some formatting * Make `is_coinbase` also part of the constructors --------- Co-authored-by: Borja Castellano <borjacastellano1@gmail.com>
1 parent 0d21901 commit c8b1548

File tree

12 files changed

+130
-263
lines changed

12 files changed

+130
-263
lines changed

key-wallet-ffi/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@ hex = "0.4"
3333
cbindgen = "0.29"
3434

3535
[dev-dependencies]
36+
key-wallet = { path = "../key-wallet", default-features = false, features = ["std", "test-utils"] }
3637
tempfile = "3.0"
3738
hex = "0.4"

key-wallet-ffi/src/utxo_tests.rs

Lines changed: 15 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ mod utxo_tests {
33
use super::super::*;
44
use crate::error::{FFIError, FFIErrorCode};
55
use key_wallet::managed_account::managed_account_type::ManagedAccountType;
6+
use key_wallet::Utxo;
67
use std::ffi::CStr;
78
use std::ptr;
89

@@ -278,11 +279,8 @@ mod utxo_tests {
278279
#[test]
279280
fn test_managed_wallet_get_utxos_multiple_accounts() {
280281
use crate::managed_wallet::FFIManagedWalletInfo;
281-
use dashcore::blockdata::script::ScriptBuf;
282-
use dashcore::{Address, OutPoint, TxOut, Txid};
283282
use key_wallet::account::account_type::StandardAccountType;
284283
use key_wallet::managed_account::ManagedAccount;
285-
use key_wallet::utxo::Utxo;
286284
use key_wallet::wallet::managed_wallet_info::ManagedWalletInfo;
287285
use key_wallet::Network;
288286

@@ -313,21 +311,9 @@ mod utxo_tests {
313311
false,
314312
);
315313

316-
for i in 0..2 {
317-
let outpoint = OutPoint {
318-
txid: Txid::from([i as u8; 32]),
319-
vout: i as u32,
320-
};
321-
let txout = TxOut {
322-
value: 10000,
323-
script_pubkey: ScriptBuf::from(vec![]),
324-
};
325-
// Create a dummy P2PKH address
326-
let dummy_pubkey_hash = dashcore::PubkeyHash::from([0u8; 20]);
327-
let script = ScriptBuf::new_p2pkh(&dummy_pubkey_hash);
328-
let address = Address::from_script(&script, Network::Testnet).unwrap();
329-
let utxo = Utxo::new(outpoint, txout, address, 100, false);
330-
bip44_account.utxos.insert(outpoint, utxo);
314+
let utxos = Utxo::new_test_batch(0..2, 10000, 100, false, false);
315+
for utxo in utxos {
316+
bip44_account.utxos.insert(utxo.outpoint, utxo);
331317
}
332318
managed_info.accounts.insert(bip44_account);
333319

@@ -351,20 +337,10 @@ mod utxo_tests {
351337
false,
352338
);
353339

354-
let outpoint = OutPoint {
355-
txid: Txid::from([10u8; 32]),
356-
vout: 0,
357-
};
358-
let txout = TxOut {
359-
value: 20000,
360-
script_pubkey: ScriptBuf::from(vec![]),
361-
};
362-
// Create a dummy P2PKH address
363-
let dummy_pubkey_hash = dashcore::PubkeyHash::from([0u8; 20]);
364-
let script = ScriptBuf::new_p2pkh(&dummy_pubkey_hash);
365-
let address = Address::from_script(&script, Network::Testnet).unwrap();
366-
let utxo = Utxo::new(outpoint, txout, address, 200, false);
367-
bip32_account.utxos.insert(outpoint, utxo);
340+
let utxos = Utxo::new_test_batch(10..11, 20000, 200, false, false);
341+
for utxo in utxos {
342+
bip32_account.utxos.insert(utxo.outpoint, utxo);
343+
}
368344
managed_info.accounts.insert(bip32_account);
369345

370346
// Create CoinJoin account with 2 UTXOs
@@ -380,21 +356,9 @@ mod utxo_tests {
380356
false,
381357
);
382358

383-
for i in 0..2 {
384-
let outpoint = OutPoint {
385-
txid: Txid::from([(20 + i) as u8; 32]),
386-
vout: i as u32,
387-
};
388-
let txout = TxOut {
389-
value: 30000,
390-
script_pubkey: ScriptBuf::from(vec![]),
391-
};
392-
// Create a dummy P2PKH address
393-
let dummy_pubkey_hash = dashcore::PubkeyHash::from([0u8; 20]);
394-
let script = ScriptBuf::new_p2pkh(&dummy_pubkey_hash);
395-
let address = Address::from_script(&script, Network::Testnet).unwrap();
396-
let utxo = Utxo::new(outpoint, txout, address, 300, false);
397-
coinjoin_account.utxos.insert(outpoint, utxo);
359+
let utxos = Utxo::new_test_batch(20..22, 30000, 300, false, false);
360+
for utxo in utxos {
361+
coinjoin_account.utxos.insert(utxo.outpoint, utxo);
398362
}
399363
managed_info.accounts.insert(coinjoin_account);
400364

@@ -418,11 +382,8 @@ mod utxo_tests {
418382
#[test]
419383
fn test_managed_wallet_get_utxos() {
420384
use crate::managed_wallet::FFIManagedWalletInfo;
421-
use dashcore::blockdata::script::ScriptBuf;
422-
use dashcore::{Address, OutPoint, TxOut, Txid};
423385
use key_wallet::account::account_type::StandardAccountType;
424386
use key_wallet::managed_account::ManagedAccount;
425-
use key_wallet::utxo::Utxo;
426387
use key_wallet::wallet::managed_wallet_info::ManagedWalletInfo;
427388
use key_wallet::Network;
428389

@@ -454,20 +415,10 @@ mod utxo_tests {
454415
false,
455416
);
456417

457-
let outpoint = OutPoint {
458-
txid: Txid::from([1u8; 32]),
459-
vout: 0,
460-
};
461-
let txout = TxOut {
462-
value: 10000,
463-
script_pubkey: ScriptBuf::from(vec![]),
464-
};
465-
// Create a dummy P2PKH address
466-
let dummy_pubkey_hash = dashcore::PubkeyHash::from([0u8; 20]);
467-
let script = ScriptBuf::new_p2pkh(&dummy_pubkey_hash);
468-
let address = Address::from_script(&script, Network::Testnet).unwrap();
469-
let utxo = Utxo::new(outpoint, txout, address, 100, false);
470-
testnet_account.utxos.insert(outpoint, utxo);
418+
let utxos = Utxo::new_test_batch(1..2, 10000, 100, false, false);
419+
for utxo in utxos {
420+
testnet_account.utxos.insert(utxo.outpoint, utxo);
421+
}
471422
managed_info.accounts.insert(testnet_account);
472423

473424
let ffi_managed_info = Box::into_raw(Box::new(FFIManagedWalletInfo::new(managed_info)));

key-wallet/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ bincode = ["serde", "dep:bincode", "dep:bincode_derive", "dash-network/bincode",
1616
bip38 = ["scrypt", "aes", "bs58", "rand"]
1717
eddsa = ["dashcore/eddsa"]
1818
bls = ["dashcore/bls"]
19+
test-utils = []
1920

2021
[dependencies]
2122
internals = { path = "../internals", package = "dashcore-private" }
@@ -47,5 +48,5 @@ async-trait = "0.1"
4748

4849
[dev-dependencies]
4950
hex = "0.4"
50-
key-wallet = { path = ".", features = ["bip38", "serde", "bincode", "eddsa", "bls"] }
51+
key-wallet = { path = ".", features = ["test-utils", "bip38", "serde", "bincode", "eddsa", "bls"] }
5152
tokio = { version = "1", features = ["macros", "rt"] }

key-wallet/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ extern crate core;
1212
#[cfg(feature = "std")]
1313
extern crate std;
1414

15+
#[cfg(any(test, feature = "test-utils"))]
16+
pub mod test_utils;
17+
1518
#[cfg(test)]
1619
#[macro_use]
1720
mod test_macros;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use dashcore::{Address, Network};
2+
3+
const TEST_PUBKEY_BYTES: [u8; 33] = [
4+
0x02, 0x50, 0x86, 0x3a, 0xd6, 0x4a, 0x87, 0xae, 0x8a, 0x2f, 0xe8, 0x3c, 0x1a, 0xf1, 0xa8, 0x40,
5+
0x3c, 0xb5, 0x3f, 0x53, 0xe4, 0x86, 0xd8, 0x51, 0x1d, 0xad, 0x8a, 0x04, 0x88, 0x7e, 0x5b, 0x23,
6+
0x52,
7+
];
8+
9+
pub fn test_address() -> Address {
10+
Address::p2pkh(&dashcore::PublicKey::from_slice(&TEST_PUBKEY_BYTES).unwrap(), Network::Testnet)
11+
}

key-wallet/src/test_utils/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
mod address;
2+
mod utxo;
3+
4+
pub use address::*;

key-wallet/src/test_utils/utxo.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
use std::ops::Range;
2+
3+
use dashcore::{OutPoint, ScriptBuf, TxOut, Txid};
4+
5+
use crate::{test_utils::test_address, Utxo};
6+
7+
impl Utxo {
8+
pub fn new_test(id: u8, value: u64, height: u32, coinbase: bool, confirmed: bool) -> Self {
9+
Self::new_test_batch(id..id + 1, value, height, coinbase, confirmed).remove(0)
10+
}
11+
12+
pub fn new_test_batch(
13+
ids_range: Range<u8>,
14+
value: u64,
15+
height: u32,
16+
coinbase: bool,
17+
confirmed: bool,
18+
) -> Vec<Self> {
19+
ids_range
20+
.enumerate()
21+
.map(|(i, id)| {
22+
let outpoint = OutPoint::new(Txid::from([id; 32]), i as u32);
23+
24+
let txout = TxOut {
25+
value,
26+
script_pubkey: ScriptBuf::new(),
27+
};
28+
29+
let mut utxo = Utxo::new(outpoint, txout, test_address(), height, coinbase);
30+
utxo.is_confirmed = confirmed;
31+
utxo
32+
})
33+
.collect()
34+
}
35+
}

key-wallet/src/utxo.rs

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -307,42 +307,10 @@ impl Default for UtxoSet {
307307
#[cfg(test)]
308308
mod tests {
309309
use super::*;
310-
use crate::Network;
311-
use dashcore::blockdata::script::ScriptBuf;
312-
use dashcore::Txid;
313-
use dashcore_hashes::{sha256d, Hash};
314-
315-
fn test_utxo(value: u64, height: u32) -> Utxo {
316-
test_utxo_with_vout(value, height, 0)
317-
}
318-
319-
fn test_utxo_with_vout(value: u64, height: u32, vout: u32) -> Utxo {
320-
let outpoint = OutPoint {
321-
txid: Txid::from_raw_hash(sha256d::Hash::from_slice(&[1u8; 32]).unwrap()),
322-
vout,
323-
};
324-
325-
let txout = TxOut {
326-
value,
327-
script_pubkey: ScriptBuf::new(),
328-
};
329-
330-
let address = Address::p2pkh(
331-
&dashcore::PublicKey::from_slice(&[
332-
0x02, 0x50, 0x86, 0x3a, 0xd6, 0x4a, 0x87, 0xae, 0x8a, 0x2f, 0xe8, 0x3c, 0x1a, 0xf1,
333-
0xa8, 0x40, 0x3c, 0xb5, 0x3f, 0x53, 0xe4, 0x86, 0xd8, 0x51, 0x1d, 0xad, 0x8a, 0x04,
334-
0x88, 0x7e, 0x5b, 0x23, 0x52,
335-
])
336-
.unwrap(),
337-
Network::Testnet,
338-
);
339-
340-
Utxo::new(outpoint, txout, address, height, false)
341-
}
342310

343311
#[test]
344312
fn test_utxo_spendability() {
345-
let mut utxo = test_utxo(100000, 100);
313+
let mut utxo = Utxo::new_test(0, 100000, 100, false, false);
346314

347315
// Unconfirmed UTXO should not be spendable
348316
assert!(!utxo.is_spendable(200));
@@ -360,8 +328,8 @@ mod tests {
360328
fn test_utxo_set_operations() {
361329
let mut set = UtxoSet::new();
362330

363-
let utxo1 = test_utxo_with_vout(100000, 100, 0);
364-
let utxo2 = test_utxo_with_vout(200000, 150, 1); // Different vout to ensure unique OutPoint
331+
let utxo1 = Utxo::new_test(0, 100000, 100, false, false);
332+
let utxo2 = Utxo::new_test(1, 200000, 150, false, false);
365333

366334
set.add(utxo1.clone());
367335
set.add(utxo2.clone());

key-wallet/src/wallet/managed_wallet_info/coin_selection.rs

Lines changed: 18 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -690,44 +690,14 @@ impl std::error::Error for SelectionError {}
690690
#[cfg(test)]
691691
mod tests {
692692
use super::*;
693-
use crate::Utxo;
694-
use dashcore::blockdata::script::ScriptBuf;
695-
use dashcore::{Address, Network, OutPoint, TxOut, Txid};
696-
use dashcore_hashes::{sha256d, Hash};
697-
698-
fn test_utxo(value: u64, confirmed: bool) -> Utxo {
699-
let outpoint = OutPoint {
700-
txid: Txid::from_raw_hash(sha256d::Hash::from_slice(&[1u8; 32]).unwrap()),
701-
vout: 0,
702-
};
703-
704-
let txout = TxOut {
705-
value,
706-
script_pubkey: ScriptBuf::new(),
707-
};
708-
709-
let address = Address::p2pkh(
710-
&dashcore::PublicKey::from_slice(&[
711-
0x02, 0x50, 0x86, 0x3a, 0xd6, 0x4a, 0x87, 0xae, 0x8a, 0x2f, 0xe8, 0x3c, 0x1a, 0xf1,
712-
0xa8, 0x40, 0x3c, 0xb5, 0x3f, 0x53, 0xe4, 0x86, 0xd8, 0x51, 0x1d, 0xad, 0x8a, 0x04,
713-
0x88, 0x7e, 0x5b, 0x23, 0x52,
714-
])
715-
.unwrap(),
716-
Network::Testnet,
717-
);
718-
719-
let mut utxo = Utxo::new(outpoint, txout, address, 100, false);
720-
utxo.is_confirmed = confirmed;
721-
utxo
722-
}
723693

724694
#[test]
725695
fn test_smallest_first_selection() {
726696
let utxos = vec![
727-
test_utxo(10000, true),
728-
test_utxo(20000, true),
729-
test_utxo(30000, true),
730-
test_utxo(40000, true),
697+
Utxo::new_test(0, 10000, 100, false, true),
698+
Utxo::new_test(0, 20000, 100, false, true),
699+
Utxo::new_test(0, 30000, 100, false, true),
700+
Utxo::new_test(0, 40000, 100, false, true),
731701
];
732702

733703
let selector = CoinSelector::new(SelectionStrategy::SmallestFirst);
@@ -742,10 +712,10 @@ mod tests {
742712
#[test]
743713
fn test_largest_first_selection() {
744714
let utxos = vec![
745-
test_utxo(10000, true),
746-
test_utxo(20000, true),
747-
test_utxo(30000, true),
748-
test_utxo(40000, true),
715+
Utxo::new_test(0, 10000, 100, false, true),
716+
Utxo::new_test(0, 20000, 100, false, true),
717+
Utxo::new_test(0, 30000, 100, false, true),
718+
Utxo::new_test(0, 40000, 100, false, true),
749719
];
750720

751721
let selector = CoinSelector::new(SelectionStrategy::LargestFirst);
@@ -758,7 +728,10 @@ mod tests {
758728

759729
#[test]
760730
fn test_insufficient_funds() {
761-
let utxos = vec![test_utxo(10000, true), test_utxo(20000, true)];
731+
let utxos = vec![
732+
Utxo::new_test(0, 10000, 100, false, true),
733+
Utxo::new_test(0, 20000, 100, false, true),
734+
];
762735

763736
let selector = CoinSelector::new(SelectionStrategy::LargestFirst);
764737
let result = selector.select_coins(&utxos, 50000, FeeRate::new(1000), 200);
@@ -770,12 +743,12 @@ mod tests {
770743
fn test_optimal_consolidation_strategy() {
771744
// Test that OptimalConsolidation strategy works correctly
772745
let utxos = vec![
773-
test_utxo(100, true),
774-
test_utxo(200, true),
775-
test_utxo(300, true),
776-
test_utxo(500, true),
777-
test_utxo(1000, true),
778-
test_utxo(2000, true),
746+
Utxo::new_test(0, 100, 100, false, true),
747+
Utxo::new_test(0, 200, 100, false, true),
748+
Utxo::new_test(0, 300, 100, false, true),
749+
Utxo::new_test(0, 500, 100, false, true),
750+
Utxo::new_test(0, 1000, 100, false, true),
751+
Utxo::new_test(0, 2000, 100, false, true),
779752
];
780753

781754
let selector = CoinSelector::new(SelectionStrategy::OptimalConsolidation);

0 commit comments

Comments
 (0)