• 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/mock.go
1
package lnwallet
2

3
import (
4
        "encoding/hex"
5
        "sync/atomic"
6
        "time"
7

8
        "github.com/btcsuite/btcd/btcec/v2"
9
        "github.com/btcsuite/btcd/btcutil"
10
        "github.com/btcsuite/btcd/btcutil/hdkeychain"
11
        "github.com/btcsuite/btcd/btcutil/psbt"
12
        "github.com/btcsuite/btcd/chaincfg"
13
        "github.com/btcsuite/btcd/chaincfg/chainhash"
14
        "github.com/btcsuite/btcd/wire"
15
        "github.com/btcsuite/btcwallet/waddrmgr"
16
        base "github.com/btcsuite/btcwallet/wallet"
17
        "github.com/btcsuite/btcwallet/wallet/txauthor"
18
        "github.com/btcsuite/btcwallet/wtxmgr"
19
        "github.com/lightningnetwork/lnd/chainntnfs"
20
        "github.com/lightningnetwork/lnd/channeldb"
21
        "github.com/lightningnetwork/lnd/fn"
22
        "github.com/lightningnetwork/lnd/lnwallet/chainfee"
23
        "github.com/lightningnetwork/lnd/tlv"
24
        "github.com/stretchr/testify/mock"
25
)
26

27
var (
28
        CoinPkScript, _ = hex.DecodeString(
29
                "001431df1bde03c074d0cf21ea2529427e1499b8f1de",
30
        )
31
)
32

33
// mockWalletController is a mock implementation of the WalletController
34
// interface. It let's us mock the interaction with the bitcoin network.
35
type mockWalletController struct {
36
        RootKey               *btcec.PrivateKey
37
        PublishedTransactions chan *wire.MsgTx
38
        index                 uint32
39
        Utxos                 []*Utxo
40
}
41

42
// A compile time check to ensure that mockWalletController implements the
43
// WalletController.
44
var _ WalletController = (*mockWalletController)(nil)
45

46
// BackEnd returns "mock" to signify a mock wallet controller.
47
func (w *mockWalletController) BackEnd() string {
×
48
        return "mock"
×
49
}
×
50

51
// FetchOutpointInfo will be called to get info about the inputs to the funding
52
// transaction.
53
func (w *mockWalletController) FetchOutpointInfo(
54
        prevOut *wire.OutPoint) (*Utxo, error) {
×
55

×
56
        utxo := &Utxo{
×
57
                AddressType:   WitnessPubKey,
×
58
                Value:         10 * btcutil.SatoshiPerBitcoin,
×
59
                PkScript:      []byte("dummy"),
×
60
                Confirmations: 1,
×
61
                OutPoint:      *prevOut,
×
62
        }
×
63

×
64
        return utxo, nil
×
65
}
×
66

67
// ScriptForOutput returns the address, witness program and redeem script for a
68
// given UTXO. An error is returned if the UTXO does not belong to our wallet or
69
// it is not a managed pubKey address.
70
func (w *mockWalletController) ScriptForOutput(*wire.TxOut) (
71
        waddrmgr.ManagedPubKeyAddress, []byte, []byte, error) {
×
72

×
73
        return nil, nil, nil, nil
×
74
}
×
75

76
// ConfirmedBalance currently returns dummy values.
77
func (w *mockWalletController) ConfirmedBalance(int32, string) (btcutil.Amount,
78
        error) {
×
79

×
80
        return 0, nil
×
81
}
×
82

83
// NewAddress is called to get new addresses for delivery, change etc.
84
func (w *mockWalletController) NewAddress(AddressType, bool,
85
        string) (btcutil.Address, error) {
×
86

×
87
        addr, _ := btcutil.NewAddressPubKey(
×
88
                w.RootKey.PubKey().SerializeCompressed(),
×
89
                &chaincfg.MainNetParams,
×
90
        )
×
91

×
92
        return addr, nil
×
93
}
×
94

95
// LastUnusedAddress currently returns dummy values.
96
func (w *mockWalletController) LastUnusedAddress(AddressType,
97
        string) (btcutil.Address, error) {
×
98

×
99
        return nil, nil
×
100
}
×
101

102
// IsOurAddress currently returns a dummy value.
103
func (w *mockWalletController) IsOurAddress(btcutil.Address) bool {
×
104
        return false
×
105
}
×
106

107
// AddressInfo currently returns a dummy value.
108
func (w *mockWalletController) AddressInfo(
109
        btcutil.Address) (waddrmgr.ManagedAddress, error) {
×
110

×
111
        return nil, nil
×
112
}
×
113

114
// ListAccounts currently returns a dummy value.
115
func (w *mockWalletController) ListAccounts(string,
116
        *waddrmgr.KeyScope) ([]*waddrmgr.AccountProperties, error) {
×
117

×
118
        return nil, nil
×
119
}
×
120

121
// RequiredReserve currently returns a dummy value.
122
func (w *mockWalletController) RequiredReserve(uint32) btcutil.Amount {
×
123
        return 0
×
124
}
×
125

126
// ListAddresses currently returns a dummy value.
127
func (w *mockWalletController) ListAddresses(string,
128
        bool) (AccountAddressMap, error) {
×
129

×
130
        return nil, nil
×
131
}
×
132

133
// ImportAccount currently returns a dummy value.
134
func (w *mockWalletController) ImportAccount(string, *hdkeychain.ExtendedKey,
135
        uint32, *waddrmgr.AddressType, bool) (*waddrmgr.AccountProperties,
136
        []btcutil.Address, []btcutil.Address, error) {
×
137

×
138
        return nil, nil, nil, nil
×
139
}
×
140

141
// ImportPublicKey currently returns a dummy value.
142
func (w *mockWalletController) ImportPublicKey(*btcec.PublicKey,
143
        waddrmgr.AddressType) error {
×
144

×
145
        return nil
×
146
}
×
147

148
// ImportTaprootScript currently returns a dummy value.
149
func (w *mockWalletController) ImportTaprootScript(waddrmgr.KeyScope,
150
        *waddrmgr.Tapscript) (waddrmgr.ManagedAddress, error) {
×
151

×
152
        return nil, nil
×
153
}
×
154

155
// SendOutputs currently returns dummy values.
156
func (w *mockWalletController) SendOutputs(fn.Set[wire.OutPoint], []*wire.TxOut,
157
        chainfee.SatPerKWeight, int32, string,
158
        base.CoinSelectionStrategy) (*wire.MsgTx, error) {
×
159

×
160
        return nil, nil
×
161
}
×
162

163
// CreateSimpleTx currently returns dummy values.
164
func (w *mockWalletController) CreateSimpleTx(fn.Set[wire.OutPoint],
165
        []*wire.TxOut, chainfee.SatPerKWeight, int32,
166
        base.CoinSelectionStrategy, bool) (*txauthor.AuthoredTx, error) {
×
167

×
168
        return nil, nil
×
169
}
×
170

171
// ListUnspentWitness is called by the wallet when doing coin selection. We just
172
// need one unspent for the funding transaction.
173
func (w *mockWalletController) ListUnspentWitness(int32, int32,
174
        string) ([]*Utxo, error) {
×
175

×
176
        // If the mock already has a list of utxos, return it.
×
177
        if w.Utxos != nil {
×
178
                return w.Utxos, nil
×
179
        }
×
180

181
        // Otherwise create one to return.
182
        utxo := &Utxo{
×
183
                AddressType: WitnessPubKey,
×
184
                Value:       btcutil.Amount(10 * btcutil.SatoshiPerBitcoin),
×
185
                PkScript:    CoinPkScript,
×
186
                OutPoint: wire.OutPoint{
×
187
                        Hash:  chainhash.Hash{},
×
188
                        Index: w.index,
×
189
                },
×
190
        }
×
191
        atomic.AddUint32(&w.index, 1)
×
192
        var ret []*Utxo
×
193
        ret = append(ret, utxo)
×
194

×
195
        return ret, nil
×
196
}
197

198
// ListTransactionDetails currently returns dummy values.
199
func (w *mockWalletController) ListTransactionDetails(int32, int32,
200
        string) ([]*TransactionDetail, error) {
×
201

×
202
        return nil, nil
×
203
}
×
204

205
// LeaseOutput returns the current time and a nil error.
206
func (w *mockWalletController) LeaseOutput(wtxmgr.LockID, wire.OutPoint,
207
        time.Duration) (time.Time, error) {
×
208

×
209
        return time.Now(), nil
×
210
}
×
211

212
// ReleaseOutput currently does nothing.
213
func (w *mockWalletController) ReleaseOutput(wtxmgr.LockID,
214
        wire.OutPoint) error {
×
215

×
216
        return nil
×
217
}
×
218

219
func (w *mockWalletController) ListLeasedOutputs() (
220
        []*base.ListLeasedOutputResult, error) {
×
221

×
222
        return nil, nil
×
223
}
×
224

225
// FundPsbt currently does nothing.
226
func (w *mockWalletController) FundPsbt(*psbt.Packet, int32,
227
        chainfee.SatPerKWeight, string, *waddrmgr.KeyScope,
228
        base.CoinSelectionStrategy, func(utxo wtxmgr.Credit) bool) (int32,
229
        error) {
×
230

×
231
        return 0, nil
×
232
}
×
233

234
// SignPsbt currently does nothing.
235
func (w *mockWalletController) SignPsbt(*psbt.Packet) ([]uint32, error) {
×
236
        return nil, nil
×
237
}
×
238

239
// FinalizePsbt currently does nothing.
240
func (w *mockWalletController) FinalizePsbt(_ *psbt.Packet, _ string) error {
×
241
        return nil
×
242
}
×
243

244
// DecorateInputs currently does nothing.
245
func (w *mockWalletController) DecorateInputs(*psbt.Packet, bool) error {
×
246
        return nil
×
247
}
×
248

249
// PublishTransaction sends a transaction to the PublishedTransactions chan.
250
func (w *mockWalletController) PublishTransaction(tx *wire.MsgTx,
UNCOV
251
        _ string) error {
×
UNCOV
252

×
UNCOV
253
        w.PublishedTransactions <- tx
×
UNCOV
254
        return nil
×
UNCOV
255
}
×
256

257
// GetTransactionDetails currently does nothing.
258
func (w *mockWalletController) GetTransactionDetails(*chainhash.Hash) (
259
        *TransactionDetail, error) {
×
260

×
261
        return nil, nil
×
262
}
×
263

264
// LabelTransaction currently does nothing.
265
func (w *mockWalletController) LabelTransaction(chainhash.Hash, string,
266
        bool) error {
×
267

×
268
        return nil
×
269
}
×
270

271
// SubscribeTransactions currently does nothing.
272
func (w *mockWalletController) SubscribeTransactions() (TransactionSubscription,
273
        error) {
×
274

×
275
        return nil, nil
×
276
}
×
277

278
// IsSynced currently returns dummy values.
279
func (w *mockWalletController) IsSynced() (bool, int64, error) {
×
280
        return true, int64(0), nil
×
281
}
×
282

283
// GetRecoveryInfo currently returns dummy values.
284
func (w *mockWalletController) GetRecoveryInfo() (bool, float64, error) {
×
285
        return true, float64(1), nil
×
286
}
×
287

288
// Start currently does nothing.
UNCOV
289
func (w *mockWalletController) Start() error {
×
UNCOV
290
        return nil
×
UNCOV
291
}
×
292

293
// Stop currently does nothing.
UNCOV
294
func (w *mockWalletController) Stop() error {
×
UNCOV
295
        return nil
×
UNCOV
296
}
×
297

298
func (w *mockWalletController) FetchTx(chainhash.Hash) (*wire.MsgTx, error) {
×
299
        return nil, nil
×
300
}
×
301

302
func (w *mockWalletController) RemoveDescendants(*wire.MsgTx) error {
×
303
        return nil
×
304
}
×
305

306
// FetchDerivationInfo queries for the wallet's knowledge of the passed
307
// pkScript and constructs the derivation info and returns it.
308
func (w *mockWalletController) FetchDerivationInfo(
309
        pkScript []byte) (*psbt.Bip32Derivation, error) {
×
310

×
311
        return nil, nil
×
312
}
×
313

314
func (w *mockWalletController) CheckMempoolAcceptance(tx *wire.MsgTx) error {
×
315
        return nil
×
316
}
×
317

318
// mockChainNotifier is a mock implementation of the ChainNotifier interface.
319
type mockChainNotifier struct {
320
        SpendChan chan *chainntnfs.SpendDetail
321
        EpochChan chan *chainntnfs.BlockEpoch
322
        ConfChan  chan *chainntnfs.TxConfirmation
323
}
324

325
// RegisterConfirmationsNtfn returns a ConfirmationEvent that contains a channel
326
// that the tx confirmation will go over.
327
func (c *mockChainNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash,
328
        pkScript []byte, numConfs, heightHint uint32,
329
        opts ...chainntnfs.NotifierOption) (*chainntnfs.ConfirmationEvent,
UNCOV
330
        error) {
×
UNCOV
331

×
UNCOV
332
        return &chainntnfs.ConfirmationEvent{
×
UNCOV
333
                Confirmed: c.ConfChan,
×
UNCOV
334
                Cancel:    func() {},
×
335
        }, nil
336
}
337

338
// RegisterSpendNtfn returns a SpendEvent that contains a channel that the spend
339
// details will go over.
340
func (c *mockChainNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint,
341
        pkScript []byte, heightHint uint32) (*chainntnfs.SpendEvent, error) {
×
342

×
343
        return &chainntnfs.SpendEvent{
×
344
                Spend:  c.SpendChan,
×
345
                Cancel: func() {},
×
346
        }, nil
347
}
348

349
// RegisterBlockEpochNtfn returns a BlockEpochEvent that contains a channel that
350
// block epochs will go over.
351
func (c *mockChainNotifier) RegisterBlockEpochNtfn(
352
        blockEpoch *chainntnfs.BlockEpoch) (*chainntnfs.BlockEpochEvent,
353
        error) {
×
354

×
355
        return &chainntnfs.BlockEpochEvent{
×
356
                Epochs: c.EpochChan,
×
357
                Cancel: func() {},
×
358
        }, nil
359
}
360

361
// Start currently returns a dummy value.
362
func (c *mockChainNotifier) Start() error {
×
363
        return nil
×
364
}
×
365

366
// Started currently returns a dummy value.
367
func (c *mockChainNotifier) Started() bool {
×
368
        return true
×
369
}
×
370

371
// Stop currently returns a dummy value.
372
func (c *mockChainNotifier) Stop() error {
×
373
        return nil
×
374
}
×
375

376
type mockChainIO struct{}
377

UNCOV
378
func (*mockChainIO) GetBestBlock() (*chainhash.Hash, int32, error) {
×
UNCOV
379
        return nil, 0, nil
×
UNCOV
380
}
×
381

382
func (*mockChainIO) GetUtxo(op *wire.OutPoint, _ []byte,
383
        heightHint uint32, _ <-chan struct{}) (*wire.TxOut, error) {
×
384

×
385
        return nil, nil
×
386
}
×
387

388
func (*mockChainIO) GetBlockHash(blockHeight int64) (*chainhash.Hash, error) {
×
389
        return nil, nil
×
390
}
×
391

392
func (*mockChainIO) GetBlock(blockHash *chainhash.Hash) (*wire.MsgBlock,
393
        error) {
×
394

×
395
        return nil, nil
×
396
}
×
397

398
func (*mockChainIO) GetBlockHeader(
399
        blockHash *chainhash.Hash) (*wire.BlockHeader, error) {
×
400

×
401
        return nil, nil
×
402
}
×
403

404
type MockAuxLeafStore struct{}
405

406
// A compile time check to ensure that MockAuxLeafStore implements the
407
// AuxLeafStore interface.
408
var _ AuxLeafStore = (*MockAuxLeafStore)(nil)
409

410
// FetchLeavesFromView attempts to fetch the auxiliary leaves that
411
// correspond to the passed aux blob, and pending original (unfiltered)
412
// HTLC view.
413
func (*MockAuxLeafStore) FetchLeavesFromView(
414
        _ CommitDiffAuxInput) fn.Result[CommitDiffAuxResult] {
×
415

×
416
        return fn.Ok(CommitDiffAuxResult{})
×
417
}
×
418

419
// FetchLeavesFromCommit attempts to fetch the auxiliary leaves that
420
// correspond to the passed aux blob, and an existing channel
421
// commitment.
422
func (*MockAuxLeafStore) FetchLeavesFromCommit(_ AuxChanState,
423
        _ channeldb.ChannelCommitment,
UNCOV
424
        _ CommitmentKeyRing) fn.Result[CommitDiffAuxResult] {
×
UNCOV
425

×
UNCOV
426
        return fn.Ok(CommitDiffAuxResult{})
×
UNCOV
427
}
×
428

429
// FetchLeavesFromRevocation attempts to fetch the auxiliary leaves
430
// from a channel revocation that stores balance + blob information.
431
func (*MockAuxLeafStore) FetchLeavesFromRevocation(
UNCOV
432
        _ *channeldb.RevocationLog) fn.Result[CommitDiffAuxResult] {
×
UNCOV
433

×
UNCOV
434
        return fn.Ok(CommitDiffAuxResult{})
×
UNCOV
435
}
×
436

437
// ApplyHtlcView serves as the state transition function for the custom
438
// channel's blob. Given the old blob, and an HTLC view, then a new
439
// blob should be returned that reflects the pending updates.
440
func (*MockAuxLeafStore) ApplyHtlcView(
441
        _ CommitDiffAuxInput) fn.Result[fn.Option[tlv.Blob]] {
×
442

×
443
        return fn.Ok(fn.None[tlv.Blob]())
×
444
}
×
445

446
// MockAuxSigner is a mock implementation of the AuxSigner interface.
447
type MockAuxSigner struct {
448
        mock.Mock
449
}
450

451
// SubmitSecondLevelSigBatch takes a batch of aux sign jobs and
452
// processes them asynchronously.
453
func (a *MockAuxSigner) SubmitSecondLevelSigBatch(chanState AuxChanState,
UNCOV
454
        tx *wire.MsgTx, jobs []AuxSigJob) error {
×
UNCOV
455

×
UNCOV
456
        args := a.Called(chanState, tx, jobs)
×
UNCOV
457

×
UNCOV
458
        // While we return, we'll also send back an instant response for the
×
UNCOV
459
        // set of jobs.
×
UNCOV
460
        for _, sigJob := range jobs {
×
UNCOV
461
                sigJob.Resp <- AuxSigJobResp{}
×
UNCOV
462
        }
×
463

UNCOV
464
        return args.Error(0)
×
465
}
466

467
// PackSigs takes a series of aux signatures and packs them into a
468
// single blob that can be sent alongside the CommitSig messages.
469
func (a *MockAuxSigner) PackSigs(
UNCOV
470
        sigs []fn.Option[tlv.Blob]) fn.Result[fn.Option[tlv.Blob]] {
×
UNCOV
471

×
UNCOV
472
        args := a.Called(sigs)
×
UNCOV
473

×
UNCOV
474
        return args.Get(0).(fn.Result[fn.Option[tlv.Blob]])
×
UNCOV
475
}
×
476

477
// UnpackSigs takes a packed blob of signatures and returns the
478
// original signatures for each HTLC, keyed by HTLC index.
479
func (a *MockAuxSigner) UnpackSigs(
UNCOV
480
        sigs fn.Option[tlv.Blob]) fn.Result[[]fn.Option[tlv.Blob]] {
×
UNCOV
481

×
UNCOV
482
        args := a.Called(sigs)
×
UNCOV
483

×
UNCOV
484
        return args.Get(0).(fn.Result[[]fn.Option[tlv.Blob]])
×
UNCOV
485
}
×
486

487
// VerifySecondLevelSigs attempts to synchronously verify a batch of aux
488
// sig jobs.
489
func (a *MockAuxSigner) VerifySecondLevelSigs(chanState AuxChanState,
UNCOV
490
        tx *wire.MsgTx, jobs []AuxVerifyJob) error {
×
UNCOV
491

×
UNCOV
492
        args := a.Called(chanState, tx, jobs)
×
UNCOV
493

×
UNCOV
494
        return args.Error(0)
×
UNCOV
495
}
×
496

497
type MockAuxContractResolver struct{}
498

499
// ResolveContract is called to resolve a contract that needs
500
// additional information to resolve properly. If no extra information
501
// is required, a nil Result error is returned.
502
func (*MockAuxContractResolver) ResolveContract(
UNCOV
503
        ResolutionReq) fn.Result[tlv.Blob] {
×
UNCOV
504

×
UNCOV
505
        return fn.Ok[tlv.Blob](nil)
×
UNCOV
506
}
×
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