2017-05-01 18:58:08 +02:00
|
|
|
package htlcswitch
|
|
|
|
|
|
|
|
import (
|
2017-11-27 08:20:45 +01:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2019-09-05 13:35:39 +02:00
|
|
|
"github.com/lightningnetwork/lnd/htlcswitch/hop"
|
2017-05-01 18:58:08 +02:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2020-06-24 12:03:00 +02:00
|
|
|
"github.com/lightningnetwork/lnd/record"
|
2017-05-01 18:58:08 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// htlcPacket is a wrapper around htlc lnwire update, which adds additional
|
|
|
|
// information which is needed by this package.
|
|
|
|
type htlcPacket struct {
|
2017-10-30 19:56:51 +01:00
|
|
|
// incomingChanID is the ID of the channel that we have received an incoming
|
|
|
|
// HTLC on.
|
|
|
|
incomingChanID lnwire.ShortChannelID
|
2017-05-01 18:58:08 +02:00
|
|
|
|
2017-10-30 19:56:51 +01:00
|
|
|
// outgoingChanID is the ID of the channel that we have offered or will
|
|
|
|
// offer an outgoing HTLC on.
|
|
|
|
outgoingChanID lnwire.ShortChannelID
|
2017-05-01 18:58:08 +02:00
|
|
|
|
2017-10-30 19:56:51 +01:00
|
|
|
// incomingHTLCID is the ID of the HTLC that we have received from the peer
|
|
|
|
// on the incoming channel.
|
|
|
|
incomingHTLCID uint64
|
2017-10-24 00:50:26 +02:00
|
|
|
|
2017-10-30 19:56:51 +01:00
|
|
|
// outgoingHTLCID is the ID of the HTLC that we offered to the peer on the
|
|
|
|
// outgoing channel.
|
|
|
|
outgoingHTLCID uint64
|
2017-10-24 00:50:26 +02:00
|
|
|
|
2017-11-27 08:20:45 +01:00
|
|
|
// sourceRef is used by forwarded htlcPackets to locate incoming Add
|
|
|
|
// entry in a fwdpkg owned by the incoming link. This value can be nil
|
|
|
|
// if there is no such entry, e.g. switch initiated payments.
|
|
|
|
sourceRef *channeldb.AddRef
|
|
|
|
|
|
|
|
// destRef is used to locate a settle/fail entry in the outgoing link's
|
|
|
|
// fwdpkg. If sourceRef is non-nil, this reference should be to a
|
|
|
|
// settle/fail in response to the sourceRef.
|
|
|
|
destRef *channeldb.SettleFailRef
|
|
|
|
|
|
|
|
// incomingAmount is the value in milli-satoshis that arrived on an
|
|
|
|
// incoming link.
|
|
|
|
incomingAmount lnwire.MilliSatoshi
|
|
|
|
|
2017-06-16 23:49:38 +02:00
|
|
|
// amount is the value of the HTLC that is being created or modified.
|
2017-08-22 08:36:43 +02:00
|
|
|
amount lnwire.MilliSatoshi
|
2017-06-16 23:49:38 +02:00
|
|
|
|
|
|
|
// htlc lnwire message type of which depends on switch request type.
|
|
|
|
htlc lnwire.Message
|
2017-06-29 15:40:45 +02:00
|
|
|
|
2017-07-15 05:08:29 +02:00
|
|
|
// obfuscator contains the necessary state to allow the switch to wrap
|
|
|
|
// any forwarded errors in an additional layer of encryption.
|
2019-09-05 13:35:39 +02:00
|
|
|
obfuscator hop.ErrorEncrypter
|
2017-06-29 15:40:45 +02:00
|
|
|
|
2017-12-05 02:28:16 +01:00
|
|
|
// localFailure is set to true if an HTLC fails for a local payment before
|
|
|
|
// the first hop. In this case, the failure reason is simply encoded, not
|
|
|
|
// encrypted with any shared secret.
|
|
|
|
localFailure bool
|
|
|
|
|
2020-02-06 18:35:17 +01:00
|
|
|
// linkFailure is non-nil for htlcs that fail at our node. This may
|
|
|
|
// occur for our own payments which fail on the outgoing link,
|
|
|
|
// or for forwards which fail in the switch or on the outgoing link.
|
|
|
|
linkFailure *LinkError
|
|
|
|
|
2019-05-01 03:20:23 +02:00
|
|
|
// convertedError is set to true if this is an HTLC fail that was
|
|
|
|
// created using an UpdateFailMalformedHTLC from the remote party. If
|
|
|
|
// this is true, then when forwarding this failure packet, we'll need
|
|
|
|
// to wrap it as if we were the first hop if it's a multi-hop HTLC. If
|
|
|
|
// it's a direct HTLC, then we'll decode the error as no encryption has
|
|
|
|
// taken place.
|
|
|
|
convertedError bool
|
|
|
|
|
2017-11-27 08:20:45 +01:00
|
|
|
// hasSource is set to true if the incomingChanID and incomingHTLCID
|
|
|
|
// fields of a forwarded fail packet are already set and do not need to
|
|
|
|
// be looked up in the circuit map.
|
|
|
|
hasSource bool
|
2018-01-17 05:11:01 +01:00
|
|
|
|
|
|
|
// isResolution is set to true if this packet was actually an incoming
|
|
|
|
// resolution message from an outside sub-system. We'll treat these as
|
|
|
|
// if they emanated directly from the switch. As a result, we'll
|
|
|
|
// encrypt all errors related to this packet as if we were the first
|
|
|
|
// hop.
|
|
|
|
isResolution bool
|
2017-11-27 08:20:45 +01:00
|
|
|
|
|
|
|
// circuit holds a reference to an Add's circuit which is persisted in
|
|
|
|
// the switch during successful forwarding.
|
|
|
|
circuit *PaymentCircuit
|
2018-06-26 05:25:21 +02:00
|
|
|
|
|
|
|
// incomingTimeout is the timeout that the incoming HTLC carried. This
|
|
|
|
// is the timeout of the HTLC applied to the incoming link.
|
|
|
|
incomingTimeout uint32
|
|
|
|
|
|
|
|
// outgoingTimeout is the timeout of the proposed outgoing HTLC. This
|
2022-01-13 17:29:43 +01:00
|
|
|
// will be extracted from the hop payload received by the incoming
|
2018-06-26 05:25:21 +02:00
|
|
|
// link.
|
|
|
|
outgoingTimeout uint32
|
2020-06-24 12:03:00 +02:00
|
|
|
|
|
|
|
// customRecords are user-defined records in the custom type range that
|
|
|
|
// were included in the payload.
|
|
|
|
customRecords record.CustomSet
|
server+htlcswitch: prevent privacy leaks, allow alias routing
This intent of this change is to prevent privacy leaks when routing
with aliases and also to allow routing when using an alias. The
aliases are our aliases.
Introduces are two maps:
* aliasToReal:
This is an N->1 mapping for a channel. The keys are the set of
aliases and the value is the confirmed, on-chain SCID.
* baseIndex:
This is also an N->1 mapping for a channel. The keys are the set
of aliases and the value is the "base" SCID (whatever is in the
OpenChannel.ShortChannelID field). There is also a base->base
mapping, so not all keys are aliases.
The above maps are populated when a link is added to the switch and
when the channel has confirmed on-chain. The maps are not removed
from if the link is removed, but this is fine since forwarding won't
occur.
* getLinkByMapping
This function is introduced to adhere to the spec requirements that
using the confirmed SCID of a private, scid-alias-feature-bit
channel does not work. Lnd implements a stricter version of the spec
and disallows this behavior if the feature-bit was negotiated, rather
than just the channel type. The old, privacy-leak behavior is
preserved.
The spec also requires that if we must fail back an HTLC, the
ChannelUpdate must use the SCID of whatever was in the onion, to avoid
a privacy leak. This is also done by passing in the relevant SCID to
the mailbox and link. Lnd will also cancel back on the "incoming" side
if the InterceptableSwitch was used or if the link failed to decrypt
the onion. In this case, we are cautious and replace the SCID if an
alias exists.
2022-04-04 22:44:51 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
|
// but receives a channel_update with the alias SCID. Instead, the
|
|
|
|
// payer should receive a channel_update with the public SCID.
|
|
|
|
originalOutgoingChanID lnwire.ShortChannelID
|
2017-11-27 08:20:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// inKey returns the circuit key used to identify the incoming htlc.
|
|
|
|
func (p *htlcPacket) inKey() CircuitKey {
|
|
|
|
return CircuitKey{
|
|
|
|
ChanID: p.incomingChanID,
|
|
|
|
HtlcID: p.incomingHTLCID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// outKey returns the circuit key used to identify the outgoing, forwarded htlc.
|
|
|
|
func (p *htlcPacket) outKey() CircuitKey {
|
|
|
|
return CircuitKey{
|
|
|
|
ChanID: p.outgoingChanID,
|
|
|
|
HtlcID: p.outgoingHTLCID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// keystone returns a tuple containing the incoming and outgoing circuit keys.
|
|
|
|
func (p *htlcPacket) keystone() Keystone {
|
|
|
|
return Keystone{
|
|
|
|
InKey: p.inKey(),
|
|
|
|
OutKey: p.outKey(),
|
|
|
|
}
|
2017-05-01 18:58:08 +02:00
|
|
|
}
|