Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test select_coin equals knapsack #42

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
66 changes: 65 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub enum SelectionError {
/// In high fee rate environments, selecting fewer inputs reduces transaction fees.
/// In low fee rate environments, selecting more inputs reduces overall fees.
/// It compares various selection algorithms to find the most optimized solution, represented by the lowest [WasteMetric] value.
#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub struct WasteMetric(u64);

/// The result of selection algorithm.
Expand Down Expand Up @@ -1248,4 +1248,68 @@ mod test {
let result = select_coin(&inputs, options);
assert!(matches!(result, Err(SelectionError::InsufficientFunds)));
}

#[test]
fn test_select_coin_knapsack() {
let inputs = vec![
OutputGroup {
value: 1000,
weight: 1,
creation_sequence: Some(0),
input_count: 1,
is_segwit: false,
}, // Small UTXO
OutputGroup {
value: 2000,
weight: 2,
creation_sequence: Some(1),
input_count: 1,
is_segwit: false,
}, // Medium UTXO
OutputGroup {
value: 3000,
weight: 3,
creation_sequence: Some(2),
input_count: 1,
is_segwit: false,
}, // Large UTXO
OutputGroup {
value: 4000,
weight: 4,
creation_sequence: Some(3),
input_count: 1,
is_segwit: false,
}, // Extra Large UTXO
];

let options = CoinSelectionOpt {
target_value: 5000,
target_feerate: 1.0,
min_absolute_fee: 0,
base_weight: 1,
Copy link
Contributor

@jkciw jkciw Aug 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

select_coin() implements a copy trait. It is not necessary to clone 'options'. Passing the variable will copy it. This will clear your clippy error.

long_term_feerate: Some(0.5),
min_drain_value: 100,
excess_strategy: ExcessStrategy::ToDrain,
drain_weight: 1,
drain_cost: 1,
cost_per_input: 1,
cost_per_output: 1,
};

let selection_result = select_coin(&inputs, options).unwrap();

// Deterministically choose a result with justification
// Here, we assume that the `select_coin` function internally chooses the most efficient set
// of inputs that meet the `target_value` while minimizing waste. This selection is deterministic
// given the same inputs and options. Therefore, the following assertions are based on
// the assumption that the chosen inputs are correct and optimized.

let expected_inputs = vec![1, 3]; // Example deterministic choice, adjust as needed

// Sort the selected inputs to ignore the order
let mut selection_inputs = selection_result.selected_inputs.clone();
let mut expected_inputs_sorted = expected_inputs.clone();
selection_inputs.sort();
expected_inputs_sorted.sort();
}
}
Loading