Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 2 additions & 0 deletions key-wallet/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//!
//! This module contains exhaustive tests for all functionality.

pub mod test_utils;

mod account_tests;

mod address_pool_tests;
Expand Down
37 changes: 37 additions & 0 deletions key-wallet/src/tests/test_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//! Test utilities for key-wallet tests

use crate::utxo::Utxo;
use crate::Network;
use dashcore::blockdata::transaction::txout::TxOut;
use dashcore::blockdata::transaction::OutPoint;
use dashcore::hashes::Hash;
use dashcore::{Address, ScriptBuf, Txid};
use dashcore_hashes::sha256d;

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)
}

pub fn test_utxo(value: u64) -> Utxo {
test_utxo_full(value, 100, 0, true)
}

pub fn test_utxo_full(value: u64, height: u32, vout: u32, confirmed: bool) -> 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 mut utxo = Utxo::new(outpoint, txout, test_address(), height, false);
utxo.is_confirmed = confirmed;
utxo
}
39 changes: 4 additions & 35 deletions key-wallet/src/utxo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,42 +307,11 @@ 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)
}
use crate::tests::test_utils::test_utxo_full;

#[test]
fn test_utxo_spendability() {
let mut utxo = test_utxo(100000, 100);
let mut utxo = test_utxo_full(100000, 100, 0, false);

// Unconfirmed UTXO should not be spendable
assert!(!utxo.is_spendable(200));
Expand All @@ -360,8 +329,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 = test_utxo_full(100000, 100, 0, false);
let utxo2 = test_utxo_full(200000, 150, 1, false); // Different vout to ensure unique OutPoint

set.add(utxo1.clone());
set.add(utxo2.clone());
Expand Down
59 changes: 10 additions & 49 deletions key-wallet/src/wallet/managed_wallet_info/coin_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -690,45 +690,11 @@ 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
}
use crate::tests::test_utils::test_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),
];
let utxos = vec![test_utxo(10000), test_utxo(20000), test_utxo(30000), test_utxo(40000)];

let selector = CoinSelector::new(SelectionStrategy::SmallestFirst);
let result = selector.select_coins(&utxos, 25000, FeeRate::new(1000), 200).unwrap();
Expand All @@ -741,12 +707,7 @@ 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),
];
let utxos = vec![test_utxo(10000), test_utxo(20000), test_utxo(30000), test_utxo(40000)];

let selector = CoinSelector::new(SelectionStrategy::LargestFirst);
let result = selector.select_coins(&utxos, 25000, FeeRate::new(1000), 200).unwrap();
Expand All @@ -758,7 +719,7 @@ mod tests {

#[test]
fn test_insufficient_funds() {
let utxos = vec![test_utxo(10000, true), test_utxo(20000, true)];
let utxos = vec![test_utxo(10000), test_utxo(20000)];

let selector = CoinSelector::new(SelectionStrategy::LargestFirst);
let result = selector.select_coins(&utxos, 50000, FeeRate::new(1000), 200);
Expand All @@ -770,12 +731,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),
test_utxo(100),
test_utxo(200),
test_utxo(300),
test_utxo(500),
test_utxo(1000),
test_utxo(2000),
];

let selector = CoinSelector::new(SelectionStrategy::OptimalConsolidation);
Expand Down
41 changes: 1 addition & 40 deletions key-wallet/src/wallet/managed_wallet_info/transaction_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -835,51 +835,12 @@ impl std::error::Error for BuilderError {}
#[cfg(test)]
mod tests {
use super::*;
use crate::tests::test_utils::{test_address, test_utxo};
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);
Expand Down
41 changes: 7 additions & 34 deletions key-wallet/src/wallet/managed_wallet_info/transaction_building.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,44 +177,17 @@ impl ManagedWalletInfo {
#[cfg(test)]
mod tests {
use super::*;
use crate::tests::test_utils::test_utxo;
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![test_utxo(100000), test_utxo(200000), test_utxo(300000)];

let recipient_address = Address::from_str("yTb47qEBpNmgXvYYsHEN4nh8yJwa5iC4Cs")
.unwrap()
Expand Down Expand Up @@ -315,7 +288,7 @@ 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![test_utxo(100000), test_utxo(200000)];

let recipient_address = Address::from_str("yTb47qEBpNmgXvYYsHEN4nh8yJwa5iC4Cs")
.unwrap()
Expand Down Expand Up @@ -350,7 +323,7 @@ mod tests {
#[test]
fn test_fee_calculation() {
// Test that fees are calculated correctly
let utxos = vec![test_utxo(1000000, true)];
let utxos = vec![test_utxo(1000000)];

let recipient_address = Address::from_str("yTb47qEBpNmgXvYYsHEN4nh8yJwa5iC4Cs")
.unwrap()
Expand Down Expand Up @@ -384,7 +357,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![test_utxo(10000)];

let recipient_address = Address::from_str("yTb47qEBpNmgXvYYsHEN4nh8yJwa5iC4Cs")
.unwrap()
Expand All @@ -408,7 +381,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![test_utxo(150226)]; // Exact amount for output + fee

let recipient_address = Address::from_str("yTb47qEBpNmgXvYYsHEN4nh8yJwa5iC4Cs")
.unwrap()
Expand Down
Loading