-
Notifications
You must be signed in to change notification settings - Fork 311
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
Allow custom fallback algorithm for bnb #1581
Allow custom fallback algorithm for bnb #1581
Conversation
3a7c3c2
to
36f7d39
Compare
Concept ACK. I want to take a closer look at it. It would also be nice to try and fix the ignored tests in |
Simple test suggestion #[test]
fn test_bnb_fallback_algorithm() {
// utxo value
// 120k + 80k + 300k
let optional_utxos = get_oldest_first_test_utxos();
let feerate = FeeRate::BROADCAST_MIN;
let target_amount = 190_000;
let drain_script = ScriptBuf::new();
// bnb won't find exact match and should select oldest first
let res = BranchAndBoundCoinSelection::<OldestFirstCoinSelection>::default()
.coin_select(
vec![],
optional_utxos,
feerate,
target_amount,
&drain_script,
&mut thread_rng(),
)
.unwrap();
assert_eq!(res.selected_amount(), 200_000);
} |
Thanks @ValuedMammal feel free to push a commit to introduce the test (if you wish). |
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.
Concept ACK 36f7d39
Overall looks good! It's getting late, I'll do another review and test it tomorrow :)
My main gripe is making the RNG required in order to implement |
AFAICT it only needs to removes the Maybe renaming the test fn to mention it relies on fallback is good too. edit: I still haven't figured out the problem with the other one from line 1236, as it's been ignored for a while. |
I also took a stab at encapsulating the RNG for |
@evanlinjin feel free to squash 89e619e which has @ValuedMammal's suggestions in it and I think this one also needs a rebase. |
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.
ACK 89e619e
Signature of `CoinSelectionAlgorithm::coin_select` has been changed to take in a `&mut RangCore`. This allows us to pass the random number generator directly to the cs algorithm. Single random draw is now it's own type `SingleRandomDraw` and impls `CoinSelectionAlgorithm`. `BranchAndBoundCoinSelection` now handles it's own fallback algorithm internally, and a generic type parameter is added to specify the fallback algorithm. `coin_selection::Error` is renamed to `InsufficientFunds` and the BnB error variants are removed. The BnB error variants are no longer needed since those cases are handled internally by `BranchAndBoundCoinSelection` (via calling the fallback algorithm). Add test_bnb_fallback_algorithm test and docs cleanup suggested by @ValuedMammal.
89e619e
to
c18204d
Compare
I've rebased this PR and incorporated @ValuedMammal 's docs and new bnb fallback test. |
I added a test to check if deterministic coin selection algos were always returning the same utxos and even without changing
I didn't have to change I also did some test cleanup and added back ignored tests that work as long as we calculate the correct target amount that bnb needs to select the expected utxos. |
9e1310d
to
61fbe30
Compare
Regarding utxo sorting I think we're also saved by the fact that |
I think I pushed everyone onto the wrong rabbit-hole with regards to I think we should revert back to using |
Added back ignored branch and bounnd tests and cleaned up calculation for target amounts.
61fbe30
to
65be4ea
Compare
Makes sense, reverted. |
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.
utACK 65be4ea
Cool, the new deterministic UTXO selection tests look like a great addition!
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.
self-ACK 65be4ea
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.
@@ -402,7 +402,7 @@ pub struct BranchAndBoundCoinSelection<Cs = SingleRandomDraw> {
fallback_algorithm: Cs,
}
-/// Error returned by branch and bond coin selection.
+/// Error returned by branch and bound coin selection.
#[derive(Debug)]
enum BnbError {
/// Branch and bound coin selection tries to avoid needing a change by finding the right inputs for
@@ -521,8 +521,8 @@ impl<Cs: CoinSelectionAlgorithm> CoinSelectionAlgorithm for BranchAndBoundCoinSe
}
match self.bnb(
- required_ogs.clone(),
- optional_ogs.clone(),
+ required_ogs,
+ optional_ogs,
curr_value,
curr_available_value,
signed_target_amount,
@@ -1383,7 +1383,7 @@ mod test {
let drain_script = ScriptBuf::default();
let target_amount = 20_000 + FEE_AMOUNT;
- BranchAndBoundCoinSelection::<SingleRandomDraw>::default()
+ BranchAndBoundCoinSelection::new(size_of_change, SingleRandomDraw)
.bnb(
vec![],
utxos,
@@ -1414,7 +1414,7 @@ mod test {
let drain_script = ScriptBuf::default();
- BranchAndBoundCoinSelection::<SingleRandomDraw>::default()
+ BranchAndBoundCoinSelection::new(size_of_change, SingleRandomDraw)
.bnb(
vec![],
utxos,
@@ -1450,7 +1450,7 @@ mod test {
let drain_script = ScriptBuf::default();
- let result = BranchAndBoundCoinSelection::<SingleRandomDraw>::default()
+ let result = BranchAndBoundCoinSelection::new(size_of_change, SingleRandomDraw)
.bnb(
vec![],
utxos,
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.
I'm working on a follow-up PR. I also think the tests need to be improved and there's a bigger issue with SingleRandomDraw
in that if used on its own it doesn't fail when there's not enough sats to meet the required amount.
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.
Fixed in #1605
…nt funds 22a2f83 fix(wallet): fix SingleRandomDraw to throw an error if insufficient funds (Steve Myers) Pull request description: ### Description * fix SingleRandomDraw to error if insufficient funds * fixed spelling and clippy errors (see: #1581 (comment)) * updated tests to check for error variant instead of a panic ### Notes to the reviewers Since the single random draw algo can be used on its own it needs to be able to return an insufficient funds error. I think the reason we didn't catch this before is that single random draw already check for sufficient required + optional utxo amounts and returns the insufficient funds error. ### Changelog notice * fix SingleRandomDraw coin selection to error if there are insufficient funds for a requested payment amount. ### Checklists #### All Submissions: * [x] I've signed all my commits * [x] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk/blob/master/CONTRIBUTING.md) * [x] I ran `cargo fmt` and `cargo clippy` before committing #### Bugfixes: * [ ] This pull request breaks the existing API * [x] I've added tests to reproduce the issue which are now passing * [ ] I'm linking the issue being fixed by this PR ACKs for top commit: ValuedMammal: ACK 22a2f83 Tree-SHA512: c434003ea6cec1423960a0c7d2f830324227f9f99d9d8f72bd7785368cf51c867036b80c300a97177a10998830ef4df924bdcad408730f9e5dddc92cda75dceb
Description
This allows the caller to set a custom fallback algorithm when using
BranchAndBoundCoinSelection
. Previously, you were forced into usingSingleRandomDraw
.Signature of
CoinSelectionAlgorithm::coin_select
has been changed to take in a&mut RangCore
. This allows us to pass the random number generator directly to the cs algorithm.Single random draw is now it's own type
SingleRandomDraw
and implsCoinSelectionAlgorithm
.BranchAndBoundCoinSelection
now handles it's own fallback algorithm internally, and a generic type parameter is added to specify the fallback algorithm.coin_selection::Error
is renamed toInsufficientFunds
and the BnB error variants are removed. The BnB error variants are no longer needed since those cases are handled internally byBranchAndBoundCoinSelection
(via calling the fallback algorithm).Notes to the reviewers
This is breaking change. Not sure how useful this is for our users. If it's deemed useful, consider including in beta.
Changelog notice
CoinSelectionAlgorithm::coin_select
to take in an additional&mut RangCore
variable. This allows us to pass a random number generator directly to the cs algorithm.SingeRandomDraw
type which implsCoinSelectAlgorithm
.BranchAndBoundCoinSelection
to call the fallback internally. An additional generic parameter is added set this.coin_selection::Error
tocoin_selection::InsufficientFunds
(which is now a struct) and therefore removing bnb error variants.Checklists
All Submissions:
cargo fmt
andcargo clippy
before committing