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

Implementation for BNB Issue#1 #29

Merged
merged 11 commits into from
Sep 21, 2024
Merged

Conversation

NeoZ666
Copy link
Contributor

@NeoZ666 NeoZ666 commented Jun 20, 2024

No description provided.

@delcin-raj
Copy link
Collaborator

make sure to run "cargo fmt" before committing changes

@NeoZ666
Copy link
Contributor Author

NeoZ666 commented Jun 23, 2024

Done

@NeoZ666
Copy link
Contributor Author

NeoZ666 commented Jun 26, 2024

Ran the cargo fmt command once again, please check.

Copy link
Collaborator

@delcin-raj delcin-raj left a 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;
Copy link
Collaborator

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()),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should be Err(NoSolutionFound)

Copy link
Collaborator

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 Show resolved Hide resolved
src/lib.rs Outdated
selected_inputs,
acc_eff_value,
depth + 1,
bnp_tries - 1,
Copy link
Collaborator

Choose a reason for hiding this comment

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

bnp_tries - 2

Copy link
Collaborator

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,
Copy link
Collaborator

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 {
Copy link
Collaborator

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(
Copy link
Collaborator

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());
Copy link
Collaborator

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());
Copy link
Collaborator

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
Comment on lines 165 to 169
} 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 {
Copy link
Contributor

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
Copy link
Collaborator

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;
Copy link
Collaborator

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 =
Copy link
Collaborator

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,
Copy link
Collaborator

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(
Copy link
Collaborator

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,
Copy link
Collaborator

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

@NeoZ666 NeoZ666 mentioned this pull request Aug 30, 2024
src/lib.rs Outdated
pub waste: WasteMetric,
}

/// Wrapped in a struct or else input for fn bnb takes too many arguments - 9/7,
Copy link
Collaborator

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
Copy link
Collaborator

Choose a reason for hiding this comment

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

clippy doc error

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@delcin-raj
Copy link
Collaborator

Waiting for the updates...

Copy link
Collaborator

@delcin-raj delcin-raj left a comment

Choose a reason for hiding this comment

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

LGTM

@delcin-raj
Copy link
Collaborator

Commits are not signed.
Fix fmt and clippy errors.

src/lib.rs Outdated
}
}
} else {
/// Avoided creation of intermediate variable, for marginally better performance.
Copy link
Collaborator

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

Copy link
Contributor Author

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
Copy link
Collaborator

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

Copy link
Collaborator

@delcin-raj delcin-raj left a comment

Choose a reason for hiding this comment

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

LGTM

@delcin-raj delcin-raj merged commit bf812ac into Bitshala-Incubator:main Sep 21, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants