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

lnd: use change output index from authoring TX to check reserved value #5665

Merged

Conversation

bjarnemagnussen
Copy link
Contributor

Description

Currently a whole UTXO is reserved when anchor-channels is used. This means that if the node has a single UTXO, it cannot be spend even if a change output would leave sufficient funds for the reserve in the wallet.

This PR makes use of the returned txauthor.AuthoredTx struct that contains a field for an eventual change index. The reason that this is not automatically detected by the CheckReservedValueTx function is because the authored transaction is created in dry-run, meaning it does not add the used change output to the wallet to watch for.

This PR hence assumes that the change index from the authored transaction returned by WalletController.CreateSimpleTx will indeed be a change output controlled by the wallet.

Fixes #5648

rpcserver.go Outdated
// If the reserved value was invalidated we make sure to explicitly
// check if a change output was undetected but that would leave
// sufficient funds for the reserved value.
case lnwallet.ErrReservedValueInvalidated:
Copy link
Member

Choose a reason for hiding this comment

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

What about moving this into CheckReservedValueTx (passing the full authored tx, etc?). This way we can more easily write additional unit tests for this, and also ensure that all other callers also get this fix.

Thinking about it more, we can likely put this behind an additional function (or modify the existing one) that is used to check if a transaction contains a change address or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The function CheckReservedValueTx now also takes an optional parameter ChangeIndex to explicitly define a change output (in addition to the check for internal wallet addresses that are being watched).

This check is currently also used when sweeping coins from the wallet. However, in that case a change address is created by calling NewAddress, which will immediately add it as being watched regardless if the sweeping being successful or not. This could now potentially be re-factored to instead add a change without adding it to the wallet, see if it succeeds by explicitly setting the change index, and then retrospectively adding it the wallet. This would be similar to how sending coins works right now.

@bjarnemagnussen bjarnemagnussen force-pushed the upstream/feat/single-utxo-reserved branch 2 times, most recently from 13e2250 to df7731e Compare August 27, 2021 09:24
@Roasbeef Roasbeef added this to the v0.14.0 milestone Aug 31, 2021
@Roasbeef Roasbeef added anchors bug fix P2 should be fixed if one has time labels Aug 31, 2021
@Crypt-iQ Crypt-iQ self-requested a review September 20, 2021 13:47
Copy link
Member

@Roasbeef Roasbeef left a comment

Choose a reason for hiding this comment

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

LGTM 🍉

Only some small code style nits

lnwallet/wallet.go Outdated Show resolved Hide resolved
lnwallet/wallet.go Outdated Show resolved Hide resolved
@Roasbeef
Copy link
Member

Also needs a release notes update

@Roasbeef Roasbeef removed the request for review from Crypt-iQ September 29, 2021 03:25
@bjarnemagnussen
Copy link
Contributor Author

Thanks @Roasbeef, I have addressed your last issues!

@Roasbeef
Copy link
Member

Roasbeef commented Oct 1, 2021

Needs a rebase then this should be g2g, thanks for working on this!

@bjarnemagnussen bjarnemagnussen force-pushed the upstream/feat/single-utxo-reserved branch from a8bb3ed to 1d84c1d Compare October 1, 2021 06:21
@bjarnemagnussen
Copy link
Contributor Author

Rebased

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 fix, LGTM 💯

@guggero guggero merged commit 036041d into lightningnetwork:master Oct 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
anchors bug fix P2 should be fixed if one has time
Projects
None yet
Development

Successfully merging this pull request may close these issues.

With an anchor-channel open having only a single UTXO cannot be selected for SendCoins/SendMany
3 participants