Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions key-wallet-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
79 changes: 15 additions & 64 deletions key-wallet-ffi/src/utxo_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);

Expand All @@ -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
Expand All @@ -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);

Expand All @@ -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;

Expand Down Expand Up @@ -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)));
Expand Down
3 changes: 2 additions & 1 deletion key-wallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down Expand Up @@ -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"] }
3 changes: 3 additions & 0 deletions key-wallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions key-wallet/src/test_utils/address.rs
Original file line number Diff line number Diff line change
@@ -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)
}
4 changes: 4 additions & 0 deletions key-wallet/src/test_utils/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod address;
mod utxo;

pub use address::*;
35 changes: 35 additions & 0 deletions key-wallet/src/test_utils/utxo.rs
Original file line number Diff line number Diff line change
@@ -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<u8>,
value: u64,
height: u32,
coinbase: bool,
confirmed: bool,
) -> Vec<Self> {
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()
}
}
38 changes: 3 additions & 35 deletions key-wallet/src/utxo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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());
Expand Down
63 changes: 18 additions & 45 deletions key-wallet/src/wallet/managed_wallet_info/coin_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
Loading
Loading