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

lightningnetwork / lnd / 17101605539

20 Aug 2025 02:35PM UTC coverage: 57.321% (-9.4%) from 66.68%
17101605539

push

github

web-flow
Merge pull request #10102 from yyforyongyu/fix-UpdatesInHorizon

Catch bad gossip peer and fix `UpdatesInHorizon`

28 of 89 new or added lines in 4 files covered. (31.46%)

29163 existing lines in 459 files now uncovered.

99187 of 173038 relevant lines covered (57.32%)

1.78 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
// charset contains valid UTF-8 characters that can be used to generate random
18
// strings for testing purposes.
19
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
20

21
// RandPartialSig generates a random ParialSig message using rapid's
22
// generators.
UNCOV
23
func RandPartialSig(t *rapid.T) *PartialSig {
×
UNCOV
24
        // Generate random private key bytes
×
UNCOV
25
        sigBytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(t, "privKeyBytes")
×
UNCOV
26

×
UNCOV
27
        var s btcec.ModNScalar
×
UNCOV
28
        s.SetByteSlice(sigBytes)
×
UNCOV
29

×
UNCOV
30
        return &PartialSig{
×
UNCOV
31
                Sig: s,
×
UNCOV
32
        }
×
UNCOV
33
}
×
34

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

×
UNCOV
43
        sigScalar := new(btcec.ModNScalar)
×
UNCOV
44
        sigScalar.SetByteSlice(sigBytes)
×
UNCOV
45

×
UNCOV
46
        return NewPartialSigWithNonce(
×
UNCOV
47
                RandMusig2Nonce(t), *sigScalar,
×
UNCOV
48
        )
×
UNCOV
49
}
×
50

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

×
UNCOV
58
        return pub
×
UNCOV
59
}
×
60

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

×
UNCOV
67
        return c
×
UNCOV
68
}
×
69

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

77
// RandFeatureVector generates a random feature vector.
UNCOV
78
func RandFeatureVector(t *rapid.T) *RawFeatureVector {
×
UNCOV
79
        featureVec := NewRawFeatureVector()
×
UNCOV
80

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

UNCOV
90
        return featureVec
×
91
}
92

93
// RandSignature generates a signature for testing.
UNCOV
94
func RandSignature(t *rapid.T) Sig {
×
UNCOV
95
        testRScalar := new(btcec.ModNScalar)
×
UNCOV
96
        testSScalar := new(btcec.ModNScalar)
×
UNCOV
97

×
UNCOV
98
        // Generate random bytes for R and S
×
UNCOV
99
        rBytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(t, "rBytes")
×
UNCOV
100
        sBytes := rapid.SliceOfN(rapid.Byte(), 32, 32).Draw(t, "sBytes")
×
UNCOV
101
        _ = testRScalar.SetByteSlice(rBytes)
×
UNCOV
102
        _ = testSScalar.SetByteSlice(sBytes)
×
UNCOV
103

×
UNCOV
104
        testSig := ecdsa.NewSignature(testRScalar, testSScalar)
×
UNCOV
105

×
UNCOV
106
        sig, err := NewSigFromSignature(testSig)
×
UNCOV
107
        if err != nil {
×
108
                panic(fmt.Sprintf("unable to create signature: %v", err))
×
109
        }
110

UNCOV
111
        return sig
×
112
}
113

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

×
120
        return hash
×
121
}
×
122

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

×
UNCOV
129
        return preimage
×
UNCOV
130
}
×
131

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

×
UNCOV
138
        return hash
×
UNCOV
139
}
×
140

141
// RandNodeAlias generates a random node alias.
UNCOV
142
func RandNodeAlias(t *rapid.T) NodeAlias {
×
UNCOV
143
        var alias NodeAlias
×
UNCOV
144
        aliasLength := rapid.IntRange(0, 32).Draw(t, "aliasLength")
×
UNCOV
145

×
UNCOV
146
        aliasBytes := rapid.SliceOfN(
×
UNCOV
147
                rapid.SampledFrom([]rune(charset)), aliasLength, aliasLength,
×
UNCOV
148
        ).Draw(t, "alias")
×
UNCOV
149

×
UNCOV
150
        copy(alias[:], string(aliasBytes))
×
UNCOV
151

×
UNCOV
152
        return alias
×
UNCOV
153
}
×
154

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

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

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

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

UNCOV
197
        return addresses
×
198
}
199

200
// RandCustomRecords generates random custom TLV records.
201
func RandCustomRecords(t *rapid.T,
202
        ignoreRecords fn.Set[uint64],
UNCOV
203
        custom bool) (CustomRecords, fn.Set[uint64]) {
×
UNCOV
204

×
UNCOV
205
        numRecords := rapid.IntRange(0, 5).Draw(t, "numCustomRecords")
×
UNCOV
206
        customRecords := make(CustomRecords)
×
UNCOV
207

×
UNCOV
208
        if numRecords == 0 {
×
UNCOV
209
                return nil, nil
×
UNCOV
210
        }
×
211

UNCOV
212
        rangeStart := 0
×
UNCOV
213
        rangeStop := int(CustomTypeStart)
×
UNCOV
214
        if custom {
×
UNCOV
215
                rangeStart = 70_000
×
UNCOV
216
                rangeStop = 100_000
×
UNCOV
217
        }
×
218

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

×
UNCOV
239
                customRecords[recordType] = record
×
UNCOV
240

×
UNCOV
241
                ignoreSet.Add(recordType)
×
242
        }
243

UNCOV
244
        return customRecords, ignoreSet
×
245
}
246

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

×
UNCOV
253
        return nonce
×
UNCOV
254
}
×
255

256
// RandExtraOpaqueData generates random extra opaque data.
257
func RandExtraOpaqueData(t *rapid.T,
UNCOV
258
        ignoreRecords fn.Set[uint64]) ExtraOpaqueData {
×
UNCOV
259

×
UNCOV
260
        // Make some random records.
×
UNCOV
261
        cRecords, _ := RandCustomRecords(t, ignoreRecords, false)
×
UNCOV
262
        if cRecords == nil {
×
UNCOV
263
                return ExtraOpaqueData{}
×
UNCOV
264
        }
×
265

266
        // Encode those records as opaque data.
UNCOV
267
        recordBytes, err := cRecords.Serialize()
×
UNCOV
268
        require.NoError(t, err)
×
UNCOV
269

×
UNCOV
270
        return ExtraOpaqueData(recordBytes)
×
271
}
272

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

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

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

×
UNCOV
310
        return validCodes[idx]
×
UNCOV
311
}
×
312

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

×
UNCOV
321
        return hash
×
UNCOV
322
}
×
323

324
// RandDeliveryAddress generates a random delivery address (script).
UNCOV
325
func RandDeliveryAddress(t *rapid.T) DeliveryAddress {
×
UNCOV
326
        addrLen := rapid.IntRange(1, 34).Draw(t, "addrLen")
×
UNCOV
327

×
UNCOV
328
        return rapid.SliceOfN(rapid.Byte(), addrLen, addrLen).Draw(
×
UNCOV
329
                t, "deliveryAddress",
×
UNCOV
330
        )
×
UNCOV
331
}
×
332

333
// RandChannelType generates a random channel type.
UNCOV
334
func RandChannelType(t *rapid.T) *ChannelType {
×
UNCOV
335
        vec := RandFeatureVector(t)
×
UNCOV
336
        chanType := ChannelType(*vec)
×
UNCOV
337

×
UNCOV
338
        return &chanType
×
UNCOV
339
}
×
340

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

×
UNCOV
347
        return &exp
×
UNCOV
348
}
×
349

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

×
UNCOV
357
        // Generate a random output index
×
UNCOV
358
        vout := uint32(rapid.IntRange(0, 10).Draw(t, "vout"))
×
UNCOV
359

×
UNCOV
360
        return wire.OutPoint{
×
UNCOV
361
                Hash:  txid,
×
UNCOV
362
                Index: vout,
×
UNCOV
363
        }
×
UNCOV
364
}
×
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