• 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

0.0
/lnwire/test_utils.go
1
package lnwire
2

3
import (
4
        "crypto/sha256"
5
        "fmt"
6
        "net"
7

8
        "github.com/btcsuite/btcd/btcec/v2"
9
        "github.com/btcsuite/btcd/btcec/v2/ecdsa"
10
        "github.com/btcsuite/btcd/chaincfg/chainhash"
11
        "github.com/btcsuite/btcd/wire"
12
        "github.com/lightningnetwork/lnd/fn/v2"
13
        "github.com/stretchr/testify/require"
14
        "pgregory.net/rapid"
15
)
16

17
// RandChannelUpdate generates a random ChannelUpdate message using rapid's
18
// generators.
UNCOV
19
func RandPartialSig(t *rapid.T) *PartialSig {
×
UNCOV
20
        // Generate random private key bytes
×
UNCOV
21
        sigBytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(t, "privKeyBytes")
×
UNCOV
22

×
UNCOV
23
        var s btcec.ModNScalar
×
UNCOV
24
        s.SetByteSlice(sigBytes)
×
UNCOV
25

×
UNCOV
26
        return &PartialSig{
×
UNCOV
27
                Sig: s,
×
UNCOV
28
        }
×
UNCOV
29
}
×
30

31
// RandPartialSigWithNonce generates a random PartialSigWithNonce using rapid
32
// generators.
UNCOV
33
func RandPartialSigWithNonce(t *rapid.T) *PartialSigWithNonce {
×
UNCOV
34
        sigLen := rapid.IntRange(1, 65).Draw(t, "partialSigLen")
×
UNCOV
35
        sigBytes := rapid.SliceOfN(
×
UNCOV
36
                rapid.Byte(), sigLen, sigLen,
×
UNCOV
37
        ).Draw(t, "partialSig")
×
UNCOV
38

×
UNCOV
39
        sigScalar := new(btcec.ModNScalar)
×
UNCOV
40
        sigScalar.SetByteSlice(sigBytes)
×
UNCOV
41

×
UNCOV
42
        return NewPartialSigWithNonce(
×
UNCOV
43
                RandMusig2Nonce(t), *sigScalar,
×
UNCOV
44
        )
×
UNCOV
45
}
×
46

47
// RandPubKey generates a random public key using rapid's generators.
UNCOV
48
func RandPubKey(t *rapid.T) *btcec.PublicKey {
×
UNCOV
49
        privKeyBytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(
×
UNCOV
50
                t, "privKeyBytes",
×
UNCOV
51
        )
×
UNCOV
52
        _, pub := btcec.PrivKeyFromBytes(privKeyBytes)
×
UNCOV
53

×
UNCOV
54
        return pub
×
UNCOV
55
}
×
56

57
// RandChannelID generates a random channel ID.
UNCOV
58
func RandChannelID(t *rapid.T) ChannelID {
×
UNCOV
59
        var c ChannelID
×
UNCOV
60
        bytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(t, "channelID")
×
UNCOV
61
        copy(c[:], bytes)
×
UNCOV
62

×
UNCOV
63
        return c
×
UNCOV
64
}
×
65

66
// RandShortChannelID generates a random short channel ID.
UNCOV
67
func RandShortChannelID(t *rapid.T) ShortChannelID {
×
UNCOV
68
        return NewShortChanIDFromInt(
×
UNCOV
69
                uint64(rapid.IntRange(1, 100000).Draw(t, "shortChanID")),
×
UNCOV
70
        )
×
UNCOV
71
}
×
72

73
// RandFeatureVector generates a random feature vector.
UNCOV
74
func RandFeatureVector(t *rapid.T) *RawFeatureVector {
×
UNCOV
75
        featureVec := NewRawFeatureVector()
×
UNCOV
76

×
UNCOV
77
        // Add a random number of random feature bits
×
UNCOV
78
        numFeatures := rapid.IntRange(0, 20).Draw(t, "numFeatures")
×
UNCOV
79
        for i := 0; i < numFeatures; i++ {
×
UNCOV
80
                bit := FeatureBit(rapid.IntRange(0, 100).Draw(
×
UNCOV
81
                        t, fmt.Sprintf("featureBit-%d", i)),
×
UNCOV
82
                )
×
UNCOV
83
                featureVec.Set(bit)
×
UNCOV
84
        }
×
85

UNCOV
86
        return featureVec
×
87
}
88

89
// RandSignature generates a signature for testing.
UNCOV
90
func RandSignature(t *rapid.T) Sig {
×
UNCOV
91
        testRScalar := new(btcec.ModNScalar)
×
UNCOV
92
        testSScalar := new(btcec.ModNScalar)
×
UNCOV
93

×
UNCOV
94
        // Generate random bytes for R and S
×
UNCOV
95
        rBytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(t, "rBytes")
×
UNCOV
96
        sBytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(t, "sBytes")
×
UNCOV
97
        _ = testRScalar.SetByteSlice(rBytes)
×
UNCOV
98
        _ = testSScalar.SetByteSlice(sBytes)
×
UNCOV
99

×
UNCOV
100
        testSig := ecdsa.NewSignature(testRScalar, testSScalar)
×
UNCOV
101

×
UNCOV
102
        sig, err := NewSigFromSignature(testSig)
×
UNCOV
103
        if err != nil {
×
104
                panic(fmt.Sprintf("unable to create signature: %v", err))
×
105
        }
106

UNCOV
107
        return sig
×
108
}
109

110
// RandPaymentHash generates a random payment hash.
111
func RandPaymentHash(t *rapid.T) [32]byte {
×
112
        var hash [32]byte
×
113
        bytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(t, "paymentHash")
×
114
        copy(hash[:], bytes)
×
115

×
116
        return hash
×
117
}
×
118

119
// RandPaymentPreimage generates a random payment preimage.
UNCOV
120
func RandPaymentPreimage(t *rapid.T) [32]byte {
×
UNCOV
121
        var preimage [32]byte
×
UNCOV
122
        bytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(t, "preimage")
×
UNCOV
123
        copy(preimage[:], bytes)
×
UNCOV
124

×
UNCOV
125
        return preimage
×
UNCOV
126
}
×
127

128
// RandChainHash generates a random chain hash.
UNCOV
129
func RandChainHash(t *rapid.T) chainhash.Hash {
×
UNCOV
130
        var hash [32]byte
×
UNCOV
131
        bytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(t, "chainHash")
×
UNCOV
132
        copy(hash[:], bytes)
×
UNCOV
133

×
UNCOV
134
        return hash
×
UNCOV
135
}
×
136

137
// RandNodeAlias generates a random node alias.
UNCOV
138
func RandNodeAlias(t *rapid.T) NodeAlias {
×
UNCOV
139
        var alias NodeAlias
×
UNCOV
140
        aliasLength := rapid.IntRange(0, 32).Draw(t, "aliasLength")
×
UNCOV
141

×
UNCOV
142
        aliasBytes := rapid.StringN(
×
UNCOV
143
                0, aliasLength, aliasLength,
×
UNCOV
144
        ).Draw(t, "alias")
×
UNCOV
145

×
UNCOV
146
        copy(alias[:], aliasBytes)
×
UNCOV
147

×
UNCOV
148
        return alias
×
UNCOV
149
}
×
150

151
// RandNetAddrs generates random network addresses.
UNCOV
152
func RandNetAddrs(t *rapid.T) []net.Addr {
×
UNCOV
153
        numAddresses := rapid.IntRange(0, 5).Draw(t, "numAddresses")
×
UNCOV
154
        if numAddresses == 0 {
×
UNCOV
155
                return nil
×
UNCOV
156
        }
×
157

UNCOV
158
        addresses := make([]net.Addr, numAddresses)
×
UNCOV
159
        for i := 0; i < numAddresses; i++ {
×
UNCOV
160
                addressType := rapid.IntRange(0, 1).Draw(
×
UNCOV
161
                        t, fmt.Sprintf("addressType-%d", i),
×
UNCOV
162
                )
×
UNCOV
163

×
UNCOV
164
                switch addressType {
×
165
                // IPv4.
UNCOV
166
                case 0:
×
UNCOV
167
                        ipBytes := rapid.SliceOfN(rapid.Byte(), 4, 4).Draw(
×
UNCOV
168
                                t, fmt.Sprintf("ipv4-%d", i),
×
UNCOV
169
                        )
×
UNCOV
170
                        port := rapid.IntRange(1, 65535).Draw(
×
UNCOV
171
                                t, fmt.Sprintf("port-%d", i),
×
UNCOV
172
                        )
×
UNCOV
173
                        addresses[i] = &net.TCPAddr{
×
UNCOV
174
                                IP:   ipBytes,
×
UNCOV
175
                                Port: port,
×
UNCOV
176
                        }
×
177

178
                // IPv6.
UNCOV
179
                case 1:
×
UNCOV
180
                        ipBytes := rapid.SliceOfN(rapid.Byte(), 16, 16).Draw(
×
UNCOV
181
                                t, fmt.Sprintf("ipv6-%d", i),
×
UNCOV
182
                        )
×
UNCOV
183
                        port := rapid.IntRange(1, 65535).Draw(
×
UNCOV
184
                                t, fmt.Sprintf("port-%d", i),
×
UNCOV
185
                        )
×
UNCOV
186
                        addresses[i] = &net.TCPAddr{
×
UNCOV
187
                                IP:   ipBytes,
×
UNCOV
188
                                Port: port,
×
UNCOV
189
                        }
×
190
                }
191
        }
192

UNCOV
193
        return addresses
×
194
}
195

196
// RandCustomRecords generates random custom TLV records.
197
func RandCustomRecords(t *rapid.T,
198
        ignoreRecords fn.Set[uint64],
UNCOV
199
        custom bool) (CustomRecords, fn.Set[uint64]) {
×
UNCOV
200

×
UNCOV
201
        numRecords := rapid.IntRange(0, 5).Draw(t, "numCustomRecords")
×
UNCOV
202
        customRecords := make(CustomRecords)
×
UNCOV
203

×
UNCOV
204
        if numRecords == 0 {
×
UNCOV
205
                return nil, nil
×
UNCOV
206
        }
×
207

UNCOV
208
        rangeStart := 0
×
UNCOV
209
        rangeStop := int(CustomTypeStart)
×
UNCOV
210
        if custom {
×
UNCOV
211
                rangeStart = 70_000
×
UNCOV
212
                rangeStop = 100_000
×
UNCOV
213
        }
×
214

UNCOV
215
        ignoreSet := fn.NewSet[uint64]()
×
UNCOV
216
        for i := 0; i < numRecords; i++ {
×
UNCOV
217
                recordType := uint64(
×
UNCOV
218
                        rapid.IntRange(rangeStart, rangeStop).
×
UNCOV
219
                                Filter(func(i int) bool {
×
UNCOV
220
                                        return !ignoreRecords.Contains(
×
UNCOV
221
                                                uint64(i),
×
UNCOV
222
                                        )
×
UNCOV
223
                                }).
×
224
                                Draw(
225
                                        t, fmt.Sprintf("recordType-%d", i),
226
                                ),
227
                )
UNCOV
228
                recordLen := rapid.IntRange(4, 64).Draw(
×
UNCOV
229
                        t, fmt.Sprintf("recordLen-%d", i),
×
UNCOV
230
                )
×
UNCOV
231
                record := rapid.SliceOfN(
×
UNCOV
232
                        rapid.Byte(), recordLen, recordLen,
×
UNCOV
233
                ).Draw(t, fmt.Sprintf("record-%d", i))
×
UNCOV
234

×
UNCOV
235
                customRecords[recordType] = record
×
UNCOV
236

×
UNCOV
237
                ignoreSet.Add(recordType)
×
238
        }
239

UNCOV
240
        return customRecords, ignoreSet
×
241
}
242

243
// RandMusig2Nonce generates a random musig2 nonce.
UNCOV
244
func RandMusig2Nonce(t *rapid.T) Musig2Nonce {
×
UNCOV
245
        var nonce Musig2Nonce
×
UNCOV
246
        bytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(t, "nonce")
×
UNCOV
247
        copy(nonce[:], bytes)
×
UNCOV
248

×
UNCOV
249
        return nonce
×
UNCOV
250
}
×
251

252
// RandExtraOpaqueData generates random extra opaque data.
253
func RandExtraOpaqueData(t *rapid.T,
UNCOV
254
        ignoreRecords fn.Set[uint64]) ExtraOpaqueData {
×
UNCOV
255

×
UNCOV
256
        // Make some random records.
×
UNCOV
257
        cRecords, _ := RandCustomRecords(t, ignoreRecords, false)
×
UNCOV
258
        if cRecords == nil {
×
UNCOV
259
                return ExtraOpaqueData{}
×
UNCOV
260
        }
×
261

262
        // Encode those records as opaque data.
UNCOV
263
        recordBytes, err := cRecords.Serialize()
×
UNCOV
264
        require.NoError(t, err)
×
UNCOV
265

×
UNCOV
266
        return ExtraOpaqueData(recordBytes)
×
267
}
268

269
// RandOpaqueReason generates a random opaque reason for HTLC failures.
UNCOV
270
func RandOpaqueReason(t *rapid.T) OpaqueReason {
×
UNCOV
271
        reasonLen := rapid.IntRange(32, 300).Draw(t, "reasonLen")
×
UNCOV
272
        return rapid.SliceOfN(rapid.Byte(), reasonLen, reasonLen).Draw(
×
UNCOV
273
                t, "opaqueReason",
×
UNCOV
274
        )
×
UNCOV
275
}
×
276

277
// RandFailCode generates a random HTLC failure code.
UNCOV
278
func RandFailCode(t *rapid.T) FailCode {
×
UNCOV
279
        // List of known failure codes to choose from Using only the documented
×
UNCOV
280
        // codes.
×
UNCOV
281
        validCodes := []FailCode{
×
UNCOV
282
                CodeInvalidRealm,
×
UNCOV
283
                CodeTemporaryNodeFailure,
×
UNCOV
284
                CodePermanentNodeFailure,
×
UNCOV
285
                CodeRequiredNodeFeatureMissing,
×
UNCOV
286
                CodePermanentChannelFailure,
×
UNCOV
287
                CodeRequiredChannelFeatureMissing,
×
UNCOV
288
                CodeUnknownNextPeer,
×
UNCOV
289
                CodeIncorrectOrUnknownPaymentDetails,
×
UNCOV
290
                CodeIncorrectPaymentAmount,
×
UNCOV
291
                CodeFinalExpiryTooSoon,
×
UNCOV
292
                CodeInvalidOnionVersion,
×
UNCOV
293
                CodeInvalidOnionHmac,
×
UNCOV
294
                CodeInvalidOnionKey,
×
UNCOV
295
                CodeTemporaryChannelFailure,
×
UNCOV
296
                CodeChannelDisabled,
×
UNCOV
297
                CodeExpiryTooSoon,
×
UNCOV
298
                CodeMPPTimeout,
×
UNCOV
299
                CodeInvalidOnionPayload,
×
UNCOV
300
                CodeFeeInsufficient,
×
UNCOV
301
        }
×
UNCOV
302

×
UNCOV
303
        // Choose a random code from the list.
×
UNCOV
304
        idx := rapid.IntRange(0, len(validCodes)-1).Draw(t, "failCodeIndex")
×
UNCOV
305

×
UNCOV
306
        return validCodes[idx]
×
UNCOV
307
}
×
308

309
// RandSHA256Hash generates a random SHA256 hash.
UNCOV
310
func RandSHA256Hash(t *rapid.T) [sha256.Size]byte {
×
UNCOV
311
        var hash [sha256.Size]byte
×
UNCOV
312
        bytes := rapid.SliceOfN(rapid.Byte(), sha256.Size, sha256.Size).Draw(
×
UNCOV
313
                t, "sha256Hash",
×
UNCOV
314
        )
×
UNCOV
315
        copy(hash[:], bytes)
×
UNCOV
316

×
UNCOV
317
        return hash
×
UNCOV
318
}
×
319

320
// RandDeliveryAddress generates a random delivery address (script).
UNCOV
321
func RandDeliveryAddress(t *rapid.T) DeliveryAddress {
×
UNCOV
322
        addrLen := rapid.IntRange(1, 34).Draw(t, "addrLen")
×
UNCOV
323

×
UNCOV
324
        return rapid.SliceOfN(rapid.Byte(), addrLen, addrLen).Draw(
×
UNCOV
325
                t, "deliveryAddress",
×
UNCOV
326
        )
×
UNCOV
327
}
×
328

329
// RandChannelType generates a random channel type.
UNCOV
330
func RandChannelType(t *rapid.T) *ChannelType {
×
UNCOV
331
        vec := RandFeatureVector(t)
×
UNCOV
332
        chanType := ChannelType(*vec)
×
UNCOV
333

×
UNCOV
334
        return &chanType
×
UNCOV
335
}
×
336

337
// RandLeaseExpiry generates a random lease expiry.
UNCOV
338
func RandLeaseExpiry(t *rapid.T) *LeaseExpiry {
×
UNCOV
339
        exp := LeaseExpiry(
×
UNCOV
340
                uint32(rapid.IntRange(1000, 1000000).Draw(t, "leaseExpiry")),
×
UNCOV
341
        )
×
UNCOV
342

×
UNCOV
343
        return &exp
×
UNCOV
344
}
×
345

346
// RandOutPoint generates a random transaction outpoint.
UNCOV
347
func RandOutPoint(t *rapid.T) wire.OutPoint {
×
UNCOV
348
        // Generate a random transaction ID
×
UNCOV
349
        var txid chainhash.Hash
×
UNCOV
350
        txidBytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(t, "txid")
×
UNCOV
351
        copy(txid[:], txidBytes)
×
UNCOV
352

×
UNCOV
353
        // Generate a random output index
×
UNCOV
354
        vout := uint32(rapid.IntRange(0, 10).Draw(t, "vout"))
×
UNCOV
355

×
UNCOV
356
        return wire.OutPoint{
×
UNCOV
357
                Hash:  txid,
×
UNCOV
358
                Index: vout,
×
UNCOV
359
        }
×
UNCOV
360
}
×
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