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

lightningnetwork / lnd / 15951470896

29 Jun 2025 04:23AM UTC coverage: 67.594% (-0.01%) from 67.606%
15951470896

Pull #9751

github

web-flow
Merge 599d9b051 into 6290edf14
Pull Request #9751: multi: update Go to 1.23.10 and update some packages

135088 of 199851 relevant lines covered (67.59%)

21909.44 hits per line

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

97.05
/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.
19
func RandPartialSig(t *rapid.T) *PartialSig {
100✔
20
        // Generate random private key bytes
100✔
21
        sigBytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(t, "privKeyBytes")
100✔
22

100✔
23
        var s btcec.ModNScalar
100✔
24
        s.SetByteSlice(sigBytes)
100✔
25

100✔
26
        return &PartialSig{
100✔
27
                Sig: s,
100✔
28
        }
100✔
29
}
100✔
30

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

146✔
39
        sigScalar := new(btcec.ModNScalar)
146✔
40
        sigScalar.SetByteSlice(sigBytes)
146✔
41

146✔
42
        return NewPartialSigWithNonce(
146✔
43
                RandMusig2Nonce(t), *sigScalar,
146✔
44
        )
146✔
45
}
146✔
46

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

2,343✔
54
        return pub
2,343✔
55
}
2,343✔
56

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

2,311✔
63
        return c
2,311✔
64
}
2,311✔
65

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

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

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

86
        return featureVec
546✔
87
}
88

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

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

2,396✔
100
        testSig := ecdsa.NewSignature(testRScalar, testSScalar)
2,396✔
101

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

107
        return sig
2,396✔
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.
120
func RandPaymentPreimage(t *rapid.T) [32]byte {
200✔
121
        var preimage [32]byte
200✔
122
        bytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(t, "preimage")
200✔
123
        copy(preimage[:], bytes)
200✔
124

200✔
125
        return preimage
200✔
126
}
200✔
127

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

700✔
134
        return hash
700✔
135
}
700✔
136

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

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

100✔
146
        copy(alias[:], aliasBytes)
100✔
147

100✔
148
        return alias
100✔
149
}
100✔
150

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

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

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

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

193
        return addresses
80✔
194
}
195

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

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

3,481✔
204
        if numRecords == 0 {
4,186✔
205
                return nil, nil
705✔
206
        }
705✔
207

208
        rangeStart := 0
2,776✔
209
        rangeStop := int(CustomTypeStart)
2,776✔
210
        if custom {
3,068✔
211
                rangeStart = 70_000
292✔
212
                rangeStop = 100_000
292✔
213
        }
292✔
214

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

7,772✔
235
                customRecords[recordType] = record
7,772✔
236

7,772✔
237
                ignoreSet.Add(recordType)
7,772✔
238
        }
239

240
        return customRecords, ignoreSet
2,776✔
241
}
242

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

552✔
249
        return nonce
552✔
250
}
552✔
251

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

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

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

2,407✔
266
        return ExtraOpaqueData(recordBytes)
2,407✔
267
}
268

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

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

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

100✔
306
        return validCodes[idx]
100✔
307
}
100✔
308

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

144✔
317
        return hash
144✔
318
}
144✔
319

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

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

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

195✔
334
        return &chanType
195✔
335
}
195✔
336

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

97✔
343
        return &exp
97✔
344
}
97✔
345

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

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

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