Skip to content

Commit

Permalink
Updates, removed function, clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
NeoZ666 authored Sep 8, 2024
1 parent 3525e6e commit 1ca7949
Showing 1 changed file with 18 additions and 62 deletions.
80 changes: 18 additions & 62 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

//! A blockchain-agnostic Rust Coinselection library


use rand::rngs::ThreadRng;
use rand::{seq::SliceRandom, thread_rng, Rng};
use std::cmp::Reverse;
use std::collections::HashSet;
use std::hash::{Hash, Hasher};
use std::sync::{Arc, Mutex};
use std::thread;
use rand::rngs::ThreadRng;

/// Represents an input candidate for Coinselection, either as a single UTXO or a group of UTXOs.
///
Expand Down Expand Up @@ -101,7 +100,7 @@ pub struct SelectionOutput {
pub waste: WasteMetric,
}

/// Wrapped in a struct or else input for fn bnb takes too many arguments - 9/7,
/// Wrapped in a struct or else input for fn bnb takes too many arguments - 9/7,
/// Leading to usage of stack instead of registers - https://users.rust-lang.org/t/avoiding-too-many-arguments-passing-to-a-function/103581
/// Fit in : 1 XMM register, 1 GPR
#[derive(Debug)]
Expand All @@ -124,7 +123,9 @@ pub fn select_coin_bnb(
let rng = &mut thread_rng();

let match_parameters = MatchParameters {
target_for_match: options.target_value + calculate_fee(options.base_weight, options.target_feerate) + options.cost_per_output,
target_for_match: options.target_value
+ calculate_fee(options.base_weight, options.target_feerate)
+ options.cost_per_output,
match_range: options.cost_per_input + options.cost_per_output,
target_feerate: options.target_feerate,
};
Expand Down Expand Up @@ -181,7 +182,7 @@ fn bnb(
depth: usize,
bnb_tries: &mut u32,
rng: &mut ThreadRng,
match_parameters: &MatchParameters,
match_parameters: &MatchParameters,
) -> Option<Vec<usize>> {
if acc_eff_value > match_parameters.target_for_match + match_parameters.match_range {
return None;
Expand All @@ -199,8 +200,11 @@ fn bnb(
if rng.gen_bool(0.5) {
// exploring the inclusion branch
// first include then omit
let new_effective_value =
acc_eff_value + effective_value(&inputs_in_desc_value[depth].1, match_parameters.target_feerate);
let new_effective_value = acc_eff_value
+ effective_value(
&inputs_in_desc_value[depth].1,
match_parameters.target_feerate,
);
selected_inputs.push(inputs_in_desc_value[depth].0);
let with_this = bnb(
inputs_in_desc_value,
Expand Down Expand Up @@ -240,7 +244,10 @@ fn bnb(
Some(without_this) => Some(without_this),
None => {
let new_effective_value = acc_eff_value
+ effective_value(&inputs_in_desc_value[depth].1, match_parameters.target_feerate);
+ effective_value(
&inputs_in_desc_value[depth].1,
match_parameters.target_feerate,
);
selected_inputs.push(inputs_in_desc_value[depth].0);
let with_this = bnb(
inputs_in_desc_value,
Expand Down Expand Up @@ -894,7 +901,7 @@ mod test {
})
}
}

fn bnb_setup_options(target_value: u64) -> CoinSelectionOpt {
CoinSelectionOpt {
target_value,
Expand Down Expand Up @@ -974,10 +981,7 @@ mod test {

// Adjust the target value to ensure it tests for multiple valid solutions
let opt = bnb_setup_options(5730);
let ans = select_coin_bnb(
&values,
opt,
);
let ans = select_coin_bnb(&values, opt);
if let Ok(selection_output) = ans {
let expected_solution = vec![7, 5, 1];
assert_eq!(
Expand All @@ -995,10 +999,7 @@ mod test {
let total_input_value: u64 = inputs.iter().map(|input| input.value).sum();
let impossible_target = total_input_value + 1000;
let options = bnb_setup_options(impossible_target);
let result = select_coin_bnb(
&inputs,
options,
);
let result = select_coin_bnb(&inputs, options);
assert!(
matches!(result, Err(SelectionError::NoSolutionFound)),
"Expected NoSolutionFound error, got {:?}",
Expand Down Expand Up @@ -1490,49 +1491,4 @@ mod test {
let result = select_coin(&inputs, options);
assert!(matches!(result, Err(SelectionError::InsufficientFunds)));
}

/// Test for Successful Coin Selection ALgorithm to be Branch and Bound.
#[test]
fn test_select_coin_bnb() {
// Define the test values
let values = [
OutputGroup {
value: 5000,
weight: 50,
input_count: 1,
is_segwit: false,
creation_sequence: None,
},
OutputGroup {
value: 600,
weight: 250,
input_count: 1,
is_segwit: false,
creation_sequence: None,
},
OutputGroup {
value: 400,
weight: 200,
input_count: 1,
is_segwit: false,
creation_sequence: None,
},
];
let opt = bnb_setup_options(5711);
let ans = select_coin(
&values,
opt,
);
if let Ok(selection_output) = ans {
let expected_solution = vec![0, 1, 2];
assert_eq!(
selection_output.selected_inputs, expected_solution,
"Expected solution {:?}, but got {:?}",
expected_solution, selection_output.selected_inputs
);
} else {
panic!("Failed to find a solution");
}
}

}

0 comments on commit 1ca7949

Please sign in to comment.