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

lightningnetwork / lnd / 11170835610

03 Oct 2024 10:41PM UTC coverage: 49.188% (-9.6%) from 58.738%
11170835610

push

github

web-flow
Merge pull request #9154 from ziggie1984/master

multi: bump btcd version.

3 of 6 new or added lines in 6 files covered. (50.0%)

26110 existing lines in 428 files now uncovered.

97359 of 197934 relevant lines covered (49.19%)

1.04 hits per line

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

0.0
/lnwallet/test_utils.go
1
package lnwallet
2

3
import (
4
        "bytes"
5
        "crypto/rand"
6
        "encoding/binary"
7
        "encoding/hex"
8
        "io"
9
        prand "math/rand"
10
        "net"
11
        "testing"
12

13
        "github.com/btcsuite/btcd/btcec/v2"
14
        "github.com/btcsuite/btcd/btcutil"
15
        "github.com/btcsuite/btcd/chaincfg/chainhash"
16
        "github.com/btcsuite/btcd/wire"
17
        "github.com/lightningnetwork/lnd/channeldb"
18
        "github.com/lightningnetwork/lnd/fn"
19
        "github.com/lightningnetwork/lnd/input"
20
        "github.com/lightningnetwork/lnd/keychain"
21
        "github.com/lightningnetwork/lnd/lntypes"
22
        "github.com/lightningnetwork/lnd/lnwallet/chainfee"
23
        "github.com/lightningnetwork/lnd/lnwire"
24
        "github.com/lightningnetwork/lnd/shachain"
25
        "github.com/lightningnetwork/lnd/tlv"
26
        "github.com/stretchr/testify/mock"
27
        "github.com/stretchr/testify/require"
28
)
29

30
var (
31
        // For simplicity a single priv key controls all of our test outputs.
32
        testWalletPrivKey = []byte{
33
                0x2b, 0xd8, 0x06, 0xc9, 0x7f, 0x0e, 0x00, 0xaf,
34
                0x1a, 0x1f, 0xc3, 0x32, 0x8f, 0xa7, 0x63, 0xa9,
35
                0x26, 0x97, 0x23, 0xc8, 0xdb, 0x8f, 0xac, 0x4f,
36
                0x93, 0xaf, 0x71, 0xdb, 0x18, 0x6d, 0x6e, 0x90,
37
        }
38

39
        // We're alice :)
40
        bobsPrivKey = []byte{
41
                0x81, 0xb6, 0x37, 0xd8, 0xfc, 0xd2, 0xc6, 0xda,
42
                0x63, 0x59, 0xe6, 0x96, 0x31, 0x13, 0xa1, 0x17,
43
                0xd, 0xe7, 0x95, 0xe4, 0xb7, 0x25, 0xb8, 0x4d,
44
                0x1e, 0xb, 0x4c, 0xfd, 0x9e, 0xc5, 0x8c, 0xe9,
45
        }
46

47
        // Use a hard-coded HD seed.
48
        testHdSeed = chainhash.Hash{
49
                0xb7, 0x94, 0x38, 0x5f, 0x2d, 0x1e, 0xf7, 0xab,
50
                0x4d, 0x92, 0x73, 0xd1, 0x90, 0x63, 0x81, 0xb4,
51
                0x4f, 0x2f, 0x6f, 0x25, 0x88, 0xa3, 0xef, 0xb9,
52
                0x6a, 0x49, 0x18, 0x83, 0x31, 0x98, 0x47, 0x53,
53
        }
54

55
        // A serializable txn for testing funding txn.
56
        testTx = &wire.MsgTx{
57
                Version: 1,
58
                TxIn: []*wire.TxIn{
59
                        {
60
                                PreviousOutPoint: wire.OutPoint{
61
                                        Hash:  chainhash.Hash{},
62
                                        Index: 0xffffffff,
63
                                },
64
                                SignatureScript: []byte{0x04, 0x31, 0xdc, 0x00, 0x1b, 0x01, 0x62},
65
                                Sequence:        0xffffffff,
66
                        },
67
                },
68
                TxOut: []*wire.TxOut{
69
                        {
70
                                Value: 5000000000,
71
                                PkScript: []byte{
72
                                        0x41, // OP_DATA_65
73
                                        0x04, 0xd6, 0x4b, 0xdf, 0xd0, 0x9e, 0xb1, 0xc5,
74
                                        0xfe, 0x29, 0x5a, 0xbd, 0xeb, 0x1d, 0xca, 0x42,
75
                                        0x81, 0xbe, 0x98, 0x8e, 0x2d, 0xa0, 0xb6, 0xc1,
76
                                        0xc6, 0xa5, 0x9d, 0xc2, 0x26, 0xc2, 0x86, 0x24,
77
                                        0xe1, 0x81, 0x75, 0xe8, 0x51, 0xc9, 0x6b, 0x97,
78
                                        0x3d, 0x81, 0xb0, 0x1c, 0xc3, 0x1f, 0x04, 0x78,
79
                                        0x34, 0xbc, 0x06, 0xd6, 0xd6, 0xed, 0xf6, 0x20,
80
                                        0xd1, 0x84, 0x24, 0x1a, 0x6a, 0xed, 0x8b, 0x63,
81
                                        0xa6, // 65-byte signature
82
                                        0xac, // OP_CHECKSIG
83
                                },
84
                        },
85
                },
86
                LockTime: 5,
87
        }
88

89
        // A valid, DER-encoded signature (taken from btcec unit tests).
90
        testSigBytes = []byte{
91
                0x30, 0x44, 0x02, 0x20, 0x4e, 0x45, 0xe1, 0x69,
92
                0x32, 0xb8, 0xaf, 0x51, 0x49, 0x61, 0xa1, 0xd3,
93
                0xa1, 0xa2, 0x5f, 0xdf, 0x3f, 0x4f, 0x77, 0x32,
94
                0xe9, 0xd6, 0x24, 0xc6, 0xc6, 0x15, 0x48, 0xab,
95
                0x5f, 0xb8, 0xcd, 0x41, 0x02, 0x20, 0x18, 0x15,
96
                0x22, 0xec, 0x8e, 0xca, 0x07, 0xde, 0x48, 0x60,
97
                0xa4, 0xac, 0xdd, 0x12, 0x90, 0x9d, 0x83, 0x1c,
98
                0xc5, 0x6c, 0xbb, 0xac, 0x46, 0x22, 0x08, 0x22,
99
                0x21, 0xa8, 0x76, 0x8d, 0x1d, 0x09,
100
        }
101

102
        aliceDustLimit = btcutil.Amount(200)
103
        bobDustLimit   = btcutil.Amount(1300)
104

105
        testChannelCapacity float64 = 10
106
)
107

108
// CreateTestChannels creates to fully populated channels to be used within
109
// testing fixtures. The channels will be returned as if the funding process
110
// has just completed.  The channel itself is funded with 10 BTC, with 5 BTC
111
// allocated to each side. Within the channel, Alice is the initiator. If
112
// tweaklessCommits is true, then the commits within the channels will use the
113
// new format, otherwise the legacy format.
114
func CreateTestChannels(t *testing.T, chanType channeldb.ChannelType,
115
        dbModifiers ...channeldb.OptionModifier) (*LightningChannel,
UNCOV
116
        *LightningChannel, error) {
×
UNCOV
117

×
UNCOV
118
        channelCapacity, err := btcutil.NewAmount(testChannelCapacity)
×
UNCOV
119
        if err != nil {
×
120
                return nil, nil, err
×
121
        }
×
122

UNCOV
123
        channelBal := channelCapacity / 2
×
UNCOV
124
        csvTimeoutAlice := uint32(5)
×
UNCOV
125
        csvTimeoutBob := uint32(4)
×
UNCOV
126
        isAliceInitiator := true
×
UNCOV
127

×
UNCOV
128
        prevOut := &wire.OutPoint{
×
UNCOV
129
                Hash:  chainhash.Hash(testHdSeed),
×
UNCOV
130
                Index: prand.Uint32(),
×
UNCOV
131
        }
×
UNCOV
132
        fundingTxIn := wire.NewTxIn(prevOut, nil, nil)
×
UNCOV
133

×
UNCOV
134
        // For each party, we'll create a distinct set of keys in order to
×
UNCOV
135
        // emulate the typical set up with live channels.
×
UNCOV
136
        var (
×
UNCOV
137
                aliceKeys []*btcec.PrivateKey
×
UNCOV
138
                bobKeys   []*btcec.PrivateKey
×
UNCOV
139
        )
×
UNCOV
140
        for i := 0; i < 5; i++ {
×
UNCOV
141
                key := make([]byte, len(testWalletPrivKey))
×
UNCOV
142
                copy(key[:], testWalletPrivKey[:])
×
UNCOV
143
                key[0] ^= byte(i + 1)
×
UNCOV
144

×
UNCOV
145
                aliceKey, _ := btcec.PrivKeyFromBytes(key)
×
UNCOV
146
                aliceKeys = append(aliceKeys, aliceKey)
×
UNCOV
147

×
UNCOV
148
                key = make([]byte, len(bobsPrivKey))
×
UNCOV
149
                copy(key[:], bobsPrivKey)
×
UNCOV
150
                key[0] ^= byte(i + 1)
×
UNCOV
151

×
UNCOV
152
                bobKey, _ := btcec.PrivKeyFromBytes(key)
×
UNCOV
153
                bobKeys = append(bobKeys, bobKey)
×
UNCOV
154
        }
×
155

UNCOV
156
        aliceCfg := channeldb.ChannelConfig{
×
UNCOV
157
                ChannelStateBounds: channeldb.ChannelStateBounds{
×
UNCOV
158
                        MaxPendingAmount: lnwire.NewMSatFromSatoshis(channelCapacity),
×
UNCOV
159
                        ChanReserve:      channelCapacity / 100,
×
UNCOV
160
                        MinHTLC:          0,
×
UNCOV
161
                        MaxAcceptedHtlcs: input.MaxHTLCNumber / 2,
×
UNCOV
162
                },
×
UNCOV
163
                CommitmentParams: channeldb.CommitmentParams{
×
UNCOV
164
                        DustLimit: aliceDustLimit,
×
UNCOV
165
                        CsvDelay:  uint16(csvTimeoutAlice),
×
UNCOV
166
                },
×
UNCOV
167
                MultiSigKey: keychain.KeyDescriptor{
×
UNCOV
168
                        PubKey: aliceKeys[0].PubKey(),
×
UNCOV
169
                },
×
UNCOV
170
                RevocationBasePoint: keychain.KeyDescriptor{
×
UNCOV
171
                        PubKey: aliceKeys[1].PubKey(),
×
UNCOV
172
                },
×
UNCOV
173
                PaymentBasePoint: keychain.KeyDescriptor{
×
UNCOV
174
                        PubKey: aliceKeys[2].PubKey(),
×
UNCOV
175
                },
×
UNCOV
176
                DelayBasePoint: keychain.KeyDescriptor{
×
UNCOV
177
                        PubKey: aliceKeys[3].PubKey(),
×
UNCOV
178
                },
×
UNCOV
179
                HtlcBasePoint: keychain.KeyDescriptor{
×
UNCOV
180
                        PubKey: aliceKeys[4].PubKey(),
×
UNCOV
181
                },
×
UNCOV
182
        }
×
UNCOV
183
        bobCfg := channeldb.ChannelConfig{
×
UNCOV
184
                ChannelStateBounds: channeldb.ChannelStateBounds{
×
UNCOV
185
                        MaxPendingAmount: lnwire.NewMSatFromSatoshis(channelCapacity),
×
UNCOV
186
                        ChanReserve:      channelCapacity / 100,
×
UNCOV
187
                        MinHTLC:          0,
×
UNCOV
188
                        MaxAcceptedHtlcs: input.MaxHTLCNumber / 2,
×
UNCOV
189
                },
×
UNCOV
190
                CommitmentParams: channeldb.CommitmentParams{
×
UNCOV
191
                        DustLimit: bobDustLimit,
×
UNCOV
192
                        CsvDelay:  uint16(csvTimeoutBob),
×
UNCOV
193
                },
×
UNCOV
194
                MultiSigKey: keychain.KeyDescriptor{
×
UNCOV
195
                        PubKey: bobKeys[0].PubKey(),
×
UNCOV
196
                },
×
UNCOV
197
                RevocationBasePoint: keychain.KeyDescriptor{
×
UNCOV
198
                        PubKey: bobKeys[1].PubKey(),
×
UNCOV
199
                },
×
UNCOV
200
                PaymentBasePoint: keychain.KeyDescriptor{
×
UNCOV
201
                        PubKey: bobKeys[2].PubKey(),
×
UNCOV
202
                },
×
UNCOV
203
                DelayBasePoint: keychain.KeyDescriptor{
×
UNCOV
204
                        PubKey: bobKeys[3].PubKey(),
×
UNCOV
205
                },
×
UNCOV
206
                HtlcBasePoint: keychain.KeyDescriptor{
×
UNCOV
207
                        PubKey: bobKeys[4].PubKey(),
×
UNCOV
208
                },
×
UNCOV
209
        }
×
UNCOV
210

×
UNCOV
211
        bobRoot, err := chainhash.NewHash(bobKeys[0].Serialize())
×
UNCOV
212
        if err != nil {
×
213
                return nil, nil, err
×
214
        }
×
UNCOV
215
        bobPreimageProducer := shachain.NewRevocationProducer(*bobRoot)
×
UNCOV
216
        bobFirstRevoke, err := bobPreimageProducer.AtIndex(0)
×
UNCOV
217
        if err != nil {
×
218
                return nil, nil, err
×
219
        }
×
UNCOV
220
        bobCommitPoint := input.ComputeCommitmentPoint(bobFirstRevoke[:])
×
UNCOV
221

×
UNCOV
222
        aliceRoot, err := chainhash.NewHash(aliceKeys[0].Serialize())
×
UNCOV
223
        if err != nil {
×
224
                return nil, nil, err
×
225
        }
×
UNCOV
226
        alicePreimageProducer := shachain.NewRevocationProducer(*aliceRoot)
×
UNCOV
227
        aliceFirstRevoke, err := alicePreimageProducer.AtIndex(0)
×
UNCOV
228
        if err != nil {
×
229
                return nil, nil, err
×
230
        }
×
UNCOV
231
        aliceCommitPoint := input.ComputeCommitmentPoint(aliceFirstRevoke[:])
×
UNCOV
232

×
UNCOV
233
        aliceCommitTx, bobCommitTx, err := CreateCommitmentTxns(
×
UNCOV
234
                channelBal, channelBal, &aliceCfg, &bobCfg, aliceCommitPoint,
×
UNCOV
235
                bobCommitPoint, *fundingTxIn, chanType, isAliceInitiator, 0,
×
UNCOV
236
        )
×
UNCOV
237
        if err != nil {
×
238
                return nil, nil, err
×
239
        }
×
240

UNCOV
241
        dbAlice, err := channeldb.Open(t.TempDir(), dbModifiers...)
×
UNCOV
242
        if err != nil {
×
243
                return nil, nil, err
×
244
        }
×
UNCOV
245
        t.Cleanup(func() {
×
UNCOV
246
                require.NoError(t, dbAlice.Close())
×
UNCOV
247
        })
×
248

UNCOV
249
        dbBob, err := channeldb.Open(t.TempDir(), dbModifiers...)
×
UNCOV
250
        if err != nil {
×
251
                return nil, nil, err
×
252
        }
×
UNCOV
253
        t.Cleanup(func() {
×
UNCOV
254
                require.NoError(t, dbBob.Close())
×
UNCOV
255
        })
×
256

UNCOV
257
        estimator := chainfee.NewStaticEstimator(6000, 0)
×
UNCOV
258
        feePerKw, err := estimator.EstimateFeePerKW(1)
×
UNCOV
259
        if err != nil {
×
260
                return nil, nil, err
×
261
        }
×
UNCOV
262
        commitFee := calcStaticFee(chanType, 0)
×
UNCOV
263
        var anchorAmt btcutil.Amount
×
UNCOV
264
        if chanType.HasAnchors() {
×
UNCOV
265
                anchorAmt += 2 * AnchorSize
×
UNCOV
266
        }
×
267

UNCOV
268
        aliceBalance := lnwire.NewMSatFromSatoshis(
×
UNCOV
269
                channelBal - commitFee - anchorAmt,
×
UNCOV
270
        )
×
UNCOV
271
        bobBalance := lnwire.NewMSatFromSatoshis(channelBal)
×
UNCOV
272

×
UNCOV
273
        aliceLocalCommit := channeldb.ChannelCommitment{
×
UNCOV
274
                CommitHeight:  0,
×
UNCOV
275
                LocalBalance:  aliceBalance,
×
UNCOV
276
                RemoteBalance: bobBalance,
×
UNCOV
277
                CommitFee:     commitFee,
×
UNCOV
278
                FeePerKw:      btcutil.Amount(feePerKw),
×
UNCOV
279
                CommitTx:      aliceCommitTx,
×
UNCOV
280
                CommitSig:     testSigBytes,
×
UNCOV
281
        }
×
UNCOV
282
        aliceRemoteCommit := channeldb.ChannelCommitment{
×
UNCOV
283
                CommitHeight:  0,
×
UNCOV
284
                LocalBalance:  aliceBalance,
×
UNCOV
285
                RemoteBalance: bobBalance,
×
UNCOV
286
                CommitFee:     commitFee,
×
UNCOV
287
                FeePerKw:      btcutil.Amount(feePerKw),
×
UNCOV
288
                CommitTx:      bobCommitTx,
×
UNCOV
289
                CommitSig:     testSigBytes,
×
UNCOV
290
        }
×
UNCOV
291
        bobLocalCommit := channeldb.ChannelCommitment{
×
UNCOV
292
                CommitHeight:  0,
×
UNCOV
293
                LocalBalance:  bobBalance,
×
UNCOV
294
                RemoteBalance: aliceBalance,
×
UNCOV
295
                CommitFee:     commitFee,
×
UNCOV
296
                FeePerKw:      btcutil.Amount(feePerKw),
×
UNCOV
297
                CommitTx:      bobCommitTx,
×
UNCOV
298
                CommitSig:     testSigBytes,
×
UNCOV
299
        }
×
UNCOV
300
        bobRemoteCommit := channeldb.ChannelCommitment{
×
UNCOV
301
                CommitHeight:  0,
×
UNCOV
302
                LocalBalance:  bobBalance,
×
UNCOV
303
                RemoteBalance: aliceBalance,
×
UNCOV
304
                CommitFee:     commitFee,
×
UNCOV
305
                FeePerKw:      btcutil.Amount(feePerKw),
×
UNCOV
306
                CommitTx:      aliceCommitTx,
×
UNCOV
307
                CommitSig:     testSigBytes,
×
UNCOV
308
        }
×
UNCOV
309

×
UNCOV
310
        var chanIDBytes [8]byte
×
UNCOV
311
        if _, err := io.ReadFull(rand.Reader, chanIDBytes[:]); err != nil {
×
312
                return nil, nil, err
×
313
        }
×
314

UNCOV
315
        shortChanID := lnwire.NewShortChanIDFromInt(
×
UNCOV
316
                binary.BigEndian.Uint64(chanIDBytes[:]),
×
UNCOV
317
        )
×
UNCOV
318

×
UNCOV
319
        aliceChannelState := &channeldb.OpenChannel{
×
UNCOV
320
                LocalChanCfg:            aliceCfg,
×
UNCOV
321
                RemoteChanCfg:           bobCfg,
×
UNCOV
322
                IdentityPub:             aliceKeys[0].PubKey(),
×
UNCOV
323
                FundingOutpoint:         *prevOut,
×
UNCOV
324
                ShortChannelID:          shortChanID,
×
UNCOV
325
                ChanType:                chanType,
×
UNCOV
326
                IsInitiator:             isAliceInitiator,
×
UNCOV
327
                Capacity:                channelCapacity,
×
UNCOV
328
                RemoteCurrentRevocation: bobCommitPoint,
×
UNCOV
329
                RevocationProducer:      alicePreimageProducer,
×
UNCOV
330
                RevocationStore:         shachain.NewRevocationStore(),
×
UNCOV
331
                LocalCommitment:         aliceLocalCommit,
×
UNCOV
332
                RemoteCommitment:        aliceRemoteCommit,
×
UNCOV
333
                Db:                      dbAlice.ChannelStateDB(),
×
UNCOV
334
                Packager:                channeldb.NewChannelPackager(shortChanID),
×
UNCOV
335
                FundingTxn:              testTx,
×
UNCOV
336
        }
×
UNCOV
337
        bobChannelState := &channeldb.OpenChannel{
×
UNCOV
338
                LocalChanCfg:            bobCfg,
×
UNCOV
339
                RemoteChanCfg:           aliceCfg,
×
UNCOV
340
                IdentityPub:             bobKeys[0].PubKey(),
×
UNCOV
341
                FundingOutpoint:         *prevOut,
×
UNCOV
342
                ShortChannelID:          shortChanID,
×
UNCOV
343
                ChanType:                chanType,
×
UNCOV
344
                IsInitiator:             !isAliceInitiator,
×
UNCOV
345
                Capacity:                channelCapacity,
×
UNCOV
346
                RemoteCurrentRevocation: aliceCommitPoint,
×
UNCOV
347
                RevocationProducer:      bobPreimageProducer,
×
UNCOV
348
                RevocationStore:         shachain.NewRevocationStore(),
×
UNCOV
349
                LocalCommitment:         bobLocalCommit,
×
UNCOV
350
                RemoteCommitment:        bobRemoteCommit,
×
UNCOV
351
                Db:                      dbBob.ChannelStateDB(),
×
UNCOV
352
                Packager:                channeldb.NewChannelPackager(shortChanID),
×
UNCOV
353
        }
×
UNCOV
354

×
UNCOV
355
        // If the channel type has a tapscript root, then we'll also specify
×
UNCOV
356
        // one here to apply to both the channels.
×
UNCOV
357
        if chanType.HasTapscriptRoot() {
×
UNCOV
358
                var tapscriptRoot chainhash.Hash
×
UNCOV
359
                _, err := io.ReadFull(rand.Reader, tapscriptRoot[:])
×
UNCOV
360
                if err != nil {
×
361
                        return nil, nil, err
×
362
                }
×
363

UNCOV
364
                someRoot := fn.Some(tapscriptRoot)
×
UNCOV
365

×
UNCOV
366
                aliceChannelState.TapscriptRoot = someRoot
×
UNCOV
367
                bobChannelState.TapscriptRoot = someRoot
×
368
        }
369

UNCOV
370
        aliceSigner := input.NewMockSigner(aliceKeys, nil)
×
UNCOV
371
        bobSigner := input.NewMockSigner(bobKeys, nil)
×
UNCOV
372

×
UNCOV
373
        // TODO(roasbeef): make mock version of pre-image store
×
UNCOV
374

×
UNCOV
375
        auxSigner := NewDefaultAuxSignerMock(t)
×
UNCOV
376

×
UNCOV
377
        alicePool := NewSigPool(1, aliceSigner)
×
UNCOV
378
        channelAlice, err := NewLightningChannel(
×
UNCOV
379
                aliceSigner, aliceChannelState, alicePool,
×
UNCOV
380
                WithLeafStore(&MockAuxLeafStore{}),
×
UNCOV
381
                WithAuxSigner(auxSigner),
×
UNCOV
382
        )
×
UNCOV
383
        if err != nil {
×
384
                return nil, nil, err
×
385
        }
×
UNCOV
386
        alicePool.Start()
×
UNCOV
387
        t.Cleanup(func() {
×
UNCOV
388
                require.NoError(t, alicePool.Stop())
×
UNCOV
389
        })
×
390

UNCOV
391
        obfuscator := createStateHintObfuscator(aliceChannelState)
×
UNCOV
392

×
UNCOV
393
        bobPool := NewSigPool(1, bobSigner)
×
UNCOV
394
        channelBob, err := NewLightningChannel(
×
UNCOV
395
                bobSigner, bobChannelState, bobPool,
×
UNCOV
396
                WithLeafStore(&MockAuxLeafStore{}),
×
UNCOV
397
                WithAuxSigner(auxSigner),
×
UNCOV
398
        )
×
UNCOV
399
        if err != nil {
×
400
                return nil, nil, err
×
401
        }
×
UNCOV
402
        bobPool.Start()
×
UNCOV
403
        t.Cleanup(func() {
×
UNCOV
404
                require.NoError(t, bobPool.Stop())
×
UNCOV
405
        })
×
406

UNCOV
407
        err = SetStateNumHint(
×
UNCOV
408
                aliceCommitTx, 0, obfuscator,
×
UNCOV
409
        )
×
UNCOV
410
        if err != nil {
×
411
                return nil, nil, err
×
412
        }
×
UNCOV
413
        err = SetStateNumHint(
×
UNCOV
414
                bobCommitTx, 0, obfuscator,
×
UNCOV
415
        )
×
UNCOV
416
        if err != nil {
×
417
                return nil, nil, err
×
418
        }
×
419

UNCOV
420
        addr := &net.TCPAddr{
×
UNCOV
421
                IP:   net.ParseIP("127.0.0.1"),
×
UNCOV
422
                Port: 18556,
×
UNCOV
423
        }
×
UNCOV
424
        if err := channelAlice.channelState.SyncPending(addr, 101); err != nil {
×
425
                return nil, nil, err
×
426
        }
×
427

UNCOV
428
        addr = &net.TCPAddr{
×
UNCOV
429
                IP:   net.ParseIP("127.0.0.1"),
×
UNCOV
430
                Port: 18555,
×
UNCOV
431
        }
×
UNCOV
432

×
UNCOV
433
        if err := channelBob.channelState.SyncPending(addr, 101); err != nil {
×
434
                return nil, nil, err
×
435
        }
×
436

437
        // Now that the channel are open, simulate the start of a session by
438
        // having Alice and Bob extend their revocation windows to each other.
UNCOV
439
        err = initRevocationWindows(channelAlice, channelBob)
×
UNCOV
440
        if err != nil {
×
441
                return nil, nil, err
×
442
        }
×
443

UNCOV
444
        return channelAlice, channelBob, nil
×
445
}
446

447
// initMusigNonce is used to manually setup musig2 nonces for a new channel,
448
// outside the normal chan-reest flow.
UNCOV
449
func initMusigNonce(chanA, chanB *LightningChannel) error {
×
UNCOV
450
        chanANonces, err := chanA.GenMusigNonces()
×
UNCOV
451
        if err != nil {
×
452
                return err
×
453
        }
×
UNCOV
454
        chanBNonces, err := chanB.GenMusigNonces()
×
UNCOV
455
        if err != nil {
×
456
                return err
×
457
        }
×
458

UNCOV
459
        if err := chanA.InitRemoteMusigNonces(chanBNonces); err != nil {
×
460
                return err
×
461
        }
×
UNCOV
462
        if err := chanB.InitRemoteMusigNonces(chanANonces); err != nil {
×
463
                return err
×
464
        }
×
465

UNCOV
466
        return nil
×
467
}
468

469
// initRevocationWindows simulates a new channel being opened within the p2p
470
// network by populating the initial revocation windows of the passed
471
// commitment state machines.
UNCOV
472
func initRevocationWindows(chanA, chanB *LightningChannel) error {
×
UNCOV
473
        // If these are taproot chanenls, then we need to also simulate sending
×
UNCOV
474
        // either FundingLocked or ChannelReestablish by calling
×
UNCOV
475
        // InitRemoteMusigNonces for both sides.
×
UNCOV
476
        if chanA.channelState.ChanType.IsTaproot() {
×
UNCOV
477
                if err := initMusigNonce(chanA, chanB); err != nil {
×
478
                        return err
×
479
                }
×
480
        }
481

UNCOV
482
        aliceNextRevoke, err := chanA.NextRevocationKey()
×
UNCOV
483
        if err != nil {
×
484
                return err
×
485
        }
×
UNCOV
486
        if err := chanB.InitNextRevocation(aliceNextRevoke); err != nil {
×
487
                return err
×
488
        }
×
489

UNCOV
490
        bobNextRevoke, err := chanB.NextRevocationKey()
×
UNCOV
491
        if err != nil {
×
492
                return err
×
493
        }
×
UNCOV
494
        if err := chanA.InitNextRevocation(bobNextRevoke); err != nil {
×
495
                return err
×
496
        }
×
497

UNCOV
498
        return nil
×
499
}
500

501
// pubkeyFromHex parses a Bitcoin public key from a hex encoded string.
UNCOV
502
func pubkeyFromHex(keyHex string) (*btcec.PublicKey, error) {
×
UNCOV
503
        bytes, err := hex.DecodeString(keyHex)
×
UNCOV
504
        if err != nil {
×
505
                return nil, err
×
506
        }
×
UNCOV
507
        return btcec.ParsePubKey(bytes)
×
508
}
509

510
// privkeyFromHex parses a Bitcoin private key from a hex encoded string.
UNCOV
511
func privkeyFromHex(keyHex string) (*btcec.PrivateKey, error) {
×
UNCOV
512
        bytes, err := hex.DecodeString(keyHex)
×
UNCOV
513
        if err != nil {
×
514
                return nil, err
×
515
        }
×
UNCOV
516
        key, _ := btcec.PrivKeyFromBytes(bytes)
×
UNCOV
517
        return key, nil
×
518

519
}
520

521
// blockFromHex parses a full Bitcoin block from a hex encoded string.
522
func blockFromHex(blockHex string) (*btcutil.Block, error) {
×
523
        bytes, err := hex.DecodeString(blockHex)
×
524
        if err != nil {
×
525
                return nil, err
×
526
        }
×
527
        return btcutil.NewBlockFromBytes(bytes)
×
528
}
529

530
// txFromHex parses a full Bitcoin transaction from a hex encoded string.
UNCOV
531
func txFromHex(txHex string) (*btcutil.Tx, error) {
×
UNCOV
532
        bytes, err := hex.DecodeString(txHex)
×
UNCOV
533
        if err != nil {
×
534
                return nil, err
×
535
        }
×
UNCOV
536
        return btcutil.NewTxFromBytes(bytes)
×
537
}
538

539
// calcStaticFee calculates appropriate fees for commitment transactions.  This
540
// function provides a simple way to allow test balance assertions to take fee
541
// calculations into account.
542
//
543
// TODO(bvu): Refactor when dynamic fee estimation is added.
UNCOV
544
func calcStaticFee(chanType channeldb.ChannelType, numHTLCs int) btcutil.Amount {
×
UNCOV
545
        const (
×
UNCOV
546
                htlcWeight = 172
×
UNCOV
547
                feePerKw   = btcutil.Amount(24/4) * 1000
×
UNCOV
548
        )
×
UNCOV
549
        htlcsWeight := htlcWeight * int64(numHTLCs)
×
UNCOV
550
        totalWeight := CommitWeight(chanType) + lntypes.WeightUnit(htlcsWeight)
×
UNCOV
551

×
UNCOV
552
        return feePerKw * (btcutil.Amount(totalWeight)) / 1000
×
UNCOV
553
}
×
554

555
// ForceStateTransition executes the necessary interaction between the two
556
// commitment state machines to transition to a new state locking in any
557
// pending updates. This method is useful when testing interactions between two
558
// live state machines.
UNCOV
559
func ForceStateTransition(chanA, chanB *LightningChannel) error {
×
UNCOV
560
        aliceNewCommit, err := chanA.SignNextCommitment()
×
UNCOV
561
        if err != nil {
×
562
                return err
×
563
        }
×
UNCOV
564
        err = chanB.ReceiveNewCommitment(aliceNewCommit.CommitSigs)
×
UNCOV
565
        if err != nil {
×
566
                return err
×
567
        }
×
568

UNCOV
569
        bobRevocation, _, _, err := chanB.RevokeCurrentCommitment()
×
UNCOV
570
        if err != nil {
×
571
                return err
×
572
        }
×
UNCOV
573
        bobNewCommit, err := chanB.SignNextCommitment()
×
UNCOV
574
        if err != nil {
×
575
                return err
×
576
        }
×
577

UNCOV
578
        _, _, err = chanA.ReceiveRevocation(bobRevocation)
×
UNCOV
579
        if err != nil {
×
580
                return err
×
581
        }
×
UNCOV
582
        err = chanA.ReceiveNewCommitment(bobNewCommit.CommitSigs)
×
UNCOV
583
        if err != nil {
×
584
                return err
×
585
        }
×
586

UNCOV
587
        aliceRevocation, _, _, err := chanA.RevokeCurrentCommitment()
×
UNCOV
588
        if err != nil {
×
589
                return err
×
590
        }
×
UNCOV
591
        _, _, err = chanB.ReceiveRevocation(aliceRevocation)
×
UNCOV
592
        if err != nil {
×
593
                return err
×
594
        }
×
595

UNCOV
596
        return nil
×
597
}
598

UNCOV
599
func NewDefaultAuxSignerMock(t *testing.T) *MockAuxSigner {
×
UNCOV
600
        auxSigner := &MockAuxSigner{}
×
UNCOV
601

×
UNCOV
602
        type testSigBlob struct {
×
UNCOV
603
                BlobInt tlv.RecordT[tlv.TlvType65634, uint16]
×
UNCOV
604
        }
×
UNCOV
605

×
UNCOV
606
        var sigBlobBuf bytes.Buffer
×
UNCOV
607
        sigBlob := testSigBlob{
×
UNCOV
608
                BlobInt: tlv.NewPrimitiveRecord[tlv.TlvType65634, uint16](5),
×
UNCOV
609
        }
×
UNCOV
610
        tlvStream, err := tlv.NewStream(sigBlob.BlobInt.Record())
×
UNCOV
611
        require.NoError(t, err, "unable to create tlv stream")
×
UNCOV
612
        require.NoError(t, tlvStream.Encode(&sigBlobBuf))
×
UNCOV
613

×
UNCOV
614
        auxSigner.On(
×
UNCOV
615
                "SubmitSecondLevelSigBatch", mock.Anything, mock.Anything,
×
UNCOV
616
                mock.Anything,
×
UNCOV
617
        ).Return(nil)
×
UNCOV
618
        auxSigner.On(
×
UNCOV
619
                "PackSigs", mock.Anything,
×
UNCOV
620
        ).Return(fn.Ok(fn.Some(sigBlobBuf.Bytes())))
×
UNCOV
621
        auxSigner.On(
×
UNCOV
622
                "UnpackSigs", mock.Anything,
×
UNCOV
623
        ).Return(fn.Ok([]fn.Option[tlv.Blob]{
×
UNCOV
624
                fn.Some(sigBlobBuf.Bytes()),
×
UNCOV
625
        }))
×
UNCOV
626
        auxSigner.On(
×
UNCOV
627
                "VerifySecondLevelSigs", mock.Anything, mock.Anything,
×
UNCOV
628
                mock.Anything,
×
UNCOV
629
        ).Return(nil)
×
UNCOV
630

×
UNCOV
631
        return auxSigner
×
UNCOV
632
}
×
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