github.com/decred/dcrlnd@v0.7.6/channeldb/migration/lnwire21/update_add_htlc.go (about)

     1  package lnwire
     2  
     3  import (
     4  	"io"
     5  )
     6  
     7  // OnionPacketSize is the size of the serialized Sphinx onion packet included
     8  // in each UpdateAddHTLC message. The breakdown of the onion packet is as
     9  // follows: 1-byte version, 33-byte ephemeral public key (for ECDH), 1300-bytes
    10  // of per-hop data, and a 32-byte HMAC over the entire packet.
    11  const OnionPacketSize = 1366
    12  
    13  // UpdateAddHTLC is the message sent by Alice to Bob when she wishes to add an
    14  // HTLC to his remote commitment transaction. In addition to information
    15  // detailing the value, the ID, expiry, and the onion blob is also included
    16  // which allows Bob to derive the next hop in the route. The HTLC added by this
    17  // message is to be added to the remote node's "pending" HTLC's.  A subsequent
    18  // CommitSig message will move the pending HTLC to the newly created commitment
    19  // transaction, marking them as "staged".
    20  type UpdateAddHTLC struct {
    21  	// ChanID is the particular active channel that this UpdateAddHTLC is
    22  	// bound to.
    23  	ChanID ChannelID
    24  
    25  	// ID is the identification server for this HTLC. This value is
    26  	// explicitly included as it allows nodes to survive single-sided
    27  	// restarts. The ID value for this sides starts at zero, and increases
    28  	// with each offered HTLC.
    29  	ID uint64
    30  
    31  	// Amount is the amount of MilliAtoms this HTLC is worth.
    32  	Amount MilliAtom
    33  
    34  	// PaymentHash is the payment hash to be included in the HTLC this
    35  	// request creates. The pre-image to this HTLC must be revealed by the
    36  	// upstream peer in order to fully settle the HTLC.
    37  	PaymentHash [32]byte
    38  
    39  	// Expiry is the number of blocks after which this HTLC should expire.
    40  	// It is the receiver's duty to ensure that the outgoing HTLC has a
    41  	// sufficient expiry value to allow her to redeem the incoming HTLC.
    42  	Expiry uint32
    43  
    44  	// OnionBlob is the raw serialized mix header used to route an HTLC in
    45  	// a privacy-preserving manner. The mix header is defined currently to
    46  	// be parsed as a 4-tuple: (groupElement, routingInfo, headerMAC,
    47  	// body).  First the receiving node should use the groupElement, and
    48  	// its current onion key to derive a shared secret with the source.
    49  	// Once the shared secret has been derived, the headerMAC should be
    50  	// checked FIRST. Note that the MAC only covers the routingInfo field.
    51  	// If the MAC matches, and the shared secret is fresh, then the node
    52  	// should strip off a layer of encryption, exposing the next hop to be
    53  	// used in the subsequent UpdateAddHTLC message.
    54  	OnionBlob [OnionPacketSize]byte
    55  }
    56  
    57  // NewUpdateAddHTLC returns a new empty UpdateAddHTLC message.
    58  func NewUpdateAddHTLC() *UpdateAddHTLC {
    59  	return &UpdateAddHTLC{}
    60  }
    61  
    62  // A compile time check to ensure UpdateAddHTLC implements the lnwire.Message
    63  // interface.
    64  var _ Message = (*UpdateAddHTLC)(nil)
    65  
    66  // Decode deserializes a serialized UpdateAddHTLC message stored in the passed
    67  // io.Reader observing the specified protocol version.
    68  //
    69  // This is part of the lnwire.Message interface.
    70  func (c *UpdateAddHTLC) Decode(r io.Reader, pver uint32) error {
    71  	return ReadElements(r,
    72  		&c.ChanID,
    73  		&c.ID,
    74  		&c.Amount,
    75  		c.PaymentHash[:],
    76  		&c.Expiry,
    77  		c.OnionBlob[:],
    78  	)
    79  }
    80  
    81  // Encode serializes the target UpdateAddHTLC into the passed io.Writer observing
    82  // the protocol version specified.
    83  //
    84  // This is part of the lnwire.Message interface.
    85  func (c *UpdateAddHTLC) Encode(w io.Writer, pver uint32) error {
    86  	return WriteElements(w,
    87  		c.ChanID,
    88  		c.ID,
    89  		c.Amount,
    90  		c.PaymentHash[:],
    91  		c.Expiry,
    92  		c.OnionBlob[:],
    93  	)
    94  }
    95  
    96  // MsgType returns the integer uniquely identifying this message type on the
    97  // wire.
    98  //
    99  // This is part of the lnwire.Message interface.
   100  func (c *UpdateAddHTLC) MsgType() MessageType {
   101  	return MsgUpdateAddHTLC
   102  }
   103  
   104  // MaxPayloadLength returns the maximum allowed payload size for an UpdateAddHTLC
   105  // complete message observing the specified protocol version.
   106  //
   107  // This is part of the lnwire.Message interface.
   108  func (c *UpdateAddHTLC) MaxPayloadLength(uint32) uint32 {
   109  	// 1450
   110  	return 32 + 8 + 4 + 8 + 32 + 1366
   111  }
   112  
   113  // TargetChanID returns the channel id of the link for which this message is
   114  // intended.
   115  //
   116  // NOTE: Part of peer.LinkUpdater interface.
   117  func (c *UpdateAddHTLC) TargetChanID() ChannelID {
   118  	return c.ChanID
   119  }