Skip to content

Commit 2ab22b0

Browse files
committed
lnwallet: refactor commit keys to use lntypes.Dual
1 parent b45d72f commit 2ab22b0

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

lnwallet/channel.go

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -575,9 +575,8 @@ func (c *commitment) toDiskCommit(
575575
// restore commitment state written to disk back into memory once we need to
576576
// restart a channel session.
577577
func (lc *LightningChannel) diskHtlcToPayDesc(feeRate chainfee.SatPerKWeight,
578-
htlc *channeldb.HTLC, localCommitKeys *CommitmentKeyRing,
579-
remoteCommitKeys *CommitmentKeyRing, whoseCommit lntypes.ChannelParty,
580-
) (PaymentDescriptor, error) {
578+
htlc *channeldb.HTLC, commitKeys lntypes.Dual[*CommitmentKeyRing],
579+
whoseCommit lntypes.ChannelParty) (PaymentDescriptor, error) {
581580

582581
// The proper pkScripts for this PaymentDescriptor must be
583582
// generated so we can easily locate them within the commitment
@@ -598,6 +597,7 @@ func (lc *LightningChannel) diskHtlcToPayDesc(feeRate chainfee.SatPerKWeight,
598597
chanType, htlc.Incoming, lntypes.Local, feeRate,
599598
htlc.Amt.ToSatoshis(), lc.channelState.LocalChanCfg.DustLimit,
600599
)
600+
localCommitKeys := commitKeys.GetForParty(lntypes.Local)
601601
if !isDustLocal && localCommitKeys != nil {
602602
scriptInfo, err := genHtlcScript(
603603
chanType, htlc.Incoming, lntypes.Local,
@@ -613,6 +613,7 @@ func (lc *LightningChannel) diskHtlcToPayDesc(feeRate chainfee.SatPerKWeight,
613613
chanType, htlc.Incoming, lntypes.Remote, feeRate,
614614
htlc.Amt.ToSatoshis(), lc.channelState.RemoteChanCfg.DustLimit,
615615
)
616+
remoteCommitKeys := commitKeys.GetForParty(lntypes.Remote)
616617
if !isDustRemote && remoteCommitKeys != nil {
617618
scriptInfo, err := genHtlcScript(
618619
chanType, htlc.Incoming, lntypes.Remote,
@@ -665,9 +666,9 @@ func (lc *LightningChannel) diskHtlcToPayDesc(feeRate chainfee.SatPerKWeight,
665666
// these payment descriptors can be re-inserted into the in-memory updateLog
666667
// for each side.
667668
func (lc *LightningChannel) extractPayDescs(feeRate chainfee.SatPerKWeight,
668-
htlcs []channeldb.HTLC, localCommitKeys *CommitmentKeyRing,
669-
remoteCommitKeys *CommitmentKeyRing, whoseCommit lntypes.ChannelParty,
670-
) ([]PaymentDescriptor, []PaymentDescriptor, error) {
669+
htlcs []channeldb.HTLC, commitKeys lntypes.Dual[*CommitmentKeyRing],
670+
whoseCommit lntypes.ChannelParty) ([]PaymentDescriptor,
671+
[]PaymentDescriptor, error) {
671672

672673
var (
673674
incomingHtlcs []PaymentDescriptor
@@ -685,9 +686,7 @@ func (lc *LightningChannel) extractPayDescs(feeRate chainfee.SatPerKWeight,
685686
htlc := htlc
686687

687688
payDesc, err := lc.diskHtlcToPayDesc(
688-
feeRate, &htlc,
689-
localCommitKeys, remoteCommitKeys,
690-
whoseCommit,
689+
feeRate, &htlc, commitKeys, whoseCommit,
691690
)
692691
if err != nil {
693692
return incomingHtlcs, outgoingHtlcs, err
@@ -716,31 +715,30 @@ func (lc *LightningChannel) diskCommitToMemCommit(
716715
// (we extended but weren't able to complete the commitment dance
717716
// before shutdown), then the localCommitPoint won't be set as we
718717
// haven't yet received a responding commitment from the remote party.
719-
var localCommitKeys, remoteCommitKeys *CommitmentKeyRing
718+
var commitKeys lntypes.Dual[*CommitmentKeyRing]
720719
if localCommitPoint != nil {
721-
localCommitKeys = DeriveCommitmentKeys(
720+
commitKeys.SetForParty(lntypes.Local, DeriveCommitmentKeys(
722721
localCommitPoint, lntypes.Local,
723722
lc.channelState.ChanType,
724723
&lc.channelState.LocalChanCfg,
725724
&lc.channelState.RemoteChanCfg,
726-
)
725+
))
727726
}
728727
if remoteCommitPoint != nil {
729-
remoteCommitKeys = DeriveCommitmentKeys(
728+
commitKeys.SetForParty(lntypes.Remote, DeriveCommitmentKeys(
730729
remoteCommitPoint, lntypes.Remote,
731730
lc.channelState.ChanType,
732731
&lc.channelState.LocalChanCfg,
733732
&lc.channelState.RemoteChanCfg,
734-
)
733+
))
735734
}
736735

737736
// With the key rings re-created, we'll now convert all the on-disk
738737
// HTLC"s into PaymentDescriptor's so we can re-insert them into our
739738
// update log.
740739
incomingHtlcs, outgoingHtlcs, err := lc.extractPayDescs(
741740
chainfee.SatPerKWeight(diskCommit.FeePerKw),
742-
diskCommit.Htlcs, localCommitKeys, remoteCommitKeys,
743-
whoseCommit,
741+
diskCommit.Htlcs, commitKeys, whoseCommit,
744742
)
745743
if err != nil {
746744
return nil, err

lnwallet/channel_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10416,7 +10416,7 @@ func TestExtractPayDescs(t *testing.T) {
1041610416
// NOTE: we use nil commitment key rings to avoid checking the htlc
1041710417
// scripts(`genHtlcScript`) as it should be tested independently.
1041810418
incomingPDs, outgoingPDs, err := lnChan.extractPayDescs(
10419-
0, htlcs, nil, nil, lntypes.Local,
10419+
0, htlcs, lntypes.Dual[*CommitmentKeyRing]{}, lntypes.Local,
1042010420
)
1042110421
require.NoError(t, err)
1042210422

0 commit comments

Comments
 (0)