-
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
Implementation for BNB Issue#1 #29
Conversation
make sure to run "cargo fmt" before committing changes |
Done |
Ran the |
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.
Add tests from core
src/lib.rs
Outdated
) -> Result<SelectionOutput, SelectionError> { | ||
unimplemented!() | ||
let mut selected_inputs: Vec<usize> = vec![]; | ||
let bnb_tries = 1000000; |
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.
this should be a constant.
src/lib.rs
Outdated
}; | ||
Ok(selection_output) | ||
} | ||
None => select_coin_srd(inputs, options, &mut rand::thread_rng()), |
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.
Should be Err(NoSolutionFound)
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.
The reasoning behind this is when the user calls the high level API, srd will be invoked regardless of the result of bnb.
So when a user calls bnb in particular signalling no solution found is better than returning the result of srd
src/lib.rs
Outdated
selected_inputs, | ||
acc_eff_value, | ||
depth + 1, | ||
bnp_tries - 1, |
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.
bnp_tries - 2
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 every recursive call should decrement this by one.
This variable is immutable
src/lib.rs
Outdated
selected_inputs, | ||
new_effective_values, | ||
depth + 1, | ||
bnp_tries - 1, |
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.
bnp_tries - 2
src/lib.rs
Outdated
@@ -358,6 +483,11 @@ fn effective_value(output: &OutputGroup, feerate: f32) -> u64 { | |||
.saturating_sub(calculate_fee(output.weight, feerate)) | |||
} | |||
|
|||
fn generate_random_bool(rng: &mut ThreadRng) -> bool { |
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.
Remove this function
src/lib.rs
Outdated
@@ -248,17 +375,15 @@ pub fn select_coin_fifo( | |||
pub fn select_coin_srd( |
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.
Undo the changes to this function
src/lib.rs
Outdated
@@ -541,7 +706,7 @@ mod test { | |||
fn test_insufficient_funds() { | |||
let inputs = setup_basic_output_groups(); | |||
let options = setup_options(7000); // Set a target value higher than the sum of all inputs | |||
let result = select_coin_srd(&inputs, options); | |||
let result = select_coin_srd(&inputs, options, &mut rand::thread_rng()); |
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.
undo the changes
src/lib.rs
Outdated
} | ||
|
||
fn test_successful_selection() { | ||
let mut inputs = setup_basic_output_groups(); | ||
let mut options = setup_options(2500); | ||
let mut result = select_coin_srd(&inputs, options); | ||
let mut result = select_coin_srd(&inputs, options, &mut rand::thread_rng()); |
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.
undo the changes
src/lib.rs
Outdated
} else if acc_eff_value >= target_for_match { | ||
return Some(selected_inputs.to_vec()); | ||
} else if bnp_tries <= 0 || depth >= inputs_in_desc_value.len() { | ||
return None; | ||
} else { |
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.
A series of if clauses
without the usage ofif-else
clause would be more readable and also the else
clause is redundant.
src/lib.rs
Outdated
unimplemented!() | ||
rng: &mut ThreadRng, | ||
) -> Option<Vec<usize>> { | ||
let target_for_match = options.target_value |
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.
This value could be computed only once as it depends on the options
No need to recompute this during every recursive call.
This can be included in the function parameter
src/lib.rs
Outdated
let target_for_match = options.target_value | ||
+ calculate_fee(options.base_weight, options.target_feerate) | ||
+ options.cost_per_output; | ||
let match_range = options.cost_per_input + options.cost_per_output; |
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.
This also
src/lib.rs
Outdated
if rng.gen_bool(0.5) { | ||
// exploring the inclusion branch | ||
// first include then omit | ||
let new_effective_values = |
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.
it's a single value
src/lib.rs
Outdated
selected_inputs: &[usize], | ||
effective_value: u64, | ||
selected_inputs: &mut Vec<usize>, | ||
acc_eff_value: u64, | ||
depth: usize, | ||
bnp_tries: u32, | ||
options: &CoinSelectionOpt, |
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.
This argument options
could potentially be discarded
src/lib.rs
Outdated
Some(_) => with_this, | ||
None => { | ||
selected_inputs.pop(); //poping out the selected utxo if it does not fit | ||
let without_this = bnb( |
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.
Just return the result of the bnb
call here.
No need for without_this
variable
src/lib.rs
Outdated
selected_inputs: &[usize], | ||
effective_value: u64, | ||
selected_inputs: &mut Vec<usize>, | ||
acc_eff_value: u64, | ||
depth: usize, | ||
bnp_tries: u32, |
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.
Correct it to bnb_tries
src/lib.rs
Outdated
pub waste: WasteMetric, | ||
} | ||
|
||
/// Wrapped in a struct or else input for fn bnb takes too many arguments - 9/7, |
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.
Fix this clippy error
Ok(selection_output) | ||
} | ||
None => Err(SelectionError::NoSolutionFound), | ||
} | ||
} | ||
|
||
/// Return empty vec if no solutions are found |
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.
clippy doc error
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.
done
Waiting for the updates... |
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.
LGTM
Commits are not signed. |
4f7dcd2
to
a8f896b
Compare
Signed-off-by: neoz666 <[email protected]>
Signed-off-by: neoz666 <[email protected]>
Signed-off-by: neoz666 <[email protected]>
Signed-off-by: neoz666 <[email protected]>
Signed-off-by: neoz666 <[email protected]>
Signed-off-by: neoz666 <[email protected]>
Signed-off-by: neoz666 <[email protected]>
src/lib.rs
Outdated
} | ||
} | ||
} else { | ||
/// Avoided creation of intermediate variable, for marginally better performance. |
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.
remove this comment, confusing to the people who don't have context
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.
done
src/lib.rs
Outdated
Some(_) => with_this, | ||
None => { | ||
selected_inputs.pop(); // poping out the selected utxo if it does not fit | ||
None // this may or may not be correct |
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.
This is correct, so remove the comment
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.
LGTM
No description provided.