Skip to content

Commit f1cac8e

Browse files
committed
lnwallet+peer: extract close types to separate pkg
The aux close types will soon be used by a different package that would otherwise cause an import cycle if used directly from lnwallet/chancloser. We now create a new sub-package lnwallet/types that will be improrted from all users of these types.
1 parent 6ade31d commit f1cac8e

File tree

5 files changed

+106
-91
lines changed

5 files changed

+106
-91
lines changed

lnwallet/chancloser/aux_closer.go

Lines changed: 5 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,13 @@
11
package chancloser
22

33
import (
4-
"github.com/btcsuite/btcd/btcec/v2"
5-
"github.com/btcsuite/btcd/btcutil"
64
"github.com/btcsuite/btcd/wire"
75
"github.com/lightningnetwork/lnd/fn/v2"
86
"github.com/lightningnetwork/lnd/lnwallet"
7+
"github.com/lightningnetwork/lnd/lnwallet/types"
98
"github.com/lightningnetwork/lnd/lnwire"
10-
"github.com/lightningnetwork/lnd/tlv"
119
)
1210

13-
// CloseOutput represents an output that should be included in the close
14-
// transaction.
15-
type CloseOutput struct {
16-
// Amt is the amount of the output.
17-
Amt btcutil.Amount
18-
19-
// DustLimit is the dust limit for the local node.
20-
DustLimit btcutil.Amount
21-
22-
// PkScript is the script that should be used to pay to the output.
23-
PkScript []byte
24-
25-
// ShutdownRecords is the set of custom records that may result in
26-
// extra close outputs being added.
27-
ShutdownRecords lnwire.CustomRecords
28-
}
29-
30-
// AuxShutdownReq is used to request a set of extra custom records to include
31-
// in the shutdown message.
32-
type AuxShutdownReq struct {
33-
// ChanPoint is the channel point of the channel that is being shut
34-
// down.
35-
ChanPoint wire.OutPoint
36-
37-
// ShortChanID is the short channel ID of the channel that is being
38-
// closed.
39-
ShortChanID lnwire.ShortChannelID
40-
41-
// Initiator is true if the local node is the initiator of the channel.
42-
Initiator bool
43-
44-
// InternalKey is the internal key for the shutdown addr. This will
45-
// only be set for taproot shutdown addrs.
46-
InternalKey fn.Option[btcec.PublicKey]
47-
48-
// CommitBlob is the blob that was included in the last commitment.
49-
CommitBlob fn.Option[tlv.Blob]
50-
51-
// FundingBlob is the blob that was included in the funding state.
52-
FundingBlob fn.Option[tlv.Blob]
53-
}
54-
55-
// AuxCloseDesc is used to describe the channel close that is being performed.
56-
type AuxCloseDesc struct {
57-
AuxShutdownReq
58-
59-
// CloseFee is the closing fee to be paid for this state.
60-
CloseFee btcutil.Amount
61-
62-
// CommitFee is the fee that was paid for the last commitment.
63-
CommitFee btcutil.Amount
64-
65-
// LocalCloseOutput is the output that the local node should be paid
66-
// to. This is None if the local party will not have an output on the
67-
// co-op close transaction.
68-
LocalCloseOutput fn.Option[CloseOutput]
69-
70-
// RemoteCloseOutput is the output that the remote node should be paid
71-
// to. This will be None if the remote party will not have an output on
72-
// the co-op close transaction.
73-
RemoteCloseOutput fn.Option[CloseOutput]
74-
}
75-
7611
// AuxCloseOutputs is used to specify extra outputs that should be used when
7712
// constructing the co-op close transaction.
7813
type AuxCloseOutputs struct {
@@ -91,14 +26,15 @@ type AuxCloseOutputs struct {
9126
type AuxChanCloser interface {
9227
// ShutdownBlob returns the set of custom records that should be
9328
// included in the shutdown message.
94-
ShutdownBlob(req AuxShutdownReq) (fn.Option[lnwire.CustomRecords],
29+
ShutdownBlob(req types.AuxShutdownReq) (fn.Option[lnwire.CustomRecords],
9530
error)
9631

9732
// AuxCloseOutputs returns the set of custom outputs that should be used
9833
// to construct the co-op close transaction.
99-
AuxCloseOutputs(desc AuxCloseDesc) (fn.Option[AuxCloseOutputs], error)
34+
AuxCloseOutputs(desc types.AuxCloseDesc) (fn.Option[AuxCloseOutputs],
35+
error)
10036

10137
// FinalizeClose is called after the close transaction has been agreed
10238
// upon.
103-
FinalizeClose(desc AuxCloseDesc, closeTx *wire.MsgTx) error
39+
FinalizeClose(desc types.AuxCloseDesc, closeTx *wire.MsgTx) error
10440
}

lnwallet/chancloser/chancloser.go

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/lightningnetwork/lnd/lnutils"
2020
"github.com/lightningnetwork/lnd/lnwallet"
2121
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
22+
"github.com/lightningnetwork/lnd/lnwallet/types"
2223
"github.com/lightningnetwork/lnd/lnwire"
2324
)
2425

@@ -239,12 +240,12 @@ type ChanCloser struct {
239240
// localCloseOutput is the local output on the closing transaction that
240241
// the local party should be paid to. This will only be populated if the
241242
// local balance isn't dust.
242-
localCloseOutput fn.Option[CloseOutput]
243+
localCloseOutput fn.Option[types.CloseOutput]
243244

244245
// remoteCloseOutput is the remote output on the closing transaction
245246
// that the remote party should be paid to. This will only be populated
246247
// if the remote balance isn't dust.
247-
remoteCloseOutput fn.Option[CloseOutput]
248+
remoteCloseOutput fn.Option[types.CloseOutput]
248249

249250
// auxOutputs are the optional additional outputs that might be added to
250251
// the closing transaction.
@@ -378,14 +379,17 @@ func (c *ChanCloser) initChanShutdown() (*lnwire.Shutdown, error) {
378379
// At this point, we'll check to see if we have any custom records to
379380
// add to the shutdown message.
380381
err := fn.MapOptionZ(c.cfg.AuxCloser, func(a AuxChanCloser) error {
381-
shutdownCustomRecords, err := a.ShutdownBlob(AuxShutdownReq{
382-
ChanPoint: c.chanPoint,
383-
ShortChanID: c.cfg.Channel.ShortChanID(),
384-
Initiator: c.cfg.Channel.IsInitiator(),
385-
InternalKey: c.localInternalKey,
386-
CommitBlob: c.cfg.Channel.LocalCommitmentBlob(),
387-
FundingBlob: c.cfg.Channel.FundingBlob(),
388-
})
382+
channel := c.cfg.Channel
383+
shutdownCustomRecords, err := a.ShutdownBlob(
384+
types.AuxShutdownReq{
385+
ChanPoint: c.chanPoint,
386+
ShortChanID: channel.ShortChanID(),
387+
Initiator: channel.IsInitiator(),
388+
InternalKey: c.localInternalKey,
389+
CommitBlob: channel.LocalCommitmentBlob(),
390+
FundingBlob: channel.FundingBlob(),
391+
},
392+
)
389393
if err != nil {
390394
return err
391395
}
@@ -442,7 +446,7 @@ func (c *ChanCloser) initChanShutdown() (*lnwire.Shutdown, error) {
442446
// it might still carry value in custom channel terms.
443447
_, dustAmt := c.cfg.Channel.LocalBalanceDust()
444448
localBalance, _ := c.cfg.Channel.CommitBalances()
445-
c.localCloseOutput = fn.Some(CloseOutput{
449+
c.localCloseOutput = fn.Some(types.CloseOutput{
446450
Amt: localBalance,
447451
DustLimit: dustAmt,
448452
PkScript: c.localDeliveryScript,
@@ -519,12 +523,12 @@ func (c *ChanCloser) NegotiationHeight() uint32 {
519523
}
520524

521525
// LocalCloseOutput returns the local close output.
522-
func (c *ChanCloser) LocalCloseOutput() fn.Option[CloseOutput] {
526+
func (c *ChanCloser) LocalCloseOutput() fn.Option[types.CloseOutput] {
523527
return c.localCloseOutput
524528
}
525529

526530
// RemoteCloseOutput returns the remote close output.
527-
func (c *ChanCloser) RemoteCloseOutput() fn.Option[CloseOutput] {
531+
func (c *ChanCloser) RemoteCloseOutput() fn.Option[types.CloseOutput] {
528532
return c.remoteCloseOutput
529533
}
530534

@@ -590,7 +594,7 @@ func (c *ChanCloser) ReceiveShutdown(msg lnwire.Shutdown) (
590594
// terms, it might still carry value in custom channel terms.
591595
_, dustAmt := c.cfg.Channel.RemoteBalanceDust()
592596
_, remoteBalance := c.cfg.Channel.CommitBalances()
593-
c.remoteCloseOutput = fn.Some(CloseOutput{
597+
c.remoteCloseOutput = fn.Some(types.CloseOutput{
594598
Amt: remoteBalance,
595599
DustLimit: dustAmt,
596600
PkScript: msg.Address,
@@ -976,15 +980,15 @@ func (c *ChanCloser) ReceiveClosingSigned( //nolint:funlen
976980
c.cfg.AuxCloser, func(aux AuxChanCloser) error {
977981
channel := c.cfg.Channel
978982
//nolint:ll
979-
req := AuxShutdownReq{
983+
req := types.AuxShutdownReq{
980984
ChanPoint: c.chanPoint,
981985
ShortChanID: c.cfg.Channel.ShortChanID(),
982986
InternalKey: c.localInternalKey,
983987
Initiator: channel.IsInitiator(),
984988
CommitBlob: channel.LocalCommitmentBlob(),
985989
FundingBlob: channel.FundingBlob(),
986990
}
987-
desc := AuxCloseDesc{
991+
desc := types.AuxCloseDesc{
988992
AuxShutdownReq: req,
989993
LocalCloseOutput: c.localCloseOutput,
990994
RemoteCloseOutput: c.remoteCloseOutput,
@@ -1053,15 +1057,15 @@ func (c *ChanCloser) auxCloseOutputs(
10531057

10541058
var closeOuts fn.Option[AuxCloseOutputs]
10551059
err := fn.MapOptionZ(c.cfg.AuxCloser, func(aux AuxChanCloser) error {
1056-
req := AuxShutdownReq{
1060+
req := types.AuxShutdownReq{
10571061
ChanPoint: c.chanPoint,
10581062
ShortChanID: c.cfg.Channel.ShortChanID(),
10591063
InternalKey: c.localInternalKey,
10601064
Initiator: c.cfg.Channel.IsInitiator(),
10611065
CommitBlob: c.cfg.Channel.LocalCommitmentBlob(),
10621066
FundingBlob: c.cfg.Channel.FundingBlob(),
10631067
}
1064-
outs, err := aux.AuxCloseOutputs(AuxCloseDesc{
1068+
outs, err := aux.AuxCloseOutputs(types.AuxCloseDesc{
10651069
AuxShutdownReq: req,
10661070
CloseFee: closeFee,
10671071
CommitFee: c.cfg.Channel.CommitFee(),

lnwallet/types/close_types.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package types
2+
3+
import (
4+
"github.com/btcsuite/btcd/btcec/v2"
5+
"github.com/btcsuite/btcd/btcutil"
6+
"github.com/btcsuite/btcd/wire"
7+
"github.com/lightningnetwork/lnd/fn/v2"
8+
"github.com/lightningnetwork/lnd/lnwire"
9+
"github.com/lightningnetwork/lnd/tlv"
10+
)
11+
12+
// CloseOutput represents an output that should be included in the close
13+
// transaction.
14+
type CloseOutput struct {
15+
// Amt is the amount of the output.
16+
Amt btcutil.Amount
17+
18+
// DustLimit is the dust limit for the local node.
19+
DustLimit btcutil.Amount
20+
21+
// PkScript is the script that should be used to pay to the output.
22+
PkScript []byte
23+
24+
// ShutdownRecords is the set of custom records that may result in
25+
// extra close outputs being added.
26+
ShutdownRecords lnwire.CustomRecords
27+
}
28+
29+
// AuxShutdownReq is used to request a set of extra custom records to include
30+
// in the shutdown message.
31+
type AuxShutdownReq struct {
32+
// ChanPoint is the channel point of the channel that is being shut
33+
// down.
34+
ChanPoint wire.OutPoint
35+
36+
// ShortChanID is the short channel ID of the channel that is being
37+
// closed.
38+
ShortChanID lnwire.ShortChannelID
39+
40+
// Initiator is true if the local node is the initiator of the channel.
41+
Initiator bool
42+
43+
// InternalKey is the internal key for the shutdown addr. This will
44+
// only be set for taproot shutdown addrs.
45+
InternalKey fn.Option[btcec.PublicKey]
46+
47+
// CommitBlob is the blob that was included in the last commitment.
48+
CommitBlob fn.Option[tlv.Blob]
49+
50+
// FundingBlob is the blob that was included in the funding state.
51+
FundingBlob fn.Option[tlv.Blob]
52+
}
53+
54+
// AuxCloseDesc is used to describe the channel close that is being performed.
55+
type AuxCloseDesc struct {
56+
AuxShutdownReq
57+
58+
// CloseFee is the closing fee to be paid for this state.
59+
CloseFee btcutil.Amount
60+
61+
// CommitFee is the fee that was paid for the last commitment.
62+
CommitFee btcutil.Amount
63+
64+
// LocalCloseOutput is the output that the local node should be paid
65+
// to. This is None if the local party will not have an output on the
66+
// co-op close transaction.
67+
LocalCloseOutput fn.Option[CloseOutput]
68+
69+
// RemoteCloseOutput is the output that the remote node should be paid
70+
// to. This will be None if the remote party will not have an output on
71+
// the co-op close transaction.
72+
RemoteCloseOutput fn.Option[CloseOutput]
73+
}

peer/brontide.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import (
4444
"github.com/lightningnetwork/lnd/lnwallet"
4545
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
4646
"github.com/lightningnetwork/lnd/lnwallet/chancloser"
47+
"github.com/lightningnetwork/lnd/lnwallet/types"
4748
"github.com/lightningnetwork/lnd/lnwire"
4849
"github.com/lightningnetwork/lnd/msgmux"
4950
"github.com/lightningnetwork/lnd/netann"
@@ -168,12 +169,12 @@ type ChannelCloseUpdate struct {
168169
// LocalCloseOutput is an optional, additional output on the closing
169170
// transaction that the local party should be paid to. This will only be
170171
// populated if the local balance isn't dust.
171-
LocalCloseOutput fn.Option[chancloser.CloseOutput]
172+
LocalCloseOutput fn.Option[types.CloseOutput]
172173

173174
// RemoteCloseOutput is an optional, additional output on the closing
174175
// transaction that the remote party should be paid to. This will only
175176
// be populated if the remote balance isn't dust.
176-
RemoteCloseOutput fn.Option[chancloser.CloseOutput]
177+
RemoteCloseOutput fn.Option[types.CloseOutput]
177178

178179
// AuxOutputs is an optional set of additional outputs that might be
179180
// included in the closing transaction. These are used for custom

rpcserver.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ import (
6868
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
6969
"github.com/lightningnetwork/lnd/lnwallet/chancloser"
7070
"github.com/lightningnetwork/lnd/lnwallet/chanfunding"
71+
"github.com/lightningnetwork/lnd/lnwallet/types"
7172
"github.com/lightningnetwork/lnd/lnwire"
7273
"github.com/lightningnetwork/lnd/macaroons"
7374
paymentsdb "github.com/lightningnetwork/lnd/payments/db"
@@ -3050,7 +3051,7 @@ func createRPCCloseUpdate(
30503051

30513052
err := fn.MapOptionZ(
30523053
u.LocalCloseOutput,
3053-
func(closeOut chancloser.CloseOutput) error {
3054+
func(closeOut types.CloseOutput) error {
30543055
cr, err := closeOut.ShutdownRecords.Serialize()
30553056
if err != nil {
30563057
return fmt.Errorf("error serializing "+
@@ -3075,7 +3076,7 @@ func createRPCCloseUpdate(
30753076

30763077
err = fn.MapOptionZ(
30773078
u.RemoteCloseOutput,
3078-
func(closeOut chancloser.CloseOutput) error {
3079+
func(closeOut types.CloseOutput) error {
30793080
cr, err := closeOut.ShutdownRecords.Serialize()
30803081
if err != nil {
30813082
return fmt.Errorf("error serializing "+

0 commit comments

Comments
 (0)