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

lightningnetwork / lnd / 15561477203

10 Jun 2025 01:54PM UTC coverage: 58.351% (-10.1%) from 68.487%
15561477203

Pull #9356

github

web-flow
Merge 6440b25db into c6d6d4c0b
Pull Request #9356: lnrpc: add incoming/outgoing channel ids filter to forwarding history request

33 of 36 new or added lines in 2 files covered. (91.67%)

28366 existing lines in 455 files now uncovered.

97715 of 167461 relevant lines covered (58.35%)

1.81 hits per line

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

47.76
/lnwallet/aux_signer.go
1
package lnwallet
2

3
import (
4
        "github.com/btcsuite/btcd/wire"
5
        "github.com/lightningnetwork/lnd/fn/v2"
6
        "github.com/lightningnetwork/lnd/input"
7
        "github.com/lightningnetwork/lnd/lntypes"
8
        "github.com/lightningnetwork/lnd/lnwallet/chainfee"
9
        "github.com/lightningnetwork/lnd/lnwire"
10
        "github.com/lightningnetwork/lnd/tlv"
11
)
12

13
// htlcCustomSigType is the TLV type that is used to encode the custom HTLC
14
// signatures within the custom data for an existing HTLC.
15
var htlcCustomSigType tlv.TlvType65543
16

17
// AuxHtlcView is a struct that contains a safe copy of an HTLC view that can
18
// be used by aux components.
19
type AuxHtlcView struct {
20
        // NextHeight is the height of the commitment transaction that will be
21
        // created using this view.
22
        NextHeight uint64
23

24
        // Updates is a Dual of the Local and Remote HTLCs.
25
        Updates lntypes.Dual[[]AuxHtlcDescriptor]
26

27
        // FeePerKw is the fee rate in sat/kw of the commitment transaction.
28
        FeePerKw chainfee.SatPerKWeight
29
}
30

31
// newAuxHtlcView creates a new safe copy of the HTLC view that can be used by
32
// aux components.
33
//
34
// NOTE: This function should only be called while holding the channel's read
35
// lock, since the underlying local/remote payment descriptors are accessed
36
// directly.
37
func newAuxHtlcView(v *HtlcView) AuxHtlcView {
×
38
        return AuxHtlcView{
×
39
                NextHeight: v.NextHeight,
×
40
                Updates: lntypes.Dual[[]AuxHtlcDescriptor]{
×
41
                        Local:  fn.Map(v.Updates.Local, newAuxHtlcDescriptor),
×
42
                        Remote: fn.Map(v.Updates.Remote, newAuxHtlcDescriptor),
×
43
                },
×
44
                FeePerKw: v.FeePerKw,
×
45
        }
×
46
}
×
47

48
// AuxHtlcDescriptor is a struct that contains the information needed to sign or
49
// verify an HTLC for custom channels.
50
type AuxHtlcDescriptor struct {
51
        // ChanID is the ChannelID of the LightningChannel that this
52
        // paymentDescriptor belongs to. We track this here so we can
53
        // reconstruct the Messages that this paymentDescriptor is built from.
54
        ChanID lnwire.ChannelID
55

56
        // RHash is the payment hash for this HTLC. The HTLC can be settled iff
57
        // the preimage to this hash is presented.
58
        RHash PaymentHash
59

60
        // Timeout is the absolute timeout in blocks, after which this HTLC
61
        // expires.
62
        Timeout uint32
63

64
        // Amount is the HTLC amount in milli-satoshis.
65
        Amount lnwire.MilliSatoshi
66

67
        // HtlcIndex is the index within the main update log for this HTLC.
68
        // Entries within the log of type Add will have this field populated,
69
        // as other entries will point to the entry via this counter.
70
        //
71
        // NOTE: This field will only be populated if EntryType is Add.
72
        HtlcIndex uint64
73

74
        // ParentIndex is the HTLC index of the entry that this update settles
75
        // or times out.
76
        //
77
        // NOTE: This field will only be populated if EntryType is Fail or
78
        // Settle.
79
        ParentIndex uint64
80

81
        // EntryType denotes the exact type of the paymentDescriptor. In the
82
        // case of a Timeout, or Settle type, then the Parent field will point
83
        // into the log to the HTLC being modified.
84
        EntryType updateType
85

86
        // CustomRecords also stores the set of optional custom records that
87
        // may have been attached to a sent HTLC.
88
        CustomRecords lnwire.CustomRecords
89

90
        // addCommitHeight[Remote|Local] encodes the height of the commitment
91
        // which included this HTLC on either the remote or local commitment
92
        // chain. This value is used to determine when an HTLC is fully
93
        // "locked-in".
94
        addCommitHeightRemote uint64
95
        addCommitHeightLocal  uint64
96

97
        // removeCommitHeight[Remote|Local] encodes the height of the
98
        // commitment which removed the parent pointer of this
99
        // paymentDescriptor either due to a timeout or a settle. Once both
100
        // these heights are below the tail of both chains, the log entries can
101
        // safely be removed.
102
        removeCommitHeightRemote uint64
103
        removeCommitHeightLocal  uint64
104
}
105

106
// AddHeight returns the height at which the HTLC was added to the commitment
107
// chain. The height is returned based on the chain the HTLC is being added to
108
// (local or remote chain).
109
func (a *AuxHtlcDescriptor) AddHeight(
110
        whoseCommitChain lntypes.ChannelParty) uint64 {
×
111

×
112
        if whoseCommitChain.IsRemote() {
×
113
                return a.addCommitHeightRemote
×
114
        }
×
115

116
        return a.addCommitHeightLocal
×
117
}
118

119
// RemoveHeight returns the height at which the HTLC was removed from the
120
// commitment chain. The height is returned based on the chain the HTLC is being
121
// removed from (local or remote chain).
122
func (a *AuxHtlcDescriptor) RemoveHeight(
123
        whoseCommitChain lntypes.ChannelParty) uint64 {
×
124

×
125
        if whoseCommitChain.IsRemote() {
×
126
                return a.removeCommitHeightRemote
×
127
        }
×
128

129
        return a.removeCommitHeightLocal
×
130
}
131

132
// newAuxHtlcDescriptor creates a new AuxHtlcDescriptor from a payment
133
// descriptor.
134
//
135
// NOTE: This function should only be called while holding the channel's read
136
// lock, since the underlying payment descriptors are accessed directly.
137
func newAuxHtlcDescriptor(p *paymentDescriptor) AuxHtlcDescriptor {
3✔
138
        return AuxHtlcDescriptor{
3✔
139
                ChanID:                   p.ChanID,
3✔
140
                RHash:                    p.RHash,
3✔
141
                Timeout:                  p.Timeout,
3✔
142
                Amount:                   p.Amount,
3✔
143
                HtlcIndex:                p.HtlcIndex,
3✔
144
                ParentIndex:              p.ParentIndex,
3✔
145
                EntryType:                p.EntryType,
3✔
146
                CustomRecords:            p.CustomRecords.Copy(),
3✔
147
                addCommitHeightRemote:    p.addCommitHeights.Remote,
3✔
148
                addCommitHeightLocal:     p.addCommitHeights.Local,
3✔
149
                removeCommitHeightRemote: p.removeCommitHeights.Remote,
3✔
150
                removeCommitHeightLocal:  p.removeCommitHeights.Local,
3✔
151
        }
3✔
152
}
3✔
153

154
// BaseAuxJob is a struct that contains the common fields that are shared among
155
// the aux sign/verify jobs.
156
type BaseAuxJob struct {
157
        // OutputIndex is the output index of the HTLC on the commitment
158
        // transaction being signed.
159
        //
160
        // NOTE: If the output is dust from the PoV of the commitment chain,
161
        // then this value will be -1.
162
        OutputIndex int32
163

164
        // KeyRing is the commitment key ring that contains the keys needed to
165
        // generate the second level HTLC signatures.
166
        KeyRing CommitmentKeyRing
167

168
        // HTLC is the HTLC that is being signed or verified.
169
        HTLC AuxHtlcDescriptor
170

171
        // Incoming is a boolean that indicates if the HTLC is incoming or
172
        // outgoing.
173
        Incoming bool
174

175
        // CommitBlob is the commitment transaction blob that contains the aux
176
        // information for this channel.
177
        CommitBlob fn.Option[tlv.Blob]
178

179
        // HtlcLeaf is the aux tap leaf that corresponds to the HTLC being
180
        // signed/verified.
181
        HtlcLeaf input.AuxTapLeaf
182
}
183

184
// AuxSigJob is a struct that contains all the information needed to sign an
185
// HTLC for custom channels.
186
type AuxSigJob struct {
187
        // SignDesc is the sign desc for this HTLC.
188
        SignDesc input.SignDescriptor
189

190
        BaseAuxJob
191

192
        // Resp is a channel that will be used to send the result of the sign
193
        // job. This channel MUST be buffered.
194
        Resp chan AuxSigJobResp
195

196
        // Cancel is a channel that is closed by the caller if they wish to
197
        // abandon all pending sign jobs part of a single batch. This should
198
        // never be closed by the validator.
199
        Cancel <-chan struct{}
200
}
201

202
// NewAuxSigJob creates a new AuxSigJob.
203
func NewAuxSigJob(sigJob SignJob, keyRing CommitmentKeyRing, incoming bool,
204
        htlc AuxHtlcDescriptor, commitBlob fn.Option[tlv.Blob],
205
        htlcLeaf input.AuxTapLeaf, cancelChan <-chan struct{}) AuxSigJob {
3✔
206

3✔
207
        return AuxSigJob{
3✔
208
                SignDesc: sigJob.SignDesc,
3✔
209
                BaseAuxJob: BaseAuxJob{
3✔
210
                        OutputIndex: sigJob.OutputIndex,
3✔
211
                        KeyRing:     keyRing,
3✔
212
                        HTLC:        htlc,
3✔
213
                        Incoming:    incoming,
3✔
214
                        CommitBlob:  commitBlob,
3✔
215
                        HtlcLeaf:    htlcLeaf,
3✔
216
                },
3✔
217
                Resp:   make(chan AuxSigJobResp, 1),
3✔
218
                Cancel: cancelChan,
3✔
219
        }
3✔
220
}
3✔
221

222
// AuxSigJobResp is a struct that contains the result of a sign job.
223
type AuxSigJobResp struct {
224
        // SigBlob is the signature blob that was generated for the HTLC. This
225
        // is an opaque TLV field that may contain the signature and other data.
226
        SigBlob fn.Option[tlv.Blob]
227

228
        // HtlcIndex is the index of the HTLC that was signed.
229
        HtlcIndex uint64
230

231
        // Err is the error that occurred when executing the specified
232
        // signature job. In the case that no error occurred, this value will
233
        // be nil.
234
        Err error
235
}
236

237
// AuxVerifyJob is a struct that contains all the information needed to verify
238
// an HTLC for custom channels.
239
type AuxVerifyJob struct {
240
        // SigBlob is the signature blob that was generated for the HTLC. This
241
        // is an opaque TLV field that may contain the signature and other data.
242
        SigBlob fn.Option[tlv.Blob]
243

244
        BaseAuxJob
245
}
246

247
// NewAuxVerifyJob creates a new AuxVerifyJob.
248
func NewAuxVerifyJob(sig fn.Option[tlv.Blob], keyRing CommitmentKeyRing,
249
        incoming bool, htlc AuxHtlcDescriptor, commitBlob fn.Option[tlv.Blob],
UNCOV
250
        htlcLeaf input.AuxTapLeaf) AuxVerifyJob {
×
UNCOV
251

×
UNCOV
252
        return AuxVerifyJob{
×
UNCOV
253
                SigBlob: sig,
×
UNCOV
254
                BaseAuxJob: BaseAuxJob{
×
UNCOV
255
                        KeyRing:    keyRing,
×
UNCOV
256
                        HTLC:       htlc,
×
UNCOV
257
                        Incoming:   incoming,
×
UNCOV
258
                        CommitBlob: commitBlob,
×
UNCOV
259
                        HtlcLeaf:   htlcLeaf,
×
UNCOV
260
                },
×
UNCOV
261
        }
×
UNCOV
262
}
×
263

264
// AuxSigner is an interface that is used to sign and verify HTLCs for custom
265
// channels. It is similar to the existing SigPool, but uses opaque blobs to
266
// shuffle around signature information and other metadata.
267
type AuxSigner interface {
268
        // SubmitSecondLevelSigBatch takes a batch of aux sign jobs and
269
        // processes them asynchronously.
270
        SubmitSecondLevelSigBatch(chanState AuxChanState, commitTx *wire.MsgTx,
271
                sigJob []AuxSigJob) error
272

273
        // PackSigs takes a series of aux signatures and packs them into a
274
        // single blob that can be sent alongside the CommitSig messages.
275
        PackSigs([]fn.Option[tlv.Blob]) fn.Result[fn.Option[tlv.Blob]]
276

277
        // UnpackSigs takes a packed blob of signatures and returns the
278
        // original signatures for each HTLC, keyed by HTLC index.
279
        UnpackSigs(fn.Option[tlv.Blob]) fn.Result[[]fn.Option[tlv.Blob]]
280

281
        // VerifySecondLevelSigs attempts to synchronously verify a batch of aux
282
        // sig jobs.
283
        VerifySecondLevelSigs(chanState AuxChanState, commitTx *wire.MsgTx,
284
                verifyJob []AuxVerifyJob) error
285
}
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