-
Notifications
You must be signed in to change notification settings - Fork 27
Prefer Utxo::Local over Utxo::Foreign in OldestFirstCoinSelection #265
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
base: master
Are you sure you want to change the base?
Prefer Utxo::Local over Utxo::Foreign in OldestFirstCoinSelection #265
Conversation
Pull Request Test Coverage Report for Build 15622468119Details
💛 - Coveralls |
Thanks for the quick fix! |
wallet/src/wallet/coin_selection.rs
Outdated
let utxos = { | ||
optional_utxos.sort_unstable_by_key(|wu| match &wu.utxo { | ||
Utxo::Local(local) => Some(local.chain_position), | ||
Utxo::Foreign { .. } => None, | ||
}); | ||
|
||
let (local_utxos, foreign_utxos): (Vec<WeightedUtxo>, Vec<WeightedUtxo>) = | ||
optional_utxos.into_iter().partition(|wu| match &wu.utxo { | ||
Utxo::Local(..) => true, | ||
Utxo::Foreign { .. } => false, | ||
}); |
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.
Why do you think about using .sort_unstable_by
or just using .sort_unstable_by_key
with a different key?
I think it's better to sort everything in one loop than in two goes.
optional_utxos.sort_unstable_by_key(|wu| {
Reverse(match &wu.utxo {
Utxo::Local(local) => Some(Reverse(local.chain_position)),
Utxo::Foreign { .. } => None,
})
});
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 like it, however, by reading the Reverse
docs I arrived to the following solution:
optional_utxos.sort_unstable_by_key(|wu| {
match &wu.utxo {
Utxo::Local(local) => (false, Some(local.chain_position)),
Utxo::Foreign { .. } => (true, None),
}
});
false
< true
, so all LocalUtxos
are ordered at the beginning of the Vec
. WDYT?
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.
That works too!
c286a82
to
ef5cf30
Compare
…irstCoinSelection Fixes bitcoindevkit#264
ef5cf30
to
d23d07b
Compare
@MaxFangX thanks for looking at the code so close! |
Description
The comments in the
OldestFirstCoinSelection
implementation stated the following:But this was not honored in the code.
This PR enforces this and ensures the expected behaviour through the two following new tests:
test_oldest_first_coin_selection_uses_all_optional_with_foreign_utxo_locals_sorted_first
test_oldest_first_coin_selection_uses_only_all_optional_local_utxos_not_a_single_foreign
Fixes #264
Changelog notice
No public APIs are changed by these commits.
Checklists
Important
This pull request DOES NOT break the existing API
cargo +nightly fmt
andcargo clippy
before committing