Skip to content

Commit

Permalink
htlcswitch+lnwallet: persist peer custom records
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeTsagk committed Apr 17, 2024
1 parent f2bfde2 commit a0f19a5
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 25 deletions.
34 changes: 25 additions & 9 deletions htlcswitch/interceptable_switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,21 @@ func (s *InterceptableSwitch) interceptForward(packet *htlcPacket,
return false, nil
}

var customRecordsBlob []byte

htlc.CustomRecordsBlob.WhenSome(
func(b tlv.RecordT[lnwire.CustomRecordsBlobTlvType,
[]byte]) {

customRecordsBlob = b.Val
},
)

customPeerRecords := new(record.CustomSet)
customPeerRecords.Decode(bytes.NewReader(customRecordsBlob))

packet.wireRecords = *customPeerRecords

intercepted := &interceptedForward{
htlc: htlc,
packet: packet,
Expand Down Expand Up @@ -619,15 +634,16 @@ func (f *interceptedForward) Packet() InterceptedPacket {
ChanID: f.packet.incomingChanID,
HtlcID: f.packet.incomingHTLCID,
},
OutgoingChanID: f.packet.outgoingChanID,
Hash: f.htlc.PaymentHash,
OutgoingExpiry: f.htlc.Expiry,
OutgoingAmount: f.htlc.Amount,
IncomingAmount: f.packet.incomingAmount,
IncomingExpiry: f.packet.incomingTimeout,
CustomRecords: f.packet.customRecords,
OnionBlob: f.htlc.OnionBlob,
AutoFailHeight: f.autoFailHeight,
OutgoingChanID: f.packet.outgoingChanID,
Hash: f.htlc.PaymentHash,
OutgoingExpiry: f.htlc.Expiry,
OutgoingAmount: f.htlc.Amount,
IncomingAmount: f.packet.incomingAmount,
IncomingExpiry: f.packet.incomingTimeout,
CustomRecords: f.packet.customRecords,
OnionBlob: f.htlc.OnionBlob,
AutoFailHeight: f.autoFailHeight,
CustomPeerRecords: f.packet.wireRecords,
}
}

Expand Down
4 changes: 4 additions & 0 deletions htlcswitch/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,10 @@ type InterceptedPacket struct {
// OnionBlob is the onion packet for the next hop
OnionBlob [lnwire.OnionPacketSize]byte

// CustomPeerRecords are user-defined records that were defined by the
// peer that forwarded this htlc to us.
CustomPeerRecords record.CustomSet

// AutoFailHeight is the block height at which this intercept will be
// failed back automatically.
AutoFailHeight int32
Expand Down
18 changes: 10 additions & 8 deletions htlcswitch/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -3369,10 +3369,11 @@ func (l *channelLink) processRemoteAdds(fwdPkg *channeldb.FwdPkg,
// Otherwise, it was already processed, we can
// can collect it and continue.
addMsg := &lnwire.UpdateAddHTLC{
Expiry: fwdInfo.OutgoingCTLV,
Amount: fwdInfo.AmountToForward,
PaymentHash: pd.RHash,
BlindingPoint: fwdInfo.NextBlinding,
Expiry: fwdInfo.OutgoingCTLV,
Amount: fwdInfo.AmountToForward,
PaymentHash: pd.RHash,
BlindingPoint: fwdInfo.NextBlinding,
CustomRecordsBlob: pd.WireRecordsBlob,
}

// Finally, we'll encode the onion packet for
Expand Down Expand Up @@ -3415,10 +3416,11 @@ func (l *channelLink) processRemoteAdds(fwdPkg *channeldb.FwdPkg,
// create the outgoing HTLC using the parameters as
// specified in the forwarding info.
addMsg := &lnwire.UpdateAddHTLC{
Expiry: fwdInfo.OutgoingCTLV,
Amount: fwdInfo.AmountToForward,
PaymentHash: pd.RHash,
BlindingPoint: fwdInfo.NextBlinding,
Expiry: fwdInfo.OutgoingCTLV,
Amount: fwdInfo.AmountToForward,
PaymentHash: pd.RHash,
BlindingPoint: fwdInfo.NextBlinding,
CustomRecordsBlob: pd.WireRecordsBlob,
}

// Finally, we'll encode the onion packet for the
Expand Down
4 changes: 4 additions & 0 deletions htlcswitch/packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ type htlcPacket struct {
// were included in the payload.
customRecords record.CustomSet

// wireRecords are user-defined records in the custom type range that
// were included in the peer's wire message.
wireRecords record.CustomSet

// originalOutgoingChanID is used when sending back failure messages.
// It is only used for forwarded Adds on option_scid_alias channels.
// This is to avoid possible confusion if a payer uses the public SCID
Expand Down
21 changes: 13 additions & 8 deletions lnwallet/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,10 @@ type PaymentDescriptor struct {
// blinded route (ie, not the introduction node) from update_add_htlc's
// TLVs.
BlindingPoint lnwire.BlindingPointRecord

// WireRecordsBlob contains the TLV records blob that was included in
// the original wire message that added this HTLC.
WireRecordsBlob lnwire.CustomRecordsBlob
}

// PayDescsFromRemoteLogUpdates converts a slice of LogUpdates received from the
Expand Down Expand Up @@ -6142,14 +6146,15 @@ func (lc *LightningChannel) ReceiveHTLC(htlc *lnwire.UpdateAddHTLC) (uint64, err
}

pd := &PaymentDescriptor{
EntryType: Add,
RHash: PaymentHash(htlc.PaymentHash),
Timeout: htlc.Expiry,
Amount: htlc.Amount,
LogIndex: lc.remoteUpdateLog.logIndex,
HtlcIndex: lc.remoteUpdateLog.htlcCounter,
OnionBlob: htlc.OnionBlob[:],
BlindingPoint: htlc.BlindingPoint,
EntryType: Add,
RHash: PaymentHash(htlc.PaymentHash),
Timeout: htlc.Expiry,
Amount: htlc.Amount,
LogIndex: lc.remoteUpdateLog.logIndex,
HtlcIndex: lc.remoteUpdateLog.htlcCounter,
OnionBlob: htlc.OnionBlob[:],
BlindingPoint: htlc.BlindingPoint,
WireRecordsBlob: htlc.CustomRecordsBlob,
}

localACKedIndex := lc.remoteCommitChain.tail().ourMessageIndex
Expand Down

0 comments on commit a0f19a5

Please sign in to comment.