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

[MILLI]: Introduce and use ChannelParty #8951

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions channeldb/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/shachain"
"github.com/lightningnetwork/lnd/tlv"
Expand Down Expand Up @@ -1690,11 +1691,11 @@ func (c *OpenChannel) isBorked(chanBucket kvdb.RBucket) (bool, error) {
// republish this tx at startup to ensure propagation, and we should still
// handle the case where a different tx actually hits the chain.
func (c *OpenChannel) MarkCommitmentBroadcasted(closeTx *wire.MsgTx,
locallyInitiated bool) error {
closer lntypes.ChannelParty) error {

return c.markBroadcasted(
ChanStatusCommitBroadcasted, forceCloseTxKey, closeTx,
locallyInitiated,
closer,
)
}

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

return c.markBroadcasted(
ChanStatusCoopBroadcasted, coopCloseTxKey, closeTx,
locallyInitiated,
closer,
)
}

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

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

// Closer identifies the ChannelParty that initiated the coop-closure process.
func (s ShutdownInfo) Closer() lntypes.ChannelParty {
if s.LocalInitiator.Val {
return lntypes.Local
}

return lntypes.Remote
}

// encode serialises the ShutdownInfo to the given io.Writer.
func (s *ShutdownInfo) encode(w io.Writer) error {
records := []tlv.Record{
Expand Down
19 changes: 13 additions & 6 deletions channeldb/channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/lnmock"
"github.com/lightningnetwork/lnd/lntest/channels"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/shachain"
"github.com/lightningnetwork/lnd/tlv"
Expand Down Expand Up @@ -1084,13 +1085,17 @@ func TestFetchWaitingCloseChannels(t *testing.T) {
},
)

if err := channel.MarkCommitmentBroadcasted(closeTx, true); err != nil {
if err := channel.MarkCommitmentBroadcasted(
closeTx, lntypes.Local,
); err != nil {
t.Fatalf("unable to mark commitment broadcast: %v", err)
}

// Now try to marking a coop close with a nil tx. This should
// succeed, but it shouldn't exit when queried.
if err = channel.MarkCoopBroadcasted(nil, true); err != nil {
if err = channel.MarkCoopBroadcasted(
nil, lntypes.Local,
); err != nil {
t.Fatalf("unable to mark nil coop broadcast: %v", err)
}
_, err := channel.BroadcastedCooperative()
Expand All @@ -1102,7 +1107,9 @@ func TestFetchWaitingCloseChannels(t *testing.T) {
// it as coop closed. Later we will test that distinct
// transactions are returned for both coop and force closes.
closeTx.TxIn[0].PreviousOutPoint.Index ^= 1
if err := channel.MarkCoopBroadcasted(closeTx, true); err != nil {
if err := channel.MarkCoopBroadcasted(
closeTx, lntypes.Local,
); err != nil {
t.Fatalf("unable to mark coop broadcast: %v", err)
}
}
Expand Down Expand Up @@ -1324,7 +1331,7 @@ func TestCloseInitiator(t *testing.T) {
// by the local party.
updateChannel: func(c *OpenChannel) error {
return c.MarkCoopBroadcasted(
&wire.MsgTx{}, true,
&wire.MsgTx{}, lntypes.Local,
)
},
expectedStatuses: []ChannelStatus{
Expand All @@ -1338,7 +1345,7 @@ func TestCloseInitiator(t *testing.T) {
// by the remote party.
updateChannel: func(c *OpenChannel) error {
return c.MarkCoopBroadcasted(
&wire.MsgTx{}, false,
&wire.MsgTx{}, lntypes.Remote,
)
},
expectedStatuses: []ChannelStatus{
Expand All @@ -1352,7 +1359,7 @@ func TestCloseInitiator(t *testing.T) {
// local initiator.
updateChannel: func(c *OpenChannel) error {
return c.MarkCommitmentBroadcasted(
&wire.MsgTx{}, true,
&wire.MsgTx{}, lntypes.Local,
carlaKC marked this conversation as resolved.
Show resolved Hide resolved
)
},
expectedStatuses: []ChannelStatus{
Expand Down
9 changes: 7 additions & 2 deletions channeldb/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/shachain"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -606,7 +607,9 @@ func TestFetchChannels(t *testing.T) {
channelIDOption(pendingWaitingChan),
)

err = pendingClosing.MarkCoopBroadcasted(nil, true)
err = pendingClosing.MarkCoopBroadcasted(
nil, lntypes.Local,
)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand All @@ -626,7 +629,9 @@ func TestFetchChannels(t *testing.T) {
channelIDOption(openWaitingChan),
openChannelOption(),
)
err = openClosing.MarkCoopBroadcasted(nil, true)
err = openClosing.MarkCoopBroadcasted(
nil, lntypes.Local,
)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
Expand Down
7 changes: 5 additions & 2 deletions contractcourt/chain_arbitrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/clock"
"github.com/lightningnetwork/lnd/lntest/mock"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwallet"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -61,12 +62,14 @@ func TestChainArbitratorRepublishCloses(t *testing.T) {
for i := 0; i < numChans/2; i++ {
closeTx := channels[i].FundingTxn.Copy()
closeTx.TxIn[0].PreviousOutPoint = channels[i].FundingOutpoint
err := channels[i].MarkCommitmentBroadcasted(closeTx, true)
err := channels[i].MarkCommitmentBroadcasted(
closeTx, lntypes.Local,
)
if err != nil {
t.Fatal(err)
}

err = channels[i].MarkCoopBroadcasted(closeTx, true)
err = channels[i].MarkCoopBroadcasted(closeTx, lntypes.Local)
if err != nil {
t.Fatal(err)
}
Expand Down
5 changes: 3 additions & 2 deletions contractcourt/chain_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/fn"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnutils"
"github.com/lightningnetwork/lnd/lnwallet"
"github.com/lightningnetwork/lnd/lnwire"
Expand Down Expand Up @@ -418,7 +419,7 @@ func (c *chainWatcher) handleUnknownLocalState(
// and remote keys for this state. We use our point as only we can
// revoke our own commitment.
commitKeyRing := lnwallet.DeriveCommitmentKeys(
commitPoint, true, c.cfg.chanState.ChanType,
commitPoint, lntypes.Local, c.cfg.chanState.ChanType,
&c.cfg.chanState.LocalChanCfg, &c.cfg.chanState.RemoteChanCfg,
)

Expand Down Expand Up @@ -891,7 +892,7 @@ func (c *chainWatcher) handlePossibleBreach(commitSpend *chainntnfs.SpendDetail,
// Create an AnchorResolution for the breached state.
anchorRes, err := lnwallet.NewAnchorResolution(
c.cfg.chanState, commitSpend.SpendingTx, retribution.KeyRing,
false,
lntypes.Remote,
)
if err != nil {
return false, fmt.Errorf("unable to create anchor "+
Expand Down
4 changes: 2 additions & 2 deletions contractcourt/channel_arbitrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ type ChannelArbitratorConfig struct {

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

// MarkChannelClosed marks the channel closed in the database, with the
// passed close summary. After this method successfully returns we can
Expand Down Expand Up @@ -1084,7 +1084,7 @@ func (c *ChannelArbitrator) stateStep(
// database, such that we can re-publish later in case it
// didn't propagate. We initiated the force close, so we
// mark broadcast with local initiator set to true.
err = c.cfg.MarkCommitmentBroadcasted(closeTx, true)
err = c.cfg.MarkCommitmentBroadcasted(closeTx, lntypes.Local)
if err != nil {
log.Errorf("ChannelArbitrator(%v): unable to "+
"mark commitment broadcasted: %v",
Expand Down
4 changes: 3 additions & 1 deletion contractcourt/channel_arbitrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,9 @@ func createTestChannelArbitrator(t *testing.T, log ArbitratorLog,
resolvedChan <- struct{}{}
return nil
},
MarkCommitmentBroadcasted: func(_ *wire.MsgTx, _ bool) error {
MarkCommitmentBroadcasted: func(_ *wire.MsgTx,
_ lntypes.ChannelParty) error {

return nil
},
MarkChannelClosed: func(*channeldb.ChannelCloseSummary,
Expand Down
2 changes: 1 addition & 1 deletion htlcswitch/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type dustHandler interface {
// getDustSum returns the dust sum on either the local or remote
// commitment. An optional fee parameter can be passed in which is used
// to calculate the dust sum.
getDustSum(remote bool,
getDustSum(whoseCommit lntypes.ChannelParty,
fee fn.Option[chainfee.SatPerKWeight]) lnwire.MilliSatoshi

// getFeeRate returns the current channel feerate.
Expand Down
41 changes: 23 additions & 18 deletions htlcswitch/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -2727,10 +2727,10 @@ func (l *channelLink) MayAddOutgoingHtlc(amt lnwire.MilliSatoshi) error {
// method.
//
// NOTE: Part of the dustHandler interface.
func (l *channelLink) getDustSum(remote bool,
func (l *channelLink) getDustSum(whoseCommit lntypes.ChannelParty,
dryRunFee fn.Option[chainfee.SatPerKWeight]) lnwire.MilliSatoshi {

return l.channel.GetDustSum(remote, dryRunFee)
return l.channel.GetDustSum(whoseCommit, dryRunFee)
}

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

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

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

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

// Calculate the dust sum for the local and remote commitments.
localDustSum := l.getDustSum(false, fn.None[chainfee.SatPerKWeight]())
remoteDustSum := l.getDustSum(true, fn.None[chainfee.SatPerKWeight]())
localDustSum := l.getDustSum(
lntypes.Local, fn.None[chainfee.SatPerKWeight](),
)
remoteDustSum := l.getDustSum(
lntypes.Remote, fn.None[chainfee.SatPerKWeight](),
)

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

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

isDust := func(feerate chainfee.SatPerKWeight, incoming,
localCommit bool, amt btcutil.Amount) bool {
isDust := func(feerate chainfee.SatPerKWeight, incoming bool,
whoseCommit lntypes.ChannelParty, amt btcutil.Amount) bool {

if localCommit {
return lnwallet.HtlcIsDust(
chantype, incoming, true, feerate, amt,
localDustLimit,
)
var dustLimit btcutil.Amount
if whoseCommit.IsLocal() {
dustLimit = localDustLimit
} else {
dustLimit = remoteDustLimit
}

return lnwallet.HtlcIsDust(
chantype, incoming, false, feerate, amt,
remoteDustLimit,
chantype, incoming, whoseCommit, feerate, amt,
dustLimit,
)
}

Expand Down
7 changes: 5 additions & 2 deletions htlcswitch/mailbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/lightningnetwork/lnd/clock"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
"github.com/lightningnetwork/lnd/lnwire"
)
Expand Down Expand Up @@ -660,15 +661,17 @@ func (m *memoryMailBox) DustPackets() (lnwire.MilliSatoshi,

// Evaluate whether this HTLC is dust on the local commitment.
if m.isDust(
m.feeRate, false, true, addPkt.amount.ToSatoshis(),
m.feeRate, false, lntypes.Local,
addPkt.amount.ToSatoshis(),
) {

localDustSum += addPkt.amount
}

// Evaluate whether this HTLC is dust on the remote commitment.
if m.isDust(
m.feeRate, false, false, addPkt.amount.ToSatoshis(),
m.feeRate, false, lntypes.Remote,
addPkt.amount.ToSatoshis(),
) {

remoteDustSum += addPkt.amount
Expand Down
2 changes: 1 addition & 1 deletion htlcswitch/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ func (f *mockChannelLink) handleSwitchPacket(pkt *htlcPacket) error {
return nil
}

func (f *mockChannelLink) getDustSum(remote bool,
func (f *mockChannelLink) getDustSum(whoseCommit lntypes.ChannelParty,
dryRunFee fn.Option[chainfee.SatPerKWeight]) lnwire.MilliSatoshi {

return 0
Expand Down
Loading
Loading