From a89e24f4878982f84531fa9c37a4d6ff65ad24a0 Mon Sep 17 00:00:00 2001 From: Keagan McClelland Date: Thu, 6 Jun 2024 11:35:52 -0700 Subject: [PATCH 01/18] lnwallet: add ChannelID method to LightningChannel In this commit we introduce a convenience method to LightningChannel to allow us to quickly grab the channel id. This will be important in upcoming commits where we need to remember the ChannelID to reconstruct update messages. --- lnwallet/channel.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lnwallet/channel.go b/lnwallet/channel.go index 49d8df566..a9d4a39a9 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -6327,6 +6327,12 @@ func (lc *LightningChannel) ChannelPoint() wire.OutPoint { return lc.channelState.FundingOutpoint } +// ChannelID returns the ChannelID of this LightningChannel. This is the same +// ChannelID that is used in update messages for this channel. +func (lc *LightningChannel) ChannelID() lnwire.ChannelID { + return lnwire.NewChanIDFromOutPoint(lc.ChannelPoint()) +} + // ShortChanID returns the short channel ID for the channel. The short channel // ID encodes the exact location in the main chain that the original // funding output can be found. From df3c6b72ab4f3ced10e28b2931775b20d7c214bb Mon Sep 17 00:00:00 2001 From: Keagan McClelland Date: Thu, 6 Jun 2024 11:44:44 -0700 Subject: [PATCH 02/18] lnwallet: track ChannelID on PaymentDescriptor In this commit we track the ChannelID on the PaymentDescriptor. This will be useful in upcoming commits that need to be able to reconstruct lnwire.Message values from PaymentDescriptors as the Messages that are exchanged to update channel state all include the ChannelID. --- lnwallet/channel.go | 28 ++++++++++++++++++++++++++++ lnwallet/payment_descriptor.go | 5 +++++ 2 files changed, 33 insertions(+) diff --git a/lnwallet/channel.go b/lnwallet/channel.go index a9d4a39a9..3073cef78 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -196,6 +196,7 @@ func PayDescsFromRemoteLogUpdates(chanID lnwire.ShortChannelID, height uint64, case *lnwire.UpdateAddHTLC: pd = PaymentDescriptor{ + ChanID: wireMsg.ChanID, RHash: wireMsg.PaymentHash, Timeout: wireMsg.Expiry, Amount: wireMsg.Amount, @@ -214,6 +215,7 @@ func PayDescsFromRemoteLogUpdates(chanID lnwire.ShortChannelID, height uint64, case *lnwire.UpdateFulfillHTLC: pd = PaymentDescriptor{ + ChanID: wireMsg.ChanID, RPreimage: wireMsg.PaymentPreimage, ParentIndex: wireMsg.ID, EntryType: Settle, @@ -226,6 +228,7 @@ func PayDescsFromRemoteLogUpdates(chanID lnwire.ShortChannelID, height uint64, case *lnwire.UpdateFailHTLC: pd = PaymentDescriptor{ + ChanID: wireMsg.ChanID, ParentIndex: wireMsg.ID, EntryType: Fail, FailReason: wireMsg.Reason[:], @@ -238,6 +241,7 @@ func PayDescsFromRemoteLogUpdates(chanID lnwire.ShortChannelID, height uint64, case *lnwire.UpdateFailMalformedHTLC: pd = PaymentDescriptor{ + ChanID: wireMsg.ChanID, ParentIndex: wireMsg.ID, EntryType: MalformedFail, FailCode: wireMsg.FailureCode, @@ -646,6 +650,7 @@ func (lc *LightningChannel) diskHtlcToPayDesc(feeRate chainfee.SatPerKWeight, // vs theirs or a pending commit for the remote party), we can now // re-create the original payment descriptor. return PaymentDescriptor{ + ChanID: lc.ChannelID(), RHash: htlc.RHash, Timeout: htlc.RefundTimeout, Amount: htlc.Amt, @@ -1147,6 +1152,7 @@ func (lc *LightningChannel) logUpdateToPayDesc(logUpdate *channeldb.LogUpdate, // as we've included this HTLC in our local commitment chain // for the remote party. pd = &PaymentDescriptor{ + ChanID: wireMsg.ChanID, RHash: wireMsg.PaymentHash, Timeout: wireMsg.Expiry, Amount: wireMsg.Amount, @@ -1192,6 +1198,7 @@ func (lc *LightningChannel) logUpdateToPayDesc(logUpdate *channeldb.LogUpdate, ogHTLC := remoteUpdateLog.lookupHtlc(wireMsg.ID) pd = &PaymentDescriptor{ + ChanID: wireMsg.ChanID, Amount: ogHTLC.Amount, RHash: ogHTLC.RHash, RPreimage: wireMsg.PaymentPreimage, @@ -1209,6 +1216,7 @@ func (lc *LightningChannel) logUpdateToPayDesc(logUpdate *channeldb.LogUpdate, ogHTLC := remoteUpdateLog.lookupHtlc(wireMsg.ID) pd = &PaymentDescriptor{ + ChanID: wireMsg.ChanID, Amount: ogHTLC.Amount, RHash: ogHTLC.RHash, ParentIndex: ogHTLC.HtlcIndex, @@ -1225,6 +1233,7 @@ func (lc *LightningChannel) logUpdateToPayDesc(logUpdate *channeldb.LogUpdate, // TODO(roasbeef): err if nil? pd = &PaymentDescriptor{ + ChanID: wireMsg.ChanID, Amount: ogHTLC.Amount, RHash: ogHTLC.RHash, ParentIndex: ogHTLC.HtlcIndex, @@ -1243,6 +1252,7 @@ func (lc *LightningChannel) logUpdateToPayDesc(logUpdate *channeldb.LogUpdate, // adding and removing it at the same height. case *lnwire.UpdateFee: pd = &PaymentDescriptor{ + ChanID: wireMsg.ChanID, LogIndex: logUpdate.LogIndex, Amount: lnwire.NewMSatFromSatoshis( btcutil.Amount(wireMsg.FeePerKw), @@ -1279,6 +1289,7 @@ func (lc *LightningChannel) localLogUpdateToPayDesc(logUpdate *channeldb.LogUpda ogHTLC := remoteUpdateLog.lookupHtlc(wireMsg.ID) return &PaymentDescriptor{ + ChanID: wireMsg.ChanID, Amount: ogHTLC.Amount, RHash: ogHTLC.RHash, RPreimage: wireMsg.PaymentPreimage, @@ -1295,6 +1306,7 @@ func (lc *LightningChannel) localLogUpdateToPayDesc(logUpdate *channeldb.LogUpda ogHTLC := remoteUpdateLog.lookupHtlc(wireMsg.ID) return &PaymentDescriptor{ + ChanID: wireMsg.ChanID, Amount: ogHTLC.Amount, RHash: ogHTLC.RHash, ParentIndex: ogHTLC.HtlcIndex, @@ -1310,6 +1322,7 @@ func (lc *LightningChannel) localLogUpdateToPayDesc(logUpdate *channeldb.LogUpda ogHTLC := remoteUpdateLog.lookupHtlc(wireMsg.ID) return &PaymentDescriptor{ + ChanID: wireMsg.ChanID, Amount: ogHTLC.Amount, RHash: ogHTLC.RHash, ParentIndex: ogHTLC.HtlcIndex, @@ -1322,6 +1335,7 @@ func (lc *LightningChannel) localLogUpdateToPayDesc(logUpdate *channeldb.LogUpda case *lnwire.UpdateFee: return &PaymentDescriptor{ + ChanID: wireMsg.ChanID, LogIndex: logUpdate.LogIndex, Amount: lnwire.NewMSatFromSatoshis( btcutil.Amount(wireMsg.FeePerKw), @@ -1353,6 +1367,7 @@ func (lc *LightningChannel) remoteLogUpdateToPayDesc(logUpdate *channeldb.LogUpd switch wireMsg := logUpdate.UpdateMsg.(type) { case *lnwire.UpdateAddHTLC: pd := &PaymentDescriptor{ + ChanID: wireMsg.ChanID, RHash: wireMsg.PaymentHash, Timeout: wireMsg.Expiry, Amount: wireMsg.Amount, @@ -1378,6 +1393,7 @@ func (lc *LightningChannel) remoteLogUpdateToPayDesc(logUpdate *channeldb.LogUpd ogHTLC := localUpdateLog.lookupHtlc(wireMsg.ID) return &PaymentDescriptor{ + ChanID: wireMsg.ChanID, Amount: ogHTLC.Amount, RHash: ogHTLC.RHash, RPreimage: wireMsg.PaymentPreimage, @@ -1394,6 +1410,7 @@ func (lc *LightningChannel) remoteLogUpdateToPayDesc(logUpdate *channeldb.LogUpd ogHTLC := localUpdateLog.lookupHtlc(wireMsg.ID) return &PaymentDescriptor{ + ChanID: wireMsg.ChanID, Amount: ogHTLC.Amount, RHash: ogHTLC.RHash, ParentIndex: ogHTLC.HtlcIndex, @@ -1409,6 +1426,7 @@ func (lc *LightningChannel) remoteLogUpdateToPayDesc(logUpdate *channeldb.LogUpd ogHTLC := localUpdateLog.lookupHtlc(wireMsg.ID) return &PaymentDescriptor{ + ChanID: wireMsg.ChanID, Amount: ogHTLC.Amount, RHash: ogHTLC.RHash, ParentIndex: ogHTLC.HtlcIndex, @@ -1427,6 +1445,7 @@ func (lc *LightningChannel) remoteLogUpdateToPayDesc(logUpdate *channeldb.LogUpd // adding and removing it at the same height. case *lnwire.UpdateFee: return &PaymentDescriptor{ + ChanID: wireMsg.ChanID, LogIndex: logUpdate.LogIndex, Amount: lnwire.NewMSatFromSatoshis( btcutil.Amount(wireMsg.FeePerKw), @@ -5962,6 +5981,7 @@ func (lc *LightningChannel) htlcAddDescriptor(htlc *lnwire.UpdateAddHTLC, openKey *models.CircuitKey) *PaymentDescriptor { return &PaymentDescriptor{ + ChanID: htlc.ChanID, EntryType: Add, RHash: PaymentHash(htlc.PaymentHash), Timeout: htlc.Expiry, @@ -6024,6 +6044,7 @@ func (lc *LightningChannel) ReceiveHTLC(htlc *lnwire.UpdateAddHTLC) (uint64, } pd := &PaymentDescriptor{ + ChanID: htlc.ChanID, EntryType: Add, RHash: PaymentHash(htlc.PaymentHash), Timeout: htlc.Expiry, @@ -6104,6 +6125,7 @@ func (lc *LightningChannel) SettleHTLC(preimage [32]byte, } pd := &PaymentDescriptor{ + ChanID: lc.ChannelID(), Amount: htlc.Amount, RPreimage: preimage, LogIndex: lc.updateLogs.Local.logIndex, @@ -6149,6 +6171,7 @@ func (lc *LightningChannel) ReceiveHTLCSettle(preimage [32]byte, htlcIndex uint6 } pd := &PaymentDescriptor{ + ChanID: lc.ChannelID(), Amount: htlc.Amount, RPreimage: preimage, ParentIndex: htlc.HtlcIndex, @@ -6210,6 +6233,7 @@ func (lc *LightningChannel) FailHTLC(htlcIndex uint64, reason []byte, } pd := &PaymentDescriptor{ + ChanID: lc.ChannelID(), Amount: htlc.Amount, RHash: htlc.RHash, ParentIndex: htlcIndex, @@ -6260,6 +6284,7 @@ func (lc *LightningChannel) MalformedFailHTLC(htlcIndex uint64, } pd := &PaymentDescriptor{ + ChanID: lc.ChannelID(), Amount: htlc.Amount, RHash: htlc.RHash, ParentIndex: htlcIndex, @@ -6302,6 +6327,7 @@ func (lc *LightningChannel) ReceiveFailHTLC(htlcIndex uint64, reason []byte, } pd := &PaymentDescriptor{ + ChanID: lc.ChannelID(), Amount: htlc.Amount, RHash: htlc.RHash, ParentIndex: htlc.HtlcIndex, @@ -8421,6 +8447,7 @@ func (lc *LightningChannel) UpdateFee(feePerKw chainfee.SatPerKWeight) error { } pd := &PaymentDescriptor{ + ChanID: lc.ChannelID(), LogIndex: lc.updateLogs.Local.logIndex, Amount: lnwire.NewMSatFromSatoshis(btcutil.Amount(feePerKw)), EntryType: FeeUpdate, @@ -8493,6 +8520,7 @@ func (lc *LightningChannel) ReceiveUpdateFee(feePerKw chainfee.SatPerKWeight) er // TODO(roasbeef): or just modify to use the other balance? pd := &PaymentDescriptor{ + ChanID: lc.ChannelID(), LogIndex: lc.updateLogs.Remote.logIndex, Amount: lnwire.NewMSatFromSatoshis(btcutil.Amount(feePerKw)), EntryType: FeeUpdate, diff --git a/lnwallet/payment_descriptor.go b/lnwallet/payment_descriptor.go index 6fe74bb6f..bedc182cf 100644 --- a/lnwallet/payment_descriptor.go +++ b/lnwallet/payment_descriptor.go @@ -70,6 +70,11 @@ func (u updateType) String() string { // TODO(roasbeef): LogEntry interface?? // - need to separate attrs for cancel/add/settle/feeupdate type PaymentDescriptor struct { + // ChanID is the ChannelID of the LightningChannel that this + // PaymentDescriptor belongs to. We track this here so we can + // reconstruct the Messages that this PaymentDescriptor is built from. + ChanID lnwire.ChannelID + // RHash is the payment hash for this HTLC. The HTLC can be settled iff // the preimage to this hash is presented. RHash PaymentHash From 5deb4c253aa36f7e51ca74ff21e9dfd959bf0250 Mon Sep 17 00:00:00 2001 From: Keagan McClelland Date: Fri, 14 Jun 2024 17:14:25 -0700 Subject: [PATCH 03/18] htlcswitch+lnwallet: fix OnionBlob to 1366 bytes --- htlcswitch/link.go | 4 ++-- lnwallet/channel.go | 26 +++++++++++--------------- lnwallet/payment_descriptor.go | 2 +- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/htlcswitch/link.go b/htlcswitch/link.go index a8e35afe6..51e7290ac 100644 --- a/htlcswitch/link.go +++ b/htlcswitch/link.go @@ -3419,7 +3419,7 @@ func (l *channelLink) processRemoteAdds(fwdPkg *channeldb.FwdPkg, // parse the onion object in order to obtain the // routing information with DecodeHopIterator function // which process the Sphinx packet. - onionReader := bytes.NewReader(pd.OnionBlob) + onionReader := bytes.NewReader(pd.OnionBlob[:]) req := hop.DecodeHopIteratorRequest{ OnionReader: onionReader, @@ -3471,7 +3471,7 @@ func (l *channelLink) processRemoteAdds(fwdPkg *channeldb.FwdPkg, // Fetch the onion blob that was included within this processed // payment descriptor. var onionBlob [lnwire.OnionPacketSize]byte - copy(onionBlob[:], pd.OnionBlob) + copy(onionBlob[:], pd.OnionBlob[:]) // Before adding the new htlc to the state machine, parse the // onion object in order to obtain the routing information with diff --git a/lnwallet/channel.go b/lnwallet/channel.go index 3073cef78..9d34bd395 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -210,8 +210,7 @@ func PayDescsFromRemoteLogUpdates(chanID lnwire.ShortChannelID, height uint64, BlindingPoint: wireMsg.BlindingPoint, CustomRecords: wireMsg.CustomRecords.Copy(), } - pd.OnionBlob = make([]byte, len(wireMsg.OnionBlob)) - copy(pd.OnionBlob[:], wireMsg.OnionBlob[:]) + pd.OnionBlob = wireMsg.OnionBlob case *lnwire.UpdateFulfillHTLC: pd = PaymentDescriptor{ @@ -532,10 +531,10 @@ func (c *commitment) toDiskCommit( HtlcIndex: htlc.HtlcIndex, LogIndex: htlc.LogIndex, Incoming: false, + OnionBlob: htlc.OnionBlob, BlindingPoint: htlc.BlindingPoint, CustomRecords: htlc.CustomRecords.Copy(), } - copy(h.OnionBlob[:], htlc.OnionBlob) if whoseCommit.IsLocal() && htlc.sig != nil { h.Signature = htlc.sig.Serialize() @@ -558,11 +557,10 @@ func (c *commitment) toDiskCommit( HtlcIndex: htlc.HtlcIndex, LogIndex: htlc.LogIndex, Incoming: true, + OnionBlob: htlc.OnionBlob, BlindingPoint: htlc.BlindingPoint, CustomRecords: htlc.CustomRecords.Copy(), } - copy(h.OnionBlob[:], htlc.OnionBlob) - if whoseCommit.IsLocal() && htlc.sig != nil { h.Signature = htlc.sig.Serialize() } @@ -657,7 +655,7 @@ func (lc *LightningChannel) diskHtlcToPayDesc(feeRate chainfee.SatPerKWeight, EntryType: Add, HtlcIndex: htlc.HtlcIndex, LogIndex: htlc.LogIndex, - OnionBlob: htlc.OnionBlob[:], + OnionBlob: htlc.OnionBlob, localOutputIndex: localOutputIndex, remoteOutputIndex: remoteOutputIndex, ourPkScript: ourP2WSH, @@ -1160,11 +1158,10 @@ func (lc *LightningChannel) logUpdateToPayDesc(logUpdate *channeldb.LogUpdate, HtlcIndex: wireMsg.ID, LogIndex: logUpdate.LogIndex, addCommitHeightRemote: commitHeight, + OnionBlob: wireMsg.OnionBlob, BlindingPoint: wireMsg.BlindingPoint, CustomRecords: wireMsg.CustomRecords.Copy(), } - pd.OnionBlob = make([]byte, len(wireMsg.OnionBlob)) - copy(pd.OnionBlob[:], wireMsg.OnionBlob[:]) isDustRemote := HtlcIsDust( lc.channelState.ChanType, false, lntypes.Remote, @@ -1375,11 +1372,10 @@ func (lc *LightningChannel) remoteLogUpdateToPayDesc(logUpdate *channeldb.LogUpd HtlcIndex: wireMsg.ID, LogIndex: logUpdate.LogIndex, addCommitHeightLocal: commitHeight, + OnionBlob: wireMsg.OnionBlob, BlindingPoint: wireMsg.BlindingPoint, CustomRecords: wireMsg.CustomRecords.Copy(), } - pd.OnionBlob = make([]byte, len(wireMsg.OnionBlob)) - copy(pd.OnionBlob, wireMsg.OnionBlob[:]) // We don't need to generate an htlc script yet. This will be // done once we sign our remote commitment. @@ -3424,10 +3420,10 @@ func (lc *LightningChannel) createCommitDiff(newCommit *commitment, Amount: pd.Amount, Expiry: pd.Timeout, PaymentHash: pd.RHash, + OnionBlob: pd.OnionBlob, BlindingPoint: pd.BlindingPoint, CustomRecords: pd.CustomRecords.Copy(), } - copy(htlc.OnionBlob[:], pd.OnionBlob) logUpdate.UpdateMsg = htlc // Gather any references for circuits opened by this Add @@ -3565,10 +3561,10 @@ func (lc *LightningChannel) getUnsignedAckedUpdates() []channeldb.LogUpdate { Amount: pd.Amount, Expiry: pd.Timeout, PaymentHash: pd.RHash, + OnionBlob: pd.OnionBlob, BlindingPoint: pd.BlindingPoint, CustomRecords: pd.CustomRecords.Copy(), } - copy(htlc.OnionBlob[:], pd.OnionBlob) logUpdate.UpdateMsg = htlc case Settle: @@ -5643,10 +5639,10 @@ func (lc *LightningChannel) ReceiveRevocation(revMsg *lnwire.RevokeAndAck) ( Amount: pd.Amount, Expiry: pd.Timeout, PaymentHash: pd.RHash, + OnionBlob: pd.OnionBlob, BlindingPoint: pd.BlindingPoint, CustomRecords: pd.CustomRecords.Copy(), } - copy(htlc.OnionBlob[:], pd.OnionBlob) logUpdate.UpdateMsg = htlc addUpdates = append(addUpdates, logUpdate) @@ -5988,7 +5984,7 @@ func (lc *LightningChannel) htlcAddDescriptor(htlc *lnwire.UpdateAddHTLC, Amount: htlc.Amount, LogIndex: lc.updateLogs.Local.logIndex, HtlcIndex: lc.updateLogs.Local.htlcCounter, - OnionBlob: htlc.OnionBlob[:], + OnionBlob: htlc.OnionBlob, OpenCircuitKey: openKey, BlindingPoint: htlc.BlindingPoint, CustomRecords: htlc.CustomRecords.Copy(), @@ -6051,7 +6047,7 @@ func (lc *LightningChannel) ReceiveHTLC(htlc *lnwire.UpdateAddHTLC) (uint64, Amount: htlc.Amount, LogIndex: lc.updateLogs.Remote.logIndex, HtlcIndex: lc.updateLogs.Remote.htlcCounter, - OnionBlob: htlc.OnionBlob[:], + OnionBlob: htlc.OnionBlob, BlindingPoint: htlc.BlindingPoint, CustomRecords: htlc.CustomRecords.Copy(), } diff --git a/lnwallet/payment_descriptor.go b/lnwallet/payment_descriptor.go index bedc182cf..e5091fc8f 100644 --- a/lnwallet/payment_descriptor.go +++ b/lnwallet/payment_descriptor.go @@ -180,7 +180,7 @@ type PaymentDescriptor struct { // routing. // // NOTE: Populated only on add payment descriptor entry types. - OnionBlob []byte + OnionBlob [lnwire.OnionPacketSize]byte // ShaOnionBlob is a sha of the onion blob. // From 5505b6dafff42e815a025c35501aa235388eb3d9 Mon Sep 17 00:00:00 2001 From: Keagan McClelland Date: Tue, 20 Aug 2024 11:01:34 -0600 Subject: [PATCH 04/18] htlcswitch: change sendMalformedHTLCError to take array instead of slice --- htlcswitch/link.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/htlcswitch/link.go b/htlcswitch/link.go index 51e7290ac..4467940be 100644 --- a/htlcswitch/link.go +++ b/htlcswitch/link.go @@ -3482,7 +3482,7 @@ func (l *channelLink) processRemoteAdds(fwdPkg *channeldb.FwdPkg, // should send the malformed htlc error to payment // sender. l.sendMalformedHTLCError(pd.HtlcIndex, failureCode, - onionBlob[:], pd.SourceRef) + pd.OnionBlob, pd.SourceRef) l.log.Errorf("unable to decode onion hop "+ "iterator: %v", failureCode) @@ -3527,7 +3527,7 @@ func (l *channelLink) processRemoteAdds(fwdPkg *channeldb.FwdPkg, // malformed. l.sendMalformedHTLCError( pd.HtlcIndex, failureCode, - onionBlob[:], pd.SourceRef, + pd.OnionBlob, pd.SourceRef, ) continue @@ -3561,7 +3561,7 @@ func (l *channelLink) processRemoteAdds(fwdPkg *channeldb.FwdPkg, // should send the malformed htlc error to payment // sender. l.sendMalformedHTLCError( - pd.HtlcIndex, failureCode, onionBlob[:], + pd.HtlcIndex, failureCode, pd.OnionBlob, pd.SourceRef, ) @@ -4065,9 +4065,10 @@ func (l *channelLink) sendIncomingHTLCFailureMsg(htlcIndex uint64, // sendMalformedHTLCError helper function which sends the malformed HTLC update // to the payment sender. func (l *channelLink) sendMalformedHTLCError(htlcIndex uint64, - code lnwire.FailCode, onionBlob []byte, sourceRef *channeldb.AddRef) { + code lnwire.FailCode, onionBlob [lnwire.OnionPacketSize]byte, + sourceRef *channeldb.AddRef) { - shaOnionBlob := sha256.Sum256(onionBlob) + shaOnionBlob := sha256.Sum256(onionBlob[:]) err := l.channel.MalformedFailHTLC(htlcIndex, code, shaOnionBlob, sourceRef) if err != nil { l.log.Errorf("unable cancel htlc: %v", err) From 721a0c5edc6052a9e062f476ba9a703a8c75c139 Mon Sep 17 00:00:00 2001 From: Keagan McClelland Date: Tue, 20 Aug 2024 11:28:37 -0600 Subject: [PATCH 05/18] lnwire+htlcswitch: change NewInvalidBlinding to use array instead of slice --- htlcswitch/link.go | 11 ++++------- lnwire/onion_error.go | 12 +++++++++--- lnwire/onion_error_test.go | 11 ++++++----- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/htlcswitch/link.go b/htlcswitch/link.go index 4467940be..80362161d 100644 --- a/htlcswitch/link.go +++ b/htlcswitch/link.go @@ -3468,11 +3468,6 @@ func (l *channelLink) processRemoteAdds(fwdPkg *channeldb.FwdPkg, // or are able to settle it (and it adheres to our fee related // constraints). - // Fetch the onion blob that was included within this processed - // payment descriptor. - var onionBlob [lnwire.OnionPacketSize]byte - copy(onionBlob[:], pd.OnionBlob[:]) - // Before adding the new htlc to the state machine, parse the // onion object in order to obtain the routing information with // DecodeHopIterator function which process the Sphinx packet. @@ -3581,7 +3576,7 @@ func (l *channelLink) processRemoteAdds(fwdPkg *channeldb.FwdPkg, l.cfg.DisallowRouteBlinding { failure := lnwire.NewInvalidBlinding( - onionBlob[:], + fn.Some(pd.OnionBlob), ) l.sendHTLCError( pd, NewLinkError(failure), obfuscator, false, @@ -4027,7 +4022,9 @@ func (l *channelLink) sendIncomingHTLCFailureMsg(htlcIndex uint64, // The specification does not require that we set the onion // blob. - failureMsg := lnwire.NewInvalidBlinding(nil) + failureMsg := lnwire.NewInvalidBlinding( + fn.None[[lnwire.OnionPacketSize]byte](), + ) reason, err := e.EncryptFirstHop(failureMsg) if err != nil { return err diff --git a/lnwire/onion_error.go b/lnwire/onion_error.go index 66db6a9f5..f8a8bf069 100644 --- a/lnwire/onion_error.go +++ b/lnwire/onion_error.go @@ -10,6 +10,7 @@ import ( "github.com/davecgh/go-spew/spew" "github.com/go-errors/errors" + "github.com/lightningnetwork/lnd/fn" "github.com/lightningnetwork/lnd/tlv" ) @@ -1271,14 +1272,19 @@ func (f *FailInvalidBlinding) Encode(w *bytes.Buffer, _ uint32) error { } // NewInvalidBlinding creates new instance of FailInvalidBlinding. -func NewInvalidBlinding(onion []byte) *FailInvalidBlinding { +func NewInvalidBlinding( + onion fn.Option[[OnionPacketSize]byte]) *FailInvalidBlinding { // The spec allows empty onion hashes for invalid blinding, so we only // include our onion hash if it's provided. - if onion == nil { + if onion.IsNone() { return &FailInvalidBlinding{} } - return &FailInvalidBlinding{OnionSHA256: sha256.Sum256(onion)} + shaSum := fn.MapOptionZ(onion, func(o [OnionPacketSize]byte) [32]byte { + return sha256.Sum256(o[:]) + }) + + return &FailInvalidBlinding{OnionSHA256: shaSum} } // DecodeFailure decodes, validates, and parses the lnwire onion failure, for diff --git a/lnwire/onion_error_test.go b/lnwire/onion_error_test.go index a06c572dc..bc14d5d42 100644 --- a/lnwire/onion_error_test.go +++ b/lnwire/onion_error_test.go @@ -9,11 +9,12 @@ import ( "testing" "github.com/davecgh/go-spew/spew" + "github.com/lightningnetwork/lnd/fn" "github.com/stretchr/testify/require" ) var ( - testOnionHash = []byte{} + testOnionHash = [OnionPacketSize]byte{} testAmount = MilliSatoshi(1) testCtlvExpiry = uint32(2) testFlags = uint16(2) @@ -43,9 +44,9 @@ var onionFailures = []FailureMessage{ &FailMPPTimeout{}, NewFailIncorrectDetails(99, 100), - NewInvalidOnionVersion(testOnionHash), - NewInvalidOnionHmac(testOnionHash), - NewInvalidOnionKey(testOnionHash), + NewInvalidOnionVersion(testOnionHash[:]), + NewInvalidOnionHmac(testOnionHash[:]), + NewInvalidOnionKey(testOnionHash[:]), NewTemporaryChannelFailure(&testChannelUpdate), NewTemporaryChannelFailure(nil), NewAmountBelowMinimum(testAmount, testChannelUpdate), @@ -56,7 +57,7 @@ var onionFailures = []FailureMessage{ NewFinalIncorrectCltvExpiry(testCtlvExpiry), NewFinalIncorrectHtlcAmount(testAmount), NewInvalidOnionPayload(testType, testOffset), - NewInvalidBlinding(testOnionHash), + NewInvalidBlinding(fn.Some(testOnionHash)), } // TestEncodeDecodeCode tests the ability of onion errors to be properly encoded From 1e2bf3e0b3ff6bdac227bc67451acbcd7c920240 Mon Sep 17 00:00:00 2001 From: Keagan McClelland Date: Fri, 14 Jun 2024 17:38:52 -0700 Subject: [PATCH 06/18] lnwallet: add function to convert paymentDescriptor to LogUpdate Here we add a function that is capable of recovering LogUpdates from paymentDescriptors and we refactor the lnwallet code to use this rather than doing JIT inline construction of the LogUpdates. --- lnwallet/channel.go | 235 +++++---------------------------- lnwallet/payment_descriptor.go | 53 ++++++++ 2 files changed, 83 insertions(+), 205 deletions(-) diff --git a/lnwallet/channel.go b/lnwallet/channel.go index 9d34bd395..205b339a2 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -3371,12 +3371,6 @@ func genRemoteHtlcSigJobs(keyRing *CommitmentKeyRing, func (lc *LightningChannel) createCommitDiff(newCommit *commitment, commitSig lnwire.Sig, htlcSigs []lnwire.Sig) *channeldb.CommitDiff { - // First, we need to convert the funding outpoint into the ID that's - // used on the wire to identify this channel. We'll use this shortly - // when recording the exact CommitSig message that we'll be sending - // out. - chanID := lnwire.NewChanIDFromOutPoint(lc.channelState.FundingOutpoint) - var ( logUpdates []channeldb.LogUpdate ackAddRefs []channeldb.AddRef @@ -3401,91 +3395,42 @@ func (lc *LightningChannel) createCommitDiff(newCommit *commitment, continue } - // Knowing that this update is a part of this new commitment, - // we'll create a log update and not its index in the log so - // we can later restore it properly if a restart occurs. - logUpdate := channeldb.LogUpdate{ - LogIndex: pd.LogIndex, - } - // We'll map the type of the PaymentDescriptor to one of the // four messages that it corresponds to. With this set of // messages obtained, we can simply read from disk and re-send // them in the case of a needed channel sync. switch pd.EntryType { case Add: - htlc := &lnwire.UpdateAddHTLC{ - ChanID: chanID, - ID: pd.HtlcIndex, - Amount: pd.Amount, - Expiry: pd.Timeout, - PaymentHash: pd.RHash, - OnionBlob: pd.OnionBlob, - BlindingPoint: pd.BlindingPoint, - CustomRecords: pd.CustomRecords.Copy(), - } - logUpdate.UpdateMsg = htlc - // Gather any references for circuits opened by this Add // HTLC. if pd.OpenCircuitKey != nil { - openCircuitKeys = append(openCircuitKeys, - *pd.OpenCircuitKey) + openCircuitKeys = append( + openCircuitKeys, *pd.OpenCircuitKey, + ) } - logUpdates = append(logUpdates, logUpdate) - - // Short circuit here since an add should not have any - // of the references gathered in the case of settles, - // fails or malformed fails. - continue - - case Settle: - logUpdate.UpdateMsg = &lnwire.UpdateFulfillHTLC{ - ChanID: chanID, - ID: pd.ParentIndex, - PaymentPreimage: pd.RPreimage, + case Settle, Fail, MalformedFail: + // Gather the fwd pkg references from any settle or fail + // packets, if they exist. + if pd.SourceRef != nil { + ackAddRefs = append(ackAddRefs, *pd.SourceRef) } - - case Fail: - logUpdate.UpdateMsg = &lnwire.UpdateFailHTLC{ - ChanID: chanID, - ID: pd.ParentIndex, - Reason: pd.FailReason, + if pd.DestRef != nil { + settleFailRefs = append( + settleFailRefs, *pd.DestRef, + ) } - - case MalformedFail: - logUpdate.UpdateMsg = &lnwire.UpdateFailMalformedHTLC{ - ChanID: chanID, - ID: pd.ParentIndex, - ShaOnionBlob: pd.ShaOnionBlob, - FailureCode: pd.FailCode, + if pd.ClosedCircuitKey != nil { + closedCircuitKeys = append( + closedCircuitKeys, *pd.ClosedCircuitKey, + ) } case FeeUpdate: - // The Amount field holds the feerate denominated in - // msat. Since feerates are only denominated in sat/kw, - // we can convert it without loss of precision. - logUpdate.UpdateMsg = &lnwire.UpdateFee{ - ChanID: chanID, - FeePerKw: uint32(pd.Amount.ToSatoshis()), - } + // Nothing special to do. } - // Gather the fwd pkg references from any settle or fail - // packets, if they exist. - if pd.SourceRef != nil { - ackAddRefs = append(ackAddRefs, *pd.SourceRef) - } - if pd.DestRef != nil { - settleFailRefs = append(settleFailRefs, *pd.DestRef) - } - if pd.ClosedCircuitKey != nil { - closedCircuitKeys = append(closedCircuitKeys, - *pd.ClosedCircuitKey) - } - - logUpdates = append(logUpdates, logUpdate) + logUpdates = append(logUpdates, pd.ToLogUpdate()) } // With the set of log updates mapped into wire messages, we'll now @@ -3513,10 +3458,6 @@ func (lc *LightningChannel) createCommitDiff(newCommit *commitment, // getUnsignedAckedUpdates returns all remote log updates that we haven't // signed for yet ourselves. func (lc *LightningChannel) getUnsignedAckedUpdates() []channeldb.LogUpdate { - // First, we need to convert the funding outpoint into the ID that's - // used on the wire to identify this channel. - chanID := lnwire.NewChanIDFromOutPoint(lc.channelState.FundingOutpoint) - // Fetch the last remote update that we have signed for. lastRemoteCommitted := lc.commitChains.Remote.tail().messageIndices.Remote @@ -3547,60 +3488,9 @@ func (lc *LightningChannel) getUnsignedAckedUpdates() []channeldb.LogUpdate { continue } - logUpdate := channeldb.LogUpdate{ - LogIndex: pd.LogIndex, - } - - // We'll map the type of the PaymentDescriptor to one of the - // four messages that it corresponds to. - switch pd.EntryType { - case Add: - htlc := &lnwire.UpdateAddHTLC{ - ChanID: chanID, - ID: pd.HtlcIndex, - Amount: pd.Amount, - Expiry: pd.Timeout, - PaymentHash: pd.RHash, - OnionBlob: pd.OnionBlob, - BlindingPoint: pd.BlindingPoint, - CustomRecords: pd.CustomRecords.Copy(), - } - logUpdate.UpdateMsg = htlc - - case Settle: - logUpdate.UpdateMsg = &lnwire.UpdateFulfillHTLC{ - ChanID: chanID, - ID: pd.ParentIndex, - PaymentPreimage: pd.RPreimage, - } - - case Fail: - logUpdate.UpdateMsg = &lnwire.UpdateFailHTLC{ - ChanID: chanID, - ID: pd.ParentIndex, - Reason: pd.FailReason, - } - - case MalformedFail: - logUpdate.UpdateMsg = &lnwire.UpdateFailMalformedHTLC{ - ChanID: chanID, - ID: pd.ParentIndex, - ShaOnionBlob: pd.ShaOnionBlob, - FailureCode: pd.FailCode, - } - - case FeeUpdate: - // The Amount field holds the feerate denominated in - // msat. Since feerates are only denominated in sat/kw, - // we can convert it without loss of precision. - logUpdate.UpdateMsg = &lnwire.UpdateFee{ - ChanID: chanID, - FeePerKw: uint32(pd.Amount.ToSatoshis()), - } - } - - logUpdates = append(logUpdates, logUpdate) + logUpdates = append(logUpdates, pd.ToLogUpdate()) } + return logUpdates } @@ -5621,55 +5511,19 @@ func (lc *LightningChannel) ReceiveRevocation(revMsg *lnwire.RevokeAndAck) ( // If we've reached this point, this HTLC will be added to the // forwarding package at the height of the remote commitment. - // All types of HTLCs will record their assigned log index. - logUpdate := channeldb.LogUpdate{ - LogIndex: pd.LogIndex, - } - - // Next, we'll map the type of the PaymentDescriptor to one of - // the four messages that it corresponds to and separate the + // We'll map the type of the PaymentDescriptor to one of the + // four messages that it corresponds to and separate the // updates into Adds and Settle/Fail/MalformedFail such that // they can be written in the forwarding package. Adds are // aggregated separately from the other types of HTLCs. switch pd.EntryType { case Add: - htlc := &lnwire.UpdateAddHTLC{ - ChanID: chanID, - ID: pd.HtlcIndex, - Amount: pd.Amount, - Expiry: pd.Timeout, - PaymentHash: pd.RHash, - OnionBlob: pd.OnionBlob, - BlindingPoint: pd.BlindingPoint, - CustomRecords: pd.CustomRecords.Copy(), - } - logUpdate.UpdateMsg = htlc - addUpdates = append(addUpdates, logUpdate) + addUpdates = append(addUpdates, pd.ToLogUpdate()) - case Settle: - logUpdate.UpdateMsg = &lnwire.UpdateFulfillHTLC{ - ChanID: chanID, - ID: pd.ParentIndex, - PaymentPreimage: pd.RPreimage, - } - settleFailUpdates = append(settleFailUpdates, logUpdate) - - case Fail: - logUpdate.UpdateMsg = &lnwire.UpdateFailHTLC{ - ChanID: chanID, - ID: pd.ParentIndex, - Reason: pd.FailReason, - } - settleFailUpdates = append(settleFailUpdates, logUpdate) - - case MalformedFail: - logUpdate.UpdateMsg = &lnwire.UpdateFailMalformedHTLC{ - ChanID: chanID, - ID: pd.ParentIndex, - ShaOnionBlob: pd.ShaOnionBlob, - FailureCode: pd.FailCode, - } - settleFailUpdates = append(settleFailUpdates, logUpdate) + case Settle, Fail, MalformedFail: + settleFailUpdates = append( + settleFailUpdates, pd.ToLogUpdate(), + ) } } @@ -9005,38 +8859,9 @@ func (lc *LightningChannel) unsignedLocalUpdates(remoteMessageIndex, // covered in the next commitment signature that the remote // sends. if pd.LogIndex < remoteMessageIndex && pd.LogIndex >= localMessageIndex { - logUpdate := channeldb.LogUpdate{ - LogIndex: pd.LogIndex, - } - - switch pd.EntryType { - case FeeUpdate: - logUpdate.UpdateMsg = &lnwire.UpdateFee{ - ChanID: chanID, - FeePerKw: uint32(pd.Amount.ToSatoshis()), - } - case Settle: - logUpdate.UpdateMsg = &lnwire.UpdateFulfillHTLC{ - ChanID: chanID, - ID: pd.ParentIndex, - PaymentPreimage: pd.RPreimage, - } - case Fail: - logUpdate.UpdateMsg = &lnwire.UpdateFailHTLC{ - ChanID: chanID, - ID: pd.ParentIndex, - Reason: pd.FailReason, - } - case MalformedFail: - logUpdate.UpdateMsg = &lnwire.UpdateFailMalformedHTLC{ - ChanID: chanID, - ID: pd.ParentIndex, - ShaOnionBlob: pd.ShaOnionBlob, - FailureCode: pd.FailCode, - } - } - - localPeerUpdates = append(localPeerUpdates, logUpdate) + localPeerUpdates = append( + localPeerUpdates, pd.ToLogUpdate(), + ) } } diff --git a/lnwallet/payment_descriptor.go b/lnwallet/payment_descriptor.go index e5091fc8f..2d6c158cd 100644 --- a/lnwallet/payment_descriptor.go +++ b/lnwallet/payment_descriptor.go @@ -231,3 +231,56 @@ type PaymentDescriptor struct { // may have been attached to a sent HTLC. CustomRecords lnwire.CustomRecords } + +// ToLogUpdate recovers the underlying LogUpdate from the paymentDescriptor. +// This operation is lossy and will forget some extra information tracked by the +// paymentDescriptor but the function is total in that all paymentDescriptors +// can be converted back to LogUpdates. +func (pd *PaymentDescriptor) ToLogUpdate() channeldb.LogUpdate { + var msg lnwire.Message + switch pd.EntryType { + case Add: + msg = &lnwire.UpdateAddHTLC{ + ChanID: pd.ChanID, + ID: pd.HtlcIndex, + Amount: pd.Amount, + PaymentHash: pd.RHash, + Expiry: pd.Timeout, + OnionBlob: pd.OnionBlob, + BlindingPoint: pd.BlindingPoint, + CustomRecords: pd.CustomRecords.Copy(), + } + case Settle: + msg = &lnwire.UpdateFulfillHTLC{ + ChanID: pd.ChanID, + ID: pd.ParentIndex, + PaymentPreimage: pd.RPreimage, + } + case Fail: + msg = &lnwire.UpdateFailHTLC{ + ChanID: pd.ChanID, + ID: pd.ParentIndex, + Reason: pd.FailReason, + } + case MalformedFail: + msg = &lnwire.UpdateFailMalformedHTLC{ + ChanID: pd.ChanID, + ID: pd.ParentIndex, + ShaOnionBlob: pd.ShaOnionBlob, + FailureCode: pd.FailCode, + } + case FeeUpdate: + // The Amount field holds the feerate denominated in + // msat. Since feerates are only denominated in sat/kw, + // we can convert it without loss of precision. + msg = &lnwire.UpdateFee{ + ChanID: pd.ChanID, + FeePerKw: uint32(pd.Amount.ToSatoshis()), + } + } + + return channeldb.LogUpdate{ + LogIndex: pd.LogIndex, + UpdateMsg: msg, + } +} From 0fb29b2598c07540988f9759a986c5a829484819 Mon Sep 17 00:00:00 2001 From: Keagan McClelland Date: Fri, 16 Aug 2024 15:55:18 -0700 Subject: [PATCH 07/18] channeldb: add convenience functions for generating Source/Dest Refs --- channeldb/forwarding_package.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/channeldb/forwarding_package.go b/channeldb/forwarding_package.go index 4c447fc03..11c0099e7 100644 --- a/channeldb/forwarding_package.go +++ b/channeldb/forwarding_package.go @@ -286,6 +286,27 @@ func NewFwdPkg(source lnwire.ShortChannelID, height uint64, } } +// SourceRef is a convenience method that returns an AddRef to this forwarding +// package for the index in the argument. It is the caller's responsibility +// to ensure that the index is in bounds. +func (f *FwdPkg) SourceRef(i uint16) AddRef { + return AddRef{ + Height: f.Height, + Index: i, + } +} + +// DestRef is a convenience method that returns a SettleFailRef to this +// forwarding package for the index in the argument. It is the caller's +// responsibility to ensure that the index is in bounds. +func (f *FwdPkg) DestRef(i uint16) SettleFailRef { + return SettleFailRef{ + Source: f.Source, + Height: f.Height, + Index: i, + } +} + // ID returns an unique identifier for this package, used to ensure that sphinx // replay processing of this batch is idempotent. func (f *FwdPkg) ID() []byte { From 877d29c717728a91b5f2842674935aa0d974b21f Mon Sep 17 00:00:00 2001 From: Keagan McClelland Date: Fri, 16 Aug 2024 12:45:35 -0700 Subject: [PATCH 08/18] htlcswitch: remove PaymentDescriptor from sendHTLCError's call signature This is done as part of a systematic removal of PaymentDescriptor from the mechanics of the htlcswitch package. --- htlcswitch/link.go | 53 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 13 deletions(-) diff --git a/htlcswitch/link.go b/htlcswitch/link.go index 80362161d..65c609f17 100644 --- a/htlcswitch/link.go +++ b/htlcswitch/link.go @@ -1532,8 +1532,11 @@ func (l *channelLink) processHtlcResolution(resolution invoices.HtlcResolution, // result. failure := getResolutionFailure(res, htlc.pd.Amount) + //nolint:forcetypeassert + add := htlc.pd.ToLogUpdate().UpdateMsg.(*lnwire.UpdateAddHTLC) l.sendHTLCError( - htlc.pd, failure, htlc.obfuscator, true, + *add, *htlc.pd.SourceRef, failure, htlc.obfuscator, + true, ) return nil @@ -3535,8 +3538,13 @@ func (l *channelLink) processRemoteAdds(fwdPkg *channeldb.FwdPkg, // payloads. Deferring this non-trival effort till a // later date failure := lnwire.NewInvalidOnionPayload(failedType, 0) + + addMsg := pd.ToLogUpdate().UpdateMsg + //nolint:forcetypeassert + add := *addMsg.(*lnwire.UpdateAddHTLC) l.sendHTLCError( - pd, NewLinkError(failure), obfuscator, false, + add, *pd.SourceRef, NewLinkError(failure), + obfuscator, false, ) l.log.Errorf("unable to decode forwarding "+ @@ -3578,8 +3586,13 @@ func (l *channelLink) processRemoteAdds(fwdPkg *channeldb.FwdPkg, failure := lnwire.NewInvalidBlinding( fn.Some(pd.OnionBlob), ) + + addMsg := pd.ToLogUpdate().UpdateMsg + //nolint:forcetypeassert + add := *addMsg.(*lnwire.UpdateAddHTLC) l.sendHTLCError( - pd, NewLinkError(failure), obfuscator, false, + add, *pd.SourceRef, NewLinkError(failure), + obfuscator, false, ) l.log.Error("rejected htlc that uses use as an " + @@ -3699,8 +3712,13 @@ func (l *channelLink) processRemoteAdds(fwdPkg *channeldb.FwdPkg, true, hop.Source, cb, ) + addMsg := pd.ToLogUpdate().UpdateMsg + //nolint:forcetypeassert + add := *addMsg.(*lnwire.UpdateAddHTLC) l.sendHTLCError( - pd, NewLinkError(failure), obfuscator, false, + add, *pd.SourceRef, + NewLinkError(failure), obfuscator, + false, ) continue } @@ -3794,7 +3812,11 @@ func (l *channelLink) processExitHop(pd *lnwallet.PaymentDescriptor, failure := NewLinkError( lnwire.NewFinalIncorrectHtlcAmount(pd.Amount), ) - l.sendHTLCError(pd, failure, obfuscator, true) + + addMsg := pd.ToLogUpdate().UpdateMsg + //nolint:forcetypeassert + add := *addMsg.(*lnwire.UpdateAddHTLC) + l.sendHTLCError(add, *pd.SourceRef, failure, obfuscator, true) return nil } @@ -3809,7 +3831,11 @@ func (l *channelLink) processExitHop(pd *lnwallet.PaymentDescriptor, failure := NewLinkError( lnwire.NewFinalIncorrectCltvExpiry(pd.Timeout), ) - l.sendHTLCError(pd, failure, obfuscator, true) + + addMsg := pd.ToLogUpdate().UpdateMsg + //nolint:forcetypeassert + add := *addMsg.(*lnwire.UpdateAddHTLC) + l.sendHTLCError(add, *pd.SourceRef, failure, obfuscator, true) return nil } @@ -3920,8 +3946,9 @@ func (l *channelLink) forwardBatch(replay bool, packets ...*htlcPacket) { // sendHTLCError functions cancels HTLC and send cancel message back to the // peer from which HTLC was received. -func (l *channelLink) sendHTLCError(pd *lnwallet.PaymentDescriptor, - failure *LinkError, e hop.ErrorEncrypter, isReceive bool) { +func (l *channelLink) sendHTLCError(add lnwire.UpdateAddHTLC, + sourceRef channeldb.AddRef, failure *LinkError, + e hop.ErrorEncrypter, isReceive bool) { reason, err := e.EncryptFirstHop(failure.WireMessage()) if err != nil { @@ -3929,7 +3956,7 @@ func (l *channelLink) sendHTLCError(pd *lnwallet.PaymentDescriptor, return } - err = l.channel.FailHTLC(pd.HtlcIndex, reason, pd.SourceRef, nil, nil) + err = l.channel.FailHTLC(add.ID, reason, &sourceRef, nil, nil) if err != nil { l.log.Errorf("unable cancel htlc: %v", err) return @@ -3938,7 +3965,7 @@ func (l *channelLink) sendHTLCError(pd *lnwallet.PaymentDescriptor, // Send the appropriate failure message depending on whether we're // in a blinded route or not. if err := l.sendIncomingHTLCFailureMsg( - pd.HtlcIndex, e, reason, + add.ID, e, reason, ); err != nil { l.log.Errorf("unable to send HTLC failure: %v", err) return @@ -3958,12 +3985,12 @@ func (l *channelLink) sendHTLCError(pd *lnwallet.PaymentDescriptor, HtlcKey{ IncomingCircuit: models.CircuitKey{ ChanID: l.ShortChanID(), - HtlcID: pd.HtlcIndex, + HtlcID: add.ID, }, }, HtlcInfo{ - IncomingTimeLock: pd.Timeout, - IncomingAmt: pd.Amount, + IncomingTimeLock: add.Expiry, + IncomingAmt: add.Amount, }, eventType, failure, From 07647fe53adbbc7e17ce0041b2e704448788e896 Mon Sep 17 00:00:00 2001 From: Keagan McClelland Date: Fri, 16 Aug 2024 12:48:04 -0700 Subject: [PATCH 09/18] htlcswitch: remove PaymentDescriptor from settleHTLC's call signature This is done as part of a systematic removal of PaymentDescriptor from the mechanics of the htlcswitch package. --- htlcswitch/link.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/htlcswitch/link.go b/htlcswitch/link.go index 65c609f17..389d65261 100644 --- a/htlcswitch/link.go +++ b/htlcswitch/link.go @@ -1520,7 +1520,9 @@ func (l *channelLink) processHtlcResolution(resolution invoices.HtlcResolution, l.log.Debugf("received settle resolution for %v "+ "with outcome: %v", circuitKey, res.Outcome) - return l.settleHTLC(res.Preimage, htlc.pd) + return l.settleHTLC( + res.Preimage, htlc.pd.HtlcIndex, *htlc.pd.SourceRef, + ) // For htlc failures, we get the relevant failure message based // on the failure resolution and then fail the htlc. @@ -3877,14 +3879,14 @@ func (l *channelLink) processExitHop(pd *lnwallet.PaymentDescriptor, // settleHTLC settles the HTLC on the channel. func (l *channelLink) settleHTLC(preimage lntypes.Preimage, - pd *lnwallet.PaymentDescriptor) error { + htlcIndex uint64, sourceRef channeldb.AddRef) error { hash := preimage.Hash() l.log.Infof("settling htlc %v as exit hop", hash) err := l.channel.SettleHTLC( - preimage, pd.HtlcIndex, pd.SourceRef, nil, nil, + preimage, htlcIndex, &sourceRef, nil, nil, ) if err != nil { return fmt.Errorf("unable to settle htlc: %w", err) @@ -3902,7 +3904,7 @@ func (l *channelLink) settleHTLC(preimage lntypes.Preimage, // remote peer. l.cfg.Peer.SendMessage(false, &lnwire.UpdateFulfillHTLC{ ChanID: l.ChanID(), - ID: pd.HtlcIndex, + ID: htlcIndex, PaymentPreimage: preimage, }) @@ -3911,7 +3913,7 @@ func (l *channelLink) settleHTLC(preimage lntypes.Preimage, HtlcKey{ IncomingCircuit: models.CircuitKey{ ChanID: l.ShortChanID(), - HtlcID: pd.HtlcIndex, + HtlcID: htlcIndex, }, }, preimage, From a0818a0d1607c3482ce435973aba157b95c3b4b9 Mon Sep 17 00:00:00 2001 From: Keagan McClelland Date: Fri, 16 Aug 2024 13:02:51 -0700 Subject: [PATCH 10/18] htlcswitch: remove PaymentDescriptor from the processExitHop's call signature This is part of a systematic removal of PaymentDescriptor from the mechanics of the htlcswitch package. --- htlcswitch/link.go | 55 ++++++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/htlcswitch/link.go b/htlcswitch/link.go index 389d65261..b00f7762a 100644 --- a/htlcswitch/link.go +++ b/htlcswitch/link.go @@ -3606,8 +3606,12 @@ func (l *channelLink) processRemoteAdds(fwdPkg *channeldb.FwdPkg, switch fwdInfo.NextHop { case hop.Exit: + addMsg := pd.ToLogUpdate().UpdateMsg + //nolint:forcetypeassert + add := *addMsg.(*lnwire.UpdateAddHTLC) err := l.processExitHop( - pd, obfuscator, fwdInfo, heightNow, pld, + add, *pd.SourceRef, obfuscator, fwdInfo, + heightNow, pld, ) if err != nil { l.failf(LinkFailureError{ @@ -3790,9 +3794,10 @@ func (l *channelLink) processRemoteAdds(fwdPkg *channeldb.FwdPkg, // processExitHop handles an htlc for which this link is the exit hop. It // returns a boolean indicating whether the commitment tx needs an update. -func (l *channelLink) processExitHop(pd *lnwallet.PaymentDescriptor, - obfuscator hop.ErrorEncrypter, fwdInfo hop.ForwardingInfo, - heightNow uint32, payload invoices.Payload) error { +func (l *channelLink) processExitHop(add lnwire.UpdateAddHTLC, + sourceRef channeldb.AddRef, obfuscator hop.ErrorEncrypter, + fwdInfo hop.ForwardingInfo, heightNow uint32, + payload invoices.Payload) error { // If hodl.ExitSettle is requested, we will not validate the final hop's // ADD, nor will we settle the corresponding invoice or respond with the @@ -3806,38 +3811,31 @@ func (l *channelLink) processExitHop(pd *lnwallet.PaymentDescriptor, // As we're the exit hop, we'll double check the hop-payload included in // the HTLC to ensure that it was crafted correctly by the sender and // is compatible with the HTLC we were extended. - if pd.Amount < fwdInfo.AmountToForward { + if add.Amount < fwdInfo.AmountToForward { l.log.Errorf("onion payload of incoming htlc(%x) has "+ - "incompatible value: expected <=%v, got %v", pd.RHash, - pd.Amount, fwdInfo.AmountToForward) + "incompatible value: expected <=%v, got %v", + add.PaymentHash, add.Amount, fwdInfo.AmountToForward) failure := NewLinkError( - lnwire.NewFinalIncorrectHtlcAmount(pd.Amount), + lnwire.NewFinalIncorrectHtlcAmount(add.Amount), ) - - addMsg := pd.ToLogUpdate().UpdateMsg - //nolint:forcetypeassert - add := *addMsg.(*lnwire.UpdateAddHTLC) - l.sendHTLCError(add, *pd.SourceRef, failure, obfuscator, true) + l.sendHTLCError(add, sourceRef, failure, obfuscator, true) return nil } // We'll also ensure that our time-lock value has been computed // correctly. - if pd.Timeout < fwdInfo.OutgoingCTLV { + if add.Expiry < fwdInfo.OutgoingCTLV { l.log.Errorf("onion payload of incoming htlc(%x) has "+ "incompatible time-lock: expected <=%v, got %v", - pd.RHash[:], pd.Timeout, fwdInfo.OutgoingCTLV) + add.PaymentHash, add.Expiry, fwdInfo.OutgoingCTLV) failure := NewLinkError( - lnwire.NewFinalIncorrectCltvExpiry(pd.Timeout), + lnwire.NewFinalIncorrectCltvExpiry(add.Expiry), ) - addMsg := pd.ToLogUpdate().UpdateMsg - //nolint:forcetypeassert - add := *addMsg.(*lnwire.UpdateAddHTLC) - l.sendHTLCError(add, *pd.SourceRef, failure, obfuscator, true) + l.sendHTLCError(add, sourceRef, failure, obfuscator, true) return nil } @@ -3845,15 +3843,15 @@ func (l *channelLink) processExitHop(pd *lnwallet.PaymentDescriptor, // Notify the invoiceRegistry of the exit hop htlc. If we crash right // after this, this code will be re-executed after restart. We will // receive back a resolution event. - invoiceHash := lntypes.Hash(pd.RHash) + invoiceHash := lntypes.Hash(add.PaymentHash) circuitKey := models.CircuitKey{ ChanID: l.ShortChanID(), - HtlcID: pd.HtlcIndex, + HtlcID: add.ID, } event, err := l.cfg.Registry.NotifyExitHopHtlc( - invoiceHash, pd.Amount, pd.Timeout, int32(heightNow), + invoiceHash, add.Amount, add.Expiry, int32(heightNow), circuitKey, l.hodlQueue.ChanIn(), payload, ) if err != nil { @@ -3862,7 +3860,16 @@ func (l *channelLink) processExitHop(pd *lnwallet.PaymentDescriptor, // Create a hodlHtlc struct and decide either resolved now or later. htlc := hodlHtlc{ - pd: pd, + pd: &lnwallet.PaymentDescriptor{ + EntryType: lnwallet.Add, + ChanID: add.ChanID, + RHash: add.PaymentHash, + Timeout: add.Expiry, + Amount: add.Amount, + HtlcIndex: add.ID, + SourceRef: &sourceRef, + BlindingPoint: add.BlindingPoint, + }, obfuscator: obfuscator, } From d881809a4dbe45bd5aa5e2095635daa994c23857 Mon Sep 17 00:00:00 2001 From: Keagan McClelland Date: Fri, 16 Aug 2024 13:05:54 -0700 Subject: [PATCH 11/18] htlcswitch: remove PaymentDescriptor from hodlHtlc This is part of a systematic removal of PaymentDescriptor from the mechanics of the htlcswitch package. --- htlcswitch/link.go | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/htlcswitch/link.go b/htlcswitch/link.go index b00f7762a..6d243db0a 100644 --- a/htlcswitch/link.go +++ b/htlcswitch/link.go @@ -442,7 +442,8 @@ func (m *hookMap) invoke() { // hodlHtlc contains htlc data that is required for resolution. type hodlHtlc struct { - pd *lnwallet.PaymentDescriptor + add lnwire.UpdateAddHTLC + sourceRef channeldb.AddRef obfuscator hop.ErrorEncrypter } @@ -1521,7 +1522,7 @@ func (l *channelLink) processHtlcResolution(resolution invoices.HtlcResolution, "with outcome: %v", circuitKey, res.Outcome) return l.settleHTLC( - res.Preimage, htlc.pd.HtlcIndex, *htlc.pd.SourceRef, + res.Preimage, htlc.add.ID, htlc.sourceRef, ) // For htlc failures, we get the relevant failure message based @@ -1532,12 +1533,10 @@ func (l *channelLink) processHtlcResolution(resolution invoices.HtlcResolution, // Get the lnwire failure message based on the resolution // result. - failure := getResolutionFailure(res, htlc.pd.Amount) + failure := getResolutionFailure(res, htlc.add.Amount) - //nolint:forcetypeassert - add := htlc.pd.ToLogUpdate().UpdateMsg.(*lnwire.UpdateAddHTLC) l.sendHTLCError( - *add, *htlc.pd.SourceRef, failure, htlc.obfuscator, + htlc.add, htlc.sourceRef, failure, htlc.obfuscator, true, ) return nil @@ -3860,16 +3859,8 @@ func (l *channelLink) processExitHop(add lnwire.UpdateAddHTLC, // Create a hodlHtlc struct and decide either resolved now or later. htlc := hodlHtlc{ - pd: &lnwallet.PaymentDescriptor{ - EntryType: lnwallet.Add, - ChanID: add.ChanID, - RHash: add.PaymentHash, - Timeout: add.Expiry, - Amount: add.Amount, - HtlcIndex: add.ID, - SourceRef: &sourceRef, - BlindingPoint: add.BlindingPoint, - }, + add: add, + sourceRef: sourceRef, obfuscator: obfuscator, } From aa38041240d6475b6256ef7fa0b9815f788bd85c Mon Sep 17 00:00:00 2001 From: Keagan McClelland Date: Fri, 16 Aug 2024 13:19:36 -0700 Subject: [PATCH 12/18] htlcswitch: remove PaymentDescriptor from processRemoteAdds call signature This is part of a systematic removal of PaymentDescriptor from the mechanics of the htlcswitch package. --- htlcswitch/link.go | 121 +++++++++++++++++++-------------------------- 1 file changed, 52 insertions(+), 69 deletions(-) diff --git a/htlcswitch/link.go b/htlcswitch/link.go index 6d243db0a..551158d85 100644 --- a/htlcswitch/link.go +++ b/htlcswitch/link.go @@ -1009,15 +1009,7 @@ func (l *channelLink) resolveFwdPkg(fwdPkg *channeldb.FwdPkg) error { // shove the entire, original set of adds down the pipeline so that the // batch of adds presented to the sphinx router does not ever change. if !fwdPkg.AckFilter.IsFull() { - adds, err := lnwallet.PayDescsFromRemoteLogUpdates( - fwdPkg.Source, fwdPkg.Height, fwdPkg.Adds, - ) - if err != nil { - l.log.Errorf("unable to process remote log updates: %v", - err) - return err - } - l.processRemoteAdds(fwdPkg, adds) + l.processRemoteAdds(fwdPkg) // If the link failed during processing the adds, we must // return to ensure we won't attempted to update the state @@ -2337,7 +2329,7 @@ func (l *channelLink) handleUpstreamMsg(msg lnwire.Message) { // We now process the message and advance our remote commit // chain. - fwdPkg, adds, settleFails, remoteHTLCs, err := l.channel. + fwdPkg, _, settleFails, remoteHTLCs, err := l.channel. ReceiveRevocation(msg) if err != nil { // TODO(halseth): force close? @@ -2389,7 +2381,7 @@ func (l *channelLink) handleUpstreamMsg(msg lnwire.Message) { } l.processRemoteSettleFails(fwdPkg, settleFails) - l.processRemoteAdds(fwdPkg, adds) + l.processRemoteAdds(fwdPkg) // If the link failed during processing the adds, we must // return to ensure we won't attempted to update the state @@ -3405,32 +3397,29 @@ func (l *channelLink) processRemoteSettleFails(fwdPkg *channeldb.FwdPkg, // indicating whether this is the first time these Adds are being processed, or // whether we are reprocessing as a result of a failure or restart. Adds that // have already been acknowledged in the forwarding package will be ignored. -func (l *channelLink) processRemoteAdds(fwdPkg *channeldb.FwdPkg, - lockedInHtlcs []*lnwallet.PaymentDescriptor) { - +// +//nolint:funlen +func (l *channelLink) processRemoteAdds(fwdPkg *channeldb.FwdPkg) { l.log.Tracef("processing %d remote adds for height %d", - len(lockedInHtlcs), fwdPkg.Height) + len(fwdPkg.Adds), fwdPkg.Height) decodeReqs := make( - []hop.DecodeHopIteratorRequest, 0, len(lockedInHtlcs), + []hop.DecodeHopIteratorRequest, 0, len(fwdPkg.Adds), ) - for _, pd := range lockedInHtlcs { - switch pd.EntryType { - - // TODO(conner): remove type switch? - case lnwallet.Add: + for _, update := range fwdPkg.Adds { + if msg, ok := update.UpdateMsg.(*lnwire.UpdateAddHTLC); ok { // Before adding the new htlc to the state machine, // parse the onion object in order to obtain the // routing information with DecodeHopIterator function // which process the Sphinx packet. - onionReader := bytes.NewReader(pd.OnionBlob[:]) + onionReader := bytes.NewReader(msg.OnionBlob[:]) req := hop.DecodeHopIteratorRequest{ OnionReader: onionReader, - RHash: pd.RHash[:], - IncomingCltv: pd.Timeout, - IncomingAmount: pd.Amount, - BlindingPoint: pd.BlindingPoint, + RHash: msg.PaymentHash[:], + IncomingCltv: msg.Expiry, + IncomingAmount: msg.Amount, + BlindingPoint: msg.BlindingPoint, } decodeReqs = append(decodeReqs, req) @@ -3452,9 +3441,13 @@ func (l *channelLink) processRemoteAdds(fwdPkg *channeldb.FwdPkg, var switchPackets []*htlcPacket - for i, pd := range lockedInHtlcs { + for i, update := range fwdPkg.Adds { idx := uint16(i) + //nolint:forcetypeassert + add := *update.UpdateMsg.(*lnwire.UpdateAddHTLC) + sourceRef := fwdPkg.SourceRef(idx) + if fwdPkg.State == channeldb.FwdStateProcessed && fwdPkg.AckFilter.Contains(idx) { @@ -3480,8 +3473,9 @@ func (l *channelLink) processRemoteAdds(fwdPkg *channeldb.FwdPkg, // If we're unable to process the onion blob then we // should send the malformed htlc error to payment // sender. - l.sendMalformedHTLCError(pd.HtlcIndex, failureCode, - pd.OnionBlob, pd.SourceRef) + l.sendMalformedHTLCError( + add.ID, failureCode, add.OnionBlob, &sourceRef, + ) l.log.Errorf("unable to decode onion hop "+ "iterator: %v", failureCode) @@ -3525,8 +3519,8 @@ func (l *channelLink) processRemoteAdds(fwdPkg *channeldb.FwdPkg, // We can't process this htlc, send back // malformed. l.sendMalformedHTLCError( - pd.HtlcIndex, failureCode, - pd.OnionBlob, pd.SourceRef, + add.ID, failureCode, add.OnionBlob, + &sourceRef, ) continue @@ -3540,11 +3534,8 @@ func (l *channelLink) processRemoteAdds(fwdPkg *channeldb.FwdPkg, // later date failure := lnwire.NewInvalidOnionPayload(failedType, 0) - addMsg := pd.ToLogUpdate().UpdateMsg - //nolint:forcetypeassert - add := *addMsg.(*lnwire.UpdateAddHTLC) l.sendHTLCError( - add, *pd.SourceRef, NewLinkError(failure), + add, sourceRef, NewLinkError(failure), obfuscator, false, ) @@ -3565,8 +3556,8 @@ func (l *channelLink) processRemoteAdds(fwdPkg *channeldb.FwdPkg, // should send the malformed htlc error to payment // sender. l.sendMalformedHTLCError( - pd.HtlcIndex, failureCode, pd.OnionBlob, - pd.SourceRef, + add.ID, failureCode, add.OnionBlob, + &sourceRef, ) l.log.Errorf("unable to decode onion "+ @@ -3585,14 +3576,11 @@ func (l *channelLink) processRemoteAdds(fwdPkg *channeldb.FwdPkg, l.cfg.DisallowRouteBlinding { failure := lnwire.NewInvalidBlinding( - fn.Some(pd.OnionBlob), + fn.Some(add.OnionBlob), ) - addMsg := pd.ToLogUpdate().UpdateMsg - //nolint:forcetypeassert - add := *addMsg.(*lnwire.UpdateAddHTLC) l.sendHTLCError( - add, *pd.SourceRef, NewLinkError(failure), + add, sourceRef, NewLinkError(failure), obfuscator, false, ) @@ -3605,11 +3593,8 @@ func (l *channelLink) processRemoteAdds(fwdPkg *channeldb.FwdPkg, switch fwdInfo.NextHop { case hop.Exit: - addMsg := pd.ToLogUpdate().UpdateMsg - //nolint:forcetypeassert - add := *addMsg.(*lnwire.UpdateAddHTLC) err := l.processExitHop( - add, *pd.SourceRef, obfuscator, fwdInfo, + add, sourceRef, obfuscator, fwdInfo, heightNow, pld, ) if err != nil { @@ -3644,18 +3629,20 @@ func (l *channelLink) processRemoteAdds(fwdPkg *channeldb.FwdPkg, } // Otherwise, it was already processed, we can - // collect it and continue. - addMsg := &lnwire.UpdateAddHTLC{ + // can collect it and continue. + outgoingAdd := &lnwire.UpdateAddHTLC{ Expiry: fwdInfo.OutgoingCTLV, Amount: fwdInfo.AmountToForward, - PaymentHash: pd.RHash, + PaymentHash: add.PaymentHash, BlindingPoint: fwdInfo.NextBlinding, } // Finally, we'll encode the onion packet for // the _next_ hop using the hop iterator // decoded for the current hop. - buf := bytes.NewBuffer(addMsg.OnionBlob[0:0]) + buf := bytes.NewBuffer( + outgoingAdd.OnionBlob[0:0], + ) // We know this cannot fail, as this ADD // was marked forwarded in a previous @@ -3667,18 +3654,18 @@ func (l *channelLink) processRemoteAdds(fwdPkg *channeldb.FwdPkg, //nolint:lll updatePacket := &htlcPacket{ incomingChanID: l.ShortChanID(), - incomingHTLCID: pd.HtlcIndex, + incomingHTLCID: add.ID, outgoingChanID: fwdInfo.NextHop, - sourceRef: pd.SourceRef, - incomingAmount: pd.Amount, - amount: addMsg.Amount, - htlc: addMsg, + sourceRef: &sourceRef, + incomingAmount: add.Amount, + amount: outgoingAdd.Amount, + htlc: outgoingAdd, obfuscator: obfuscator, - incomingTimeout: pd.Timeout, + incomingTimeout: add.Expiry, outgoingTimeout: fwdInfo.OutgoingCTLV, inOnionCustomRecords: pld.CustomRecords(), inboundFee: inboundFee, - inWireCustomRecords: pd.CustomRecords.Copy(), + inWireCustomRecords: add.CustomRecords.Copy(), } switchPackets = append( switchPackets, updatePacket, @@ -3696,7 +3683,7 @@ func (l *channelLink) processRemoteAdds(fwdPkg *channeldb.FwdPkg, addMsg := &lnwire.UpdateAddHTLC{ Expiry: fwdInfo.OutgoingCTLV, Amount: fwdInfo.AmountToForward, - PaymentHash: pd.RHash, + PaymentHash: add.PaymentHash, BlindingPoint: fwdInfo.NextBlinding, } @@ -3717,13 +3704,9 @@ func (l *channelLink) processRemoteAdds(fwdPkg *channeldb.FwdPkg, true, hop.Source, cb, ) - addMsg := pd.ToLogUpdate().UpdateMsg - //nolint:forcetypeassert - add := *addMsg.(*lnwire.UpdateAddHTLC) l.sendHTLCError( - add, *pd.SourceRef, - NewLinkError(failure), obfuscator, - false, + add, sourceRef, NewLinkError(failure), + obfuscator, false, ) continue } @@ -3742,18 +3725,18 @@ func (l *channelLink) processRemoteAdds(fwdPkg *channeldb.FwdPkg, //nolint:lll updatePacket := &htlcPacket{ incomingChanID: l.ShortChanID(), - incomingHTLCID: pd.HtlcIndex, + incomingHTLCID: add.ID, outgoingChanID: fwdInfo.NextHop, - sourceRef: pd.SourceRef, - incomingAmount: pd.Amount, + sourceRef: &sourceRef, + incomingAmount: add.Amount, amount: addMsg.Amount, htlc: addMsg, obfuscator: obfuscator, - incomingTimeout: pd.Timeout, + incomingTimeout: add.Expiry, outgoingTimeout: fwdInfo.OutgoingCTLV, inOnionCustomRecords: pld.CustomRecords(), inboundFee: inboundFee, - inWireCustomRecords: pd.CustomRecords.Copy(), + inWireCustomRecords: add.CustomRecords.Copy(), } fwdPkg.FwdFilter.Set(idx) From 1ae5705954e783c4edc7e3494151b83f04ede245 Mon Sep 17 00:00:00 2001 From: Keagan McClelland Date: Fri, 16 Aug 2024 13:28:44 -0700 Subject: [PATCH 13/18] htlcswitch: remove PaymentDescriptor from processRemoteSettleFails call signature This is part of a systematic removal of PaymentDescriptor from the mechanics of the htlcswitch package. --- htlcswitch/link.go | 53 +++++++++++++++++----------------------------- 1 file changed, 19 insertions(+), 34 deletions(-) diff --git a/htlcswitch/link.go b/htlcswitch/link.go index 551158d85..b8e4038e8 100644 --- a/htlcswitch/link.go +++ b/htlcswitch/link.go @@ -993,15 +993,7 @@ func (l *channelLink) resolveFwdPkg(fwdPkg *channeldb.FwdPkg) error { // If the package is fully acked but not completed, it must still have // settles and fails to propagate. if !fwdPkg.SettleFailFilter.IsFull() { - settleFails, err := lnwallet.PayDescsFromRemoteLogUpdates( - fwdPkg.Source, fwdPkg.Height, fwdPkg.SettleFails, - ) - if err != nil { - l.log.Errorf("unable to process remote log updates: %v", - err) - return err - } - l.processRemoteSettleFails(fwdPkg, settleFails) + l.processRemoteSettleFails(fwdPkg) } // Finally, replay *ALL ADDS* in this forwarding package. The @@ -2329,7 +2321,7 @@ func (l *channelLink) handleUpstreamMsg(msg lnwire.Message) { // We now process the message and advance our remote commit // chain. - fwdPkg, _, settleFails, remoteHTLCs, err := l.channel. + fwdPkg, _, _, remoteHTLCs, err := l.channel. ReceiveRevocation(msg) if err != nil { // TODO(halseth): force close? @@ -2380,7 +2372,7 @@ func (l *channelLink) handleUpstreamMsg(msg lnwire.Message) { } } - l.processRemoteSettleFails(fwdPkg, settleFails) + l.processRemoteSettleFails(fwdPkg) l.processRemoteAdds(fwdPkg) // If the link failed during processing the adds, we must @@ -3287,17 +3279,17 @@ func (l *channelLink) updateChannelFee(feePerKw chainfee.SatPerKWeight) error { // the context of the provided forwarding package. Any settles or fails that // have already been acknowledged in the forwarding package will not be sent to // the switch. -func (l *channelLink) processRemoteSettleFails(fwdPkg *channeldb.FwdPkg, - settleFails []*lnwallet.PaymentDescriptor) { - - if len(settleFails) == 0 { +func (l *channelLink) processRemoteSettleFails(fwdPkg *channeldb.FwdPkg) { + if len(fwdPkg.SettleFails) == 0 { return } l.log.Debugf("settle-fail-filter: %v", fwdPkg.SettleFailFilter) var switchPackets []*htlcPacket - for i, pd := range settleFails { + for i, update := range fwdPkg.SettleFails { + destRef := fwdPkg.DestRef(uint16(i)) + // Skip any settles or fails that have already been // acknowledged by the incoming link that originated the // forwarded Add. @@ -3308,12 +3300,11 @@ func (l *channelLink) processRemoteSettleFails(fwdPkg *channeldb.FwdPkg, // TODO(roasbeef): rework log entries to a shared // interface. - switch pd.EntryType { - + switch msg := update.UpdateMsg.(type) { // A settle for an HTLC we previously forwarded HTLC has been // received. So we'll forward the HTLC to the switch which will // handle propagating the settle to the prior hop. - case lnwallet.Settle: + case *lnwire.UpdateFulfillHTLC: // If hodl.SettleIncoming is requested, we will not // forward the SETTLE to the switch and will not signal // a free slot on the commitment transaction. @@ -3324,11 +3315,9 @@ func (l *channelLink) processRemoteSettleFails(fwdPkg *channeldb.FwdPkg, settlePacket := &htlcPacket{ outgoingChanID: l.ShortChanID(), - outgoingHTLCID: pd.ParentIndex, - destRef: pd.DestRef, - htlc: &lnwire.UpdateFulfillHTLC{ - PaymentPreimage: pd.RPreimage, - }, + outgoingHTLCID: msg.ID, + destRef: &destRef, + htlc: msg, } // Add the packet to the batch to be forwarded, and @@ -3340,7 +3329,7 @@ func (l *channelLink) processRemoteSettleFails(fwdPkg *channeldb.FwdPkg, // been received. As a result a new slot will be freed up in // our commitment state, so we'll forward this to the switch so // the backwards undo can continue. - case lnwallet.Fail: + case *lnwire.UpdateFailHTLC: // If hodl.SettleIncoming is requested, we will not // forward the FAIL to the switch and will not signal a // free slot on the commitment transaction. @@ -3355,16 +3344,12 @@ func (l *channelLink) processRemoteSettleFails(fwdPkg *channeldb.FwdPkg, // set on the packet. failPacket := &htlcPacket{ outgoingChanID: l.ShortChanID(), - outgoingHTLCID: pd.ParentIndex, - destRef: pd.DestRef, - htlc: &lnwire.UpdateFailHTLC{ - Reason: lnwire.OpaqueReason( - pd.FailReason, - ), - }, + outgoingHTLCID: msg.ID, + destRef: &destRef, + htlc: msg, } - l.log.Debugf("Failed to send %s", pd.Amount) + l.log.Debugf("Failed to send HTLC with ID=%d", msg.ID) // If the failure message lacks an HMAC (but includes // the 4 bytes for encoding the message and padding @@ -3374,7 +3359,7 @@ func (l *channelLink) processRemoteSettleFails(fwdPkg *channeldb.FwdPkg, // to an actual error, by encrypting it as if we were // the originating hop. convertedErrorSize := lnwire.FailureMessageLength + 4 - if len(pd.FailReason) == convertedErrorSize { + if len(msg.Reason) == convertedErrorSize { failPacket.convertedError = true } From 957557a9378cdc7b525d0eeec70a87e60e4e1759 Mon Sep 17 00:00:00 2001 From: Keagan McClelland Date: Fri, 16 Aug 2024 14:35:49 -0700 Subject: [PATCH 14/18] htlcswitch+lnwallet: remove PaymentDescriptor from ReceiveRevocation returns This is part of a systematic removal of PaymentDescriptor from the public API of the lnwallet package. This marks the last change needed before we make the PaymentDescriptor structure private. --- htlcswitch/link.go | 3 +- htlcswitch/link_isolated_test.go | 2 +- htlcswitch/link_test.go | 6 +- lnwallet/channel.go | 67 ++++++-------- lnwallet/channel_test.go | 150 +++++++++++++++++-------------- lnwallet/test_utils.go | 4 +- lnwallet/transactions_test.go | 2 +- 7 files changed, 120 insertions(+), 114 deletions(-) diff --git a/htlcswitch/link.go b/htlcswitch/link.go index b8e4038e8..83495f357 100644 --- a/htlcswitch/link.go +++ b/htlcswitch/link.go @@ -2321,8 +2321,7 @@ func (l *channelLink) handleUpstreamMsg(msg lnwire.Message) { // We now process the message and advance our remote commit // chain. - fwdPkg, _, _, remoteHTLCs, err := l.channel. - ReceiveRevocation(msg) + fwdPkg, remoteHTLCs, err := l.channel.ReceiveRevocation(msg) if err != nil { // TODO(halseth): force close? l.failf( diff --git a/htlcswitch/link_isolated_test.go b/htlcswitch/link_isolated_test.go index 89d0ef319..5d769d920 100644 --- a/htlcswitch/link_isolated_test.go +++ b/htlcswitch/link_isolated_test.go @@ -129,7 +129,7 @@ func (l *linkTestContext) receiveRevAndAckAliceToBob() { l.t.Fatalf("expected RevokeAndAck, got %T", msg) } - _, _, _, _, err := l.bobChannel.ReceiveRevocation(rev) + _, _, err := l.bobChannel.ReceiveRevocation(rev) if err != nil { l.t.Fatalf("bob failed receiving revocation: %v", err) } diff --git a/htlcswitch/link_test.go b/htlcswitch/link_test.go index 60010d6ff..53a084209 100644 --- a/htlcswitch/link_test.go +++ b/htlcswitch/link_test.go @@ -2335,7 +2335,7 @@ func handleStateUpdate(link *channelLink, if !ok { return fmt.Errorf("expected RevokeAndAck got %T", msg) } - _, _, _, _, err = remoteChannel.ReceiveRevocation(revoke) + _, _, err = remoteChannel.ReceiveRevocation(revoke) if err != nil { return fmt.Errorf("unable to receive "+ "revocation: %v", err) @@ -2389,7 +2389,7 @@ func updateState(batchTick chan time.Time, link *channelLink, return fmt.Errorf("expected RevokeAndAck got %T", msg) } - _, _, _, _, err = remoteChannel.ReceiveRevocation(revoke) + _, _, err = remoteChannel.ReceiveRevocation(revoke) if err != nil { return fmt.Errorf("unable to receive "+ "revocation: %v", err) @@ -3643,7 +3643,7 @@ func TestChannelLinkTrimCircuitsRemoteCommit(t *testing.T) { rev, _, _, err := harness.bobChannel.RevokeCurrentCommitment() require.NoError(t, err, "unable to revoke current commitment") - _, _, _, _, err = alice.channel.ReceiveRevocation(rev) + _, _, err = alice.channel.ReceiveRevocation(rev) require.NoError(t, err, "unable to receive revocation") // Restart Alice's link, which simulates a disconnection with the remote diff --git a/lnwallet/channel.go b/lnwallet/channel.go index 205b339a2..6d9837229 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -5375,15 +5375,10 @@ func (lc *LightningChannel) RevokeCurrentCommitment() (*lnwire.RevokeAndAck, // The returned values correspond to: // 1. The forwarding package corresponding to the remote commitment height // that was revoked. -// 2. The PaymentDescriptor of any Add HTLCs that were locked in by this -// revocation. -// 3. The PaymentDescriptor of any Settle/Fail HTLCs that were locked in by -// this revocation. -// 4. The set of HTLCs present on the current valid commitment transaction +// 2. The set of HTLCs present on the current valid commitment transaction // for the remote party. func (lc *LightningChannel) ReceiveRevocation(revMsg *lnwire.RevokeAndAck) ( - *channeldb.FwdPkg, []*PaymentDescriptor, []*PaymentDescriptor, - []channeldb.HTLC, error) { + *channeldb.FwdPkg, []channeldb.HTLC, error) { lc.Lock() defer lc.Unlock() @@ -5392,10 +5387,10 @@ func (lc *LightningChannel) ReceiveRevocation(revMsg *lnwire.RevokeAndAck) ( store := lc.channelState.RevocationStore revocation, err := chainhash.NewHash(revMsg.Revocation[:]) if err != nil { - return nil, nil, nil, nil, err + return nil, nil, err } if err := store.AddNextEntry(revocation); err != nil { - return nil, nil, nil, nil, err + return nil, nil, err } // Verify that if we use the commitment point computed based off of the @@ -5404,7 +5399,7 @@ func (lc *LightningChannel) ReceiveRevocation(revMsg *lnwire.RevokeAndAck) ( currentCommitPoint := lc.channelState.RemoteCurrentRevocation derivedCommitPoint := input.ComputeCommitmentPoint(revMsg.Revocation[:]) if !derivedCommitPoint.IsEqual(currentCommitPoint) { - return nil, nil, nil, nil, fmt.Errorf("revocation key mismatch") + return nil, nil, fmt.Errorf("revocation key mismatch") } // Now that we've verified that the prior commitment has been properly @@ -5434,10 +5429,8 @@ func (lc *LightningChannel) ReceiveRevocation(revMsg *lnwire.RevokeAndAck) ( // updates to disk and optimistically buffer the forwarding package in // memory. var ( - addsToForward []*PaymentDescriptor - addUpdates []channeldb.LogUpdate - settleFailsToForward []*PaymentDescriptor - settleFailUpdates []channeldb.LogUpdate + addUpdatesToForward []channeldb.LogUpdate + settleFailUpdatesToForward []channeldb.LogUpdate ) var addIndex, settleFailIndex uint16 @@ -5489,7 +5482,13 @@ func (lc *LightningChannel) ReceiveRevocation(revMsg *lnwire.RevokeAndAck) ( addIndex++ pd.isForwarded = true - addsToForward = append(addsToForward, pd) + + // At this point we put the update into our list of + // updates that we will eventually put into the + // FwdPkg at this height. + addUpdatesToForward = append( + addUpdatesToForward, pd.ToLogUpdate(), + ) case pd.EntryType != Add && committedRmv && shouldFwdRmv: // Construct a reference specifying the location that @@ -5503,28 +5502,17 @@ func (lc *LightningChannel) ReceiveRevocation(revMsg *lnwire.RevokeAndAck) ( settleFailIndex++ pd.isForwarded = true - settleFailsToForward = append(settleFailsToForward, pd) + + // At this point we put the update into our list of + // updates that we will eventually put into the + // FwdPkg at this height. + settleFailUpdatesToForward = append( + settleFailUpdatesToForward, pd.ToLogUpdate(), + ) default: continue } - - // If we've reached this point, this HTLC will be added to the - // forwarding package at the height of the remote commitment. - // We'll map the type of the PaymentDescriptor to one of the - // four messages that it corresponds to and separate the - // updates into Adds and Settle/Fail/MalformedFail such that - // they can be written in the forwarding package. Adds are - // aggregated separately from the other types of HTLCs. - switch pd.EntryType { - case Add: - addUpdates = append(addUpdates, pd.ToLogUpdate()) - - case Settle, Fail, MalformedFail: - settleFailUpdates = append( - settleFailUpdates, pd.ToLogUpdate(), - ) - } } // We use the remote commitment chain's tip as it will soon become the tail @@ -5540,7 +5528,8 @@ func (lc *LightningChannel) ReceiveRevocation(revMsg *lnwire.RevokeAndAck) ( // type, construct a forwarding package using the height that the remote // commitment chain will be extended after persisting the revocation. fwdPkg := channeldb.NewFwdPkg( - source, remoteChainTail, addUpdates, settleFailUpdates, + source, remoteChainTail, addUpdatesToForward, + settleFailUpdatesToForward, ) // We will soon be saving the current remote commitment to revocation @@ -5553,7 +5542,7 @@ func (lc *LightningChannel) ReceiveRevocation(revMsg *lnwire.RevokeAndAck) ( revocation, lc.channelState, lc.leafStore, ) if err != nil { - return nil, nil, nil, nil, err + return nil, nil, err } // Now that we have a new verification nonce from them, we can refresh @@ -5561,7 +5550,7 @@ func (lc *LightningChannel) ReceiveRevocation(revMsg *lnwire.RevokeAndAck) ( if lc.channelState.ChanType.IsTaproot() { localNonce, err := revMsg.LocalNonce.UnwrapOrErrV(errNoNonce) if err != nil { - return nil, nil, nil, nil, err + return nil, nil, err } session, err := lc.musigSessions.RemoteSession.Refresh( @@ -5570,7 +5559,7 @@ func (lc *LightningChannel) ReceiveRevocation(revMsg *lnwire.RevokeAndAck) ( }, ) if err != nil { - return nil, nil, nil, nil, err + return nil, nil, err } lc.musigSessions.RemoteSession = session @@ -5586,7 +5575,7 @@ func (lc *LightningChannel) ReceiveRevocation(revMsg *lnwire.RevokeAndAck) ( ourOutputIndex, theirOutputIndex, ) if err != nil { - return nil, nil, nil, nil, err + return nil, nil, err } // Since they revoked the current lowest height in their commitment @@ -5603,7 +5592,7 @@ func (lc *LightningChannel) ReceiveRevocation(revMsg *lnwire.RevokeAndAck) ( remoteHTLCs := lc.channelState.RemoteCommitment.Htlcs - return fwdPkg, addsToForward, settleFailsToForward, remoteHTLCs, nil + return fwdPkg, remoteHTLCs, nil } // LoadFwdPkgs loads any pending log updates from disk and returns the payment diff --git a/lnwallet/channel_test.go b/lnwallet/channel_test.go index e28098052..0af74dd1d 100644 --- a/lnwallet/channel_test.go +++ b/lnwallet/channel_test.go @@ -132,7 +132,7 @@ func testAddSettleWorkflow(t *testing.T, tweakless bool, // Alice then processes this revocation, sending her own revocation for // her prior commitment transaction. Alice shouldn't have any HTLCs to // forward since she's sending an outgoing HTLC. - fwdPkg, _, _, _, err := aliceChannel.ReceiveRevocation(bobRevocation) + fwdPkg, _, err := aliceChannel.ReceiveRevocation(bobRevocation) require.NoError(t, err, "alice unable to process bob's revocation") if len(fwdPkg.Adds) != 0 { t.Fatalf("alice forwards %v add htlcs, should forward none", @@ -157,7 +157,7 @@ func testAddSettleWorkflow(t *testing.T, tweakless bool, // is fully locked in within both commitment transactions. Bob should // also be able to forward an HTLC now that the HTLC has been locked // into both commitment transactions. - fwdPkg, _, _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) + fwdPkg, _, err = bobChannel.ReceiveRevocation(aliceRevocation) require.NoError(t, err, "bob unable to process alice's revocation") if len(fwdPkg.Adds) != 1 { t.Fatalf("bob forwards %v add htlcs, should only forward one", @@ -249,7 +249,7 @@ func testAddSettleWorkflow(t *testing.T, tweakless bool, aliceNewCommit, err = aliceChannel.SignNextCommitment() require.NoError(t, err, "alice unable to sign new commitment") - fwdPkg, _, _, _, err = bobChannel.ReceiveRevocation(aliceRevocation2) + fwdPkg, _, err = bobChannel.ReceiveRevocation(aliceRevocation2) require.NoError(t, err, "bob unable to process alice's revocation") if len(fwdPkg.Adds) != 0 { t.Fatalf("bob forwards %v add htlcs, should forward none", @@ -286,7 +286,7 @@ func testAddSettleWorkflow(t *testing.T, tweakless bool, } } - fwdPkg, _, _, _, err = aliceChannel.ReceiveRevocation(bobRevocation2) + fwdPkg, _, err = aliceChannel.ReceiveRevocation(bobRevocation2) require.NoError(t, err, "alice unable to process bob's revocation") if len(fwdPkg.Adds) != 0 { // Alice should now be able to forward the settlement HTLC to @@ -468,7 +468,7 @@ func TestChannelZeroAddLocalHeight(t *testing.T) { aliceRevocation, _, _, err := aliceChannel.RevokeCurrentCommitment() require.NoError(t, err) - _, _, _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) + _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) require.NoError(t, err) // We now restore Alice's channel as this was the point at which @@ -644,7 +644,7 @@ func testCommitHTLCSigTieBreak(t *testing.T, restart bool) { bobRevocation, _, _, err := bobChannel.RevokeCurrentCommitment() require.NoError(t, err, "unable to revoke bob's commitment") - _, _, _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) + _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) require.NoError(t, err, "unable to receive bob's revocation") // Now have Bob initiate the second half of the commitment dance. Here @@ -2556,7 +2556,7 @@ func TestUpdateFeeSenderCommits(t *testing.T) { // Alice receives the revocation of the old one, and can now assume // that Bob's received everything up to the signature she sent, // including the HTLC and fee update. - _, _, _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) + _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) require.NoError(t, err, "alice unable to process bob's revocation") // Alice receives new signature from Bob, and assumes this covers the @@ -2584,7 +2584,7 @@ func TestUpdateFeeSenderCommits(t *testing.T) { } // Bob receives revocation from Alice. - _, _, _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) + _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) require.NoError(t, err, "bob unable to process alice's revocation") } @@ -2644,7 +2644,7 @@ func TestUpdateFeeReceiverCommits(t *testing.T) { require.NoError(t, err, "unable to generate bob revocation") // Bob receives the revocation of the old commitment - _, _, _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) + _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) require.NoError(t, err, "alice unable to process bob's revocation") // Alice will sign next commitment. Since she sent the revocation, she @@ -2685,7 +2685,7 @@ func TestUpdateFeeReceiverCommits(t *testing.T) { // Alice receives revocation from Bob, and can now be sure that Bob // received the two updates, and they are considered locked in. - _, _, _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) + _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) require.NoError(t, err, "bob unable to process alice's revocation") // Alice will receive the signature from Bob, which will cover what was @@ -2713,7 +2713,7 @@ func TestUpdateFeeReceiverCommits(t *testing.T) { } // Bob receives revocation from Alice. - _, _, _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) + _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) require.NoError(t, err, "bob unable to process alice's revocation") } @@ -2822,7 +2822,7 @@ func TestUpdateFeeMultipleUpdates(t *testing.T) { // Alice receives the revocation of the old one, and can now assume that // Bob's received everything up to the signature she sent, including the // HTLC and fee update. - _, _, _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) + _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) require.NoError(t, err, "alice unable to process bob's revocation") // Alice receives new signature from Bob, and assumes this covers the @@ -2852,7 +2852,7 @@ func TestUpdateFeeMultipleUpdates(t *testing.T) { } // Bob receives revocation from Alice. - _, _, _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) + _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) require.NoError(t, err, "bob unable to process alice's revocation") } @@ -3267,13 +3267,13 @@ func testChanSyncOweCommitment(t *testing.T, chanType channeldb.ChannelType) { require.NoError(t, err, "unable to revoke bob commitment") bobNewCommit, err := bobChannel.SignNextCommitment() require.NoError(t, err, "bob unable to sign commitment") - _, _, _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) + _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) require.NoError(t, err, "alice unable to recv revocation") err = aliceChannel.ReceiveNewCommitment(bobNewCommit.CommitSigs) require.NoError(t, err, "alice unable to rev bob's commitment") aliceRevocation, _, _, err := aliceChannel.RevokeCurrentCommitment() require.NoError(t, err, "alice unable to revoke commitment") - _, _, _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) + _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) require.NoError(t, err, "bob unable to recv revocation") // At this point, we'll now assert that their log states are what we @@ -3466,7 +3466,7 @@ func testChanSyncOweCommitmentPendingRemote(t *testing.T, t.Fatalf("unable to revoke commitment: %v", err) } - _, _, _, _, err = aliceChannel.ReceiveRevocation(bobRevoke) + _, _, err = aliceChannel.ReceiveRevocation(bobRevoke) if err != nil { t.Fatalf("unable to revoke commitment: %v", err) } @@ -3502,7 +3502,7 @@ func testChanSyncOweCommitmentPendingRemote(t *testing.T, if err != nil { t.Fatal(err) } - _, _, _, _, err = bobChannel.ReceiveRevocation(aliceRevoke) + _, _, err = bobChannel.ReceiveRevocation(aliceRevoke) if err != nil { t.Fatal(err) } @@ -3603,7 +3603,7 @@ func testChanSyncOweRevocation(t *testing.T, chanType channeldb.ChannelType) { bobNewCommit, err := bobChannel.SignNextCommitment() require.NoError(t, err, "bob unable to sign commitment") - _, _, _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) + _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) require.NoError(t, err, "alice unable to recv revocation") err = aliceChannel.ReceiveNewCommitment(bobNewCommit.CommitSigs) require.NoError(t, err, "alice unable to rev bob's commitment") @@ -3698,7 +3698,7 @@ func testChanSyncOweRevocation(t *testing.T, chanType channeldb.ChannelType) { // We'll continue by then allowing bob to process Alice's revocation // message. - _, _, _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) + _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) require.NoError(t, err, "bob unable to recv revocation") // Finally, Alice will add an HTLC over her own such that we assert the @@ -3916,13 +3916,13 @@ func testChanSyncOweRevocationAndCommit(t *testing.T, // We'll now finish the state transition by having Alice process both // messages, and send her final revocation. - _, _, _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) + _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) require.NoError(t, err, "alice unable to recv revocation") err = aliceChannel.ReceiveNewCommitment(bobNewCommit.CommitSigs) require.NoError(t, err, "alice unable to recv bob's commitment") aliceRevocation, _, _, err := aliceChannel.RevokeCurrentCommitment() require.NoError(t, err, "alice unable to revoke commitment") - _, _, _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) + _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) require.NoError(t, err, "bob unable to recv revocation") } @@ -4016,7 +4016,7 @@ func testChanSyncOweRevocationAndCommitForceTransition(t *testing.T, // local commit chain getting height > remote commit chain. aliceRevocation, _, _, err := aliceChannel.RevokeCurrentCommitment() require.NoError(t, err, "alice unable to revoke commitment") - _, _, _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) + _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) require.NoError(t, err, "bob unable to recv revocation") // Next, Alice will settle that incoming HTLC, then we'll start the @@ -4148,7 +4148,7 @@ func testChanSyncOweRevocationAndCommitForceTransition(t *testing.T, // Now, we'll continue the exchange, sending Bob's revocation and // signature message to Alice, ending with Alice sending her revocation // message to Bob. - _, _, _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) + _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) require.NoError(t, err, "alice unable to recv revocation") err = aliceChannel.ReceiveNewCommitment(&CommitSigs{ CommitSig: bobSigMsg.CommitSig, @@ -4158,7 +4158,7 @@ func testChanSyncOweRevocationAndCommitForceTransition(t *testing.T, require.NoError(t, err, "alice unable to rev bob's commitment") aliceRevocation, _, _, err = aliceChannel.RevokeCurrentCommitment() require.NoError(t, err, "alice unable to revoke commitment") - _, _, _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) + _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) require.NoError(t, err, "bob unable to recv revocation") } @@ -4584,13 +4584,13 @@ func TestChannelRetransmissionFeeUpdate(t *testing.T) { require.NoError(t, err, "unable to revoke bob commitment") bobNewCommit, err := bobChannel.SignNextCommitment() require.NoError(t, err, "bob unable to sign commitment") - _, _, _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) + _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) require.NoError(t, err, "alice unable to recv revocation") err = aliceChannel.ReceiveNewCommitment(bobNewCommit.CommitSigs) require.NoError(t, err, "alice unable to rev bob's commitment") aliceRevocation, _, _, err := aliceChannel.RevokeCurrentCommitment() require.NoError(t, err, "alice unable to revoke commitment") - _, _, _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) + _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) require.NoError(t, err, "bob unable to recv revocation") // Both parties should now have the latest fee rate locked-in. @@ -4782,13 +4782,13 @@ func TestFeeUpdateOldDiskFormat(t *testing.T) { require.NoError(t, err, "unable to revoke bob commitment") bobNewCommitSigs, err := bobChannel.SignNextCommitment() require.NoError(t, err, "bob unable to sign commitment") - _, _, _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) + _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) require.NoError(t, err, "alice unable to recv revocation") err = aliceChannel.ReceiveNewCommitment(bobNewCommitSigs.CommitSigs) require.NoError(t, err, "alice unable to rev bob's commitment") aliceRevocation, _, _, err := aliceChannel.RevokeCurrentCommitment() require.NoError(t, err, "alice unable to revoke commitment") - _, _, _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) + _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) require.NoError(t, err, "bob unable to recv revocation") // Both parties should now have the latest fee rate locked-in. @@ -5497,7 +5497,7 @@ func TestLockedInHtlcForwardingSkipAfterRestart(t *testing.T) { } // Alice should detect that she doesn't need to forward any HTLC's. - fwdPkg, _, _, _, err := aliceChannel.ReceiveRevocation(bobRevocation) + fwdPkg, _, err := aliceChannel.ReceiveRevocation(bobRevocation) if err != nil { t.Fatal(err) } @@ -5528,7 +5528,7 @@ func TestLockedInHtlcForwardingSkipAfterRestart(t *testing.T) { // Bob should now detect that he now has 2 incoming HTLC's that he can // forward along. - fwdPkg, _, _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) + fwdPkg, _, err = bobChannel.ReceiveRevocation(aliceRevocation) if err != nil { t.Fatal(err) } @@ -5573,7 +5573,7 @@ func TestLockedInHtlcForwardingSkipAfterRestart(t *testing.T) { // At this point, Bob receives the revocation from Alice, which is now // his signal to examine all the HTLC's that have been locked in to // process. - fwdPkg, _, _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) + fwdPkg, _, err = bobChannel.ReceiveRevocation(aliceRevocation) if err != nil { t.Fatal(err) } @@ -5622,22 +5622,31 @@ func TestLockedInHtlcForwardingSkipAfterRestart(t *testing.T) { // Alice should detect that she doesn't need to forward any Adds's, but // that the Fail has been locked in an can be forwarded. - _, adds, settleFails, _, err := aliceChannel.ReceiveRevocation(bobRevocation) + fwdPkg, _, err = aliceChannel.ReceiveRevocation(bobRevocation) if err != nil { t.Fatal(err) } + + adds := fwdPkg.Adds + settleFails := fwdPkg.SettleFails if len(adds) != 0 { t.Fatalf("alice shouldn't forward any HTLC's, instead wants to "+ - "forward %v htlcs", len(adds)) + "forward %v htlcs", len(fwdPkg.Adds)) } if len(settleFails) != 1 { t.Fatalf("alice should only forward %d HTLC's, instead wants to "+ - "forward %v htlcs", 1, len(settleFails)) + "forward %v htlcs", 1, len(fwdPkg.SettleFails)) } - if settleFails[0].ParentIndex != htlc.ID { + + fail, ok := settleFails[0].UpdateMsg.(*lnwire.UpdateFailHTLC) + if !ok { + t.Fatalf("expected UpdateFailHTLC, got %T", + settleFails[0].UpdateMsg) + } + if fail.ID != htlc.ID { t.Fatalf("alice should forward fail for htlcid=%d, instead "+ "forwarding id=%d", htlc.ID, - settleFails[0].ParentIndex) + fail.ID) } // We'll now restart both Alice and Bob. This emulates a reconnection @@ -5671,7 +5680,7 @@ func TestLockedInHtlcForwardingSkipAfterRestart(t *testing.T) { // Alice should detect that she doesn't need to forward any HTLC's, as // the updates haven't been committed by Bob yet. - fwdPkg, _, _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) + fwdPkg, _, err = aliceChannel.ReceiveRevocation(bobRevocation) if err != nil { t.Fatal(err) } @@ -5702,7 +5711,7 @@ func TestLockedInHtlcForwardingSkipAfterRestart(t *testing.T) { // Bob should detect that he has nothing to forward, as he hasn't // received any HTLCs. - fwdPkg, _, _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) + fwdPkg, _, err = bobChannel.ReceiveRevocation(aliceRevocation) if err != nil { t.Fatal(err) } @@ -5732,10 +5741,13 @@ func TestLockedInHtlcForwardingSkipAfterRestart(t *testing.T) { // When Alice receives the revocation, she should detect that she // can now forward the freshly locked-in Fail. - _, adds, settleFails, _, err = aliceChannel.ReceiveRevocation(bobRevocation) + fwdPkg, _, err = aliceChannel.ReceiveRevocation(bobRevocation) if err != nil { t.Fatal(err) } + + adds = fwdPkg.Adds + settleFails = fwdPkg.SettleFails if len(adds) != 0 { t.Fatalf("alice shouldn't forward any HTLC's, instead wants to "+ "forward %v htlcs", len(adds)) @@ -5744,10 +5756,16 @@ func TestLockedInHtlcForwardingSkipAfterRestart(t *testing.T) { t.Fatalf("alice should only forward one HTLC, instead wants to "+ "forward %v htlcs", len(settleFails)) } - if settleFails[0].ParentIndex != htlc2.ID { + + fail, ok = settleFails[0].UpdateMsg.(*lnwire.UpdateFailHTLC) + if !ok { + t.Fatalf("expected UpdateFailHTLC, got %T", + settleFails[0].UpdateMsg) + } + if fail.ID != htlc2.ID { t.Fatalf("alice should forward fail for htlcid=%d, instead "+ "forwarding id=%d", htlc2.ID, - settleFails[0].ParentIndex) + fail.ID) } } @@ -6347,13 +6365,13 @@ func TestMaxAsynchronousHtlcs(t *testing.T) { bobRevocation, _, _, err := bobChannel.RevokeCurrentCommitment() require.NoError(t, err, "unable to revoke revocation") - _, _, _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) + _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) require.NoError(t, err, "unable to receive revocation") aliceRevocation, _, _, err := aliceChannel.RevokeCurrentCommitment() require.NoError(t, err, "unable to revoke revocation") - _, _, _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) + _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) require.NoError(t, err, "unable to receive revocation") // Send the final Add which should succeed as in step 6. @@ -7035,7 +7053,7 @@ func TestChannelRestoreUpdateLogs(t *testing.T) { // sent. However her local commitment chain still won't include the // state with the HTLC, since she hasn't received a new commitment // signature from Bob yet. - _, _, _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) + _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) require.NoError(t, err, "unable to receive revocation") // Now make Alice send and sign an additional HTLC. We don't let Bob @@ -7211,7 +7229,7 @@ func TestChannelRestoreUpdateLogsFailedHTLC(t *testing.T) { aliceRevocation, _, _, err := aliceChannel.RevokeCurrentCommitment() require.NoError(t, err, "unable to revoke commitment") - _, _, _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) + _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) require.NoError(t, err, "bob unable to process alice's revocation") // At this point Alice has advanced her local commitment chain to a @@ -7241,7 +7259,7 @@ func TestChannelRestoreUpdateLogsFailedHTLC(t *testing.T) { // the corresponding Fail from the local update log. bobRevocation, _, _, err := bobChannel.RevokeCurrentCommitment() require.NoError(t, err, "unable to revoke commitment") - _, _, _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) + _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) require.NoError(t, err, "unable to receive revocation") assertInLogs(t, aliceChannel, 0, 0, 0, 0) @@ -7466,7 +7484,7 @@ func TestChannelRestoreCommitHeight(t *testing.T) { bobChannel = restoreAndAssertCommitHeights(t, bobChannel, true, 0, 1, 0) // Alice receives the revocation, ACKing her pending commitment. - _, _, _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) + _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) require.NoError(t, err, "unable to receive revocation") // However, the HTLC is still not locked into her local commitment, so @@ -7495,7 +7513,7 @@ func TestChannelRestoreCommitHeight(t *testing.T) { t, aliceChannel, false, 0, 1, 1, ) - _, _, _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) + _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) require.NoError(t, err, "unable to receive revocation") // Alice ACKing Bob's pending commitment shouldn't change the heights @@ -7540,7 +7558,7 @@ func TestChannelRestoreCommitHeight(t *testing.T) { bobChannel = restoreAndAssertCommitHeights(t, bobChannel, true, 1, 2, 0) // Alice receives the revocation, ACKing her pending commitment for Bob. - _, _, _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) + _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) require.NoError(t, err, "unable to receive revocation") // Alice receiving Bob's revocation should bump both addCommitHeightRemote @@ -7578,7 +7596,7 @@ func TestChannelRestoreCommitHeight(t *testing.T) { // Bob receives the revocation, which should set both addCommitHeightRemote // fields to 2. - _, _, _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) + _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) require.NoError(t, err, "unable to receive revocation") bobChannel = restoreAndAssertCommitHeights(t, bobChannel, true, 0, 2, 2) @@ -7681,7 +7699,7 @@ func TestForceCloseBorkedState(t *testing.T) { // At this point, all channel mutating methods should now fail as they // shouldn't be able to proceed if the channel is borked. - _, _, _, _, err = aliceChannel.ReceiveRevocation(revokeMsg) + _, _, err = aliceChannel.ReceiveRevocation(revokeMsg) if err != channeldb.ErrChanBorked { t.Fatalf("advance commitment tail should have failed") } @@ -9477,7 +9495,7 @@ func TestChannelUnsignedAckedFailure(t *testing.T) { // -----rev-----> aliceRevocation, _, _, err := aliceChannel.RevokeCurrentCommitment() require.NoError(t, err) - _, _, _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) + _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) require.NoError(t, err) // Alice should sign the next commitment and go down before @@ -9502,7 +9520,7 @@ func TestChannelUnsignedAckedFailure(t *testing.T) { // <----rev------ bobRevocation, _, _, err := bobChannel.RevokeCurrentCommitment() require.NoError(t, err) - _, _, _, _, err = newAliceChannel.ReceiveRevocation(bobRevocation) + _, _, err = newAliceChannel.ReceiveRevocation(bobRevocation) require.NoError(t, err) // Now Bob sends an HTLC to Alice. @@ -9588,7 +9606,7 @@ func TestChannelLocalUnsignedUpdatesFailure(t *testing.T) { // <----rev----- bobRevocation, _, _, err := bobChannel.RevokeCurrentCommitment() require.NoError(t, err) - _, _, _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) + _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) require.NoError(t, err) // Restart Alice and assert that she can receive Bob's next commitment @@ -9672,7 +9690,7 @@ func TestChannelSignedAckRegression(t *testing.T) { // <----rev----- bobRevocation, _, _, err := bobChannel.RevokeCurrentCommitment() require.NoError(t, err) - _, _, _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) + _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) require.NoError(t, err) // <----sig----- @@ -9699,7 +9717,7 @@ func TestChannelSignedAckRegression(t *testing.T) { // <----rev----- bobRevocation, _, _, err = bobChannel.RevokeCurrentCommitment() require.NoError(t, err) - _, _, _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) + _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) require.NoError(t, err) // Restart Bob's channel state here. @@ -9712,7 +9730,7 @@ func TestChannelSignedAckRegression(t *testing.T) { // -----rev----> aliceRevocation, _, _, err := aliceChannel.RevokeCurrentCommitment() require.NoError(t, err) - fwdPkg, _, _, _, err := newBobChannel.ReceiveRevocation(aliceRevocation) + fwdPkg, _, err := newBobChannel.ReceiveRevocation(aliceRevocation) require.NoError(t, err) // Assert that the fwdpkg is not empty. @@ -9804,7 +9822,7 @@ func TestIsChannelClean(t *testing.T) { // <---rev--- bobRevocation, _, _, err := bobChannel.RevokeCurrentCommitment() require.NoError(t, err) - _, _, _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) + _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) require.NoError(t, err) assertCleanOrDirty(false, aliceChannel, bobChannel, t) @@ -9818,7 +9836,7 @@ func TestIsChannelClean(t *testing.T) { // ---rev---> aliceRevocation, _, _, err := aliceChannel.RevokeCurrentCommitment() require.NoError(t, err) - _, _, _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) + _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) require.NoError(t, err) assertCleanOrDirty(false, aliceChannel, bobChannel, t) @@ -9839,7 +9857,7 @@ func TestIsChannelClean(t *testing.T) { // ---rev---> aliceRevocation, _, _, err = aliceChannel.RevokeCurrentCommitment() require.NoError(t, err) - _, _, _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) + _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) require.NoError(t, err) assertCleanOrDirty(false, aliceChannel, bobChannel, t) @@ -9853,7 +9871,7 @@ func TestIsChannelClean(t *testing.T) { // <---rev--- bobRevocation, _, _, err = bobChannel.RevokeCurrentCommitment() require.NoError(t, err) - _, _, _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) + _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) require.NoError(t, err) assertCleanOrDirty(true, aliceChannel, bobChannel, t) @@ -9877,7 +9895,7 @@ func TestIsChannelClean(t *testing.T) { // <---rev--- bobRevocation, _, _, err = bobChannel.RevokeCurrentCommitment() require.NoError(t, err) - _, _, _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) + _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) require.NoError(t, err) assertCleanOrDirty(false, aliceChannel, bobChannel, t) @@ -9892,7 +9910,7 @@ func TestIsChannelClean(t *testing.T) { // ---rev---> aliceRevocation, _, _, err = aliceChannel.RevokeCurrentCommitment() require.NoError(t, err) - _, _, _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) + _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) require.NoError(t, err) assertCleanOrDirty(true, aliceChannel, bobChannel, t) } @@ -10035,7 +10053,7 @@ func testGetDustSum(t *testing.T, chantype channeldb.ChannelType) { // dust. bobRevocation, _, _, err := bobChannel.RevokeCurrentCommitment() require.NoError(t, err) - _, _, _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) + _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) require.NoError(t, err) checkDust(aliceChannel, htlc2Amt, htlc2Amt) checkDust(bobChannel, htlc2Amt, htlc2Amt) @@ -10048,7 +10066,7 @@ func testGetDustSum(t *testing.T, chantype channeldb.ChannelType) { require.NoError(t, err) aliceRevocation, _, _, err := aliceChannel.RevokeCurrentCommitment() require.NoError(t, err) - _, _, _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) + _, _, err = bobChannel.ReceiveRevocation(aliceRevocation) require.NoError(t, err) checkDust(aliceChannel, htlc2Amt, htlc2Amt) checkDust(bobChannel, htlc2Amt, htlc2Amt) @@ -11008,7 +11026,7 @@ func TestAsynchronousSendingWithFeeBuffer(t *testing.T) { bobRevocation, _, _, err := bobChannel.RevokeCurrentCommitment() require.NoError(t, err) - _, _, _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) + _, _, err = aliceChannel.ReceiveRevocation(bobRevocation) require.NoError(t, err) // Before testing the behavior of the fee buffer, we are going to fail diff --git a/lnwallet/test_utils.go b/lnwallet/test_utils.go index 5e0cac1f8..a9f71f24c 100644 --- a/lnwallet/test_utils.go +++ b/lnwallet/test_utils.go @@ -566,7 +566,7 @@ func ForceStateTransition(chanA, chanB *LightningChannel) error { return err } - _, _, _, _, err = chanA.ReceiveRevocation(bobRevocation) + _, _, err = chanA.ReceiveRevocation(bobRevocation) if err != nil { return err } @@ -579,7 +579,7 @@ func ForceStateTransition(chanA, chanB *LightningChannel) error { if err != nil { return err } - _, _, _, _, err = chanB.ReceiveRevocation(aliceRevocation) + _, _, err = chanB.ReceiveRevocation(aliceRevocation) if err != nil { return err } diff --git a/lnwallet/transactions_test.go b/lnwallet/transactions_test.go index 439e7ce95..3588acfeb 100644 --- a/lnwallet/transactions_test.go +++ b/lnwallet/transactions_test.go @@ -366,7 +366,7 @@ func testVectors(t *testing.T, chanType channeldb.ChannelType, test testCase) { revMsg, _, _, err := remoteChannel.RevokeCurrentCommitment() require.NoError(t, err) - _, _, _, _, err = localChannel.ReceiveRevocation(revMsg) + _, _, err = localChannel.ReceiveRevocation(revMsg) require.NoError(t, err) remoteNewCommit, err := remoteChannel.SignNextCommitment() From dc60f78f7f6e9828943c0f127a5c0a147060a38d Mon Sep 17 00:00:00 2001 From: Keagan McClelland Date: Fri, 16 Aug 2024 14:52:44 -0700 Subject: [PATCH 15/18] htlcswitch: remove PaymentDescriptor conversion from reforwardSettleFails This is part of a systematic removal of PaymentDescriptor from the mechanics of the htlcswitch package. --- htlcswitch/switch.go | 40 +++++++++++++++------------------------- 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/htlcswitch/switch.go b/htlcswitch/switch.go index a839ec3bf..01596ade0 100644 --- a/htlcswitch/switch.go +++ b/htlcswitch/switch.go @@ -1911,18 +1911,8 @@ func (s *Switch) loadChannelFwdPkgs(source lnwire.ShortChannelID) ([]*channeldb. // NOTE: This should mimic the behavior processRemoteSettleFails. func (s *Switch) reforwardSettleFails(fwdPkgs []*channeldb.FwdPkg) { for _, fwdPkg := range fwdPkgs { - settleFails, err := lnwallet.PayDescsFromRemoteLogUpdates( - fwdPkg.Source, fwdPkg.Height, fwdPkg.SettleFails, - ) - if err != nil { - log.Errorf("Unable to process remote log updates: %v", - err) - continue - } - - switchPackets := make([]*htlcPacket, 0, len(settleFails)) - for i, pd := range settleFails { - + switchPackets := make([]*htlcPacket, 0, len(fwdPkg.SettleFails)) + for i, update := range fwdPkg.SettleFails { // Skip any settles or fails that have already been // acknowledged by the incoming link that originated the // forwarded Add. @@ -1930,20 +1920,18 @@ func (s *Switch) reforwardSettleFails(fwdPkgs []*channeldb.FwdPkg) { continue } - switch pd.EntryType { - + switch msg := update.UpdateMsg.(type) { // A settle for an HTLC we previously forwarded HTLC has // been received. So we'll forward the HTLC to the // switch which will handle propagating the settle to // the prior hop. - case lnwallet.Settle: + case *lnwire.UpdateFulfillHTLC: + destRef := fwdPkg.DestRef(uint16(i)) settlePacket := &htlcPacket{ outgoingChanID: fwdPkg.Source, - outgoingHTLCID: pd.ParentIndex, - destRef: pd.DestRef, - htlc: &lnwire.UpdateFulfillHTLC{ - PaymentPreimage: pd.RPreimage, - }, + outgoingHTLCID: msg.ID, + destRef: &destRef, + htlc: msg, } // Add the packet to the batch to be forwarded, and @@ -1955,7 +1943,7 @@ func (s *Switch) reforwardSettleFails(fwdPkgs []*channeldb.FwdPkg) { // received. As a result a new slot will be freed up in our // commitment state, so we'll forward this to the switch so the // backwards undo can continue. - case lnwallet.Fail: + case *lnwire.UpdateFailHTLC: // Fetch the reason the HTLC was canceled so // we can continue to propagate it. This // failure originated from another node, so @@ -1964,11 +1952,13 @@ func (s *Switch) reforwardSettleFails(fwdPkgs []*channeldb.FwdPkg) { // additional circuit information for us. failPacket := &htlcPacket{ outgoingChanID: fwdPkg.Source, - outgoingHTLCID: pd.ParentIndex, - destRef: pd.DestRef, - htlc: &lnwire.UpdateFailHTLC{ - Reason: lnwire.OpaqueReason(pd.FailReason), + outgoingHTLCID: msg.ID, + destRef: &channeldb.SettleFailRef{ + Source: fwdPkg.Source, + Height: fwdPkg.Height, + Index: uint16(i), }, + htlc: msg, } // Add the packet to the batch to be forwarded, and From 27ff5f085a3f15ea4d5042caac9d243fc58748e9 Mon Sep 17 00:00:00 2001 From: Keagan McClelland Date: Fri, 16 Aug 2024 14:54:30 -0700 Subject: [PATCH 16/18] lnwallet: remove unused function PayDescsFromRemoteLogUpdates This function is no longer used as of the last commit and it is the last remaining leak of the PaymentDescriptor type through the public API. --- lnwallet/channel.go | 97 --------------------------------------------- 1 file changed, 97 deletions(-) diff --git a/lnwallet/channel.go b/lnwallet/channel.go index 6d9837229..1e9ba5842 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -168,103 +168,6 @@ func (e *ErrCommitSyncLocalDataLoss) Error() string { // payments requested by the wallet/daemon. type PaymentHash [32]byte -// PayDescsFromRemoteLogUpdates converts a slice of LogUpdates received from the -// remote peer into PaymentDescriptors to inform a link's forwarding decisions. -// -// NOTE: The provided `logUpdates` MUST correspond exactly to either the Adds -// or SettleFails in this channel's forwarding package at `height`. -func PayDescsFromRemoteLogUpdates(chanID lnwire.ShortChannelID, height uint64, - logUpdates []channeldb.LogUpdate) ([]*PaymentDescriptor, error) { - - // Allocate enough space to hold all of the payment descriptors we will - // reconstruct, and also the list of pointers that will be returned to - // the caller. - payDescs := make([]PaymentDescriptor, 0, len(logUpdates)) - payDescPtrs := make([]*PaymentDescriptor, 0, len(logUpdates)) - - // Iterate over the log updates we loaded from disk, and reconstruct the - // payment descriptor corresponding to one of the four types of htlcs we - // can receive from the remote peer. We only repopulate the information - // necessary to process the packets and, if necessary, forward them to - // the switch. - // - // For each log update, we include either an AddRef or a SettleFailRef - // so that they can be ACK'd and garbage collected. - for i, logUpdate := range logUpdates { - var pd PaymentDescriptor - switch wireMsg := logUpdate.UpdateMsg.(type) { - - case *lnwire.UpdateAddHTLC: - pd = PaymentDescriptor{ - ChanID: wireMsg.ChanID, - RHash: wireMsg.PaymentHash, - Timeout: wireMsg.Expiry, - Amount: wireMsg.Amount, - EntryType: Add, - HtlcIndex: wireMsg.ID, - LogIndex: logUpdate.LogIndex, - SourceRef: &channeldb.AddRef{ - Height: height, - Index: uint16(i), - }, - BlindingPoint: wireMsg.BlindingPoint, - CustomRecords: wireMsg.CustomRecords.Copy(), - } - pd.OnionBlob = wireMsg.OnionBlob - - case *lnwire.UpdateFulfillHTLC: - pd = PaymentDescriptor{ - ChanID: wireMsg.ChanID, - RPreimage: wireMsg.PaymentPreimage, - ParentIndex: wireMsg.ID, - EntryType: Settle, - DestRef: &channeldb.SettleFailRef{ - Source: chanID, - Height: height, - Index: uint16(i), - }, - } - - case *lnwire.UpdateFailHTLC: - pd = PaymentDescriptor{ - ChanID: wireMsg.ChanID, - ParentIndex: wireMsg.ID, - EntryType: Fail, - FailReason: wireMsg.Reason[:], - DestRef: &channeldb.SettleFailRef{ - Source: chanID, - Height: height, - Index: uint16(i), - }, - } - - case *lnwire.UpdateFailMalformedHTLC: - pd = PaymentDescriptor{ - ChanID: wireMsg.ChanID, - ParentIndex: wireMsg.ID, - EntryType: MalformedFail, - FailCode: wireMsg.FailureCode, - ShaOnionBlob: wireMsg.ShaOnionBlob, - DestRef: &channeldb.SettleFailRef{ - Source: chanID, - Height: height, - Index: uint16(i), - }, - } - - // NOTE: UpdateFee is not expected since they are not forwarded. - case *lnwire.UpdateFee: - return nil, fmt.Errorf("unexpected update fee") - - } - - payDescs = append(payDescs, pd) - payDescPtrs = append(payDescPtrs, &payDescs[i]) - } - - return payDescPtrs, nil -} - // commitment represents a commitment to a new state within an active channel. // New commitments can be initiated by either side. Commitments are ordered // into a commitment chain, with one existing for both parties. Each side can From 4fadbb09bddabbb8c78b572f285fe34d3048e819 Mon Sep 17 00:00:00 2001 From: Keagan McClelland Date: Fri, 14 Jun 2024 16:30:28 -0700 Subject: [PATCH 17/18] htlcswitch+lnwallet: quarantine paymentDescriptor to lnwallet The objective of this commit is to make paymentDescriptor a private data structure so we can quarantine it to the lnwallet package. To accomplish this we had to prevent it from leaking out via the arguments or return values of the public functions in lnwallet. This naturally had consequences for the htlcswitch package as we choose other mechanisms for tracking the data that paymentDescriptor was responsible for. Astoundingly, this was highly successful and allowed us to remove a ton of redundant code. The diff for this commit represents a substantial reduction in total lines of code as well as extraneous arguments and return values from key functions. This also sets the stage for future commits where we actually will be attempting to rid lnwallet of paymentDescriptor completely. --- lnwallet/channel.go | 174 +++++++++++++++++---------------- lnwallet/channel_test.go | 74 +++++++------- lnwallet/commitment.go | 6 +- lnwallet/payment_descriptor.go | 20 ++-- lnwallet/update_log.go | 24 ++--- 5 files changed, 150 insertions(+), 148 deletions(-) diff --git a/lnwallet/channel.go b/lnwallet/channel.go index 1e9ba5842..0000026c2 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -234,11 +234,11 @@ type commitment struct { // outgoingHTLCs is a slice of all the outgoing HTLC's (from our PoV) // on this commitment transaction. - outgoingHTLCs []PaymentDescriptor + outgoingHTLCs []paymentDescriptor // incomingHTLCs is a slice of all the incoming HTLC's (from our PoV) // on this commitment transaction. - incomingHTLCs []PaymentDescriptor + incomingHTLCs []paymentDescriptor // customBlob stores opaque bytes that may be used by custom channels // to store extra data for a given commitment state. @@ -254,8 +254,8 @@ type commitment struct { // this map in order to locate the details needed to validate an HTLC // signature while iterating of the outputs in the local commitment // view. - outgoingHTLCIndex map[int32]*PaymentDescriptor - incomingHTLCIndex map[int32]*PaymentDescriptor + outgoingHTLCIndex map[int32]*paymentDescriptor + incomingHTLCIndex map[int32]*paymentDescriptor } // locateOutputIndex is a small helper function to locate the output index of a @@ -263,7 +263,7 @@ type commitment struct { // passed in is to be retained for each output within the commitment // transition. This ensures that we don't assign multiple HTLCs to the same // index within the commitment transaction. -func locateOutputIndex(p *PaymentDescriptor, tx *wire.MsgTx, +func locateOutputIndex(p *paymentDescriptor, tx *wire.MsgTx, whoseCommit lntypes.ChannelParty, dups map[PaymentHash][]int32, cltvs []uint32) (int32, error) { @@ -303,7 +303,7 @@ func locateOutputIndex(p *PaymentDescriptor, tx *wire.MsgTx, // populateHtlcIndexes modifies the set of HTLCs locked-into the target view // to have full indexing information populated. This information is required as // we need to keep track of the indexes of each HTLC in order to properly write -// the current state to disk, and also to locate the PaymentDescriptor +// the current state to disk, and also to locate the paymentDescriptor // corresponding to HTLC outputs in the commitment transaction. func (c *commitment) populateHtlcIndexes(chanType channeldb.ChannelType, cltvs []uint32) error { @@ -313,12 +313,12 @@ func (c *commitment) populateHtlcIndexes(chanType channeldb.ChannelType, // must keep this index so we can validate the HTLC signatures sent to // us. dups := make(map[PaymentHash][]int32) - c.outgoingHTLCIndex = make(map[int32]*PaymentDescriptor) - c.incomingHTLCIndex = make(map[int32]*PaymentDescriptor) + c.outgoingHTLCIndex = make(map[int32]*paymentDescriptor) + c.incomingHTLCIndex = make(map[int32]*paymentDescriptor) // populateIndex is a helper function that populates the necessary // indexes within the commitment view for a particular HTLC. - populateIndex := func(htlc *PaymentDescriptor, incoming bool) error { + populateIndex := func(htlc *paymentDescriptor, incoming bool) error { isDust := HtlcIsDust( chanType, incoming, c.whoseCommit, c.feePerKw, htlc.Amount.ToSatoshis(), c.dustLimit, @@ -482,15 +482,15 @@ func (c *commitment) toDiskCommit( func (lc *LightningChannel) diskHtlcToPayDesc(feeRate chainfee.SatPerKWeight, htlc *channeldb.HTLC, commitKeys lntypes.Dual[*CommitmentKeyRing], whoseCommit lntypes.ChannelParty, - auxLeaf input.AuxTapLeaf) (PaymentDescriptor, error) { + auxLeaf input.AuxTapLeaf) (paymentDescriptor, error) { - // The proper pkScripts for this PaymentDescriptor must be + // The proper pkScripts for this paymentDescriptor must be // generated so we can easily locate them within the commitment // transaction in the future. var ( ourP2WSH, theirP2WSH []byte ourWitnessScript, theirWitnessScript []byte - pd PaymentDescriptor + pd paymentDescriptor chanType = lc.channelState.ChanType ) @@ -550,7 +550,7 @@ func (lc *LightningChannel) diskHtlcToPayDesc(feeRate chainfee.SatPerKWeight, // With the scripts reconstructed (depending on if this is our commit // vs theirs or a pending commit for the remote party), we can now // re-create the original payment descriptor. - return PaymentDescriptor{ + return paymentDescriptor{ ChanID: lc.ChannelID(), RHash: htlc.RHash, Timeout: htlc.RefundTimeout, @@ -577,16 +577,16 @@ func (lc *LightningChannel) diskHtlcToPayDesc(feeRate chainfee.SatPerKWeight, func (lc *LightningChannel) extractPayDescs(feeRate chainfee.SatPerKWeight, htlcs []channeldb.HTLC, commitKeys lntypes.Dual[*CommitmentKeyRing], whoseCommit lntypes.ChannelParty, - auxLeaves fn.Option[CommitAuxLeaves]) ([]PaymentDescriptor, - []PaymentDescriptor, error) { + auxLeaves fn.Option[CommitAuxLeaves]) ([]paymentDescriptor, + []paymentDescriptor, error) { var ( - incomingHtlcs []PaymentDescriptor - outgoingHtlcs []PaymentDescriptor + incomingHtlcs []paymentDescriptor + outgoingHtlcs []paymentDescriptor ) // For each included HTLC within this commitment state, we'll convert - // the disk format into our in memory PaymentDescriptor format, + // the disk format into our in memory paymentDescriptor format, // partitioning based on if we offered or received the HTLC. for _, htlc := range htlcs { // TODO(roasbeef): set isForwarded to false for all? need to @@ -668,7 +668,7 @@ func (lc *LightningChannel) diskCommitToMemCommit( } // With the key rings re-created, we'll now convert all the on-disk - // HTLC"s into PaymentDescriptor's so we can re-insert them into our + // HTLC"s into paymentDescriptor's so we can re-insert them into our // update log. incomingHtlcs, outgoingHtlcs, err := lc.extractPayDescs( chainfee.SatPerKWeight(diskCommit.FeePerKw), @@ -1024,7 +1024,7 @@ func (lc *LightningChannel) ResetState() { lc.Unlock() } -// logUpdateToPayDesc converts a LogUpdate into a matching PaymentDescriptor +// logUpdateToPayDesc converts a LogUpdate into a matching paymentDescriptor // entry that can be re-inserted into the update log. This method is used when // we extended a state to the remote party, but the connection was obstructed // before we could finish the commitment dance. In this case, we need to @@ -1034,25 +1034,25 @@ func (lc *LightningChannel) logUpdateToPayDesc(logUpdate *channeldb.LogUpdate, remoteUpdateLog *updateLog, commitHeight uint64, feeRate chainfee.SatPerKWeight, remoteCommitKeys *CommitmentKeyRing, remoteDustLimit btcutil.Amount, - auxLeaves fn.Option[CommitAuxLeaves]) (*PaymentDescriptor, error) { + auxLeaves fn.Option[CommitAuxLeaves]) (*paymentDescriptor, error) { // Depending on the type of update message we'll map that to a distinct - // PaymentDescriptor instance. - var pd *PaymentDescriptor + // paymentDescriptor instance. + var pd *paymentDescriptor switch wireMsg := logUpdate.UpdateMsg.(type) { - // For offered HTLC's, we'll map that to a PaymentDescriptor with the + // For offered HTLC's, we'll map that to a paymentDescriptor with the // type Add, ensuring we restore the necessary fields. From the PoV of // the commitment chain, this HTLC was included in the remote chain, // but not the local chain. case *lnwire.UpdateAddHTLC: // First, we'll map all the relevant fields in the // UpdateAddHTLC message to their corresponding fields in the - // PaymentDescriptor struct. We also set addCommitHeightRemote + // paymentDescriptor struct. We also set addCommitHeightRemote // as we've included this HTLC in our local commitment chain // for the remote party. - pd = &PaymentDescriptor{ + pd = &paymentDescriptor{ ChanID: wireMsg.ChanID, RHash: wireMsg.PaymentHash, Timeout: wireMsg.Expiry, @@ -1093,11 +1093,11 @@ func (lc *LightningChannel) logUpdateToPayDesc(logUpdate *channeldb.LogUpdate, // For HTLC's we're offered we'll fetch the original offered HTLC // from the remote party's update log so we can retrieve the same - // PaymentDescriptor that SettleHTLC would produce. + // paymentDescriptor that SettleHTLC would produce. case *lnwire.UpdateFulfillHTLC: ogHTLC := remoteUpdateLog.lookupHtlc(wireMsg.ID) - pd = &PaymentDescriptor{ + pd = &paymentDescriptor{ ChanID: wireMsg.ChanID, Amount: ogHTLC.Amount, RHash: ogHTLC.RHash, @@ -1115,7 +1115,7 @@ func (lc *LightningChannel) logUpdateToPayDesc(logUpdate *channeldb.LogUpdate, case *lnwire.UpdateFailHTLC: ogHTLC := remoteUpdateLog.lookupHtlc(wireMsg.ID) - pd = &PaymentDescriptor{ + pd = &paymentDescriptor{ ChanID: wireMsg.ChanID, Amount: ogHTLC.Amount, RHash: ogHTLC.RHash, @@ -1132,7 +1132,7 @@ func (lc *LightningChannel) logUpdateToPayDesc(logUpdate *channeldb.LogUpdate, ogHTLC := remoteUpdateLog.lookupHtlc(wireMsg.ID) // TODO(roasbeef): err if nil? - pd = &PaymentDescriptor{ + pd = &paymentDescriptor{ ChanID: wireMsg.ChanID, Amount: ogHTLC.Amount, RHash: ogHTLC.RHash, @@ -1151,7 +1151,7 @@ func (lc *LightningChannel) logUpdateToPayDesc(logUpdate *channeldb.LogUpdate, // height to the same value, as we consider the fee update locked in by // adding and removing it at the same height. case *lnwire.UpdateFee: - pd = &PaymentDescriptor{ + pd = &paymentDescriptor{ ChanID: wireMsg.ChanID, LogIndex: logUpdate.LogIndex, Amount: lnwire.NewMSatFromSatoshis( @@ -1166,29 +1166,29 @@ func (lc *LightningChannel) logUpdateToPayDesc(logUpdate *channeldb.LogUpdate, return pd, nil } -// localLogUpdateToPayDesc converts a LogUpdate into a matching PaymentDescriptor -// entry that can be re-inserted into the local update log. This method is used -// when we sent an update+sig, receive a revocation, but drop right before the -// counterparty can sign for the update we just sent. In this case, we need to -// re-insert the original entries back into the update log so we'll be expecting -// the peer to sign them. The height of the remote commitment is expected to be -// provided and we restore all log update entries with this height, even though -// the real height may be lower. In the way these fields are used elsewhere, this -// doesn't change anything. +// localLogUpdateToPayDesc converts a LogUpdate into a matching +// paymentDescriptor entry that can be re-inserted into the local update log. +// This method is used when we sent an update+sig, receive a revocation, but +// drop right before the counterparty can sign for the update we just sent. In +// this case, we need to re-insert the original entries back into the update +// log so we'll be expecting the peer to sign them. The height of the remote +// commitment is expected to be provided and we restore all log update entries +// with this height, even though the real height may be lower. In the way these +// fields are used elsewhere, this doesn't change anything. func (lc *LightningChannel) localLogUpdateToPayDesc(logUpdate *channeldb.LogUpdate, - remoteUpdateLog *updateLog, commitHeight uint64) (*PaymentDescriptor, + remoteUpdateLog *updateLog, commitHeight uint64) (*paymentDescriptor, error) { // Since Add updates aren't saved to disk under this key, the update will // never be an Add. switch wireMsg := logUpdate.UpdateMsg.(type) { // For HTLCs that we settled, we'll fetch the original offered HTLC from - // the remote update log so we can retrieve the same PaymentDescriptor that - // ReceiveHTLCSettle would produce. + // the remote update log so we can retrieve the same paymentDescriptor + // that ReceiveHTLCSettle would produce. case *lnwire.UpdateFulfillHTLC: ogHTLC := remoteUpdateLog.lookupHtlc(wireMsg.ID) - return &PaymentDescriptor{ + return &paymentDescriptor{ ChanID: wireMsg.ChanID, Amount: ogHTLC.Amount, RHash: ogHTLC.RHash, @@ -1205,7 +1205,7 @@ func (lc *LightningChannel) localLogUpdateToPayDesc(logUpdate *channeldb.LogUpda case *lnwire.UpdateFailHTLC: ogHTLC := remoteUpdateLog.lookupHtlc(wireMsg.ID) - return &PaymentDescriptor{ + return &paymentDescriptor{ ChanID: wireMsg.ChanID, Amount: ogHTLC.Amount, RHash: ogHTLC.RHash, @@ -1221,7 +1221,7 @@ func (lc *LightningChannel) localLogUpdateToPayDesc(logUpdate *channeldb.LogUpda case *lnwire.UpdateFailMalformedHTLC: ogHTLC := remoteUpdateLog.lookupHtlc(wireMsg.ID) - return &PaymentDescriptor{ + return &paymentDescriptor{ ChanID: wireMsg.ChanID, Amount: ogHTLC.Amount, RHash: ogHTLC.RHash, @@ -1234,7 +1234,7 @@ func (lc *LightningChannel) localLogUpdateToPayDesc(logUpdate *channeldb.LogUpda }, nil case *lnwire.UpdateFee: - return &PaymentDescriptor{ + return &paymentDescriptor{ ChanID: wireMsg.ChanID, LogIndex: logUpdate.LogIndex, Amount: lnwire.NewMSatFromSatoshis( @@ -1251,7 +1251,7 @@ func (lc *LightningChannel) localLogUpdateToPayDesc(logUpdate *channeldb.LogUpda } // remoteLogUpdateToPayDesc converts a LogUpdate into a matching -// PaymentDescriptor entry that can be re-inserted into the update log. This +// paymentDescriptor entry that can be re-inserted into the update log. This // method is used when we revoked a local commitment, but the connection was // obstructed before we could sign a remote commitment that contains these // updates. In this case, we need to re-insert the original entries back into @@ -1261,12 +1261,12 @@ func (lc *LightningChannel) localLogUpdateToPayDesc(logUpdate *channeldb.LogUpda // may be lower. In the way these fields are used elsewhere, this doesn't change // anything. func (lc *LightningChannel) remoteLogUpdateToPayDesc(logUpdate *channeldb.LogUpdate, - localUpdateLog *updateLog, commitHeight uint64) (*PaymentDescriptor, + localUpdateLog *updateLog, commitHeight uint64) (*paymentDescriptor, error) { switch wireMsg := logUpdate.UpdateMsg.(type) { case *lnwire.UpdateAddHTLC: - pd := &PaymentDescriptor{ + pd := &paymentDescriptor{ ChanID: wireMsg.ChanID, RHash: wireMsg.PaymentHash, Timeout: wireMsg.Expiry, @@ -1287,11 +1287,11 @@ func (lc *LightningChannel) remoteLogUpdateToPayDesc(logUpdate *channeldb.LogUpd // For HTLCs that the remote party settled, we'll fetch the original // offered HTLC from the local update log so we can retrieve the same - // PaymentDescriptor that ReceiveHTLCSettle would produce. + // paymentDescriptor that ReceiveHTLCSettle would produce. case *lnwire.UpdateFulfillHTLC: ogHTLC := localUpdateLog.lookupHtlc(wireMsg.ID) - return &PaymentDescriptor{ + return &paymentDescriptor{ ChanID: wireMsg.ChanID, Amount: ogHTLC.Amount, RHash: ogHTLC.RHash, @@ -1308,7 +1308,7 @@ func (lc *LightningChannel) remoteLogUpdateToPayDesc(logUpdate *channeldb.LogUpd case *lnwire.UpdateFailHTLC: ogHTLC := localUpdateLog.lookupHtlc(wireMsg.ID) - return &PaymentDescriptor{ + return &paymentDescriptor{ ChanID: wireMsg.ChanID, Amount: ogHTLC.Amount, RHash: ogHTLC.RHash, @@ -1324,7 +1324,7 @@ func (lc *LightningChannel) remoteLogUpdateToPayDesc(logUpdate *channeldb.LogUpd case *lnwire.UpdateFailMalformedHTLC: ogHTLC := localUpdateLog.lookupHtlc(wireMsg.ID) - return &PaymentDescriptor{ + return &paymentDescriptor{ ChanID: wireMsg.ChanID, Amount: ogHTLC.Amount, RHash: ogHTLC.RHash, @@ -1343,7 +1343,7 @@ func (lc *LightningChannel) remoteLogUpdateToPayDesc(logUpdate *channeldb.LogUpd // height to the same value, as we consider the fee update locked in by // adding and removing it at the same height. case *lnwire.UpdateFee: - return &PaymentDescriptor{ + return &paymentDescriptor{ ChanID: wireMsg.ChanID, LogIndex: logUpdate.LogIndex, Amount: lnwire.NewMSatFromSatoshis( @@ -2536,10 +2536,10 @@ type HtlcView struct { NextHeight uint64 // OurUpdates are our outgoing HTLCs. - OurUpdates []*PaymentDescriptor + OurUpdates []*paymentDescriptor // TheirUpdates are their incoming HTLCs. - TheirUpdates []*PaymentDescriptor + TheirUpdates []*paymentDescriptor // FeePerKw is the fee rate in sat/kw of the commitment transaction. FeePerKw chainfee.SatPerKWeight @@ -2551,7 +2551,7 @@ type HtlcView struct { func (lc *LightningChannel) fetchHTLCView(theirLogIndex, ourLogIndex uint64) *HtlcView { - var ourHTLCs []*PaymentDescriptor + var ourHTLCs []*paymentDescriptor for e := lc.updateLogs.Local.Front(); e != nil; e = e.Next() { htlc := e.Value @@ -2563,7 +2563,7 @@ func (lc *LightningChannel) fetchHTLCView(theirLogIndex, } } - var theirHTLCs []*PaymentDescriptor + var theirHTLCs []*paymentDescriptor for e := lc.updateLogs.Remote.Front(); e != nil; e = e.Next() { htlc := e.Value @@ -2701,13 +2701,13 @@ func (lc *LightningChannel) fetchCommitmentView( // commitment are mutated, we'll manually copy over each HTLC to its // respective slice. c.outgoingHTLCs = make( - []PaymentDescriptor, len(filteredHTLCView.OurUpdates), + []paymentDescriptor, len(filteredHTLCView.OurUpdates), ) for i, htlc := range filteredHTLCView.OurUpdates { c.outgoingHTLCs[i] = *htlc } c.incomingHTLCs = make( - []PaymentDescriptor, len(filteredHTLCView.TheirUpdates), + []paymentDescriptor, len(filteredHTLCView.TheirUpdates), ) for i, htlc := range filteredHTLCView.TheirUpdates { c.incomingHTLCs[i] = *htlc @@ -2882,9 +2882,9 @@ func (lc *LightningChannel) evaluateHTLCView(view *HtlcView, ourBalance, // fetchParent is a helper that looks up update log parent entries in the // appropriate log. -func (lc *LightningChannel) fetchParent(entry *PaymentDescriptor, +func (lc *LightningChannel) fetchParent(entry *paymentDescriptor, whoseCommitChain, whoseUpdateLog lntypes.ChannelParty, -) (*PaymentDescriptor, error) { +) (*paymentDescriptor, error) { var ( updateLog *updateLog @@ -2937,7 +2937,7 @@ func (lc *LightningChannel) fetchParent(entry *PaymentDescriptor, // If the HTLC hasn't yet been committed in either chain, then the height it // was committed is updated. Keeping track of this inclusion height allows us to // later compact the log once the change is fully committed in both chains. -func processAddEntry(htlc *PaymentDescriptor, ourBalance, +func processAddEntry(htlc *paymentDescriptor, ourBalance, theirBalance *lnwire.MilliSatoshi, nextHeight uint64, whoseCommitChain lntypes.ChannelParty, isIncoming, mutateState bool) { @@ -2975,7 +2975,7 @@ func processAddEntry(htlc *PaymentDescriptor, ourBalance, // processRemoveEntry processes a log entry which settles or times out a // previously added HTLC. If the removal entry has already been processed, it // is skipped. -func processRemoveEntry(htlc *PaymentDescriptor, ourBalance, +func processRemoveEntry(htlc *paymentDescriptor, ourBalance, theirBalance *lnwire.MilliSatoshi, nextHeight uint64, whoseCommitChain lntypes.ChannelParty, isIncoming, mutateState bool) { @@ -3024,7 +3024,7 @@ func processRemoveEntry(htlc *PaymentDescriptor, ourBalance, // processFeeUpdate processes a log update that updates the current commitment // fee. -func processFeeUpdate(feeUpdate *PaymentDescriptor, nextHeight uint64, +func processFeeUpdate(feeUpdate *paymentDescriptor, nextHeight uint64, whoseCommitChain lntypes.ChannelParty, mutateState bool, view *HtlcView) { @@ -3298,7 +3298,7 @@ func (lc *LightningChannel) createCommitDiff(newCommit *commitment, continue } - // We'll map the type of the PaymentDescriptor to one of the + // We'll map the type of the paymentDescriptor to one of the // four messages that it corresponds to. With this set of // messages obtained, we can simply read from disk and re-send // them in the case of a needed channel sync. @@ -3333,7 +3333,7 @@ func (lc *LightningChannel) createCommitDiff(newCommit *commitment, // Nothing special to do. } - logUpdates = append(logUpdates, pd.ToLogUpdate()) + logUpdates = append(logUpdates, pd.toLogUpdate()) } // With the set of log updates mapped into wire messages, we'll now @@ -3391,7 +3391,7 @@ func (lc *LightningChannel) getUnsignedAckedUpdates() []channeldb.LogUpdate { continue } - logUpdates = append(logUpdates, pd.ToLogUpdate()) + logUpdates = append(logUpdates, pd.toLogUpdate()) } return logUpdates @@ -3535,11 +3535,11 @@ func (lc *LightningChannel) applyCommitFee( // commitment transaction in terms of the ChannelConstraints that we and our // remote peer agreed upon during the funding workflow. The // predict[Our|Their]Add should parameters should be set to a valid -// PaymentDescriptor if we are validating in the state when adding a new HTLC, +// paymentDescriptor if we are validating in the state when adding a new HTLC, // or nil otherwise. func (lc *LightningChannel) validateCommitmentSanity(theirLogCounter, ourLogCounter uint64, whoseCommitChain lntypes.ChannelParty, - buffer BufferType, predictOurAdd, predictTheirAdd *PaymentDescriptor, + buffer BufferType, predictOurAdd, predictTheirAdd *paymentDescriptor, ) error { // First fetch the initial balance before applying any updates. @@ -3666,7 +3666,7 @@ func (lc *LightningChannel) validateCommitmentSanity(theirLogCounter, // validateUpdates take a set of updates, and validates them against // the passed channel constraints. - validateUpdates := func(updates []*PaymentDescriptor, + validateUpdates := func(updates []*paymentDescriptor, constraints *channeldb.ChannelConfig) error { // We keep track of the number of HTLCs in flight for the @@ -5390,7 +5390,7 @@ func (lc *LightningChannel) ReceiveRevocation(revMsg *lnwire.RevokeAndAck) ( // updates that we will eventually put into the // FwdPkg at this height. addUpdatesToForward = append( - addUpdatesToForward, pd.ToLogUpdate(), + addUpdatesToForward, pd.toLogUpdate(), ) case pd.EntryType != Add && committedRmv && shouldFwdRmv: @@ -5410,10 +5410,12 @@ func (lc *LightningChannel) ReceiveRevocation(revMsg *lnwire.RevokeAndAck) ( // updates that we will eventually put into the // FwdPkg at this height. settleFailUpdatesToForward = append( - settleFailUpdatesToForward, pd.ToLogUpdate(), + settleFailUpdatesToForward, pd.toLogUpdate(), ) default: + // The update was not "freshly locked in" so we will + // ignore it as we construct the forwarding package. continue } } @@ -5720,9 +5722,9 @@ func (lc *LightningChannel) MayAddOutgoingHtlc(amt lnwire.MilliSatoshi) error { // htlcAddDescriptor returns a payment descriptor for the htlc and open key // provided to add to our local update log. func (lc *LightningChannel) htlcAddDescriptor(htlc *lnwire.UpdateAddHTLC, - openKey *models.CircuitKey) *PaymentDescriptor { + openKey *models.CircuitKey) *paymentDescriptor { - return &PaymentDescriptor{ + return &paymentDescriptor{ ChanID: htlc.ChanID, EntryType: Add, RHash: PaymentHash(htlc.PaymentHash), @@ -5739,7 +5741,7 @@ func (lc *LightningChannel) htlcAddDescriptor(htlc *lnwire.UpdateAddHTLC, // validateAddHtlc validates the addition of an outgoing htlc to our local and // remote commitments. -func (lc *LightningChannel) validateAddHtlc(pd *PaymentDescriptor, +func (lc *LightningChannel) validateAddHtlc(pd *paymentDescriptor, buffer BufferType) error { // Make sure adding this HTLC won't violate any of the constraints we // must keep on the commitment transactions. @@ -5785,7 +5787,7 @@ func (lc *LightningChannel) ReceiveHTLC(htlc *lnwire.UpdateAddHTLC) (uint64, "ID %d", htlc.ID, lc.updateLogs.Remote.htlcCounter) } - pd := &PaymentDescriptor{ + pd := &paymentDescriptor{ ChanID: htlc.ChanID, EntryType: Add, RHash: PaymentHash(htlc.PaymentHash), @@ -5866,7 +5868,7 @@ func (lc *LightningChannel) SettleHTLC(preimage [32]byte, return ErrInvalidSettlePreimage{preimage[:], htlc.RHash[:]} } - pd := &PaymentDescriptor{ + pd := &paymentDescriptor{ ChanID: lc.ChannelID(), Amount: htlc.Amount, RPreimage: preimage, @@ -5912,7 +5914,7 @@ func (lc *LightningChannel) ReceiveHTLCSettle(preimage [32]byte, htlcIndex uint6 return ErrInvalidSettlePreimage{preimage[:], htlc.RHash[:]} } - pd := &PaymentDescriptor{ + pd := &paymentDescriptor{ ChanID: lc.ChannelID(), Amount: htlc.Amount, RPreimage: preimage, @@ -5974,7 +5976,7 @@ func (lc *LightningChannel) FailHTLC(htlcIndex uint64, reason []byte, return ErrHtlcIndexAlreadyFailed(htlcIndex) } - pd := &PaymentDescriptor{ + pd := &paymentDescriptor{ ChanID: lc.ChannelID(), Amount: htlc.Amount, RHash: htlc.RHash, @@ -6025,7 +6027,7 @@ func (lc *LightningChannel) MalformedFailHTLC(htlcIndex uint64, return ErrHtlcIndexAlreadyFailed(htlcIndex) } - pd := &PaymentDescriptor{ + pd := &paymentDescriptor{ ChanID: lc.ChannelID(), Amount: htlc.Amount, RHash: htlc.RHash, @@ -6068,7 +6070,7 @@ func (lc *LightningChannel) ReceiveFailHTLC(htlcIndex uint64, reason []byte, return ErrHtlcIndexAlreadyFailed(htlcIndex) } - pd := &PaymentDescriptor{ + pd := &paymentDescriptor{ ChanID: lc.ChannelID(), Amount: htlc.Amount, RHash: htlc.RHash, @@ -8188,7 +8190,7 @@ func (lc *LightningChannel) UpdateFee(feePerKw chainfee.SatPerKWeight) error { return err } - pd := &PaymentDescriptor{ + pd := &paymentDescriptor{ ChanID: lc.ChannelID(), LogIndex: lc.updateLogs.Local.logIndex, Amount: lnwire.NewMSatFromSatoshis(btcutil.Amount(feePerKw)), @@ -8261,7 +8263,7 @@ func (lc *LightningChannel) ReceiveUpdateFee(feePerKw chainfee.SatPerKWeight) er } // TODO(roasbeef): or just modify to use the other balance? - pd := &PaymentDescriptor{ + pd := &paymentDescriptor{ ChanID: lc.ChannelID(), LogIndex: lc.updateLogs.Remote.logIndex, Amount: lnwire.NewMSatFromSatoshis(btcutil.Amount(feePerKw)), @@ -8752,7 +8754,7 @@ func (lc *LightningChannel) unsignedLocalUpdates(remoteMessageIndex, // sends. if pd.LogIndex < remoteMessageIndex && pd.LogIndex >= localMessageIndex { localPeerUpdates = append( - localPeerUpdates, pd.ToLogUpdate(), + localPeerUpdates, pd.toLogUpdate(), ) } } diff --git a/lnwallet/channel_test.go b/lnwallet/channel_test.go index 0af74dd1d..744be3ae5 100644 --- a/lnwallet/channel_test.go +++ b/lnwallet/channel_test.go @@ -6934,8 +6934,8 @@ func TestNewBreachRetributionSkipsDustHtlcs(t *testing.T) { } } -// compareHtlcs compares two PaymentDescriptors. -func compareHtlcs(htlc1, htlc2 *PaymentDescriptor) error { +// compareHtlcs compares two paymentDescriptors. +func compareHtlcs(htlc1, htlc2 *paymentDescriptor) error { if htlc1.LogIndex != htlc2.LogIndex { return fmt.Errorf("htlc log index did not match") } @@ -6953,7 +6953,7 @@ func compareHtlcs(htlc1, htlc2 *PaymentDescriptor) error { } // compareIndexes is a helper method to compare two index maps. -func compareIndexes(a, b map[uint64]*fn.Node[*PaymentDescriptor]) error { +func compareIndexes(a, b map[uint64]*fn.Node[*paymentDescriptor]) error { for k1, e1 := range a { e2, ok := b[k1] if !ok { @@ -7422,7 +7422,7 @@ func TestChannelRestoreCommitHeight(t *testing.T) { t.Fatalf("unable to create new channel: %v", err) } - var pd *PaymentDescriptor + var pd *paymentDescriptor if remoteLog { h := newChannel.updateLogs.Local.lookupHtlc(htlcIndex) if h != nil { @@ -8191,8 +8191,8 @@ func TestFetchParent(t *testing.T) { name string whoseCommitChain lntypes.ChannelParty whoseUpdateLog lntypes.ChannelParty - localEntries []*PaymentDescriptor - remoteEntries []*PaymentDescriptor + localEntries []*paymentDescriptor + remoteEntries []*paymentDescriptor // parentIndex is the parent index of the entry that we will // lookup with fetch parent. @@ -8226,7 +8226,7 @@ func TestFetchParent(t *testing.T) { { name: "remote log + chain, remote add height 0", localEntries: nil, - remoteEntries: []*PaymentDescriptor{ + remoteEntries: []*paymentDescriptor{ // This entry will be added at log index =0. { HtlcIndex: 1, @@ -8248,7 +8248,7 @@ func TestFetchParent(t *testing.T) { }, { name: "remote log, local chain, local add height 0", - remoteEntries: []*PaymentDescriptor{ + remoteEntries: []*paymentDescriptor{ // This entry will be added at log index =0. { HtlcIndex: 1, @@ -8271,7 +8271,7 @@ func TestFetchParent(t *testing.T) { }, { name: "local log + chain, local add height 0", - localEntries: []*PaymentDescriptor{ + localEntries: []*paymentDescriptor{ // This entry will be added at log index =0. { HtlcIndex: 1, @@ -8295,7 +8295,7 @@ func TestFetchParent(t *testing.T) { { name: "local log + remote chain, remote add height 0", - localEntries: []*PaymentDescriptor{ + localEntries: []*paymentDescriptor{ // This entry will be added at log index =0. { HtlcIndex: 1, @@ -8319,7 +8319,7 @@ func TestFetchParent(t *testing.T) { { name: "remote log found", localEntries: nil, - remoteEntries: []*PaymentDescriptor{ + remoteEntries: []*paymentDescriptor{ // This entry will be added at log index =0. { HtlcIndex: 1, @@ -8342,7 +8342,7 @@ func TestFetchParent(t *testing.T) { }, { name: "local log found", - localEntries: []*PaymentDescriptor{ + localEntries: []*paymentDescriptor{ // This entry will be added at log index =0. { HtlcIndex: 1, @@ -8388,7 +8388,7 @@ func TestFetchParent(t *testing.T) { } parent, err := lc.fetchParent( - &PaymentDescriptor{ + &paymentDescriptor{ ParentIndex: test.parentIndex, }, test.whoseCommitChain, @@ -8451,8 +8451,8 @@ func TestEvaluateView(t *testing.T) { tests := []struct { name string - ourHtlcs []*PaymentDescriptor - theirHtlcs []*PaymentDescriptor + ourHtlcs []*paymentDescriptor + theirHtlcs []*paymentDescriptor whoseCommitChain lntypes.ChannelParty mutateState bool @@ -8484,7 +8484,7 @@ func TestEvaluateView(t *testing.T) { name: "our fee update is applied", whoseCommitChain: lntypes.Local, mutateState: false, - ourHtlcs: []*PaymentDescriptor{ + ourHtlcs: []*paymentDescriptor{ { Amount: ourFeeUpdateAmt, EntryType: FeeUpdate, @@ -8501,8 +8501,8 @@ func TestEvaluateView(t *testing.T) { name: "their fee update is applied", whoseCommitChain: lntypes.Local, mutateState: false, - ourHtlcs: []*PaymentDescriptor{}, - theirHtlcs: []*PaymentDescriptor{ + ourHtlcs: []*paymentDescriptor{}, + theirHtlcs: []*paymentDescriptor{ { Amount: theirFeeUpdateAmt, EntryType: FeeUpdate, @@ -8519,14 +8519,14 @@ func TestEvaluateView(t *testing.T) { name: "htlcs adds without settles", whoseCommitChain: lntypes.Local, mutateState: false, - ourHtlcs: []*PaymentDescriptor{ + ourHtlcs: []*paymentDescriptor{ { HtlcIndex: 0, Amount: htlcAddAmount, EntryType: Add, }, }, - theirHtlcs: []*PaymentDescriptor{ + theirHtlcs: []*paymentDescriptor{ { HtlcIndex: 0, Amount: htlcAddAmount, @@ -8553,7 +8553,7 @@ func TestEvaluateView(t *testing.T) { name: "our htlc settled, state mutated", whoseCommitChain: lntypes.Local, mutateState: true, - ourHtlcs: []*PaymentDescriptor{ + ourHtlcs: []*paymentDescriptor{ { HtlcIndex: 0, Amount: htlcAddAmount, @@ -8561,7 +8561,7 @@ func TestEvaluateView(t *testing.T) { addCommitHeightLocal: addHeight, }, }, - theirHtlcs: []*PaymentDescriptor{ + theirHtlcs: []*paymentDescriptor{ { HtlcIndex: 0, Amount: htlcAddAmount, @@ -8588,7 +8588,7 @@ func TestEvaluateView(t *testing.T) { name: "our htlc settled, state not mutated", whoseCommitChain: lntypes.Local, mutateState: false, - ourHtlcs: []*PaymentDescriptor{ + ourHtlcs: []*paymentDescriptor{ { HtlcIndex: 0, Amount: htlcAddAmount, @@ -8596,7 +8596,7 @@ func TestEvaluateView(t *testing.T) { addCommitHeightLocal: addHeight, }, }, - theirHtlcs: []*PaymentDescriptor{ + theirHtlcs: []*paymentDescriptor{ { HtlcIndex: 0, Amount: htlcAddAmount, @@ -8623,7 +8623,7 @@ func TestEvaluateView(t *testing.T) { name: "their htlc settled, state mutated", whoseCommitChain: lntypes.Local, mutateState: true, - ourHtlcs: []*PaymentDescriptor{ + ourHtlcs: []*paymentDescriptor{ { HtlcIndex: 0, Amount: htlcAddAmount, @@ -8638,7 +8638,7 @@ func TestEvaluateView(t *testing.T) { ParentIndex: 1, }, }, - theirHtlcs: []*PaymentDescriptor{ + theirHtlcs: []*paymentDescriptor{ { HtlcIndex: 0, Amount: htlcAddAmount, @@ -8667,7 +8667,7 @@ func TestEvaluateView(t *testing.T) { whoseCommitChain: lntypes.Local, mutateState: false, - ourHtlcs: []*PaymentDescriptor{ + ourHtlcs: []*paymentDescriptor{ { HtlcIndex: 0, Amount: htlcAddAmount, @@ -8682,7 +8682,7 @@ func TestEvaluateView(t *testing.T) { ParentIndex: 0, }, }, - theirHtlcs: []*PaymentDescriptor{ + theirHtlcs: []*paymentDescriptor{ { HtlcIndex: 0, Amount: htlcAddAmount, @@ -8789,7 +8789,7 @@ func TestEvaluateView(t *testing.T) { // checkExpectedHtlcs checks that a set of htlcs that we have contains all the // htlcs we expect. -func checkExpectedHtlcs(t *testing.T, actual []*PaymentDescriptor, +func checkExpectedHtlcs(t *testing.T, actual []*paymentDescriptor, expected map[uint64]bool) { if len(expected) != len(actual) { @@ -8981,7 +8981,7 @@ func TestProcessFeeUpdate(t *testing.T) { // Create a fee update with add and remove heights as // set in the test. heights := test.startHeights - update := &PaymentDescriptor{ + update := &paymentDescriptor{ Amount: ourFeeUpdateAmt, addCommitHeightRemote: heights.remoteAdd, addCommitHeightLocal: heights.localAdd, @@ -9008,7 +9008,7 @@ func TestProcessFeeUpdate(t *testing.T) { } } -func checkHeights(t *testing.T, update *PaymentDescriptor, expected heights) { +func checkHeights(t *testing.T, update *paymentDescriptor, expected heights) { updateHeights := heights{ localAdd: update.addCommitHeightLocal, localRemove: update.removeCommitHeightLocal, @@ -9377,7 +9377,7 @@ func TestProcessAddRemoveEntry(t *testing.T) { t.Parallel() heights := test.startHeights - update := &PaymentDescriptor{ + update := &paymentDescriptor{ Amount: updateAmount, addCommitHeightLocal: heights.localAdd, addCommitHeightRemote: heights.remoteAdd, @@ -10572,7 +10572,7 @@ func testNewBreachRetribution(t *testing.T, chanType channeldb.ChannelType) { } // TestExtractPayDescs asserts that `extractPayDescs` can correctly turn a -// slice of htlcs into two slices of PaymentDescriptors. +// slice of htlcs into two slices of paymentDescriptors. func TestExtractPayDescs(t *testing.T) { t.Parallel() @@ -10609,22 +10609,22 @@ func TestExtractPayDescs(t *testing.T) { ) require.NoError(t, err) - // Assert the incoming PaymentDescriptors are matched. + // Assert the incoming paymentDescriptors are matched. for i, pd := range incomingPDs { htlc := incomings[i] assertPayDescMatchHTLC(t, pd, htlc) } - // Assert the outgoing PaymentDescriptors are matched. + // Assert the outgoing paymentDescriptors are matched. for i, pd := range outgoingPDs { htlc := outgoings[i] assertPayDescMatchHTLC(t, pd, htlc) } } -// assertPayDescMatchHTLC compares a PaymentDescriptor to a channeldb.HTLC and +// assertPayDescMatchHTLC compares a paymentDescriptor to a channeldb.HTLC and // asserts that the fields are matched. -func assertPayDescMatchHTLC(t *testing.T, pd PaymentDescriptor, +func assertPayDescMatchHTLC(t *testing.T, pd paymentDescriptor, htlc channeldb.HTLC) { require := require.New(t) diff --git a/lnwallet/commitment.go b/lnwallet/commitment.go index 2ff23ab63..73565ea18 100644 --- a/lnwallet/commitment.go +++ b/lnwallet/commitment.go @@ -1229,10 +1229,10 @@ func genHtlcScript(chanType channeldb.ChannelType, isIncoming bool, // is incoming and if it's being applied to our commitment transaction or that // of the remote node's. Additionally, in order to be able to efficiently // locate the added HTLC on the commitment transaction from the -// PaymentDescriptor that generated it, the generated script is stored within +// paymentDescriptor that generated it, the generated script is stored within // the descriptor itself. func addHTLC(commitTx *wire.MsgTx, whoseCommit lntypes.ChannelParty, - isIncoming bool, paymentDesc *PaymentDescriptor, + isIncoming bool, paymentDesc *paymentDescriptor, keyRing *CommitmentKeyRing, chanType channeldb.ChannelType, auxLeaf input.AuxTapLeaf) error { @@ -1253,7 +1253,7 @@ func addHTLC(commitTx *wire.MsgTx, whoseCommit lntypes.ChannelParty, amountPending := int64(paymentDesc.Amount.ToSatoshis()) commitTx.AddTxOut(wire.NewTxOut(amountPending, pkScript)) - // Store the pkScript of this particular PaymentDescriptor so we can + // Store the pkScript of this particular paymentDescriptor so we can // quickly locate it within the commitment transaction later. if whoseCommit.IsLocal() { paymentDesc.ourPkScript = pkScript diff --git a/lnwallet/payment_descriptor.go b/lnwallet/payment_descriptor.go index 2d6c158cd..5a51f29ce 100644 --- a/lnwallet/payment_descriptor.go +++ b/lnwallet/payment_descriptor.go @@ -62,17 +62,17 @@ func (u updateType) String() string { } } -// PaymentDescriptor represents a commitment state update which either adds, -// settles, or removes an HTLC. PaymentDescriptors encapsulate all necessary +// paymentDescriptor represents a commitment state update which either adds, +// settles, or removes an HTLC. paymentDescriptors encapsulate all necessary // metadata w.r.t to an HTLC, and additional data pairing a settle message to // the original added HTLC. // // TODO(roasbeef): LogEntry interface?? // - need to separate attrs for cancel/add/settle/feeupdate -type PaymentDescriptor struct { +type paymentDescriptor struct { // ChanID is the ChannelID of the LightningChannel that this - // PaymentDescriptor belongs to. We track this here so we can - // reconstruct the Messages that this PaymentDescriptor is built from. + // paymentDescriptor belongs to. We track this here so we can + // reconstruct the Messages that this paymentDescriptor is built from. ChanID lnwire.ChannelID // RHash is the payment hash for this HTLC. The HTLC can be settled iff @@ -170,7 +170,7 @@ type PaymentDescriptor struct { // removeCommitHeight[Remote|Local] encodes the height of the // commitment which removed the parent pointer of this - // PaymentDescriptor either due to a timeout or a settle. Once both + // paymentDescriptor either due to a timeout or a settle. Once both // these heights are below the tail of both chains, the log entries can // safely be removed. removeCommitHeightRemote uint64 @@ -199,7 +199,7 @@ type PaymentDescriptor struct { // [our|their|]PkScript are the raw public key scripts that encodes the // redemption rules for this particular HTLC. These fields will only be - // populated iff the EntryType of this PaymentDescriptor is Add. + // populated iff the EntryType of this paymentDescriptor is Add. // ourPkScript is the ourPkScript from the context of our local // commitment chain. theirPkScript is the latest pkScript from the // context of the remote commitment chain. @@ -212,7 +212,7 @@ type PaymentDescriptor struct { theirPkScript []byte theirWitnessScript []byte - // EntryType denotes the exact type of the PaymentDescriptor. In the + // EntryType denotes the exact type of the paymentDescriptor. In the // case of a Timeout, or Settle type, then the Parent field will point // into the log to the HTLC being modified. EntryType updateType @@ -232,11 +232,11 @@ type PaymentDescriptor struct { CustomRecords lnwire.CustomRecords } -// ToLogUpdate recovers the underlying LogUpdate from the paymentDescriptor. +// toLogUpdate recovers the underlying LogUpdate from the paymentDescriptor. // This operation is lossy and will forget some extra information tracked by the // paymentDescriptor but the function is total in that all paymentDescriptors // can be converted back to LogUpdates. -func (pd *PaymentDescriptor) ToLogUpdate() channeldb.LogUpdate { +func (pd *paymentDescriptor) toLogUpdate() channeldb.LogUpdate { var msg lnwire.Message switch pd.EntryType { case Add: diff --git a/lnwallet/update_log.go b/lnwallet/update_log.go index 5b9d6c565..42e5373a2 100644 --- a/lnwallet/update_log.go +++ b/lnwallet/update_log.go @@ -29,16 +29,16 @@ type updateLog struct { // List is the updatelog itself, we embed this value so updateLog has // access to all the method of a list.List. - *fn.List[*PaymentDescriptor] + *fn.List[*paymentDescriptor] // updateIndex maps a `logIndex` to a particular update entry. It // deals with the four update types: // `Fail|MalformedFail|Settle|FeeUpdate` - updateIndex map[uint64]*fn.Node[*PaymentDescriptor] + updateIndex map[uint64]*fn.Node[*paymentDescriptor] // htlcIndex maps a `htlcCounter` to an offered HTLC entry, hence the // `Add` update. - htlcIndex map[uint64]*fn.Node[*PaymentDescriptor] + htlcIndex map[uint64]*fn.Node[*paymentDescriptor] // modifiedHtlcs is a set that keeps track of all the current modified // htlcs, hence update types `Fail|MalformedFail|Settle`. A modified @@ -50,9 +50,9 @@ type updateLog struct { // newUpdateLog creates a new updateLog instance. func newUpdateLog(logIndex, htlcCounter uint64) *updateLog { return &updateLog{ - List: fn.NewList[*PaymentDescriptor](), - updateIndex: make(map[uint64]*fn.Node[*PaymentDescriptor]), - htlcIndex: make(map[uint64]*fn.Node[*PaymentDescriptor]), + List: fn.NewList[*paymentDescriptor](), + updateIndex: make(map[uint64]*fn.Node[*paymentDescriptor]), + htlcIndex: make(map[uint64]*fn.Node[*paymentDescriptor]), logIndex: logIndex, htlcCounter: htlcCounter, modifiedHtlcs: fn.NewSet[uint64](), @@ -64,7 +64,7 @@ func newUpdateLog(logIndex, htlcCounter uint64) *updateLog { // state. This function differs from appendHtlc in that it won't increment // either of log's counters. If the HTLC is already present, then it is // ignored. -func (u *updateLog) restoreHtlc(pd *PaymentDescriptor) { +func (u *updateLog) restoreHtlc(pd *paymentDescriptor) { if _, ok := u.htlcIndex[pd.HtlcIndex]; ok { return } @@ -74,7 +74,7 @@ func (u *updateLog) restoreHtlc(pd *PaymentDescriptor) { // appendUpdate appends a new update to the tip of the updateLog. The entry is // also added to index accordingly. -func (u *updateLog) appendUpdate(pd *PaymentDescriptor) { +func (u *updateLog) appendUpdate(pd *paymentDescriptor) { u.updateIndex[u.logIndex] = u.PushBack(pd) u.logIndex++ } @@ -82,13 +82,13 @@ func (u *updateLog) appendUpdate(pd *PaymentDescriptor) { // restoreUpdate appends a new update to the tip of the updateLog. The entry is // also added to index accordingly. This function differs from appendUpdate in // that it won't increment the log index counter. -func (u *updateLog) restoreUpdate(pd *PaymentDescriptor) { +func (u *updateLog) restoreUpdate(pd *paymentDescriptor) { u.updateIndex[pd.LogIndex] = u.PushBack(pd) } // appendHtlc appends a new HTLC offer to the tip of the update log. The entry // is also added to the offer index accordingly. -func (u *updateLog) appendHtlc(pd *PaymentDescriptor) { +func (u *updateLog) appendHtlc(pd *paymentDescriptor) { u.htlcIndex[u.htlcCounter] = u.PushBack(pd) u.htlcCounter++ @@ -97,7 +97,7 @@ func (u *updateLog) appendHtlc(pd *PaymentDescriptor) { // lookupHtlc attempts to look up an offered HTLC according to its offer // index. If the entry isn't found, then a nil pointer is returned. -func (u *updateLog) lookupHtlc(i uint64) *PaymentDescriptor { +func (u *updateLog) lookupHtlc(i uint64) *paymentDescriptor { htlc, ok := u.htlcIndex[i] if !ok { return nil @@ -145,7 +145,7 @@ func compactLogs(ourLog, theirLog *updateLog, localChainTail, remoteChainTail uint64) { compactLog := func(logA, logB *updateLog) { - var nextA *fn.Node[*PaymentDescriptor] + var nextA *fn.Node[*paymentDescriptor] for e := logA.Front(); e != nil; e = nextA { // Assign next iteration element at top of loop because // we may remove the current element from the list, From 93d17a48a8ac4a7bfac23c930e2e10b0566dc85e Mon Sep 17 00:00:00 2001 From: Keagan McClelland Date: Fri, 14 Jun 2024 17:45:36 -0700 Subject: [PATCH 18/18] lnwallet: remove unnecessary chanID argument form unsignedLocalUpdates --- lnwallet/channel.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lnwallet/channel.go b/lnwallet/channel.go index 0000026c2..32492171f 100644 --- a/lnwallet/channel.go +++ b/lnwallet/channel.go @@ -5324,7 +5324,6 @@ func (lc *LightningChannel) ReceiveRevocation(revMsg *lnwire.RevokeAndAck) ( localChainTail := lc.commitChains.Local.tail().height source := lc.ShortChanID() - chanID := lnwire.NewChanIDFromOutPoint(lc.channelState.FundingOutpoint) // Determine the set of htlcs that can be forwarded as a result of // having received the revocation. We will simultaneously construct the @@ -5426,7 +5425,7 @@ func (lc *LightningChannel) ReceiveRevocation(revMsg *lnwire.RevokeAndAck) ( localMessageIndex := lc.commitChains.Local.tail().messageIndices.Local localPeerUpdates := lc.unsignedLocalUpdates( - remoteMessageIndex, localMessageIndex, chanID, + remoteMessageIndex, localMessageIndex, ) // Now that we have gathered the set of HTLCs to forward, separated by @@ -8736,7 +8735,7 @@ func (lc *LightningChannel) FwdMinHtlc() lnwire.MilliSatoshi { // NOTE: remoteMessageIndex is the height on the tip because this is called // before the tail is advanced to the tip during ReceiveRevocation. func (lc *LightningChannel) unsignedLocalUpdates(remoteMessageIndex, - localMessageIndex uint64, chanID lnwire.ChannelID) []channeldb.LogUpdate { + localMessageIndex uint64) []channeldb.LogUpdate { var localPeerUpdates []channeldb.LogUpdate for e := lc.updateLogs.Local.Front(); e != nil; e = e.Next() {