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 3 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
75 changes: 74 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub enum SelectionError {
/// During low fee rate environment, slecting more number of inputs will help minimize the over all fees paid by the wallet during its lifetime.
/// This is used to compare various selection algorithm and find the most
/// optimizewd solution, represented by least [WasteMetric] value.
#[derive(Debug)]
#[derive(Debug, PartialEq)]
pub struct WasteMetric(u64);

/// The result of selection algorithm
Expand Down Expand Up @@ -1223,4 +1223,77 @@ 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();
let knapsack_result = select_coin_knapsack(&inputs, options).unwrap();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Don't call the knapsack fn.
You have to deterministically choose a result with justification in comments


// Sort the selected inputs to ignore the order
let mut selection_inputs = selection_result.selected_inputs.clone();
let mut knapsack_inputs = knapsack_result.selected_inputs.clone();
selection_inputs.sort();
knapsack_inputs.sort();

// Compare the sorted results
assert_eq!(selection_inputs, knapsack_inputs);

// Compare waste metrics
assert_eq!(selection_result.waste, knapsack_result.waste);

// Additional assertions to compare against other algorithms (e.g., fifo, bnb, srd)
let fifo_result = select_coin_fifo(&inputs, options).unwrap();
let bnb_result = select_coin_bnb(&inputs, options).unwrap();
let srd_result = select_coin_srd(&inputs, options).unwrap();

// Ensure that knapsack result is not the same as other algorithms
Copy link
Collaborator

Choose a reason for hiding this comment

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

No need for these asserts.
They might fail

assert_ne!(knapsack_result.selected_inputs, fifo_result.selected_inputs);
assert_ne!(knapsack_result.selected_inputs, bnb_result.selected_inputs);
assert_ne!(knapsack_result.selected_inputs, srd_result.selected_inputs);
}
}
Loading