Skip to content

Commit 14ff12a

Browse files
authored
Merge pull request #8951 from ProofOfKeags/refactor/lnwallet-channel-channel-party
[MILLI]: Introduce and use ChannelParty
2 parents 16d80f5 + 1f9cac5 commit 14ff12a

25 files changed

+577
-421
lines changed

channeldb/channel.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/lightningnetwork/lnd/input"
2626
"github.com/lightningnetwork/lnd/keychain"
2727
"github.com/lightningnetwork/lnd/kvdb"
28+
"github.com/lightningnetwork/lnd/lntypes"
2829
"github.com/lightningnetwork/lnd/lnwire"
2930
"github.com/lightningnetwork/lnd/shachain"
3031
"github.com/lightningnetwork/lnd/tlv"
@@ -1690,11 +1691,11 @@ func (c *OpenChannel) isBorked(chanBucket kvdb.RBucket) (bool, error) {
16901691
// republish this tx at startup to ensure propagation, and we should still
16911692
// handle the case where a different tx actually hits the chain.
16921693
func (c *OpenChannel) MarkCommitmentBroadcasted(closeTx *wire.MsgTx,
1693-
locallyInitiated bool) error {
1694+
closer lntypes.ChannelParty) error {
16941695

16951696
return c.markBroadcasted(
16961697
ChanStatusCommitBroadcasted, forceCloseTxKey, closeTx,
1697-
locallyInitiated,
1698+
closer,
16981699
)
16991700
}
17001701

@@ -1706,11 +1707,11 @@ func (c *OpenChannel) MarkCommitmentBroadcasted(closeTx *wire.MsgTx,
17061707
// ensure propagation, and we should still handle the case where a different tx
17071708
// actually hits the chain.
17081709
func (c *OpenChannel) MarkCoopBroadcasted(closeTx *wire.MsgTx,
1709-
locallyInitiated bool) error {
1710+
closer lntypes.ChannelParty) error {
17101711

17111712
return c.markBroadcasted(
17121713
ChanStatusCoopBroadcasted, coopCloseTxKey, closeTx,
1713-
locallyInitiated,
1714+
closer,
17141715
)
17151716
}
17161717

@@ -1719,7 +1720,7 @@ func (c *OpenChannel) MarkCoopBroadcasted(closeTx *wire.MsgTx,
17191720
// which should specify either a coop or force close. It adds a status which
17201721
// indicates the party that initiated the channel close.
17211722
func (c *OpenChannel) markBroadcasted(status ChannelStatus, key []byte,
1722-
closeTx *wire.MsgTx, locallyInitiated bool) error {
1723+
closeTx *wire.MsgTx, closer lntypes.ChannelParty) error {
17231724

17241725
c.Lock()
17251726
defer c.Unlock()
@@ -1741,7 +1742,7 @@ func (c *OpenChannel) markBroadcasted(status ChannelStatus, key []byte,
17411742
// Add the initiator status to the status provided. These statuses are
17421743
// set in addition to the broadcast status so that we do not need to
17431744
// migrate the original logic which does not store initiator.
1744-
if locallyInitiated {
1745+
if closer.IsLocal() {
17451746
status |= ChanStatusLocalCloseInitiator
17461747
} else {
17471748
status |= ChanStatusRemoteCloseInitiator
@@ -4486,6 +4487,15 @@ func NewShutdownInfo(deliveryScript lnwire.DeliveryAddress,
44864487
}
44874488
}
44884489

4490+
// Closer identifies the ChannelParty that initiated the coop-closure process.
4491+
func (s ShutdownInfo) Closer() lntypes.ChannelParty {
4492+
if s.LocalInitiator.Val {
4493+
return lntypes.Local
4494+
}
4495+
4496+
return lntypes.Remote
4497+
}
4498+
44894499
// encode serialises the ShutdownInfo to the given io.Writer.
44904500
func (s *ShutdownInfo) encode(w io.Writer) error {
44914501
records := []tlv.Record{

channeldb/channel_test.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/lightningnetwork/lnd/kvdb"
2222
"github.com/lightningnetwork/lnd/lnmock"
2323
"github.com/lightningnetwork/lnd/lntest/channels"
24+
"github.com/lightningnetwork/lnd/lntypes"
2425
"github.com/lightningnetwork/lnd/lnwire"
2526
"github.com/lightningnetwork/lnd/shachain"
2627
"github.com/lightningnetwork/lnd/tlv"
@@ -1084,13 +1085,17 @@ func TestFetchWaitingCloseChannels(t *testing.T) {
10841085
},
10851086
)
10861087

1087-
if err := channel.MarkCommitmentBroadcasted(closeTx, true); err != nil {
1088+
if err := channel.MarkCommitmentBroadcasted(
1089+
closeTx, lntypes.Local,
1090+
); err != nil {
10881091
t.Fatalf("unable to mark commitment broadcast: %v", err)
10891092
}
10901093

10911094
// Now try to marking a coop close with a nil tx. This should
10921095
// succeed, but it shouldn't exit when queried.
1093-
if err = channel.MarkCoopBroadcasted(nil, true); err != nil {
1096+
if err = channel.MarkCoopBroadcasted(
1097+
nil, lntypes.Local,
1098+
); err != nil {
10941099
t.Fatalf("unable to mark nil coop broadcast: %v", err)
10951100
}
10961101
_, err := channel.BroadcastedCooperative()
@@ -1102,7 +1107,9 @@ func TestFetchWaitingCloseChannels(t *testing.T) {
11021107
// it as coop closed. Later we will test that distinct
11031108
// transactions are returned for both coop and force closes.
11041109
closeTx.TxIn[0].PreviousOutPoint.Index ^= 1
1105-
if err := channel.MarkCoopBroadcasted(closeTx, true); err != nil {
1110+
if err := channel.MarkCoopBroadcasted(
1111+
closeTx, lntypes.Local,
1112+
); err != nil {
11061113
t.Fatalf("unable to mark coop broadcast: %v", err)
11071114
}
11081115
}
@@ -1324,7 +1331,7 @@ func TestCloseInitiator(t *testing.T) {
13241331
// by the local party.
13251332
updateChannel: func(c *OpenChannel) error {
13261333
return c.MarkCoopBroadcasted(
1327-
&wire.MsgTx{}, true,
1334+
&wire.MsgTx{}, lntypes.Local,
13281335
)
13291336
},
13301337
expectedStatuses: []ChannelStatus{
@@ -1338,7 +1345,7 @@ func TestCloseInitiator(t *testing.T) {
13381345
// by the remote party.
13391346
updateChannel: func(c *OpenChannel) error {
13401347
return c.MarkCoopBroadcasted(
1341-
&wire.MsgTx{}, false,
1348+
&wire.MsgTx{}, lntypes.Remote,
13421349
)
13431350
},
13441351
expectedStatuses: []ChannelStatus{
@@ -1352,7 +1359,7 @@ func TestCloseInitiator(t *testing.T) {
13521359
// local initiator.
13531360
updateChannel: func(c *OpenChannel) error {
13541361
return c.MarkCommitmentBroadcasted(
1355-
&wire.MsgTx{}, true,
1362+
&wire.MsgTx{}, lntypes.Local,
13561363
)
13571364
},
13581365
expectedStatuses: []ChannelStatus{

channeldb/db_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/btcsuite/btcd/wire"
1515
"github.com/lightningnetwork/lnd/keychain"
1616
"github.com/lightningnetwork/lnd/kvdb"
17+
"github.com/lightningnetwork/lnd/lntypes"
1718
"github.com/lightningnetwork/lnd/lnwire"
1819
"github.com/lightningnetwork/lnd/shachain"
1920
"github.com/stretchr/testify/require"
@@ -606,7 +607,9 @@ func TestFetchChannels(t *testing.T) {
606607
channelIDOption(pendingWaitingChan),
607608
)
608609

609-
err = pendingClosing.MarkCoopBroadcasted(nil, true)
610+
err = pendingClosing.MarkCoopBroadcasted(
611+
nil, lntypes.Local,
612+
)
610613
if err != nil {
611614
t.Fatalf("unexpected error: %v", err)
612615
}
@@ -626,7 +629,9 @@ func TestFetchChannels(t *testing.T) {
626629
channelIDOption(openWaitingChan),
627630
openChannelOption(),
628631
)
629-
err = openClosing.MarkCoopBroadcasted(nil, true)
632+
err = openClosing.MarkCoopBroadcasted(
633+
nil, lntypes.Local,
634+
)
630635
if err != nil {
631636
t.Fatalf("unexpected error: %v", err)
632637
}

contractcourt/chain_arbitrator_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/lightningnetwork/lnd/channeldb/models"
1212
"github.com/lightningnetwork/lnd/clock"
1313
"github.com/lightningnetwork/lnd/lntest/mock"
14+
"github.com/lightningnetwork/lnd/lntypes"
1415
"github.com/lightningnetwork/lnd/lnwallet"
1516
"github.com/stretchr/testify/require"
1617
)
@@ -61,12 +62,14 @@ func TestChainArbitratorRepublishCloses(t *testing.T) {
6162
for i := 0; i < numChans/2; i++ {
6263
closeTx := channels[i].FundingTxn.Copy()
6364
closeTx.TxIn[0].PreviousOutPoint = channels[i].FundingOutpoint
64-
err := channels[i].MarkCommitmentBroadcasted(closeTx, true)
65+
err := channels[i].MarkCommitmentBroadcasted(
66+
closeTx, lntypes.Local,
67+
)
6568
if err != nil {
6669
t.Fatal(err)
6770
}
6871

69-
err = channels[i].MarkCoopBroadcasted(closeTx, true)
72+
err = channels[i].MarkCoopBroadcasted(closeTx, lntypes.Local)
7073
if err != nil {
7174
t.Fatal(err)
7275
}

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 "+

contractcourt/channel_arbitrator.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ type ChannelArbitratorConfig struct {
129129

130130
// MarkCommitmentBroadcasted should mark the channel as the commitment
131131
// being broadcast, and we are waiting for the commitment to confirm.
132-
MarkCommitmentBroadcasted func(*wire.MsgTx, bool) error
132+
MarkCommitmentBroadcasted func(*wire.MsgTx, lntypes.ChannelParty) error
133133

134134
// MarkChannelClosed marks the channel closed in the database, with the
135135
// passed close summary. After this method successfully returns we can
@@ -1084,7 +1084,7 @@ func (c *ChannelArbitrator) stateStep(
10841084
// database, such that we can re-publish later in case it
10851085
// didn't propagate. We initiated the force close, so we
10861086
// mark broadcast with local initiator set to true.
1087-
err = c.cfg.MarkCommitmentBroadcasted(closeTx, true)
1087+
err = c.cfg.MarkCommitmentBroadcasted(closeTx, lntypes.Local)
10881088
if err != nil {
10891089
log.Errorf("ChannelArbitrator(%v): unable to "+
10901090
"mark commitment broadcasted: %v",

contractcourt/channel_arbitrator_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,9 @@ func createTestChannelArbitrator(t *testing.T, log ArbitratorLog,
416416
resolvedChan <- struct{}{}
417417
return nil
418418
},
419-
MarkCommitmentBroadcasted: func(_ *wire.MsgTx, _ bool) error {
419+
MarkCommitmentBroadcasted: func(_ *wire.MsgTx,
420+
_ lntypes.ChannelParty) error {
421+
420422
return nil
421423
},
422424
MarkChannelClosed: func(*channeldb.ChannelCloseSummary,

htlcswitch/interfaces.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ type dustHandler interface {
6363
// getDustSum returns the dust sum on either the local or remote
6464
// commitment. An optional fee parameter can be passed in which is used
6565
// to calculate the dust sum.
66-
getDustSum(remote bool,
66+
getDustSum(whoseCommit lntypes.ChannelParty,
6767
fee fn.Option[chainfee.SatPerKWeight]) lnwire.MilliSatoshi
6868

6969
// getFeeRate returns the current channel feerate.

htlcswitch/link.go

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2727,10 +2727,10 @@ func (l *channelLink) MayAddOutgoingHtlc(amt lnwire.MilliSatoshi) error {
27272727
// method.
27282728
//
27292729
// NOTE: Part of the dustHandler interface.
2730-
func (l *channelLink) getDustSum(remote bool,
2730+
func (l *channelLink) getDustSum(whoseCommit lntypes.ChannelParty,
27312731
dryRunFee fn.Option[chainfee.SatPerKWeight]) lnwire.MilliSatoshi {
27322732

2733-
return l.channel.GetDustSum(remote, dryRunFee)
2733+
return l.channel.GetDustSum(whoseCommit, dryRunFee)
27342734
}
27352735

27362736
// getFeeRate is a wrapper method that retrieves the underlying channel's
@@ -2784,8 +2784,8 @@ func (l *channelLink) exceedsFeeExposureLimit(
27842784

27852785
// Get the sum of dust for both the local and remote commitments using
27862786
// this "dry-run" fee.
2787-
localDustSum := l.getDustSum(false, dryRunFee)
2788-
remoteDustSum := l.getDustSum(true, dryRunFee)
2787+
localDustSum := l.getDustSum(lntypes.Local, dryRunFee)
2788+
remoteDustSum := l.getDustSum(lntypes.Remote, dryRunFee)
27892789

27902790
// Calculate the local and remote commitment fees using this dry-run
27912791
// fee.
@@ -2826,12 +2826,16 @@ func (l *channelLink) isOverexposedWithHtlc(htlc *lnwire.UpdateAddHTLC,
28262826
amount := htlc.Amount.ToSatoshis()
28272827

28282828
// See if this HTLC is dust on both the local and remote commitments.
2829-
isLocalDust := dustClosure(feeRate, incoming, true, amount)
2830-
isRemoteDust := dustClosure(feeRate, incoming, false, amount)
2829+
isLocalDust := dustClosure(feeRate, incoming, lntypes.Local, amount)
2830+
isRemoteDust := dustClosure(feeRate, incoming, lntypes.Remote, amount)
28312831

28322832
// Calculate the dust sum for the local and remote commitments.
2833-
localDustSum := l.getDustSum(false, fn.None[chainfee.SatPerKWeight]())
2834-
remoteDustSum := l.getDustSum(true, fn.None[chainfee.SatPerKWeight]())
2833+
localDustSum := l.getDustSum(
2834+
lntypes.Local, fn.None[chainfee.SatPerKWeight](),
2835+
)
2836+
remoteDustSum := l.getDustSum(
2837+
lntypes.Remote, fn.None[chainfee.SatPerKWeight](),
2838+
)
28352839

28362840
// Grab the larger of the local and remote commitment fees w/o dust.
28372841
commitFee := l.getCommitFee(false)
@@ -2882,25 +2886,26 @@ func (l *channelLink) isOverexposedWithHtlc(htlc *lnwire.UpdateAddHTLC,
28822886
// the HTLC is incoming (i.e. one that the remote sent), a boolean denoting
28832887
// whether to evaluate on the local or remote commit, and finally an HTLC
28842888
// amount to test.
2885-
type dustClosure func(chainfee.SatPerKWeight, bool, bool, btcutil.Amount) bool
2889+
type dustClosure func(feerate chainfee.SatPerKWeight, incoming bool,
2890+
whoseCommit lntypes.ChannelParty, amt btcutil.Amount) bool
28862891

28872892
// dustHelper is used to construct the dustClosure.
28882893
func dustHelper(chantype channeldb.ChannelType, localDustLimit,
28892894
remoteDustLimit btcutil.Amount) dustClosure {
28902895

2891-
isDust := func(feerate chainfee.SatPerKWeight, incoming,
2892-
localCommit bool, amt btcutil.Amount) bool {
2896+
isDust := func(feerate chainfee.SatPerKWeight, incoming bool,
2897+
whoseCommit lntypes.ChannelParty, amt btcutil.Amount) bool {
28932898

2894-
if localCommit {
2895-
return lnwallet.HtlcIsDust(
2896-
chantype, incoming, true, feerate, amt,
2897-
localDustLimit,
2898-
)
2899+
var dustLimit btcutil.Amount
2900+
if whoseCommit.IsLocal() {
2901+
dustLimit = localDustLimit
2902+
} else {
2903+
dustLimit = remoteDustLimit
28992904
}
29002905

29012906
return lnwallet.HtlcIsDust(
2902-
chantype, incoming, false, feerate, amt,
2903-
remoteDustLimit,
2907+
chantype, incoming, whoseCommit, feerate, amt,
2908+
dustLimit,
29042909
)
29052910
}
29062911

htlcswitch/mailbox.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"time"
1010

1111
"github.com/lightningnetwork/lnd/clock"
12+
"github.com/lightningnetwork/lnd/lntypes"
1213
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
1314
"github.com/lightningnetwork/lnd/lnwire"
1415
)
@@ -660,15 +661,17 @@ func (m *memoryMailBox) DustPackets() (lnwire.MilliSatoshi,
660661

661662
// Evaluate whether this HTLC is dust on the local commitment.
662663
if m.isDust(
663-
m.feeRate, false, true, addPkt.amount.ToSatoshis(),
664+
m.feeRate, false, lntypes.Local,
665+
addPkt.amount.ToSatoshis(),
664666
) {
665667

666668
localDustSum += addPkt.amount
667669
}
668670

669671
// Evaluate whether this HTLC is dust on the remote commitment.
670672
if m.isDust(
671-
m.feeRate, false, false, addPkt.amount.ToSatoshis(),
673+
m.feeRate, false, lntypes.Remote,
674+
addPkt.amount.ToSatoshis(),
672675
) {
673676

674677
remoteDustSum += addPkt.amount

0 commit comments

Comments
 (0)