Skip to content

Commit 0996e4f

Browse files
committed
multi: refactor lnwallet/channel.go to use ChannelParty in select places
We also include changes to contractcourt, htlcswitch and peer to stitch the boundaries together.
1 parent 3393444 commit 0996e4f

File tree

10 files changed

+409
-361
lines changed

10 files changed

+409
-361
lines changed

contractcourt/chain_watcher.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/lightningnetwork/lnd/channeldb"
2121
"github.com/lightningnetwork/lnd/fn"
2222
"github.com/lightningnetwork/lnd/input"
23+
"github.com/lightningnetwork/lnd/lntypes"
2324
"github.com/lightningnetwork/lnd/lnutils"
2425
"github.com/lightningnetwork/lnd/lnwallet"
2526
"github.com/lightningnetwork/lnd/lnwire"
@@ -418,7 +419,7 @@ func (c *chainWatcher) handleUnknownLocalState(
418419
// and remote keys for this state. We use our point as only we can
419420
// revoke our own commitment.
420421
commitKeyRing := lnwallet.DeriveCommitmentKeys(
421-
commitPoint, true, c.cfg.chanState.ChanType,
422+
commitPoint, lntypes.Local, c.cfg.chanState.ChanType,
422423
&c.cfg.chanState.LocalChanCfg, &c.cfg.chanState.RemoteChanCfg,
423424
)
424425

@@ -891,7 +892,7 @@ func (c *chainWatcher) handlePossibleBreach(commitSpend *chainntnfs.SpendDetail,
891892
// Create an AnchorResolution for the breached state.
892893
anchorRes, err := lnwallet.NewAnchorResolution(
893894
c.cfg.chanState, commitSpend.SpendingTx, retribution.KeyRing,
894-
false,
895+
lntypes.Remote,
895896
)
896897
if err != nil {
897898
return false, fmt.Errorf("unable to create anchor "+

htlcswitch/link.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2730,7 +2730,12 @@ func (l *channelLink) MayAddOutgoingHtlc(amt lnwire.MilliSatoshi) error {
27302730
func (l *channelLink) getDustSum(remote bool,
27312731
dryRunFee fn.Option[chainfee.SatPerKWeight]) lnwire.MilliSatoshi {
27322732

2733-
return l.channel.GetDustSum(remote, dryRunFee)
2733+
party := lntypes.Local
2734+
if remote {
2735+
party = lntypes.Remote
2736+
}
2737+
2738+
return l.channel.GetDustSum(party, dryRunFee)
27342739
}
27352740

27362741
// getFeeRate is a wrapper method that retrieves the underlying channel's
@@ -2893,13 +2898,13 @@ func dustHelper(chantype channeldb.ChannelType, localDustLimit,
28932898

28942899
if localCommit {
28952900
return lnwallet.HtlcIsDust(
2896-
chantype, incoming, true, feerate, amt,
2901+
chantype, incoming, lntypes.Local, feerate, amt,
28972902
localDustLimit,
28982903
)
28992904
}
29002905

29012906
return lnwallet.HtlcIsDust(
2902-
chantype, incoming, false, feerate, amt,
2907+
chantype, incoming, lntypes.Remote, feerate, amt,
29032908
remoteDustLimit,
29042909
)
29052910
}

lnwallet/chancloser/chancloser.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/lightningnetwork/lnd/htlcswitch"
1616
"github.com/lightningnetwork/lnd/input"
1717
"github.com/lightningnetwork/lnd/labels"
18+
"github.com/lightningnetwork/lnd/lntypes"
1819
"github.com/lightningnetwork/lnd/lnutils"
1920
"github.com/lightningnetwork/lnd/lnwallet"
2021
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
@@ -207,8 +208,8 @@ type ChanCloser struct {
207208
// settled channel funds to.
208209
remoteDeliveryScript []byte
209210

210-
// locallyInitiated is true if we initiated the channel close.
211-
locallyInitiated bool
211+
// closer is ChannelParty who initiated the coop close
212+
closer lntypes.ChannelParty
212213

213214
// cachedClosingSigned is a cached copy of a received ClosingSigned that
214215
// we use to handle a specific race condition caused by the independent
@@ -267,7 +268,8 @@ func (d *SimpleCoopFeeEstimator) EstimateFee(chanType channeldb.ChannelType,
267268
// be populated iff, we're the initiator of this closing request.
268269
func NewChanCloser(cfg ChanCloseCfg, deliveryScript []byte,
269270
idealFeePerKw chainfee.SatPerKWeight, negotiationHeight uint32,
270-
closeReq *htlcswitch.ChanClose, locallyInitiated bool) *ChanCloser {
271+
closeReq *htlcswitch.ChanClose,
272+
closer lntypes.ChannelParty) *ChanCloser {
271273

272274
chanPoint := cfg.Channel.ChannelPoint()
273275
cid := lnwire.NewChanIDFromOutPoint(chanPoint)
@@ -283,7 +285,7 @@ func NewChanCloser(cfg ChanCloseCfg, deliveryScript []byte,
283285
priorFeeOffers: make(
284286
map[btcutil.Amount]*lnwire.ClosingSigned,
285287
),
286-
locallyInitiated: locallyInitiated,
288+
closer: closer,
287289
}
288290
}
289291

@@ -366,7 +368,7 @@ func (c *ChanCloser) initChanShutdown() (*lnwire.Shutdown, error) {
366368
// message we are about to send in order to ensure that if a
367369
// re-establish occurs then we will re-send the same Shutdown message.
368370
shutdownInfo := channeldb.NewShutdownInfo(
369-
c.localDeliveryScript, c.locallyInitiated,
371+
c.localDeliveryScript, c.closer.IsLocal(),
370372
)
371373
err := c.cfg.Channel.MarkShutdownSent(shutdownInfo)
372374
if err != nil {
@@ -650,7 +652,7 @@ func (c *ChanCloser) BeginNegotiation() (fn.Option[lnwire.ClosingSigned],
650652
// externally consistent, and reflect that the channel is being
651653
// shutdown by the time the closing request returns.
652654
err := c.cfg.Channel.MarkCoopBroadcasted(
653-
nil, c.locallyInitiated,
655+
nil, c.closer,
654656
)
655657
if err != nil {
656658
return noClosingSigned, err
@@ -861,7 +863,7 @@ func (c *ChanCloser) ReceiveClosingSigned( //nolint:funlen
861863
// database, such that it can be republished if something goes
862864
// wrong.
863865
err = c.cfg.Channel.MarkCoopBroadcasted(
864-
closeTx, c.locallyInitiated,
866+
closeTx, c.closer,
865867
)
866868
if err != nil {
867869
return noClosing, err

lnwallet/chancloser/chancloser_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/lightningnetwork/lnd/channeldb"
1717
"github.com/lightningnetwork/lnd/input"
1818
"github.com/lightningnetwork/lnd/keychain"
19+
"github.com/lightningnetwork/lnd/lntypes"
1920
"github.com/lightningnetwork/lnd/lnutils"
2021
"github.com/lightningnetwork/lnd/lnwallet"
2122
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
@@ -150,7 +151,9 @@ func (m *mockChannel) ChannelPoint() wire.OutPoint {
150151
return m.chanPoint
151152
}
152153

153-
func (m *mockChannel) MarkCoopBroadcasted(*wire.MsgTx, bool) error {
154+
func (m *mockChannel) MarkCoopBroadcasted(*wire.MsgTx,
155+
lntypes.ChannelParty) error {
156+
154157
return nil
155158
}
156159

@@ -338,7 +341,7 @@ func TestMaxFeeClamp(t *testing.T) {
338341
Channel: &channel,
339342
MaxFee: test.inputMaxFee,
340343
FeeEstimator: &SimpleCoopFeeEstimator{},
341-
}, nil, test.idealFee, 0, nil, false,
344+
}, nil, test.idealFee, 0, nil, lntypes.Remote,
342345
)
343346

344347
// We'll call initFeeBaseline early here since we need
@@ -379,7 +382,7 @@ func TestMaxFeeBailOut(t *testing.T) {
379382
MaxFee: idealFee * 2,
380383
}
381384
chanCloser := NewChanCloser(
382-
closeCfg, nil, idealFee, 0, nil, false,
385+
closeCfg, nil, idealFee, 0, nil, lntypes.Remote,
383386
)
384387

385388
// We'll now force the channel state into the
@@ -503,7 +506,7 @@ func TestTaprootFastClose(t *testing.T) {
503506
DisableChannel: func(wire.OutPoint) error {
504507
return nil
505508
},
506-
}, nil, idealFee, 0, nil, true,
509+
}, nil, idealFee, 0, nil, lntypes.Local,
507510
)
508511
aliceCloser.initFeeBaseline()
509512

@@ -520,7 +523,7 @@ func TestTaprootFastClose(t *testing.T) {
520523
DisableChannel: func(wire.OutPoint) error {
521524
return nil
522525
},
523-
}, nil, idealFee, 0, nil, false,
526+
}, nil, idealFee, 0, nil, lntypes.Remote,
524527
)
525528
bobCloser.initFeeBaseline()
526529

lnwallet/chancloser/interface.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/btcsuite/btcd/wire"
88
"github.com/lightningnetwork/lnd/channeldb"
99
"github.com/lightningnetwork/lnd/input"
10+
"github.com/lightningnetwork/lnd/lntypes"
1011
"github.com/lightningnetwork/lnd/lnwallet"
1112
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
1213
"github.com/lightningnetwork/lnd/lnwire"
@@ -33,7 +34,7 @@ type Channel interface { //nolint:interfacebloat
3334

3435
// MarkCoopBroadcasted persistently marks that the channel close
3536
// transaction has been broadcast.
36-
MarkCoopBroadcasted(*wire.MsgTx, bool) error
37+
MarkCoopBroadcasted(*wire.MsgTx, lntypes.ChannelParty) error
3738

3839
// MarkShutdownSent persists the given ShutdownInfo. The existence of
3940
// the ShutdownInfo represents the fact that the Shutdown message has

0 commit comments

Comments
 (0)