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

Simplify coin selection for sendcoins #8516

Closed
wants to merge 10 commits into from

Conversation

Chinwendu20
Copy link
Contributor

@Chinwendu20 Chinwendu20 commented Mar 4, 2024

Change Description

This PR fixes this issue: #6949 (comment)
Depends on: btcsuite/btcwallet#912 and #8729

In this well detailed issue comment, the process of sending funds using utxos selected by the sender currently has over five steps in which one has to call various psbt APIs.

The goal of this PR is to improve user experience by including utxos to the sendcoins command and corresponding rpc structs (SendCoinsRequest and SendCoinsResponse ) which would enable the sender described to achieve the same aim with one command.

This is done by adding a new field to rpc structs SendCoinsRequest and SendCoinsResponse to enable users request for that functionality and enable the driving function (in which we modify its functionality as well) use these utxos when crafting the transaction for this functionality.

Additionally, the sweepall field in the above mentioned request and response rpc structs when true now not only sweeps ALL funds in the wallet but also ALL funds in the selected utxos when used in conjunction with the new select utxos field.

The lncli sendcoins command was updated to include the flag, utxo to enable this functionality on that end.

There were also slight internal logic change, where a slice of utxos are now accepted by relevant functions as variadic argument, functional options to enable this functionality.

Steps to Test

Steps for reviewers to follow to test the change.

Pull Request Checklist

Testing

  • Your PR passes all CI checks.
  • Tests covering the positive and negative (error paths) are included.
  • Bug fixes contain tests triggering the bug to prevent regressions.

Code Style and Documentation

📝 Please see our Contribution Guidelines for further guidance.

Copy link
Contributor

coderabbitai bot commented Mar 4, 2024

Important

Review skipped

Auto reviews are limited to specific labels.

Labels to auto review (1)
  • llm-review

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@Chinwendu20
Copy link
Contributor Author

Hello @hieblmi here is the PR

@guggero guggero requested review from guggero and removed request for Roasbeef, yyforyongyu and ziggie1984 March 4, 2024 08:44
sweep/walletsweep.go Outdated Show resolved Hide resolved
@Chinwendu20 Chinwendu20 marked this pull request as draft March 4, 2024 11:05
@Chinwendu20
Copy link
Contributor Author

Chinwendu20 commented Mar 4, 2024

I would be using the psbt API instead as this approach would not craft a transaction with all inputs selected by the user as pointed out here:
btcsuite/btcwallet#912 (comment)

Modified it, no need for this

@Chinwendu20
Copy link
Contributor Author

Hello @sputn1ck, do you think I should create a different function instead of adding an arg to an exported one as per your comment here: btcsuite/btcwallet#912 (comment)

@ellemouton ellemouton requested review from sputn1ck and removed request for Crypt-iQ and ellemouton March 11, 2024 13:42
@bennyhodl
Copy link

tACK.

Tested locally and worked as expected with one utxo, sweep, and multiple utxos. Would like to see this merged as I am signing PSBTs manually because my application requires 1 input --> 1 output transactions.

@guggero guggero removed their request for review March 13, 2024 16:30
@bennyhodl
Copy link

Is there an update on this @Chinwendu20 ?

@Chinwendu20
Copy link
Contributor Author

Is there an update on this @Chinwendu20 ?

Thanks I should push an update by the end of this week..

@Chinwendu20 Chinwendu20 force-pushed the sendcoins branch 2 times, most recently from b6bede7 to 3677d4b Compare June 7, 2024 10:15
Copy link
Collaborator

@hieblmi hieblmi left a comment

Choose a reason for hiding this comment

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

tACK, apart from the failing itest cases which seem to be unrelated to this PR I have nits left.

Great work again on this @Chinwendu20.

@@ -974,12 +974,13 @@ func (b *BtcWallet) ImportTaprootScript(scope waddrmgr.KeyScope,
// the specified outputs. In the case the wallet has insufficient funds, or the
// outputs are non-standard, a non-nil error will be returned.
//
// NOTE: This method requires the global coin selection lock to be held.
// NOTE: This method requires the global coin selection lock to be held.n
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: remove n

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 not addressed

cli.StringSliceFlag{
Name: "utxo",
Usage: "a utxo specified as outpoint(tx:idx) which " +
"will be used as input for 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: period instead of ;

@@ -384,6 +384,9 @@
required](https://github.com/lightningnetwork/lnd/pull/8681) to be consistent
with the flow when a payment request isn't used.

* [Added `outpoints` to `SendCoinsRequest`](
Copy link
Collaborator

Choose a reason for hiding this comment

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

move to release notes for v0.18.1-beta.

Signed-off-by: Ononiwu Maureen <59079323+Chinwendu20@users.noreply.github.com>
@Chinwendu20 Chinwendu20 force-pushed the sendcoins branch 2 times, most recently from 23df67f to 7065b64 Compare June 12, 2024 14:00
@Chinwendu20
Copy link
Contributor Author

@guggero we have two approvals now, I think we can merge?

@hieblmi
Copy link
Collaborator

hieblmi commented Jun 13, 2024

@guggero we have two approvals now, I think we can merge?

Wanted to make sure that @yyforyongyu's CRs were addressed, would be good to get his go too.

Copy link
Collaborator

@yyforyongyu yyforyongyu left a comment

Choose a reason for hiding this comment

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

There's a missing check that we need to ensure provided utxos are known to the wallet.

@@ -974,12 +974,13 @@ func (b *BtcWallet) ImportTaprootScript(scope waddrmgr.KeyScope,
// the specified outputs. In the case the wallet has insufficient funds, or the
// outputs are non-standard, a non-nil error will be returned.
//
// NOTE: This method requires the global coin selection lock to be held.
// NOTE: This method requires the global coin selection lock to be held.n
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 not addressed

@@ -974,7 +974,7 @@ func (b *BtcWallet) ImportTaprootScript(scope waddrmgr.KeyScope,
// the specified outputs. In the case the wallet has insufficient funds, or the
// outputs are non-standard, a non-nil error will be returned.
//
// NOTE: This method requires the global coin selection lock to be held.n
// NOTE: This method requires the global coin selection lock to be held.
Copy link
Collaborator

Choose a reason for hiding this comment

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

ok i see it now, this should be cherry-picked to where it's introduced.

func CraftSweepAllTx(feeRate, maxFeeRate chainfee.SatPerKWeight,
blockHeight uint32, deliveryAddrs []DeliveryAddr,
changeAddr btcutil.Address, coinSelectLocker CoinSelectionLocker,
utxoSource UtxoSource, outputLeaser OutputLeaser,
signer input.Signer, minConfs int32) (*WalletSweepPackage, error) {
signer input.Signer, minConfs int32,
selectUtxos ...wire.OutPoint) (*WalletSweepPackage, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why do we want to use a variadic param rather than a slice here? I think it's easier if it's selectUtxos []wire.OutPoint) and we can pass the selectOutpoints instead of selectOutpoints... in SendCoins?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not really easier in my opinion as that would also mean we would have to make changes other places this function was called

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think the diff is fairly simple,

diff --git a/rpcserver.go b/rpcserver.go
index 80cff0763..619c635a8 100644
--- a/rpcserver.go
+++ b/rpcserver.go
@@ -1389,7 +1389,7 @@ func (r *rpcServer) SendCoins(ctx context.Context,
 		sweepTxPkg, err := sweep.CraftSweepAllTx(
 			feePerKw, maxFeeRate, uint32(bestHeight), nil,
 			targetAddr, wallet, wallet, wallet.WalletController,
-			r.server.cc.Signer, minConfs, selectOutpoints...,
+			r.server.cc.Signer, minConfs, selectOutpoints,
 		)
 		if err != nil {
 			return nil, err
@@ -1444,7 +1444,7 @@ func (r *rpcServer) SendCoins(ctx context.Context,
 				outputs, targetAddr, wallet, wallet,
 				wallet.WalletController,
 				r.server.cc.Signer, minConfs,
-				selectOutpoints...,
+				selectOutpoints,
 			)
 			if err != nil {
 				return nil, err
diff --git a/sweep/walletsweep.go b/sweep/walletsweep.go
index f0ec70e7c..1fa2068ea 100644
--- a/sweep/walletsweep.go
+++ b/sweep/walletsweep.go
@@ -220,7 +220,7 @@ func CraftSweepAllTx(feeRate, maxFeeRate chainfee.SatPerKWeight,
 	changeAddr btcutil.Address, coinSelectLocker CoinSelectionLocker,
 	utxoSource UtxoSource, outputLeaser OutputLeaser,
 	signer input.Signer, minConfs int32,
-	selectUtxos ...wire.OutPoint) (*WalletSweepPackage, error) {
+	selectUtxos []wire.OutPoint) (*WalletSweepPackage, error) {
 
 	// TODO(roasbeef): turn off ATPL as well when available?
 
diff --git a/sweep/walletsweep_test.go b/sweep/walletsweep_test.go
index 8cc93c941..724b9d85d 100644
--- a/sweep/walletsweep_test.go
+++ b/sweep/walletsweep_test.go
@@ -346,7 +346,7 @@ func TestCraftSweepAllTxCoinSelectFail(t *testing.T) {
 
 	_, err := CraftSweepAllTx(
 		0, 0, 10, nil, nil, coinSelectLocker, utxoSource, utxoLeaser,
-		nil, 0,
+		nil, 0, nil,
 	)
 
 	// Since we instructed the coin select locker to fail above, we should
@@ -372,7 +372,7 @@ func TestCraftSweepAllTxUnknownWitnessType(t *testing.T) {
 
 	_, err := CraftSweepAllTx(
 		0, 0, 10, nil, nil, coinSelectLocker, utxoSource, utxoLeaser,
-		nil, 0,
+		nil, 0, nil,
 	)
 
 	// Since passed in a p2wsh output, which is unknown, we should fail to
@@ -461,7 +461,7 @@ func TestCraftSweepAllTx(t *testing.T) {
 			sweepPkg, err := CraftSweepAllTx(
 				0, 0, 10, nil, deliveryAddr, coinSelectLocker,
 				utxoSource, utxoLeaser, signer, 0,
-				tc.selectUtxos...,
+				tc.selectUtxos,
 			)
 			if tc.errString != "" {
 				require.ErrorContains(t, err, tc.errString)

@@ -260,6 +263,19 @@ func CraftSweepAllTx(feeRate, maxFeeRate chainfee.SatPerKWeight,

log.Trace("[WithCoinSelectLock] finished fetching UTXOs")

// Use select utxos, if provided.
if len(selectUtxos) > 0 {
if fn.HasDuplicates(selectUtxos) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This check has been repeated multiple times - if it's already done in SendCoins so there's no need to check it here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

CraftSweepAllTx is an independent function it is not bound to be used in sendcoins only, so I think it makes sense that it carries out this check as well

Copy link
Collaborator

Choose a reason for hiding this comment

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

In that case, all other places should remove this check, there's no need to duplicate this logic and we should implement it once - easier to test and maintain.

Copy link
Collaborator

Choose a reason for hiding this comment

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

A better option is to use set (map) - that way we don't even need to worry about duplicates.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Or maybe the Set structure in the fn package? What do you think?

selectUtxos: nil,
},
{
name: "sweep select utxos in wallet, no error",
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: select -> selected, same below

Copy link
Contributor Author

Choose a reason for hiding this comment

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

select can actually be used as an adjective

if ctx.IsSet("utxo") {
utxos := ctx.StringSlice("utxo")

if fn.HasDuplicates(utxos) {
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 checked again - we should enforce the single responsibility principle and have only one place to implement this logic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

prelim checks. I think I have seen this done a couple of places for lncli

Copy link
Collaborator

Choose a reason for hiding this comment

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

In that case, we should also fix them in future PRs.

@@ -376,14 +376,15 @@ func (h *HarnessRPC) SendCoins(
return resp
}

// SendCoinsAssertErr sends a given amount of money to the specified address
// from the passed node and asserts an error has returned.
func (h *HarnessRPC) SendCoinsAssertErr(req *lnrpc.SendCoinsRequest) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we stick to the XXXAssertErr pattern? That's the pattern used in all other assertions, OpenChannelAssertErr, ConnectPeerAssertErr, etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is acctually a logic change in this function. Hence AssertErr might not be really descriptive

ctxt, cancel := context.WithTimeout(h.runCtx, DefaultTimeout)
defer cancel()

_, err := h.LN.SendCoins(ctxt, req)
require.Error(h, err, "node %s didn't not return an error", h.Name)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we can keep this assertion.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@@ -13,6 +13,7 @@ require (
github.com/btcsuite/btcwallet v0.16.10-0.20240404104514-b2f31f9045fb
github.com/btcsuite/btcwallet/wallet/txauthor v1.3.4
github.com/btcsuite/btcwallet/wallet/txrules v1.2.1
github.com/btcsuite/btcwallet/wallet/txsizes v1.2.4
Copy link
Collaborator

Choose a reason for hiding this comment

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

hmmm how did you create this commit? Like what was the command used?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

make tidy-module

Txid: sweepHash[:],
Label: "label that will not work",
Overwrite: false,
tcs := []struct {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I've laid out my view on table-driven test here and I think we should avoid it in itest.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In general, are you saying that if a function has more than two args we should not use TDD for it?

I like using table-driven tests when the outputs of a function are only determined by one or two inputs in a test setup,

Copy link
Collaborator

Choose a reason for hiding this comment

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

It depends - but we should discourage using it in itest because it's difficult to maintain.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am sorry I am having difficulty refactoring this to not use TDD. I am not sure what the non TDD version should look like. Would it be something like this?

similarfunction := func(t *testing.T, req *sendcoinsRequest, someconditionalsthatwecheck){
----
}

// With select utxos
ht.Run(){
similarfunction()
}

// With no select utxos
ht.Run(){
similarfunction()
}

Would I need to refactor testsendcoinsselectutxos as well?

@Chinwendu20
Copy link
Contributor Author

@yyforyongyu I think we do that already? I responded to that in the specific code piece you wrote the review in. Let me know if that works.

@saubyk saubyk removed the request for review from sputn1ck June 17, 2024 03:25
Signed-off-by: Ononiwu Maureen <59079323+Chinwendu20@users.noreply.github.com>
Signed-off-by: Ononiwu Maureen <59079323+Chinwendu20@users.noreply.github.com>
Signed-off-by: Ononiwu Maureen <59079323+Chinwendu20@users.noreply.github.com>
Signed-off-by: Ononiwu Maureen <59079323+Chinwendu20@users.noreply.github.com>
Signed-off-by: Ononiwu Maureen <59079323+Chinwendu20@users.noreply.github.com>
This commit changes `SendCoinsAssertErr` to `SendCoinsReturnErr`. The
function now returns the resulting error from calling `sendCoins`.

Signed-off-by: Ononiwu Maureen <59079323+Chinwendu20@users.noreply.github.com>
Signed-off-by: Ononiwu Maureen <59079323+Chinwendu20@users.noreply.github.com>
Signed-off-by: Ononiwu Maureen <59079323+Chinwendu20@users.noreply.github.com>
Signed-off-by: Ononiwu Maureen <59079323+Chinwendu20@users.noreply.github.com>
@saubyk saubyk added the P3 might get fixed, nice to have label Jun 25, 2024
@saubyk saubyk removed this from the v0.18.3 milestone Aug 1, 2024
@lightninglabs-deploy
Copy link

@yyforyongyu: review reminder
@Chinwendu20, remember to re-request review from reviewers when ready

@guggero
Copy link
Collaborator

guggero commented Aug 5, 2024

Replaced by #8955.

@guggero guggero closed this Aug 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P3 might get fixed, nice to have
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants