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

Cancel back outgoing dust htlcs before commitment is confirmed. #9068

Merged
merged 9 commits into from
Nov 8, 2024
29 changes: 18 additions & 11 deletions contractcourt/briefcase.go
Original file line number Diff line number Diff line change
Expand Up @@ -1475,16 +1475,23 @@ func decodeBreachResolution(r io.Reader, b *BreachResolution) error {
return binary.Read(r, endian, &b.FundingOutPoint.Index)
}

func encodeHtlcSetKey(w io.Writer, h *HtlcSetKey) error {
err := binary.Write(w, endian, h.IsRemote)
func encodeHtlcSetKey(w io.Writer, htlcSetKey HtlcSetKey) error {
err := binary.Write(w, endian, htlcSetKey.IsRemote)
if err != nil {
return err
}
return binary.Write(w, endian, h.IsPending)

return binary.Write(w, endian, htlcSetKey.IsPending)
}

func encodeCommitSet(w io.Writer, c *CommitSet) error {
if err := encodeHtlcSetKey(w, c.ConfCommitKey); err != nil {
confCommitKey, err := c.ConfCommitKey.UnwrapOrErr(
fmt.Errorf("HtlcSetKey is not set"),
)
if err != nil {
return err
}
if err := encodeHtlcSetKey(w, confCommitKey); err != nil {
return err
}

Expand All @@ -1494,8 +1501,7 @@ func encodeCommitSet(w io.Writer, c *CommitSet) error {
}

for htlcSetKey, htlcs := range c.HtlcSets {
htlcSetKey := htlcSetKey
if err := encodeHtlcSetKey(w, &htlcSetKey); err != nil {
if err := encodeHtlcSetKey(w, htlcSetKey); err != nil {
return err
}

Expand All @@ -1517,13 +1523,14 @@ func decodeHtlcSetKey(r io.Reader, h *HtlcSetKey) error {
}

func decodeCommitSet(r io.Reader) (*CommitSet, error) {
c := &CommitSet{
ConfCommitKey: &HtlcSetKey{},
HtlcSets: make(map[HtlcSetKey][]channeldb.HTLC),
confCommitKey := HtlcSetKey{}
if err := decodeHtlcSetKey(r, &confCommitKey); err != nil {
return nil, err
}

if err := decodeHtlcSetKey(r, c.ConfCommitKey); err != nil {
return nil, err
c := &CommitSet{
ConfCommitKey: fn.Some(confCommitKey),
HtlcSets: make(map[HtlcSetKey][]channeldb.HTLC),
}

var numSets uint8
Expand Down
3 changes: 2 additions & 1 deletion contractcourt/briefcase_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/davecgh/go-spew/spew"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/fn"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/lnmock"
Expand Down Expand Up @@ -753,7 +754,7 @@ func TestCommitSetStorage(t *testing.T) {
for _, pendingRemote := range []bool{true, false} {
for _, confType := range confTypes {
commitSet := &CommitSet{
ConfCommitKey: &confType,
ConfCommitKey: fn.Some(confType),
HtlcSets: make(map[HtlcSetKey][]channeldb.HTLC),
}
commitSet.HtlcSets[LocalHtlcSet] = activeHTLCs
Expand Down
16 changes: 8 additions & 8 deletions contractcourt/chain_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ type BreachCloseInfo struct {
// HTLCs to determine if any additional actions need to be made based on the
// remote party's commitments.
type CommitSet struct {
// ConfCommitKey if non-nil, identifies the commitment that was
// When the ConfCommitKey is set, it signals that the commitment tx was
// confirmed in the chain.
ConfCommitKey *HtlcSetKey
ConfCommitKey fn.Option[HtlcSetKey]

// HtlcSets stores the set of all known active HTLC for each active
// commitment at the time of channel closure.
Expand Down Expand Up @@ -509,7 +509,7 @@ func (c *chainWatcher) handleUnknownLocalState(

// If this is our commitment transaction, then we try to act even
// though we won't be able to sweep HTLCs.
chainSet.commitSet.ConfCommitKey = &LocalHtlcSet
chainSet.commitSet.ConfCommitKey = fn.Some(LocalHtlcSet)
if err := c.dispatchLocalForceClose(
commitSpend, broadcastStateNum, chainSet.commitSet,
); err != nil {
Expand Down Expand Up @@ -806,7 +806,7 @@ func (c *chainWatcher) handleKnownLocalState(
return false, nil
}

chainSet.commitSet.ConfCommitKey = &LocalHtlcSet
chainSet.commitSet.ConfCommitKey = fn.Some(LocalHtlcSet)
if err := c.dispatchLocalForceClose(
commitSpend, broadcastStateNum, chainSet.commitSet,
); err != nil {
Expand Down Expand Up @@ -844,7 +844,7 @@ func (c *chainWatcher) handleKnownRemoteState(
log.Infof("Remote party broadcast base set, "+
"commit_num=%v", chainSet.remoteStateNum)

chainSet.commitSet.ConfCommitKey = &RemoteHtlcSet
chainSet.commitSet.ConfCommitKey = fn.Some(RemoteHtlcSet)
err := c.dispatchRemoteForceClose(
commitSpend, chainSet.remoteCommit,
chainSet.commitSet,
Expand All @@ -869,7 +869,7 @@ func (c *chainWatcher) handleKnownRemoteState(
log.Infof("Remote party broadcast pending set, "+
"commit_num=%v", chainSet.remoteStateNum+1)

chainSet.commitSet.ConfCommitKey = &RemotePendingHtlcSet
chainSet.commitSet.ConfCommitKey = fn.Some(RemotePendingHtlcSet)
err := c.dispatchRemoteForceClose(
commitSpend, *chainSet.remotePendingCommit,
chainSet.commitSet,
Expand Down Expand Up @@ -936,7 +936,7 @@ func (c *chainWatcher) handlePossibleBreach(commitSpend *chainntnfs.SpendDetail,
// only used to ensure a nil-pointer-dereference doesn't occur and is
// not used otherwise. The HTLC's may not exist for the
// RemotePendingHtlcSet.
chainSet.commitSet.ConfCommitKey = &RemoteHtlcSet
chainSet.commitSet.ConfCommitKey = fn.Some(RemoteHtlcSet)

// THEY'RE ATTEMPTING TO VIOLATE THE CONTRACT LAID OUT WITHIN THE
// PAYMENT CHANNEL. Therefore we close the signal indicating a revoked
Expand Down Expand Up @@ -997,7 +997,7 @@ func (c *chainWatcher) handleUnknownRemoteState(
// means we won't be able to recover any HTLC funds.
//
// TODO(halseth): can we try to recover some HTLCs?
chainSet.commitSet.ConfCommitKey = &RemoteHtlcSet
chainSet.commitSet.ConfCommitKey = fn.Some(RemoteHtlcSet)
err := c.dispatchRemoteForceClose(
commitSpend, channeldb.ChannelCommitment{},
chainSet.commitSet, commitPoint,
Expand Down
Loading
Loading