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

lightningnetwork / lnd / 16024244444

02 Jul 2025 11:46AM UTC coverage: 57.264% (-0.5%) from 57.803%
16024244444

Pull #10012

github

web-flow
Merge 32af9d5c7 into 1d2e5472b
Pull Request #10012: multi: prevent goroutine leak in brontide

3 of 29 new or added lines in 2 files covered. (10.34%)

1169 existing lines in 16 files now uncovered.

97585 of 170411 relevant lines covered (57.26%)

1.19 hits per line

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

0.0
/chainntnfs/btcdnotify/btcd.go
1
package btcdnotify
2

3
import (
4
        "errors"
5
        "fmt"
6
        "sync"
7
        "sync/atomic"
8
        "time"
9

10
        "github.com/btcsuite/btcd/btcjson"
11
        "github.com/btcsuite/btcd/btcutil"
12
        "github.com/btcsuite/btcd/chaincfg"
13
        "github.com/btcsuite/btcd/chaincfg/chainhash"
14
        "github.com/btcsuite/btcd/rpcclient"
15
        "github.com/btcsuite/btcd/txscript"
16
        "github.com/btcsuite/btcd/wire"
17
        "github.com/btcsuite/btcwallet/chain"
18
        "github.com/lightningnetwork/lnd/blockcache"
19
        "github.com/lightningnetwork/lnd/chainntnfs"
20
        "github.com/lightningnetwork/lnd/fn/v2"
21
        "github.com/lightningnetwork/lnd/queue"
22
)
23

24
const (
25
        // notifierType uniquely identifies this concrete implementation of the
26
        // ChainNotifier interface.
27
        notifierType = "btcd"
28
)
29

30
// chainUpdate encapsulates an update to the current main chain. This struct is
31
// used as an element within an unbounded queue in order to avoid blocking the
32
// main rpc dispatch rule.
33
type chainUpdate struct {
34
        blockHash   *chainhash.Hash
35
        blockHeight int32
36

37
        // connected is true if this update is a new block and false if it is a
38
        // disconnected block.
39
        connect bool
40
}
41

42
// txUpdate encapsulates a transaction related notification sent from btcd to
43
// the registered RPC client. This struct is used as an element within an
44
// unbounded queue in order to avoid blocking the main rpc dispatch rule.
45
type txUpdate struct {
46
        tx      *btcutil.Tx
47
        details *btcjson.BlockDetails
48
}
49

50
// TODO(roasbeef): generalize struct below:
51
//  * move chans to config, allow outside callers to handle send conditions
52

53
// BtcdNotifier implements the ChainNotifier interface using btcd's websockets
54
// notifications. Multiple concurrent clients are supported. All notifications
55
// are achieved via non-blocking sends on client channels.
56
type BtcdNotifier struct {
57
        epochClientCounter uint64 // To be used atomically.
58

59
        start   sync.Once
60
        active  int32 // To be used atomically.
61
        stopped int32 // To be used atomically.
62

63
        chainConn   *chain.RPCClient
64
        chainParams *chaincfg.Params
65

66
        notificationCancels  chan interface{}
67
        notificationRegistry chan interface{}
68

69
        txNotifier *chainntnfs.TxNotifier
70

71
        blockEpochClients map[uint64]*blockEpochRegistration
72

73
        bestBlock chainntnfs.BlockEpoch
74

75
        // blockCache is a LRU block cache.
76
        blockCache *blockcache.BlockCache
77

78
        chainUpdates *queue.ConcurrentQueue
79
        txUpdates    *queue.ConcurrentQueue
80

81
        // spendHintCache is a cache used to query and update the latest height
82
        // hints for an outpoint. Each height hint represents the earliest
83
        // height at which the outpoint could have been spent within the chain.
84
        spendHintCache chainntnfs.SpendHintCache
85

86
        // confirmHintCache is a cache used to query the latest height hints for
87
        // a transaction. Each height hint represents the earliest height at
88
        // which the transaction could have confirmed within the chain.
89
        confirmHintCache chainntnfs.ConfirmHintCache
90

91
        // memNotifier notifies clients of events related to the mempool.
92
        memNotifier *chainntnfs.MempoolNotifier
93

94
        wg   sync.WaitGroup
95
        quit chan struct{}
96
}
97

98
// Ensure BtcdNotifier implements the ChainNotifier interface at compile time.
99
var _ chainntnfs.ChainNotifier = (*BtcdNotifier)(nil)
100

101
// Ensure BtcdNotifier implements the MempoolWatcher interface at compile time.
102
var _ chainntnfs.MempoolWatcher = (*BtcdNotifier)(nil)
103

104
// New returns a new BtcdNotifier instance. This function assumes the btcd node
105
// detailed in the passed configuration is already running, and willing to
106
// accept new websockets clients.
107
func New(config *rpcclient.ConnConfig, chainParams *chaincfg.Params,
108
        spendHintCache chainntnfs.SpendHintCache,
109
        confirmHintCache chainntnfs.ConfirmHintCache,
UNCOV
110
        blockCache *blockcache.BlockCache) (*BtcdNotifier, error) {
×
UNCOV
111

×
UNCOV
112
        notifier := &BtcdNotifier{
×
UNCOV
113
                chainParams: chainParams,
×
UNCOV
114

×
UNCOV
115
                notificationCancels:  make(chan interface{}),
×
UNCOV
116
                notificationRegistry: make(chan interface{}),
×
UNCOV
117

×
UNCOV
118
                blockEpochClients: make(map[uint64]*blockEpochRegistration),
×
UNCOV
119

×
UNCOV
120
                chainUpdates: queue.NewConcurrentQueue(10),
×
UNCOV
121
                txUpdates:    queue.NewConcurrentQueue(10),
×
UNCOV
122

×
UNCOV
123
                spendHintCache:   spendHintCache,
×
UNCOV
124
                confirmHintCache: confirmHintCache,
×
UNCOV
125

×
UNCOV
126
                blockCache:  blockCache,
×
UNCOV
127
                memNotifier: chainntnfs.NewMempoolNotifier(),
×
UNCOV
128

×
UNCOV
129
                quit: make(chan struct{}),
×
UNCOV
130
        }
×
UNCOV
131

×
UNCOV
132
        ntfnCallbacks := &rpcclient.NotificationHandlers{
×
UNCOV
133
                OnBlockConnected:    notifier.onBlockConnected,
×
UNCOV
134
                OnBlockDisconnected: notifier.onBlockDisconnected,
×
UNCOV
135
                OnRedeemingTx:       notifier.onRedeemingTx,
×
UNCOV
136
        }
×
UNCOV
137

×
UNCOV
138
        rpcCfg := &chain.RPCClientConfig{
×
UNCOV
139
                ReconnectAttempts:    20,
×
UNCOV
140
                Conn:                 config,
×
UNCOV
141
                Chain:                chainParams,
×
UNCOV
142
                NotificationHandlers: ntfnCallbacks,
×
UNCOV
143
        }
×
UNCOV
144

×
UNCOV
145
        chainRPC, err := chain.NewRPCClientWithConfig(rpcCfg)
×
UNCOV
146
        if err != nil {
×
147
                return nil, err
×
148
        }
×
149

UNCOV
150
        notifier.chainConn = chainRPC
×
UNCOV
151

×
UNCOV
152
        return notifier, nil
×
153
}
154

155
// Start connects to the running btcd node over websockets, registers for block
156
// notifications, and finally launches all related helper goroutines.
UNCOV
157
func (b *BtcdNotifier) Start() error {
×
UNCOV
158
        var startErr error
×
UNCOV
159
        b.start.Do(func() {
×
UNCOV
160
                startErr = b.startNotifier()
×
UNCOV
161
        })
×
162

UNCOV
163
        return startErr
×
164
}
165

166
// Started returns true if this instance has been started, and false otherwise.
UNCOV
167
func (b *BtcdNotifier) Started() bool {
×
UNCOV
168
        return atomic.LoadInt32(&b.active) != 0
×
UNCOV
169
}
×
170

171
// Stop shutsdown the BtcdNotifier.
UNCOV
172
func (b *BtcdNotifier) Stop() error {
×
UNCOV
173
        // Already shutting down?
×
UNCOV
174
        if atomic.AddInt32(&b.stopped, 1) != 1 {
×
175
                return nil
×
176
        }
×
177

UNCOV
178
        chainntnfs.Log.Info("btcd notifier shutting down...")
×
UNCOV
179
        defer chainntnfs.Log.Debug("btcd notifier shutdown complete")
×
UNCOV
180

×
UNCOV
181
        // Shutdown the rpc client, this gracefully disconnects from btcd, and
×
UNCOV
182
        // cleans up all related resources.
×
UNCOV
183
        b.chainConn.Stop()
×
UNCOV
184

×
UNCOV
185
        close(b.quit)
×
UNCOV
186
        b.wg.Wait()
×
UNCOV
187

×
UNCOV
188
        b.chainUpdates.Stop()
×
UNCOV
189
        b.txUpdates.Stop()
×
UNCOV
190

×
UNCOV
191
        // Notify all pending clients of our shutdown by closing the related
×
UNCOV
192
        // notification channels.
×
UNCOV
193
        for _, epochClient := range b.blockEpochClients {
×
UNCOV
194
                close(epochClient.cancelChan)
×
UNCOV
195
                epochClient.wg.Wait()
×
UNCOV
196

×
UNCOV
197
                close(epochClient.epochChan)
×
UNCOV
198
        }
×
UNCOV
199
        b.txNotifier.TearDown()
×
UNCOV
200

×
UNCOV
201
        // Stop the mempool notifier.
×
UNCOV
202
        b.memNotifier.TearDown()
×
UNCOV
203

×
UNCOV
204
        return nil
×
205
}
206

207
// startNotifier is the main starting point for the BtcdNotifier. It connects
208
// to btcd and start the main dispatcher goroutine.
UNCOV
209
func (b *BtcdNotifier) startNotifier() error {
×
UNCOV
210
        chainntnfs.Log.Infof("btcd notifier starting...")
×
UNCOV
211

×
UNCOV
212
        // Start our concurrent queues before starting the chain connection, to
×
UNCOV
213
        // ensure onBlockConnected and onRedeemingTx callbacks won't be
×
UNCOV
214
        // blocked.
×
UNCOV
215
        b.chainUpdates.Start()
×
UNCOV
216
        b.txUpdates.Start()
×
UNCOV
217

×
UNCOV
218
        // Connect to btcd, and register for notifications on connected, and
×
UNCOV
219
        // disconnected blocks.
×
UNCOV
220
        if err := b.chainConn.Connect(20); err != nil {
×
221
                b.txUpdates.Stop()
×
222
                b.chainUpdates.Stop()
×
223
                return err
×
224
        }
×
225

226
        // Before we fetch the best block/block height we need to register the
227
        // notifications for connected blocks, otherwise we might think we are
228
        // at an earlier block height because during block notification
229
        // registration we might have already mined some new blocks. Hence we
230
        // will not get notified accordingly.
UNCOV
231
        if err := b.chainConn.NotifyBlocks(); err != nil {
×
232
                b.txUpdates.Stop()
×
233
                b.chainUpdates.Stop()
×
234
                return err
×
235
        }
×
236

UNCOV
237
        currentHash, currentHeight, err := b.chainConn.GetBestBlock()
×
UNCOV
238
        if err != nil {
×
239
                b.txUpdates.Stop()
×
240
                b.chainUpdates.Stop()
×
241
                return err
×
242
        }
×
243

UNCOV
244
        bestBlock, err := b.chainConn.GetBlock(currentHash)
×
UNCOV
245
        if err != nil {
×
246
                b.txUpdates.Stop()
×
247
                b.chainUpdates.Stop()
×
248
                return err
×
249
        }
×
250

UNCOV
251
        b.txNotifier = chainntnfs.NewTxNotifier(
×
UNCOV
252
                uint32(currentHeight), chainntnfs.ReorgSafetyLimit,
×
UNCOV
253
                b.confirmHintCache, b.spendHintCache,
×
UNCOV
254
        )
×
UNCOV
255

×
UNCOV
256
        b.bestBlock = chainntnfs.BlockEpoch{
×
UNCOV
257
                Height:      currentHeight,
×
UNCOV
258
                Hash:        currentHash,
×
UNCOV
259
                BlockHeader: &bestBlock.Header,
×
UNCOV
260
        }
×
UNCOV
261

×
UNCOV
262
        b.wg.Add(1)
×
UNCOV
263
        go b.notificationDispatcher()
×
UNCOV
264

×
UNCOV
265
        // Set the active flag now that we've completed the full
×
UNCOV
266
        // startup.
×
UNCOV
267
        atomic.StoreInt32(&b.active, 1)
×
UNCOV
268

×
UNCOV
269
        chainntnfs.Log.Debugf("btcd notifier started")
×
UNCOV
270

×
UNCOV
271
        return nil
×
272
}
273

274
// onBlockConnected implements on OnBlockConnected callback for rpcclient.
275
// Ingesting a block updates the wallet's internal utxo state based on the
276
// outputs created and destroyed within each block.
UNCOV
277
func (b *BtcdNotifier) onBlockConnected(hash *chainhash.Hash, height int32, t time.Time) {
×
UNCOV
278
        // Append this new chain update to the end of the queue of new chain
×
UNCOV
279
        // updates.
×
UNCOV
280
        select {
×
281
        case b.chainUpdates.ChanIn() <- &chainUpdate{
282
                blockHash:   hash,
283
                blockHeight: height,
284
                connect:     true,
UNCOV
285
        }:
×
286
        case <-b.quit:
×
287
                return
×
288
        }
289
}
290

291
// filteredBlock represents a new block which has been connected to the main
292
// chain. The slice of transactions will only be populated if the block
293
// includes a transaction that confirmed one of our watched txids, or spends
294
// one of the outputs currently being watched.
295
//
296
// TODO(halseth): this is currently used for complete blocks. Change to use
297
// onFilteredBlockConnected and onFilteredBlockDisconnected, making it easier
298
// to unify with the Neutrino implementation.
299
type filteredBlock struct {
300
        hash   chainhash.Hash
301
        height uint32
302
        block  *btcutil.Block
303

304
        // connected is true if this update is a new block and false if it is a
305
        // disconnected block.
306
        connect bool
307
}
308

309
// onBlockDisconnected implements on OnBlockDisconnected callback for rpcclient.
UNCOV
310
func (b *BtcdNotifier) onBlockDisconnected(hash *chainhash.Hash, height int32, t time.Time) {
×
UNCOV
311
        // Append this new chain update to the end of the queue of new chain
×
UNCOV
312
        // updates.
×
UNCOV
313
        select {
×
314
        case b.chainUpdates.ChanIn() <- &chainUpdate{
315
                blockHash:   hash,
316
                blockHeight: height,
317
                connect:     false,
UNCOV
318
        }:
×
319
        case <-b.quit:
×
320
                return
×
321
        }
322
}
323

324
// onRedeemingTx implements on OnRedeemingTx callback for rpcclient.
UNCOV
325
func (b *BtcdNotifier) onRedeemingTx(tx *btcutil.Tx, details *btcjson.BlockDetails) {
×
UNCOV
326
        // Append this new transaction update to the end of the queue of new
×
UNCOV
327
        // chain updates.
×
UNCOV
328
        select {
×
UNCOV
329
        case b.txUpdates.ChanIn() <- &txUpdate{tx, details}:
×
330
        case <-b.quit:
×
331
                return
×
332
        }
333
}
334

335
// notificationDispatcher is the primary goroutine which handles client
336
// notification registrations, as well as notification dispatches.
UNCOV
337
func (b *BtcdNotifier) notificationDispatcher() {
×
UNCOV
338
        defer b.wg.Done()
×
UNCOV
339

×
UNCOV
340
out:
×
UNCOV
341
        for {
×
UNCOV
342
                select {
×
UNCOV
343
                case cancelMsg := <-b.notificationCancels:
×
UNCOV
344
                        switch msg := cancelMsg.(type) {
×
UNCOV
345
                        case *epochCancel:
×
UNCOV
346
                                chainntnfs.Log.Infof("Cancelling epoch "+
×
UNCOV
347
                                        "notification, epoch_id=%v", msg.epochID)
×
UNCOV
348

×
UNCOV
349
                                // First, we'll lookup the original
×
UNCOV
350
                                // registration in order to stop the active
×
UNCOV
351
                                // queue goroutine.
×
UNCOV
352
                                reg := b.blockEpochClients[msg.epochID]
×
UNCOV
353
                                reg.epochQueue.Stop()
×
UNCOV
354

×
UNCOV
355
                                // Next, close the cancel channel for this
×
UNCOV
356
                                // specific client, and wait for the client to
×
UNCOV
357
                                // exit.
×
UNCOV
358
                                close(b.blockEpochClients[msg.epochID].cancelChan)
×
UNCOV
359
                                b.blockEpochClients[msg.epochID].wg.Wait()
×
UNCOV
360

×
UNCOV
361
                                // Once the client has exited, we can then
×
UNCOV
362
                                // safely close the channel used to send epoch
×
UNCOV
363
                                // notifications, in order to notify any
×
UNCOV
364
                                // listeners that the intent has been
×
UNCOV
365
                                // canceled.
×
UNCOV
366
                                close(b.blockEpochClients[msg.epochID].epochChan)
×
UNCOV
367
                                delete(b.blockEpochClients, msg.epochID)
×
368
                        }
UNCOV
369
                case registerMsg := <-b.notificationRegistry:
×
UNCOV
370
                        switch msg := registerMsg.(type) {
×
UNCOV
371
                        case *chainntnfs.HistoricalConfDispatch:
×
UNCOV
372
                                // Look up whether the transaction/output script
×
UNCOV
373
                                // has already confirmed in the active chain.
×
UNCOV
374
                                // We'll do this in a goroutine to prevent
×
UNCOV
375
                                // blocking potentially long rescans.
×
UNCOV
376
                                //
×
UNCOV
377
                                // TODO(wilmer): add retry logic if rescan fails?
×
UNCOV
378
                                b.wg.Add(1)
×
UNCOV
379

×
UNCOV
380
                                //nolint:ll
×
UNCOV
381
                                go func(msg *chainntnfs.HistoricalConfDispatch) {
×
UNCOV
382
                                        defer b.wg.Done()
×
UNCOV
383

×
UNCOV
384
                                        confDetails, _, err := b.historicalConfDetails(
×
UNCOV
385
                                                msg.ConfRequest,
×
UNCOV
386
                                                msg.StartHeight, msg.EndHeight,
×
UNCOV
387
                                        )
×
UNCOV
388
                                        if err != nil {
×
389
                                                chainntnfs.Log.Error(err)
×
390
                                                return
×
391
                                        }
×
392

393
                                        // If the historical dispatch finished
394
                                        // without error, we will invoke
395
                                        // UpdateConfDetails even if none were
396
                                        // found. This allows the notifier to
397
                                        // begin safely updating the height hint
398
                                        // cache at tip, since any pending
399
                                        // rescans have now completed.
UNCOV
400
                                        err = b.txNotifier.UpdateConfDetails(
×
UNCOV
401
                                                msg.ConfRequest, confDetails,
×
UNCOV
402
                                        )
×
UNCOV
403
                                        if err != nil {
×
404
                                                chainntnfs.Log.Error(err)
×
405
                                        }
×
406
                                }(msg)
407

UNCOV
408
                        case *blockEpochRegistration:
×
UNCOV
409
                                chainntnfs.Log.Infof("New block epoch subscription")
×
UNCOV
410

×
UNCOV
411
                                b.blockEpochClients[msg.epochID] = msg
×
UNCOV
412

×
UNCOV
413
                                // If the client did not provide their best
×
UNCOV
414
                                // known block, then we'll immediately dispatch
×
UNCOV
415
                                // a notification for the current tip.
×
UNCOV
416
                                if msg.bestBlock == nil {
×
UNCOV
417
                                        b.notifyBlockEpochClient(
×
UNCOV
418
                                                msg, b.bestBlock.Height,
×
UNCOV
419
                                                b.bestBlock.Hash,
×
UNCOV
420
                                                b.bestBlock.BlockHeader,
×
UNCOV
421
                                        )
×
UNCOV
422

×
UNCOV
423
                                        msg.errorChan <- nil
×
UNCOV
424
                                        continue
×
425
                                }
426

427
                                // Otherwise, we'll attempt to deliver the
428
                                // backlog of notifications from their best
429
                                // known block.
UNCOV
430
                                missedBlocks, err := chainntnfs.GetClientMissedBlocks(
×
UNCOV
431
                                        b.chainConn, msg.bestBlock,
×
UNCOV
432
                                        b.bestBlock.Height, true,
×
UNCOV
433
                                )
×
UNCOV
434
                                if err != nil {
×
435
                                        msg.errorChan <- err
×
436
                                        continue
×
437
                                }
438

UNCOV
439
                                for _, block := range missedBlocks {
×
440
                                        b.notifyBlockEpochClient(
×
441
                                                msg, block.Height, block.Hash,
×
442
                                                block.BlockHeader,
×
443
                                        )
×
444
                                }
×
445

UNCOV
446
                                msg.errorChan <- nil
×
447
                        }
448

UNCOV
449
                case item := <-b.chainUpdates.ChanOut():
×
UNCOV
450
                        update := item.(*chainUpdate)
×
UNCOV
451
                        if update.connect {
×
UNCOV
452
                                blockHeader, err := b.chainConn.GetBlockHeader(
×
UNCOV
453
                                        update.blockHash,
×
UNCOV
454
                                )
×
UNCOV
455
                                if err != nil {
×
456
                                        chainntnfs.Log.Errorf("Unable to fetch "+
×
457
                                                "block header: %v", err)
×
458
                                        continue
×
459
                                }
460

UNCOV
461
                                if blockHeader.PrevBlock != *b.bestBlock.Hash {
×
UNCOV
462
                                        // Handle the case where the notifier
×
UNCOV
463
                                        // missed some blocks from its chain
×
UNCOV
464
                                        // backend
×
UNCOV
465
                                        chainntnfs.Log.Infof("Missed blocks, " +
×
UNCOV
466
                                                "attempting to catch up")
×
UNCOV
467
                                        newBestBlock, missedBlocks, err :=
×
UNCOV
468
                                                chainntnfs.HandleMissedBlocks(
×
UNCOV
469
                                                        b.chainConn,
×
UNCOV
470
                                                        b.txNotifier,
×
UNCOV
471
                                                        b.bestBlock,
×
UNCOV
472
                                                        update.blockHeight,
×
UNCOV
473
                                                        true,
×
UNCOV
474
                                                )
×
UNCOV
475
                                        if err != nil {
×
476
                                                // Set the bestBlock here in case
×
477
                                                // a catch up partially completed.
×
478
                                                b.bestBlock = newBestBlock
×
479
                                                chainntnfs.Log.Error(err)
×
480
                                                continue
×
481
                                        }
482

UNCOV
483
                                        for _, block := range missedBlocks {
×
484
                                                err := b.handleBlockConnected(block)
×
485
                                                if err != nil {
×
486
                                                        chainntnfs.Log.Error(err)
×
487
                                                        continue out
×
488
                                                }
489
                                        }
490
                                }
491

UNCOV
492
                                newBlock := chainntnfs.BlockEpoch{
×
UNCOV
493
                                        Height:      update.blockHeight,
×
UNCOV
494
                                        Hash:        update.blockHash,
×
UNCOV
495
                                        BlockHeader: blockHeader,
×
UNCOV
496
                                }
×
UNCOV
497
                                if err := b.handleBlockConnected(newBlock); err != nil {
×
498
                                        chainntnfs.Log.Error(err)
×
499
                                }
×
UNCOV
500
                                continue
×
501
                        }
502

UNCOV
503
                        if update.blockHeight != b.bestBlock.Height {
×
UNCOV
504
                                chainntnfs.Log.Infof("Missed disconnected" +
×
UNCOV
505
                                        "blocks, attempting to catch up")
×
UNCOV
506
                        }
×
507

UNCOV
508
                        newBestBlock, err := chainntnfs.RewindChain(
×
UNCOV
509
                                b.chainConn, b.txNotifier, b.bestBlock,
×
UNCOV
510
                                update.blockHeight-1,
×
UNCOV
511
                        )
×
UNCOV
512
                        if err != nil {
×
UNCOV
513
                                chainntnfs.Log.Errorf("Unable to rewind chain "+
×
UNCOV
514
                                        "from height %d to height %d: %v",
×
UNCOV
515
                                        b.bestBlock.Height, update.blockHeight-1, err)
×
UNCOV
516
                        }
×
517

518
                        // Set the bestBlock here in case a chain rewind
519
                        // partially completed.
UNCOV
520
                        b.bestBlock = newBestBlock
×
521

UNCOV
522
                case item := <-b.txUpdates.ChanOut():
×
UNCOV
523
                        newSpend := item.(*txUpdate)
×
UNCOV
524
                        tx := newSpend.tx
×
UNCOV
525

×
UNCOV
526
                        // Init values.
×
UNCOV
527
                        isMempool := false
×
UNCOV
528
                        height := uint32(0)
×
UNCOV
529

×
UNCOV
530
                        // Unwrap values.
×
UNCOV
531
                        if newSpend.details == nil {
×
UNCOV
532
                                isMempool = true
×
UNCOV
533
                        } else {
×
UNCOV
534
                                height = uint32(newSpend.details.Height)
×
UNCOV
535
                        }
×
536

537
                        // Handle the transaction.
UNCOV
538
                        b.handleRelevantTx(tx, isMempool, height)
×
539

UNCOV
540
                case <-b.quit:
×
UNCOV
541
                        break out
×
542
                }
543
        }
544
}
545

546
// handleRelevantTx handles a new transaction that has been seen either in a
547
// block or in the mempool. If in mempool, it will ask the mempool notifier to
548
// handle it. If in a block, it will ask the txNotifier to handle it, and
549
// cancel any relevant subscriptions made in the mempool.
550
func (b *BtcdNotifier) handleRelevantTx(tx *btcutil.Tx,
UNCOV
551
        mempool bool, height uint32) {
×
UNCOV
552

×
UNCOV
553
        // If this is a mempool spend, we'll ask the mempool notifier to handle
×
UNCOV
554
        // it.
×
UNCOV
555
        if mempool {
×
UNCOV
556
                err := b.memNotifier.ProcessRelevantSpendTx(tx)
×
UNCOV
557
                if err != nil {
×
558
                        chainntnfs.Log.Errorf("Unable to process transaction "+
×
559
                                "%v: %v", tx.Hash(), err)
×
560
                }
×
561

UNCOV
562
                return
×
563
        }
564

565
        // Otherwise this is a confirmed spend, and we'll ask the tx notifier
566
        // to handle it.
UNCOV
567
        err := b.txNotifier.ProcessRelevantSpendTx(tx, height)
×
UNCOV
568
        if err != nil {
×
569
                chainntnfs.Log.Errorf("Unable to process transaction %v: %v",
×
570
                        tx.Hash(), err)
×
571

×
572
                return
×
573
        }
×
574

575
        // Once the tx is processed, we will ask the memNotifier to unsubscribe
576
        // the input.
577
        //
578
        // NOTE(yy): we could build it into txNotifier.ProcessRelevantSpendTx,
579
        // but choose to implement it here so we can easily decouple the two
580
        // notifiers in the future.
UNCOV
581
        b.memNotifier.UnsubsribeConfirmedSpentTx(tx)
×
582
}
583

584
// historicalConfDetails looks up whether a confirmation request (txid/output
585
// script) has already been included in a block in the active chain and, if so,
586
// returns details about said block.
587
func (b *BtcdNotifier) historicalConfDetails(confRequest chainntnfs.ConfRequest,
588
        startHeight, endHeight uint32) (*chainntnfs.TxConfirmation,
UNCOV
589
        chainntnfs.TxConfStatus, error) {
×
UNCOV
590

×
UNCOV
591
        // If a txid was not provided, then we should dispatch upon seeing the
×
UNCOV
592
        // script on-chain, so we'll short-circuit straight to scanning manually
×
UNCOV
593
        // as there doesn't exist a script index to query.
×
UNCOV
594
        if confRequest.TxID == chainntnfs.ZeroHash {
×
595
                return b.confDetailsManually(
×
596
                        confRequest, startHeight, endHeight,
×
597
                )
×
598
        }
×
599

600
        // Otherwise, we'll dispatch upon seeing a transaction on-chain with the
601
        // given hash.
602
        //
603
        // We'll first attempt to retrieve the transaction using the node's
604
        // txindex.
UNCOV
605
        txNotFoundErr := "No information available about transaction"
×
UNCOV
606
        txConf, txStatus, err := chainntnfs.ConfDetailsFromTxIndex(
×
UNCOV
607
                b.chainConn, confRequest, txNotFoundErr,
×
UNCOV
608
        )
×
UNCOV
609

×
UNCOV
610
        // We'll then check the status of the transaction lookup returned to
×
UNCOV
611
        // determine whether we should proceed with any fallback methods.
×
UNCOV
612
        switch {
×
613

614
        // We failed querying the index for the transaction, fall back to
615
        // scanning manually.
616
        case err != nil:
×
617
                chainntnfs.Log.Debugf("Unable to determine confirmation of %v "+
×
618
                        "through the backend's txindex (%v), scanning manually",
×
619
                        confRequest.TxID, err)
×
620

×
621
                return b.confDetailsManually(
×
622
                        confRequest, startHeight, endHeight,
×
623
                )
×
624

625
        // The transaction was found within the node's mempool.
UNCOV
626
        case txStatus == chainntnfs.TxFoundMempool:
×
627

628
        // The transaction was found within the node's txindex.
UNCOV
629
        case txStatus == chainntnfs.TxFoundIndex:
×
630

631
        // The transaction was not found within the node's mempool or txindex.
UNCOV
632
        case txStatus == chainntnfs.TxNotFoundIndex:
×
633

634
        // Unexpected txStatus returned.
635
        default:
×
636
                return nil, txStatus,
×
637
                        fmt.Errorf("Got unexpected txConfStatus: %v", txStatus)
×
638
        }
639

UNCOV
640
        return txConf, txStatus, nil
×
641
}
642

643
// confDetailsManually looks up whether a transaction/output script has already
644
// been included in a block in the active chain by scanning the chain's blocks
645
// within the given range. If the transaction/output script is found, its
646
// confirmation details are returned. Otherwise, nil is returned.
647
func (b *BtcdNotifier) confDetailsManually(confRequest chainntnfs.ConfRequest,
648
        startHeight, endHeight uint32) (*chainntnfs.TxConfirmation,
649
        chainntnfs.TxConfStatus, error) {
×
650

×
651
        // Begin scanning blocks at every height to determine where the
×
652
        // transaction was included in.
×
653
        for height := endHeight; height >= startHeight && height > 0; height-- {
×
654
                // Ensure we haven't been requested to shut down before
×
655
                // processing the next height.
×
656
                select {
×
657
                case <-b.quit:
×
658
                        return nil, chainntnfs.TxNotFoundManually,
×
659
                                chainntnfs.ErrChainNotifierShuttingDown
×
660
                default:
×
661
                }
662

663
                blockHash, err := b.chainConn.GetBlockHash(int64(height))
×
664
                if err != nil {
×
665
                        return nil, chainntnfs.TxNotFoundManually,
×
666
                                fmt.Errorf("unable to get hash from block "+
×
667
                                        "with height %d", height)
×
668
                }
×
669

670
                // TODO: fetch the neutrino filters instead.
671
                block, err := b.GetBlock(blockHash)
×
672
                if err != nil {
×
673
                        return nil, chainntnfs.TxNotFoundManually,
×
674
                                fmt.Errorf("unable to get block with hash "+
×
675
                                        "%v: %v", blockHash, err)
×
676
                }
×
677

678
                // For every transaction in the block, check which one matches
679
                // our request. If we find one that does, we can dispatch its
680
                // confirmation details.
681
                for txIndex, tx := range block.Transactions {
×
682
                        if !confRequest.MatchesTx(tx) {
×
683
                                continue
×
684
                        }
685

686
                        return &chainntnfs.TxConfirmation{
×
687
                                Tx:          tx.Copy(),
×
688
                                BlockHash:   blockHash,
×
689
                                BlockHeight: height,
×
690
                                TxIndex:     uint32(txIndex),
×
691
                                Block:       block,
×
692
                        }, chainntnfs.TxFoundManually, nil
×
693
                }
694
        }
695

696
        // If we reach here, then we were not able to find the transaction
697
        // within a block, so we avoid returning an error.
698
        return nil, chainntnfs.TxNotFoundManually, nil
×
699
}
700

701
// handleBlockConnected applies a chain update for a new block. Any watched
702
// transactions included this block will processed to either send notifications
703
// now or after numConfirmations confs.
704
// TODO(halseth): this is reusing the neutrino notifier implementation, unify
705
// them.
UNCOV
706
func (b *BtcdNotifier) handleBlockConnected(epoch chainntnfs.BlockEpoch) error {
×
UNCOV
707
        // First, we'll fetch the raw block as we'll need to gather all the
×
UNCOV
708
        // transactions to determine whether any are relevant to our registered
×
UNCOV
709
        // clients.
×
UNCOV
710
        rawBlock, err := b.GetBlock(epoch.Hash)
×
UNCOV
711
        if err != nil {
×
712
                return fmt.Errorf("unable to get block: %w", err)
×
713
        }
×
UNCOV
714
        newBlock := &filteredBlock{
×
UNCOV
715
                hash:    *epoch.Hash,
×
UNCOV
716
                height:  uint32(epoch.Height),
×
UNCOV
717
                block:   btcutil.NewBlock(rawBlock),
×
UNCOV
718
                connect: true,
×
UNCOV
719
        }
×
UNCOV
720

×
UNCOV
721
        // We'll then extend the txNotifier's height with the information of
×
UNCOV
722
        // this new block, which will handle all of the notification logic for
×
UNCOV
723
        // us.
×
UNCOV
724
        err = b.txNotifier.ConnectTip(newBlock.block, newBlock.height)
×
UNCOV
725
        if err != nil {
×
726
                return fmt.Errorf("unable to connect tip: %w", err)
×
727
        }
×
728

UNCOV
729
        chainntnfs.Log.Infof("New block: height=%v, sha=%v", epoch.Height,
×
UNCOV
730
                epoch.Hash)
×
UNCOV
731

×
UNCOV
732
        // Now that we've guaranteed the new block extends the txNotifier's
×
UNCOV
733
        // current tip, we'll proceed to dispatch notifications to all of our
×
UNCOV
734
        // registered clients whom have had notifications fulfilled. Before
×
UNCOV
735
        // doing so, we'll make sure update our in memory state in order to
×
UNCOV
736
        // satisfy any client requests based upon the new block.
×
UNCOV
737
        b.bestBlock = epoch
×
UNCOV
738

×
UNCOV
739
        err = b.txNotifier.NotifyHeight(uint32(epoch.Height))
×
UNCOV
740
        if err != nil {
×
741
                return fmt.Errorf("unable to notify height: %w", err)
×
742
        }
×
743

UNCOV
744
        b.notifyBlockEpochs(
×
UNCOV
745
                epoch.Height, epoch.Hash, epoch.BlockHeader,
×
UNCOV
746
        )
×
UNCOV
747

×
UNCOV
748
        return nil
×
749
}
750

751
// notifyBlockEpochs notifies all registered block epoch clients of the newly
752
// connected block to the main chain.
753
func (b *BtcdNotifier) notifyBlockEpochs(newHeight int32,
UNCOV
754
        newSha *chainhash.Hash, blockHeader *wire.BlockHeader) {
×
UNCOV
755

×
UNCOV
756
        for _, client := range b.blockEpochClients {
×
UNCOV
757
                b.notifyBlockEpochClient(
×
UNCOV
758
                        client, newHeight, newSha, blockHeader,
×
UNCOV
759
                )
×
UNCOV
760
        }
×
761
}
762

763
// notifyBlockEpochClient sends a registered block epoch client a notification
764
// about a specific block.
765
func (b *BtcdNotifier) notifyBlockEpochClient(epochClient *blockEpochRegistration,
UNCOV
766
        height int32, sha *chainhash.Hash, blockHeader *wire.BlockHeader) {
×
UNCOV
767

×
UNCOV
768
        epoch := &chainntnfs.BlockEpoch{
×
UNCOV
769
                Height:      height,
×
UNCOV
770
                Hash:        sha,
×
UNCOV
771
                BlockHeader: blockHeader,
×
UNCOV
772
        }
×
UNCOV
773

×
UNCOV
774
        select {
×
UNCOV
775
        case epochClient.epochQueue.ChanIn() <- epoch:
×
776
        case <-epochClient.cancelChan:
×
777
        case <-b.quit:
×
778
        }
779
}
780

781
// RegisterSpendNtfn registers an intent to be notified once the target
782
// outpoint/output script has been spent by a transaction on-chain. When
783
// intending to be notified of the spend of an output script, a nil outpoint
784
// must be used. The heightHint should represent the earliest height in the
785
// chain of the transaction that spent the outpoint/output script.
786
//
787
// Once a spend of has been detected, the details of the spending event will be
788
// sent across the 'Spend' channel.
789
func (b *BtcdNotifier) RegisterSpendNtfn(outpoint *wire.OutPoint,
UNCOV
790
        pkScript []byte, heightHint uint32) (*chainntnfs.SpendEvent, error) {
×
UNCOV
791

×
UNCOV
792
        // Register the conf notification with the TxNotifier. A non-nil value
×
UNCOV
793
        // for `dispatch` will be returned if we are required to perform a
×
UNCOV
794
        // manual scan for the confirmation. Otherwise the notifier will begin
×
UNCOV
795
        // watching at tip for the transaction to confirm.
×
UNCOV
796
        ntfn, err := b.txNotifier.RegisterSpend(outpoint, pkScript, heightHint)
×
UNCOV
797
        if err != nil {
×
UNCOV
798
                return nil, err
×
UNCOV
799
        }
×
800

801
        // We'll then request the backend to notify us when it has detected the
802
        // outpoint/output script as spent.
803
        //
804
        // TODO(wilmer): use LoadFilter API instead.
UNCOV
805
        if outpoint == nil || *outpoint == chainntnfs.ZeroOutPoint {
×
806
                _, addrs, _, err := txscript.ExtractPkScriptAddrs(
×
807
                        pkScript, b.chainParams,
×
808
                )
×
809
                if err != nil {
×
810
                        return nil, fmt.Errorf("unable to parse script: %w",
×
811
                                err)
×
812
                }
×
813
                if err := b.chainConn.NotifyReceived(addrs); err != nil {
×
814
                        return nil, err
×
815
                }
×
UNCOV
816
        } else {
×
UNCOV
817
                ops := []*wire.OutPoint{outpoint}
×
UNCOV
818
                if err := b.chainConn.NotifySpent(ops); err != nil {
×
819
                        return nil, err
×
820
                }
×
821
        }
822

823
        // If the txNotifier didn't return any details to perform a historical
824
        // scan of the chain, then we can return early as there's nothing left
825
        // for us to do.
UNCOV
826
        if ntfn.HistoricalDispatch == nil {
×
UNCOV
827
                return ntfn.Event, nil
×
UNCOV
828
        }
×
829

830
        // Otherwise, we'll need to dispatch a historical rescan to determine if
831
        // the outpoint was already spent at a previous height.
832
        //
833
        // We'll short-circuit the path when dispatching the spend of a script,
834
        // rather than an outpoint, as there aren't any additional checks we can
835
        // make for scripts.
UNCOV
836
        if outpoint == nil || *outpoint == chainntnfs.ZeroOutPoint {
×
837
                startHash, err := b.chainConn.GetBlockHash(
×
838
                        int64(ntfn.HistoricalDispatch.StartHeight),
×
839
                )
×
840
                if err != nil {
×
841
                        return nil, err
×
842
                }
×
843

844
                // TODO(wilmer): add retry logic if rescan fails?
845
                _, addrs, _, err := txscript.ExtractPkScriptAddrs(
×
846
                        pkScript, b.chainParams,
×
847
                )
×
848
                if err != nil {
×
849
                        return nil, fmt.Errorf("unable to parse address: %w",
×
850
                                err)
×
851
                }
×
852

853
                asyncResult := b.chainConn.RescanAsync(startHash, addrs, nil)
×
854
                go func() {
×
855
                        if rescanErr := asyncResult.Receive(); rescanErr != nil {
×
856
                                chainntnfs.Log.Errorf("Rescan to determine "+
×
857
                                        "the spend details of %v failed: %v",
×
858
                                        ntfn.HistoricalDispatch.SpendRequest,
×
859
                                        rescanErr)
×
860
                        }
×
861
                }()
862

863
                return ntfn.Event, nil
×
864
        }
865

866
        // When dispatching spends of outpoints, there are a number of checks we
867
        // can make to start our rescan from a better height or completely avoid
868
        // it.
869
        //
870
        // We'll start by checking the backend's UTXO set to determine whether
871
        // the outpoint has been spent. If it hasn't, we can return to the
872
        // caller as well.
UNCOV
873
        txOut, err := b.chainConn.GetTxOut(&outpoint.Hash, outpoint.Index, true)
×
UNCOV
874
        if err != nil {
×
875
                return nil, err
×
876
        }
×
UNCOV
877
        if txOut != nil {
×
UNCOV
878
                // We'll let the txNotifier know the outpoint is still unspent
×
UNCOV
879
                // in order to begin updating its spend hint.
×
UNCOV
880
                err := b.txNotifier.UpdateSpendDetails(
×
UNCOV
881
                        ntfn.HistoricalDispatch.SpendRequest, nil,
×
UNCOV
882
                )
×
UNCOV
883
                if err != nil {
×
884
                        return nil, err
×
885
                }
×
886

UNCOV
887
                return ntfn.Event, nil
×
888
        }
889

890
        // Since the outpoint was spent, as it no longer exists within the UTXO
891
        // set, we'll determine when it happened by scanning the chain. We'll
892
        // begin by fetching the block hash of our starting height.
UNCOV
893
        startHash, err := b.chainConn.GetBlockHash(
×
UNCOV
894
                int64(ntfn.HistoricalDispatch.StartHeight),
×
UNCOV
895
        )
×
UNCOV
896
        if err != nil {
×
897
                return nil, fmt.Errorf("unable to get block hash for height "+
×
898
                        "%d: %v", ntfn.HistoricalDispatch.StartHeight, err)
×
899
        }
×
900

901
        // As a minimal optimization, we'll query the backend's transaction
902
        // index (if enabled) to determine if we have a better rescan starting
903
        // height. We can do this as the GetRawTransaction call will return the
904
        // hash of the block it was included in within the chain.
UNCOV
905
        tx, err := b.chainConn.GetRawTransactionVerbose(&outpoint.Hash)
×
UNCOV
906
        if err != nil {
×
UNCOV
907
                // Avoid returning an error if the transaction was not found to
×
UNCOV
908
                // proceed with fallback methods.
×
UNCOV
909
                jsonErr, ok := err.(*btcjson.RPCError)
×
UNCOV
910
                if !ok || jsonErr.Code != btcjson.ErrRPCNoTxInfo {
×
911
                        return nil, fmt.Errorf("unable to query for txid %v: "+
×
912
                                "%w", outpoint.Hash, err)
×
913
                }
×
914
        }
915

916
        // If the transaction index was enabled, we'll use the block's hash to
917
        // retrieve its height and check whether it provides a better starting
918
        // point for our rescan.
UNCOV
919
        if tx != nil {
×
UNCOV
920
                // If the transaction containing the outpoint hasn't confirmed
×
UNCOV
921
                // on-chain, then there's no need to perform a rescan.
×
UNCOV
922
                if tx.BlockHash == "" {
×
UNCOV
923
                        return ntfn.Event, nil
×
UNCOV
924
                }
×
925

UNCOV
926
                blockHash, err := chainhash.NewHashFromStr(tx.BlockHash)
×
UNCOV
927
                if err != nil {
×
928
                        return nil, err
×
929
                }
×
UNCOV
930
                blockHeader, err := b.chainConn.GetBlockHeaderVerbose(blockHash)
×
UNCOV
931
                if err != nil {
×
932
                        return nil, fmt.Errorf("unable to get header for "+
×
933
                                "block %v: %v", blockHash, err)
×
934
                }
×
935

UNCOV
936
                spentHeight := uint32(blockHeader.Height)
×
UNCOV
937
                chainntnfs.Log.Debugf("Outpoint(%v) has spent at height %v",
×
UNCOV
938
                        outpoint, spentHeight)
×
UNCOV
939

×
UNCOV
940
                // Since the tx has already been spent at spentHeight, the
×
UNCOV
941
                // heightHint specified by the caller is no longer relevant. We
×
UNCOV
942
                // now update the starting height to be the spent height to make
×
UNCOV
943
                // sure we won't miss it in the rescan.
×
UNCOV
944
                if spentHeight != ntfn.HistoricalDispatch.StartHeight {
×
UNCOV
945
                        startHash, err = b.chainConn.GetBlockHash(
×
UNCOV
946
                                int64(spentHeight),
×
UNCOV
947
                        )
×
UNCOV
948
                        if err != nil {
×
949
                                return nil, fmt.Errorf("unable to get block "+
×
950
                                        "hash for height %d: %v",
×
951
                                        blockHeader.Height, err)
×
952
                        }
×
953

UNCOV
954
                        ntfn.HistoricalDispatch.StartHeight = spentHeight
×
955
                }
956
        }
957

958
        // Now that we've determined the best starting point for our rescan,
959
        // we can go ahead and dispatch it.
960
        //
961
        // In order to ensure that we don't block the caller on what may be a
962
        // long rescan, we'll launch a new goroutine to handle the async result
963
        // of the rescan. We purposefully prevent from adding this goroutine to
964
        // the WaitGroup as we cannot wait for a quit signal due to the
965
        // asyncResult channel not being exposed.
966
        //
967
        // TODO(wilmer): add retry logic if rescan fails?
UNCOV
968
        asyncResult := b.chainConn.RescanAsync(
×
UNCOV
969
                startHash, nil, []*wire.OutPoint{outpoint},
×
UNCOV
970
        )
×
UNCOV
971
        go func() {
×
UNCOV
972
                if rescanErr := asyncResult.Receive(); rescanErr != nil {
×
973
                        chainntnfs.Log.Errorf("Rescan to determine the spend "+
×
974
                                "details of %v failed: %v", outpoint, rescanErr)
×
975
                }
×
976
        }()
977

UNCOV
978
        return ntfn.Event, nil
×
979
}
980

981
// RegisterConfirmationsNtfn registers an intent to be notified once the target
982
// txid/output script has reached numConfs confirmations on-chain. When
983
// intending to be notified of the confirmation of an output script, a nil txid
984
// must be used. The heightHint should represent the earliest height at which
985
// the txid/output script could have been included in the chain.
986
//
987
// Progress on the number of confirmations left can be read from the 'Updates'
988
// channel. Once it has reached all of its confirmations, a notification will be
989
// sent across the 'Confirmed' channel.
990
func (b *BtcdNotifier) RegisterConfirmationsNtfn(txid *chainhash.Hash,
991
        pkScript []byte, numConfs, heightHint uint32,
UNCOV
992
        opts ...chainntnfs.NotifierOption) (*chainntnfs.ConfirmationEvent, error) {
×
UNCOV
993

×
UNCOV
994
        // Register the conf notification with the TxNotifier. A non-nil value
×
UNCOV
995
        // for `dispatch` will be returned if we are required to perform a
×
UNCOV
996
        // manual scan for the confirmation. Otherwise the notifier will begin
×
UNCOV
997
        // watching at tip for the transaction to confirm.
×
UNCOV
998
        ntfn, err := b.txNotifier.RegisterConf(
×
UNCOV
999
                txid, pkScript, numConfs, heightHint, opts...,
×
UNCOV
1000
        )
×
UNCOV
1001
        if err != nil {
×
1002
                return nil, err
×
1003
        }
×
1004

UNCOV
1005
        if ntfn.HistoricalDispatch == nil {
×
UNCOV
1006
                return ntfn.Event, nil
×
UNCOV
1007
        }
×
1008

UNCOV
1009
        select {
×
UNCOV
1010
        case b.notificationRegistry <- ntfn.HistoricalDispatch:
×
UNCOV
1011
                return ntfn.Event, nil
×
1012
        case <-b.quit:
×
1013
                return nil, chainntnfs.ErrChainNotifierShuttingDown
×
1014
        }
1015
}
1016

1017
// blockEpochRegistration represents a client's intent to receive a
1018
// notification with each newly connected block.
1019
type blockEpochRegistration struct {
1020
        epochID uint64
1021

1022
        epochChan chan *chainntnfs.BlockEpoch
1023

1024
        epochQueue *queue.ConcurrentQueue
1025

1026
        bestBlock *chainntnfs.BlockEpoch
1027

1028
        errorChan chan error
1029

1030
        cancelChan chan struct{}
1031

1032
        wg sync.WaitGroup
1033
}
1034

1035
// epochCancel is a message sent to the BtcdNotifier when a client wishes to
1036
// cancel an outstanding epoch notification that has yet to be dispatched.
1037
type epochCancel struct {
1038
        epochID uint64
1039
}
1040

1041
// RegisterBlockEpochNtfn returns a BlockEpochEvent which subscribes the
1042
// caller to receive notifications, of each new block connected to the main
1043
// chain. Clients have the option of passing in their best known block, which
1044
// the notifier uses to check if they are behind on blocks and catch them up. If
1045
// they do not provide one, then a notification will be dispatched immediately
1046
// for the current tip of the chain upon a successful registration.
1047
func (b *BtcdNotifier) RegisterBlockEpochNtfn(
UNCOV
1048
        bestBlock *chainntnfs.BlockEpoch) (*chainntnfs.BlockEpochEvent, error) {
×
UNCOV
1049

×
UNCOV
1050
        reg := &blockEpochRegistration{
×
UNCOV
1051
                epochQueue: queue.NewConcurrentQueue(20),
×
UNCOV
1052
                epochChan:  make(chan *chainntnfs.BlockEpoch, 20),
×
UNCOV
1053
                cancelChan: make(chan struct{}),
×
UNCOV
1054
                epochID:    atomic.AddUint64(&b.epochClientCounter, 1),
×
UNCOV
1055
                bestBlock:  bestBlock,
×
UNCOV
1056
                errorChan:  make(chan error, 1),
×
UNCOV
1057
        }
×
UNCOV
1058

×
UNCOV
1059
        reg.epochQueue.Start()
×
UNCOV
1060

×
UNCOV
1061
        // Before we send the request to the main goroutine, we'll launch a new
×
UNCOV
1062
        // goroutine to proxy items added to our queue to the client itself.
×
UNCOV
1063
        // This ensures that all notifications are received *in order*.
×
UNCOV
1064
        reg.wg.Add(1)
×
UNCOV
1065
        go func() {
×
UNCOV
1066
                defer reg.wg.Done()
×
UNCOV
1067

×
UNCOV
1068
                for {
×
UNCOV
1069
                        select {
×
UNCOV
1070
                        case ntfn := <-reg.epochQueue.ChanOut():
×
UNCOV
1071
                                blockNtfn := ntfn.(*chainntnfs.BlockEpoch)
×
UNCOV
1072
                                select {
×
UNCOV
1073
                                case reg.epochChan <- blockNtfn:
×
1074

UNCOV
1075
                                case <-reg.cancelChan:
×
UNCOV
1076
                                        return
×
1077

1078
                                case <-b.quit:
×
1079
                                        return
×
1080
                                }
1081

UNCOV
1082
                        case <-reg.cancelChan:
×
UNCOV
1083
                                return
×
1084

UNCOV
1085
                        case <-b.quit:
×
UNCOV
1086
                                return
×
1087
                        }
1088
                }
1089
        }()
1090

UNCOV
1091
        select {
×
1092
        case <-b.quit:
×
1093
                // As we're exiting before the registration could be sent,
×
1094
                // we'll stop the queue now ourselves.
×
1095
                reg.epochQueue.Stop()
×
1096

×
1097
                return nil, errors.New("chainntnfs: system interrupt while " +
×
1098
                        "attempting to register for block epoch notification.")
×
UNCOV
1099
        case b.notificationRegistry <- reg:
×
UNCOV
1100
                return &chainntnfs.BlockEpochEvent{
×
UNCOV
1101
                        Epochs: reg.epochChan,
×
UNCOV
1102
                        Cancel: func() {
×
UNCOV
1103
                                cancel := &epochCancel{
×
UNCOV
1104
                                        epochID: reg.epochID,
×
UNCOV
1105
                                }
×
UNCOV
1106

×
UNCOV
1107
                                // Submit epoch cancellation to notification dispatcher.
×
UNCOV
1108
                                select {
×
UNCOV
1109
                                case b.notificationCancels <- cancel:
×
UNCOV
1110
                                        // Cancellation is being handled, drain
×
UNCOV
1111
                                        // the epoch channel until it is closed
×
UNCOV
1112
                                        // before yielding to caller.
×
UNCOV
1113
                                        for {
×
UNCOV
1114
                                                select {
×
UNCOV
1115
                                                case _, ok := <-reg.epochChan:
×
UNCOV
1116
                                                        if !ok {
×
UNCOV
1117
                                                                return
×
UNCOV
1118
                                                        }
×
1119
                                                case <-b.quit:
×
1120
                                                        return
×
1121
                                                }
1122
                                        }
UNCOV
1123
                                case <-b.quit:
×
1124
                                }
1125
                        },
1126
                }, nil
1127
        }
1128
}
1129

1130
// GetBlock is used to retrieve the block with the given hash. This function
1131
// wraps the blockCache's GetBlock function.
1132
func (b *BtcdNotifier) GetBlock(hash *chainhash.Hash) (*wire.MsgBlock,
UNCOV
1133
        error) {
×
UNCOV
1134

×
UNCOV
1135
        return b.blockCache.GetBlock(hash, b.chainConn.GetBlock)
×
UNCOV
1136
}
×
1137

1138
// SubscribeMempoolSpent allows the caller to register a subscription to watch
1139
// for a spend of an outpoint in the mempool.The event will be dispatched once
1140
// the outpoint is spent in the mempool.
1141
//
1142
// NOTE: part of the MempoolWatcher interface.
1143
func (b *BtcdNotifier) SubscribeMempoolSpent(
UNCOV
1144
        outpoint wire.OutPoint) (*chainntnfs.MempoolSpendEvent, error) {
×
UNCOV
1145

×
UNCOV
1146
        event := b.memNotifier.SubscribeInput(outpoint)
×
UNCOV
1147

×
UNCOV
1148
        ops := []*wire.OutPoint{&outpoint}
×
UNCOV
1149

×
UNCOV
1150
        return event, b.chainConn.NotifySpent(ops)
×
UNCOV
1151
}
×
1152

1153
// CancelMempoolSpendEvent allows the caller to cancel a subscription to watch
1154
// for a spend of an outpoint in the mempool.
1155
//
1156
// NOTE: part of the MempoolWatcher interface.
1157
func (b *BtcdNotifier) CancelMempoolSpendEvent(
UNCOV
1158
        sub *chainntnfs.MempoolSpendEvent) {
×
UNCOV
1159

×
UNCOV
1160
        b.memNotifier.UnsubscribeEvent(sub)
×
UNCOV
1161
}
×
1162

1163
// LookupInputMempoolSpend takes an outpoint and queries the mempool to find
1164
// its spending tx. Returns the tx if found, otherwise fn.None.
1165
//
1166
// NOTE: part of the MempoolWatcher interface.
1167
func (b *BtcdNotifier) LookupInputMempoolSpend(
UNCOV
1168
        op wire.OutPoint) fn.Option[wire.MsgTx] {
×
UNCOV
1169

×
UNCOV
1170
        // Find the spending txid.
×
UNCOV
1171
        txid, found := b.chainConn.LookupInputMempoolSpend(op)
×
UNCOV
1172
        if !found {
×
UNCOV
1173
                return fn.None[wire.MsgTx]()
×
UNCOV
1174
        }
×
1175

1176
        // Query the spending tx using the id.
UNCOV
1177
        tx, err := b.chainConn.GetRawTransaction(&txid)
×
UNCOV
1178
        if err != nil {
×
1179
                // TODO(yy): enable logging errors in this package.
×
1180
                return fn.None[wire.MsgTx]()
×
1181
        }
×
1182

UNCOV
1183
        return fn.Some(*tx.MsgTx().Copy())
×
1184
}
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