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

lightningnetwork / lnd / 9915780197

13 Jul 2024 12:30AM UTC coverage: 49.268% (-9.1%) from 58.413%
9915780197

push

github

web-flow
Merge pull request #8653 from ProofOfKeags/fn-prim

DynComms [0/n]: `fn` package additions

92837 of 188433 relevant lines covered (49.27%)

1.55 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/lnwallet/chainfee"
21
)
22

23
var (
24
        CoinPkScript, _ = hex.DecodeString(
25
                "001431df1bde03c074d0cf21ea2529427e1499b8f1de",
26
        )
27
)
28

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

38
// BackEnd returns "mock" to signify a mock wallet controller.
39
func (w *mockWalletController) BackEnd() string {
×
40
        return "mock"
×
41
}
×
42

43
// FetchInputInfo will be called to get info about the inputs to the funding
44
// transaction.
45
func (w *mockWalletController) FetchInputInfo(
46
        prevOut *wire.OutPoint) (*Utxo, error) {
×
47

×
48
        utxo := &Utxo{
×
49
                AddressType:   WitnessPubKey,
×
50
                Value:         10 * btcutil.SatoshiPerBitcoin,
×
51
                PkScript:      []byte("dummy"),
×
52
                Confirmations: 1,
×
53
                OutPoint:      *prevOut,
×
54
        }
×
55

×
56
        return utxo, nil
×
57
}
×
58

59
// ScriptForOutput returns the address, witness program and redeem script for a
60
// given UTXO. An error is returned if the UTXO does not belong to our wallet or
61
// it is not a managed pubKey address.
62
func (w *mockWalletController) ScriptForOutput(*wire.TxOut) (
63
        waddrmgr.ManagedPubKeyAddress, []byte, []byte, error) {
×
64

×
65
        return nil, nil, nil, nil
×
66
}
×
67

68
// ConfirmedBalance currently returns dummy values.
69
func (w *mockWalletController) ConfirmedBalance(int32, string) (btcutil.Amount,
70
        error) {
×
71

×
72
        return 0, nil
×
73
}
×
74

75
// NewAddress is called to get new addresses for delivery, change etc.
76
func (w *mockWalletController) NewAddress(AddressType, bool,
77
        string) (btcutil.Address, error) {
×
78

×
79
        addr, _ := btcutil.NewAddressPubKey(
×
80
                w.RootKey.PubKey().SerializeCompressed(),
×
81
                &chaincfg.MainNetParams,
×
82
        )
×
83

×
84
        return addr, nil
×
85
}
×
86

87
// LastUnusedAddress currently returns dummy values.
88
func (w *mockWalletController) LastUnusedAddress(AddressType,
89
        string) (btcutil.Address, error) {
×
90

×
91
        return nil, nil
×
92
}
×
93

94
// IsOurAddress currently returns a dummy value.
95
func (w *mockWalletController) IsOurAddress(btcutil.Address) bool {
×
96
        return false
×
97
}
×
98

99
// AddressInfo currently returns a dummy value.
100
func (w *mockWalletController) AddressInfo(
101
        btcutil.Address) (waddrmgr.ManagedAddress, error) {
×
102

×
103
        return nil, nil
×
104
}
×
105

106
// ListAccounts currently returns a dummy value.
107
func (w *mockWalletController) ListAccounts(string,
108
        *waddrmgr.KeyScope) ([]*waddrmgr.AccountProperties, error) {
×
109

×
110
        return nil, nil
×
111
}
×
112

113
// RequiredReserve currently returns a dummy value.
114
func (w *mockWalletController) RequiredReserve(uint32) btcutil.Amount {
×
115
        return 0
×
116
}
×
117

118
// ListAddresses currently returns a dummy value.
119
func (w *mockWalletController) ListAddresses(string,
120
        bool) (AccountAddressMap, error) {
×
121

×
122
        return nil, nil
×
123
}
×
124

125
// ImportAccount currently returns a dummy value.
126
func (w *mockWalletController) ImportAccount(string, *hdkeychain.ExtendedKey,
127
        uint32, *waddrmgr.AddressType, bool) (*waddrmgr.AccountProperties,
128
        []btcutil.Address, []btcutil.Address, error) {
×
129

×
130
        return nil, nil, nil, nil
×
131
}
×
132

133
// ImportPublicKey currently returns a dummy value.
134
func (w *mockWalletController) ImportPublicKey(*btcec.PublicKey,
135
        waddrmgr.AddressType) error {
×
136

×
137
        return nil
×
138
}
×
139

140
// ImportTaprootScript currently returns a dummy value.
141
func (w *mockWalletController) ImportTaprootScript(waddrmgr.KeyScope,
142
        *waddrmgr.Tapscript) (waddrmgr.ManagedAddress, error) {
×
143

×
144
        return nil, nil
×
145
}
×
146

147
// SendOutputs currently returns dummy values.
148
func (w *mockWalletController) SendOutputs([]*wire.TxOut,
149
        chainfee.SatPerKWeight, int32, string,
150
        base.CoinSelectionStrategy) (*wire.MsgTx, error) {
×
151

×
152
        return nil, nil
×
153
}
×
154

155
// CreateSimpleTx currently returns dummy values.
156
func (w *mockWalletController) CreateSimpleTx([]*wire.TxOut,
157
        chainfee.SatPerKWeight, int32, base.CoinSelectionStrategy,
158
        bool) (*txauthor.AuthoredTx, error) {
×
159

×
160
        return nil, nil
×
161
}
×
162

163
// ListUnspentWitness is called by the wallet when doing coin selection. We just
164
// need one unspent for the funding transaction.
165
func (w *mockWalletController) ListUnspentWitness(int32, int32,
166
        string) ([]*Utxo, error) {
×
167

×
168
        // If the mock already has a list of utxos, return it.
×
169
        if w.Utxos != nil {
×
170
                return w.Utxos, nil
×
171
        }
×
172

173
        // Otherwise create one to return.
174
        utxo := &Utxo{
×
175
                AddressType: WitnessPubKey,
×
176
                Value:       btcutil.Amount(10 * btcutil.SatoshiPerBitcoin),
×
177
                PkScript:    CoinPkScript,
×
178
                OutPoint: wire.OutPoint{
×
179
                        Hash:  chainhash.Hash{},
×
180
                        Index: w.index,
×
181
                },
×
182
        }
×
183
        atomic.AddUint32(&w.index, 1)
×
184
        var ret []*Utxo
×
185
        ret = append(ret, utxo)
×
186

×
187
        return ret, nil
×
188
}
189

190
// ListTransactionDetails currently returns dummy values.
191
func (w *mockWalletController) ListTransactionDetails(int32, int32,
192
        string) ([]*TransactionDetail, error) {
×
193

×
194
        return nil, nil
×
195
}
×
196

197
// LeaseOutput returns the current time and a nil error.
198
func (w *mockWalletController) LeaseOutput(wtxmgr.LockID, wire.OutPoint,
199
        time.Duration) (time.Time, []byte, btcutil.Amount, error) {
×
200

×
201
        return time.Now(), nil, 0, nil
×
202
}
×
203

204
// ReleaseOutput currently does nothing.
205
func (w *mockWalletController) ReleaseOutput(wtxmgr.LockID,
206
        wire.OutPoint) error {
×
207

×
208
        return nil
×
209
}
×
210

211
func (w *mockWalletController) ListLeasedOutputs() (
212
        []*base.ListLeasedOutputResult, error) {
×
213

×
214
        return nil, nil
×
215
}
×
216

217
// FundPsbt currently does nothing.
218
func (w *mockWalletController) FundPsbt(*psbt.Packet, int32,
219
        chainfee.SatPerKWeight, string, *waddrmgr.KeyScope,
220
        base.CoinSelectionStrategy, func(utxo wtxmgr.Credit) bool) (int32,
221
        error) {
×
222

×
223
        return 0, nil
×
224
}
×
225

226
// SignPsbt currently does nothing.
227
func (w *mockWalletController) SignPsbt(*psbt.Packet) ([]uint32, error) {
×
228
        return nil, nil
×
229
}
×
230

231
// FinalizePsbt currently does nothing.
232
func (w *mockWalletController) FinalizePsbt(_ *psbt.Packet, _ string) error {
×
233
        return nil
×
234
}
×
235

236
// DecorateInputs currently does nothing.
237
func (w *mockWalletController) DecorateInputs(*psbt.Packet, bool) error {
×
238
        return nil
×
239
}
×
240

241
// PublishTransaction sends a transaction to the PublishedTransactions chan.
242
func (w *mockWalletController) PublishTransaction(tx *wire.MsgTx,
243
        _ string) error {
×
244

×
245
        w.PublishedTransactions <- tx
×
246
        return nil
×
247
}
×
248

249
// GetTransactionDetails currently does nothing.
250
func (w *mockWalletController) GetTransactionDetails(*chainhash.Hash) (
251
        *TransactionDetail, error) {
×
252

×
253
        return nil, nil
×
254
}
×
255

256
// LabelTransaction currently does nothing.
257
func (w *mockWalletController) LabelTransaction(chainhash.Hash, string,
258
        bool) error {
×
259

×
260
        return nil
×
261
}
×
262

263
// SubscribeTransactions currently does nothing.
264
func (w *mockWalletController) SubscribeTransactions() (TransactionSubscription,
265
        error) {
×
266

×
267
        return nil, nil
×
268
}
×
269

270
// IsSynced currently returns dummy values.
271
func (w *mockWalletController) IsSynced() (bool, int64, error) {
×
272
        return true, int64(0), nil
×
273
}
×
274

275
// GetRecoveryInfo currently returns dummy values.
276
func (w *mockWalletController) GetRecoveryInfo() (bool, float64, error) {
×
277
        return true, float64(1), nil
×
278
}
×
279

280
// Start currently does nothing.
281
func (w *mockWalletController) Start() error {
×
282
        return nil
×
283
}
×
284

285
// Stop currently does nothing.
286
func (w *mockWalletController) Stop() error {
×
287
        return nil
×
288
}
×
289

290
func (w *mockWalletController) FetchTx(chainhash.Hash) (*wire.MsgTx, error) {
×
291
        return nil, nil
×
292
}
×
293

294
func (w *mockWalletController) RemoveDescendants(*wire.MsgTx) error {
×
295
        return nil
×
296
}
×
297

298
func (w *mockWalletController) CheckMempoolAcceptance(tx *wire.MsgTx) error {
×
299
        return nil
×
300
}
×
301

302
// mockChainNotifier is a mock implementation of the ChainNotifier interface.
303
type mockChainNotifier struct {
304
        SpendChan chan *chainntnfs.SpendDetail
305
        EpochChan chan *chainntnfs.BlockEpoch
306
        ConfChan  chan *chainntnfs.TxConfirmation
307
}
308

309
// RegisterConfirmationsNtfn returns a ConfirmationEvent that contains a channel
310
// that the tx confirmation will go over.
311
func (c *mockChainNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash,
312
        pkScript []byte, numConfs, heightHint uint32,
313
        opts ...chainntnfs.NotifierOption) (*chainntnfs.ConfirmationEvent,
314
        error) {
×
315

×
316
        return &chainntnfs.ConfirmationEvent{
×
317
                Confirmed: c.ConfChan,
×
318
                Cancel:    func() {},
×
319
        }, nil
320
}
321

322
// RegisterSpendNtfn returns a SpendEvent that contains a channel that the spend
323
// details will go over.
324
func (c *mockChainNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint,
325
        pkScript []byte, heightHint uint32) (*chainntnfs.SpendEvent, error) {
×
326

×
327
        return &chainntnfs.SpendEvent{
×
328
                Spend:  c.SpendChan,
×
329
                Cancel: func() {},
×
330
        }, nil
331
}
332

333
// RegisterBlockEpochNtfn returns a BlockEpochEvent that contains a channel that
334
// block epochs will go over.
335
func (c *mockChainNotifier) RegisterBlockEpochNtfn(
336
        blockEpoch *chainntnfs.BlockEpoch) (*chainntnfs.BlockEpochEvent,
337
        error) {
×
338

×
339
        return &chainntnfs.BlockEpochEvent{
×
340
                Epochs: c.EpochChan,
×
341
                Cancel: func() {},
×
342
        }, nil
343
}
344

345
// Start currently returns a dummy value.
346
func (c *mockChainNotifier) Start() error {
×
347
        return nil
×
348
}
×
349

350
// Started currently returns a dummy value.
351
func (c *mockChainNotifier) Started() bool {
×
352
        return true
×
353
}
×
354

355
// Stop currently returns a dummy value.
356
func (c *mockChainNotifier) Stop() error {
×
357
        return nil
×
358
}
×
359

360
type mockChainIO struct{}
361

362
func (*mockChainIO) GetBestBlock() (*chainhash.Hash, int32, error) {
×
363
        return nil, 0, nil
×
364
}
×
365

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

×
369
        return nil, nil
×
370
}
×
371

372
func (*mockChainIO) GetBlockHash(blockHeight int64) (*chainhash.Hash, error) {
×
373
        return nil, nil
×
374
}
×
375

376
func (*mockChainIO) GetBlock(blockHash *chainhash.Hash) (*wire.MsgBlock,
377
        error) {
×
378

×
379
        return nil, nil
×
380
}
×
381

382
func (*mockChainIO) GetBlockHeader(
383
        blockHash *chainhash.Hash) (*wire.BlockHeader, error) {
×
384

×
385
        return nil, nil
×
386
}
×
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