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

Use selected Utxos while crafting txn #912

Merged
merged 5 commits into from
Mar 18, 2024
Merged

Conversation

Chinwendu20
Copy link

@Chinwendu20 Chinwendu20 commented Mar 4, 2024

Related issue: lightningnetwork/lnd#6949 (comment)
and it is needed for lightningnetwork/lnd#8516

This pull request enables SendOutput in the wallet to use passed UTXOS as inputs for transactions. All passed UTXOS would be used as inputs.

Copy link

@sputn1ck sputn1ck left a comment

Choose a reason for hiding this comment

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

Thanks for the PR @Chinwendu20 !
I left some comments. I think we should definitely cover the new code in some test cases.

wallet/wallet.go Show resolved Hide resolved
wallet/wallet.go Show resolved Hide resolved
wallet/createtx.go Outdated Show resolved Hide resolved
if err != nil {
return err

arrangedCoins := wrappedEligible
Copy link

@sputn1ck sputn1ck Mar 4, 2024

Choose a reason for hiding this comment

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

We're using the arrangedCoins later in inputSource := makeInputSource(arrangedCoins). I wonder what happens if we're using an output amount much small than the combined inputs, I think it would be expected to use all the provided input, but I'm not sure if makeInputSource will do that. I think some test coverage is definitely needed here.

Copy link
Author

Choose a reason for hiding this comment

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

If I understand correctly, you are asking what would happen if a user wants to send an amount way smaller than the combined amount present in the select utxos?
I do not think that scenario is a cause for alarm or violates anything?

Copy link

@sputn1ck sputn1ck Mar 4, 2024

Choose a reason for hiding this comment

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

What I'm trying to convey is that not the arrangedCoins, but the inputSource is the final instance for selecting the inputs. In the current approach if we have preselected 2 inputs[1btc, 1btc] and want to send to an output with 0.5btc we would only select the first input. I don't think this is the intention of this PR, as we want to use all the inputs we select.

See:

func makeInputSource(eligible []Coin) txauthor.InputSource {

inputAmount, inputs, inputValues, scripts, err := fetchInputs(targetAmount + targetFee)

Copy link
Author

Choose a reason for hiding this comment

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

Ohh I must have missed this requirement, my understanding was that we needed to use the utxos provided by the users only to make a decision on what to send to an address. I did not realize we had to use all the inputs. I would have to close this PR then to use the psbt API on lnd side.

Ononiwu Maureen added 3 commits March 5, 2024 03:04
@Chinwendu20 Chinwendu20 marked this pull request as ready for review March 5, 2024 02:15
@Chinwendu20
Copy link
Author

I think we can continue with this approach, more straightforward than the psbt API, having to fund, finalize and all that, this is more direct. Made some changes with respect to the requirement as well.

Copy link

@sputn1ck sputn1ck left a comment

Choose a reason for hiding this comment

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

Approach seems correct now! Just left some comments that would clean up the pr a bit

wallet/wallet.go Outdated Show resolved Hide resolved
wallet/createtx.go Outdated Show resolved Hide resolved
wallet/createtx.go Show resolved Hide resolved
wallet/createtx.go Outdated Show resolved Hide resolved
wallet/createtx.go Outdated Show resolved Hide resolved
wallet/createtx_test.go Outdated Show resolved Hide resolved
@@ -399,3 +402,96 @@ func TestCreateSimpleCustomChange(t *testing.T) {
require.Equal(t, scriptType, txscript.WitnessV0PubKeyHashTy)
}
}

// TestCreateSimpleCustomChange tests that it's possible to let the
// CreateSimpleTx use all coins for coin selection, but specify a custom scope
Copy link
Author

Choose a reason for hiding this comment

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

Fix comment.

@Chinwendu20 Chinwendu20 force-pushed the sendcoin branch 6 times, most recently from ed1195c to 508c3a0 Compare March 8, 2024 13:26
Copy link
Collaborator

@guggero guggero left a comment

Choose a reason for hiding this comment

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

Nice work! I like that we can re-use the constant input source for this.
Code looks good apart from a few code style nits.

wallet/wallet.go Outdated Show resolved Hide resolved
wallet/wallet.go Outdated Show resolved Hide resolved
wallet/wallet.go Outdated Show resolved Hide resolved
wallet/wallet.go Outdated Show resolved Hide resolved
wallet/createtx.go Outdated Show resolved Hide resolved
wallet/createtx.go Outdated Show resolved Hide resolved
wallet/createtx.go Outdated Show resolved Hide resolved
}
}

arrangedCoins, err := coinSelectionStrategy.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe we should just rename coinSelectionStrategy to simply strategy to avoid this weird folding. The type contains enough information to warrant this shorter name IMO.

Copy link
Author

Choose a reason for hiding this comment

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

I would change the name to that. But I am curious in the case we are not able to change the name, what would be the best way to handle this situation?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Then the way you formatted it is would have been correct. I just really personally don't like that style, so I always try to avoid it if possible. There are a couple of tricks you can use to achieve that: Shorten the name of the struct the method is called on, shorten the method name, extract the code into its own function/method to reduce indentation, reduce indentation by avoiding else blocks where it makes sense (e.g. return in the if, so the else block can continue un-indented) and so on.

wallet/createtx_test.go Outdated Show resolved Hide resolved
wallet/createtx_test.go Outdated Show resolved Hide resolved
@Chinwendu20 Chinwendu20 force-pushed the sendcoin branch 2 times, most recently from 0964003 to 3a16e1f Compare March 14, 2024 07:19
Copy link
Collaborator

@guggero guggero left a comment

Choose a reason for hiding this comment

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

Final nits, otherwise LGTM 🎉

wallet/wallet.go Outdated
coinSelectionStrategy, label, selectedUtxos...)
}

// sendOutputs creates and sends payment transactions.It returns the transaction
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: missing space after full stop.

e, ok := eligibleByOutpoint[outpoint]

if !ok {
return fmt.Errorf("selected outpoint"+
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: missing space after word "outpoint".

Copy link

@sputn1ck sputn1ck left a comment

Choose a reason for hiding this comment

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

LGTM!

@guggero guggero merged commit 4342a56 into btcsuite:master Mar 18, 2024
3 checks passed
buck54321 pushed a commit to buck54321/btcwallet that referenced this pull request Apr 21, 2024
Use selected Utxos while crafting txn
buck54321 pushed a commit to buck54321/btcwallet that referenced this pull request Apr 21, 2024
Use selected Utxos while crafting txn
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