-
Notifications
You must be signed in to change notification settings - Fork 332
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
Refactor/use iterators to preselect utxos #1798
base: master
Are you sure you want to change the base?
Refactor/use iterators to preselect utxos #1798
Conversation
Hey @nymius, does this actually fix something or is this purely a refactor? Note that we want to redo the tx building / creating logic to use |
Hi @evanlinjin. Yes, maybe I went to far with the changes, still, it fixes the duplicate issue I open in #1794 by using a Then, about the refactor: The research of the code push me to isolate the pre selection steps as items on a checklist and that's why It ended up as a refactor. My idea is to further isolate each filter in their own iterator adaptor (a separated function for each one) that consumes The heavy use of iterators came for trying to avoid the allocation of helpers, like I envisioned something like this: let optional_utxos = self
.list_unspent()
.check_are_not_already_manually_selected()
.check_are_not_unspendable()
.check_confirmed_only_if_RBF()
.check_is_local_utxo()
.check_is_mature_if_coinbase();
// then
let (required, optional) = optional_utxos.chain(required_utxos.iter().clone())
.get_weighted_utxos()
.chain(foreign_utxos.iter().clone())
.apply_custom_validation_for_all_tx_inputs()
.split_utxos_in_required_and_optional() Discussing this today with @ValuedMammal, I decided to do the following changes:
If we don't agree on the above, I propose the following alternatives:
|
05ac09c
to
e42b5aa
Compare
05d94f3
to
388b7cc
Compare
Rebased |
crates/wallet/src/wallet/mod.rs
Outdated
// notice expected doesn't include the first output from two_output_tx as it should be | ||
// filtered out | ||
let expected = vec![OutPoint { txid, vout: 0 }, OutPoint { txid, vout: 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.
I'm confused by this comment, is it left over from the other test?
I think this test would be better if we add a bunch of utxos (~10) to the wallet, then call filter_utxos
and check that 1) none are duplicated and 2) none that we expect to be present are filtered out by accident.
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 confused by this comment, is it left over from the other test?
I'm confused too, probably I changed the test after the comment.
I think this test would be better if we add a bunch of utxos (~10) to the wallet, then call
filter_utxos
and check that 1) none are duplicated and 2) none that we expect to be present are filtered out by accident.
What I wanted to prove with the tests I implemented is that the only needed test case for filter_utxos
is when there are duplicated utxos across optional and required.
In order to do that, here, I wanted to prove is impossible for filter_chain_unspent
to produce duplicated optional utxos. That's why I mistakenly tried to insert the same tx multiple times. The thing is I have no knowledge of any way to incorporate duplicated utxos in the wallet bypassing those checks, and as the list is generated from an iterator on demand, the only way I can think right now is some kind of mock for the method, as it isn't an input of filter_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.
Will remove the test for now, as I don't find a deterministic way to produce duplicated utxos which will be de duplicated later by wallet logic.
In relation to 2) I think this line should be enough to assert that.
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.
Test removed in fe3c17e
📌 I don't think we've considered what would happen if a third party provides us with a "foreign" utxo that actually duplicates a utxo owned by the wallet, potentially causing us to sign something we didn't agree to. It might need more research so I'll open an issue. #1819 |
There were multiple calls for de-duplication of selected UTxOs. As the test `test_filter_duplicates` shows, there are four possible cases for duplication of UTxOs while feeding the coin selection algorithms. 1. no duplication: out of concern 2. duplication in the required utxos only: covered by the source of `required_utxos`, `Wallet::list_unspent`, which roots back the provided `UTxOs` to a `HashMap` which should avoid any duplication by definition 3. duplication in the optional utxos only: is the only one possible as optional `UTxOs` are stored in a `Vec` and no checks are performed about the duplicity of their members. 4. duplication across the required and optional utxos: is already covered by `Wallet::preselect_utxos`, which avoid the processing of required UTxOs while listing the unspent available UTxOs in the wallet. This refactor changes: - `TxParams::utxos` type to be `HashSet<LocalOutput>` avoiding the duplication case 3, and allowing a query closer to O(1) on avg. to cover duplication case 4 (before was O(n) where n is the size of required utxos). - Moves the computation of the `WeightedUtxos` to the last part of UTxO filtering, allowing the unification of the computation for local outputs. - Removes some extra allocations done for helpers structures or intermediate results while filtering UTxOs. - Allows for future integration of UTxO filtering methods for other utilities. - Adds more comments for each filtering step. With these changes all four cases would be covered, and `coin_selection::filter_duplicates` would be no longer needed.
There is no case in which current_height is None within create_tx.
Manually selected utxos are already available. We don't need a method to hand them out.
…UTxOs The `preselect_utxos` method (now `filter_utxos`) had an off-by-one error that was making the selection of optional UTxOs too restrictive, by requiring the coinbase outputs to surpass or equal coinbase maturity time at the current height of the selection, and not in the block in which the transaction may be included in the blockchain (be spent), probably, the next one. The bug is still in `filter_utxos`. The changes in this commit fix it by making use of the correctly defined `FullTxOut<A>::is_mature` method, which test positively a UTxO as mature if it is elegible for inclusion in the next mined block.
…nal utxos This test replaces the one used to test `coin_selection::filter_duplicates` introduced in 5299db3. As the code changed and there is not a single point to verificate the following properties: - there are no duplicates in required utxos - there are no duplicates in optional utxos - there are no duplicates across optional and required utxos anymore, test have been prefixed with `not_duplicated_utxos*` to allow its joint execution by using the following command: cargo test -- not_duplicated_utxos
At the moment is not possible to introduce a pre-condition not fulfilling the desired property to check the function under test behaves properly and produces a post-condition fulfilling the property.
388b7cc
to
e8e21e1
Compare
I created a separated draft PR #1823 to address the issue. |
Description
There were multiple calls for de-duplication of selected UTxOs in
Wallet::create_tx
: (1) and (2).As the test
test_filter_duplicates
shows, there are four possible cases for duplication of UTxOs while feeding the coin selection algorithms.required_utxos
,Wallet::list_unspent
, which roots back the providedUTxOs
to aHashMap
which should avoid any duplication by definitionUTxOs
are stored in aVec
and no checks are performed about the duplicity of their members.Wallet::preselect_utxos
, which avoid the processing of required UTxOs while listing the unspent available UTxOs in the wallet.This refactor does the following:
TxParams::utxos
type to beHashSet<LocalOutput>
avoiding the duplication case 3required_utxos
,Wallet::list_unspent
comes from aHashMap
which should avoid duplication by definition.WeightedUtxos
to the last part of UTxO filtering, allowing the unification of the computation for local outputs.foreign_utxos
, which should include a provided satisfation weight to use them effectively, andutxos
, manually selected UTxOs for which the wallet can compute their satisfaction weight without external resources.With these changes all four cases would be covered, and
coin_selection::filter_duplicates
is no longer needed.Fixes #1794.
The
preselect_utxos
method (nowfilter_utxos
) had an off-by-one error that was making the selection of optional UTxOs too restrictive, by requiring the coinbase outputs to surpass or equal coinbase maturity time at the current height of the selection, and not in the block in which the transaction may be included in the blockchain (be spent), probably, the next one.The bug still was in
filter_utxos
.The changes in commit 1b57d82 fix it by making use of the correctly defined
FullTxOut<A>::is_mature
method, which test positively a UTxO as mature if it is elegible for inclusion in the next mined block.Fixes #1810.
Notes to the reviewers
I added three test to cover the interesting cases for duplication:
- there are no duplicates in required utxos
- there are no duplicates in optional utxos
- there are no duplicates across optional and required utxos
the three of them have been prefixed with
not_duplicated_utxos*
to allow its joint execution under the command:cargo test -- not_duplicated_utxos
because the guarantees for the three conditions above are spread in different parts of the code.
Tests for issue #1810 have not been explicitly added, as there already was a
text_spend_coinbase
test which was corrected to ensure coinbase maturation is considered properly.Changelog notice
No changes to public APIs.
Checklists
cargo fmt
andcargo clippy
before committing