• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 9915780197

13 Jul 2024 12:30AM UTC coverage: 49.268% (-9.1%) from 58.413%
9915780197

push

github

web-flow
Merge pull request #8653 from ProofOfKeags/fn-prim

DynComms [0/n]: `fn` package additions

92837 of 188433 relevant lines covered (49.27%)

1.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

67.19
/lnwire/update_add_htlc.go
1
package lnwire
2

3
import (
4
        "bytes"
5
        "io"
6

7
        "github.com/btcsuite/btcd/btcec/v2"
8
        "github.com/lightningnetwork/lnd/tlv"
9
)
10

11
// OnionPacketSize is the size of the serialized Sphinx onion packet included
12
// in each UpdateAddHTLC message. The breakdown of the onion packet is as
13
// follows: 1-byte version, 33-byte ephemeral public key (for ECDH), 1300-bytes
14
// of per-hop data, and a 32-byte HMAC over the entire packet.
15
const OnionPacketSize = 1366
16

17
type (
18
        // BlindingPointTlvType is the type for ephemeral pubkeys used in
19
        // route blinding.
20
        BlindingPointTlvType = tlv.TlvType0
21

22
        // BlindingPointRecord holds an optional blinding point on update add
23
        // htlc.
24
        //nolint:lll
25
        BlindingPointRecord = tlv.OptionalRecordT[BlindingPointTlvType, *btcec.PublicKey]
26
)
27

28
// UpdateAddHTLC is the message sent by Alice to Bob when she wishes to add an
29
// HTLC to his remote commitment transaction. In addition to information
30
// detailing the value, the ID, expiry, and the onion blob is also included
31
// which allows Bob to derive the next hop in the route. The HTLC added by this
32
// message is to be added to the remote node's "pending" HTLCs.  A subsequent
33
// CommitSig message will move the pending HTLC to the newly created commitment
34
// transaction, marking them as "staged".
35
type UpdateAddHTLC struct {
36
        // ChanID is the particular active channel that this UpdateAddHTLC is
37
        // bound to.
38
        ChanID ChannelID
39

40
        // ID is the identification server for this HTLC. This value is
41
        // explicitly included as it allows nodes to survive single-sided
42
        // restarts. The ID value for this sides starts at zero, and increases
43
        // with each offered HTLC.
44
        ID uint64
45

46
        // Amount is the amount of millisatoshis this HTLC is worth.
47
        Amount MilliSatoshi
48

49
        // PaymentHash is the payment hash to be included in the HTLC this
50
        // request creates. The pre-image to this HTLC must be revealed by the
51
        // upstream peer in order to fully settle the HTLC.
52
        PaymentHash [32]byte
53

54
        // Expiry is the number of blocks after which this HTLC should expire.
55
        // It is the receiver's duty to ensure that the outgoing HTLC has a
56
        // sufficient expiry value to allow her to redeem the incoming HTLC.
57
        Expiry uint32
58

59
        // OnionBlob is the raw serialized mix header used to route an HTLC in
60
        // a privacy-preserving manner. The mix header is defined currently to
61
        // be parsed as a 4-tuple: (groupElement, routingInfo, headerMAC,
62
        // body).  First the receiving node should use the groupElement, and
63
        // its current onion key to derive a shared secret with the source.
64
        // Once the shared secret has been derived, the headerMAC should be
65
        // checked FIRST. Note that the MAC only covers the routingInfo field.
66
        // If the MAC matches, and the shared secret is fresh, then the node
67
        // should strip off a layer of encryption, exposing the next hop to be
68
        // used in the subsequent UpdateAddHTLC message.
69
        OnionBlob [OnionPacketSize]byte
70

71
        // BlindingPoint is the ephemeral pubkey used to optionally blind the
72
        // next hop for this htlc.
73
        BlindingPoint BlindingPointRecord
74

75
        // ExtraData is the set of data that was appended to this message to
76
        // fill out the full maximum transport message size. These fields can
77
        // be used to specify optional data such as custom TLV fields.
78
        ExtraData ExtraOpaqueData
79
}
80

81
// NewUpdateAddHTLC returns a new empty UpdateAddHTLC message.
82
func NewUpdateAddHTLC() *UpdateAddHTLC {
×
83
        return &UpdateAddHTLC{}
×
84
}
×
85

86
// A compile time check to ensure UpdateAddHTLC implements the lnwire.Message
87
// interface.
88
var _ Message = (*UpdateAddHTLC)(nil)
89

90
// Decode deserializes a serialized UpdateAddHTLC message stored in the passed
91
// io.Reader observing the specified protocol version.
92
//
93
// This is part of the lnwire.Message interface.
94
func (c *UpdateAddHTLC) Decode(r io.Reader, pver uint32) error {
3✔
95
        if err := ReadElements(r,
3✔
96
                &c.ChanID,
3✔
97
                &c.ID,
3✔
98
                &c.Amount,
3✔
99
                c.PaymentHash[:],
3✔
100
                &c.Expiry,
3✔
101
                c.OnionBlob[:],
3✔
102
                &c.ExtraData,
3✔
103
        ); err != nil {
3✔
104
                return err
×
105
        }
×
106

107
        blindingRecord := c.BlindingPoint.Zero()
3✔
108
        tlvMap, err := c.ExtraData.ExtractRecords(&blindingRecord)
3✔
109
        if err != nil {
3✔
110
                return err
×
111
        }
×
112

113
        if val, ok := tlvMap[c.BlindingPoint.TlvType()]; ok && val == nil {
6✔
114
                c.BlindingPoint = tlv.SomeRecordT(blindingRecord)
3✔
115
        }
3✔
116

117
        // Set extra data to nil if we didn't parse anything out of it so that
118
        // we can use assert.Equal in tests.
119
        if len(tlvMap) == 0 {
6✔
120
                c.ExtraData = nil
3✔
121
        }
3✔
122

123
        return nil
3✔
124
}
125

126
// Encode serializes the target UpdateAddHTLC into the passed io.Writer
127
// observing the protocol version specified.
128
//
129
// This is part of the lnwire.Message interface.
130
func (c *UpdateAddHTLC) Encode(w *bytes.Buffer, pver uint32) error {
3✔
131
        if err := WriteChannelID(w, c.ChanID); err != nil {
3✔
132
                return err
×
133
        }
×
134

135
        if err := WriteUint64(w, c.ID); err != nil {
3✔
136
                return err
×
137
        }
×
138

139
        if err := WriteMilliSatoshi(w, c.Amount); err != nil {
3✔
140
                return err
×
141
        }
×
142

143
        if err := WriteBytes(w, c.PaymentHash[:]); err != nil {
3✔
144
                return err
×
145
        }
×
146

147
        if err := WriteUint32(w, c.Expiry); err != nil {
3✔
148
                return err
×
149
        }
×
150

151
        if err := WriteBytes(w, c.OnionBlob[:]); err != nil {
3✔
152
                return err
×
153
        }
×
154

155
        // Only include blinding point in extra data if present.
156
        var records []tlv.RecordProducer
3✔
157

3✔
158
        c.BlindingPoint.WhenSome(func(b tlv.RecordT[BlindingPointTlvType,
3✔
159
                *btcec.PublicKey]) {
6✔
160

3✔
161
                records = append(records, &b)
3✔
162
        })
3✔
163

164
        err := EncodeMessageExtraData(&c.ExtraData, records...)
3✔
165
        if err != nil {
3✔
166
                return err
×
167
        }
×
168

169
        return WriteBytes(w, c.ExtraData)
3✔
170
}
171

172
// MsgType returns the integer uniquely identifying this message type on the
173
// wire.
174
//
175
// This is part of the lnwire.Message interface.
176
func (c *UpdateAddHTLC) MsgType() MessageType {
3✔
177
        return MsgUpdateAddHTLC
3✔
178
}
3✔
179

180
// TargetChanID returns the channel id of the link for which this message is
181
// intended.
182
//
183
// NOTE: Part of peer.LinkUpdater interface.
184
func (c *UpdateAddHTLC) TargetChanID() ChannelID {
3✔
185
        return c.ChanID
3✔
186
}
3✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc