github.com/decred/dcrlnd@v0.7.6/htlcswitch/packet.go (about)

     1  package htlcswitch
     2  
     3  import (
     4  	"github.com/decred/dcrlnd/channeldb"
     5  	"github.com/decred/dcrlnd/htlcswitch/hop"
     6  	"github.com/decred/dcrlnd/lnwire"
     7  	"github.com/decred/dcrlnd/record"
     8  )
     9  
    10  // htlcPacket is a wrapper around htlc lnwire update, which adds additional
    11  // information which is needed by this package.
    12  type htlcPacket struct {
    13  	// incomingChanID is the ID of the channel that we have received an incoming
    14  	// HTLC on.
    15  	incomingChanID lnwire.ShortChannelID
    16  
    17  	// outgoingChanID is the ID of the channel that we have offered or will
    18  	// offer an outgoing HTLC on.
    19  	outgoingChanID lnwire.ShortChannelID
    20  
    21  	// incomingHTLCID is the ID of the HTLC that we have received from the peer
    22  	// on the incoming channel.
    23  	incomingHTLCID uint64
    24  
    25  	// outgoingHTLCID is the ID of the HTLC that we offered to the peer on
    26  	// the outgoing channel.
    27  	outgoingHTLCID uint64
    28  
    29  	// sourceRef is used by forwarded htlcPackets to locate incoming Add
    30  	// entry in a fwdpkg owned by the incoming link. This value can be nil
    31  	// if there is no such entry, e.g. switch initiated payments.
    32  	sourceRef *channeldb.AddRef
    33  
    34  	// destRef is used to locate a settle/fail entry in the outgoing link's
    35  	// fwdpkg. If sourceRef is non-nil, this reference should be to a
    36  	// settle/fail in response to the sourceRef.
    37  	destRef *channeldb.SettleFailRef
    38  
    39  	// incomingAmount is the value in milli-atoms that arrived on an
    40  	// incoming link.
    41  	incomingAmount lnwire.MilliAtom
    42  
    43  	// amount is the value of the HTLC that is being created or modified.
    44  	amount lnwire.MilliAtom
    45  
    46  	// htlc lnwire message type of which depends on switch request type.
    47  	htlc lnwire.Message
    48  
    49  	// obfuscator contains the necessary state to allow the switch to wrap
    50  	// any forwarded errors in an additional layer of encryption.
    51  	obfuscator hop.ErrorEncrypter
    52  
    53  	// localFailure is set to true if an HTLC fails for a local payment before
    54  	// the first hop. In this case, the failure reason is simply encoded, not
    55  	// encrypted with any shared secret.
    56  	localFailure bool
    57  
    58  	// linkFailure is non-nil for htlcs that fail at our node. This may
    59  	// occur for our own payments which fail on the outgoing link,
    60  	// or for forwards which fail in the switch or on the outgoing link.
    61  	linkFailure *LinkError
    62  
    63  	// convertedError is set to true if this is an HTLC fail that was
    64  	// created using an UpdateFailMalformedHTLC from the remote party. If
    65  	// this is true, then when forwarding this failure packet, we'll need
    66  	// to wrap it as if we were the first hop if it's a multi-hop HTLC. If
    67  	// it's a direct HTLC, then we'll decode the error as no encryption has
    68  	// taken place.
    69  	convertedError bool
    70  
    71  	// hasSource is set to true if the incomingChanID and incomingHTLCID
    72  	// fields of a forwarded fail packet are already set and do not need to
    73  	// be looked up in the circuit map.
    74  	hasSource bool
    75  
    76  	// isResolution is set to true if this packet was actually an incoming
    77  	// resolution message from an outside sub-system. We'll treat these as
    78  	// if they emanated directly from the switch. As a result, we'll
    79  	// encrypt all errors related to this packet as if we were the first
    80  	// hop.
    81  	isResolution bool
    82  
    83  	// circuit holds a reference to an Add's circuit which is persisted in
    84  	// the switch during successful forwarding.
    85  	circuit *PaymentCircuit
    86  
    87  	// incomingTimeout is the timeout that the incoming HTLC carried. This
    88  	// is the timeout of the HTLC applied to the incoming link.
    89  	incomingTimeout uint32
    90  
    91  	// outgoingTimeout is the timeout of the proposed outgoing HTLC. This
    92  	// will be extraced from the hop payload recevived by the incoming
    93  	// link.
    94  	outgoingTimeout uint32
    95  
    96  	// customRecords are user-defined records in the custom type range that
    97  	// were included in the payload.
    98  	customRecords record.CustomSet
    99  }
   100  
   101  // inKey returns the circuit key used to identify the incoming htlc.
   102  func (p *htlcPacket) inKey() CircuitKey {
   103  	return CircuitKey{
   104  		ChanID: p.incomingChanID,
   105  		HtlcID: p.incomingHTLCID,
   106  	}
   107  }
   108  
   109  // outKey returns the circuit key used to identify the outgoing, forwarded htlc.
   110  func (p *htlcPacket) outKey() CircuitKey {
   111  	return CircuitKey{
   112  		ChanID: p.outgoingChanID,
   113  		HtlcID: p.outgoingHTLCID,
   114  	}
   115  }
   116  
   117  // keystone returns a tuple containing the incoming and outgoing circuit keys.
   118  func (p *htlcPacket) keystone() Keystone {
   119  	return Keystone{
   120  		InKey:  p.inKey(),
   121  		OutKey: p.outKey(),
   122  	}
   123  }