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

lightningnetwork / lnd / 11292787765

11 Oct 2024 12:58PM UTC coverage: 49.179% (-9.5%) from 58.716%
11292787765

push

github

web-flow
Merge pull request #9168 from feelancer21/fix-lncli-wallet-proto

lnrpc: fix lncli documentation tags in walletkit.proto

97369 of 197987 relevant lines covered (49.18%)

1.04 hits per line

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

62.71
/lnwallet/payment_descriptor.go
1
package lnwallet
2

3
import (
4
        "crypto/sha256"
5

6
        "github.com/lightningnetwork/lnd/channeldb"
7
        "github.com/lightningnetwork/lnd/channeldb/models"
8
        "github.com/lightningnetwork/lnd/input"
9
        "github.com/lightningnetwork/lnd/lnwire"
10
)
11

12
// updateType is the exact type of an entry within the shared HTLC log.
13
type updateType uint8
14

15
const (
16
        // Add is an update type that adds a new HTLC entry into the log.
17
        // Either side can add a new pending HTLC by adding a new Add entry
18
        // into their update log.
19
        Add updateType = iota
20

21
        // Fail is an update type which removes a prior HTLC entry from the
22
        // log. Adding a Fail entry to one's log will modify the _remote_
23
        // party's update log once a new commitment view has been evaluated
24
        // which contains the Fail entry.
25
        Fail
26

27
        // MalformedFail is an update type which removes a prior HTLC entry
28
        // from the log. Adding a MalformedFail entry to one's log will modify
29
        // the _remote_ party's update log once a new commitment view has been
30
        // evaluated which contains the MalformedFail entry. The difference
31
        // from Fail type lie in the different data we have to store.
32
        MalformedFail
33

34
        // Settle is an update type which settles a prior HTLC crediting the
35
        // balance of the receiving node. Adding a Settle entry to a log will
36
        // result in the settle entry being removed on the log as well as the
37
        // original add entry from the remote party's log after the next state
38
        // transition.
39
        Settle
40

41
        // FeeUpdate is an update type sent by the channel initiator that
42
        // updates the fee rate used when signing the commitment transaction.
43
        FeeUpdate
44
)
45

46
// String returns a human readable string that uniquely identifies the target
47
// update type.
48
func (u updateType) String() string {
×
49
        switch u {
×
50
        case Add:
×
51
                return "Add"
×
52
        case Fail:
×
53
                return "Fail"
×
54
        case MalformedFail:
×
55
                return "MalformedFail"
×
56
        case Settle:
×
57
                return "Settle"
×
58
        case FeeUpdate:
×
59
                return "FeeUpdate"
×
60
        default:
×
61
                return "<unknown type>"
×
62
        }
63
}
64

65
// paymentDescriptor represents a commitment state update which either adds,
66
// settles, or removes an HTLC. paymentDescriptors encapsulate all necessary
67
// metadata w.r.t to an HTLC, and additional data pairing a settle message to
68
// the original added HTLC.
69
//
70
// TODO(roasbeef): LogEntry interface??
71
//   - need to separate attrs for cancel/add/settle/feeupdate
72
type paymentDescriptor struct {
73
        // ChanID is the ChannelID of the LightningChannel that this
74
        // paymentDescriptor belongs to. We track this here so we can
75
        // reconstruct the Messages that this paymentDescriptor is built from.
76
        ChanID lnwire.ChannelID
77

78
        // RHash is the payment hash for this HTLC. The HTLC can be settled iff
79
        // the preimage to this hash is presented.
80
        RHash PaymentHash
81

82
        // RPreimage is the preimage that settles the HTLC pointed to within the
83
        // log by the ParentIndex.
84
        RPreimage PaymentHash
85

86
        // Timeout is the absolute timeout in blocks, after which this HTLC
87
        // expires.
88
        Timeout uint32
89

90
        // Amount is the HTLC amount in milli-satoshis.
91
        Amount lnwire.MilliSatoshi
92

93
        // LogIndex is the log entry number that his HTLC update has within the
94
        // log. Depending on if IsIncoming is true, this is either an entry the
95
        // remote party added, or one that we added locally.
96
        LogIndex uint64
97

98
        // HtlcIndex is the index within the main update log for this HTLC.
99
        // Entries within the log of type Add will have this field populated,
100
        // as other entries will point to the entry via this counter.
101
        //
102
        // NOTE: This field will only be populate if EntryType is Add.
103
        HtlcIndex uint64
104

105
        // ParentIndex is the HTLC index of the entry that this update settles
106
        // or times out.
107
        //
108
        // NOTE: This field will only be populate if EntryType is Fail or
109
        // Settle.
110
        ParentIndex uint64
111

112
        // SourceRef points to an Add update in a forwarding package owned by
113
        // this channel.
114
        //
115
        // NOTE: This field will only be populated if EntryType is Fail or
116
        // Settle.
117
        SourceRef *channeldb.AddRef
118

119
        // DestRef points to a Fail/Settle update in another link's forwarding
120
        // package.
121
        //
122
        // NOTE: This field will only be populated if EntryType is Fail or
123
        // Settle, and the forwarded Add successfully included in an outgoing
124
        // link's commitment txn.
125
        DestRef *channeldb.SettleFailRef
126

127
        // OpenCircuitKey references the incoming Chan/HTLC ID of an Add HTLC
128
        // packet delivered by the switch.
129
        //
130
        // NOTE: This field is only populated for payment descriptors in the
131
        // *local* update log, and if the Add packet was delivered by the
132
        // switch.
133
        OpenCircuitKey *models.CircuitKey
134

135
        // ClosedCircuitKey references the incoming Chan/HTLC ID of the Add HTLC
136
        // that opened the circuit.
137
        //
138
        // NOTE: This field is only populated for payment descriptors in the
139
        // *local* update log, and if settle/fails have a committed circuit in
140
        // the circuit map.
141
        ClosedCircuitKey *models.CircuitKey
142

143
        // localOutputIndex is the output index of this HTLc output in the
144
        // commitment transaction of the local node.
145
        //
146
        // NOTE: If the output is dust from the PoV of the local commitment
147
        // chain, then this value will be -1.
148
        localOutputIndex int32
149

150
        // remoteOutputIndex is the output index of this HTLC output in the
151
        // commitment transaction of the remote node.
152
        //
153
        // NOTE: If the output is dust from the PoV of the remote commitment
154
        // chain, then this value will be -1.
155
        remoteOutputIndex int32
156

157
        // sig is the signature for the second-level HTLC transaction that
158
        // spends the version of this HTLC on the commitment transaction of the
159
        // local node. This signature is generated by the remote node and
160
        // stored by the local node in the case that local node needs to
161
        // broadcast their commitment transaction.
162
        sig input.Signature
163

164
        // addCommitHeight[Remote|Local] encodes the height of the commitment
165
        // which included this HTLC on either the remote or local commitment
166
        // chain. This value is used to determine when an HTLC is fully
167
        // "locked-in".
168
        addCommitHeightRemote uint64
169
        addCommitHeightLocal  uint64
170

171
        // removeCommitHeight[Remote|Local] encodes the height of the
172
        // commitment which removed the parent pointer of this
173
        // paymentDescriptor either due to a timeout or a settle. Once both
174
        // these heights are below the tail of both chains, the log entries can
175
        // safely be removed.
176
        removeCommitHeightRemote uint64
177
        removeCommitHeightLocal  uint64
178

179
        // OnionBlob is an opaque blob which is used to complete multi-hop
180
        // routing.
181
        //
182
        // NOTE: Populated only on add payment descriptor entry types.
183
        OnionBlob [lnwire.OnionPacketSize]byte
184

185
        // ShaOnionBlob is a sha of the onion blob.
186
        //
187
        // NOTE: Populated only in payment descriptor with MalformedFail type.
188
        ShaOnionBlob [sha256.Size]byte
189

190
        // FailReason stores the reason why a particular payment was canceled.
191
        //
192
        // NOTE: Populate only in fail payment descriptor entry types.
193
        FailReason []byte
194

195
        // FailCode stores the code why a particular payment was canceled.
196
        //
197
        // NOTE: Populated only in payment descriptor with MalformedFail type.
198
        FailCode lnwire.FailCode
199

200
        // [our|their|]PkScript are the raw public key scripts that encodes the
201
        // redemption rules for this particular HTLC. These fields will only be
202
        // populated iff the EntryType of this paymentDescriptor is Add.
203
        // ourPkScript is the ourPkScript from the context of our local
204
        // commitment chain. theirPkScript is the latest pkScript from the
205
        // context of the remote commitment chain.
206
        //
207
        // NOTE: These values may change within the logs themselves, however,
208
        // they'll stay consistent within the commitment chain entries
209
        // themselves.
210
        ourPkScript        []byte
211
        ourWitnessScript   []byte
212
        theirPkScript      []byte
213
        theirWitnessScript []byte
214

215
        // EntryType denotes the exact type of the paymentDescriptor. In the
216
        // case of a Timeout, or Settle type, then the Parent field will point
217
        // into the log to the HTLC being modified.
218
        EntryType updateType
219

220
        // isForwarded denotes if an incoming HTLC has been forwarded to any
221
        // possible upstream peers in the route.
222
        isForwarded bool
223

224
        // BlindingPoint is an optional ephemeral key used in route blinding.
225
        // This value is set for nodes that are relaying payments inside of a
226
        // blinded route (ie, not the introduction node) from update_add_htlc's
227
        // TLVs.
228
        BlindingPoint lnwire.BlindingPointRecord
229

230
        // CustomRecords also stores the set of optional custom records that
231
        // may have been attached to a sent HTLC.
232
        CustomRecords lnwire.CustomRecords
233
}
234

235
// toLogUpdate recovers the underlying LogUpdate from the paymentDescriptor.
236
// This operation is lossy and will forget some extra information tracked by the
237
// paymentDescriptor but the function is total in that all paymentDescriptors
238
// can be converted back to LogUpdates.
239
func (pd *paymentDescriptor) toLogUpdate() channeldb.LogUpdate {
2✔
240
        var msg lnwire.Message
2✔
241
        switch pd.EntryType {
2✔
242
        case Add:
2✔
243
                msg = &lnwire.UpdateAddHTLC{
2✔
244
                        ChanID:        pd.ChanID,
2✔
245
                        ID:            pd.HtlcIndex,
2✔
246
                        Amount:        pd.Amount,
2✔
247
                        PaymentHash:   pd.RHash,
2✔
248
                        Expiry:        pd.Timeout,
2✔
249
                        OnionBlob:     pd.OnionBlob,
2✔
250
                        BlindingPoint: pd.BlindingPoint,
2✔
251
                        CustomRecords: pd.CustomRecords.Copy(),
2✔
252
                }
2✔
253
        case Settle:
2✔
254
                msg = &lnwire.UpdateFulfillHTLC{
2✔
255
                        ChanID:          pd.ChanID,
2✔
256
                        ID:              pd.ParentIndex,
2✔
257
                        PaymentPreimage: pd.RPreimage,
2✔
258
                }
2✔
259
        case Fail:
2✔
260
                msg = &lnwire.UpdateFailHTLC{
2✔
261
                        ChanID: pd.ChanID,
2✔
262
                        ID:     pd.ParentIndex,
2✔
263
                        Reason: pd.FailReason,
2✔
264
                }
2✔
265
        case MalformedFail:
2✔
266
                msg = &lnwire.UpdateFailMalformedHTLC{
2✔
267
                        ChanID:       pd.ChanID,
2✔
268
                        ID:           pd.ParentIndex,
2✔
269
                        ShaOnionBlob: pd.ShaOnionBlob,
2✔
270
                        FailureCode:  pd.FailCode,
2✔
271
                }
2✔
272
        case FeeUpdate:
×
273
                // The Amount field holds the feerate denominated in
×
274
                // msat. Since feerates are only denominated in sat/kw,
×
275
                // we can convert it without loss of precision.
×
276
                msg = &lnwire.UpdateFee{
×
277
                        ChanID:   pd.ChanID,
×
278
                        FeePerKw: uint32(pd.Amount.ToSatoshis()),
×
279
                }
×
280
        }
281

282
        return channeldb.LogUpdate{
2✔
283
                LogIndex:  pd.LogIndex,
2✔
284
                UpdateMsg: msg,
2✔
285
        }
2✔
286
}
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