Skip to content

Commit 59f3268

Browse files
committed
contractcourt: introduce option for commitKey.
1 parent d5e8df7 commit 59f3268

File tree

5 files changed

+55
-31
lines changed

5 files changed

+55
-31
lines changed

contractcourt/briefcase.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,16 +1475,23 @@ func decodeBreachResolution(r io.Reader, b *BreachResolution) error {
14751475
return binary.Read(r, endian, &b.FundingOutPoint.Index)
14761476
}
14771477

1478-
func encodeHtlcSetKey(w io.Writer, h *HtlcSetKey) error {
1479-
err := binary.Write(w, endian, h.IsRemote)
1478+
func encodeHtlcSetKey(w io.Writer, htlcSetKey HtlcSetKey) error {
1479+
err := binary.Write(w, endian, htlcSetKey.IsRemote)
14801480
if err != nil {
14811481
return err
14821482
}
1483-
return binary.Write(w, endian, h.IsPending)
1483+
1484+
return binary.Write(w, endian, htlcSetKey.IsPending)
14841485
}
14851486

14861487
func encodeCommitSet(w io.Writer, c *CommitSet) error {
1487-
if err := encodeHtlcSetKey(w, c.ConfCommitKey); err != nil {
1488+
confCommitKey, err := c.ConfCommitKey.UnwrapOrErr(
1489+
fmt.Errorf("HtlcSetKey is not set"),
1490+
)
1491+
if err != nil {
1492+
return err
1493+
}
1494+
if err := encodeHtlcSetKey(w, confCommitKey); err != nil {
14881495
return err
14891496
}
14901497

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

14961503
for htlcSetKey, htlcs := range c.HtlcSets {
1497-
htlcSetKey := htlcSetKey
1498-
if err := encodeHtlcSetKey(w, &htlcSetKey); err != nil {
1504+
if err := encodeHtlcSetKey(w, htlcSetKey); err != nil {
14991505
return err
15001506
}
15011507

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

15191525
func decodeCommitSet(r io.Reader) (*CommitSet, error) {
1520-
c := &CommitSet{
1521-
ConfCommitKey: &HtlcSetKey{},
1522-
HtlcSets: make(map[HtlcSetKey][]channeldb.HTLC),
1526+
confCommitKey := HtlcSetKey{}
1527+
if err := decodeHtlcSetKey(r, &confCommitKey); err != nil {
1528+
return nil, err
15231529
}
15241530

1525-
if err := decodeHtlcSetKey(r, c.ConfCommitKey); err != nil {
1526-
return nil, err
1531+
c := &CommitSet{
1532+
ConfCommitKey: fn.Some(confCommitKey),
1533+
HtlcSets: make(map[HtlcSetKey][]channeldb.HTLC),
15271534
}
15281535

15291536
var numSets uint8

contractcourt/briefcase_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/btcsuite/btcd/wire"
1515
"github.com/davecgh/go-spew/spew"
1616
"github.com/lightningnetwork/lnd/channeldb"
17+
"github.com/lightningnetwork/lnd/fn"
1718
"github.com/lightningnetwork/lnd/input"
1819
"github.com/lightningnetwork/lnd/kvdb"
1920
"github.com/lightningnetwork/lnd/lnmock"
@@ -753,7 +754,7 @@ func TestCommitSetStorage(t *testing.T) {
753754
for _, pendingRemote := range []bool{true, false} {
754755
for _, confType := range confTypes {
755756
commitSet := &CommitSet{
756-
ConfCommitKey: &confType,
757+
ConfCommitKey: fn.Some(confType),
757758
HtlcSets: make(map[HtlcSetKey][]channeldb.HTLC),
758759
}
759760
commitSet.HtlcSets[LocalHtlcSet] = activeHTLCs

contractcourt/chain_watcher.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ type BreachCloseInfo struct {
9393
// HTLCs to determine if any additional actions need to be made based on the
9494
// remote party's commitments.
9595
type CommitSet struct {
96-
// ConfCommitKey if non-nil, identifies the commitment that was
96+
// When the ConfCommitKey is set, it signals that the commitment tx was
9797
// confirmed in the chain.
98-
ConfCommitKey *HtlcSetKey
98+
ConfCommitKey fn.Option[HtlcSetKey]
9999

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

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

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

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

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

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

contractcourt/channel_arbitrator.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -682,8 +682,14 @@ func (c *ChannelArbitrator) relaunchResolvers(commitSet *CommitSet,
682682
// chain actions may exclude some information, but we cannot recover it
683683
// for these older nodes at the moment.
684684
var confirmedHTLCs []channeldb.HTLC
685-
if commitSet != nil && commitSet.ConfCommitKey != nil {
686-
confirmedHTLCs = commitSet.HtlcSets[*commitSet.ConfCommitKey]
685+
if commitSet != nil && commitSet.ConfCommitKey.IsSome() {
686+
confCommitKey, err := commitSet.ConfCommitKey.UnwrapOrErr(
687+
fmt.Errorf("no commitKey available"),
688+
)
689+
if err != nil {
690+
return err
691+
}
692+
confirmedHTLCs = commitSet.HtlcSets[confCommitKey]
687693
} else {
688694
chainActions, err := c.log.FetchChainActions()
689695
if err != nil {
@@ -2223,15 +2229,21 @@ func (c *ChannelArbitrator) constructChainActions(confCommitSet *CommitSet,
22232229
// then this is an older node that had a pending close channel before
22242230
// the CommitSet was introduced. In this case, we'll just return the
22252231
// existing ChainActionMap they had on disk.
2226-
if confCommitSet == nil || confCommitSet.ConfCommitKey == nil {
2232+
if confCommitSet == nil || confCommitSet.ConfCommitKey.IsNone() {
22272233
return c.log.FetchChainActions()
22282234
}
22292235

22302236
// Otherwise, we have the full commitment set written to disk, and can
22312237
// proceed as normal.
22322238
htlcSets := confCommitSet.toActiveHTLCSets()
2233-
switch *confCommitSet.ConfCommitKey {
2239+
confCommitKey, err := confCommitSet.ConfCommitKey.UnwrapOrErr(
2240+
fmt.Errorf("no commitKey available"),
2241+
)
2242+
if err != nil {
2243+
return nil, err
2244+
}
22342245

2246+
switch confCommitKey {
22352247
// If the local commitment transaction confirmed, then we'll examine
22362248
// that as well as their commitments to the set of chain actions.
22372249
case LocalHtlcSet:

contractcourt/channel_arbitrator_test.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ func TestChannelArbitratorRemoteForceClose(t *testing.T) {
590590
chanArb.cfg.ChainEvents.RemoteUnilateralClosure <- &RemoteUnilateralCloseInfo{
591591
UnilateralCloseSummary: uniClose,
592592
CommitSet: CommitSet{
593-
ConfCommitKey: &RemoteHtlcSet,
593+
ConfCommitKey: fn.Some(RemoteHtlcSet),
594594
HtlcSets: make(map[HtlcSetKey][]channeldb.HTLC),
595595
},
596596
}
@@ -777,7 +777,7 @@ func TestChannelArbitratorBreachClose(t *testing.T) {
777777
},
778778
AnchorResolution: anchorRes,
779779
CommitSet: CommitSet{
780-
ConfCommitKey: &RemoteHtlcSet,
780+
ConfCommitKey: fn.Some(RemoteHtlcSet),
781781
HtlcSets: map[HtlcSetKey][]channeldb.HTLC{
782782
RemoteHtlcSet: {htlc1, htlc2},
783783
},
@@ -999,7 +999,7 @@ func TestChannelArbitratorLocalForceClosePendingHtlc(t *testing.T) {
999999
},
10001000
ChannelCloseSummary: &channeldb.ChannelCloseSummary{},
10011001
CommitSet: CommitSet{
1002-
ConfCommitKey: &LocalHtlcSet,
1002+
ConfCommitKey: fn.Some(LocalHtlcSet),
10031003
HtlcSets: map[HtlcSetKey][]channeldb.HTLC{
10041004
LocalHtlcSet: htlcSet,
10051005
},
@@ -1527,7 +1527,7 @@ func TestChannelArbitratorForceCloseBreachedChannel(t *testing.T) {
15271527
},
15281528
}
15291529
log.commitSet = &CommitSet{
1530-
ConfCommitKey: &RemoteHtlcSet,
1530+
ConfCommitKey: fn.Some(RemoteHtlcSet),
15311531
HtlcSets: map[HtlcSetKey][]channeldb.HTLC{
15321532
RemoteHtlcSet: {},
15331533
},
@@ -1954,8 +1954,12 @@ func TestChannelArbitratorDanglingCommitForceClose(t *testing.T) {
19541954
},
19551955
ChannelCloseSummary: &channeldb.ChannelCloseSummary{},
19561956
CommitSet: CommitSet{
1957-
ConfCommitKey: &testCase.confCommit,
1958-
HtlcSets: make(map[HtlcSetKey][]channeldb.HTLC),
1957+
ConfCommitKey: fn.Some(
1958+
testCase.confCommit,
1959+
),
1960+
HtlcSets: make(
1961+
map[HtlcSetKey][]channeldb.HTLC,
1962+
),
19591963
},
19601964
}
19611965

@@ -2875,7 +2879,7 @@ func TestChannelArbitratorAnchors(t *testing.T) {
28752879
},
28762880
ChannelCloseSummary: &channeldb.ChannelCloseSummary{},
28772881
CommitSet: CommitSet{
2878-
ConfCommitKey: &LocalHtlcSet,
2882+
ConfCommitKey: fn.Some(LocalHtlcSet),
28792883
HtlcSets: map[HtlcSetKey][]channeldb.HTLC{},
28802884
},
28812885
}

0 commit comments

Comments
 (0)