-
Notifications
You must be signed in to change notification settings - Fork 17
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
Musab1258
wants to merge
5
commits into
Bitshala-Incubator:main
Choose a base branch
from
Musab1258:knapsack_success_test
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
32cb228
test select_coin equals knapsack
Musab1258 81d3427
linting and formatting
Musab1258 f4ff387
minor fixes to knapsack success test
Musab1258 fa7b292
made changes to knapsack success test
Musab1258 514b5db
merged main to resolve clippy issue
Musab1258 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -1223,4 +1223,70 @@ 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 target_value = 5000; | ||
|
||
let options = CoinSelectionOpt { | ||
target_value, | ||
target_feerate: 1.0, | ||
min_absolute_fee: 0, | ||
base_weight: 1, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 mut selection_result = select_coin(&inputs, options.clone()).unwrap(); | ||
let mut knapsack_result = select_coin_knapsack(&inputs, options).unwrap(); | ||
|
||
// Sort the selected inputs to ignore the order | ||
selection_result.selected_inputs.sort(); | ||
knapsack_result.selected_inputs.sort(); | ||
|
||
// Compare the sorted results | ||
assert_eq!( | ||
selection_result.selected_inputs, | ||
knapsack_result.selected_inputs | ||
); | ||
|
||
// Compare waste metrics | ||
assert_eq!(selection_result.waste, knapsack_result.waste); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because the input values are static, it doesn't serve any purpose to declare a separate variable target_value outside the struct.