Skip to content

funding: use p2tr by default for batch_open_channel#7603

Merged
Roasbeef merged 2 commits intolightningnetwork:masterfrom
Torakushi:batch_open_channel_p2tr
Apr 17, 2023
Merged

funding: use p2tr by default for batch_open_channel#7603
Roasbeef merged 2 commits intolightningnetwork:masterfrom
Torakushi:batch_open_channel_p2tr

Conversation

@Torakushi
Copy link
Copy Markdown

fixes #7601

In LND v0.15.1, batch_open_channel used p2tr by default for change output. However, in a later version, a breaking change has been fixed in FundPSBT to make the change type configurable (default to p2wkh). As batch_open_channel uses FundPsbt, we need to put back p2tr as default for change output for this specific case

@Torakushi
Copy link
Copy Markdown
Author

@ziggie1984

@saubyk saubyk added this to the v0.17.0 milestone Apr 16, 2023
@saubyk saubyk added funding Related to the opening of new channels with funding transactions on the blockchain taproot labels Apr 16, 2023
Copy link
Copy Markdown
Collaborator

@ziggie1984 ziggie1984 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 this quick PR, left some comments :)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I am not sure if the change-output is always the last output though, maybe just find the change by excluding the amounts? Its maybe constant for this scenario but not generally speaking because its sorted I think in the btcwallet package because there the funding takes place.

Could you explain your thought behind your formula ?

Copy link
Copy Markdown
Author

@Torakushi Torakushi Apr 17, 2023

Choose a reason for hiding this comment

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

This formula doesn't take the last output. Here is the explanation!

If you have M channels points and 1 change-output, then the indexes go from 0 to M.
We know the OutputIndex of every channel point, so we just need to find the missing int between 0 and M.

To do that, we know how to calculate the sum of the M consecutive int (aka if there is no missing int):

Ideal_Sum= M*(M+1)/2

In our case, we know the sum of all the channel output indexes, so, to find out the missing int, we just need to take the difference:

Ideal_Sum- Sum(Channels outpoint indexes)

In our case M=3, so you have the formula in the code (Ideal_Sum=6)

With an example: if channel indexes are 0,1,3 then change output index is 6-(0+1+3)=2

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for your explanation, nice again something learned :). Not sure if this ideal sum thing is common knowledge but I would suggest a nice comment maybe ?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

no problem! :) will add it!

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: your comment describes the code I would have prefered something like this:

Suggestion:

//For calculating the change output index we use the formula for consecutive sum of integers n(n+1)/2. In our case n=3. All the channel point indices are known so we just calculate the difference to get the change output index`

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Pretty neat indeed.

@Torakushi Torakushi marked this pull request as ready for review April 17, 2023 06:43
@Torakushi Torakushi force-pushed the batch_open_channel_p2tr branch 3 times, most recently from 41663f1 to 978d4e5 Compare April 17, 2023 07:20
@guggero guggero self-requested a review April 17, 2023 07:48
Copy link
Copy Markdown
Collaborator

@ziggie1984 ziggie1984 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, LGTM

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: your comment describes the code I would have prefered something like this:

Suggestion:

//For calculating the change output index we use the formula for consecutive sum of integers n(n+1)/2. In our case n=3. All the channel point indices are known so we just calculate the difference to get the change output index`

@Torakushi Torakushi force-pushed the batch_open_channel_p2tr branch from 978d4e5 to baf09db Compare April 17, 2023 08:17
Copy link
Copy Markdown
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.

Thanks for the fix, LGTM 🎉

funding/batch.go Outdated
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nit: move to above the struct initialization to avoid line too long.

	firstReq := b.channels[0].fundingReq
	feeRateSatPerKVByte := firstReq.FundingFeePerKw.FeePerKVByte()
	changeType := walletrpc.ChangeAddressType_CHANGE_ADDRESS_TYPE_P2TR
	fundPsbtReq := &walletrpc.FundPsbtRequest{
		Template: &walletrpc.FundPsbtRequest_Raw{
			Raw: txTemplate,
		},
		Fees: &walletrpc.FundPsbtRequest_SatPerVbyte{
			SatPerVbyte: uint64(feeRateSatPerKVByte) / 1000,
		},
		MinConfs:         firstReq.MinConfs,
		SpendUnconfirmed: firstReq.MinConfs == 0,
		ChangeType:       changeType,
	}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can use txscript.ParsePkScript instead which returns a PkScript that has a Class() method.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Pretty neat indeed.

Torakushi added 2 commits April 17, 2023 17:37
In LND v0.15.1, batch_open_channel used p2tr by default
for change output. However, in a later version, a breaking
change has been fixed in FundPSBT to make the change type
configurable (default to p2wkh). As batch_open_channel
uses FundPsbt, we need to put back p2tr as default for
change output
@Torakushi Torakushi force-pushed the batch_open_channel_p2tr branch from baf09db to 894fc9f Compare April 17, 2023 15:39
Copy link
Copy Markdown
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 🛵

@Roasbeef Roasbeef merged commit 37911e2 into lightningnetwork:master Apr 17, 2023
@Roasbeef Roasbeef modified the milestones: v0.17.0, v0.16.1 Apr 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

funding Related to the opening of new channels with funding transactions on the blockchain taproot

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug]: batchopenchannel return the change on a P2WPKH address instead of P2TR

5 participants